I'm rather new to this library and I have an issue I can't handle.
This is BarChart I created.
What I need is to have a small bar for 0 (zero) values.
This is the code I made for this chart:
Code: Select all
static JFreeChart drawBarChart(Map<String, Integer> answerMap) {
//Dataset used by drawing BarChart
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
answerMap.forEach((k, v) -> dataset.setValue(v, k, ""));
JFreeChart barChart = ChartFactory.createBarChart("", "", "", dataset,
PlotOrientation.HORIZONTAL, true, false, false);
//Plot - class that controls visual presentation of BarChart
Plot plot = barChart.getPlot();
plot.setBackgroundPaint(Color.WHITE);
plot.setOutlineVisible(false);
plot.setNoDataMessage("No data to show");
//Legend
LegendTitle legend = barChart.getLegend();
legend.setHorizontalAlignment(org.jfree.chart.ui.HorizontalAlignment.LEFT);
//Bar colors
CategoryPlot categoryPlot = barChart.getCategoryPlot();
BarRenderer renderer = (BarRenderer) categoryPlot.getRenderer();
renderer.setShadowVisible(false);
renderer.setBarPainter(new StandardBarPainter());
for (int i = 0; i < dataset.getRowCount(); i++) {
renderer.setSeriesPaint(i, new Color(getColorPalette()[i]));
}
// set the plot's axes to display integers
TickUnitSource ticks = NumberAxis.createIntegerTickUnits();
NumberAxis range = (NumberAxis) categoryPlot.getRangeAxis();
range.setStandardTickUnits(ticks);
//Set Labels on Bars
CategoryItemRenderer itemRenderer = ((CategoryPlot) barChart.getPlot()).getRenderer();
renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setDefaultItemLabelsVisible(true);
barChart.getCategoryPlot().setRenderer(itemRenderer);
return barChart;
}