Example usage for org.jfree.chart.axis NumberAxis setRange

List of usage examples for org.jfree.chart.axis NumberAxis setRange

Introduction

In this page you can find the example usage for org.jfree.chart.axis NumberAxis setRange.

Prototype

public void setRange(Range range) 

Source Link

Document

Sets the range for the axis and sends a change event to all registered listeners.

Usage

From source file:net.atomique.ksar.xml.PlotStackConfig.java

public NumberAxis getAxis() {
    NumberAxis tmp = new NumberAxis(title);

    if (base == 1024) {

        NumberFormat decimalformat1 = new IEEE1541Number((int) factor);
        tmp.setNumberFormatOverride(decimalformat1);

    } else if (base == 1000) {

        NumberFormat decimalformat1 = new ISNumber((int) factor);
        tmp.setNumberFormatOverride(decimalformat1);

    } else if (base != 0) {
        log.error("base value is not handled");
    }/*  ww  w .j  a  v  a 2 s .  com*/

    if (range != null) {
        tmp.setRange(range);
    }
    return tmp;
}

From source file:asl.util.PlotMaker2.java

public void writePlot(String fileName) {
    //System.out.format("== plotTitle=[%s] fileName=[%s]\n", plotTitle, fileName);

    File outputFile = new File(fileName);

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

    NumberAxis horizontalAxis = new NumberAxis("x-axis default"); // x = domain

    if (fileName.contains("nlnm") || fileName.contains("coher") || fileName.contains("stn")) { // NLNM or StationDeviation 
        horizontalAxis = new LogarithmicAxis("Period (sec)");
        horizontalAxis.setRange(new Range(1, 11000));
        horizontalAxis.setTickUnit(new NumberTickUnit(5.0));
    } else { // EventCompareSynthetics/StrongMotion
        horizontalAxis = new NumberAxis("Time (s)");
        double x[] = panels.get(0).getTraces().get(0).getxData();
        horizontalAxis.setRange(new Range(x[0], x[x.length - 1]));
    }

    CombinedDomainXYPlot combinedPlot = new CombinedDomainXYPlot(horizontalAxis);
    combinedPlot.setGap(15.);

    // Loop over (3) panels for this plot:

    for (Panel panel : panels) {

        NumberAxis verticalAxis = new NumberAxis("y-axis default"); // y = range

        if (fileName.contains("nlnm") || fileName.contains("stn")) { // NLNM or StationDeviation 
            verticalAxis = new NumberAxis("PSD 10log10(m**2/s**4)/Hz dB");
            verticalAxis.setRange(new Range(-190, -95));
            verticalAxis.setTickUnit(new NumberTickUnit(5.0));
        } else if (fileName.contains("coher")) { // Coherence
            verticalAxis = new NumberAxis("Coherence, Gamma");
            verticalAxis.setRange(new Range(0, 1.2));
            verticalAxis.setTickUnit(new NumberTickUnit(0.1));
        } else { // EventCompareSynthetics/StrongMotion
            verticalAxis = new NumberAxis("Displacement (m)");
        }

        Font fontPlain = new Font("Verdana", Font.PLAIN, 14);
        Font fontBold = new Font("Verdana", Font.BOLD, 18);
        verticalAxis.setLabelFont(fontBold);
        verticalAxis.setTickLabelFont(fontPlain);
        horizontalAxis.setLabelFont(fontBold);
        horizontalAxis.setTickLabelFont(fontPlain);

        XYSeriesCollection seriesCollection = new XYSeriesCollection();
        XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        XYPlot xyplot = new XYPlot((XYDataset) seriesCollection, horizontalAxis, verticalAxis, renderer);
        xyplot.setDomainGridlinesVisible(true);
        xyplot.setRangeGridlinesVisible(true);
        xyplot.setRangeGridlinePaint(Color.black);
        xyplot.setDomainGridlinePaint(Color.black);

        // Plot each trace on this panel:
        int iTrace = 0;
        for (Trace trace : panel.getTraces()) {

            XYSeries series = new XYSeries(trace.getName());

            double xdata[] = trace.getxData();
            double ydata[] = trace.getyData();
            for (int k = 0; k < xdata.length; k++) {
                series.add(xdata[k], ydata[k]);
            }

            renderer.setSeriesPaint(iTrace, trace.getColor());
            renderer.setSeriesStroke(iTrace, trace.getStroke());
            renderer.setSeriesLinesVisible(iTrace, true);
            renderer.setSeriesShapesVisible(iTrace, false);

            seriesCollection.addSeries(series);

            iTrace++;
        }

        // Add Annotations for each trace - This is done in a separate loop so that
        //                      the upper/lower limits for this panel will be known
        double xmin = horizontalAxis.getRange().getLowerBound();
        double xmax = horizontalAxis.getRange().getUpperBound();
        double ymin = verticalAxis.getRange().getLowerBound();
        double ymax = verticalAxis.getRange().getUpperBound();
        double delX = Math.abs(xmax - xmin);
        double delY = Math.abs(ymax - ymin);

        // Annotation (x,y) in normalized units - where upper-right corner = (1,1)
        double xAnn = 0.97; // Right center coords of the trace name (e.g., "00-LHZ")
        double yAnn = 0.95;

        double yOff = 0.05; // Vertical distance between different trace legends

        iTrace = 0;
        for (Trace trace : panel.getTraces()) {
            if (!trace.getName().contains("NLNM") && !trace.getName().contains("NHNM")) {
                // x1 > x2 > x3, e.g.:
                //  o-------o   00-LHZ
                //  x3     x2       x1

                double scale = .01; // Controls distance between trace label and line segment
                double xL = .04; // Length of trace line segment in legend

                double xAnn2 = xAnn - scale * trace.getName().length();
                double xAnn3 = xAnn - scale * trace.getName().length() - xL;

                double x1 = xAnn * delX + xmin; // Right hand x-coord of text in range units
                double x2 = xAnn2 * delX + xmin; // x-coord of line segment end in range units
                double x3 = xAnn3 * delX + xmin; // x-coord of line segment end in range units

                double y = (yAnn - (iTrace * yOff)) * delY + ymin;

                if (horizontalAxis instanceof LogarithmicAxis) {
                    double logMin = Math.log10(xmin);
                    double logMax = Math.log10(xmax);
                    delX = logMax - logMin;
                    x1 = Math.pow(10, xAnn * delX + logMin);
                    x2 = Math.pow(10, xAnn2 * delX + logMin);
                    x3 = Math.pow(10, xAnn3 * delX + logMin);
                }
                xyplot.addAnnotation(new XYLineAnnotation(x3, y, x2, y, trace.getStroke(), trace.getColor()));
                XYTextAnnotation xyText = new XYTextAnnotation(trace.getName(), x1, y);
                xyText.setFont(new Font("Verdana", Font.BOLD, 18));
                xyText.setTextAnchor(TextAnchor.CENTER_RIGHT);
                xyplot.addAnnotation(xyText);
            }
            iTrace++;
        }

        combinedPlot.add(xyplot, 1);

    } // panel

    final JFreeChart chart = new JFreeChart(combinedPlot);
    chart.setTitle(new TextTitle(plotTitle, new Font("Verdana", Font.BOLD, 18)));
    chart.removeLegend();

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

    }

}

