vijay
vijay

Reputation: 1139

JFreeChart Bar chart Design Change

I am looking for a way to change the design of the Bar chart in JFreechart. Here is my excel graph. How can I change the jfreechart barchart in the below design format?

enter image description here

Upvotes: 0

Views: 1351

Answers (1)

longhua
longhua

Reputation: 4242

The following code can generate a similar chart. Does it suffice?

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

Upvotes: 3

Related Questions