Stacked Bar Chart Labels Position and Value Marker Problem

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
ninitha
Posts: 51
Joined: Mon Aug 24, 2009 7:05 am
antibot: No, of course not.

Stacked Bar Chart Labels Position and Value Marker Problem

Post by ninitha » Mon Oct 05, 2009 11:23 am

Hi,

I am using JDK1.5 and JFreeChart 1.0.13.

I need to generate stacked bar charts either horizontally or vertically depending upon user's input.

I was able to generate the bar labels at the top of each bar with the following code

Code: Select all

renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
But the above works good when the chart is vertically oriented..When I change the chart orientation as horizontal as in :

Code: Select all

chart = ChartFactory.createStackedBarChart(chartTitle,null,null,dataset,PlotOrientation.HORIZONTAL,true,true,false);
The labels dont appear exactly at the end of each bar...instead it takes up the same top position which i dont want...Please do let me know how to place the labels at the end of each bar in a horizontal bar chart.

I want a chart as follows:
Image

But currently, am getting as follows:
Image

One more problem am facing is with the value marker. Following is the code I have used in my code.

Code: Select all

Marker mark = new ValueMarker(0);
		mark.setPaint(Color.black);
		mark.setStroke(new BasicStroke(1.0f));
		plot.addRangeMarker(mark);	
