I\'m creating a XYBarChart using JFreeChart and adding multiple series to it. Currently for a given x-value and different Y-values from the series, all of them are getting s
EDIT Use ClusteredXYBarRenderer
instead of XYBarRenderer
. This draws adjacent bars (instead of StackedBars) and provides zooming capabilities.
There is no factory method in ChartFactory
to create ClusteredXYBarRenderer
.
Use the below method to create the Bar Chart.
private static JFreeChart createClusteredChart(String title, String categoryAxisLabel, String valueAxisLabel, IntervalXYDataset dataset) {
NumberAxis domainAxis = new NumberAxis(categoryAxisLabel);
domainAxis.setAutoRangeIncludesZero(false);
ValueAxis valueAxis = new NumberAxis(valueAxisLabel);
XYBarRenderer renderer = new ClusteredXYBarRenderer();
XYPlot plot = new XYPlot(dataset, domainAxis, valueAxis, renderer);
plot.setOrientation(PlotOrientation.VERTICAL);
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
return chart;
}
XYBarRenderer
that you are using is correct. But I think you should use CategoryDataSet
instead of XYSeriesCollection
. Try using below dataset instead of XYSeriesCollection
. This will produce adjacent bars instead of stacked bars.
private static CategoryDataset createDataset() {
String series1 = "First";
String series2 = "Second";
String series3 = "Third";
String category1 = "Category 1";
String category2 = "Category 2";
String category3 = "Category 3";
String category4 = "Category 4";
String category5 = "Category 5";
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(1.0D, series1, category1);
dataset.addValue(4.0D, series1, category2);
dataset.addValue(3.0D, series1, category3);
dataset.addValue(5.0D, series1, category4);
dataset.addValue(5.0D, series1, category5);
dataset.addValue(5.0D, series2, category1);
dataset.addValue(7.0D, series2, category2);
dataset.addValue(6.0D, series2, category3);
dataset.addValue(8.0D, series2, category4);
dataset.addValue(4.0D, series2, category5);
dataset.addValue(4.0D, series3, category1);
dataset.addValue(3.0D, series3, category2);
dataset.addValue(2.0D, series3, category3);
dataset.addValue(3.0D, series3, category4);
dataset.addValue(6.0D, series3, category5);
return dataset;
}