Example usage for org.jfree.chart ChartPanel getChartRenderingInfo

List of usage examples for org.jfree.chart ChartPanel getChartRenderingInfo

Introduction

In this page you can find the example usage for org.jfree.chart ChartPanel getChartRenderingInfo.

Prototype

public ChartRenderingInfo getChartRenderingInfo() 

Source Link

Document

Returns the chart rendering info from the most recent chart redraw.

Usage

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

/**
 * Returns dimensions for limiting factor width or height
 * //  www  .jav a 2  s  .c om
 * @param myChart
 * @return
 */
public static Dimension calcMaxSize(ChartPanel myChart, double chartWidth, double chartHeight) {
    makeChartResizable(myChart);
    // paint on a ghost panel
    JPanel parent = (JPanel) myChart.getParent();
    JPanel p = new JPanel();
    p.removeAll();
    p.add(myChart, BorderLayout.CENTER);
    p.setBounds(myChart.getBounds());
    myChart.paintImmediately(myChart.getBounds());
    p.removeAll();
    parent.add(myChart);

    XYPlot plot = (XYPlot) myChart.getChart().getPlot();
    ChartRenderingInfo info = myChart.getChartRenderingInfo();
    Rectangle2D dataArea = info.getPlotInfo().getDataArea();
    Rectangle2D chartArea = info.getChartArea();

    // calc title space: will be added later to the right plot size
    double titleWidth = chartArea.getWidth() - dataArea.getWidth();
    double titleHeight = chartArea.getHeight() - dataArea.getHeight();

    // calculatig width for max height

    // calc right plot size with axis dim.
    // real plot width is given by factor;
    double realPH = chartHeight - titleHeight;

    // ranges
    ValueAxis domainAxis = plot.getDomainAxis();
    org.jfree.data.Range x = domainAxis.getRange();
    ValueAxis rangeAxis = plot.getRangeAxis();
    org.jfree.data.Range y = rangeAxis.getRange();

    // real plot height can be calculated by
    double realPW = realPH / y.getLength() * x.getLength();

    double width = realPW + titleWidth;
    // if width is higher than given chartWidth then calc height for chartWidth
    if (width > chartWidth) {
        // calc right plot size with axis dim.
        // real plot width is given by factor;
        realPW = chartWidth - titleWidth;

        // real plot height can be calculated by
        realPH = realPW / x.getLength() * y.getLength();

        double height = realPH + titleHeight;
        // Return size
        return new Dimension((int) chartWidth, (int) height);
    } else {
        // Return size
        return new Dimension((int) width, (int) chartHeight);
    }
}

From source file:net.sf.maltcms.common.charts.api.overlay.AbstractChartOverlay.java

/**
 *
 * @param chartPanel//from  w w  w .j a  va2  s .co  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.bioclipse.model.ScatterPlotMouseHandler.java

private double domainValueTo2D(ChartPanel chartPanel, XYPlot plot, double value) {
    ChartRenderingInfo info = chartPanel.getChartRenderingInfo();
    Rectangle2D dataArea = info.getPlotInfo().getDataArea();
    Number x = plot.getDomainAxis().valueToJava2D(value, dataArea, plot.getDomainAxisEdge());

    return x.doubleValue();
}

From source file:net.bioclipse.model.ScatterPlotMouseHandler.java

private double rangeValueTo2D(ChartPanel chartPanel, XYPlot plot, double value) {
    ChartRenderingInfo info = chartPanel.getChartRenderingInfo();
    Rectangle2D dataArea = info.getPlotInfo().getDataArea();
    Number y = plot.getRangeAxis().valueToJava2D(value, dataArea, plot.getRangeAxisEdge());

    return y.doubleValue();
}

From source file:net.bioclipse.model.ScatterPlotMouseHandler.java

private Number getDomainX(ChartPanel chartPanel, XYPlot plot, Point2D mousePoint) {
    ChartRenderingInfo info = chartPanel.getChartRenderingInfo();
    Rectangle2D dataArea = info.getPlotInfo().getDataArea();
    Number x = plot.getDomainAxis().java2DToValue(mousePoint.getX(), dataArea, plot.getDomainAxisEdge());

    return x;/*from   www .j  a  v  a2  s . c  o  m*/
}

From source file:net.bioclipse.model.ScatterPlotMouseHandler.java

