Example usage for org.jfree.chart.fx ChartViewer getChart

List of usage examples for org.jfree.chart.fx ChartViewer getChart

Introduction

In this page you can find the example usage for org.jfree.chart.fx ChartViewer getChart.

Prototype

public JFreeChart getChart() 

Source Link

Document

Returns the chart that is being displayed by this node.

Usage

From source file:io.github.mzmine.util.jfreechart.JFreeChartUtils.java

public static void exportToImageFile(ChartViewer chartNode, File file, ImgFileType fileType) {

    final JFreeChart chart = chartNode.getChart();
    final int width = (int) chartNode.getWidth();
    final int height = (int) chartNode.getHeight();

    try {/*from   www .  j a v a2s.com*/

        switch (fileType) {

        case JPG:
            ExportUtils.writeAsJPEG(chart, width, height, file);
            break;

        case PNG:
            ExportUtils.writeAsPNG(chart, width, height, file);
            break;

        case SVG:
            setDrawSeriesLineAsPath(chart, true);
            ExportUtils.writeAsSVG(chart, width, height, file);
            setDrawSeriesLineAsPath(chart, false);
            break;

        case PDF:
            setDrawSeriesLineAsPath(chart, true);
            ExportUtils.writeAsPDF(chart, width, height, file);
            setDrawSeriesLineAsPath(chart, false);
            break;

        case EMF:
            FileOutputStream out2 = new FileOutputStream(file);
            setDrawSeriesLineAsPath(chart, true);
            EMFGraphics2D g2d2 = new EMFGraphics2D(out2, new Dimension(width, height));
            g2d2.startExport();
            chart.draw(g2d2, new Rectangle(width, height));
            g2d2.endExport();
            setDrawSeriesLineAsPath(chart, false);
            break;

        case EPS:
            FileOutputStream out = new FileOutputStream(file);
            setDrawSeriesLineAsPath(chart, true);
            EPSDocumentGraphics2D g2d = new EPSDocumentGraphics2D(false);
            g2d.setGraphicContext(new GraphicContext());
            g2d.setupDocument(out, width, height);
            chart.draw(g2d, new Rectangle(width, height));
            g2d.finish();
            setDrawSeriesLineAsPath(chart, false);
            out.close();
            break;

        }

    } catch (IOException e) {
        MZmineGUI.displayMessage("Unable to save image: " + e.getMessage());
        e.printStackTrace();
    }
}

From source file:net.sf.mzmine.chartbasics.ChartLogicsFX.java

/**
 * //from   w  w w .jav  a2  s  . c  o m
 * @param myChart
 * @return Range the domainAxis zoom (X-axis)
 */
public static Range getZoomDomainAxis(ChartViewer myChart) {
    XYPlot plot = (XYPlot) myChart.getChart().getPlot();
    ValueAxis domainAxis = plot.getDomainAxis();

    return new Range(domainAxis.getLowerBound(), domainAxis.getUpperBound());
}

From source file:net.sf.mzmine.chartbasics.ChartLogicsFX.java

/**
 * Auto range the range axis/*from  ww  w.ja va  2 s .  c  om*/
 * 
 * @param myChart
 * @param zoom
 * @param autoRangeY if true the range (Y) axis auto bounds will be restored
 */
public static void autoRangeAxis(ChartViewer myChart) {
    XYPlot plot = (XYPlot) myChart.getChart().getPlot();
    if (plot instanceof Zoomable) {
        Zoomable z = plot;
        Point2D endPoint = new Point2D.Double(0, 0);
        PlotRenderingInfo pri = myChart.getRenderingInfo().getPlotInfo();
        z.zoomRangeAxes(0, pri, endPoint);
    }
}

From source file:net.sf.mzmine.chartbasics.ChartLogicsFX.java

/**
 * Auto range the range axis//from   w ww  .j  ava 2 s . co  m
 * 
 * @param myChart
 * @param zoom
 * @param autoRangeY if true the range (Y) axis auto bounds will be restored
 */
