/** * Receives notification of mouse clicks on the panel. These are translated and passed on to any * registered {@link ChartMouseListener}s. * * @param event * Information about the mouse event. */ @Override public void mouseClicked(MouseEvent event) { Insets insets = getInsets(); int x = (int) ((event.getX() - insets.left) / this.scaleX); int y = (int) ((event.getY() - insets.top) / this.scaleY); this.anchor = new Point2D.Double(x, y); if (this.chart == null) { return; } this.chart.setNotify(true); // force a redraw // new entity code... Object[] listeners = this.chartMouseListeners.getListeners(ChartMouseListener.class); if (listeners.length == 0) { return; } ChartEntity entity = null; if (this.info != null) { EntityCollection entities = this.info.getEntityCollection(); if (entities != null) { entity = entities.getEntity(x, y); } } ChartMouseEvent chartEvent = new ChartMouseEvent(getChart(), event, entity); for (int i = listeners.length - 1; i >= 0; i -= 1) { ((ChartMouseListener) listeners[i]).chartMouseClicked(chartEvent); } }
/** * Implementation of the MouseMotionListener's method. * * @param e * the event. */ @Override public void mouseMoved(MouseEvent e) { Graphics2D g2 = (Graphics2D) getGraphics(); if (this.horizontalAxisTrace) { drawHorizontalAxisTrace(g2, e.getX()); } if (this.verticalAxisTrace) { drawVerticalAxisTrace(g2, e.getY()); } g2.dispose(); Object[] listeners = this.chartMouseListeners.getListeners(ChartMouseListener.class); if (listeners.length == 0) { return; } Insets insets = getInsets(); int x = (int) ((e.getX() - insets.left) / this.scaleX); int y = (int) ((e.getY() - insets.top) / this.scaleY); ChartEntity entity = null; if (this.info != null) { EntityCollection entities = this.info.getEntityCollection(); if (entities != null) { entity = entities.getEntity(x, y); } } // we can only generate events if the panel's chart is not null // (see bug report 1556951) if (this.chart != null) { ChartMouseEvent event = new ChartMouseEvent(getChart(), e, entity); for (int i = listeners.length - 1; i >= 0; i -= 1) { ((ChartMouseListener) listeners[i]).chartMouseMoved(event); } } }
/** * Adds a listener to the list of objects listening for chart mouse events. * * @param listener * the listener (<code>null</code> not permitted). */ @Override public void addChartMouseListener(ChartMouseListener listener) { if (listener == null) { throw new IllegalArgumentException("Null 'listener' argument."); } this.chartMouseListeners.add(ChartMouseListener.class, listener); }
/** * Receives notification of mouse clicks on the panel. These are translated and passed on to any registered * {@link ChartMouseListener}s. * * @param event * Information about the mouse event. */ @Override public void mouseClicked(MouseEvent event) { Insets insets = getInsets(); int x = (int) ((event.getX() - insets.left) / this.scaleX); int y = (int) ((event.getY() - insets.top) / this.scaleY); this.anchor = new Point2D.Double(x, y); if (this.chart == null) { return; } this.chart.setNotify(true); // force a redraw // new entity code... Object[] listeners = this.chartMouseListeners.getListeners(ChartMouseListener.class); if (listeners.length == 0) { return; } ChartEntity entity = null; if (this.info != null) { EntityCollection entities = this.info.getEntityCollection(); if (entities != null) { entity = entities.getEntity(x, y); } } ChartMouseEvent chartEvent = new ChartMouseEvent(getChart(), event, entity); for (int i = listeners.length - 1; i >= 0; i -= 1) { ((ChartMouseListener) listeners[i]).chartMouseClicked(chartEvent); } }
/** * Ugly code creating the graph, layer and all that stuff */ private void createGraph() { //graphedValues = new TimeSeries(item.getUnit()); //graphedValues = new TimeSeries(item.getUnit(), Second.class); //graphedValues.setMaximumItemAge(3000); //graphedValues.setMaximumItemCount(30); dataset= new TimeSeriesCollection(); //dataset.addSeries(this.graphedValues); dataset.addSeries(graphedValues); DateAxis domain = new DateAxis("Time"); NumberAxis range = new NumberAxis(item.getUnit()); //range.setRange(item.getMinValue(), item.getMaxValue()); range.setAutoRange(true); domain.setTickLabelFont(new Font("SansSerif", Font.PLAIN, 12)); range.setTickLabelFont(new Font("SansSerif", Font.PLAIN, 12)); domain.setLabelFont(new Font("SansSerif", Font.PLAIN, 14)); range.setLabelFont(new Font("SansSerif", Font.PLAIN, 14)); XYItemRenderer renderer = new XYLineAndShapeRenderer(true,true); renderer.setSeriesPaint(0, Color.RED); //renderer.setSeriesPaint(1, Color.GREEN); renderer.setBaseStroke(new BasicStroke(3f, BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL)); XYPlot plot = new XYPlot(dataset, domain, range, renderer); plot.setBackgroundPaint(Color.lightGray); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.white); plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0)); domain.setAutoRange(true); domain.setLowerMargin(0.0); domain.setUpperMargin(0.0); domain.setTickLabelsVisible(true); range.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); JFreeChart chart = new JFreeChart("(Id: " + item.getItemId() + ")", new Font("SansSerif",Font.BOLD, 24), plot, true); //List<String> subtitles = new List<String>(); //subtitles.add("(on " + device.getName() + ")"); //chart.setSubtitles(subtitles); chart.setBackgroundPaint(Color.white); chartPanel = new ChartPanel(chart); chartPanel.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createEmptyBorder(4, 4, 4, 4), BorderFactory.createLineBorder(Color.black))); chartPanel.setSize(new Dimension(400, 400)); chartPanel.addChartMouseListener(new ChartMouseListener(){ public void chartMouseClicked(ChartMouseEvent e){ try{ XYItemEntity xyitem=(XYItemEntity) e.getEntity(); double x = dataset.getXValue(xyitem.getSeriesIndex(), xyitem.getItem()); double y = dataset.getYValue(xyitem.getSeriesIndex(), xyitem.getItem()); JOptionPane.showMessageDialog(new JFrame(), ""+y+" :: "+new Second(new Date((long)x))); } catch(Exception exception ) { } } public void chartMouseMoved(ChartMouseEvent arg0) { } }); }
/** * Removes a listener from the list of objects listening for chart mouse events. * * @param listener * the listener. */ @Override public void removeChartMouseListener(ChartMouseListener listener) { this.chartMouseListeners.remove(ChartMouseListener.class, listener); }
/** * Adds a listener to the list of objects listening for chart mouse events. * * @param listener the listener (<code>null</code> not permitted). */ public void addChartMouseListener(ChartMouseListener listener) { this.chartMouseListeners.add(ChartMouseListener.class, listener); }
/** * Removes a listener from the list of objects listening for chart mouse * events. * * @param listener the listener. */ public void removeChartMouseListener(ChartMouseListener listener) { this.chartMouseListeners.remove(ChartMouseListener.class, listener); }