0

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

使用 jfreechart 与同一类别的条形图一起创建条形图

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

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

运行此处提到的代码后得到的图像

对于 2012 年 9 月 27 日的日期,我需要对所有条形图进行聚类并无间隙地显示。他们中的许多人建议我减少 barRenderer 的保证金(下面的代码)

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

但这会使条形图的大小变得非常大,我只想要所附图像中的条形图大小。请帮忙。

4

1 回答 1

1

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

图片

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();
        //}
    }
}
于 2012-09-30T07:41:59.637 回答