Reputation: 3700
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();
}
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.
Upvotes: 0
Views: 2164
Reputation: 205885
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.
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();
//}
}
}
Upvotes: 1