Question

I am trying to remove the gaps between the bars on a XYBarRenderer in a TimeSeriesChart. In other words, I would like to expand the bar when there is no data before and after the bar's time. Is it possible? I will really appreciate your help.

here is my code:

protected JFreeChart criarChart(XYDataset dataset){
    JFreeChart chart;

    chart = ChartFactory.createTimeSeriesChart(
            this.getTitulo(), //titulo
            this.getEixoX(), //nome do eixo-x
            this.getEixoY(), //nome do eixo-y
            dataset, //dados
            true, //criar legenda?
            true, //criar tooltips?
            false); //criar URLs?

    chart.setBackgroundPaint(Color.white);

    XYPlot plot = (XYPlot) chart.getPlot();
    plot.setOrientation(PlotOrientation.VERTICAL);
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);
    plot.setDomainCrosshairVisible(true);
    plot.setRangeCrosshairVisible(true);
    plot.getRenderer().setSeriesPaint(0, Color.red);

    plot.setRenderer(new ClusteredXYBarRenderer() {
                @Override
                public Paint getItemPaint(int series, int item) {
                    XYDataset dataset = getPlot().getDataset();
                    if (dataset.getYValue(series, item) >= 0.0) {
                        return Color.green;
                    }
                    else {
                        return Color.red;
                    }
                }
            }
    );
    XYItemRenderer renderer = plot.getRenderer();
    if(renderer instanceof XYBarRenderer){
        XYBarRenderer r = (XYBarRenderer)renderer;
        r.setBarPainter(new StandardXYBarPainter());
        r.setMargin(-20.0);
        r.setShadowVisible(false);
    }
    //mostra o tooltip das barras do grafico
    plot.getRenderer().setBaseToolTipGenerator(new StandardXYToolTipGenerator(
    StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
    new SimpleDateFormat("HH:mm"), new DecimalFormat("#0")));

    DateAxis axis = (DateAxis) plot.getDomainAxis();
    axis.setDateFormatOverride(new SimpleDateFormat("HH:mm"));

    return chart;
}

I saw somewhere to change the Margin, so I tried to use:

r.setMargin(-20.0);

but didn't work.

Was it helpful?

Solution

The XYBarRenderer relies on the dataset to supply values that determine the width of the bars (see the getStartXValue() and getEndXValue() methods). It also trims the width by a percentage referred to as the 'margin'. The margin is only used if it is greater than zero, and you would specify a number like 0.20 (twenty percent). The default margin is 0.0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top