Example usage for org.jfree.chart ChartPanel setPreferredSize

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

Introduction

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

Prototype

@BeanProperty(preferred = true, description = "The preferred size of the component.")
public void setPreferredSize(Dimension preferredSize) 

Source Link

Document

Sets the preferred size of this component.

Usage

From source file:net.commerce.zocalo.freechart.ChartGenerator.java

public static JFreeChart buildBarChart(DefaultCategoryDataset dataset, PlotOrientation orientation, int hSize,
        int vSize) {
    JFreeChart chart = ChartFactory.createBarChart(null, // chart title
            null, // domain axis label
            null, // range axis label
            dataset, // data
            orientation, // the plot orientation
            false, // include legend
            false, // tooltips
            false // generate urls
    );//ww w. j  a  v  a 2  s. c om

    CategoryPlot plot = (CategoryPlot) chart.getPlot();
    plot.setNoDataMessage("NO DATA!");

    BarRenderer renderer = (BarRenderer) plot.getRenderer();
    renderer.setBaseItemLabelsVisible(true);
    renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    plot.setRenderer(renderer);

    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setUpperMargin(0.15);

    ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new Dimension(hSize, vSize));
    chart.setBackgroundPaint(Color.white);
    return chart;
}

From source file:unalcol.termites.boxplots.SucessfulRates.java

private static JFreeChart createChart(CategoryDataset categorydataset, ArrayList<Double> pf) {
    JFreeChart jfreechart = ChartFactory.createBarChart("Success Rates - " + getTitle(pf), "", "",
            categorydataset, PlotOrientation.VERTICAL, true, true, false);
    jfreechart.getTitle().setFont(new Font("Sans-Serif", Font.PLAIN, 18));
    jfreechart.setBackgroundPaint(new Color(221, 223, 238));
    CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
    categoryplot.setBackgroundPaint(Color.white);
    categoryplot.setDomainGridlinePaint(Color.white);
    categoryplot.setRangeGridlinePaint(Color.gray);
    categoryplot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);

    BarRenderer renderer = (BarRenderer) categoryplot.getRenderer();
    //categoryplot.setBackgroundPaint(new Color(221, 223, 238));

    renderer.setSeriesPaint(0, new Color(130, 165, 70));
    renderer.setSeriesPaint(1, new Color(220, 165, 70));
    renderer.setSeriesPaint(4, new Color(255, 165, 70));
    renderer.setDrawBarOutline(false);//from   www. ja  v a2s  . co m
    renderer.setShadowVisible(false);
    // renderer.setMaximumBarWidth(1);
    renderer.setGradientPaintTransformer(null);
    renderer.setDefaultBarPainter(new StandardBarPainter());

    categoryplot.setRenderer(renderer);

    NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
    numberaxis.setUpperMargin(0.25D);
    CategoryItemRenderer categoryitemrenderer = categoryplot.getRenderer();
    categoryitemrenderer.setBaseItemLabelsVisible(true);
    categoryitemrenderer.setBaseItemLabelGenerator(new LabelGenerator(null));
    numberaxis.setRange(0, 100);
    //numberaxis.setNumberFormatOverride(NumberFormat.getPercentInstance());

    Font font = new Font("SansSerif", Font.ROMAN_BASELINE, 12);
    numberaxis.setTickLabelFont(font);
    CategoryAxis axisd = categoryplot.getDomainAxis();
    ValueAxis axisr = categoryplot.getRangeAxis();
    axisd.setTickLabelFont(font);
    axisr.setTickLabelFont(font);

    final ChartPanel chartPanel = new ChartPanel(jfreechart);
    chartPanel.setPreferredSize(new java.awt.Dimension(650, 370));

    FileOutputStream output;
    try {
        output = new FileOutputStream("sucessrates1" + pf + mazeMode + ".jpg");
        ChartUtilities.writeChartAsJPEG(output, 1.0f, jfreechart, 650, 370, null);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(MessagesSent1.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(MessagesSent1.class.getName()).log(Level.SEVERE, null, ex);
    }
    return jfreechart;
}

From source file:oct.util.Util.java

