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