From source file:asl.plotmaker.PlotMaker2.java

public void writePlot(String fileName) {
    // System.out.format("== plotTitle=[%s] fileName=[%s]\n", plotTitle,
    // fileName);

    File outputFile = new File(fileName);

    // Check that we will be able to output the file without problems and if
    // not --> return
    if (!checkFileOut(outputFile)) {
        // System.out.format("== plotMaker: request to output plot=[%s] but we are unable to create it "
        // + " --> skip plot\n", fileName );
        logger.warn("== Request to output plot=[{}] but we are unable to create it " + " --> skip plot\n",
                fileName);/* ww  w  .j ava2  s .co m*/
        return;
    }

    NumberAxis horizontalAxis = new NumberAxis("x-axis default"); // x =
    // domain

    if (fileName.contains("alnm") || fileName.contains("nlnm") || fileName.contains("coher")
            || fileName.contains("stn")) { // NLNM or StationDeviation
        horizontalAxis = new LogarithmicAxis("Period (sec)");
        horizontalAxis.setRange(new Range(1, 11000));
        horizontalAxis.setTickUnit(new NumberTickUnit(5.0));
    } else { // EventCompareSynthetics/StrongMotion
        horizontalAxis = new NumberAxis("Time (s)");
        double x[] = panels.get(0).getTraces().get(0).getxData();
        horizontalAxis.setRange(new Range(x[0], x[x.length - 1]));
    }

    CombinedDomainXYPlot combinedPlot = new CombinedDomainXYPlot(horizontalAxis);
    combinedPlot.setGap(15.);

    // Loop over (3) panels for this plot:

    for (Panel panel : panels) {

        NumberAxis verticalAxis = new NumberAxis("y-axis default"); // y =
        // range

        if (fileName.contains("alnm") || fileName.contains("nlnm") || fileName.contains("stn")) { // NLNM
            // or
            // StationDeviation
            verticalAxis = new NumberAxis("PSD 10log10(m**2/s**4)/Hz dB");
            verticalAxis.setRange(new Range(-190, -80));
            verticalAxis.setTickUnit(new NumberTickUnit(5.0));
        } else if (fileName.contains("coher")) { // Coherence
            verticalAxis = new NumberAxis("Coherence, Gamma");
            verticalAxis.setRange(new Range(0, 1.2));
            verticalAxis.setTickUnit(new NumberTickUnit(0.1));
        } else { // EventCompareSynthetics/StrongMotion
            verticalAxis = new NumberAxis("Displacement (m)");
        }

        Font fontPlain = new Font("Verdana", Font.PLAIN, 14);
        Font fontBold = new Font("Verdana", Font.BOLD, 18);
        verticalAxis.setLabelFont(fontBold);
        verticalAxis.setTickLabelFont(fontPlain);
        horizontalAxis.setLabelFont(fontBold);
        horizontalAxis.setTickLabelFont(fontPlain);

        XYSeriesCollection seriesCollection = new XYSeriesCollection();
        XYDotRenderer renderer = new XYDotRenderer();
        XYPlot xyplot = new XYPlot(seriesCollection, horizontalAxis, verticalAxis, renderer);
        xyplot.setDomainGridlinesVisible(true);
        xyplot.setRangeGridlinesVisible(true);
        xyplot.setRangeGridlinePaint(Color.black);
        xyplot.setDomainGridlinePaint(Color.black);

        // Plot each trace on this panel:
        int iTrace = 0;
        for (Trace trace : panel.getTraces()) {

            XYSeries series = new XYSeries(trace.getName());

            double xdata[] = trace.getxData();
            double ydata[] = trace.getyData();
            for (int k = 0; k < xdata.length; k++) {
                series.add(xdata[k], ydata[k]);
            }

            renderer.setSeriesPaint(iTrace, trace.getColor());
            renderer.setSeriesStroke(iTrace, trace.getStroke());

            seriesCollection.addSeries(series);

            iTrace++;
        }

        // Add Annotations for each trace - This is done in a separate loop
        // so that
        // the upper/lower limits for this panel will be known
        double xmin = horizontalAxis.getRange().getLowerBound();
        double xmax = horizontalAxis.getRange().getUpperBound();
        double ymin = verticalAxis.getRange().getLowerBound();
        double ymax = verticalAxis.getRange().getUpperBound();
        double delX = Math.abs(xmax - xmin);
        double delY = Math.abs(ymax - ymin);

        // Annotation (x,y) in normalized units - where upper-right corner =
        // (1,1)
        double xAnn = 0.97; // Right center coords of the trace name (e.g.,
        // "00-LHZ")
        double yAnn = 0.95;

        double yOff = 0.05; // Vertical distance between different trace
        // legends

        iTrace = 0;
        for (Trace trace : panel.getTraces()) {
            if (!trace.getName().contains("NLNM") && !trace.getName().contains("NHNM")
                    && !trace.getName().contains("ALNM")) {
                // x1 > x2 > x3, e.g.:
                // o-------o 00-LHZ
                // x3 x2 x1

                double scale = .01; // Controls distance between trace label
                // and line segment
                double xL = .04; // Length of trace line segment in legend

                double xAnn2 = xAnn - scale * trace.getName().length();
                double xAnn3 = xAnn - scale * trace.getName().length() - xL;

                double x1 = xAnn * delX + xmin; // Right hand x-coord of
                // text in range units
                double x2 = xAnn2 * delX + xmin; // x-coord of line segment
                // end in range units
                double x3 = xAnn3 * delX + xmin; // x-coord of line segment
                // end in range units

                double y = (yAnn - (iTrace * yOff)) * delY + ymin;

                if (horizontalAxis instanceof LogarithmicAxis) {
                    double logMin = Math.log10(xmin);
                    double logMax = Math.log10(xmax);
                    delX = logMax - logMin;
                    x1 = Math.pow(10, xAnn * delX + logMin);
                    x2 = Math.pow(10, xAnn2 * delX + logMin);
                    x3 = Math.pow(10, xAnn3 * delX + logMin);
                }
                xyplot.addAnnotation(new XYLineAnnotation(x3, y, x2, y, trace.getStroke(), trace.getColor()));
                XYTextAnnotation xyText = new XYTextAnnotation(trace.getName(), x1, y);
                xyText.setFont(new Font("Verdana", Font.BOLD, 18));
                xyText.setTextAnchor(TextAnchor.CENTER_RIGHT);
                xyplot.addAnnotation(xyText);
            }
            iTrace++;
        }

        combinedPlot.add(xyplot, 1);

    } // panel

    final JFreeChart chart = new JFreeChart(combinedPlot);
    chart.setTitle(new TextTitle(plotTitle, new Font("Verdana", Font.BOLD, 18)));
    chart.removeLegend();

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

}

