0

My question is very similar to the question asked in this SO link

Create barchart using jfreechart with bars of same category together

If I run the below example, I get the chart as an image, (image attached)

DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.setValue(2, "Lesson-1", "27-sep-2012");
dataset.setValue(7, "Lesson-2", "27-sep-2012");
dataset.setValue(4, "Lesson-3", "27-sep-2012");

JFreeChart chart = ChartFactory.createBarChart(
    "BarChart using JFreeChart", "Student sample", "Marks sample", dataset,
    PlotOrientation.VERTICAL, true, true, false);
chart.setBackgroundPaint(Color.yellow);
chart.getTitle().setPaint(Color.blue);
CategoryPlot plot = chart.getCategoryPlot();

BarRenderer br = (BarRenderer) plot.getRenderer();
br.setItemMargin(0.7);
try {
    ChartUtilities.saveChartAsJPEG(new File(
        "D:/jfreeimages/sample.jpeg"), chart, 500, 300);
} catch (IOException e) {
    e.printStackTrace();
}

Image which I got after running the code mentioned here

for the date 27-sep-2012, i need all the bars to be clustered and to be displayed without any gaps. Many of them suggested me to have lesser margin for barRenderer (code below)

BarRenderer br =(BarRenderer) plot.getRenderer() ;
br.setItemMargin(0.0);

but this makes the size of the bar very bigger, I want the size of the bar as in the attached image only. Please help.

Community
  • 1
  • 1
Arun
  • 3,440
  • 11
  • 60
  • 108
  • You may want to try using br.setMaximumBarWidth(Double width) in conjunction with the setItemMargin(0.0). For details, see: http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/renderer/category/BarRenderer.html#setMaximumBarWidth(double) – jcern Sep 29 '12 at 14:59
  • I tried that too, but still I couldn't achieve what am trying to !! – Arun Sep 29 '12 at 17:59

1 Answers1

1

In addition to setItemMargin(0) in the renderer, you may be able to adjust the axis margins and panel size to get the desired effect.

image

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 * @see http://stackoverflow.com/a/12659576/230513
 */
public class StudentSample {

    public static void main(String[] args) {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.setValue(2, "Lesson-1", "27-sep-2012");
        dataset.setValue(7, "Lesson-2", "27-sep-2012");
        dataset.setValue(4, "Lesson-3", "27-sep-2012");

        JFreeChart chart = ChartFactory.createBarChart(
            "BarChart using JFreeChart", "Student sample", "Marks sample",
            dataset, PlotOrientation.VERTICAL, true, true, false);
        chart.setBackgroundPaint(Color.yellow);
        chart.getTitle().setPaint(Color.blue);
        CategoryPlot plot = chart.getCategoryPlot();
        BarRenderer br = (BarRenderer) plot.getRenderer();
        br.setItemMargin(0);
        CategoryAxis domain = plot.getDomainAxis();
        domain.setLowerMargin(0.25);
        domain.setUpperMargin(0.25);

        JFrame f = new JFrame("TreeEditorDemo");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ChartPanel cp = new ChartPanel(chart){

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(300, 300);
            }
        };
        f.add(cp);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);

        //try {
        //    ChartUtilities.saveChartAsJPEG(
        //        new File("temp.jpg"), chart, 300, 300);
        //} catch (IOException e) {
        //    e.printStackTrace();
        //}
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks .. but i need to save the chart as jpeg in a specific folder. that is the requirement. i added categoryAxis piece of code from your reply. still i get the bars not clustered :-( . am I missing something here ?? – Arun Sep 30 '12 at 08:10
  • If i set itemMargin to 0 then the bars are going big, if there is only one data, so i had to use barRenderer.setMaximumBarWidth(.06); The problem is, if i set the value of maximumBarWidth, then the clustering is not happening :-( – Arun Sep 30 '12 at 08:18
  • I suspect you have yet to change the corresponding `width` and `height` parameters in `saveChartAsJPEG()`. If you're still having problems, please update your [sscce](http://sscce.org/) to show your current approach and values. – trashgod Sep 30 '12 at 14:58