Change in size of the barchart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
tarunvictor
Posts: 6
Joined: Fri Mar 06, 2015 3:20 pm
antibot: No, of course not.

Change in size of the barchart

Post by tarunvictor » Sat Mar 07, 2015 8:43 am

Hi All

Even though i saw lot of options available in the internet I am not able to find the exact solution. I have a bar chart with 900* 600 size. The number of values are dynamic and it will be having maximum of 50 values. The chart look good when it is having 50 values but worst when it is having only few, worst is when it is having only one value :P . Is it possible to give a fixed width ? . My requirement is a fixed bar chart for each value, if there is only value the remaining space in the chart can be empty but the size of the bar should be constant.
Currently there is an option to give fixed width which is in percentage but it is automatically adjusting the size of the bar(s) depends on the size of the chart and the number of values.

Looking forward for your reply, thanks in advance

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

Re: Change in size of the barchart

Post by paradoxoff » Mon Mar 09, 2015 11:20 am

A fixed width will stop to be useful when the number of bar is growing.
Have you seen the setMaximumBarWidth-method of the BarRenderer class?

tarunvictor
Posts: 6
Joined: Fri Mar 06, 2015 3:20 pm
antibot: No, of course not.

Re: Change in size of the barchart

Post by tarunvictor » Mon Mar 09, 2015 4:24 pm

Yes I checked setMaximumBarWidth but its not working as it in percentage. My number of bars are fixed so I need bar with fixed width. My maximum bars are to 50, if I have 5 bars then remaining space can be free.

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

Re: Change in size of the barchart

Post by paradoxoff » Mon Mar 09, 2015 4:31 pm

Write your own renderer that extends BarRenderer and override calculateBarW0 to return a constant value.

tarunvictor
Posts: 6
Joined: Fri Mar 06, 2015 3:20 pm
antibot: No, of course not.

Re: Change in size of the barchart

Post by tarunvictor » Tue Mar 10, 2015 3:25 pm

Unable to extend BarRender, when I try to type cast, it throws cast exception, due to up casting of the class
CategoryPlot plot = chart.getCategoryPlot();
CustomBarRenderer barRenderer = (CustomBarRenderer) plot.getRenderer();

Is there any other way to override the method calculateBarW0??

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

Re: Change in size of the barchart

Post by paradoxoff » Tue Mar 10, 2015 5:35 pm

if you have written a new renderer named CustomBarRenderer, you have to use it to construct your chart. Apparently, you haven´t done so, otherwise, the cast wouldn´t fail. Since you do not show the code that creates the chart, it is impossible to suggest a better way.
By the way, why do you need to access your new renderer?

tarunvictor
Posts: 6
Joined: Fri Mar 06, 2015 3:20 pm
antibot: No, of course not.

Re: Change in size of the barchart

Post by tarunvictor » Wed Mar 11, 2015 9:22 am

private static JFreeChart createBarChart(CategoryDataset dataset, String chartTitle, String YLabel, String XLabel) {
JFreeChart chart = null;
chart = ChartFactory.createBarChart(
chartTitle,
XLabel,
YLabel,
dataset,
PlotOrientation.VERTICAL,
true,
false,
false
);

CategoryPlot plot = chart.getCategoryPlot();
final ValueAxis rangeAxis = ((CategoryPlot) plot).getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
CategoryAxis domainAxis = ((CategoryPlot) plot).getDomainAxis();
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
domainAxis.setLowerMargin(.01);
domainAxis.setCategoryMargin(.01);
domainAxis.setUpperMargin(.01);
chart.setBackgroundPaint(Color.white);
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setDomainGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.white);
Font font3 = new Font("Dialog", Font.PLAIN, 25);
plot.getDomainAxis().setLabelFont(font3);
plot.getRangeAxis().setLabelFont(font3);
BarRenderer barRenderer = (BarRenderer) plot.getRenderer();
barRenderer.setMaximumBarWidth(.3);
barRenderer.setItemMargin(-2);
barRenderer.setDrawBarOutline(false);
CategoryItemLabelGenerator generator = new StandardCategoryItemLabelGenerator();
barRenderer.setSeriesItemLabelGenerator(0, generator);
barRenderer.setSeriesItemLabelsVisible(0, true);
barRenderer.setSeriesPositiveItemLabelPosition(0, new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12,TextAnchor.BASELINE_CENTER));
barRenderer.setItemLabelAnchorOffset(10);
GradientPaint gradientPaint = null;
if("Warnings".equalsIgnoreCase(YLabel)){
barRenderer.setSeriesPaint(0, Color.YELLOW);
}else{
//barRenderer.setSeriesPaint(0, Color.RED);
barRenderer.setSeriesPaint(0, new Color(255, 101, 101));
}
domainAxis.setCategoryLabelPositions(
CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)
);
return chart;
}

Please find the code above, as I mentioned earlier my category axis values are dynamic(max of 50) and I want the width of the bar fixed, that is if i have one value in cateogory axis the width of the bar should be same as earlier. Currently width is getting adjusted which I want to make constant.

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

Re: Change in size of the barchart

Post by paradoxoff » Wed Mar 11, 2015 9:30 am

Since the ChartFactory does not know about your new renderer (how could it), it is using a normal BarRenderer to construct the chart, and your attempted cast to CustomBarRenderer fails.
You need these lines in your code:

Code: Select all

CustomBarRenderer renderer = new CustomBarRenderer();//or whatever siganture the constructor of your new renderer has
//further customizations, such as maximum bar width, item margin, etc.
plot.setRenderer(renderer);

tarunvictor
Posts: 6
Joined: Fri Mar 06, 2015 3:20 pm
antibot: No, of course not.

Re: Change in size of the barchart

Post by tarunvictor » Tue Mar 17, 2015 9:42 am

Thanks it worked

Locked