From source file:asl.util.PlotMaker.java

public void plotCoherence(double per[], double[] gamma, String plotString) {

    final String plotTitle = String.format("%04d%03d.%s.%s-%s", date.get(Calendar.YEAR),
            date.get(Calendar.DAY_OF_YEAR), station, channelX, channelY);
    final String pngName = String.format("%s/%04d%03d.%s.%s-%s.%s.png", outputDir, date.get(Calendar.YEAR),
            date.get(Calendar.DAY_OF_YEAR), station, channelX, channelY, 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("== plotCoherence: request to output plot=[%s] but we are unable to create it "
                + " --> skip plot\n", pngName);
        return;//  ww w.  java 2s .  com
    }

    final String legend = String.format("%s--%s", channelX, channelY);
    final XYSeries series1 = new XYSeries(legend);

    for (int k = 0; k < gamma.length; k++) {
        series1.add(per[k], gamma[k]);
    }

    //final XYItemRenderer renderer1 = new StandardXYItemRenderer();
    final XYLineAndShapeRenderer renderer1 = new XYLineAndShapeRenderer();
    Rectangle rectangle = new Rectangle(3, 3);
    renderer1.setSeriesShape(0, rectangle);
    renderer1.setSeriesShapesVisible(0, true);
    renderer1.setSeriesLinesVisible(0, false);

    Paint[] paints = new Paint[] { Color.red, Color.black };
    renderer1.setSeriesPaint(0, paints[0]);

    final NumberAxis rangeAxis1 = new NumberAxis("Coherence, Gamma");
    rangeAxis1.setRange(new Range(0, 1.2));
    rangeAxis1.setTickUnit(new NumberTickUnit(0.1));

    final LogarithmicAxis horizontalAxis = new LogarithmicAxis("Period (sec)");
    horizontalAxis.setRange(new Range(0.05, 10000));

    final XYSeriesCollection seriesCollection = new XYSeriesCollection();
    seriesCollection.addSeries(series1);

    final XYPlot xyplot = new XYPlot((XYDataset) seriesCollection, horizontalAxis, rangeAxis1, renderer1);

    xyplot.setDomainGridlinesVisible(true);
    xyplot.setRangeGridlinesVisible(true);
    xyplot.setRangeGridlinePaint(Color.black);
    xyplot.setDomainGridlinePaint(Color.black);

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

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

    }
}

