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.