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

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

Introduction

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

Prototype

public void setRange(double lower, double upper) 

Source Link

Document

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

Usage

From source file:com.wattzap.view.graphs.MMPGraph.java

public MMPGraph(XYSeries series) {
    super();/*  ww w  . j a  v a2 s.  c om*/

    NumberAxis yAxis = new NumberAxis(userPrefs.messages.getString("poWtt"));
    yAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    double maxY = series.getMaxY();
    yAxis.setRange(0, maxY + 20);
    yAxis.setTickLabelPaint(Color.white);
    yAxis.setLabelPaint(Color.white);

    LogAxis xAxis = new LogAxis(userPrefs.messages.getString("time"));
    xAxis.setTickLabelPaint(Color.white);
    xAxis.setBase(4);
    xAxis.setAutoRange(false);

    xAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

    xAxis.setRange(1, series.getMaxX() + 500);
    xAxis.setNumberFormatOverride(new NumberFormat() {

        @Override
        public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) {

            long millis = (long) number * 1000;

            if (millis >= 60000) {
                return new StringBuffer(String.format("%d m %d s",
                        TimeUnit.MILLISECONDS.toMinutes((long) millis),
                        TimeUnit.MILLISECONDS.toSeconds((long) millis)
                                - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) millis))));
            } else {
                return new StringBuffer(String.format("%d s",

                        TimeUnit.MILLISECONDS.toSeconds((long) millis)
                                - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) millis))));
            }
        }

        @Override
        public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos) {
            return new StringBuffer(String.format("%s", number));
        }

        @Override
        public Number parse(String source, ParsePosition parsePosition) {
            return null;
        }
    });

    XYPlot plot = new XYPlot(new XYSeriesCollection(series), xAxis, yAxis,
            new XYLineAndShapeRenderer(true, false));

    JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, false);

    chart.setBackgroundPaint(Color.gray);
    plot = chart.getXYPlot();
    plot.setBackgroundPaint(Color.darkGray);
    /*plot.setDomainGridlinePaint(Color.lightGray);
    plot.setRangeGridlinePaint(Color.lightGray);*/

    ValueAxis domainAxis = plot.getDomainAxis();
    domainAxis.setTickLabelPaint(Color.white);
    domainAxis.setLabelPaint(Color.white);

    chartPanel = new ChartPanel(chart);
    chartPanel.setSize(100, 800);
    chartPanel.setFillZoomRectangle(true);
    chartPanel.setMouseWheelEnabled(true);
    chartPanel.setBackground(Color.gray);

    setLayout(new BorderLayout());
    add(chartPanel, BorderLayout.CENTER);
    setBackground(Color.black);
    chartPanel.revalidate();
    setVisible(true);
}

From source file:netplot.GenericPlotPanel.java

void genericConfig(JFreeChart chart, XYPlot plot, int plotIndex) {
    if (!enableLegend) {
        chart.removeLegend();//from www. j ava  2 s  . c o m
    }

    XYItemRenderer xyItemRenderer = plot.getRenderer();
    //May also be XYBarRenderer
    if (xyItemRenderer instanceof XYLineAndShapeRenderer) {
        XYToolTipGenerator xyToolTipGenerator = xyItemRenderer.getBaseToolTipGenerator();
        //If currently an XYLineAndShapeRenderer replace it so that we inc the colour for every plotIndex
        XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(linesEnabled, shapesEnabled);
        //Ensure we don't loose the tool tips on the new renderer
        renderer.setBaseToolTipGenerator(xyToolTipGenerator);
        renderer.setBasePaint(getPlotColour(plotIndex));
        renderer.setSeriesStroke(0, new BasicStroke(lineWidth, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL),
                true);
        plot.setRenderer(plotIndex, renderer);
    }

    //If we have a new y axis then we need a new data set
    if (yAxisName != null && yAxisName.length() > 0) {
        if (logYAxis) {
            LogAxis yAxis = new LogAxis(yAxisName);
            yAxis.setAutoRange(false);
            yAxis.setNumberFormatOverride(new LogFormat(10, "10", true));
            yAxis.setRange(minScaleValue, maxScaleValue);
            yAxis.setLowerBound(minScaleValue);
            yAxis.setUpperBound(maxScaleValue);
            plot.setRangeAxis(yAxisIndex, yAxis);
            plot.setRangeAxisLocation(yAxisIndex, AxisLocation.BOTTOM_OR_LEFT);
        } else {
            NumberAxis axis = new NumberAxis(yAxisName);
            axis.setAutoRangeIncludesZero(zeroOnYScale);
            if (autoScaleEnabled) {
                axis.setAutoRange(true);
            } else {
                Range range = new Range(minScaleValue, maxScaleValue);
                axis.setRangeWithMargins(range, true, true);
            }
            if (yAxisTickCount > 0) {
                NumberTickUnit tick = new NumberTickUnit(yAxisTickCount);
                axis.setTickUnit(tick);
            }
            plot.setRangeAxis(yAxisIndex, axis);
            plot.setRangeAxisLocation(yAxisIndex, AxisLocation.BOTTOM_OR_LEFT);
        }
        yAxisIndex++;
    }
    plot.mapDatasetToRangeAxis(plotIndex, yAxisIndex - 1);
    ValueAxis a = plot.getDomainAxis();
    if (xAxisName.length() > 0) {
        a.setLabel(xAxisName);
    }
    //We can enable/disable zero on the axis if we have a NumberAxis
    if (a instanceof NumberAxis) {
        ((NumberAxis) a).setAutoRangeIncludesZero(zeroOnXScale);
    }
}

