Barchart with 3 values

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
raj_jfree
Posts: 20
Joined: Tue Sep 29, 2009 8:32 am
antibot: No, of course not.

Barchart with 3 values

Post by raj_jfree » Mon Nov 23, 2009 9:07 am

Hi,
I need to create a bar chart with these data.
final double[][] data = new double[][] { { 10, 25, 35 } };
final double[][] count = new double[][] { { 40, 30, 20 } };
String[] columnKeyPrefix = new String[] { "Jan", "Feb", "Mar" };

my x-axis is jan,feb ,mar and my y-axis should in the data range .the values in each bar should be the count.

required chart:

Image.

when using defaultcategorydataset

Code: Select all

		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
			for (int r = 0; r < data.length; r++) {
				String rowKey = "" + (r + 1);
				for (int c = 0; c < data[r].length; c++) {
					String columnKey = "" + columnKeyPrefix[c];
					dataset.addValue(new Double(data[r][c]), rowKey, columnKey);
				}
			
and set doing this

Code: Select all

barrenderer.setBaseItemLabelsVisible(true);
       barrenderer.setBaseItemLabelGenerator(new LabelGenerator());
i am able to get the chart but the labels displayed inside the bar are data values,but i want to display count values.


can anyone help me in solving this,

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

Re: Barchart with 3 values

Post by remiohead » Tue Nov 24, 2009 2:29 pm

You could do it by extending BarRenderer and overriding getItemLabelGenerator(int, int):

Code: Select all

@Override
public CategoryItemLabelGenerator getItemLabelGenerator(final int row, final int column) {
   String value = "40"; // Change to what you need for this data point
   StandardCategoryItemLabelGenerator lbl = 
      new StandardCategoryItemLabelGenerator(value, NumberFormat.getInstance());
   return lbl;
}

Locked