經過鼠標拖動來修改JFreeChart序列值java
最近因一個應用的須要,要經過鼠標的拖動來修改序列的值,在網上查了好多資料,都沒有找到好的實現,在CSDN也有很多這方面的詢問貼子。在參考網上前人資料和閱讀JFreeChart文檔的基礎,總算有了個初步的實現。發在這裏,但願能對別人有點借鑑,也但願獲得你們指點,進一步完善。dom
一 JFreeCJFreeChart的基本使用ssh
JFreeChart是一個開源的Java圖形開發包,它從柱形圖,餅形圖,到雷達圖,蠟燭圖等等無所不包,能夠用在C/S,B/S應用的開發。當前的JFreeChart的最新版本爲jfreechart-1.0.12。能夠到http://www.jfree.org/下載。測試
不論建立何種類型的圖表,JFreeChart都遵循如下幾個步驟:
一、創建Dataset。全部的數據都存放在Dataset中的ui
二、創建JFreeChart來表明要顯示的圖形,將dataset中的數據導入到JFreeChart中this
三、設置JFreeChart的顯示屬性。這一步可以使用默認的屬性。
四、圖片顯示。orm
二 重要的類和接口:對象
org.jfree.data.general.Dataset 全部數據源類都要實現的接口
org.jfree.chart.ChartFactory 由它來產生 JFreeChart 對象
org.jfree.chart.JFreeChart 全部對圖形的調整都是經過它來進行
org.jfree.chart.plot.Plot 經過JFreeChart 對象得到它,經過它對圖形外部部分進行調整接口
三.本文實現主要思路事件
本文的拖動修改數據主要是藉助XYItemEntity類的對象完成,每一個XYItemEntity都包含了咱們須要的重要信息:
XYDataset dataset:該節點所在數據集
int series:所在數據序列的序號
int item:所在序列中數據項的序號
因爲JFreeChart的鼠標監聽接口中沒有提供拖拽方法(也多是我還沒找到),因此本文先在JFreeChart的chartMouseClicked事件中獲得XYItemEntity的對象,而後在awt的MouseMotionListener監聽接口的mouseDragged方法中處理數據的拖動、修改和圖表更新。具體實現源碼以下:
TimeSeriesTest. java
//插入代碼的那個框框實在是很差使,改了幾回都改很差,直接把代碼粘爲文本
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.text.SimpleDateFormat;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartMouseEvent;
import org.jfree.chart.ChartMouseListener;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.XYItemEntity;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.time.Day;
import org.jfree.data.time.Hour;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RefineryUtilities;
public class TimeSeriesTest extends ApplicationFrame implements ChartMouseListener, MouseListener, MouseMotionListener{
private static final long serialVersionUID = 1L;
private JFreeChart chart;
private ChartPanel chartPanel;
private boolean domainCrosshairState;
private XYItemEntity xyItemEntity;
private boolean isDraged;
public TimeSeriesTest(String str){
super(str);
XYDataset localXYDataset = createDataset();
chart = createChart(localXYDataset);
chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new Dimension(800, 500));
// chartPanel.setMouseZoomable(true, false);
chartPanel.setMouseZoomable(false);
chartPanel.addChartMouseListener(this);
chartPanel.addMouseListener(this);
chartPanel.addMouseMotionListener(this);
domainCrosshairState = false;
isDraged = false;
setContentPane(chartPanel);
}
private static XYDataset createDataset(){
Day localDay = new Day();
TimeSeries localTimeSeries1 = new TimeSeries("隨機數據", Hour.class);
localTimeSeries1.add(new Hour(0, localDay), 520.2D);
localTimeSeries1.add(new Hour(2, localDay), 575.1D);
localTimeSeries1.add(new Hour(4, localDay), 564.4D);
localTimeSeries1.add(new Hour(6, localDay), 540.2D);
localTimeSeries1.add(new Hour(8, localDay), 530.2D);
localTimeSeries1.add(new Hour(10, localDay), 555.2D);
localTimeSeries1.add(new Hour(12, localDay), 580.4D);
localTimeSeries1.add(new Hour(14, localDay), 583.2D);
localTimeSeries1.add(new Hour(16, localDay), 595.2D);
localTimeSeries1.add(new Hour(18, localDay), 564.4D);
localTimeSeries1.add(new Hour(20, localDay), 523.2D);
localTimeSeries1.add(new Hour(22, localDay), 515.2D);
localTimeSeries1.add(new Hour(24, localDay), 530.4D);
TimeSeries localTimeSeries2 = new TimeSeries("隨機數據", Hour.class);
localTimeSeries2.add(new Hour(0, localDay), 620.2D);
localTimeSeries2.add(new Hour(2, localDay), 654.1D);
localTimeSeries2.add(new Hour(4, localDay), 664.4D);
localTimeSeries2.add(new Hour(6, localDay), 670.2D);
localTimeSeries2.add(new Hour(8, localDay), 690.2D);
localTimeSeries2.add(new Hour(10, localDay), 695.2D);
localTimeSeries2.add(new Hour(12, localDay), 680.4D);
localTimeSeries2.add(new Hour(14, localDay), 683.2D);
localTimeSeries2.add(new Hour(16, localDay), 695.2D);
localTimeSeries2.add(new Hour(18, localDay), 714.4D);
localTimeSeries2.add(new Hour(20, localDay), 723.2D);
localTimeSeries2.add(new Hour(22, localDay), 715.2D);
localTimeSeries2.add(new Hour(24, localDay), 680.4D);
TimeSeriesCollection localTimeSeriesCollection = new TimeSeriesCollection();
localTimeSeriesCollection.addSeries(localTimeSeries1);
localTimeSeriesCollection.addSeries(localTimeSeries2);
return localTimeSeriesCollection;
}
private static JFreeChart createChart(XYDataset xyDataset){
JFreeChart localChart = ChartFactory.createTimeSeriesChart("時間序列測試", "時間", "數值", xyDataset, true, true, false);
TextTitle title = localChart.getTitle();
title.setFont(new Font("宋體", Font.BOLD, 24));
LegendTitle legend = localChart.getLegend();
legend.setItemFont(new Font("宋體", Font.ITALIC, 14));
XYPlot plot = (XYPlot)localChart.getPlot();
plot.setBackgroundPaint(null);
// X 軸
DateAxis xAxis = (DateAxis)plot.getDomainAxis();
xAxis.setLabelFont(new Font("宋體", Font.PLAIN, 18));
xAxis.setLabelPaint(Color.BLUE);
SimpleDateFormat fmt = new SimpleDateFormat("k:mm");
xAxis.setTickUnit(new DateTickUnit(DateTickUnit.HOUR, 2, fmt));
// Y 軸
ValueAxis yAxis = plot.getRangeAxis();
yAxis.setLabelFont(new Font("宋體", Font.PLAIN, 18));
yAxis.setLabelPaint(Color.BLUE);
return localChart;
}
public static JPanel createDemoPanel(){
JFreeChart chart = createChart(createDataset());
return new ChartPanel(chart);
}
public void chartMouseClicked(ChartMouseEvent e){
if(e.getTrigger().getClickCount() == 2){
domainCrosshairState = !domainCrosshairState;
// plot.setDomainCrosshairVisible(domainCrosshairState);
chartPanel.setHorizontalAxisTrace(domainCrosshairState); // 雙擊豎線
}
xyItemEntity = (XYItemEntity)e.getEntity();
if(xyItemEntity == null) return;
int seriesIndex = xyItemEntity.getSeriesIndex();
XYPlot xyPlot = (XYPlot)chart.getPlot();
XYLineAndShapeRenderer xyLineAndShapeRenderer = (XYLineAndShapeRenderer)xyPlot.getRenderer();
for(int index = 0; index < xyItemEntity.getDataset().getSeriesCount(); index++){
xyLineAndShapeRenderer.setSeriesShapesVisible(index, false);
}
xyLineAndShapeRenderer.setSeriesShapesVisible(seriesIndex, true); // 數據點可見
}
public void mousePressed(MouseEvent e){
// System.out.println("mouse key is pressed!");
}
public void mouseReleased(MouseEvent e){
// System.out.println("mouse key is released!");
xyItemEntity = null;
isDraged = false;
}
public void mouseEntered(MouseEvent mouseEvent){
// System.out.println( "mouse key is pressed!");
}
public void mouseExited(MouseEvent mouseEvent){
// System.out.println( "mouse key is pressed!");
}
public void mouseClicked(MouseEvent mouseEvent){
// System.out.println( "mouse key is pressed!");
}
public void chartMouseMoved(ChartMouseEvent e){
}
public void mouseDragged(MouseEvent e){
if(xyItemEntity==null) return;
int seriesIndex = xyItemEntity.getSeriesIndex();
int itemIndex = xyItemEntity.getItem();
int xPos = e.getX();
int yPos = e.getY();
Point2D point2D = chartPanel.translateScreenToJava2D(new Point(xPos, yPos));
XYPlot xyPlot = (XYPlot)chart.getPlot();
ChartRenderingInfo chartRenderingInfo = chartPanel.getChartRenderingInfo();
Rectangle2D rectangle2D = chartRenderingInfo.getPlotInfo().getDataArea();
ValueAxis valueAxis = xyPlot.getRangeAxis();
RectangleEdge rectangleEdge = xyPlot.getRangeAxisEdge();
double value = valueAxis.java2DToValue(point2D.getY(), rectangle2D, rectangleEdge);
TimeSeriesCollection tsc = (TimeSeriesCollection)xyItemEntity.getDataset();
TimeSeries ts = tsc.getSeries(seriesIndex);
ts.update(itemIndex,value);
isDraged = true;
}
public void mouseMoved(MouseEvent e){
}
public static void main(String[] paramArrayOfString){
TimeSeriesTest dragValueApp = new TimeSeriesTest("時間序列拖動示例");
dragValueApp.pack();
RefineryUtilities.centerFrameOnScreen(dragValueApp);
dragValueApp.setVisible(true);
}
}
以上是本人研習兩天摸索出來的,寫在這裏算是拋磚引玉,還請各位改進不足之處。
PS
無版權,可自由修改;