Changing color of label value on bar/ pie charts

0

I want to know if we can change color of the label values on the bar charts. Not the x axis and Y axis labels but the actual value displaying on the bar.

 

Thanks in advance for all help.

ldesaraju's picture
Joined: Mar 2 2011 - 7:54am
Last seen: 3 years 7 months ago

8 Answers:

0

Hi, If you are talking about item labels, their color can be set in the <itemLabel> element in the barPlot: <itemLabel color="#FF0000" backgroundColor="#00FFFF"/> Hope this helps, sanda



Post Edited by shertage at 03/04/2011 16:11
shertage's picture
782
Joined: Sep 26 2006 - 8:06pm
Last seen: 3 months 1 week ago
0

Thank You Sanda.

In fact, I am talking about Item label color.

I tried the option you mentioned even before posting. Somehow , the changes I applied seem to occur on pie charts but not bar charts. Do we have any restriction for bar charts.

General Item label color was black Initially. My background color is dark and hence , I am uanle to see the actual label being printed on bar. I wanted to change to white which did not work on bar graph. I clicked thr Item Label on Bar3D plot properties on Ireport and changed the color to white. It did not work.

 

If I change my color to white, what would my background color be?Can it be white? Does that background color has any significance since on bar chart it usually is taken up by series color? Am I making sense...:)

 

ldesaraju's picture
Joined: Mar 2 2011 - 7:54am
Last seen: 3 years 7 months ago
0

Hi, Bar3D charts need an additional setting for item label positions. This setting can be customized in a chart customizer: CategoryPlot categoryPlot = (CategoryPlot)jfreeChart.getPlot(); categoryPlot.getRenderer().setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER)); Hope this helps, sanda

shertage's picture
782
Joined: Sep 26 2006 - 8:06pm
Last seen: 3 months 1 week ago
0
Sanda, It looks like you are just setting the position, but we want change of color. I tried the customizer and it did not work.
ldesaraju's picture
Joined: Mar 2 2011 - 7:54am
Last seen: 3 years 7 months ago
0

I think I am getting some where here.

Initially, i thought the code you gave did not work. Now it works. I tried to use same Customizer class for 2 charts, one which was telling maximum category label lines and the other label color.  Can I not use same customizer for both? If I want both features to be applied for all charts of same type?

Also, what are these Anchor positions? My ItemLabel comes in white now, but outside the stacked bar range. I tried to Center/Inside12 as opposed to Outside12. It did not work. Please help.

 

Thanks YOU

Code:
		CategoryPlot cplot = chart.getCategoryPlot();
		cplot.getRenderer().setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.BOTTOM_CENTER));
		cplot.getRenderer().setBaseItemLabelPaint(Color.WHITE);</td></tr></tbody></table>
ldesaraju's picture
Joined: Mar 2 2011 - 7:54am
Last seen: 3 years 7 months ago
0

Hi, Let's see if all needed elements are put together in your report:

  1. The isShowLabels attribute in the bar3DPlot element has to be set to true (it's false by default): <bar3DPlot isShowLabels="true">.
  2. The itemLabel in the bar3DPlot element has to be set too: <itemLabel color="#FFFFFF" backgroundColor="#000000"/>
  3. Label positions have to be set in a chart customizer, like already discussed

CategoryPlot categoryPlot = (CategoryPlot)jfreeChart.getPlot(); categoryPlot.getRenderer().setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER)); If all these settings don't work together as expected, then please post the jrxml sample here, maybe some other clues could be found within. Hope this helps, sanda

PS: Sorry, I wrote my answer at the same time with your previous one, and didn't know meanwhile it started to work.

If both charts are the same type (ie Bar3D), then the same customizer applied to them will set the same properties for both charts. If you need different settings for each chart, then different chart customizer classes are required.

If charts are different types (ie Bar and Bar3D), one can distinguish between them - using the class name, or the instanceof operator - and different settings can be applied for each chart apart.

These anchors are used internally by the JFreeChart engine in order to render labels at some predefined positions. They represent positions an item label can take, relative to a given bar in the CategoryPlot, and the relative text position within the label rectangle. Test them and choose the most appropriate for your needs.

 Regards,

sanda



Post Edited by shertage at 03/07/2011 15:57
shertage's picture
782
Joined: Sep 26 2006 - 8:06pm
Last seen: 3 months 1 week ago
0

The color changed to white, but I can't seem to get position right. No matter how many Item LabelPosition I tried, it still is showing them outside the bars. I tried Center, Inside1, Inside5, Inside 10, Oustside5, outside12. All seem to be yielding same result.

ldesaraju's picture
Joined: Mar 2 2011 - 7:54am
Last seen: 3 years 7 months ago
0

Hi, But how about this one? categoryPlot.getRenderer().setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER, TextAnchor.CENTER, Math.PI/2)); Regarding the chart customizers role in JasperReports, I have to point out that is very important to distinguish between JasperReports and JFreeChart libraries, because they are not the same. JFreeChart is a third party library, and chart customizers just pass the chart configuration responsibility to the JFreeChart engine itself. So, when someone uses a chart customizer, it's supposed that he or she is able to work directly with JFreeChart APIs. If you really need advanced chart configuration, like the above one, then you have to search the appropriate JFreeChart classes and methods on your own. JasperReports is a reporting engine, it is not dedicated to chart generation. When rendering charts it uses the most common and the most frequent required chart settings available in JFreeChart. All other possible settings can be done using the customizer, and properly knowing the JFreeChart APIs. And here is no more JasperReports' area, here is only you facing the JFreeChart APIs. :) This is why, if the last solution I suggested above doesn't work, I would encourage you to 'dig' into JFreeChart by yourself, and become familiarized with these APIs. Your own solution should be the most appropriate one. Hope this helps and have a nice day, sanda

shertage's picture
782
Joined: Sep 26 2006 - 8:06pm
Last seen: 3 months 1 week ago
Feedback
randomness