Example usage for org.jfree.chart.plot XYPlot XYPlot

List of usage examples for org.jfree.chart.plot XYPlot XYPlot

Introduction

In this page you can find the example usage for org.jfree.chart.plot XYPlot XYPlot.

Prototype

public XYPlot(XYDataset dataset, ValueAxis domainAxis, ValueAxis rangeAxis, XYItemRenderer renderer) 

Source Link

Document

Creates a new plot with the specified dataset, axes and renderer.

Usage

From source file:org.hxzon.demo.jfreechart.DatasetVisibleDemo.java

private static JFreeChart createTimeSeriesChart(XYDataset dataset) {
    //DomainAxis//w ww .  ja v  a  2 s .c o  m
    DateAxis timeAxis = new DateAxis("");
    timeAxis.setLowerMargin(0.02); // reduce the default margins
    timeAxis.setUpperMargin(0.02);
    timeAxis.setDateFormatOverride(new SimpleDateFormat("MM-yyyy"));
    //RangeAxis
    NumberAxis valueAxis = new NumberAxis("");
    valueAxis.setAutoRangeIncludesZero(false); // override default
    //        valueAxis.setAutoRange(false);
    //        valueAxis.setDefaultAutoRange(new Range(100, 1150));
    //Renderer
    XYToolTipGenerator toolTipGenerator = StandardXYToolTipGenerator.getTimeSeriesInstance();
    XYURLGenerator urlGenerator = null;
    //            urlGenerator = new StandardXYURLGenerator();
    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true, false);
    renderer.setBaseToolTipGenerator(toolTipGenerator);
    renderer.setURLGenerator(urlGenerator);
    renderer.setBaseShapesVisible(true);
    renderer.setBaseShapesFilled(true);
    renderer.setDrawSeriesLineAsPath(true);
    //Plot
    XYPlot plot = new XYPlot(dataset, timeAxis, valueAxis, null);
    plot.setRenderer(renderer);
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);
    plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
    plot.setDomainCrosshairVisible(true);
    plot.setRangeCrosshairVisible(true);
    //        plot.setRangePannable(true);
    //chart
    JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, true);

    chart.setBackgroundPaint(Color.white);
    return chart;
}

From source file:org.hxzon.demo.jfreechart.XYDatasetDemo2.java

private static JFreeChart createTimeSeriesChart(XYDataset dataset) {

    DateAxis timeAxis = new DateAxis(xAxisLabel);
    timeAxis.setLowerMargin(0.02); // reduce the default margins
    timeAxis.setUpperMargin(0.02);//from  w w w.j a  v a 2 s.  c  o  m
    NumberAxis valueAxis = new NumberAxis(yAxisLabel);
    valueAxis.setAutoRangeIncludesZero(false); // override default
    XYPlot plot = new XYPlot(dataset, timeAxis, valueAxis, null);

    XYToolTipGenerator toolTipGenerator = null;
    if (tooltips) {
        toolTipGenerator = StandardXYToolTipGenerator.getTimeSeriesInstance();
    }

    XYURLGenerator urlGenerator = null;
    if (urls) {
        urlGenerator = new StandardXYURLGenerator();
    }

    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true, false);
    renderer.setBaseToolTipGenerator(toolTipGenerator);
    renderer.setURLGenerator(urlGenerator);
    plot.setRenderer(renderer);

    JFreeChart chart = new JFreeChart("TimeSeries Chart Demo", JFreeChart.DEFAULT_TITLE_FONT, plot, legend);
    chart.setBackgroundPaint(Color.white);

    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);
    plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
    plot.setDomainCrosshairVisible(true);
    plot.setRangeCrosshairVisible(true);

    //        renderer.setBaseShapesVisible(true);
    renderer.setBaseShapesFilled(true);
    renderer.setDrawSeriesLineAsPath(true);

    timeAxis.setDateFormatOverride(new SimpleDateFormat("MM-yyyy"));

    return chart;
}

