i want to display the labels( (3/4),(1/2),(3/5) instead of (75),(50),(60) ) for the items.
i want to make the green line have a offset that the columns are in the two green lines center.
the following is the code.
thanks;
sxrsing
Code: Select all
package org.jfree.chart.demo;
import java.awt.Color;
import java.awt.Insets;
import java.text.DecimalFormat;
import org.jfree.chart.ChartColor;
import org.jfree.chart.ChartFactory;
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.NumberTickUnit;
import org.jfree.chart.labels.StandardCategoryLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.CategoryItemRenderer;
import org.jfree.data.CategoryDataset;
import org.jfree.data.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class BarChart extends ApplicationFrame {
/**
* Creates a new demo.
*
* @param title the frame title.
*/
public BarChart(final 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);
}
/**
* Creates a sample dataset.
*
* @return a sample dataset.
*/
private CategoryDataset createDataset() {
try {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
String strSeries = "series1" ;
dataset.addValue(3/4.0 * 100, strSeries, "Category1");
dataset.addValue(1/2.0 * 100, strSeries, "Category2");
dataset.addValue(3/5.0 * 100, strSeries, "Category3");
return dataset;
} catch (Exception e) {
return null;
}
}
/**
* Creates a sample chart.
*
* @param dataset the dataset.
*
* @return a sample chart.
*/
private JFreeChart createChart(final CategoryDataset dataset) {
final JFreeChart chart = ChartFactory.createBarChart(
"", // chart title
"", // domain axis label
"", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // the plot orientation
false, // include legend
true,
false
);
chart.setBackgroundPaint(Color.lightGray);
// get a reference to the plot for further customisation...
CategoryPlot plot = chart.getCategoryPlot();
plot.setNoDataMessage("NO DATA!");
plot.setBackgroundPaint(ChartColor.white);
plot.setDomainGridlinesVisible(true);
plot.setDomainGridlinePaint(ChartColor.GRAY);
plot.setDomainGridlinesVisible(true);
plot.setRangeGridlinesVisible(true);
plot.setRangeGridlinePaint(ChartColor.GRAY);
plot.setInsets(new Insets(25, 0, 0, 25));
CategoryItemRenderer renderer = plot.getRenderer();
renderer.setSeriesPaint(0, new ChartColor(130, 196, 223));
StandardCategoryLabelGenerator categoryLabel = new StandardCategoryLabelGenerator("{2}",new DecimalFormat( "(###.##)" ));
renderer.setSeriesLabelGenerator(0,categoryLabel);
renderer.setItemLabelsVisible(true);
plot.setRenderer(renderer);
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setTickUnit(new NumberTickUnit(50, new DecimalFormat("0")));
rangeAxis.setAutoRange(false);
rangeAxis.setRange(0, 100);
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryMargin(0.4);
domainAxis.setLowerMargin(0.05);
domainAxis.setUpperMargin(0.05);
plot.setDomainAxis(domainAxis);
//set barchart's alpha
plot.setForegroundAlpha(0.8f);
return chart;
}
/**
* Starting point for the demonstration application.
*
* @param args ignored.
*/
public static void main(final String[] args) {
BarChart demo = new BarChart("Bar Chart");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
[/b]