private static JPanel createChartPanel(String title, XYDataset dataset) {
    String xAxisLabel = "X";
    String yAxisLabel = "Y";

    JFreeChart chart = ChartFactory.createXYLineChart(title, xAxisLabel, yAxisLabel, dataset);
    ChartPanel panel = new ChartPanel(chart);
    panel.setPreferredSize(new Dimension(640, 480));
    panel.setFillZoomRectangle(true);/*  ww  w.  j  a  v  a2s. c o  m*/
    panel.setMouseWheelEnabled(true);
    return panel;
}

From source file:edu.unc.LCCC.caBIG.DWD.javaCode.visualization.SetUpPlotWindow.java

public static JPanel createXYLineAndShapeRendererChart(XYDataset data, String x, String y, int w, int h) {
    //XYDataset data = new SampleXYDataset2();
    JFreeChart chart = ChartFactory.createScatterPlot(null, // the title of the chart
            x, // the label for the X axis
            y, // the label for the Y axis
            data, // the dataset for the chart
            PlotOrientation.VERTICAL, // the orientation of the chart
            true, // a flag specifying whether or not a legend is required
            true, // a flag specifying whether or not tooltips should be generated
            false); // a flag specifying whether or not the chart should generate URLs

    XYPlot plot = (XYPlot) chart.getPlot();
    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
    renderer.setSeriesLinesVisible(0, false);
    renderer.setSeriesShapesVisible(0, true);

    renderer.setSeriesLinesVisible(1, false);
    renderer.setSeriesShapesVisible(1, true);

    renderer.setSeriesLinesVisible(2, true);
    renderer.setSeriesShapesVisible(2, false);

    renderer.setToolTipGenerator(new StandardXYToolTipGenerator());
    renderer.setDefaultEntityRadius(6);/*  ww  w .  j a v  a 2  s .  c o m*/
    plot.setRenderer(renderer);

    ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(w, h));

    return chartPanel;
}

From source file:edu.unc.LCCC.caBIG.DWD.javaCode.visualization.SetUpPlotWindow.java

public static JPanel createXYScatterPlotChart(XYDataset data, String x, String y, int w, int h) {
    //XYDataset data = new SampleXYDataset2();
    JFreeChart chart = ChartFactory.createScatterPlot(null, // the title of the chart
            x, // the label for the X axis
            y, // the label for the Y axis
            data, // the dataset for the chart
            PlotOrientation.VERTICAL, // the orientation of the chart
            true, // a flag specifying whether or not a legend is required
            true, // a flag specifying whether or not tooltips should be generated
            false); // a flag specifying whether or not the chart should generate URLs

    XYPlot plot = (XYPlot) chart.getXYPlot();

    plot.setDomainCrosshairVisible(true);
    plot.setRangeCrosshairVisible(true);
    /*/*from  ww w . j a  v  a 2 s.  co m*/
    NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
    domainAxis.setAutoRangeIncludesZero(false);
             
    ValueAxis xAxis = plot.getDomainAxis();
    xAxis.setFixedDimension(50);
            
    ValueAxis yAxis = plot.getRangeAxis();
    */
    //StandardXYItemRenderer renderer = (StandardXYItemRenderer) plot.getRenderer();
    //renderer.setSeriesPaint(0, java.awt.Color.red);

    ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(w, h));

    return chartPanel;
}

From source file:lu.lippmann.cdb.lab.mds.MDSViewBuilder.java

/**
 * /*  w w  w . jav a  2  s . co m*/
 * @param clusters
 */
