我已经设法使用 jfreechart 绘制了一个带有几个点的 xy 图表。
我想要做的是能够单击已绘制的线条上的任意位置并获取其 x 或 y 轴值。
有人可以帮帮我吗?这是我第一次使用 j freechart,我感到有些失落。
到目前为止,我创建了数据集并生成了图表。
TimeSeries s = new TimeSeries("security", Day.class);
while (rate_i.hasNext()) {
rate r = (rate) rate_i.next();
Calendar cal = Calendar.getInstance();
cal.setTime(r.d);
int month = cal.get(Calendar.MONTH) + 1;
int day = cal.get(Calendar.DATE);
int year = cal.get(Calendar.YEAR);
s.add(new Day(day, month, year), r.rate);
}
TimeSeriesCollection ds = new TimeSeriesCollection();
ds.addSeries(s);
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Security Performance over time.", // title
"Date", // x-axis label
"Value", // y-axis label
ds, // data
true, // create legend?
true, // generate tooltips?
false // generate URLs?
);
XYPlot xyplot = (XYPlot) chart.getPlot();
xyplot.setDomainPannable(true);
xyplot.setRangePannable(false);
xyplot.setDomainCrosshairVisible(true);
xyplot.setRangeCrosshairVisible(true);
org.jfree.chart.renderer.xy.XYItemRenderer xyitemrenderer = xyplot
.getRenderer();
if (xyitemrenderer instanceof XYLineAndShapeRenderer) {
XYLineAndShapeRenderer xylineandshaperenderer =
(XYLineAndShapeRenderer) xyitemrenderer;
xylineandshaperenderer.setBaseShapesVisible(false);
}
DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis();
dateaxis.setDateFormatOverride(
new SimpleDateFormat("EEE, MMM d, ''yy"));
ChartFrame frame = new ChartFrame("Chart", chart);
frame.setVisible(true);
frame.setSize(700, 900);