使用jFreePanel向折线图上的X轴添加年份 [英] Adding years to X axis on line chart using jFreePanel

查看:40
本文介绍了使用jFreePanel向折线图上的X轴添加年份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我浏览了其他示例,但没有找到可以回答此问题的示例.我在Java中使用jFreePanel.我正在尝试创建一个折线图,其X轴标记为年份变化(例如:2005、2006、2007等).数据由每天获取的读数组成,因此不可能指出每个读数,但按年份将其细分似乎是非常合理的.不过,我正在努力弄清楚该如何做.

I have looked through other examples but not found one that answers this question. I'm using jFreePanel in java. I'm trying to create a line chart with the X-axis labeled marking the year change (ie: 2005, 2006, 2007, etc). The data is made up of readings taken daily, so it would not be possible to indicate each one, but breaking them down by year seems very reasonable. I'm struggling to figure out how to do it, though.

所以,而不是...

X-axis __________________________________________________________________________________

                                 2000-01-01 to 2009-06-30

应该看起来像这样...

It should look like this...

X-axis __________________________________________________________________________________
      2000     2001     2002    2003    2004    2005    2006    2007    2008    2009
                                 2000-01-01 to 2009-06-30

相关代码在这里...

The relevant code is here...

private JFreeChart createChart(final CategoryDataset dataset) {

    // create the chart...
    final JFreeChart chart = ChartFactory.createLineChart(
        site,                       // chart title
        firstDate + " to " + DBChart.lastDate,  // domain axis label
        "Height",                   // range axis label
        dataset,                    // data
        PlotOrientation.VERTICAL,   // orientation
        false,                      // exclude legend
        false,                      // tooltips
        false                       // urls
    );

    chart.setBackgroundPaint(Color.white);

    final CategoryPlot plot = (CategoryPlot) chart.getPlot();
    plot.setBackgroundPaint(Color.lightGray);
    plot.setRangeGridlinePaint(Color.white);

    // Customize the range axis...
    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    chart.getCategoryPlot().getRangeAxis().setRange(getLowestLow(data), getHighestHigh(data));
    rangeAxis.setAutoRangeIncludesZero(false);

    // Customize the renderer...
    final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
    renderer.setSeriesShapesVisible(0, false);   

    return chart;
}

private double getLowestLow(Object data[][]) {
    double lowest;
    lowest = Double.parseDouble(data[0][2].toString());
    System.out.println(lowest);
    for (int i = 1; i < data.length - 1; i++) {
        if (data[i][2] != null) {
            if (Double.parseDouble(data[i][2].toString()) < lowest) {
                lowest = Double.parseDouble(data[i][2].toString());
            }
        }
    }

    return lowest;
}

private double getHighestHigh(Object data[][]) {
    double highest;
    highest = Double.parseDouble(data[0][2].toString());
    for (int i = 1; i < data.length - 1; i++) {
        if (data[i][2] != null) {
            if (Double.parseDouble(data[i][2].toString()) > highest) {
                highest = Double.parseDouble(data[i][2].toString());
            }
        }
    }
    return highest;
}

由于有了另一个线程,我能够自定义Y轴,但是找不到关于如何使用X轴进行操作的信息,而且我以前从未使用过此库.任何帮助将不胜感激.

Thanks to another thread, I'm able to customize the Y-axis, but I'm not finding information on how to do what I want with the X-axis and I've never used this library before. Any help would be appreciated.

推荐答案

考虑使用 createTimeSeriesChart(),而不是 ChartFactory.createLineChart .然后,您可以使用 setDateFormatOverride()自然地格式化日期轴,如此处所示.

Instead of ChartFactory.createLineChart, consider createTimeSeriesChart(). Then you can format the date axis naturally, as shown here using setDateFormatOverride().

DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("HH:mm"));

我不确定多线轴;或者,考虑 TextTitle DateTitle ,如此处所示.

I'm not sure about a multiline axis; alternatively, consider a TextTitle or DateTitle, as shown here.

这篇关于使用jFreePanel向折线图上的X轴添加年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