条形图中具有相同类别的条形图

时间:2012-09-29 14:49:36

标签: java java-ee jfreechart

我的问题与此SO链接中提出的问题非常相似

Create barchart using jfreechart with bars of same category together

如果我运行以下示例,我将图表作为图像,(附图像)

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

对于27-sep-2012的日期,我需要对所有条形进行聚类并显示没有任何间隙。他们中的许多人建议我为barRenderer(下面的代码)保留较少的保证金

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

但是这会使条形图的尺寸变得更大,我想要条形图的尺寸仅在附加图像中。请帮忙。

1 个答案:

答案 0 :(得分:1)

除了渲染器中的setItemMargin(0)之外,您还可以调整轴边距和面板大小以获得所需的效果。

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();
        //}
    }
}