From source file:net.sf.maltcms.chromaui.msviewer.ui.panel.MassSpectrumPanel.java

private void initChartComponents() {

    this.sc = new XYSeriesCollection();
    barDataset = new XYBarDataset(sc, barWidth);
    XYBarRenderer renderer = new XYBarRenderer(0.1d);
    StandardXYBarPainter sp = new StandardXYBarPainter();
    renderer.setBarPainter(sp);/*from   w w  w  . j  av a  2s . c  o m*/
    renderer.setShadowVisible(false);
    renderer.setDrawBarOutline(false);
    NumberAxis intensityAxis = new NumberAxis("intensity");
    intensityAxis.setNumberFormatOverride(defaultNumberFormat);
    intensityAxis.setUpperMargin(0.10d);
    NumberAxis mzAxis = new NumberAxis("m/z");
    mzAxis.setAutoRangeIncludesZero(false);
    this.plot = new XYPlot(barDataset, mzAxis, intensityAxis, renderer);
    this.plot.setForegroundAlpha(0.85f);

    plot.setDomainCrosshairLockedOnData(true);
    plot.setDomainCrosshairVisible(true);
    ((XYBarRenderer) plot.getRenderer()).setShadowVisible(false);
    ((XYBarRenderer) plot.getRenderer()).setDrawBarOutline(false);
    ((XYBarRenderer) plot.getRenderer()).setBaseFillPaint(Color.RED);
    ((XYBarRenderer) plot.getRenderer()).setBarPainter(new StandardXYBarPainter());
    plot.getRenderer().setBaseItemLabelsVisible(true);
    plot.getRenderer().setBaseToolTipGenerator(new XYToolTipGenerator() {
        @Override
        public String generateToolTip(XYDataset xyd, int i, int i1) {
            Comparable comp = xyd.getSeriesKey(i);
            double x = xyd.getXValue(i, i1);
            double y = xyd.getYValue(i, i1);
            StringBuilder sb = new StringBuilder();
            sb.append(comp);
            sb.append(": ");
            sb.append("x=");
            sb.append(String.format("%.4f", x));
            sb.append(" y=");
            sb.append(String.format("%.4f", y));
            return sb.toString();
        }
    });
    plot.setDomainPannable(true);
    plot.setRangePannable(true);
    sc.addChangeListener(plot);
    JFreeChart msChart = new JFreeChart(this.plot);
    msChart.addChangeListener(this.defaultNumberFormat);
    //      System.out.println("Creating ms chart 3");
    this.cp = new ContextAwareChartPanel(msChart, true, true, true, true, true);
    this.cp.setInitialDelay(1);
    this.cp.getChart().getLegend().setVisible(true);
    this.cp.setMouseWheelEnabled(true);
    this.clearActionPerformed(null);
    this.jPanel2.removeAll();
    this.jPanel2.add(cp);
    this.jPanel2.repaint();
    this.massLabelsSpinner.setValue(topK);
}

From source file:projects.tgas.exec.HrDiagram.java

/**
 * Plot the HR diagram chart from the given data.
 * @param series/*from  w  w  w.  j a  v  a2s  .c  o  m*/
 *    A {@link XYSeries} containing the HR diagram data.
 * @return
 *    A {@link JFreeChart} containing the plot.
 */
