鼠标拖动来修改JFreeChart 节点上的值

举报
风吹稻花香 发表于 2021/06/04 23:48:45 2021/06/04
【摘要】 http://www.thinksaas.cn/topics/0/84/84071.html package zhipu.view.xulie; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Point; import java.a...

http://www.thinksaas.cn/topics/0/84/84071.html


package zhipu.view.xulie;

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); // 双击竖线
 } try{//放在try块中可避免弹出异常提示,目前还没有找到得到XYItemEntity对象的好办法
 xyItemEntity = (XYItemEntity)e.getEntity();
 } catch(Exception ee){
ee.printStackTrace();
 } 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);
   }
}

文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/jacke121/article/details/72848936

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。