public static void buildKMeansChart(final List<Instances> clusters) {
    final XYSeriesCollection dataset = new XYSeriesCollection();

    final JFreeChart chart = ChartFactory.createScatterPlot("", // title 
            "X", "Y", // axis labels 
            dataset, // dataset 
            PlotOrientation.VERTICAL, true, // legend? yes 
            true, // tooltips? yes 
            false // URLs? no 
    );

    final XYPlot xyPlot = (XYPlot) chart.getPlot();

    ((NumberAxis) xyPlot.getDomainAxis()).setTickUnit(new NumberTickUnit(2.0));
    ((NumberAxis) xyPlot.getRangeAxis()).setTickUnit(new NumberTickUnit(2.0));

    Attribute clsAttribute = null;
    int nbClass = 1;
    Instances cluster0 = clusters.get(0);
    if (cluster0.classIndex() != -1) {
        clsAttribute = cluster0.classAttribute();
        nbClass = clsAttribute.numValues();
    }
    if (nbClass <= 1) {
        dataset.addSeries(new XYSeries("Serie #1", false));
    } else {
        for (int i = 0; i < nbClass; i++) {
            dataset.addSeries(new XYSeries(clsAttribute.value(i), false));
        }
    }

    final XYToolTipGenerator gen = new XYToolTipGenerator() {
        @Override
        public String generateToolTip(XYDataset dataset, int series, int item) {
            return "TODO";
        }
    };

    for (int i = 0; i < nbClass; i++) {
        dataset.getSeries(i).clear();
        xyPlot.getRenderer().setSeriesToolTipGenerator(i, gen);
    }

    final int nbClusters = clusters.size();
    for (int i = 0; i < nbClusters; i++) {
        Instances instances = clusters.get(i);
        final int nbInstances = instances.numInstances();
        for (int j = 0; j < nbInstances; j++) {
            final Instance oInst = instances.instance(j);
            dataset.getSeries(i).add(oInst.value(0), oInst.value(1));
        }
    }

    final TitledBorder titleBorder = new TitledBorder("Kmeans of projection");
    ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setMouseWheelEnabled(true);
    chartPanel.setPreferredSize(new Dimension(1200, 900));
    chartPanel.setBorder(titleBorder);
    chartPanel.setBackground(Color.WHITE);

    JXFrame frame = new JXFrame();
    frame.getContentPane().add(chartPanel);
    frame.setVisible(true);
    frame.pack();

}

From source file:lu.lippmann.cdb.lab.mds.MDSViewBuilder.java

/**
 * /* ww w  .  j av a  2 s  .co m*/
 */
public static JXPanel buildMDSViewFromDataSet(final Instances instances, final MDSResult mdsResult,
        final int maxInstances, final Listener<Instances> listener, final String... attrNameToUseAsPointTitle)
        throws Exception {

    final XYSeriesCollection dataset = new XYSeriesCollection();

    final JFreeChart chart = ChartFactory.createScatterPlot("", // title 
            "X", "Y", // axis labels 
            dataset, // dataset 
            PlotOrientation.VERTICAL, attrNameToUseAsPointTitle.length == 0, // legend? 
            true, // tooltips? yes 
            false // URLs? no 
    );

    final XYPlot xyPlot = (XYPlot) chart.getPlot();

    xyPlot.setBackgroundPaint(Color.WHITE);
    xyPlot.getDomainAxis().setTickLabelsVisible(false);
    xyPlot.getRangeAxis().setTickLabelsVisible(false);

    //FIXME : should be different for Shih
    if (!mdsResult.isNormalized()) {
        String stress = FormatterUtil.DECIMAL_FORMAT
                .format(ClassicMDS.getKruskalStressFromMDSResult(mdsResult));
        chart.setTitle(mdsResult.getCInstances().isCollapsed()
                ? "Collapsed MDS(Instances=" + maxInstances + ",Stress=" + stress + ")"
                : "MDS(Stress=" + stress + ")");
    } else {
        chart.setTitle(mdsResult.getCInstances().isCollapsed() ? "Collapsed MDS(Instances=" + maxInstances + ")"
                : "MDS");
    }

    final SimpleMatrix coordinates = mdsResult.getCoordinates();
    buildFilteredSeries(mdsResult, xyPlot, attrNameToUseAsPointTitle);

    final ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setMouseWheelEnabled(true);
    chartPanel.setPreferredSize(new Dimension(1200, 900));
    chartPanel.setBorder(new TitledBorder("MDS Projection"));
    chartPanel.setBackground(Color.WHITE);

    final JButton selectionButton = new JButton("Select data");
    selectionButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            final org.jfree.data.Range XDomainRange = xyPlot.getDomainAxis().getRange();
            final org.jfree.data.Range YDomainRange = xyPlot.getRangeAxis().getRange();
            final Instances cInstances = mdsResult.getCollapsedInstances();
            final Instances selectedInstances = new Instances(cInstances, 0);
            List<Instances> clusters = null;
            if (mdsResult.getCInstances().isCollapsed()) {
                clusters = mdsResult.getCInstances().getCentroidMap().getClusters();
            }
            for (int i = 0; i < cInstances.numInstances(); i++) {
                final Instance centroid = instances.instance(i);
                if (XDomainRange.contains(coordinates.get(i, 0))
                        && YDomainRange.contains(coordinates.get(i, 1))) {
                    if (mdsResult.getCInstances().isCollapsed()) {
                        if (clusters != null) {
                            final Instances elementsOfCluster = clusters.get(i);
                            final int nbElements = elementsOfCluster.numInstances();
                            for (int k = 0; k < nbElements; k++) {
                                selectedInstances.add(elementsOfCluster.get(k));
                            }
                        }
                    } else {
                        selectedInstances.add(centroid);
                    }
                }
            }
            if (listener != null) {
                listener.onAction(selectedInstances);
            }
        }
    });

    final JXPanel allPanel = new JXPanel();
    allPanel.setLayout(new BorderLayout());
    allPanel.add(chartPanel, BorderLayout.CENTER);
    final JXPanel southPanel = new JXPanel();
    southPanel.add(selectionButton);
    allPanel.add(southPanel, BorderLayout.SOUTH);
    return allPanel;
}

