Example usage for org.jfree.chart ChartFrame setPreferredSize

List of usage examples for org.jfree.chart ChartFrame setPreferredSize

Introduction

In this page you can find the example usage for org.jfree.chart ChartFrame setPreferredSize.

Prototype

public void setPreferredSize(Dimension preferredSize) 

Source Link

Document

Sets the preferred size of this component to a constant value.

Usage

From source file:org.matsim.contrib.util.chart.ChartWindowUtils.java

public static void showFrame(JFreeChart chart, String title, int width, int height) {
    ChartFrame frame = new ChartFrame(title, chart);
    frame.setPreferredSize(new Dimension(width, height));
    SwingUtils.showWindow(frame, false);
}

From source file:com.imaging100x.tracker.TrackerUtils.java

/**
* Create a frame with a plot of the data given in XYSeries
*//*from w w w . j  a v  a2s .c o m*/
public static void plotData(String title, final XYSeries data, String xTitle, String yTitle, int xLocation,
        int yLocation) {
    // JFreeChart code
    XYSeriesCollection dataset = new XYSeriesCollection();
    dataset.addSeries(data);
    JFreeChart chart = ChartFactory.createScatterPlot(title, // Title
            xTitle, // x-axis Label
            yTitle, // y-axis Label
            dataset, // Dataset
            PlotOrientation.VERTICAL, // Plot Orientation
            false, // Show Legend
            true, // Use tooltips
            false // Configure chart to generate URLs?
    );
    final XYPlot plot = (XYPlot) chart.getPlot();
    plot.setBackgroundPaint(Color.white);
    plot.setRangeGridlinePaint(Color.lightGray);
    XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
    renderer.setBaseShapesVisible(true);
    renderer.setSeriesPaint(0, Color.black);
    renderer.setSeriesFillPaint(0, Color.white);
    renderer.setSeriesLinesVisible(0, true);
    Shape circle = new Ellipse2D.Float(-2.0f, -2.0f, 4.0f, 4.0f);
    renderer.setSeriesShape(0, circle, false);
    renderer.setUseFillPaint(true);

    ChartFrame graphFrame = new ChartFrame(title, chart);
    graphFrame.getChartPanel().setMouseWheelEnabled(true);
    graphFrame.setPreferredSize(new Dimension(SIZE, SIZE));
    graphFrame.setResizable(true);
    graphFrame.pack();
    graphFrame.setLocation(xLocation, yLocation);
    graphFrame.setVisible(true);

    dataset.addChangeListener(new DatasetChangeListener() {

        public void datasetChanged(DatasetChangeEvent dce) {
            double xRange = data.getMaxX() - data.getMinX();
            double yRange = data.getMaxY() - data.getMinY();
            double xAvg = (data.getMaxX() + data.getMinX()) / 2;
            double yAvg = (data.getMaxY() + data.getMinY()) / 2;
            double range = xRange;
            if (yRange > range) {
                range = yRange;
            }
            double offset = 0.55 * range;
            plot.getDomainAxis().setRange(xAvg - offset, xAvg + offset);
            plot.getRangeAxis().setRange(yAvg - offset, yAvg + offset);
        }

    });

}

From source file:jmbench.plots.MemoryRelativeBarPlot.java

public void displayWindow(int width, int height) {

    ChartFrame window = new ChartFrame(chart.getTitle().getText(), chart);

    window.setMinimumSize(new Dimension(width, height));
    window.setPreferredSize(window.getMinimumSize());
    window.setVisible(true);//from w  w w. ja  va 2s  .co m
}

From source file:jmbench.plots.SummaryWhiskerPlot.java

public void displayWindow(int width, int height) {

    JFreeChart chart = createChart();// w w w  .  j  av  a 2s  .  c om

    ChartFrame window = new ChartFrame(chart.getTitle().getText(), chart);

    window.setMinimumSize(new Dimension(width, height));
    window.setPreferredSize(window.getMinimumSize());
    window.setVisible(true);
}

From source file:e3fraud.gui.MainWindow.java

