List of usage examples for org.jfree.chart ChartPanel getChart
public JFreeChart getChart()
From source file:net.sf.mzmine.chartbasics.ChartLogics.java
/** * Zoom into a chart panel/* w ww .ja v a 2 s. c o m*/ * * @param myChart * @param zoom * @param autoRangeY if true the range (Y) axis auto bounds will be restored */ public static void setZoomDomainAxis(ChartPanel myChart, Range zoom, boolean autoRangeY) { XYPlot plot = (XYPlot) myChart.getChart().getPlot(); ValueAxis domainAxis = plot.getDomainAxis(); setZoomAxis(domainAxis, keepRangeWithinAutoBounds(domainAxis, zoom)); if (autoRangeY) { autoRangeAxis(myChart); } }
From source file:net.sf.mzmine.chartbasics.ChartLogics.java
/** * Apply an absolute offset to domain (x) axis and move it * //from w w w. ja va 2s . c o m * @param myChart * @param xoffset * @param autoRangeY */ public static void offsetDomainAxisAbsolute(ChartPanel myChart, double xoffset, boolean autoRangeY) { XYPlot plot = (XYPlot) myChart.getChart().getPlot(); ValueAxis domainAxis = plot.getDomainAxis(); // apply offset on x Range range = new Range(domainAxis.getLowerBound() + xoffset, domainAxis.getUpperBound() + xoffset); setZoomDomainAxis(myChart, keepRangeWithinAutoBounds(domainAxis, range), autoRangeY); }
From source file:net.sf.mzmine.chartbasics.ChartLogics.java
/** * Move a chart by a percentage x-offset if xoffset is <0 the shift will be negativ (xoffset>0 * results in a positive shift)/*from www . j a v a 2 s . com*/ * * @param myChart * @param xoffset in percent * @param autoRangeY if true the range (Y) axis auto bounds will be restored */ public static void offsetDomainAxis(ChartPanel myChart, double xoffset, boolean autoRangeY) { XYPlot plot = (XYPlot) myChart.getChart().getPlot(); ValueAxis domainAxis = plot.getDomainAxis(); // apply offset on x double distance = (domainAxis.getUpperBound() - domainAxis.getLowerBound()) * xoffset; Range range = new Range(domainAxis.getLowerBound() + distance, domainAxis.getUpperBound() + distance); setZoomDomainAxis(myChart, keepRangeWithinAutoBounds(domainAxis, range), autoRangeY); }
From source file:net.sf.maltcms.common.charts.api.overlay.AbstractChartOverlay.java
/** * * @param chartPanel//w w w . j a v a 2 s .c om * @param x1 * @param y1 * @return */ public static AffineTransform getModelToViewTransformXY(ChartPanel chartPanel, double x1, double y1) { double zoomX = chartPanel.getScaleX(); double zoomY = chartPanel.getScaleY(); Insets insets = chartPanel.getInsets(); AffineTransform at = getTranslateInstance(insets.left, insets.top); at.concatenate(getScaleInstance(zoomX, zoomY)); Plot plot = chartPanel.getChart().getPlot(); if (plot instanceof XYPlot) { XYPlot xyp = (XYPlot) plot; RectangleEdge xAxisLocation = xyp.getDomainAxisEdge(); RectangleEdge yAxisLocation = xyp.getRangeAxisEdge(); PlotOrientation orientation = xyp.getOrientation(); Rectangle2D dataArea = chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea(); double transX = xyp.getDomainAxis().valueToJava2D(x1, dataArea, xAxisLocation); double transY = xyp.getRangeAxis().valueToJava2D(y1, dataArea, yAxisLocation); if (orientation == PlotOrientation.HORIZONTAL) { double tmp = transX; transX = transY; transY = tmp; } at.concatenate(getTranslateInstance(transX, transY)); return at; } throw new IllegalArgumentException("Unsupported plot type: " + plot.getClass()); }
From source file:net.sf.maltcms.common.charts.api.overlay.AbstractChartOverlay.java
/** * * @param chartPanel/*from w w w .j a v a 2 s . c o m*/ * @param category * @param y * @return */ public static AffineTransform getModelToViewTransformCategory(ChartPanel chartPanel, int category, double y) { double zoomX = chartPanel.getScaleX(); double zoomY = chartPanel.getScaleY(); Insets insets = chartPanel.getInsets(); AffineTransform at = getTranslateInstance(insets.left, insets.top); at.concatenate(getScaleInstance(zoomX, zoomY)); Plot plot = chartPanel.getChart().getPlot(); if (plot instanceof CategoryPlot) { CategoryPlot xyp = (CategoryPlot) plot; CategoryDataset cds = xyp.getDataset(); RectangleEdge xAxisLocation = xyp.getDomainAxisEdge(); RectangleEdge yAxisLocation = xyp.getRangeAxisEdge(); PlotOrientation orientation = xyp.getOrientation(); Comparable<?> categoryKey = cds.getColumnKey(category); Rectangle2D dataArea = chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea(); double transX = xyp.getDomainAxis().getCategoryMiddle(categoryKey, cds.getColumnKeys(), dataArea, xAxisLocation); double transY = xyp.getRangeAxis().valueToJava2D(y, dataArea, yAxisLocation); if (orientation == PlotOrientation.HORIZONTAL) { double tmp = transX; transX = transY; transY = tmp; } at.concatenate(getTranslateInstance(transX, transY)); return at; } throw new IllegalArgumentException("Unsupported plot type: " + plot.getClass()); }
From source file:net.sf.mzmine.chartbasics.ChartLogics.java
/** * Calculates the size of a chart for a given fixed plot width Domain and Range axes need to share * the same unit (e.g. mm)// www . jav a 2 s . c om * * @param chart * @param plotWidth * @return */ public static Dimension calcSizeForPlotWidth(ChartPanel myChart, double plotWidth, int iterations) { // ranges XYPlot plot = (XYPlot) myChart.getChart().getPlot(); ValueAxis domainAxis = plot.getDomainAxis(); Range x = domainAxis.getRange(); ValueAxis rangeAxis = plot.getRangeAxis(); Range y = rangeAxis.getRange(); // plot height is fixed double plotHeight = plotWidth / x.getLength() * y.getLength(); return calcSizeForPlotSize(myChart, plotWidth, plotHeight, iterations); }
From source file:net.sf.mzmine.chartbasics.ChartLogics.java
/** * Data width to pixel width on screen//from ww w. j ava 2 s . co m * * @param myChart * @param dataWidth width of data * @param axis for width calculation * @return */ public static double calcWidthOnScreen(ChartPanel myChart, double dataWidth, ValueAxis axis, RectangleEdge axisEdge) { XYPlot plot = (XYPlot) myChart.getChart().getPlot(); ChartRenderingInfo info = myChart.getChartRenderingInfo(); Rectangle2D dataArea = info.getPlotInfo().getDataArea(); double width2D = axis.lengthToJava2D(dataWidth, dataArea, axisEdge); return width2D; }
From source file:ec.util.chart.swing.Charts.java
public static void copyChart(@Nonnull ChartPanel chartPanel) { Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); Insets insets = chartPanel.getInsets(); int w = chartPanel.getWidth() - insets.left - insets.right; int h = chartPanel.getHeight() - insets.top - insets.bottom; Transferable selection = new ChartTransferable2(chartPanel.getChart(), w, h, chartPanel.getMinimumDrawWidth(), chartPanel.getMinimumDrawHeight(), chartPanel.getMaximumDrawWidth(), chartPanel.getMaximumDrawHeight(), true); clipboard.setContents(selection, null); }
From source file:ec.util.chart.swing.Charts.java
@Nullable public static LegendItemEntity getSeriesForPoint(@Nonnull Point pt, @Nonnull ChartPanel cp) { final double chartX; final double chartY; final Rectangle2D plotArea; final XYPlot plot; {//from w ww .j av a 2s. co m // Let's find the X and Y values of the clicked point Point2D p = cp.translateScreenToJava2D(pt); chartX = p.getX(); chartY = p.getY(); // Let's find plotArea and plot XYPlot tmpPlot = cp.getChart().getXYPlot(); PlotRenderingInfo plotInfo = cp.getChartRenderingInfo().getPlotInfo(); if (tmpPlot instanceof CombinedDomainXYPlot) { int subplotIndex = plotInfo.getSubplotIndex(p); if (subplotIndex == -1) { return null; } plotArea = plotInfo.getSubplotInfo(subplotIndex).getDataArea(); plot = ((CombinedDomainXYPlot) tmpPlot).findSubplot(plotInfo, p); } else { plotArea = plotInfo.getDataArea(); plot = tmpPlot; } } // Let's avoid unnecessary computation final ValueAxis domainAxis = plot.getDomainAxis(); final ValueAxis rangeAxis = plot.getRangeAxis(); final RectangleEdge domainAxisEdge = plot.getDomainAxisEdge(); final RectangleEdge rangeAxisEdge = plot.getRangeAxisEdge(); final double x = domainAxis.java2DToValue(chartX, plotArea, domainAxisEdge); final double sensitivity = TOL; double distanceClickSeries = TOL + 1; Entry<XYDataset, Comparable> result = null; // For each series in each datasets for (XYDataset dataset : asDatasetList(plot)) { for (int series = 0; series < dataset.getSeriesCount(); series++) { // Index of the closest data item of the current series just left to the click int lp = getNearestLeftPoint(x, 0, dataset.getItemCount(series) - 1, series, dataset); try { // X and Y values of data items to the left and to the right double leftX = dataset.getXValue(series, lp); double leftY = dataset.getYValue(series, lp); double rightX = dataset.getXValue(series, lp + 1); double rightY = dataset.getYValue(series, lp + 1); double lx = domainAxis.valueToJava2D(leftX, plotArea, domainAxisEdge); double ly = rangeAxis.valueToJava2D(leftY, plotArea, rangeAxisEdge); double rx = domainAxis.valueToJava2D(rightX, plotArea, domainAxisEdge); double ry = rangeAxis.valueToJava2D(rightY, plotArea, rangeAxisEdge); // Distance to left point double distL = Point2D.distance(lx, ly, chartX, chartY); // Distance to right point double distR = Point2D.distance(rx, ry, chartX, chartY); // Average of both distances double distLRavg = (distL + distR) / 2d; // Distance to the segment between L and R //double distSeg = Line2D.ptSegDist(leftX, leftY, rightX, rightY, chartX, chartY); double distSeg = ptSegDist(lx, ly, rx, ry, chartX, chartY); // With a line renderer, this is probably a bit of overkill as // distSeg would be enough, but it becomes more reliable to check all these // if using splines double tmp = Math.min(Math.min(distSeg, Math.min(distL, distR)), distLRavg); // Are we closer than the previous series? if (tmp < sensitivity && tmp < distanceClickSeries) { distanceClickSeries = tmp; result = new SimpleEntry<>(dataset, dataset.getSeriesKey(series)); } } catch (Exception ex) { /* * An exception might happen when some series have less data * than others, catching the the exception here will simply rule * them out from the detection on this click */ } } } return result != null ? createFakeLegendItemEntity(result.getKey(), result.getValue()) : null; }
From source file:GUI.PlotHere.java
public void PutPoint(int key, double x, double y) { Component[] a = GraphHerePanel.getComponents(); ChartPanel chartpanel = (ChartPanel) a[0]; JFreeChart chart = chartpanel.getChart(); XYPlot plot = (XYPlot) chart.getPlot(); XYSeriesCollection data = (XYSeriesCollection) plot.getDataset(); XYSeries XYseries = data.getSeries(key); XYseries.add(x, y);//from w w w . j a v a 2s . c om this.revalidate(); this.repaint(); }