From source file:dk.netarkivet.harvester.harvesting.monitor.StartedJobHistoryChartGen.java

/**
 * Set the axis range./*  w  w  w .jav  a2 s .  co  m*/
 * @param axis a numberAxis
 * @param range a range
 */
private void setAxisRange(NumberAxis axis, double[] range) {
    if (range == null || range.length != 2) {
        axis.setAutoRange(true);
    } else {
        double lower = range[0];
        double upper = range[1];
        ArgumentNotValid.checkTrue(lower < upper, "Incorrect range");
        axis.setAutoRange(false);
        axis.setRange(new Range(lower, upper));
    }
}

From source file:asl.util.PlotMaker.java

public void plotSpecAmp(double freq[], double[] amp, double[] phase, String plotString) {

    // plotTitle = "2012074.IU_ANMO.00-BHZ " + plotString
    final String plotTitle = String.format("%04d%03d.%s.%s %s", date.get(Calendar.YEAR),
            date.get(Calendar.DAY_OF_YEAR), station, channel, plotString);
    // plot filename = "2012074.IU_ANMO.00-BHZ" + plotString + ".png"
    final String pngName = String.format("%s/%04d%03d.%s.%s.%s.png", outputDir, date.get(Calendar.YEAR),
            date.get(Calendar.DAY_OF_YEAR), station, channel, 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("== plotSpecAmp: request to output plot=[%s] but we are unable to create it "
                + " --> skip plot\n", pngName);
        return;//www  .  ja va 2  s  .  c om
    }

    final XYSeries series1 = new XYSeries("Amplitude");
    final XYSeries series2 = new XYSeries("Phase");

    double maxdB = 0.;
    for (int k = 0; k < freq.length; k++) {
        double dB = 20. * Math.log10(amp[k]);
        series1.add(freq[k], dB);
        series2.add(freq[k], phase[k]);
        if (dB > maxdB) {
            maxdB = dB;
        }
    }

    //final XYItemRenderer renderer = new StandardXYItemRenderer();
    final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
    Rectangle rectangle = new Rectangle(3, 3);
    renderer.setSeriesShape(0, rectangle);
    //renderer.setSeriesShapesVisible(0, true);
    renderer.setSeriesShapesVisible(0, false);
    renderer.setSeriesLinesVisible(0, true);

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

    Paint[] paints = new Paint[] { Color.red, Color.blue };
    renderer.setSeriesPaint(0, paints[0]);
    //renderer.setSeriesPaint(1, paints[1]);

    final XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer();
    renderer2.setSeriesPaint(0, paints[1]);
    renderer2.setSeriesShapesVisible(0, false);
    renderer2.setSeriesLinesVisible(0, true);

    // Stroke is part of Java Swing ...
    //renderer2.setBaseStroke( new Stroke( ... ) );

    double ymax;
    if (maxdB < 10) {
        ymax = 10.;
    } else {
        ymax = maxdB + 2;
        ;
    }

    final NumberAxis verticalAxis = new NumberAxis("Spec Amp (dB)");
    verticalAxis.setRange(new Range(-40, ymax));
    verticalAxis.setTickUnit(new NumberTickUnit(5));

    //final LogarithmicAxis verticalAxis = new LogarithmicAxis("Amplitude Response");
    //verticalAxis.setRange( new Range(0.01 , 10) );

    final LogarithmicAxis horizontalAxis = new LogarithmicAxis("Frequency (Hz)");
    //horizontalAxis.setRange( new Range(0.0001 , 100.5) );
    horizontalAxis.setRange(new Range(0.00009, 110));

    final XYSeriesCollection seriesCollection = new XYSeriesCollection();
    seriesCollection.addSeries(series1);

    final XYPlot xyplot = new XYPlot((XYDataset) seriesCollection, null, verticalAxis, renderer);
    //final XYPlot xyplot = new XYPlot((XYDataset)seriesCollection, horizontalAxis, verticalAxis, renderer);

    xyplot.setDomainGridlinesVisible(true);
    xyplot.setRangeGridlinesVisible(true);
    xyplot.setRangeGridlinePaint(Color.black);
    xyplot.setDomainGridlinePaint(Color.black);

    final NumberAxis phaseAxis = new NumberAxis("Phase (Deg)");
    phaseAxis.setRange(new Range(-180, 180));
    phaseAxis.setTickUnit(new NumberTickUnit(30));
    final XYSeriesCollection seriesCollection2 = new XYSeriesCollection();
    seriesCollection2.addSeries(series2);
    final XYPlot xyplot2 = new XYPlot((XYDataset) seriesCollection2, null, phaseAxis, renderer2);

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

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

    // Here we need to see if test dir exists and create it if necessary ...
    try {
        //ChartUtilities.saveChartAsJPEG(new File("chart.jpg"), chart, 500, 300);
        //ChartUtilities.saveChartAsPNG(outputFile, chart, 500, 300);
        ChartUtilities.saveChartAsPNG(outputFile, chart, 1000, 800);
    } catch (IOException e) {
        System.err.println("Problem occurred creating chart.");

    }
}