private Number getRangeY(ChartPanel chartPanel, XYPlot plot, Point2D mousePoint) {
    ChartRenderingInfo info = chartPanel.getChartRenderingInfo();
    Rectangle2D dataArea = info.getPlotInfo().getDataArea();
    Number y = plot.getRangeAxis().java2DToValue(mousePoint.getY(), dataArea, plot.getRangeAxisEdge());

    return y;/*from  w ww .j  av a 2s.c  om*/
}

From source file:fuel.gui.stats.MotorStatsPanel.java

private void refreshGraphs(Motorcycle motor) {
    graphContainer.removeAll();//from   www  . j a v  a  2s .c om
    if (motor != null) {
        DefaultPieDataset usageDataset = new DefaultPieDataset();
        try {
            ResultSet thisMotor = database
                    .Query("SELECT SUM(distance) FROM fuelrecords WHERE motorcycleId = " + motor.getId(), true);
            ResultSet otherMotors = database.Query(
                    "SELECT SUM(distance) FROM fuelrecords WHERE NOT motorcycleId = " + motor.getId(), true);
            thisMotor.next();
            otherMotors.next();

            usageDataset.setValue(motor.toString(), thisMotor.getInt("1"));
            usageDataset.setValue("Andere motoren", otherMotors.getInt("1"));

        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage() + ex.getCause());
        }
        JFreeChart usagePiechart = ChartFactory.createPieChart3D("", usageDataset, true, true, false);
        PiePlot3D plot3 = (PiePlot3D) usagePiechart.getPlot();
        plot3.setForegroundAlpha(0.6f);
        //plot3.setCircular(true);

        JPanel usagePiechartPanel = new ChartPanel(usagePiechart);
        usagePiechartPanel
                .setBorder(BorderFactory.createTitledBorder(BorderFactory.createTitledBorder("Motorgebruik")));
        usagePiechartPanel.setPreferredSize(new java.awt.Dimension(240, 240));
        usagePiechartPanel.setLayout(new BorderLayout());

        DefaultPieDataset stationDataset = new DefaultPieDataset();
        try {
            for (Station station : database.getStations()) {
                ResultSet numberStations = database.Query(
                        "SELECT DISTINCT stationId FROM fuelrecords WHERE stationId = " + station.getId(),
                        true);
                if (numberStations.next()) {
                    ResultSet otherMotors = database.Query("SELECT COUNT(*) FROM fuelrecords WHERE stationId = "
                            + station.getId() + " AND motorcycleId = " + motor.getId(), true);
                    otherMotors.next();
                    if (otherMotors.getInt("1") > 0) {
                        stationDataset.setValue(station.toString(), otherMotors.getInt("1"));
                    }
                }
            }

        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage() + ex.getCause());
        }
        JFreeChart stationPiechart = ChartFactory.createPieChart3D("", stationDataset, true, true, false);
        PiePlot3D plot2 = (PiePlot3D) stationPiechart.getPlot();
        plot2.setForegroundAlpha(0.6f);
        //plot3.setCircular(true);

        JPanel stationPiechartPanel = new ChartPanel(stationPiechart);
        stationPiechartPanel.setBorder(
                BorderFactory.createTitledBorder(BorderFactory.createTitledBorder("Tankstation verhouding")));
        stationPiechartPanel.setPreferredSize(new java.awt.Dimension(240, 240));
        stationPiechartPanel.setLayout(new BorderLayout());

        DefaultPieDataset fuelDataset = new DefaultPieDataset();
        try {
            ResultSet numberResults = database.Query("SELECT DISTINCT typeOfGas FROM fuelrecords", true);
            while (numberResults.next()) {
                ResultSet thisStation = database.Query(
                        "SELECT SUM(liter) FROM fuelrecords WHERE typeOfGas = '"
                                + numberResults.getString("typeOfGas") + "'AND motorcycleId = " + motor.getId(),
                        true);
                thisStation.next();
                if (thisStation.getDouble("1") > 0) {
                    fuelDataset.setValue(numberResults.getString("TYPEOFGAS"), thisStation.getDouble("1"));
                }
            }

        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage() + ex.getCause());
        }
        JFreeChart fuelPieChart = ChartFactory.createPieChart3D("", fuelDataset, true, true, false);
        PiePlot3D plot1 = (PiePlot3D) fuelPieChart.getPlot();
        plot1.setForegroundAlpha(0.6f);
        //plot3.setCircular(true);

        JPanel fuelPieChartPanel = new ChartPanel(fuelPieChart);
        fuelPieChartPanel.setBorder(
                BorderFactory.createTitledBorder(BorderFactory.createTitledBorder("Brandstof verhouding")));
        fuelPieChartPanel.setPreferredSize(new java.awt.Dimension(240, 240));

        DefaultCategoryDataset barDataset = new DefaultCategoryDataset();
        try {
            ResultSet motorThing = database
                    .Query("SELECT distance/liter,date FROM fuelrecords WHERE motorcycleId = " + motor.getId()
                            + " ORDER BY date ASC", true);
            while (motorThing.next()) {
                barDataset.addValue(motorThing.getDouble("1"), motorThing.getString("DATE"), "Verbruik");
            }

        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage() + ex.getCause());
        }

        JFreeChart barChart = ChartFactory.createBarChart3D("", // chart title
                "", // domain axis label
                "Aantal", // range axis label
                barDataset, // data
                PlotOrientation.VERTICAL, false, // include legend
                true, // tooltips?
                false // URLs?
        );
        CategoryPlot plot = barChart.getCategoryPlot();
        BarRenderer3D renderer = (BarRenderer3D) plot.getRenderer();
        renderer.setDrawBarOutline(false);

        ChartPanel barChartPanel = new ChartPanel(barChart);
        barChartPanel.getChartRenderingInfo().setEntityCollection(null);
        barChartPanel.setBorder(BorderFactory.createTitledBorder("Verbruik"));
        barChartPanel.setPreferredSize(new java.awt.Dimension(320, 240));
        barChartPanel.setLayout(new BorderLayout());

        JPanel piePanel = new JPanel(new GridLayout(0, 3));
        piePanel.add(usagePiechartPanel);
        piePanel.add(stationPiechartPanel);
        piePanel.add(fuelPieChartPanel);

        //uitgaven
        DefaultPieDataset expensesDataset = new DefaultPieDataset();
        try {
            Map<String, ResultSet> allCosts = new HashMap<String, ResultSet>();
            ResultSet fuelCosts = database
                    .Query("SELECT SUM(cost) FROM fuelrecords WHERE motorcycleId = " + motor.getId(), true);
            allCosts.put("Brandstof", fuelCosts);
            ResultSet expenses = database.Query("SELECT DISTINCT categoryid FROM expenses", true);
            while (expenses.next()) {
                ResultSet set = database.Query("SELECT SUM(costs) FROM expenses WHERE categoryid = "
                        + expenses.getInt("categoryid") + " AND motorcycleid = " + motor.getId(), true);
                ResultSet set2 = database
                        .Query("SELECT name FROM categories WHERE id = " + expenses.getInt("categoryid"), true);
                set2.next();
                allCosts.put(set2.getString("name"), set);
            }

            for (Map.Entry<String, ResultSet> element : allCosts.entrySet()) {
                element.getValue().next();
                if (element.getValue().getInt("1") > 0) {
                    expensesDataset.setValue(element.getKey(), element.getValue().getInt("1"));
                }
            }

        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage() + ex.getCause());
        }
        JFreeChart expensesPiechart = ChartFactory.createPieChart3D("", expensesDataset, true, true, false);
        PiePlot3D plot4 = (PiePlot3D) expensesPiechart.getPlot();
        plot4.setForegroundAlpha(0.6f);

        JPanel expensesPiePanel = new ChartPanel(expensesPiechart);
        expensesPiePanel
                .setBorder(BorderFactory.createTitledBorder(BorderFactory.createTitledBorder("Uitgaven")));
        expensesPiePanel.setPreferredSize(new java.awt.Dimension(240, 240));
        expensesPiePanel.setLayout(new BorderLayout());

        graphContainer.add(piePanel);
        graphContainer.add(barChartPanel);
        graphContainer.add(expensesPiePanel);
    }
    revalidate();
    repaint();
}

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