private static JFreeChart getHrChart(XYSeries series) {

    XYSeriesCollection hrData = new XYSeriesCollection();
    hrData.addSeries(series);

    // Set up the renderer
    XYLineAndShapeRenderer hrRenderer = new XYLineAndShapeRenderer();

    hrRenderer.setSeriesLinesVisible(0, false);
    hrRenderer.setSeriesShapesVisible(0, true);
    hrRenderer.setSeriesShape(0, new Ellipse2D.Double(-0.5, -0.5, 1, 1));

    // Configure axes
    NumberAxis xAxis = new NumberAxis("B - V [mag]");
    xAxis.setRange(-0.5, 2.25);

    NumberAxis yAxis = new NumberAxis("G [mag]");
    yAxis.setInverted(true);
    yAxis.setRange(-5, 13);

    // Configure plot
    XYPlot xyplot = new XYPlot(hrData, xAxis, yAxis, hrRenderer);
    xyplot.setBackgroundPaint(Color.white);

    JFreeChart hrChart = new JFreeChart("HR diagram of TGAS stars", xyplot);
    hrChart.removeLegend();
    hrChart.setBackgroundPaint(Color.white);

    return hrChart;
}

From source file:asl.util.PlotMaker.java

public void plotZNE_3x3(ArrayList<double[]> channelData, double[] xsecs, int nstart, int nend,
        String eventString, String plotString) {

    // Expecting 9 channels packed like:            Panel   Trace1  Trace2  Trace3
    // channels[0] = 00-LHZ                           1     00-LHZ   10-LHZ   20-LHZ
    // channels[1] = 00-LHND                          2     00-LHND  10-LHND  20-LHND
    // channels[2] = 00-LHED                          3     00-LHED  10-LHED  20-LHED
    // channels[3] = 10-LHZ                           
    // channels[4] = 10-LHND                          
    // channels[5] = 10-LHED                          
    // channels[6] = 20-LHZ                           
    // channels[7] = 20-LHND                         
    // channels[8] = 20-LHED                        

    final String plotTitle = String.format("%04d%03d [Stn:%s] [Event:%s] %s", date.get(Calendar.YEAR),
            date.get(Calendar.DAY_OF_YEAR), station, eventString, plotString);
    final String pngName = String.format("%s/%s.%s.%s.png", outputDir, eventString, station, plotString);
    File outputFile = new File(pngName);

    // Check that we will be able to output the file without problems and if not --> return
    if (!checkFileOut(outputFile)) {
        System.out.format("== plotZNE_3x3: request to output plot=[%s] but we are unable to create it "
                + " --> skip plot\n", pngName);
        return;//  w ww. j a va2s  . c o  m
    }

    if (channelData.size() != channels.length) {
        System.out.format("== plotZNE_3x3: Error: We have [%d channels] but [%d channelData]\n",
                channels.length, channelData.size());
        return;
    }

    XYSeries[] series = new XYSeries[channels.length];
    for (int i = 0; i < channels.length; i++) {
        series[i] = new XYSeries(channels[i].toString());
        double[] data = channelData.get(i);
        //for (int k = 0; k < xsecs.length; k++){
        for (int k = 0; k < data.length; k++) {
            series[i].add(xsecs[k], data[k]);
        }
    }

    // I. Panel I = Verticals

    // Use the first data array, within the plotted range (nstart - nend) to scale the plots:
    double[] data = channelData.get(0);
    double ymax = 0;
    for (int k = nstart; k < nend; k++) {
        if (data[k] > ymax)
            ymax = data[k];
    }

    final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
    Paint[] paints = new Paint[] { Color.red, Color.blue, Color.green };
    for (int i = 0; i < paints.length; i++) {
        renderer.setSeriesPaint(i, paints[i]);
        renderer.setSeriesLinesVisible(i, true);
        renderer.setSeriesShapesVisible(i, false);
    }

    final NumberAxis verticalAxis = new NumberAxis("Displacement (m)");
    verticalAxis.setRange(new Range(-ymax, ymax));
    //verticalAxis.setTickUnit( new NumberTickUnit(5) );

    final NumberAxis horizontalAxis = new NumberAxis("Time (s)");
    horizontalAxis.setRange(new Range(nstart, nend));
    //horizontalAxis.setRange( new Range(0.00009 , 110) );
    final NumberAxis hAxis = new NumberAxis("Time (s)");
    hAxis.setRange(new Range(nstart, nend));

    final XYSeriesCollection seriesCollection1 = new XYSeriesCollection();
    seriesCollection1.addSeries(series[0]);
    seriesCollection1.addSeries(series[3]);
    seriesCollection1.addSeries(series[6]);
    //final XYPlot xyplot1 = new XYPlot((XYDataset)seriesCollection1, null, verticalAxis, renderer);
    //final XYPlot xyplot1 = new XYPlot((XYDataset)seriesCollection1, horizontalAxis, verticalAxis, renderer);
    final XYPlot xyplot1 = new XYPlot((XYDataset) seriesCollection1, hAxis, verticalAxis, renderer);
    double x = .95 * xsecs[nend];
    double y = .90 * ymax;
    XYTextAnnotation annotation1 = new XYTextAnnotation("Vertical", x, y);
    annotation1.setFont(new Font("SansSerif", Font.PLAIN, 14));
    xyplot1.addAnnotation(annotation1);

    // II. Panel II = North

    // Use the first data array, within the plotted range (nstart - nend) to scale the plots:
    data = channelData.get(1);
    ymax = 0;
    for (int k = nstart; k < nend; k++) {
        if (data[k] > ymax)
            ymax = data[k];
    }
    final NumberAxis verticalAxisN = new NumberAxis("Displacement (m)");
    verticalAxisN.setRange(new Range(-ymax, ymax));

    final XYSeriesCollection seriesCollection2 = new XYSeriesCollection();
    seriesCollection2.addSeries(series[1]);
    seriesCollection2.addSeries(series[4]);
    seriesCollection2.addSeries(series[7]);
    final XYPlot xyplot2 = new XYPlot((XYDataset) seriesCollection2, null, verticalAxisN, renderer);
    XYTextAnnotation annotation2 = new XYTextAnnotation("North-South", x, y);
    annotation2.setFont(new Font("SansSerif", Font.PLAIN, 14));
    xyplot2.addAnnotation(annotation2);

    // III. Panel III = East

    // Use the first data array, within the plotted range (nstart - nend) to scale the plots:
    data = channelData.get(2);
    ymax = 0;
    for (int k = nstart; k < nend; k++) {
        if (data[k] > ymax)
            ymax = data[k];
    }
    final NumberAxis verticalAxisE = new NumberAxis("Displacement (m)");
    verticalAxisE.setRange(new Range(-ymax, ymax));

    final XYSeriesCollection seriesCollection3 = new XYSeriesCollection();
    seriesCollection3.addSeries(series[2]);
    seriesCollection3.addSeries(series[5]);
    seriesCollection3.addSeries(series[8]);
    final XYPlot xyplot3 = new XYPlot((XYDataset) seriesCollection3, null, verticalAxisE, renderer);
    XYTextAnnotation annotation3 = new XYTextAnnotation("East-West", x, y);
    annotation3.setFont(new Font("SansSerif", Font.PLAIN, 14));
    xyplot3.addAnnotation(annotation3);

    //CombinedXYPlot combinedPlot = new CombinedXYPlot( horizontalAxis, CombinedXYPlot.VERTICAL );
    CombinedDomainXYPlot combinedPlot = new CombinedDomainXYPlot(horizontalAxis);
    combinedPlot.add(xyplot1, 1);
    combinedPlot.add(xyplot2, 1);
    combinedPlot.add(xyplot3, 1);
    combinedPlot.setGap(15.);

    final JFreeChart chart = new JFreeChart(combinedPlot);
    chart.setTitle(new TextTitle(plotTitle));

    try {
        ChartUtilities.saveChartAsPNG(outputFile, chart, 1400, 800);
    } catch (IOException e) {
        System.err.println("Problem occurred creating chart.");

    }

}