public static void autoDomainAxis(ChartViewer myChart) {
    XYPlot plot = (XYPlot) myChart.getChart().getPlot();
    if (plot instanceof Zoomable) {
        Zoomable z = plot;
        Point2D endPoint = new Point2D.Double(0, 0);
        PlotRenderingInfo pri = myChart.getRenderingInfo().getPlotInfo();
        z.zoomDomainAxes(0, pri, endPoint);
    }
}

From source file:net.sf.mzmine.chartbasics.ChartLogicsFX.java

/**
 * Auto range the range axis//from   w w w .j  av a  2  s. co  m
 * 
 * @param myChart
 * @param zoom
 * @param autoRangeY if true the range (Y) axis auto bounds will be restored
 */
public static void autoAxes(ChartViewer myChart) {
    XYPlot plot = (XYPlot) myChart.getChart().getPlot();
    if (plot instanceof Zoomable) {
        Zoomable z = plot;
        Point2D endPoint = new Point2D.Double(0, 0);
        PlotRenderingInfo pri = myChart.getRenderingInfo().getPlotInfo();
        boolean saved = plot.isNotify();
        plot.setNotify(false);
        z.zoomDomainAxes(0, pri, endPoint);
        z.zoomRangeAxes(0, pri, endPoint);
        plot.setNotify(saved);
    }
}

From source file:io.github.mzmine.util.jfreechart.JFreeChartUtils.java

public static void printChart(ChartViewer chartNode) {

    // As of java 1.8.0_74, the JavaFX printing support seems to do poor
    // job. It creates pixelated, low-resolution print outs. For that
    // reason, we use the AWT PrinterJob class, until the JavaFX printing
    // support is improved.
    SwingUtilities.invokeLater(() -> {
        PrinterJob job = PrinterJob.getPrinterJob();
        PageFormat pf = job.defaultPage();
        PageFormat pf2 = job.pageDialog(pf);
        if (pf2 == pf)
            return;
        ChartPanel p = new ChartPanel(chartNode.getChart());
        job.setPrintable(p, pf2);/*from   ww w .  ja v a  2 s .  c  o  m*/
        if (!job.printDialog())
            return;
        try {
            job.print();
        } catch (PrinterException e) {
            e.printStackTrace();
            MZmineGUI.displayMessage("Error printing: " + e.getMessage());
        }
    });
}

From source file:net.sf.mzmine.chartbasics.ChartLogicsFX.java

/**
 * Zoom into a chart panel//from w  ww  . j a v a2 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(ChartViewer 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.ChartLogicsFX.java

/**
 * Apply an absolute offset to domain (x) axis and move it
 * //  w ww.  j a  v a2s  .  c o m
 * @param myChart
 * @param xoffset
 * @param autoRangeY
 */
public static void offsetDomainAxisAbsolute(ChartViewer 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.ChartLogicsFX.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)//  w  w w  . j a v a2s  .  c o  m
 * 
 * @param myChart
 * @param xoffset in percent
 * @param autoRangeY if true the range (Y) axis auto bounds will be restored
 */
public static void offsetDomainAxis(ChartViewer 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.mzmine.chartbasics.ChartLogicsFX.java

/**
 * Zoom in (negative yzoom) or zoom out of range axis.
 * // ww  w.jav a  2  s  .co m
 * @param myChart
 * @param yzoom percentage zoom factor
 * @param holdLowerBound if true only the upper bound will be zoomed
 */
public static void zoomRangeAxis(ChartViewer myChart, double yzoom, boolean holdLowerBound) {
    XYPlot plot = (XYPlot) myChart.getChart().getPlot();
    ValueAxis rangeAxis = plot.getRangeAxis();

    double lower = rangeAxis.getLowerBound();
    double upper = rangeAxis.getUpperBound();
    double dist = upper - lower;

    if (holdLowerBound) {
        upper += dist * yzoom;
    } else {
        lower -= dist * yzoom / 2;
        upper += dist * yzoom / 2;
    }

    if (lower < upper) {
        Range range = new Range(lower, upper);
        setZoomAxis(rangeAxis, keepRangeWithinAutoBounds(rangeAxis, range));
    }
}