Home
Analytics
How to give colors for the categories of pie chart
gowthamkar
Hi All,<br />
<br />
In a report i am using many pie charts. In that the categories are Excellent, Good, Satisfactory and Poor.<br />
I want to give different colors for these categories <br />
<br />
ie: excellent - blue ; good - red ; satisfactory - green <br />
<br />
<strong class='bbc'>values for chart 1</strong><br />
<br />
excellent - 10<br />
good - 20<br />
satisfactory - 1<br />
<br />
In this case since all the categories having values there is no problem.<br />
<br />
<br />
<strong class='bbc'>values for chart 2</strong><br />
<br />
excellent - 13<br />
good - 0<br />
satisfactory - 11<br />
<br />
<br />
Here the value for Good is '0'. Because of this the color of "Good" is assigned to "satisfactory" and "satisfactory" color to "poor".<br />
<br />
I didn't know how to fix this. <br />
<br />
Please give me a solution for this problem. For reference i have attached the screen shot.<br />
even though any of the category value is '0' the color of remaining categories should not be changed.
Find more posts tagged with
Comments
mwilliams
What does your data look like in your dataSet? Is it not set up in a way where you can use the field of Timeliness, Knowledge, etc as the optional y-series grouping, rather than creating a separate chart for it? If you are able to make it with the grouping setup, the colors will match automatically.<br />
<br />
If you're unable to do that, you can put something like this in the script of all of your charts to get the colors to match up:<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
function beforeDrawDataPoint( dph, fill, icsc )
{
if (dph.getBaseDisplayValue() == "Excellent"){
fill.set(0,0,255);
}
else if (dph.getBaseDisplayValue() == "Good"){
fill.set(255,0,0);
}
else{
fill.set(0,255,0);
}
}
function beforeDrawLegendItem( lerh, bounds, icsc )
{
if (lerh.getLabel().getCaption().getValue() == "Excellent"){
lerh.getFill().set(0,0,255);
}
else if (lerh.getLabel().getCaption().getValue() == "Good"){
lerh.getFill().set(255,0,0);
}
else{
lerh.getFill().set(0,255,0);
}
}
</pre>
gunasekar
Hi william,
Thanks for ur reply and i have solved the problem using script as you said.
mwilliams
Not a problem. Let us know whenever you have questions!
gunasekar
<blockquote class='ipsBlockquote' data-author="'mwilliams'" data-cid="79817" data-time="1310133758" data-date="08 July 2011 - 07:02 AM"><p>
Not a problem. Let us know whenever you have questions!<br /></p></blockquote>
gunasekar
Sure William
sairag
Hey Wlliams, Thanks a lot !
AR
mwilliams
You're welcome!
Dude34
Hi Williams,<br />
<br />
I have exactly the same kind of need, I have an X grouping value rendering "OK", "KO", "Unqualified".<br />
I would like the "OK" group to be always green, the "KO" red, etc..<br />
But if I have 0% in one group, the next one just take the first available color in series palette, and it won't match.<br />
<br />
I tried to use your script, but it seems to not work on BIRT 3.7.2, as the Fill.set(X,X,X) method does not exists anymore.<br />
<br />
Is there a new way to do that stuff ?<br />
<br />
Thanks,<br />
Dude.<br />
<br />
<blockquote class='ipsBlockquote' data-author="'mwilliams'" data-cid="79773" data-time="1310067841" data-date="07 July 2011 - 12:44 PM"><p>
What does your data look like in your dataSet? Is it not set up in a way where you can use the field of Timeliness, Knowledge, etc as the optional y-series grouping, rather than creating a separate chart for it? If you are able to make it with the grouping setup, the colors will match automatically.<br />
<br />
If you're unable to do that, you can put something like this in the script of all of your charts to get the colors to match up:<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
function beforeDrawDataPoint( dph, fill, icsc )
{
if (dph.getBaseDisplayValue() == "Excellent"){
fill.set(0,0,255);
}
else if (dph.getBaseDisplayValue() == "Good"){
fill.set(255,0,0);
}
else{
fill.set(0,255,0);
}
}
function beforeDrawLegendItem( lerh, bounds, icsc )
{
if (lerh.getLabel().getCaption().getValue() == "Excellent"){
lerh.getFill().set(0,0,255);
}
else if (lerh.getLabel().getCaption().getValue() == "Good"){
lerh.getFill().set(255,0,0);
}
else{
lerh.getFill().set(0,255,0);
}
}
</pre></p></blockquote>
mwilliams
I don't have a problem with fill.set(r,g,b ), in 3.7.2. Can you reproduce the issue you're having in a report that I can run? This way I can see what you're doing. Thanks.
Dude34
<blockquote class='ipsBlockquote' data-author="'mwilliams'" data-cid="114543" data-time="1361908523" data-date="26 February 2013 - 12:55 PM"><p>
I don't have a problem with fill.set(r,g,B), in 3.7.2. Can you reproduce the issue you're having in a report that I can run? This way I can see what you're doing. Thanks.<br /></p></blockquote>
<br />
Ok, I realize it was not the version but because I use gradient which have a startColor and an endColor.<br />
Anyway, the following code will work with gradient colors in palette :<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
function setGradientColorByValue( value, fill )
{
importPackage( org.eclipse.birt.chart.model.attribute );
importPackage( org.eclipse.birt.chart.model.attribute.impl );
var startColor = null;
var endColor = null;
if (value == "Approuv?") {
startColor = ColorDefinitionImpl.create(51, 153, 102);
endColor = ColorDefinitionImpl.create(88, 226, 115);
} else if (value == "KO") {
startColor = ColorDefinitionImpl.create(255, 153, 0);
endColor = ColorDefinitionImpl.create(255, 196, 8);
} else if (value == "Non qualifi?") {
startColor = ColorDefinitionImpl.create(128, 128, 128);
endColor = ColorDefinitionImpl.create(153, 204, 255);
} else {
startColor = ColorDefinitionImpl.BLACK();
endColor = ColorDefinitionImpl.WHITE();
}
if( fill.getClass().isAssignableFrom(GradientImpl)){
fill.setStartColor(startColor);
fill.setEndColor(endColor);
}
}
/**
* Called before drawing each datapoint graphical representation or marker.
*
*
@param
dph
* DataPointHints
*
@param
fill
* Fill
*
@param
icsc
* IChartScriptContext
*/
function beforeDrawDataPoint( dph, fill, icsc )
{
setGradientColorByValue( dph.getBaseValue(), fill );
}
/**
* Called before drawing the legend item.
*
*
@param
lerh
* LegendEntryRenderingHints
*
@param
bounds
* Bounds
*
@param
icsc
* IChartScriptContext
*
@since
Version 2.2.0
*/
function beforeDrawLegendItem( lerh, bounds, icsc )
{
setGradientColorByValue( lerh.getLabel().getCaption().getValue(), lerh.getFill() );
}
</pre>
mwilliams
That was going to be my next question.
Glad you got it working! Let us know whenever you have questions.