How to code an xml array correctly

Hi,

I want to give to my pie chart a colors array, but I do not know how to code it correctly.
What I tried (but did not work of course) :


<chart:serialChart id="chart"
                   categoryField="year"
                   creditsPosition="bottom-left"
                   height="100%"
                   width="100%"
                  <b> colors="&#91;"#42f486", "#ea36f7"&#93;"</b>>

And I got an other question. how to disable the text next to the chart?
Like here , the “New” and the “Returning” text?
I have a legend part next to the pie chart and I think it do not looks well if texts are twice.

Thanks

Okay, I could solve the second problem with that: labelsEnabled=“false”

For the other problem, to set the pieChart’s slices colors, I found in documentation, that I can set in the Controller. So I inserted this to my controller:

@Inject
private Chart piechart;

@Override
public void init(Map<String, Object> params) {
	
	piechart.setColors(Arrays.asList(
            Color.valueOf("#446493"),
            Color.valueOf("#5E3D2C"),
            Color.valueOf("#D0A557")));
}

But I get an error: setColors is undefined…

Okay I could make it with that:

((PieChart) piechart).setColors(Arrays.asList(
	            Color.valueOf("#18db38"),
	            Color.valueOf("#ce1616")));

Hi,
If you want to set colors declaratively use colors tag instead of property:

<chart:serialChart height="100%"
                   width="100%"
                   categoryField="id">
    <chart:colors>
        <chart:color value="BLUE"/>
        <chart:color value="GREEN"/>
    </chart:colors>
    <chart:data>
        <chart:item>
            <chart:property name="id" value="1" type="int"/>
            <chart:property name="value1" value="1" type="int"/>
            <chart:property name="value2" value="4" type="int"/>
        </chart:item>
        <chart:item>
            <chart:property name="id" value="2" type="int"/>
            <chart:property name="value1" value="4" type="int"/>
            <chart:property name="value2" value="3" type="int"/>
        </chart:item>
        <chart:item>
            <chart:property name="id" value="3" type="int"/>
            <chart:property name="value1" value="3" type="int"/>
            <chart:property name="value2" value="9" type="int"/>
        </chart:item>
    </chart:data>
    <chart:graphs>
        <chart:graph valueField="value1"/>
        <chart:graph valueField="value2"/>
    </chart:graphs>
</chart:serialChart>

In case of programmatic changes I recommend that you inject your chart using typed reference, for instance:

@Inject
private SerialChart myChart;
...
myChart.setColors(Arrays.asList(
        Color.AQUAMARINE, 
        Color.ALICEBLUE)
);

If you use Intellij Idea IDE, you can simply use Code - Generate - Inject menu (or Alt+Insert shortcut) to insert fields with @Inject annotation.

1 Like