我想知道,是否可以将两个参数都设置为日期,在本示例中,它将第一个参数设为可比较,将第二个参数设为double。但我希望将双精度显示为日期。有没有可以使用的类。如果不是,是否有其他方法将两者都显示为日期。例如,我需要同时使用x轴和y轴作为日期。
为了 data.addValue("8/4/2012" ,7.0)
data.addValue("8/4/2012" ,7.0)
我想要这样(“ 8/4/2012 20:06:02”,“ 8/5/2012”)->下图是否可能?
在此先感谢您。
public class Example1 { public static void main(String args[]){ DefaultKeyedValues data = new DefaultKeyedValues(); data.addValue("8/4/2012" ,7.0); data.addValue("19/04/2012",5.0); CategoryDataset dataset = DatasetUtilities.createCategoryDataset("Population", data); JFreeChart chart = ChartFactory.createBarChart("Population","Date","Population",dataset,PlotOrientation.VERTICAL,true,true,false); ChartFrame frame = new ChartFrame("Test", chart); //Switch from a Bar Rendered to a LineAndShapeRenderer so the chart looks like an XYChart LineAndShapeRenderer renderer = new LineAndShapeRenderer(); renderer.setBaseLinesVisible(false); //TUrn of the lines CategoryPlot plot = (CategoryPlot) chart.getPlot(); plot.setRenderer(0, renderer); NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis(); numberAxis.setRange(new Range(0,10)); frame.pack(); frame.setVisible(true); } }
而不是a NumberAxis,请使用DateAxis。那将使您DateFormat在setDateFormatOverride()方法中使用a 。
NumberAxis
DateAxis
DateFormat
setDateFormatOverride()
更新:中有一个完整的示例org.jfree.chart.demo.TimeSeriesChartDemo1。您可能想要createLineChart()。您会在这里让范围轴显示日期。
org.jfree.chart.demo.TimeSeriesChartDemo1
createLineChart()
public class Example1 { public static void main(String args[]) { DefaultKeyedValues data = new DefaultKeyedValues(); data.addValue("8/4/2012", new Day(8, 4, 2012).getFirstMillisecond()); data.addValue("19/04/2012", new Day(19, 4, 2012).getFirstMillisecond()); CategoryDataset dataset = DatasetUtilities .createCategoryDataset("Population", data); JFreeChart chart = ChartFactory.createLineChart("Population", "Date", "Population", dataset, PlotOrientation.VERTICAL, true, true, false); CategoryPlot plot = (CategoryPlot) chart.getPlot(); LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer(); renderer.setBaseShapesVisible(true); renderer.setBaseLinesVisible(false); DateAxis range = new DateAxis("Date"); range.setDateFormatOverride(new SimpleDateFormat("dd/MM/yyyy")); plot.setRangeAxis(range); ChartFrame frame = new ChartFrame("Test", chart); frame.pack(); frame.setVisible(true); } }