最近做个项目要用到jFreechart,所以在用的过程中的一些东西记录下来,方便自己以后查询
1.图形平移
private void moveRightActionPerformed(java.awt.event.ActionEvent evt) {
Rectangle2D screenDataArea = chartPanel.getScreenDataArea();
screenDataArea.setRect(screenDataArea.getX() + MOVE_LEFT_STEP, screenDataArea.getY(), screenDataArea.getWidth(), screenDataArea.getHeight());
chartPanel.zoom(screenDataArea);
chartPanel.repaint();
}
2.通过鼠标对legend进行操作
……
chartPanel.addChartMouseListener(new MouseListener());//添加鼠标监听响应
……
private class MouseListener implements ChartMouseListener {
public void chartMouseClicked(ChartMouseEvent event) {
if (event.getTrigger().getClickCount() == 2) { //双击相应
ChartEntity localChartEntity = event.getEntity();
if (localChartEntity instanceof LegendItemEntity) { // 判别是否是legend
LegendItemEntity legendItemEntity = (LegendItemEntity) localChartEntity;
XYPlot xyPlot = (XYPlot) chart.getPlot();
TimeSeriesCollection historyDataset = (TimeSeriesCollection) xyPlot.getDataset(HISTROY_LINE); // 数据集
XYLineAndShapeRenderer historyXyLineAndShapeRenderer = (XYLineAndShapeRenderer) xyPlot.getRenderer(HISTROY_LINE); // 画线
// 其他操作……
}
}
public void chartMouseMoved(ChartMouseEvent event) {
}
}
3.多坐标轴操作
private JFreeChart createChart() {
JFreeChart chart = ChartFactory.createTimeSeriesChart("综合曲线面板", "历史曲线时间", "历史曲线值", historyDataset, true, true, false);
chart.getTitle().setFont(titleFont);
XYPlot xyPlot = (XYPlot) chart.getPlot();
DateAxis histroyDataAxis = new DateAxis("历史曲线时间");
histroyDataAxis.setAutoRange(true);
histroyDataAxis.setLabelFont(axisFont);
histroyDataAxis.setTickLabelFont(axisFont);
xyPlot.setDomainAxis(HISTROY_LINE, histroyDataAxis);
ValueAxis historyRangeAxis = new NumberAxis("历史数据值");
historyRangeAxis.setLabelFont(axisFont);
historyRangeAxis.setTickLabelFont(axisFont);
xyPlot.setRangeAxis(HISTROY_LINE,historyRangeAxis);
XYLineAndShapeRenderer historyXyLineAndShapeRenderer = new XYLineAndShapeRenderer();
historyXyLineAndShapeRenderer.setBaseShapesVisible(true);
int i = 0;
xyPlot.setRenderer(HISTROY_LINE, historyXyLineAndShapeRenderer);
realTimeDataset = new TimeSeriesCollection();
xyPlot.setDataset(REAL_TIME_LINE, realTimeDataset);
ValueAxis realTimeValueaxis = new DateAxis("实时时间");
realTimeValueaxis.setLabelFont(axisFont);
//自动设置数据轴数据范围
realTimeValueaxis.setAutoRange(true);
//数据轴固定数据范围 30s
realTimeValueaxis.setFixedAutoRange(30000D);
xyPlot.setDomainAxis(REAL_TIME_LINE, realTimeValueaxis);
ValueAxis realTimeRangeAxis = new NumberAxis("实时数据值");
realTimeRangeAxis.setLabelFont(axisFont);
xyPlot.setRangeAxis(REAL_TIME_LINE, realTimeRangeAxis);
XYLineAndShapeRenderer realTimeXyLineAndShapeRenderer = new XYLineAndShapeRenderer();
//historyXyLineAndShapeRenderer.setBaseShapesVisible(true);
realTimeXyLineAndShapeRenderer.setUseFillPaint(true);
realTimeXyLineAndShapeRenderer.setLegendItemToolTipGenerator(new StandardXYSeriesLabelGenerator("history {0}"));
xyPlot.setRenderer(REAL_TIME_LINE, realTimeXyLineAndShapeRenderer);
// 数据映射
xyPlot.setDomainAxisLocation(REAL_TIME_LINE, AxisLocation.TOP_OR_RIGHT);
xyPlot.setRangeAxisLocation(REAL_TIME_LINE,AxisLocation.TOP_OR_RIGHT);
xyPlot.setDomainAxisLocation(HISTROY_LINE, AxisLocation.BOTTOM_OR_LEFT);
xyPlot.setRangeAxisLocation(HISTROY_LINE, AxisLocation.BOTTOM_OR_LEFT);
xyPlot.mapDatasetToDomainAxis(REAL_TIME_LINE, REAL_TIME_LINE);
xyPlot.mapDatasetToRangeAxis(REAL_TIME_LINE, REAL_TIME_LINE);
xyPlot.mapDatasetToDomainAxis(HISTROY_LINE, HISTROY_LINE);
xyPlot.mapDatasetToRangeAxis(HISTROY_LINE, HISTROY_LINE);
return chart;
4.
jfreechart中,series是表示每个线,他有个自己的key,另外series还有自己在render中的一个index,两者转化靠Dataset中indexOf()函数