Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.11 KB | None | 0 0
  1. import java.awt.BasicStroke;
  2. import java.awt.Color;
  3. import java.awt.Cursor;
  4. import java.awt.Dimension;
  5. import java.awt.Point;
  6. import java.awt.event.MouseEvent;
  7. import java.awt.event.MouseListener;
  8. import java.awt.event.MouseMotionListener;
  9. import java.awt.geom.Point2D;
  10. import java.awt.geom.Rectangle2D;
  11.  
  12. import org.jfree.chart.ChartFactory;
  13. import org.jfree.chart.ChartMouseEvent;
  14. import org.jfree.chart.ChartMouseListener;
  15. import org.jfree.chart.ChartPanel;
  16. import org.jfree.chart.ChartRenderingInfo;
  17. import org.jfree.chart.JFreeChart;
  18. import org.jfree.chart.axis.ValueAxis;
  19. import org.jfree.chart.entity.ChartEntity;
  20. import org.jfree.chart.entity.EntityCollection;
  21. import org.jfree.chart.entity.XYItemEntity;
  22. import org.jfree.chart.labels.StandardXYSeriesLabelGenerator;
  23. import org.jfree.chart.plot.XYPlot;
  24. import org.jfree.chart.renderer.xy.XYItemRenderer;
  25. import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
  26. import org.jfree.data.time.Month;
  27. import org.jfree.data.time.TimeSeries;
  28. import org.jfree.data.time.TimeSeriesCollection;
  29. import org.jfree.data.xy.XYDataset;
  30. import org.jfree.ui.ApplicationFrame;
  31. import org.jfree.ui.RefineryUtilities;
  32.  
  33.  
  34. @SuppressWarnings("serial")
  35. public class SeriesAndPointDragAndMove extends ApplicationFrame implements
  36. ChartMouseListener, MouseListener, MouseMotionListener {
  37.  
  38. public static void main(String[] paramArrayOfString) {
  39. SeriesAndPointDragAndMove seriesAndPointDragAndMove = new SeriesAndPointDragAndMove(
  40. "Series & Point Dragging Demo");
  41. seriesAndPointDragAndMove.pack();
  42. RefineryUtilities.centerFrameOnScreen(seriesAndPointDragAndMove);
  43. seriesAndPointDragAndMove.setVisible(true);
  44. }
  45.  
  46. boolean canMove = false;
  47. double finalMovePointY = 0;
  48. ChartRenderingInfo info = null;;
  49. double initialMovePointY = 0;
  50. JFreeChart jfreechart = null;
  51. ChartPanel localChartPanel = null;
  52. TimeSeries localTimeSeries = new TimeSeries("Series");
  53. TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
  54. XYItemEntity xyItemEntity = null;
  55.  
  56. public SeriesAndPointDragAndMove(String paramString) {
  57. super(paramString);
  58. jfreechart = ChartFactory.createTimeSeriesChart(
  59. "Series & Point Dragging Demo", "Date", "Price Per Unit",
  60. createDataset(), true, true, false);
  61. localChartPanel = new ChartPanel(jfreechart);
  62. localChartPanel.addChartMouseListener(this);
  63. localChartPanel.addMouseMotionListener(this);
  64. localChartPanel.addMouseListener(this);
  65. localChartPanel.setPreferredSize(new Dimension(750, 500));
  66. localChartPanel.setAutoscrolls(false);
  67. localChartPanel.setMouseZoomable(false);
  68. this.info = localChartPanel.getChartRenderingInfo();
  69. XYPlot localXYPlot = (XYPlot) jfreechart.getPlot();
  70. XYItemRenderer localXYItemRenderer = localXYPlot.getRenderer();
  71.  
  72. localXYItemRenderer.setSeriesStroke(0, new BasicStroke(2.0F));
  73.  
  74. XYLineAndShapeRenderer localXYLineAndShapeRenderer = (XYLineAndShapeRenderer) localXYPlot
  75. .getRenderer();
  76. localXYLineAndShapeRenderer.setBaseShapesVisible(true);
  77.  
  78. localXYLineAndShapeRenderer.setSeriesFillPaint(0, Color.white);
  79. localXYLineAndShapeRenderer.setUseFillPaint(true);
  80. localXYLineAndShapeRenderer
  81. .setLegendItemToolTipGenerator(new StandardXYSeriesLabelGenerator(
  82. "Tooltip {0}"));
  83. ValueAxis range = localXYPlot.getRangeAxis();
  84. range.setLowerBound(0); // set lower limit so that can't move in -ve
  85. // range
  86. range.setUpperBound(10000); // set upper limit as per app. needs
  87. setContentPane(localChartPanel);
  88. }
  89.  
  90. public void chartMouseClicked(ChartMouseEvent paramChartMouseEvent) {
  91. }
  92.  
  93. public void chartMouseMoved(ChartMouseEvent paramChartMouseEvent) {
  94. }
  95.  
  96. public XYDataset createDataset() {
  97. localTimeSeries.add(new Month(1, 2002), 500.19999999999999D);
  98. localTimeSeries.add(new Month(2, 2002), 694.10000000000002D);
  99. localTimeSeries.add(new Month(3, 2002), 734.39999999999998D);
  100. localTimeSeries.add(new Month(4, 2002), 453.19999999999999D);
  101. localTimeSeries.add(new Month(5, 2002), 200.19999999999999D);
  102. localTimeSeries.add(new Month(6, 2002), 345.60000000000002D);
  103. localTimeSeries.add(new Month(7, 2002), 500.19999999999999D);
  104. localTimeSeries.add(new Month(8, 2002), 694.10000000000002D);
  105. localTimeSeries.add(new Month(9, 2002), 734.39999999999998D);
  106. localTimeSeries.add(new Month(10, 2002), 453.19999999999999D);
  107. localTimeSeries.add(new Month(11, 2002), 500.19999999999999D);
  108. localTimeSeries.add(new Month(12, 2002), 345.60000000000002D);
  109. timeseriescollection.addSeries(localTimeSeries);
  110. return timeseriescollection;
  111. }
  112.  
  113. public void mouseClicked(MouseEvent e) {
  114. }
  115.  
  116. public void mouseDragged(MouseEvent e) {
  117. // at a time use one of them.
  118. //moveTimeSeries(e); // comment or uncomment to enable or disable series
  119. // movement
  120. movePoint(e); // comment or uncomment to enable or disable selected
  121. // point movement
  122. }
  123.  
  124. public void mouseEntered(MouseEvent e) {
  125. }
  126.  
  127. public void mouseExited(MouseEvent e) {
  128. canMove = false; // stop movement if cursor is moved out from the chart
  129. // area
  130. initialMovePointY = 0;
  131. localChartPanel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
  132. }
  133.  
  134. public void mouseMoved(MouseEvent e) {
  135. }
  136.  
  137. public void mousePressed(MouseEvent e) {
  138. int x = e.getX(); // initialized point whenenver mouse is pressed
  139. int y = e.getY();
  140. EntityCollection entities = this.info.getEntityCollection();
  141. ChartMouseEvent cme = new ChartMouseEvent(jfreechart, e, entities
  142. .getEntity(x, y));
  143. ChartEntity entity = cme.getEntity();
  144. if ((entity != null) && (entity instanceof XYItemEntity)) {
  145. xyItemEntity = (XYItemEntity) entity;
  146. } else if (!(entity instanceof XYItemEntity)) {
  147. xyItemEntity = null;
  148. return;
  149. }
  150. if (xyItemEntity == null) {
  151. return; // return if not pressed on any series point
  152. }
  153. Point pt = e.getPoint();
  154. XYPlot xy = jfreechart.getXYPlot();
  155. Rectangle2D dataArea = localChartPanel.getChartRenderingInfo()
  156. .getPlotInfo().getDataArea();
  157. Point2D p = localChartPanel.translateScreenToJava2D(pt);
  158. initialMovePointY = xy.getRangeAxis().java2DToValue(p.getY(), dataArea,
  159. xy.getRangeAxisEdge());
  160. canMove = true;
  161. localChartPanel.setCursor(new Cursor(Cursor.HAND_CURSOR));
  162. }
  163.  
  164. public void mouseReleased(MouseEvent e) {
  165. // stop dragging on mouse released
  166. canMove = false;
  167. initialMovePointY = 0;
  168. localChartPanel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
  169. }
  170. //This function calls during mouse dragging
  171. public void movePoint(MouseEvent me) {
  172. try {
  173. if (canMove) {
  174. int itemIndex = xyItemEntity.getItem();
  175. Point pt = me.getPoint();
  176. XYPlot xy = jfreechart.getXYPlot();
  177. Rectangle2D dataArea = localChartPanel.getChartRenderingInfo()
  178. .getPlotInfo().getDataArea();
  179. Point2D p = localChartPanel.translateScreenToJava2D(pt);
  180. finalMovePointY = xy.getRangeAxis().java2DToValue(p.getY(),
  181. dataArea, xy.getRangeAxisEdge());
  182. double difference = finalMovePointY - initialMovePointY;
  183. if (localTimeSeries.getValue(itemIndex).doubleValue()
  184. + difference > xy.getRangeAxis().getRange().getLength()
  185. || localTimeSeries.getValue(itemIndex).doubleValue()
  186. + difference < 0.0D) {
  187. initialMovePointY = finalMovePointY;
  188. }
  189. // retrict movement for upper and lower limit (upper limit
  190. // should be as per application needs)
  191. double targetPoint = localTimeSeries.getValue(itemIndex)
  192. .doubleValue()
  193. + difference;
  194. if (targetPoint > 10000 || targetPoint < 0) {
  195. return;
  196. } else
  197. localTimeSeries.update(itemIndex, targetPoint);
  198.  
  199. jfreechart.fireChartChanged();
  200. localChartPanel.updateUI();
  201. initialMovePointY = finalMovePointY;
  202. }
  203. } catch (Exception e) {
  204. System.out.println(e);
  205. }
  206. }
  207.  
  208. public void moveTimeSeries(MouseEvent me) {
  209. try {
  210. if (canMove) {
  211. Point pt = me.getPoint();
  212. XYPlot xy = jfreechart.getXYPlot();
  213. Rectangle2D dataArea = localChartPanel.getChartRenderingInfo()
  214. .getPlotInfo().getDataArea();
  215. Point2D p = localChartPanel.translateScreenToJava2D(pt);
  216. finalMovePointY = xy.getRangeAxis().java2DToValue(p.getY(),
  217. dataArea, xy.getRangeAxisEdge());
  218. double difference = finalMovePointY - initialMovePointY;
  219. for (int i = 0; i < localTimeSeries.getItemCount(); i++) {
  220. if (localTimeSeries.getValue(i).doubleValue() + difference > xy
  221. .getRangeAxis().getRange().getLength()
  222. || localTimeSeries.getValue(i).doubleValue()
  223. + difference < 0.0D) {
  224. initialMovePointY = finalMovePointY;
  225. }
  226. }
  227.  
  228. // retrict movement for upper and lower limit (upper limit
  229. // should be as per application needs)
  230. for (int i = 0; i < localTimeSeries.getItemCount(); i++) {
  231. double targetPoint = localTimeSeries.getValue(i)
  232. .doubleValue()
  233. + difference;
  234. if (targetPoint > 10000 || targetPoint < 0) {
  235. return;
  236. }
  237. }
  238. for (int i = 0; i < localTimeSeries.getItemCount(); i++) {
  239. double targetPoint = localTimeSeries.getValue(i)
  240. .doubleValue()
  241. + difference;
  242. localTimeSeries.update(i, targetPoint);
  243. }
  244. jfreechart.fireChartChanged();
  245. localChartPanel.updateUI();
  246. initialMovePointY = finalMovePointY;
  247. }
  248. } catch (Exception e) {
  249. System.out.println(e);
  250. }
  251. }
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement