CategoryAxis values in barchart which uses custom renderer

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.

CategoryAxis values in barchart which uses custom renderer

Post by raj_jfree » Thu Oct 22, 2009 6:43 am

Hi,
I tried to create a barchart using categorydataset and categoryplot..
it uses CategoryAxis for domainAxis, ValueAxis for rangeAxis

i want to display my own category axis values instead of the one which are generated like category1, category2, category3...

Commented with red color in the code ...is there any way to to display my own values... say like 30,60,90,>90 instead of category1, category2, category3,category4....

below is my code

Code: Select all


import java.awt.Color;
import java.awt.Font;
import java.awt.Paint;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.TextAnchor;


/**
 * A bar chart that uses a custom renderer to display different colors within a
 * series. No legend is displayed because there is only one series but the
 * colors are not consistent.
 * 
 */
public class AgeingChart extends ApplicationFrame {

	public AgeingChart(String title) {
		super(title);
		final CategoryDataset dataset = createDataset();
		final JFreeChart chart = createChart(dataset);
		final ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
		setContentPane(chartPanel);
		// TODO Auto-generated constructor stub
	}

	/**
	 * A custom renderer that returns a different color for each item in a
	 * single series.
	 */
	class CustomRenderer extends BarRenderer {

		/** The colors. */
		private Paint[] colors;

		/**
		 * Creates a new renderer.
		 * 
		 * @param colors
		 *            the colors.
		 */
		public CustomRenderer(final Paint[] colors) {
			this.colors = colors;
		}

		/**
		 * Returns the paint for an item. Overrides the default behaviour
		 * inherited from AbstractSeriesRenderer.
		 * 
		 * @param row
		 *            the series.
		 * @param column
		 *            the category.
		 * 
		 * @return The item color.
		 */
		public Paint getItemPaint(final int row, final int column) {
			return this.colors[column % this.colors.length];
		}
	}

	/**
	 * Creates a sample dataset.
	 * 
	 * @return a sample dataset.
	 */
	private CategoryDataset createDataset() {
		final double[][] data = new double[][] { { 4.0, 3.0,3.0, 6.0 } };
		return DatasetUtilities.createCategoryDataset("Series ", "Category", data);
	}

	/**
	 * Creates a sample chart.
	 * 
	 * @param dataset
	 *            the dataset.
	 * 
	 * @return a sample chart.
	 */
	private JFreeChart createChart(final CategoryDataset dataset) {

		final CategoryItemRenderer renderer = new CustomRenderer(new Paint[] { Color.red, Color.blue, Color.green, Color.orange, Color.cyan,
				Color.magenta, Color.blue });
		renderer.setBaseItemLabelsVisible(true);
		final ItemLabelPosition p = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.CENTER_RIGHT, TextAnchor.CENTER_RIGHT, 0);
		renderer.setBasePositiveItemLabelPosition(p);
		renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		renderer.setBaseItemLabelsVisible(true);
		
		renderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
		renderer.setBaseItemURLGenerator(new StandardCategoryURLGenerator(" ", "series","status"));
		
		ValueAxis yAxis = new NumberAxis(" ");
		yAxis.setLabelPaint(new Color(47,109,193));
		yAxis.setTickLabelPaint(new Color(47,109,193));
		yAxis.setAutoRange(true);
[color=#FF0000]//		CategoryAxis xAxis = new CategoryAxis(" ", new String[] { "30", "60", "90", "Above 90" });
		
		CategoryAxis xAxis = new CategoryAxis();[/color]
		xAxis.addCategoryLabelToolTip("Category1", "30");
		xAxis.addCategoryLabelToolTip("Category2", "60");
		xAxis.addCategoryLabelToolTip("Category3", "90");
		xAxis.addCategoryLabelToolTip("Category4", "Above 90");
		xAxis.setTickLabelPaint(new Color(47,109,193));
		
		
		CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer);
		JFreeChart jfreechart = new JFreeChart("", new Font("Tahoma", 0, 18), plot, true);


		return jfreechart;

	}


	/**
	 * Starting point for the demonstration application.
	 * 
	 * @param args
	 *            ignored.
	 */
	public static void main(final String[] args) {

		final AgeingChart demo = new AgeingChart("Bar Chart Demo 3");
		demo.pack();
		RefineryUtilities.centerFrameOnScreen(demo);
		demo.setVisible(true);

	}

}

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: CategoryAxis values in barchart which uses custom renderer

Post by david.gilbert » Thu Oct 22, 2009 9:06 pm

Sure. The category keys can be any object that implements the Comparable interface. The text displayed comes from the toString() method. So you can write your own class that implements Comparable and returns whatever you need in the toString() method.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

raj_jfree
Posts: 20
Joined: Tue Sep 29, 2009 8:32 am
antibot: No, of course not.

Re: CategoryAxis values in barchart which uses custom renderer

Post by raj_jfree » Fri Oct 23, 2009 11:42 am

Thanks for the reply
i have done this way

Code: Select all

private CategoryDataset createDataset() {
		final double[][] data = new double[][] { { 4.0, 3.0, 3.0, 6.0 } };
		String[] columnKeyPrefix = new String[] { "30", "60", "90", "Above 90" };

		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);
			}
		}
		return dataset;
	}

Locked