JFreeChart条形图设计变更

时间:2013-04-16 15:17:09

标签: java jfreechart bar-chart

我正在寻找一种方法来改变JFreechart中条形图的设计。这是我的excel图。如何更改以下设计格式的jfreechart条形图?

enter image description here

1 个答案:

答案 0 :(得分:2)

以下代码可以生成类似的图表。这样就够了吗?

bar chart example

public class BarExample {
    public static void main(String arg[]) throws IOException {
        DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
        dataSet.setValue(2, Integer.valueOf(1), Integer.valueOf(5));
        dataSet.setValue(7, Integer.valueOf(1), Integer.valueOf(10));
        dataSet.setValue(4, Integer.valueOf(1), Integer.valueOf(15));
        dataSet.setValue(9, Integer.valueOf(1), Integer.valueOf(20));
        dataSet.setValue(6, Integer.valueOf(1), Integer.valueOf(25));
        JFreeChart chart = ChartFactory.createBarChart
                ("Chart", "Number of Days", "Number of ECR", dataSet,
                        PlotOrientation.VERTICAL, false, true, false);
        CategoryPlot plot = chart.getCategoryPlot();
        // Reduce margin between bars
        plot.getDomainAxis().setCategoryMargin(0.0);
        // Reduce left and right margin
        plot.getDomainAxis().setLowerMargin(0.0);
        plot.getDomainAxis().setUpperMargin(0.0);
        FileOutputStream outputStream = new FileOutputStream(
                new File("chart.png"));
        ChartUtilities.writeChartAsPNG(outputStream, chart, 1024, 768);
    }
}