/** * Adds an entity to the collection. * * @param entities the entity collection being populated. * @param area the entity area (if <code>null</code> a default will be used). * @param entityX the entity's center x-coordinate in user space. * @param entityY the entity's center y-coordinate in user space. * @param dataset the dataset. * @param series the series. * @param item the item. */ protected void addEntity(EntityCollection entities, Shape area, XYDataset dataset, int series, int item, double entityX, double entityY) { if (area == null) { area = new Ellipse2D.Double( entityX - this.defaultEntityRadius, entityY - this.defaultEntityRadius, this.defaultEntityRadius * 2, this.defaultEntityRadius * 2 ); } String tip = null; XYToolTipGenerator generator = getToolTipGenerator(series, item); if (generator != null) { tip = generator.generateToolTip(dataset, series, item); } String url = null; if (getURLGenerator() != null) { url = getURLGenerator().generateURL(dataset, series, item); } XYItemEntity entity = new XYItemEntity(area, dataset, series, item, tip, url); entities.addEntity(entity); }
/** * Adds an entity to the collection. * * @param entities the entity collection being populated. * @param area the entity area (if <code>null</code> a default will be * used). * @param dataset the dataset. * @param series the series. * @param item the item. * @param entityX the entity's center x-coordinate in user space. * @param entityY the entity's center y-coordinate in user space. */ protected void addEntity(EntityCollection entities, Shape area, XYDataset dataset, int series, int item, double entityX, double entityY) { if (!getItemCreateEntity(series, item)) { return; } if (area == null) { area = new Ellipse2D.Double(entityX - this.defaultEntityRadius, entityY - this.defaultEntityRadius, this.defaultEntityRadius * 2, this.defaultEntityRadius * 2); } String tip = null; XYToolTipGenerator generator = getToolTipGenerator(series, item); if (generator != null) { tip = generator.generateToolTip(dataset, series, item); } String url = null; if (getURLGenerator() != null) { url = getURLGenerator().generateURL(dataset, series, item); } XYItemEntity entity = new XYItemEntity(area, dataset, series, item, tip, url); entities.add(entity); }
/** * A check to ensure that an item that falls outside the plot's data area * does NOT generate an item entity. */ @Test public void testNoDisplayedItem() { XYSeriesCollection dataset = new XYSeriesCollection(); XYSeries s1 = new XYSeries("S1"); s1.add(10.0, 10.0); dataset.addSeries(s1); JFreeChart chart = ChartFactory.createXYLineChart("Title", "X", "Y", dataset, PlotOrientation.VERTICAL, false, true, false); XYPlot plot = (XYPlot) chart.getPlot(); plot.setRenderer(new StandardXYItemRenderer()); NumberAxis xAxis = (NumberAxis) plot.getDomainAxis(); xAxis.setRange(0.0, 5.0); NumberAxis yAxis = (NumberAxis) plot.getRangeAxis(); yAxis.setRange(0.0, 5.0); BufferedImage image = new BufferedImage(200 , 100, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); ChartRenderingInfo info = new ChartRenderingInfo(); chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null, info); g2.dispose(); EntityCollection ec = info.getEntityCollection(); assertFalse(TestUtilities.containsInstanceOf(ec.getEntities(), XYItemEntity.class)); }
/** * A check to ensure that an item that falls outside the plot's data area * does NOT generate an item entity. */ @Test public void testNoDisplayedItem() { XYSeriesCollection dataset = new XYSeriesCollection(); XYSeries s1 = new XYSeries("S1"); s1.add(10.0, 10.0); dataset.addSeries(s1); JFreeChart chart = ChartFactory.createXYLineChart("Title", "X", "Y", dataset, PlotOrientation.VERTICAL, false, true, false); XYPlot plot = (XYPlot) chart.getPlot(); plot.setRenderer(new StandardXYItemRenderer()); NumberAxis xAxis = (NumberAxis) plot.getDomainAxis(); xAxis.setRange(0.0, 5.0); NumberAxis yAxis = (NumberAxis) plot.getRangeAxis(); yAxis.setRange(0.0, 5.0); BufferedImage image = new BufferedImage(200 , 100, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); ChartRenderingInfo info = new ChartRenderingInfo(); chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null, info); g2.dispose(); EntityCollection ec = info.getEntityCollection(); assertFalse(TestUtils.containsInstanceOf(ec.getEntities(), XYItemEntity.class)); }
@Override public JRPrintHyperlink getEntityHyperlink(ChartEntity entity) { JRPrintHyperlink printHyperlink = null; if (hasHyperlinks() && entity instanceof XYItemEntity) { XYItemEntity itemEntity = (XYItemEntity) entity; XYDataset dataset = itemEntity.getDataset(); Comparable<?> serie = dataset.getSeriesKey(itemEntity.getSeriesIndex()); Map<Pair, JRPrintHyperlink> serieHyperlinks = itemHyperlinks.get(serie); if (serieHyperlinks != null) { Number x = dataset.getX(itemEntity.getSeriesIndex(), itemEntity.getItem()); Number y = dataset.getY(itemEntity.getSeriesIndex(), itemEntity.getItem()); Pair<Number,Number> xyKey = new Pair<Number,Number>(x, y); printHyperlink = serieHyperlinks.get(xyKey); } } return printHyperlink; }
@Override public JRPrintHyperlink getEntityHyperlink(ChartEntity entity) { JRPrintHyperlink printHyperlink = null; if (hasHyperlinks() && entity instanceof XYItemEntity) { XYItemEntity itemEntity = (XYItemEntity) entity; TimeSeriesCollection dataset = (TimeSeriesCollection) itemEntity.getDataset(); TimeSeries series = dataset.getSeries(itemEntity.getSeriesIndex()); Map<RegularTimePeriod, JRPrintHyperlink> serieHyperlinks = itemHyperlinks.get(series.getKey()); if (serieHyperlinks != null) { RegularTimePeriod timePeriod = series.getTimePeriod(itemEntity.getItem()); printHyperlink = serieHyperlinks.get(timePeriod); } } return printHyperlink; }
@Override public JRPrintHyperlink getEntityHyperlink(ChartEntity entity) { JRPrintHyperlink printHyperlink = null; if (hasHyperlinks() && entity instanceof XYItemEntity) { XYItemEntity itemEntity = (XYItemEntity) entity; TimePeriodValuesCollection dataset = (TimePeriodValuesCollection) itemEntity.getDataset(); TimePeriodValues series = dataset.getSeries(itemEntity.getSeriesIndex()); Map<TimePeriod, JRPrintHyperlink> serieHyperlinks = itemHyperlinks.get(series.getKey()); if (serieHyperlinks != null) { TimePeriod timePeriod = series.getTimePeriod(itemEntity.getItem()); printHyperlink = serieHyperlinks.get(timePeriod); } } return printHyperlink; }
/** * Callback method for receiving notification of a mouse movement on a * chart. * * @param event information about the event. */ public void chartMouseMoved(ChartMouseEvent event) { if (!(event.getChart().getXYPlot().getRenderer() instanceof MotionBubbleRenderer)) { return; } MotionBubbleRenderer renderer = (MotionBubbleRenderer) event.getChart().getXYPlot().getRenderer(); if (!(event.getEntity() instanceof XYItemEntity)) { renderer.setHighlightedItem(-1, -1); return; } XYItemEntity item = (XYItemEntity) event.getEntity(); renderer.setHighlightedItem(item.getSeriesIndex(), item.getItem()); }
/** * A check to ensure that an item that falls outside the plot's data area * does NOT generate an item entity. */ public void testNoDisplayedItem() { XYSeriesCollection dataset = new XYSeriesCollection(); XYSeries s1 = new XYSeries("S1"); s1.add(10.0, 10.0); dataset.addSeries(s1); JFreeChart chart = ChartFactory.createXYLineChart("Title", "X", "Y", dataset, false); XYPlot plot = (XYPlot) chart.getPlot(); plot.setRenderer(new StandardXYItemRenderer()); NumberAxis xAxis = (NumberAxis) plot.getDomainAxis(); xAxis.setRange(0.0, 5.0); NumberAxis yAxis = (NumberAxis) plot.getRangeAxis(); yAxis.setRange(0.0, 5.0); BufferedImage image = new BufferedImage(200 , 100, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); ChartRenderingInfo info = new ChartRenderingInfo(); chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null, info); g2.dispose(); EntityCollection ec = info.getEntityCollection(); assertFalse(TestUtilities.containsInstanceOf(ec.getEntities(), XYItemEntity.class)); }
public void mousePressed(MouseEvent e) { chartPanel.requestFocusInWindow(); Insets insets = chartPanel.getInsets(); int x = (int) ((e.getX() - insets.left) / chartPanel.getScaleX()); int y = (int) ((e.getY() - insets.top) / chartPanel.getScaleY()); ChartEntity entity = chartPanel.getChartRenderingInfo().getEntityCollection().getEntity(x, y); if (entity == null || !(entity instanceof XYItemEntity)) return; IsMovable = true; chartPanel.setCursor(new Cursor(Cursor.HAND_CURSOR)); xyItemEntity = (XYItemEntity)entity; XYPlot plot = chartPanel.getChart().getXYPlot(); Rectangle2D dataArea = chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea(); Point2D p = chartPanel.translateScreenToJava2D(e.getPoint()); initialMovePointY = plot.getRangeAxis().java2DToValue(p.getY(), dataArea, plot.getRangeAxisEdge()); }
/** * Confirm that the equals method can distinguish all the required fields. */ public void testEquals() { XYItemEntity e1 = new XYItemEntity( new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0), new TimeSeriesCollection(), 1, 9, "ToolTip", "URL" ); XYItemEntity e2 = new XYItemEntity( new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0), new TimeSeriesCollection(), 1, 9, "ToolTip", "URL" ); assertTrue(e1.equals(e2)); e1.setArea(new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0)); assertFalse(e1.equals(e2)); e2.setArea(new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0)); assertTrue(e1.equals(e2)); e1.setToolTipText("New ToolTip"); assertFalse(e1.equals(e2)); e2.setToolTipText("New ToolTip"); assertTrue(e1.equals(e2)); e1.setURLText("New URL"); assertFalse(e1.equals(e2)); e2.setURLText("New URL"); assertTrue(e1.equals(e2)); e1.setSeriesIndex(88); assertFalse(e1.equals(e2)); e2.setSeriesIndex(88); assertTrue(e1.equals(e2)); e1.setItem(88); assertFalse(e1.equals(e2)); e2.setItem(88); assertTrue(e1.equals(e2)); }
/** * Adds an entity to the collection. * * @param entities the entity collection being populated. * @param area the entity area (if <code>null</code> a default will be * used). * @param dataset the dataset. * @param series the series. * @param item the item. * @param entityX the entity's center x-coordinate in user space (only * used if <code>area</code> is <code>null</code>). * @param entityY the entity's center y-coordinate in user space (only * used if <code>area</code> is <code>null</code>). */ protected void addEntity(EntityCollection entities, Shape area, XYDataset dataset, int series, int item, double entityX, double entityY) { if (!getItemCreateEntity(series, item)) { return; } Shape hotspot = area; if (hotspot == null) { double r = getDefaultEntityRadius(); double w = r * 2; if (getPlot().getOrientation() == PlotOrientation.VERTICAL) { hotspot = new Ellipse2D.Double(entityX - r, entityY - r, w, w); } else { hotspot = new Ellipse2D.Double(entityY - r, entityX - r, w, w); } } String tip = null; XYToolTipGenerator generator = getToolTipGenerator(series, item); if (generator != null) { tip = generator.generateToolTip(dataset, series, item); } String url = null; if (getURLGenerator() != null) { url = getURLGenerator().generateURL(dataset, series, item); } XYItemEntity entity = new XYItemEntity(hotspot, dataset, series, item, tip, url); entities.add(entity); }
/** * Adds an entity to the collection. Note the the {@code entityX} and * {@code entityY} coordinates are in Java2D space, should already be * adjusted for the plot orientation, and will only be used if * {@code hotspot} is {@code null}. * * @param entities the entity collection being populated. * @param hotspot the entity area (if {@code null} a default will be * used). * @param dataset the dataset. * @param series the series. * @param item the item. * @param entityX the entity x-coordinate (in Java2D space, only used if * {@code hotspot} is {@code null}). * @param entityY the entity y-coordinate (in Java2D space, only used if * {@code hotspot} is {@code null}). */ protected void addEntity(EntityCollection entities, Shape hotspot, XYDataset dataset, int series, int item, double entityX, double entityY) { if (!getItemCreateEntity(series, item)) { return; } // if not hotspot is provided, we create a default based on the // provided data coordinates (which are already in Java2D space) if (hotspot == null) { double r = getDefaultEntityRadius(); double w = r * 2; hotspot = new Ellipse2D.Double(entityX - r, entityY - r, w, w); } String tip = null; XYToolTipGenerator generator = getToolTipGenerator(series, item); if (generator != null) { tip = generator.generateToolTip(dataset, series, item); } String url = null; if (getURLGenerator() != null) { url = getURLGenerator().generateURL(dataset, series, item); } XYItemEntity entity = new XYItemEntity(hotspot, dataset, series, item, tip, url); entities.add(entity); }
/** * Adds an entity to the collection. * * @param entities the entity collection being populated. * @param area the entity area (if {@code null} a default will be * used). * @param dataset the dataset. * @param series the series. * @param item the item. * @param entityX the entity's center x-coordinate in user space (only * used if {@code area} is {@code null}). * @param entityY the entity's center y-coordinate in user space (only * used if {@code area} is {@code null}). */ protected void addEntity(EntityCollection entities, Shape area, XYDataset dataset, int series, int item, double entityX, double entityY) { if (!getItemCreateEntity(series, item)) { return; } Shape hotspot = area; if (hotspot == null) { double r = getDefaultEntityRadius(); double w = r * 2; if (getPlot().getOrientation() == PlotOrientation.VERTICAL) { hotspot = new Ellipse2D.Double(entityX - r, entityY - r, w, w); } else { hotspot = new Ellipse2D.Double(entityY - r, entityX - r, w, w); } } String tip = null; XYToolTipGenerator generator = getToolTipGenerator(series, item); if (generator != null) { tip = generator.generateToolTip(dataset, series, item); } String url = null; if (getURLGenerator() != null) { url = getURLGenerator().generateURL(dataset, series, item); } XYItemEntity entity = new XYItemEntity(hotspot, dataset, series, item, tip, url); entities.add(entity); }
protected Dataset getEntityDataset(ChartEntity entity) { Dataset dataset = null; if (entity instanceof CategoryItemEntity) { dataset = ((CategoryItemEntity) entity).getDataset(); } else if (entity instanceof XYItemEntity) { dataset = ((XYItemEntity) entity).getDataset(); } return dataset; }
@Override public JRPrintHyperlink getEntityHyperlink(ChartEntity entity) { JRPrintHyperlink printHyperlink = null; if (hasHyperlinks() && entity instanceof XYItemEntity) { XYItemEntity itemEntity = (XYItemEntity) entity; int item = itemEntity.getItem(); if (item >= 0 && item < itemHyperlinks.size()) { printHyperlink = itemHyperlinks.get(item); } } return printHyperlink; }
/** * The gesture entity type * * @param entity * @return */ public static Entity getGestureEntity(ChartEntity entity) { if (entity == null) return NONE; if (entity instanceof PlotEntity) return PLOT; if (entity instanceof AxisEntity) { AxisEntity e = (AxisEntity) entity; if (e.getAxis().getPlot() instanceof XYPlot) { XYPlot plot = ((XYPlot) e.getAxis().getPlot()); for (int i = 0; i < plot.getDomainAxisCount(); i++) if (plot.getDomainAxis(i).equals(e.getAxis())) return DOMAIN_AXIS; for (int i = 0; i < plot.getRangeAxisCount(); i++) if (plot.getRangeAxis(i).equals(e.getAxis())) return RANGE_AXIS; } // else return basic axis return AXIS; } if (entity instanceof LegendItemEntity) return LEGEND_ITEM; if (entity instanceof XYItemEntity) return XY_ITEM; if (entity instanceof XYAnnotationEntity) return XY_ANNOTATION; if (entity instanceof TitleEntity) { if (((TitleEntity) entity).getTitle() instanceof TextTitle) return TEXT_TITLE; else return NON_TEXT_TITLE; } if (entity instanceof JFreeChartEntity) return JFREECHART; if (entity instanceof CategoryItemEntity) return CATEGORY_ITEM; return GENERAL; }
/** * Handles mouse clicks in the chart panel. Selects/de-selects data points. * * @param event */ private void mouseClickedInChart(ChartMouseEvent event) { ArrayList<ChartEntity> entities = getEntitiesForPoint(event.getTrigger().getPoint().x, event.getTrigger().getPoint().y); if (entities.isEmpty()) { return; } boolean dataPointsSelected = false; // check if any data points are selected, and select/de-select them for (ChartEntity entity : entities) { // Get entity details if (entity instanceof XYItemEntity) { selectEntity((XYItemEntity) entity, true); dataPointsSelected = true; } } // if no data points were selected then clear the selection if (!dataPointsSelected) { selectedDataPoints = new HashMap<Integer, ArrayList<Integer>>(); selectedModelRows = new ArrayList<Integer>(); } filterTable(); chartPanel.getChart().fireChartChanged(); }
/** * Selects/de-selects a given entity in the plot. * * @param entity the entity to select/de-select * @param removeSelected if true, already selected entities are de-selected */ private void selectEntity(XYItemEntity entity, boolean removeSelected) { Integer seriesIndex = ((XYItemEntity) entity).getSeriesIndex(); Integer itemIndex = ((XYItemEntity) entity).getItem(); if (selectedDataPoints.containsKey(seriesIndex)) { if (selectedDataPoints.get(seriesIndex).contains(itemIndex)) { if (removeSelected) { selectedDataPoints.get(seriesIndex).remove(itemIndex); if (selectedDataPoints.get(seriesIndex).isEmpty()) { selectedDataPoints.remove(seriesIndex); } selectedModelRows.remove(dataPointToRowNumber.get(seriesIndex + "_" + itemIndex)); } } else { selectedDataPoints.get(seriesIndex).add(itemIndex); selectedModelRows.add(dataPointToRowNumber.get(seriesIndex + "_" + itemIndex)); } } else { ArrayList<Integer> itemList = new ArrayList<Integer>(); itemList.add(itemIndex); selectedDataPoints.put(seriesIndex, itemList); if (!selectedModelRows.contains(dataPointToRowNumber.get(seriesIndex + "_" + itemIndex))) { selectedModelRows.add(dataPointToRowNumber.get(seriesIndex + "_" + itemIndex)); } } }
/** * Adds an entity to the collection. * * @param entities the entity collection being populated. * @param area the entity area (if <code>null</code> a default will be * used). * @param dataset the dataset. * @param series the series. * @param item the item. * @param entityX the entity's center x-coordinate in user space (only * used if <code>area</code> is <code>null</code>). * @param entityY the entity's center y-coordinate in user space (only * used if <code>area</code> is <code>null</code>). */ protected void addEntity(EntityCollection entities, Shape area, XYDataset dataset, int series, int item, double entityX, double entityY) { if (!getItemCreateEntity(series, item)) { return; } Shape hotspot = area; if (hotspot == null) { double w = this.defaultEntityRadius * 2; if (getPlot().getOrientation() == PlotOrientation.VERTICAL) { hotspot = new Ellipse2D.Double( entityX - this.defaultEntityRadius, entityY - this.defaultEntityRadius, w, w); } else { hotspot = new Ellipse2D.Double( entityY - this.defaultEntityRadius, entityX - this.defaultEntityRadius, w, w); } } String tip = null; XYToolTipGenerator generator = getToolTipGenerator(series, item); if (generator != null) { tip = generator.generateToolTip(dataset, series, item); } String url = null; if (getURLGenerator() != null) { url = getURLGenerator().generateURL(dataset, series, item); } XYItemEntity entity = new XYItemEntity(hotspot, dataset, series, item, tip, url); entities.add(entity); }
private ClickData getClickData(ChartEntity entity) { if (entity instanceof XYItemEntity) { XYItemEntity xyEntity = (XYItemEntity) entity; TimeSeriesCollection dataset = (TimeSeriesCollection) xyEntity.getDataset(); int series = xyEntity.getSeriesIndex(); int item = xyEntity.getItem(); double yValue = dataset.getYValue(series, item); long time = (long) dataset.getXValue(series, item); return new ClickData(time, (TimeSeriesWithStats) dataset.getSeries(series), yValue); } return null; }
/** * Confirm that the equals method can distinguish all the required fields. */ public void testEquals() { XYItemEntity e1 = new XYItemEntity(new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0), new TimeSeriesCollection(), 1, 9, "ToolTip", "URL"); XYItemEntity e2 = new XYItemEntity(new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0), new TimeSeriesCollection(), 1, 9, "ToolTip", "URL"); assertTrue(e1.equals(e2)); e1.setArea(new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0)); assertFalse(e1.equals(e2)); e2.setArea(new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0)); assertTrue(e1.equals(e2)); e1.setToolTipText("New ToolTip"); assertFalse(e1.equals(e2)); e2.setToolTipText("New ToolTip"); assertTrue(e1.equals(e2)); e1.setURLText("New URL"); assertFalse(e1.equals(e2)); e2.setURLText("New URL"); assertTrue(e1.equals(e2)); e1.setSeriesIndex(88); assertFalse(e1.equals(e2)); e2.setSeriesIndex(88); assertTrue(e1.equals(e2)); e1.setItem(88); assertFalse(e1.equals(e2)); e2.setItem(88); assertTrue(e1.equals(e2)); }