From source file:edu.fullerton.viewerplugin.XYPlotter.java

private ChartPanel getPanel(double[][] data) throws WebUtilException {
    ChartPanel ret = null;/*  w  w w.  j a  va 2 s  .c  o  m*/
    try {
        XYSeries xys;
        XYSeriesCollection mtds = new XYSeriesCollection();
        String mylegend = legend == null || legend.isEmpty() ? "series 1" : legend;

        xys = new XYSeries(legend);

        int len = data.length;
        double minx = Double.MAX_VALUE;
        double maxx = Double.MIN_VALUE;
        double miny = Double.MAX_VALUE;
        double maxy = Double.MIN_VALUE;
        boolean gotZeroX = false;
        boolean gotZeroY = false;

        for (int i = 0; i < len; i++) {
            double x = data[i][0];
            double y = data[i][1];
            if (x == 0) {
                gotZeroX = true;
            } else {
                minx = Math.min(minx, x);
                maxx = Math.max(maxx, x);
            }
            if (y == 0) {
                gotZeroY = true;
            } else {
                miny = Math.min(miny, y);
                maxy = Math.max(maxy, y);
            }
        }
        // this kludge lets us plot a 0 on a log axis
        double fakeZeroX = 0.;
        double fakeZeroY = 0.;
        if (gotZeroX) {
            if (logXaxis) {
                fakeZeroX = minx / 10;
            } else {
                minx = Math.min(0, minx);
                maxx = Math.max(0, maxx);
            }
        }
        if (gotZeroY) {
            if (logYaxis) {
                fakeZeroY = miny / 10;
            } else {
                miny = Math.min(0, miny);
                maxy = Math.max(0, maxy);
            }
        }
        for (int i = 0; i < len; i++) {
            double x = data[i][0];
            double y = data[i][1];
            x = x == 0 ? fakeZeroX : x;
            y = y == 0 ? fakeZeroY : y;
            xys.add(x, y);
        }
        mtds.addSeries(xys);

        DefaultXYDataset ds = new DefaultXYDataset();

        int exp;
        if (maxy == 0. && miny == 0.) {
            miny = -1.;
            exp = 0;
            logYaxis = false;
        } else {

            maxy = maxy > miny ? maxy : miny * 10;
            exp = PluginSupport.scaleRange(mtds, miny, maxy);
            if (!logYaxis && exp > 0) {
                yLabel += " x 1e-" + Integer.toString(exp);
            }
        }
        JFreeChart chart = ChartFactory.createXYLineChart(title, xLabel, yLabel, ds, PlotOrientation.VERTICAL,
                true, false, false);
        org.jfree.chart.plot.XYPlot plot = (org.jfree.chart.plot.XYPlot) chart.getPlot();
        if (logYaxis) {
            LogAxis rangeAxis = new LogAxis(yLabel);
            double smallest = miny * Math.pow(10, exp);
            rangeAxis.setSmallestValue(smallest);
            rangeAxis.setMinorTickCount(9);

            LogAxisNumberFormat lanf = new LogAxisNumberFormat();
            lanf.setExp(exp);
            rangeAxis.setNumberFormatOverride(lanf);
            rangeAxis.setRange(smallest, maxy * Math.pow(10, exp));
            plot.setRangeAxis(rangeAxis);
        }
        if (logXaxis) {
            LogAxis domainAxis = new LogAxis(xLabel);
            domainAxis.setMinorTickCount(9);
            domainAxis.setSmallestValue(minx);
            domainAxis.setNumberFormatOverride(new LogAxisNumberFormat());
            plot.setDomainAxis(domainAxis);
        }
        ValueAxis domainAxis = plot.getDomainAxis();
        if (fmin != null && fmin > 0) {
            domainAxis.setLowerBound(fmin);
        }
        if (fmax != null && fmax > 0) {
            domainAxis.setUpperBound(fmax);
        }
        plot.setDomainAxis(domainAxis);
        plot.setDataset(0, mtds);

        // Set the line thickness
        XYLineAndShapeRenderer r = (XYLineAndShapeRenderer) plot.getRenderer();
        BasicStroke str = new BasicStroke(lineThickness);
        int n = plot.getSeriesCount();
        for (int i = 0; i < n; i++) {
            r.setSeriesStroke(i, str);
        }

        if (legend == null || legend.isEmpty()) {
            chart.removeLegend();
        }
        ret = new ChartPanel(chart);
    } catch (Exception ex) {
        throw new WebUtilException("Creating spectrum plot" + ex.getLocalizedMessage());
    }
    return ret;

}

From source file:edu.fullerton.viewerplugin.SpectrumPlot.java

