我正在尝试使用jfree图表绘制图表。要求是根据日期绘制时间值,如图所示。我无法弄清楚如何在一个轴上使用时间和在另一个轴上使用日期,因为在构建数据系列时,.add()方法似乎不提供日期和时间的参数。我知道我可以传递日期和刻度值,但我不想在轴上显示那些刻度值,我想要像下面这样的东西
注意:图像显示的是线条,但这些实际上是相互靠近的点,使它看起来像线条。所以基本上我需要画一个散点图。
答案 0 :(得分:2)
只需创建一个常规散点图,其中x和y值为"自1970年1月1日起的毫秒数" (java.util.Date使用的编码)。然后使用DateAxis实例替换图表上的轴。这是一个例子:
package org.jfree.chart.demo;
import java.awt.Color;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickMarkPosition;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Day;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.RefineryUtilities;
public class StackOverflow26556268 extends ApplicationFrame {
private static final long serialVersionUID = 1L;
public StackOverflow26556268(String title) {
super(title);
ChartPanel chartPanel = (ChartPanel) createDemoPanel();
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
private static JFreeChart createChart(XYDataset dataset) {
JFreeChart chart = ChartFactory.createScatterPlot(
"StackOverflow26556268", "Date", "Time", dataset);
chart.removeLegend();
chart.setBackgroundPaint(Color.white);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);
DateAxis xAxis = new DateAxis("Date");
xAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
plot.setDomainAxis(xAxis);
plot.setRangeAxis(new DateAxis("Time"));
return chart;
}
private static XYDataset createDataset() {
XYSeries s1 = new XYSeries("S1");
s1.add(new Day(1, 10, 2014).getMiddleMillisecond(), 720000);
s1.add(new Day(1, 10, 2014).getMiddleMillisecond(), 820000);
s1.add(new Day(2, 10, 2014).getMiddleMillisecond(), 1020000);
s1.add(new Day(2, 10, 2014).getMiddleMillisecond(), 920000);
s1.add(new Day(3, 10, 2014).getMiddleMillisecond(), 1220000);
s1.add(new Day(3, 10, 2014).getMiddleMillisecond(), 1320000);
s1.add(new Day(4, 10, 2014).getMiddleMillisecond(), 1620000);
s1.add(new Day(4, 10, 2014).getMiddleMillisecond(), 1520000);
s1.add(new Day(5, 10, 2014).getMiddleMillisecond(), 1320000);
s1.add(new Day(5, 10, 2014).getMiddleMillisecond(), 1820000);
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(s1);
return dataset;
}
public static JPanel createDemoPanel() {
JFreeChart chart = createChart(createDataset());
ChartPanel panel = new ChartPanel(chart);
panel.setFillZoomRectangle(true);
panel.setMouseWheelEnabled(true);
return panel;
}
public static void main(String[] args) {
StackOverflow26556268 demo = new StackOverflow26556268(
"StackOverflow26556268/Example");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}