From source file:asl.util.PlotMaker.java

public void plotSpecAmp2(double freq[], double[] amp1, double[] phase1, double[] amp2, double[] phase2,
        String plotTitle, String pngName) {

    /**//from   w  ww.ja v a 2s .c om
            final String plotTitle = String.format("%04d%03d.%s.%s %s", date.get(Calendar.YEAR), date.get(Calendar.DAY_OF_YEAR)
                                        ,station, channel, plotString);
            final String pngName   = String.format("%s/%04d%03d.%s.%s.%s.png", outputDir, date.get(Calendar.YEAR), date.get(Calendar.DAY_OF_YEAR)
                                        ,station, channel, 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("== plotSpecAmp: request to output plot=[%s] but we are unable to create it "
                + " --> skip plot\n", pngName);
        return;
    }
    // Plot x-axis (frequency) range
    final double XMIN = .00009;
    final double XMAX = freq[freq.length - 1];

    System.out.format("== plotSpecAmp2: nfreq=%d npts=%d pngName=%s\n", freq.length, amp2.length, pngName);

    final XYSeries series1 = new XYSeries("Amp_PZ");
    final XYSeries series1b = new XYSeries("Amp_Cal");

    final XYSeries series2 = new XYSeries("Phase_PZ");
    final XYSeries series2b = new XYSeries("Phase_Cal");

    double maxdB = 0.;
    for (int k = 0; k < freq.length; k++) {
        double dB = amp1[k];
        //double dB = 20. * Math.log10( amp1[k] );
        //series1.add( freq[k], dB );
        //series1.add( freq[k], 20. * Math.log10( amp1[k] ) );
        //series1b.add(freq[k], 20. * Math.log10( amp2[k] ));
        series1.add(freq[k], amp1[k]);
        series1b.add(freq[k], amp2[k]);
        series2.add(freq[k], phase1[k]);
        series2b.add(freq[k], phase2[k]);
        if (dB > maxdB) {
            maxdB = dB;
        }
    }

    //final XYItemRenderer renderer = new StandardXYItemRenderer();
    final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
    Rectangle rectangle = new Rectangle(3, 3);
    renderer.setSeriesShape(0, rectangle);
    //renderer.setSeriesShapesVisible(0, true);
    renderer.setSeriesShapesVisible(0, false);
    renderer.setSeriesLinesVisible(0, true);

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

    Paint[] paints = new Paint[] { Color.red, Color.blue };
    renderer.setSeriesPaint(0, paints[0]);
    //renderer.setSeriesPaint(1, paints[1]);

    final XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer();
    renderer2.setSeriesPaint(0, paints[1]);
    renderer2.setSeriesShapesVisible(0, false);
    renderer2.setSeriesLinesVisible(0, true);

    // Stroke is part of Java Swing ...
    //renderer2.setBaseStroke( new Stroke( ... ) );

    double ymax;
    if (maxdB < 10) {
        ymax = 10.;
    } else {
        ymax = maxdB + 2;
        ;
    }

    final NumberAxis verticalAxis = new NumberAxis("Spec Amp (dB)");
    verticalAxis.setRange(new Range(-40, ymax));
    verticalAxis.setTickUnit(new NumberTickUnit(5));

    //final LogarithmicAxis verticalAxis = new LogarithmicAxis("Amplitude Response");
    //verticalAxis.setRange( new Range(0.01 , 10) );

    final LogarithmicAxis horizontalAxis = new LogarithmicAxis("Frequency (Hz)");
    //horizontalAxis.setRange( new Range(0.0001 , 100.5) );
    //horizontalAxis.setRange( new Range(0.00009 , 110) );
    horizontalAxis.setRange(new Range(XMIN, XMAX));

    final XYSeriesCollection seriesCollection = new XYSeriesCollection();
    seriesCollection.addSeries(series1);
    seriesCollection.addSeries(series1b);

    final XYPlot xyplot = new XYPlot((XYDataset) seriesCollection, null, verticalAxis, renderer);
    //final XYPlot xyplot = new XYPlot((XYDataset)seriesCollection, horizontalAxis, verticalAxis, renderer);

    xyplot.setDomainGridlinesVisible(true);
    xyplot.setRangeGridlinesVisible(true);
    xyplot.setRangeGridlinePaint(Color.black);
    xyplot.setDomainGridlinePaint(Color.black);

    final NumberAxis phaseAxis = new NumberAxis("Phase (Deg)");
    phaseAxis.setRange(new Range(-180, 180));
    phaseAxis.setTickUnit(new NumberTickUnit(30));
    final XYSeriesCollection seriesCollection2 = new XYSeriesCollection();
    seriesCollection2.addSeries(series2);
    seriesCollection2.addSeries(series2b);
    final XYPlot xyplot2 = new XYPlot((XYDataset) seriesCollection2, null, phaseAxis, renderer2);

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

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

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

    }
}

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;/*from   w  ww  .  jav a2  s .  co  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:asl.util.PlotMaker.java

public void plotPSD(double per[], double[] model, double[] nhnmPer, double[] nhnm, double[] psd,
        String modelName, String plotString) {

    // plotTitle = "2012074.IU_ANMO.00-BHZ " + plotString
    final String plotTitle = String.format("%04d%03d.%s.%s %s", date.get(Calendar.YEAR),
            date.get(Calendar.DAY_OF_YEAR), station, channel, plotString);
    // plot filename = "2012074.IU_ANMO.00-BHZ" + plotString + ".png"
    final String pngName = String.format("%s/%04d%03d.%s.%s.%s.png", outputDir, date.get(Calendar.YEAR),
            date.get(Calendar.DAY_OF_YEAR), station, channel, 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(//from   ww w  .  ja v a  2 s .c om
                "== plotPSD: request to output plot=[%s] but we are unable to create it " + " --> skip plot\n",
                pngName);
        return;
    }

    Boolean plotNHNM = false;
    //if (nhnm.length > 0) {
    if (nhnm != null) {
        plotNHNM = true;
    }

    final XYSeries series1 = new XYSeries(modelName);
    final XYSeries series2 = new XYSeries(channel.toString());
    final XYSeries series3 = new XYSeries("NHNM");

    for (int k = 0; k < per.length; k++) {
        series1.add(per[k], model[k]);
        series2.add(per[k], psd[k]);
    }

    if (plotNHNM) {
        for (int k = 0; k < nhnmPer.length; k++) {
            series3.add(nhnmPer[k], nhnm[k]);
        }
    }

    //final XYItemRenderer renderer = new StandardXYItemRenderer();
    final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
    Rectangle rectangle = new Rectangle(3, 3);

    renderer.setSeriesShape(0, rectangle);
    renderer.setSeriesShapesVisible(0, false);
    renderer.setSeriesLinesVisible(0, true);

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

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

    Paint[] paints = new Paint[] { Color.blue, Color.red, Color.black };
    renderer.setSeriesPaint(0, paints[0]);
    renderer.setSeriesPaint(1, paints[1]);
    renderer.setSeriesPaint(2, paints[2]);

    final NumberAxis rangeAxis1 = new NumberAxis("PSD 10log10(m**2/s**4)/Hz dB");
    //rangeAxis1.setRange( new Range(-190, -120));
    rangeAxis1.setRange(new Range(-190, -95));
    rangeAxis1.setTickUnit(new NumberTickUnit(5.0));

    final LogarithmicAxis horizontalAxis = new LogarithmicAxis("Period (sec)");
    horizontalAxis.setRange(new Range(0.05, 10000));

    final XYSeriesCollection seriesCollection = new XYSeriesCollection();
    seriesCollection.addSeries(series1);
    seriesCollection.addSeries(series2);

    if (plotNHNM) {
        seriesCollection.addSeries(series3);
    }

    final XYPlot xyplot = new XYPlot((XYDataset) seriesCollection, horizontalAxis, rangeAxis1, renderer);

    xyplot.setDomainGridlinesVisible(true);
    xyplot.setRangeGridlinesVisible(true);
    xyplot.setRangeGridlinePaint(Color.black);
    xyplot.setDomainGridlinePaint(Color.black);

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

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

    }
}

From source file:org.metacsp.utility.UI.PlotBoxTLSmall.java

/**
 * Creates a chart for the PlotBoxBehavior
 * // w ww  .java 2  s.  c  o  m
 * @param dataset  A dataset for the chart.
 * 
 * @return A chart where the PlotBoxBehavior will be plotted.
 */