From source file:org.optaplanner.benchmark.impl.statistic.bestscore.BestScoreProblemStatistic.java

private XYPlot createPlot(BenchmarkReport benchmarkReport, int scoreLevelIndex) {
    Locale locale = benchmarkReport.getLocale();
    NumberAxis xAxis = new NumberAxis("Time spent");
    xAxis.setNumberFormatOverride(new MillisecondsSpentNumberFormat(locale));
    NumberAxis yAxis = new NumberAxis("Best score level " + scoreLevelIndex);
    yAxis.setNumberFormatOverride(NumberFormat.getInstance(locale));
    yAxis.setAutoRangeIncludesZero(false);
    XYPlot plot = new XYPlot(null, xAxis, yAxis, null);
    plot.setOrientation(PlotOrientation.VERTICAL);
    return plot;/*w w w  .j  a  va2s . c  o  m*/
}

From source file:JQGraphicModule.QueryTableApp.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    ChartPanel panel;/*  www .  j  ava2  s .c  o m*/
    JFreeChart chart = null;
    if (l.isSelected()) {
        // linear chart
        int validar = 1;
        XYSplineRenderer renderer = new XYSplineRenderer();
        XYSeriesCollection dataset = new XYSeriesCollection();

        XYSeries serie = new XYSeries("Data");

        XYPlot plot;
        lineas.removeAll();

        try {
            for (int f = 0; f < 6; f++) {
                serie.add(Float.parseFloat(String.valueOf(datos.getValueAt(f, 0))),
                        Float.parseFloat(String.valueOf(datos.getValueAt(f, 1))));
            }
        } catch (Exception e) {
            validar = 0;
            System.out.println("Cannot create data series for line graph");
        } // end catch

        if (validar == 1) {
            dataset.addSeries(serie);
            plot = new XYPlot(dataset, x, y, rederer);
            chart = new JFreeChart(plot);
            chart.setTitle("Line chart");
        } else {
            JOptionPane.showMessageDialog(this, "You should fill the table with data");
        }

    } else {
        if (b.isSelected()) {
            // bar chart
            DefaultCategoryDataset data = new DefaultCategoryDataset();
            // TODO: finish the bar chart
        } else {
            // pir chart
        } // end else
    }
    panel = new ChartPanel(chart);
    panel.setBounds(5, 10, 410, 350);
    if (l.isSelected()) {
        lineas.add(panel);
        lineas.repaint();
    } else {
        if (b.isSelected()) {
            //bar chart
        } //end if
        else { // pie chart
        } // end else
    } //end else
}

