是否可以在用JFreechart绘制的Timeseries -Chart 中用非周期性的时间间隔标记 x 轴?
我已经制作了一个可编译的最小示例。在这个例子中是指出我想要的时间间隔的注释。
我已经尝试过的是扩展RegularTimePeriod -class。由于一些错误,我放弃了这种方法,而是想问是否有更简单的方法来做到这一点。
这是最小的示例(注意:它缺少导入):
public class MyTimeSeriesGraphMinimalExample {
public static void main(String args[]) {
TimeSeries timeseries = new TimeSeries("Series 1");
//these two should form one interval
timeseries.add(new Day(1,1,2013),10);//1
timeseries.add(new Day(5,1,2013),10);//2
//these two should form one interval
timeseries.add(new Day(11,1,2013),9);//1
timeseries.add(new Day(14,1,2013),9);//2
TimeSeries timeseries1 = new TimeSeries("Series 2");
//these two should form one interval
timeseries1.add(new Day(1, 1,2013), 5);//1
timeseries1.add(new Day(5, 1,2013), 5);//2
//these two should form one interval
timeseries1.add(new Day(11, 1,2013), 8);//1
timeseries1.add(new Day(14, 1,2013), 8);//2
TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
timeseriescollection.addSeries(timeseries);
timeseriescollection.addSeries(timeseries1);
XYDataset xydataset = timeseriescollection;
//chart-visual-property-settings
JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(
"Time Series Demo 3", "Time", "Value", xydataset, true, true,
false);
XYPlot xyplot = (XYPlot) jfreechart.getPlot();
DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis();
dateaxis.setTickUnit(new DateTickUnit(DateTickUnitType.DAY, 1,
new SimpleDateFormat("dd-MMM")));
dateaxis.setVerticalTickLabels(true);
XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyplot
.getRenderer();
xylineandshaperenderer.setBaseShapesVisible(true);
xylineandshaperenderer.setSeriesFillPaint(0, Color.red);
xylineandshaperenderer.setSeriesFillPaint(1, Color.green);
xylineandshaperenderer.setSeriesPaint(0, Color.red);
xylineandshaperenderer.setSeriesPaint(1, Color.green);
xylineandshaperenderer.setUseFillPaint(true);
xylineandshaperenderer
.setLegendItemToolTipGenerator(new StandardXYSeriesLabelGenerator(
"Tooltip {0}"));
//draw
try {
ChartUtilities.saveChartAsJPEG(new File("C:/minimalExampleChart.jpeg"),
jfreechart, 600, 500);
} catch (Exception e) {
// TODO: handle exception
}
}
}