@SuppressWarnings("deprecation")
private JFreeChart createChart(CategoryDataset dataset) {

    //      String s = name;
    String s = null;
    String tit = null;
    String ax = null;
    //      if (first)
    //         tit = title + " (EST)";
    //      else if (last)
    //         ax = "Time";

    tit = this.name;

    chart = ChartFactory.createStackedBarChart(tit, // chart title
            s, // domain axis label
            ax, // range axis label
            dataset, // data
            PlotOrientation.HORIZONTAL, // the plot orientation
            false, // legend
            false, // tooltips
            false // urls
    );
    CategoryPlot plot = chart.getCategoryPlot();

    chart.getTitle().setHorizontalAlignment(HorizontalAlignment.LEFT);
    plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);

    //plot.getCategories();
    //CategoryItemRenderer renderer = plot.getRenderer();
    StackedBarRenderer renderer = (StackedBarRenderer) plot.getRenderer();
    renderer.setItemLabelsVisible(true);
    renderer.setItemLabelGenerator(new LabelGenerator(true));
    ItemLabelPosition pos = new ItemLabelPosition(ItemLabelAnchor.INSIDE1, TextAnchor.TOP_RIGHT);
    renderer.setPositiveItemLabelPositionFallback(pos);
    for (int i = 0; i < dataset.getRowCount(); i++) {
        renderer.setSeriesPositiveItemLabelPosition(i, pos);
    }

    /*
    if (values.elementAt(0) instanceof ResourceLevel) {
       renderer.setItemLabelGenerator( new PlotBoxTL.LabelGenerator(true));
    }
    else
       renderer.setItemLabelGenerator( new PlotBoxTL.LabelGenerator(false));
    */
    renderer.setToolTipGenerator(new PlotBoxTooltip());
    plot.setRenderer(renderer);
    // renderer.getSeriesStroke(0).
    plot.setForegroundAlpha(0.8f);

    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    rangeAxis.setLowerMargin(2.0);
    rangeAxis.setUpperMargin(2.0);

    //long origin = stl.getSerializableSimpleTimeline().getEarliestStartTime();
    //long horizon = stl.getSerializableSimpleTimeline().getLatestEndTime();
    long origin = stl.getPulses()[0].longValue();
    NumberFormat nf = new DecimalFormat();
    rangeAxis.setNumberFormatOverride(nf);
    if (this.range != null)
        rangeAxis.setRange(range);
    //rangeAxis.setRange((new Double(origin)).doubleValue(), (new Double(horizon)).doubleValue());

    ///// 0 should be replaced by the start of the horizon
    renderer.setBase(origin);

    //renderer.setBase();

    for (int i = 0; i < durations.length; i++) {
        if (stl.isInconsistent(values[i]))
            renderer.setSeriesPaint(i, new Color(198, 30, 69));
        else if (stl.isCritical(values[i]))
            renderer.setSeriesPaint(i, new Color(238, 234, 111));
        else if (stl.isUndetermined(values[i]))
            renderer.setSeriesPaint(i, new Color(255, 255, 255));
        else
            renderer.setSeriesPaint(i, new Color(111, 180, 238));
        renderer.setSeriesOutlinePaint(i, Color.black);
    }

    renderer.setBaseSeriesVisibleInLegend(false, false);

    renderer.setSeriesStroke(0, new BasicStroke(40f));

    return chart;
}