Adding years to X axis on line chart using jFreePanel

允我心安 提交于 2019-12-25 01:25:16

问题


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.

So, instead of...

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

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.


回答1:


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"));

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



来源:https://stackoverflow.com/questions/27219852/adding-years-to-x-axis-on-line-chart-using-jfreepanel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!