From source file:org.esa.snap.rcp.statistics.StatisticsPanel.java

private static ChartPanel getHistogramPlotPanel(XIntervalSeriesCollection dataset, String xAxisLabel,
        String yAxisLabel, Color color) {
    JFreeChart chart = ChartFactory.createHistogram(null, xAxisLabel, yAxisLabel, dataset,
            PlotOrientation.VERTICAL, false, // Legend?
            true, // tooltips
            false // url
    );/*from  w w w . j  a v a  2 s.com*/
    final XYPlot xyPlot = chart.getXYPlot();
    //xyPlot.setForegroundAlpha(0.85f);
    xyPlot.setNoDataMessage("No data");
    xyPlot.setAxisOffset(new RectangleInsets(5, 5, 5, 5));

    final XYBarRenderer renderer = (XYBarRenderer) xyPlot.getRenderer();
    renderer.setDrawBarOutline(false);
    renderer.setShadowVisible(false);
    renderer.setSeriesPaint(0, color);
    StandardXYBarPainter painter = new StandardXYBarPainter();
    renderer.setBarPainter(painter);

    ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new Dimension(300, 200));
    //        chartPanel.getPopupMenu().add(createCopyDataToClipboardMenuItem());
    return chartPanel;
}

From source file:test.LineChart_AWT.java

public LineChart_AWT(String applicationTitle, String chartTitle) {
    super(applicationTitle);
    JFreeChart lineChart = ChartFactory.createLineChart(chartTitle, "Years", "Number of Schools",
            createDataset(), PlotOrientation.VERTICAL, true, true, false);

    ChartPanel chartPanel = new ChartPanel(lineChart);
    chartPanel.setPreferredSize(new java.awt.Dimension(560, 367));
    setContentPane(chartPanel);//  w ww . ja v  a 2 s  .  com
}

From source file:org.jfree.chart.demo.StackedBarDemo1.java

public StackedBarDemo1(String s) {
    super(s);/*from  w  w w  .  j a v  a 2 s  .c  o  m*/
    DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
    defaultcategorydataset.addValue(1.0D, "Row 1", "Column 1");
    defaultcategorydataset.addValue(5D, "Row 1", "Column 2");
    defaultcategorydataset.addValue(3D, "Row 1", "Column 3");
    defaultcategorydataset.addValue(2D, "Row 2", "Column 1");
    defaultcategorydataset.addValue(3D, "Row 2", "Column 2");
    defaultcategorydataset.addValue(2D, "Row 2", "Column 3");
    org.jfree.chart.JFreeChart jfreechart = ChartFactory.createStackedBarChart("StackedBarDemo1", "Category",
            "Value", defaultcategorydataset, PlotOrientation.VERTICAL, true, true, false);
    ChartPanel chartpanel = new ChartPanel(jfreechart, false);
    chartpanel.setPreferredSize(new Dimension(500, 270));
    setContentPane(chartpanel);
}