1

Currently I'm plotting a dual axis bar chart using JFreeChart. The bars for the intervals are very close to each other. Because of that, the label for intervals is not shown in the graph. How to increase the space between the bars?

    CategoryAxis domainAxis = new CategoryAxis("Value");
    domainAxis.setAxisLinePaint(Color.blue);
    domainAxis.setLowerMargin(0.05);
    domainAxis.setUpperMargin(0.05);
    NumberAxis rangeAxis = new NumberAxis("Category 2");
    BarRenderer renderer1 = new BarRenderer();
    renderer1.setMaximumBarWidth(.50);
    renderer1.setSeriesPaint(0, Color.blue);
    renderer1.setShadowVisible(false);
    CategoryPlot plot = new CategoryPlot(dataset1, domainAxis, rangeAxis, renderer1);
    JFreeChart chart = new JFreeChart("", plot);
    chart.setBackgroundPaint(Color.WHITE);
    plot.setBackgroundPaint(Color.WHITE);
    plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);   
    plot.setDataset(1, dataset2);
    plot.mapDatasetToRangeAxis(1, 1);
    ValueAxis axis2 = new NumberAxis("Category 1");
    plot.setRangeAxis(1, axis2);
    plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);
    BarRenderer renderer2 = new BarRenderer();
    renderer2.setSeriesPaint(0, Color.green);
    renderer2.setMaximumBarWidth(.100);
    renderer2.setShadowVisible(false);  
    plot.setRenderer(1, renderer2);
    return chart;

The complete code:

import java.awt.Dimension;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class BarChartDemo7 extends ApplicationFrame {

static class LabelGenerator extends StandardCategoryItemLabelGenerator {

    public String generateLabel(CategoryDataset dataset, int series,
            int category) {
        return dataset.getRowKey(series).toString();
    }
}

public BarChartDemo7(String title) {
    super(title);
    JPanel chartPanel = createDemoPanel();
    chartPanel.setPreferredSize(new Dimension(500, 270));
    setContentPane(chartPanel);
}

private static CategoryDataset createDataset() {

    String series1 = "First";
    String series2 = "Second";
    String series3 = "Third";

    DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    for(int i=0;i<120;i++){
        dataset.addValue(i, series1, Integer.toString(i));
    }

    for(int i=0;i<120;i++){
        dataset.addValue(i, series2, Integer.toString(i));
    }

    for(int i=0;i<120;i++){
        dataset.addValue(i, series3, Integer.toString(i));
    }


    return dataset;

}

private static JFreeChart createChart(CategoryDataset dataset) {


    JFreeChart chart = ChartFactory.createBarChart(
            "Bar Chart Demo 7",       
            "Category",              
            "Value",                  
            dataset,                  
            PlotOrientation.HORIZONTAL, 
            false,                    
            true,                    
            false                  
            );

    CategoryPlot plot = (CategoryPlot) chart.getPlot();
    plot.setRangePannable(true);

    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

    BarRenderer renderer = (BarRenderer) plot.getRenderer();
    renderer.setDrawBarOutline(false);
    renderer.setItemMargin(0.10);

    return chart;

}

public static JPanel createDemoPanel() {
    JFreeChart chart = createChart(createDataset());
    ChartPanel panel = new ChartPanel(chart);
    panel.setMouseWheelEnabled(true);
    return panel;
}

public static void main(String[] args) {
    BarChartDemo7 demo = new BarChartDemo7(
            "");
    demo.pack();
    RefineryUtilities.centerFrameOnScreen(demo);
    demo.setVisible(true);
}

}

Chart with overlapping intervals

I want to increase the space between the intervals so that they do not overlap.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Reetika
  • 11
  • 3
  • Possible duplicate of [*Width of the bar in JFreeChart*](http://stackoverflow.com/q/1973936/230513). – trashgod Jan 11 '17 at 20:55
  • No its not. As the setMaximumBarWidth() is not helpful in my case because the number of intervals are more and jfreechart tries to accomodate all the intervals and thus they are all overlapping. I tried to increase the window size of jfreechart and put it in scrollpane, but that also dint help. It just made the chart bigger and intervals were still overlapping. – Reetika Jan 16 '17 at 03:50
  • Without a [mcve], I'm guessing. Possible duplicate of [*How can I add a horizontal scroll bar in JFreeChart?*](http://stackoverflow.com/q/11807984/230513). – trashgod Jan 16 '17 at 14:05
  • I have edited my question with the complete example. – Reetika Jan 30 '17 at 05:33
  • Use a `SlidingCategoryDataset` as shown in `SlidingCategoryDatasetDemo1`. – trashgod Jan 30 '17 at 07:37
  • Tried... Not working :( Still overlapping !! – Reetika Jan 30 '17 at 08:22
  • That's odd; they don't overlap in `SlidingCategoryDatasetDemo1`. – trashgod Jan 30 '17 at 10:23
  • Yes, because the number of intervals are less in it. In my case i have to show 120 intervals in the chart and the chart is not accomodating it properly. – Reetika Jan 30 '17 at 10:38

0 Answers0