From source file:org.spantus.exp.segment.draw.AbstractGraphGenerator.java

public JFreeChart getChart(ComparisionResult result) {
    CombinedDomainXYPlot plot = new CombinedDomainXYPlot(new NumberAxis("Time"));
    plot.setGap(10.0);/*  w ww. j  a v  a2s .  co m*/
    plot.setOrientation(PlotOrientation.VERTICAL);

    XYSeriesCollection[] seriesArr = createSeries(result);
    for (XYSeriesCollection series : seriesArr) {
        XYSeriesCollection data = series;
        StandardXYItemRenderer renderer = new StandardXYItemRenderer();
        renderer.setAutoPopulateSeriesPaint(false);
        renderer.setBasePaint(Color.BLACK);
        NumberAxis rangeAxis = new NumberAxis();
        rangeAxis.setLabel(((XYSeries) series.getSeries().get(0)).getDescription());
        rangeAxis.setAutoRange(true);
        XYPlot subplot = new XYPlot(data, null, rangeAxis, renderer);
        plot.add(subplot);
    }
    String name = result.getName() == null ? "Segmentation" : "Segmentation: " + result.getName();

    JFreeChart chart = new JFreeChart(name, JFreeChart.DEFAULT_TITLE_FONT, plot, true);

    return chart;

}

From source file:edu.cudenver.bios.chartsvc.resource.LegendResource.java

