BarChart zero values

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
gagister
Posts: 1
Joined: Tue Oct 15, 2019 11:09 am
antibot: No, of course not.

BarChart zero values

Post by gagister » Tue Oct 15, 2019 1:10 pm

Hello to everybody,

I'm rather new to this library and I have an issue I can't handle.

This is BarChart I created.

Image

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;
    }
Thank you in advance for help.

Locked