Example usage for org.jfree.chart ChartPanel setLayout

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

Introduction

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

Prototype

public void setLayout(LayoutManager mgr) 

Source Link

Document

Sets the layout manager for this container.

Usage

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

public PieChartPanel(DefaultPieDataset pieDataset, String message) {
    JFreeChart pieChart = ChartFactory.createPieChart3D("", pieDataset, true, true, false);
    PiePlot3D plot1 = (PiePlot3D) pieChart.getPlot();
    plot1.setForegroundAlpha(0.6f);/*from  w  ww . j  a v a 2  s.  c om*/
    //plot3.setCircular(true);

    ChartPanel barChartPanel = new ChartPanel(pieChart);
    barChartPanel.getChartRenderingInfo().setEntityCollection(null);
    barChartPanel.setBorder(BorderFactory.createTitledBorder(message));
    barChartPanel.setPreferredSize(new java.awt.Dimension(320, 240));
    barChartPanel.setLayout(new BorderLayout());
    setLayout(new BorderLayout());
    add(barChartPanel);
}

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

public BarChartPanel(DefaultCategoryDataset barDataset, String message, boolean stacked) {
    JFreeChart barChart;//  www .  j a v a 2  s . co m
    if (stacked) {
        barChart = ChartFactory.createStackedBarChart3D("", // chart title
                "", // domain axis label
                "", // range axis label
                barDataset, // data
                PlotOrientation.VERTICAL, true, // include legend
                true, // tooltips?
                true // URLs?
        );
    } else {
        barChart = ChartFactory.createBarChart3D("", // chart title
                "", // domain axis label
                "", // range axis label
                barDataset, // data
                PlotOrientation.VERTICAL, false, // include legend
                true, // tooltips?
                true // URLs?
        );
    }
    CategoryPlot plot = barChart.getCategoryPlot();
    BarRenderer3D renderer = (BarRenderer3D) plot.getRenderer();
    renderer.setDrawBarOutline(false);
    final CategoryAxis domainAxis = plot.getDomainAxis();
    double count = barDataset.getColumnCount();
    double extra = 16 / count;
    domainAxis.setCategoryLabelPositions(
            CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / (2 + extra)));

    ChartPanel barChartPanel = new ChartPanel(barChart);
    barChartPanel.getChartRenderingInfo().setEntityCollection(null);
    barChartPanel.setBorder(BorderFactory.createTitledBorder(message));
    int wider = barDataset.getColumnCount() * 12;
    barChartPanel.setPreferredSize(new java.awt.Dimension(192 + wider, 240));
    barChartPanel.setLayout(new BorderLayout());
    setLayout(new BorderLayout());
    add(barChartPanel);
}

From source file:com.devoteam.srit.xmlloader.core.report.derived.DerivedCounter.java

public ChartPanel getChartPanel() {
    ChartPanel chartPanel = new ChartPanel(null);
    chartPanel.setLayout(new javax.swing.BoxLayout(chartPanel, javax.swing.BoxLayout.Y_AXIS));
    chartPanel.setAlignmentX(java.awt.Component.LEFT_ALIGNMENT);
    chartPanel.setAlignmentY(java.awt.Component.TOP_ALIGNMENT);

    if (timeChart == null) {
        timeChart = getTimeChart();/* ww w . j  av a2 s  .  com*/
    }
    chartPanel.add(new ChartPanel(timeChart));

    if (this instanceof StatValue) {
        if (histogramChart == null) {
            histogramChart = getHistogramChart();
        }
        chartPanel.add(new ChartPanel(histogramChart));
    }

    return chartPanel;
}

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

private void refreshGraphs(Motorcycle motor) {
    graphContainer.removeAll();/*from www . j  av a  2s  .c o m*/
    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:trendgraph.XYLineChart_AWT.java

public XYLineChart_AWT(int yearStart, int yearEnd, String[] creditUnionName, String columnName)
        throws SQLException {
    super("Graph");
    super.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    this.yearStart = yearStart;
    this.yearEnd = yearEnd;
    this.creditUnionName = creditUnionName;
    this.columnName = columnName;
    saveGraphButton = new JButton("Save Graph");
    saveGraphButton.setBorderPainted(false);
    saveGraphButton.setFocusPainted(false);

    JFreeChart xylineChart = ChartFactory.createXYLineChart("CU Report", "Year (YYYY)", //X-axis
            columnName, //Y-axis (replace with columnName
            createDataset(), PlotOrientation.VERTICAL, true, true, false);

    ChartPanel chartPanel = new ChartPanel(xylineChart);
    chartPanel.setPreferredSize(new java.awt.Dimension(1000, 800)); //(x, y)
    final XYPlot plot = xylineChart.getXYPlot();
    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();

    renderer.setSeriesPaint(0, Color.RED); //can be GREEN, YELLOW, ETC.

    renderer.setSeriesStroke(0, new BasicStroke(3.0f)); //Font size

    renderer.setSeriesPaint(1, Color.BLUE); //can be GREEN, YELLOW, ETC.

    renderer.setSeriesStroke(1, new BasicStroke(3.0f)); //Font size

    renderer.setSeriesPaint(2, Color.GREEN); //can be GREEN, YELLOW, ETC.

    renderer.setSeriesStroke(2, new BasicStroke(3.0f)); //Font size

    renderer.setSeriesPaint(3, Color.yellow); //can be GREEN, YELLOW, ETC.

    renderer.setSeriesStroke(3, new BasicStroke(3.0f)); //Font size

    plot.setRenderer(renderer);//  w w w  .ja  v  a2s . c o  m
    chartPanel.setLayout(new FlowLayout(FlowLayout.TRAILING));
    chartPanel.add(saveGraphButton);
    setContentPane(chartPanel);
    pack();
    RefineryUtilities.centerFrameOnScreen(this);
    setVisible(true);

    saveGraphButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Rectangle rect = chartPanel.getBounds();
            FileChooser chooser = new FileChooser();

            //get chosen path and save the variable
            String path = chooser.getPath();
            path = path.replace("\\", "/");

            String format = "png";
            String fileName = path + "." + format;
            BufferedImage captureImage = new BufferedImage(rect.width, rect.height,
                    BufferedImage.TYPE_INT_ARGB);
            chartPanel.paint(captureImage.getGraphics());

            File file = new File(fileName);

            try {
                ImageIO.write(captureImage, format, file);

                //write data to file
            } catch (IOException ex) {
                Logger.getLogger(XYLineChart_AWT.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });
}