private XYPlot buildScatterPlot(Chart chart) throws ResourceException {
    // the first series is treated as the x values
    if (chart.getSeries() == null || chart.getSeries().size() <= 0)
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "No data series specified");

    // create the jfree chart series
    XYSeriesCollection chartData = new XYSeriesCollection();
    // use a spline renderer to make the connecting lines smooth
    XYSplineRenderer rend = new XYSplineRenderer();

    int seriesIdx = 0;
    for (Series series : chart.getSeries()) {
        XYSeries xySeries = new XYSeries(series.getLabel());

        List<Double> xList = series.getXCoordinates();
        List<Double> yList = series.getYCoordinates();
        if (xList != null && yList != null && xList.size() == yList.size()) {
            for (int i = 0; i < xList.size(); i++) {
                xySeries.add(xList.get(i), yList.get(i));
            }//from www.  j  a v a 2 s. co m
        }

        // set the line style
        rend.setSeriesPaint(seriesIdx, Color.BLACK);
        if (seriesIdx > 0) {
            rend.setSeriesStroke(seriesIdx, new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
                    1.0f, new float[] { (float) seriesIdx, (float) 2 * seriesIdx }, 0.0f));
        }
        // add the series to the data set
        chartData.addSeries(xySeries);
        seriesIdx++;
    }

    // turn off shapes displayed at each data point to make a smooth curve
    rend.setBaseShapesVisible(false);

    // Create the line chart
    NumberAxis xAxis = new NumberAxis();
    xAxis.setAutoRangeIncludesZero(false);
    if (chart.getXAxis() != null) {
        Axis xAxisSpec = chart.getXAxis();
        xAxis.setLabel(xAxisSpec.getLabel());
        if (!Double.isNaN(xAxisSpec.getRangeMin()) && !Double.isNaN(xAxisSpec.getRangeMax())) {
            xAxis.setRange(xAxisSpec.getRangeMin(), xAxisSpec.getRangeMax());
        }
    }
    NumberAxis yAxis = new NumberAxis();
    if (chart.getYAxis() != null) {
        Axis yAxisSpec = chart.getYAxis();
        yAxis.setLabel(chart.getYAxis().getLabel());
        if (!Double.isNaN(yAxisSpec.getRangeMin()) && !Double.isNaN(yAxisSpec.getRangeMax())) {
            xAxis.setRange(yAxisSpec.getRangeMin(), yAxisSpec.getRangeMax());
        }
    }
    XYPlot plot = new XYPlot((XYDataset) chartData, xAxis, yAxis, rend);
    plot.setDomainGridlinesVisible(false);
    plot.setRangeGridlinesVisible(false);

    return plot;
}

From source file:de.dal33t.powerfolder.ui.information.stats.StatsInformationCard.java

private JPanel getAveragePanel() {
    DateAxis domain = new DateAxis(Translation.getTranslation("stats_information_card.date"));
    TimeSeriesCollection series = new TimeSeriesCollection();
    NumberAxis axis = new NumberAxis(Translation.getTranslation("stats_information_card.percentage"));

    series.addSeries(percentageBandwidthSeries);

    XYItemRenderer renderer = new StandardXYItemRenderer();
    XYPlot plot = new XYPlot(series, domain, axis, renderer);
    JFreeChart graph = new JFreeChart(plot);
    ChartPanel cp = new ChartPanel(graph);

    FormLayout layout = new FormLayout("3dlu, fill:pref:grow, 3dlu",
            "3dlu, pref , 3dlu, pref, 3dlu, fill:pref:grow, 3dlu");
    DefaultFormBuilder builder = new DefaultFormBuilder(layout);
    CellConstraints cc = new CellConstraints();

    JPanel p = buildPercentStatsControlPanel();

    builder.add(p, cc.xy(2, 2));/*from  w  w  w. j av  a2s  .  co  m*/
    builder.addSeparator(null, cc.xyw(1, 4, 3));
    builder.add(cp, cc.xy(2, 6));
    return builder.getPanel();
}