The above draws the marker at the value of 0 if the chart is horizontally oriented...but doesnt work for vertical orientation of chart.. :(

With the same code for vertical orientation, the chart displays as follows.
Image

Please let me know how to solve these problems.

ninitha
Posts: 51
Joined: Mon Aug 24, 2009 7:05 am
antibot: No, of course not.

Re: Stacked Bar Chart Labels Position and Value Marker Problem

Post by ninitha » Mon Oct 05, 2009 11:37 am

One more thing is only recently I have upgraded to jfreechart 1.0.13... Before, i was using jfreechart 1.0.9...

When using JFreeChart 1.0.9, the below code resulted in Range Gridlines.

Code: Select all

plot.setRangeGridlinesVisible(true);
where as now in 1.0.13, am not getting the range gridlines even if the visibility is set to true.. :( what am I missing here??

ninitha
Posts: 51
Joined: Mon Aug 24, 2009 7:05 am
antibot: No, of course not.

Re: Stacked Bar Chart Labels Position and Value Marker Problem

Post by ninitha » Wed Oct 07, 2009 1:00 pm

Hi,

Any solution to the above problems?

Pegasus
Posts: 6
Joined: Wed Oct 07, 2009 12:48 pm
antibot: No, of course not.

Re: Stacked Bar Chart Labels Position and Value Marker Problem

Post by Pegasus » Wed Oct 07, 2009 1:07 pm

hi, maybe you can help me. I wanted to know what the property which the data is written on the vertical bar, as your third image.Thanks.

ninitha
Posts: 51
Joined: Mon Aug 24, 2009 7:05 am
antibot: No, of course not.

Re: Stacked Bar Chart Labels Position and Value Marker Problem

Post by ninitha » Wed Oct 07, 2009 1:56 pm

Pegasus,

Its first piece of code in the post...

Anybody, pls let me know why things that work in 1.0.9 dont work in 1.0.13(range gridlines)... am I missing anything here?

And reg the marker problem, when I analysed, I see that when the domain axis labels are set to true, the marker appears for vertical orientation...whereas it is independent of domain axis labels being true in horizontal orientation....not sure why.. :(

And david, am sure u will have a way to place the labels at the end of horizontal bars.... pls help me..

ninitha
Posts: 51
Joined: Mon Aug 24, 2009 7:05 am
antibot: No, of course not.

Re: Stacked Bar Chart Labels Position and Value Marker Problem

Post by ninitha » Wed Oct 07, 2009 2:10 pm

Also is anyone aware of how to display the Range Axis labels as percentage(percentage next to the number it displays) and also the labels at the end of the bar should display percentage symbol along with the number... I tried setNumberFormatOverride of Number Axis to NumberFormat.getPercentInstance()... But that was displaying some other percentage instead of the values that were input...

remiohead
Posts: 201
Joined: Fri Oct 02, 2009 3:53 pm
antibot: No, of course not.

Re: Stacked Bar Chart Labels Position and Value Marker Problem

Post by remiohead » Wed Oct 07, 2009 8:14 pm

Outside end data labels:

Code: Select all

ItemLabelAnchor lblAnchor = ItemLabelAnchor.OUTSIDE3;
TextAnchor txtAnchor = TextAnchor.CENTER_LEFT;

remiohead
Posts: 201
Joined: Fri Oct 02, 2009 3:53 pm
antibot: No, of course not.

Re: Stacked Bar Chart Labels Position and Value Marker Problem

Post by remiohead » Wed Oct 07, 2009 8:16 pm

For the other problem, if you just want a line at 0 why not make the category axis line visible and turn off tick marks etc?

ninitha
Posts: 51
Joined: Mon Aug 24, 2009 7:05 am
antibot: No, of course not.

Re: Stacked Bar Chart Labels Position and Value Marker Problem

Post by ninitha » Thu Oct 08, 2009 6:21 am

Thanks remiohead.. It solved my labels problem... Dint know how to choose the ItemLabelAnchor and TextAnchor since they are not descriptive... How to select them based on the location in which we want the labels?? it would be of good help if you could explain...

And the marker one... I have tried that alternative too...making the category axis visible without tick marks and tick labels...But it doesnt seem to fit exactly..There is space between the bars and the axis...Dunno why.. :(

And do you know how to place symbols(dollar or percentage sign along with the number they show up) in the axis...

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Stacked Bar Chart Labels Position and Value Marker Problem

Post by paradoxoff » Thu Oct 08, 2009 7:55 am

ninitha wrote:Dint know how to choose the ItemLabelAnchor and TextAnchor since they are not descriptive... How to select them based on the location in which we want the labels??
There are 25 different ItemLabelAnchor, CENTER, INSIDE1 to INSIDE12 and OUTSIDE1 to OUTSIDE12.
They basically define the place of the label relative to a clock: CENTER means in the center of the clock, 1 to 12 corresponds to the hour of the clock, and INSIDE/OUTSIDE relates to inside and outside the clock.
You have used OUTSIDE12 which will place the labels at 12 o´clock, i.e.horizonatlly centered on the top. This is fine for normal plot orientation but looks weird for a horizontal plot. For a horizontal plot, you need to place the labels at the outer left side, which is 3 o'clock, and thus the correct ItemLabelAnchor is OUTSIDE3.
ninitha wrote: And the marker one... I have tried that alternative too...making the category axis visible without tick marks and tick labels...But it doesnt seem to fit exactly..There is space between the bars and the axis...Dunno why..
I have no idea why the marker doesn´t work. Maybe you have given it the same color as the chart/plot background. In any case the axis seems to work. The space probably comes from an axis offset (XYPlot.setAxisOffset). You could try to set that to RectangleInsets.ZERO_INSETS, which, btw, is the default.
If you have indeed an non-ZERO_INSETS axis offset, it is clear that you are applying some customization to the chart which also might explain the misbehaviour of the marker.

ninitha
Posts: 51
Joined: Mon Aug 24, 2009 7:05 am
antibot: No, of course not.

Re: Stacked Bar Chart Labels Position and Value Marker Problem

Post by ninitha » Thu Oct 08, 2009 9:42 am

Excellent explanation paradoxoff... Thank you.... Now, am able to place my labels where ever I want.. :D

And the axis problem...In version jfreechart 1.0.13, the axis are spaced some 4 points apart(where as in 1.0.9, it is zero insets)..With your suggestion, i have set it to ZERO INSETS... Now it looks gr8... this will be an alternative for marker problem :)

Now the only problem haunting me is setRangeGridlines not working in version 13.... :(

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Stacked Bar Chart Labels Position and Value Marker Problem

Post by paradoxoff » Thu Oct 08, 2009 12:02 pm

ninitha wrote: And the axis problem...In version jfreechart 1.0.13, the axis are spaced some 4 points apart(where as in 1.0.9, it is zero insets)..With your suggestion, i have set it to ZERO INSETS.
Now the only problem haunting me is setRangeGridlines not working in version 13.... :(
You fell prey to the ChartTheme mechanism that was introduced in JFreeChart 1.0.11.
You are apparently using the ChartFactory to create the charts. If you have not touched the defaults, the ChartFactory will apply the settings defined in the "JFree" theme to newly created charts. Among other things, this leads to an axis offset of 4 for XYPlots (the default axis offset defined in the XYPlot class is still ZERO_INSETS), and to white grid lines. At the same time, the background paint for a plot will be set to Color.lightGray.
Since your plots have a white background, you are probably applying some own customizations to the charts. In order to make the grid lines visible again, you simply have to change their paint to Color.black or whatever was the default in 1.0.9.
Alternatively, you could tell the ChartFactory to use the "legay theme" that doesn´t change the defaults at all

Code: Select all

ChartFactory.setChartTheme(ChartTheme.createLegacyTheme());

ninitha
Posts: 51
Joined: Mon Aug 24, 2009 7:05 am
antibot: No, of course not.

Re: Stacked Bar Chart Labels Position and Value Marker Problem

Post by ninitha » Thu Oct 08, 2009 12:06 pm

waw... :D Thank you so much... Will do as you say... I dint know this before.. Thanks for taking pains in making me understand...

chechaquo
Posts: 10
Joined: Tue Oct 28, 2008 12:42 pm

Re: Stacked Bar Chart Labels Position and Value Marker Problem

Post by chechaquo » Mon Oct 19, 2009 12:45 pm

Is it possible to place labels vertically for vertical chart, not horizontally as it is by default? (i.e. in the third chart rotate labels by 90 degree on the right ) :roll:

chechaquo
Posts: 10
Joined: Tue Oct 28, 2008 12:42 pm

Re: Stacked Bar Chart Labels Position and Value Marker Problem

Post by chechaquo » Mon Oct 19, 2009 2:03 pm

Got it, just needed to use ItemLabelPosition(ItemLabelAnchor itemLabelAnchor, org.jfree.ui.TextAnchor textAnchor, org.jfree.ui.TextAnchor rotationAnchor, double angle) :oops:

Locked