0

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.

Iakona
  • 157
  • 2
  • 12
  • Please edit your question to include a [complete example](http://stackoverflow.com/help/mcve) that exhibits the problem you describe. – trashgod Dec 01 '14 at 05:48
  • @trashgod I hope that helps. – Iakona Dec 01 '14 at 12:10
  • Since I was not getting responses, I was able to talk my boss into buying the JFreeChart Developer Guide. If anyone needs to accomplish anything similar, I guess it's necessary. They have many examples of different graphs and provide the code so you can see how they produced the graph. If you will be doing many graphs in Java, I highly recommend the guide. – Iakona Dec 03 '14 at 19:31
  • I [agree](http://stackoverflow.com/a/21816284/230513); it's a good investment for professional developers. – trashgod Dec 03 '14 at 20:32

1 Answers1

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • That first example is kinda along the lines. It's changing the x-axis. What I'm trying to do is only populate the first entry of each year with just the year, the all the other 364 days has nothing. If I could just mark the changing of the year along the x-axis I'd be set. – Iakona Dec 02 '14 at 14:50