public void actionPerformed(ActionEvent e) {

    //Handle open button action.
    if (e.getSource() == openButton) {
        int returnVal = fc.showOpenDialog(MainWindow.this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            //parse file
            this.baseModel = FileParser.parseFile(file);
            log.append(currentTime.currentTime() + " Opened: " + file.getName() + "." + newline);
        } else {/*  w ww  .  j a  v  a2  s.  co m*/
            log.append(currentTime.currentTime() + " Open command cancelled by user." + newline);
        }
        log.setCaretPosition(log.getDocument().getLength());

        //handle Generate button
    } else if (e.getSource() == generateButton) {
        if (this.baseModel != null) {
            //have the user indicate the ToA via pop-up
            JFrame frame1 = new JFrame("Select Target of Assessment");
            Map<String, Resource> actorsMap = this.baseModel.getActorsMap();
            String selectedActorString = (String) JOptionPane.showInputDialog(frame1,
                    "Which actor's perspective are you taking?", "Choose main actor",
                    JOptionPane.QUESTION_MESSAGE, null, actorsMap.keySet().toArray(),
                    actorsMap.keySet().toArray()[0]);
            if (selectedActorString == null) {
                log.append(currentTime.currentTime() + " Attack generation cancelled!" + newline);
            } else {
                lastSelectedActorString = selectedActorString;
                //have the user select a need via pop-up
                JFrame frame2 = new JFrame("Select graph parameter");
                Map<String, Resource> needsMap = this.baseModel.getNeedsMap();
                String selectedNeedString = (String) JOptionPane.showInputDialog(frame2,
                        "What do you want to use as parameter?", "Choose need to parametrize",
                        JOptionPane.QUESTION_MESSAGE, null, needsMap.keySet().toArray(),
                        needsMap.keySet().toArray()[0]);
                if (selectedNeedString == null) {
                    log.append("Attack generation cancelled!" + newline);
                } else {
                    lastSelectedNeedString = selectedNeedString;
                    //have the user select occurence interval via pop-up
                    JTextField xField = new JTextField("1", 4);
                    JTextField yField = new JTextField("500", 4);
                    JPanel myPanel = new JPanel();
                    myPanel.add(new JLabel("Mininum occurences:"));
                    myPanel.add(xField);
                    myPanel.add(Box.createHorizontalStrut(15)); // a spacer
                    myPanel.add(new JLabel("Maximum occurences:"));
                    myPanel.add(yField);
                    int result = JOptionPane.showConfirmDialog(null, myPanel,
                            "Please Enter occurence rate interval", JOptionPane.OK_CANCEL_OPTION);

                    if (result == JOptionPane.CANCEL_OPTION) {
                        log.append("Attack generation cancelled!" + newline);
                    } else if (result == JOptionPane.OK_OPTION) {
                        startValue = Integer.parseInt(xField.getText());
                        endValue = Integer.parseInt(yField.getText());

                        selectedNeed = needsMap.get(selectedNeedString);
                        selectedActor = actorsMap.get(selectedActorString);

                        //Have a Worker thread to the time-consuming generation and raking (to not freeze the GUI)
                        GenerationWorker generationWorker = new GenerationWorker(baseModel, selectedActorString,
                                selectedActor, selectedNeed, selectedNeedString, startValue, endValue, log,
                                lossButton, gainButton, lossGainButton, gainLossButton, groupingButton,
                                collusionsButton) {
                            //make it so that when Worker is done
                            @Override
                            protected void done() {
                                try {
                                    progressBar.setVisible(false);
                                    System.err.println("I made it invisible");
                                    //the Worker's result is retrieved
                                    treeModel.setRoot(get());
                                    tree.setModel(treeModel);

                                    tree.updateUI();
                                    tree.collapseRow(1);
                                    //tree.expandRow(0);
                                    tree.setRootVisible(false);
                                    setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                                } catch (InterruptedException | ExecutionException ex) {
                                    Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
                                    setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                                    log.append("Out of memory; please increase heap size of JVM");
                                    PopUps.infoBox(
                                            "Encountered an error. Most likely out of memory; try increasing the heap size of JVM",
                                            "Error");
                                }
                            }
                        };
                        this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                        progressBar.setVisible(true);
                        progressBar.setIndeterminate(true);
                        progressBar.setString("generating...");
                        generationWorker.addPropertyChangeListener(new PropertyChangeListener() {
                            public void propertyChange(PropertyChangeEvent evt) {
                                if ("phase".equals(evt.getPropertyName())) {
                                    progressBar.setMaximum(100);
                                    progressBar.setIndeterminate(false);
                                    progressBar.setString("ranking...");
                                } else if ("progress".equals(evt.getPropertyName())) {
                                    progressBar.setValue((Integer) evt.getNewValue());
                                }
                            }
                        });
                        generationWorker.execute();
                    }
                }
            }
        } else {
            log.append("Load a model file first!" + newline);
        }
    } //handle the refresh button
    else if (e.getSource() == refreshButton) {
        if (lastSelectedNeedString != null && lastSelectedActorString != null) {
            Map<String, Resource> actorsMap = this.baseModel.getActorsMap();
            Map<String, Resource> needsMap = this.baseModel.getNeedsMap();
            selectedNeed = needsMap.get(lastSelectedNeedString);
            selectedActor = actorsMap.get(lastSelectedActorString);

            //Have a Worker thread to the time-consuming generation and raking (to not freeze the GUI)
            GenerationWorker generationWorker = new GenerationWorker(baseModel, lastSelectedActorString,
                    selectedActor, selectedNeed, lastSelectedNeedString, startValue, endValue, log, lossButton,
                    gainButton, lossGainButton, gainLossButton, groupingButton, collusionsButton) {
                //make it so that when Worker is done
                @Override
                protected void done() {
                    try {
                        progressBar.setVisible(false);
                        //the Worker's result is retrieved
                        treeModel.setRoot(get());
                        tree.setModel(treeModel);
                        tree.updateUI();
                        tree.collapseRow(1);
                        //tree.expandRow(0);
                        tree.setRootVisible(false);
                        setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    } catch (InterruptedException | ExecutionException ex) {
                        Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
                        setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                        log.append("Most likely out of memory; please increase heap size of JVM");
                        PopUps.infoBox(
                                "Encountered an error. Most likely out of memory; try increasing the heap size of JVM",
                                "Error");
                    }
                }
            };
            setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            progressBar.setVisible(true);
            progressBar.setIndeterminate(true);
            progressBar.setString("generating...");
            generationWorker.addPropertyChangeListener(new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent evt) {
                    if ("phase".equals(evt.getPropertyName())) {
                        progressBar.setMaximum(100);
                        progressBar.setIndeterminate(false);
                        progressBar.setString("ranking...");
                    } else if ("progress".equals(evt.getPropertyName())) {
                        progressBar.setValue((Integer) evt.getNewValue());
                    }
                }
            });
            generationWorker.execute();

        } else {
            log.append(currentTime.currentTime() + " Nothing to refresh. Generate models first" + newline);
        }

    } //handle show ideal graph button
    else if (e.getSource() == idealGraphButton) {
        if (this.baseModel != null) {
            graph1 = GraphingTool.generateGraph(baseModel, selectedNeed, startValue, endValue, true);//expected graph 
            ChartFrame chartframe1 = new ChartFrame("Ideal results", graph1);
            chartframe1.setPreferredSize(new Dimension(CHART_WIDTH, CHART_HEIGHT));
            chartframe1.pack();
            chartframe1.setLocationByPlatform(true);
            chartframe1.setVisible(true);
        } else {
            log.append(currentTime.currentTime() + " Load a model file first!" + newline);
        }
    } //Handle the graph extend button//Handle the graph extend button
    else if (e.getSource() == expandButton) {
        //make sure there is a graph to show
        if (graph2 == null) {
            log.append(currentTime.currentTime() + " No graph to display. Select one first." + newline);
        } else {
            //this makes sure both graphs have the same y axis:
            //            double lowerBound = min(graph1.getXYPlot().getRangeAxis().getRange().getLowerBound(), graph2.getXYPlot().getRangeAxis().getRange().getLowerBound());
            //            double upperBound = max(graph1.getXYPlot().getRangeAxis().getRange().getUpperBound(), graph2.getXYPlot().getRangeAxis().getRange().getUpperBound());
            //            graph1.getXYPlot().getRangeAxis().setRange(lowerBound, upperBound);
            //            graph2.getXYPlot().getRangeAxis().setRange(lowerBound, upperBound);
            chartPane.removeAll();
            chartPanel = new ChartPanel(graph2);
            chartPanel.setPreferredSize(new Dimension(CHART_WIDTH, CHART_HEIGHT));
            chartPane.add(chartPanel);
            chartPane.add(collapseButton);
            extended = true;
            this.setPreferredSize(new Dimension(this.getWidth() + CHART_WIDTH, this.getHeight()));
            JFrame frame = (JFrame) getRootPane().getParent();
            frame.pack();
        }
    } //Handle the graph collapse button//Handle the graph collapse button
    else if (e.getSource() == collapseButton) {
        System.out.println("resizing by -" + CHART_WIDTH);
        chartPane.removeAll();
        chartPane.add(expandButton);
        this.setPreferredSize(new Dimension(this.getWidth() - CHART_WIDTH, this.getHeight()));
        chartPane.repaint();
        chartPane.revalidate();
        extended = false;
        JFrame frame = (JFrame) getRootPane().getParent();
        frame.pack();
    }
}