private ChartPanel getPanel(ArrayList<ChanDataBuffer> dbufs, boolean compact) throws WebUtilException {
    ChartPanel ret = null;/* ww w  . j a  v a2s. com*/
    try {
        float tfsMax = 0;
        for (ChanDataBuffer buf : dbufs) {
            ChanInfo ci = buf.getChanInfo();
            float fs = ci.getRate();
            tfsMax = Math.max(fs, tfsMax);
        }
        setFsMax(tfsMax);
        String gtitle = getTitle(dbufs, compact);
        int nbuf = dbufs.size();
        XYSeries[] xys = new XYSeries[nbuf];
        XYSeriesCollection mtds = new XYSeriesCollection();

        int cnum = 0;
        compact = dbufs.size() > 2 ? false : compact;
        float bw = 1.f;
        for (ChanDataBuffer dbuf : dbufs) {
            String legend = getLegend(dbuf, compact);

            xys[cnum] = new XYSeries(legend);

            bw = calcSpectrum(xys[cnum], dbuf);

            mtds.addSeries(xys[cnum]);
        }

        DefaultXYDataset ds = new DefaultXYDataset();
        String yLabel = pwrScale.toString();
        DecimalFormat dform = new DecimalFormat("0.0###");
        String xLabel;
        xLabel = String.format("Frequency Hz - (bw: %1$s, #fft: %2$,d, s/fft: %3$.2f, ov: %4$.2f)",
                dform.format(bw), nfft, secperfft, overlap);

        Double minx, miny, maxx, maxy;
        Double[] rng = new Double[4];
        if (fmin <= 0) {
            fmin = bw;
        }
        float searchFmax = fmax;
        if (fmax <= 0 || fmax == Float.MAX_VALUE) {
            fmax = tfsMax / 2;
            searchFmax = tfsMax / 2 * 0.8f;
        }
        PluginSupport.getRangeLimits(mtds, rng, 2, fmin, searchFmax);
        minx = rng[0];
        miny = rng[1];
        maxx = rng[2];
        maxy = rng[3];

        findSmallest(mtds);
        int exp;
        if (maxy == 0. && miny == 0.) {
            miny = -1.;
            exp = 0;
            logYaxis = false;
        } else {
            miny = miny > 0 ? miny : smallestY;
            maxy = maxy > 0 ? maxy : miny * 10;
            exp = PluginSupport.scaleRange(mtds, miny, maxy);
            if (!logYaxis) {
                yLabel += " x 1e-" + Integer.toString(exp);
            }
        }
        JFreeChart chart = ChartFactory.createXYLineChart(gtitle, xLabel, yLabel, ds, PlotOrientation.VERTICAL,
                true, false, false);
        XYPlot plot = (XYPlot) chart.getPlot();
        if (logYaxis) {
            LogAxis rangeAxis = new LogAxis(yLabel);
            double smallest = miny * Math.pow(10, exp);
            rangeAxis.setSmallestValue(smallest);
            rangeAxis.setMinorTickCount(9);

            LogAxisNumberFormat lanf = new LogAxisNumberFormat();
            lanf.setExp(exp);

            rangeAxis.setNumberFormatOverride(lanf);
            rangeAxis.setRange(smallest, maxy * Math.pow(10, exp));
            rangeAxis.setStandardTickUnits(LogAxis.createLogTickUnits(Locale.US));
            plot.setRangeAxis(rangeAxis);
            plot.setRangeGridlinesVisible(true);
            plot.setRangeGridlinePaint(Color.BLACK);
        }
        if (logXaxis) {
            LogAxis domainAxis = new LogAxis(xLabel);
            domainAxis.setBase(2);
            domainAxis.setMinorTickCount(9);
            domainAxis.setMinorTickMarksVisible(true);
            domainAxis.setSmallestValue(smallestX);
            domainAxis.setNumberFormatOverride(new LogAxisNumberFormat());
            //domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
            plot.setDomainAxis(domainAxis);
            plot.setDomainGridlinesVisible(true);
            plot.setDomainGridlinePaint(Color.BLACK);
        }
        ValueAxis domainAxis = plot.getDomainAxis();
        if (fmin > Float.MIN_VALUE) {
            domainAxis.setLowerBound(fmin);
        }
        if (fmax != Float.MAX_VALUE) {
            domainAxis.setUpperBound(fmax);
        }
        plot.setDomainAxis(domainAxis);
        plot.setDataset(0, mtds);
        plot.setDomainGridlinePaint(Color.DARK_GRAY);
        plot.setRangeGridlinePaint(Color.DARK_GRAY);

        // Set the line thickness
        XYLineAndShapeRenderer r = (XYLineAndShapeRenderer) plot.getRenderer();
        BasicStroke str = new BasicStroke(lineThickness);
        int n = plot.getSeriesCount();
        for (int i = 0; i < n; i++) {
            r.setSeriesStroke(i, str);
        }
        plot.setBackgroundPaint(Color.WHITE);
        if (compact) {
            chart.removeLegend();
        }
        ret = new ChartPanel(chart);
    } catch (Exception ex) {
        throw new WebUtilException("Creating spectrum plot" + ex.getLocalizedMessage());
    }
    return ret;

}