功能的需求:
1,将数据同时显示table和曲线图
2,在曲线图中,将某些特定的数据点,以不同的颜色显示
3,在table中的选中一行数据,同步在曲线图中高亮显示;在曲线图中单击一个数据点,同步在table中高亮显示改行数据
用法,使用JfreeChart.jar开源包
private XYDataset createxyDataset (){ //创建XYDataset
XYSeries xySeries1 = new XYSeries("series1");//所有数据
XYSeries xySeries2 = new XYSeries("series2");//需要特定显示的数据集
//xySeries1.add(,) 添加数据
//xySeries2.add(,)
XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
xySeriesCollection.addSeries(xySeries1);
xySeriesCollection.addSeries(xySeries2);
return xySeriesCollection;
}
private Jfreechart creatChart (){//创建Jfreechart
Jfreechart chart = ChartFactory.createXYLineChart("Test", "X Value", "Y Value", createxyDataset(), PlotOrientation.VERTICAL, true, true, false);
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
//设置基本数据的曲线状态为红色,用直线连接数据点,数据点不可见
renderer.setSeriesPaint(0, Color.RED);
renderer.setSeriesLinesVisible(0, true);
renderer.setSeriesShapesVisible(0, false);
//设置特定数据的曲线状态为蓝色,不用直线连接数据点,数据点可见
renderer.setSeriesPaint(1, Color.BLUE);
renderer.setSeriesLinesVisible(1, false);
renderer.setSeriesShapesVisible(1, true);
renderer.setSeriesShape(1, new Ellipse2D.Double(-1.0, -1.0, 1.0, 1.0));
renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
XYPlot localXYPlot = (XYPlot) chart.getPlot();
localXYPlot.setRenderer(renderer);
}
private ChartPanel getChartPanel () {
JfreeChart chart = creatChart();
ChartPanel chartPanel = new ChartPanel(chart , ChartPanel.DEFAULT_WIDTH, ChartPanel.DEFAULT_HEIGHT, ChartPanel.DEFAULT_MINIMUM_DRAW_WIDTH, ChartPanel.DEFAULT_MINIMUM_DRAW_HEIGHT, ChartPanel.DEFAULT_MAXIMUM_DRAW_WIDTH, ChartPanel.DEFAULT_MAXIMUM_DRAW_HEIGHT, ChartPanel.DEFAULT_BUFFER_USED, false, false, false, false, false, true) {
public void mouseClicked(MouseEvent e) {
if (!e.isMetaDown()) {
super.mouseClicked(e);
this.setAnchor(getNearestValuePoint(chartPanel.getAnchor()));// 设置为单击最chart上的点时,找最近的数据点
}
}
};
chartPanel.setDisplayToolTips(true);
chartPanel.setDoubleBuffered(true);
}
return chartPanel;
}
private Point2D getNearestValuePoint (Point2D anchor) {
Plot reportPlot = chart.getPlot();
PlotRenderingInfo reportRenderInfo = chartPanel.getChartRenderingInfo().getPlotInfo();
if (reportPlot instanceof XYPlot) {
XYPlot xyPlot = (XYPlot) reportPlot;
double hvalue = Double.NEGATIVE_INFINITY;
ValueAxis xaxis = null;
PlotRenderingInfo subplotInfo = null;
subplotInfo = reportRenderInfo;
Rectangle2D dataArea = subplotInfo.getDataArea();
if (dataArea.contains(anchor.getX(), anchor.getY())) {
// set the anchor value for the horizontal axis...
xaxis = xyPlot.getDomainAxis();
if (xaxis != null) {
hvalue = xaxis.java2DToValue(anchor.getX(), subplotInfo.getDataArea(), xyPlot.getDomainAxisEdge());
}
}
double clickFreq = hvalue;
if (hvalue != Double.NEGATIVE_INFINITY && xaxis != null) {
double clickxValue; //根据数据集中,计算离单击点最近的数据点,省略计算方法,该数据点clickxValue值
double xvalue = xaxis.valueToJava2D(clickFreq, subplotInfo.getDataArea(), xyPlot.getDomainAxisEdge());
//在Chart上高亮显示单击的数据点,以在x轴上画直线的方式高亮显示
xyPlot.clearDomainMarkers();
ValueMarker marker = new ValueMarker(clickFreq, Color.blue, new BasicStroke(1.0f));
xyPlot.addDomainMarker(marker, Layer.FOREGROUND);
highLightTable(clickxValue);//同步显示tabel,省略该方法
return new Point2D.Double(xvalue, anchor.getY());
}
}
}