/**
 * Calculates the size of a chart for a given fixed plot width and height
 * /* w  w w .  ja  v a2s.c om*/
 * @param chart
 * @param plotWidth
 * @return
 */
public static Dimension calcSizeForPlotSize(ChartPanel myChart, double plotWidth, double plotHeight,
        int iterations) {
    makeChartResizable(myChart);

    // estimate plotwidth / height
    double estimatedChartWidth = plotWidth + 200;
    double estimatedChartHeight = plotHeight + 200;

    double lastW = estimatedChartWidth;
    double lastH = estimatedChartHeight;

    // paint and get closer
    try {
        for (int i = 0; i < iterations; i++) {
            // paint on ghost panel with estimated height (if copy panel==true)
            myChart.setSize((int) estimatedChartWidth, (int) estimatedChartHeight);
            myChart.paintImmediately(myChart.getBounds());

            // rendering info
            ChartRenderingInfo info = myChart.getChartRenderingInfo();
            Rectangle2D dataArea = info.getPlotInfo().getDataArea();
            Rectangle2D chartArea = info.getChartArea();

            // // calc title space: will be added later to the right plot size
            // double titleWidth = chartArea.getWidth()-dataArea.getWidth();
            // double titleHeight = chartArea.getHeight()-dataArea.getHeight();

            // calc width and height
            estimatedChartWidth = estimatedChartWidth - dataArea.getWidth() + plotWidth;
            estimatedChartHeight = estimatedChartHeight - dataArea.getHeight() + plotHeight;

            if ((int) lastW == (int) estimatedChartWidth && (int) lastH == (int) estimatedChartHeight)
                break;
            else {
                lastW = estimatedChartWidth;
                lastH = estimatedChartHeight;
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return new Dimension((int) estimatedChartWidth, (int) estimatedChartHeight);
}

From source file:net.sf.mzmine.chartbasics.graphicsexport.ChartExportUtil.java

/**
 * takes Only Width in account/*  w w  w  .  j a  va  2  s  . co  m*/
 * 
 * @param chart
 * @param sett
 * @throws Exception
 */
public static void writeChartToImage(ChartPanel chart, GraphicsExportParameters sett) throws Exception {
    boolean repaint = false;
    FixedSize fixed = sett.getFixedSize();

    double oldW = sett.getWidthPixel();
    double oldH = sett.getHeightPixel();

    // Size only by width?
    if (sett.isUseOnlyWidth()) {
        // fixed size for chart or plot
        if (fixed.equals(FixedSize.Chart)) {
            sett.setHeightPixel(ChartLogics.calcHeightToWidth(chart, oldW, false));
        } else {
            // fixed plot width
            sett.setPixelSize(ChartLogics.calcSizeForPlotWidth(chart, oldW));
        }
    } else if (fixed.equals(FixedSize.Plot)) {
        // fixed plot size - width and height are given
        sett.setPixelSize(ChartLogics.calcSizeForPlotSize(chart, oldW, oldH));
    }

    Dimension size = sett.getPixelSize();
    // resize
    chart.setPreferredSize(size);
    chart.setMaximumSize(size);
    chart.setMinimumSize(size);
    // repaint
    if (repaint) {
        chart.revalidate();
        chart.repaint();
    }
    writeChartToImage(chart.getChart(), sett, chart.getChartRenderingInfo());
    // reset size
    sett.setPixelSize(oldW, oldH);
}

From source file:net.sf.mzmine.chartbasics.gui.swing.ChartGestureMouseAdapter.java

/**
 * Find chartentities like JFreeChartEntity, AxisEntity, PlotEntity, TitleEntity, XY...
 * //from www  . j a  v  a  2  s. com
 * @param chartPanel
 * @param x
 * @param y
 * @return
 */
private ChartEntity findChartEntity(ChartPanel chartPanel, MouseEvent e) {
    // coordinates to find chart entities
    Insets insets = chartPanel.getInsets();
    int x = (int) ((e.getX() - insets.left) / chartPanel.getScaleX());
    int y = (int) ((e.getY() - insets.top) / chartPanel.getScaleY());

    if (lastEntity != null && x == lastEntityX && y == lastEntityY)
        return lastEntity;
    else {
        ChartRenderingInfo info = chartPanel.getChartRenderingInfo();
        ChartEntity entity = null;
        if (info != null) {
            EntityCollection entities = info.getEntityCollection();
            if (entities != null) {
                entity = entities.getEntity(x, y);
            }
        }
        return entity;
    }
}