【问题标题】:JFreeChart with truncated data points带有截断数据点的 JFreeChart
【发布时间】:2012-12-17 09:36:38
【问题描述】:

我用下面的代码创建了一个 JFreeChart,但是 Y 轴标记被截断了。即使数据点在 Y 轴上重叠,我应该如何显示图表? 基本上,我希望从我的文件中生成 Y 轴点,在图表中填充并显示适当的范围。

private static JFreeChart buildChart(TimeSeriesCollection dataset,
    String title, boolean endPoints) throws IOException {

// Create the chart

    JFreeChart chart0 = ChartFactory.createTimeSeriesChart(
        title, "Hour", "Count", dataset, true, true, false);

// Setup the appearance of the chart
    chart0.setBackgroundPaint(Color.white);
    XYPlot plot = (XYPlot) chart0.getXYPlot();
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);
    plot.setAxisOffset(new RectangleInsets(10.0, 10.0, 10.0, 10.0));
    plot.setDomainCrosshairVisible(true);
    plot.setRangeCrosshairVisible(true);

// Display data points or just the lines?

    if (endPoints) {
        XYItemRenderer renderer = plot.getRenderer();
        if (renderer instanceof StandardXYItemRenderer) {
            StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer;

            rr.setBaseShapesVisible(true);
            rr.setBaseShapesFilled(true);
            rr.setDrawSeriesLineAsPath(true);
            rr.setSeriesPaint(0, Color.blue.brighter());
            rr.setSeriesVisible(0, true); // default
            rr.setSeriesVisibleInLegend(0, true);  // default

            NumberAxis domainAxis = new NumberAxis();
            domainAxis.setUpperMargin(0.15);
            domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
            domainAxis = (NumberAxis) plot.getDomainAxis();
            domainAxis = (NumberAxis) plot.getRangeAxis();
            domainAxis.setAutoRangeIncludesZero(false);
        }
    }

 // Tell the chart how we would like dates to read
    DateAxis axis = (DateAxis) plot.getDomainAxis();
    axis.setAutoRange(true);

 //axis.getDefaultAutoRange();
    axis.setDateFormatOverride(new SimpleDateFormat("HH:mm"));

    try {

        ChartUtilities.saveChartAsJPEG(new File("suc.jpg"), 1.0f, chart0, 990, 700);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return chart0;
}

下面是创建的图片,可以清楚的看到Y轴有重叠显示。

【问题讨论】:

  • 请编辑您的问题以包含一个sscce 来展示您描述的问题;使用显示的格式。
  • @trashgod -- 我已经设法解决了这个问题,通过读取数据点,找到最大数量,然后在 xyplot setRange() 方法下插入最大值,就像 plot.getRangeAxis().setRange( 1.0,苏克马克西);不幸的是,我是一个使用 stackoverflow 的小伙伴,所以我无法理解最后一条评论。

标签: java jfreechart


【解决方案1】:

我已经设法解决了这个问题,通过读取数据点,找到最大数量,然后在 xyplot setRange() 方法下插入最大值。

您不应该这样做。在下面的摘录中,为什么要获取域轴,参考范围轴替换它,然后更改范围轴?你的意思是改变域轴吗?看到这个相关的example

domainAxis = (NumberAxis) plot.getDomainAxis();
domainAxis = (NumberAxis) plot.getRangeAxis();
domainAxis.setAutoRangeIncludesZero(false);

附录:最小的sscce 显示随机数据的自动范围。

import java.awt.EventQueue;
import java.util.Random;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Day;
import org.jfree.data.time.Hour;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

/** @see https://stackoverflow.com/a/14198851/230513 */
public class Test {

    private static final int N = 10;
    private static final Random random = new Random();

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Test().display();
            }
        });
    }

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new ChartPanel(buildChart(createDataset(), "Title")));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private static TimeSeriesCollection createDataset() {

        final TimeSeries series = new TimeSeries("Data");
        Hour current = new Hour(0, new Day());
        for (int i = 0; i < N; i++) {
            series.add(current, random.nextGaussian());
            current = (Hour) current.next();
        }
        return new TimeSeriesCollection(series);
    }

    private static JFreeChart buildChart(
        TimeSeriesCollection dataset, String title) {
        JFreeChart chart = ChartFactory.createTimeSeriesChart(
            title, "Hour", "Count", dataset, true, true, false);
        XYPlot plot = chart.getXYPlot();
        plot.setDomainCrosshairVisible(true);
        plot.setRangeCrosshairVisible(true);
        return chart;
    }
}

【讨论】:

  • 我删除了该部分,并从我的文件中读取了数据点。对于范围轴和域轴,因为我正在从文件中读取两个轴,所以我必须为域轴执行此操作DateAxis axis = (DateAxis) plot.getDomainAxis(); axis.setTickUnit(new DateTickUnit(DateTickUnitType.HOUR, 1)); axis.setDateFormatOverride(new SimpleDateFormat("HH:mm")); 对于已删除的部分.. 我将其保留为渲染XYPlot plot = (XYPlot) chart0.getXYPlot(); plot.getDomainAxis().setAutoRange(true); plot.getRangeAxis().setRange(1.0, SucMaxi );
  • 请编辑您的问题以包含一个sscce,以显示您当前的方法。
  • 我目前的做法贴在下面
【解决方案2】:

这是我目前在图表上如何呈现数据的方法...

private static JFreeChart buildChart(TimeSeriesCollection dataset,
    String title, boolean endPoints) throws IOException {
// Create the chart
    JFreeChart chart0 = ChartFactory.createTimeSeriesChart(
        title,
        "Hour", "Count",
        dataset,
        true,
        true,
        false);

// Setup the appearance of the chart

    chart0.setBackgroundPaint(Color.white);
    XYPlot plot = (XYPlot) chart0.getXYPlot();
    plot.getDomainAxis().setAutoRange(true);
    plot.getRangeAxis().setRange(1.0, SucMaxi);
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);
    plot.getAxisOffset();
    plot.setAxisOffset(new RectangleInsets(10.0, 10.0, 10.0, 10.0));
    plot.setDomainCrosshairVisible(true);
    plot.setRangeCrosshairVisible(true);

// Display data points or just the lines?

    if (endPoints) {
        XYItemRenderer renderer = plot.getRenderer();
        if (renderer instanceof StandardXYItemRenderer) {
            StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer;
            rr.setBaseShapesVisible(true);
            rr.setBaseShapesFilled(true);
            rr.setDrawSeriesLineAsPath(true);
            rr.setSeriesPaint(0, Color.blue.brighter());
            rr.setSeriesVisible(0, true); // default
            rr.setSeriesVisibleInLegend(0, true);  // default      
        }
    }

// Tell the chart how we would like dates to read

    DateAxis axis = (DateAxis) plot.getDomainAxis();

// Tick the X Axis by unit tick 1 hour
    axis.setTickUnit(new DateTickUnit(DateTickUnitType.HOUR, 1));
    axis.setDateFormatOverride(new SimpleDateFormat("HH:mm"));

    try {
        ChartUtilities.saveChartAsJPEG(
            new File("suc.jpg"), 1.0f, chart0, 1000, 700);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return chart0;
}

【讨论】:

    猜你喜欢
    • 2013-04-26
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    • 2013-12-31
    • 1970-01-01
    相关资源
    最近更新 更多