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

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

Introduction

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

Prototype

public void setRangeAxis(int index, ValueAxis axis, boolean notify) 

Source Link

Document

Sets a range axis and, if requested, sends a PlotChangeEvent to all registered listeners.

Usage

From source file:ChartUsingJava.CombinedXYPlot.java

/**
 * Sets the range axis that is shared by all the subplots.
 *
 * @param axis  the axis.//from   ww w.  ja va2 s  . c  om
 */
public void setRangeAxis(ValueAxis axis) {
    Iterator l_itr = getSubplots().iterator();
    while (l_itr.hasNext()) {
        XYPlot l_subplot = (XYPlot) l_itr.next();
        l_subplot.setRangeAxis(0, axis, false);
    }

    super.setRangeAxis(axis);
    if (null == axis) {
        return;
    }

    axis.configure();
}

From source file:ChartUsingJava.CombinedXYPlot.java

/**
 * Adds a new subplot with the specified weight.
 *
 * @param subplot  the subplot.//w  ww .j a va2 s. com
 * @param weight  the weight for the subplot.
 */
public void add(XYPlot subplot, int weight) {
    super.add(subplot, weight);

    ValueAxis l_range = super.getRangeAxis();
    subplot.setRangeAxis(0, l_range, false);

    super.setRangeAxis(l_range);
    if (null == l_range) {
        return;
    }

    l_range.configure();
}

From source file:ucar.unidata.idv.control.chart.ChartHolder.java

/**
 * add data set/*ww w  .  ja va  2 s.  c  o  m*/
 *
 * @param dataset dataset
 * @param rangeAxis axis
 * @param renderer renderer
 * @param side which side
 */
protected void add(XYDataset dataset, ValueAxis rangeAxis, XYItemRenderer renderer, AxisLocation side) {
    synchronized (chartManager.getMutex()) {
        XYPlot xyPlot = (XYPlot) plot;
        xyPlot.setRangeAxis(paramCount, rangeAxis, false);
        xyPlot.setDataset(paramCount, dataset);
        xyPlot.mapDatasetToRangeAxis(paramCount, paramCount);
        xyPlot.setRenderer(paramCount, renderer);
        if (side != null) {
            xyPlot.setRangeAxisLocation(paramCount, side);
        }
        paramCount++;
    }
}

From source file:ucar.unidata.idv.control.chart.VerticalProfileChart.java

/**
 * Initialize the plot// w w w  .ja  va  2 s . c  o  m
 *
 * @param plot the plot to initialize
 */
protected void initPlot(Plot plot) {
    XYPlot xyPlot = (XYPlot) plot;
    xyPlot.setOrientation(PlotOrientation.HORIZONTAL);

    int count = xyPlot.getDatasetCount();
    for (int i = 0; i < count; i++) {
        xyPlot.setDataset(i, null);
        xyPlot.setRenderer(i, null);
    }
    xyPlot.clearRangeAxes();
    XYSeriesCollection dummyDataset = new XYSeriesCollection();
    //ValueAxis          rangeAxis    = new FixedWidthNumberAxis();
    ValueAxis rangeAxis = new NumberAxis();
    xyPlot.setRangeAxis(0, rangeAxis, false);
    xyPlot.setDataset(0, dummyDataset);
    xyPlot.mapDatasetToRangeAxis(0, 0);
    xyPlot.setRenderer(0, new XYLineAndShapeRenderer());
}

From source file:ucar.unidata.idv.flythrough.ChartDecorator.java

/**
 * _more_//from   www .j a va2s  .  com
 *
 * @param g2 _more_
 * @param comp _more_
 *
 * @return _more_
 */
public boolean paintDashboard(Graphics2D g2, JComponent comp) {
    try {
        List<SampleInfo> infos = new ArrayList<SampleInfo>(sampleInfos);
        if (infos.size() == 0) {
            return false;
        }
        Rectangle b = comp.getBounds();
        JFrame dummyFrame = new JFrame("");
        XYSeriesCollection dataset = new XYSeriesCollection();
        JFreeChart chart = Flythrough.createChart(dataset);
        XYPlot xyPlot = (XYPlot) chart.getPlot();

        int chartHeight = b.height - flythrough.getDashboardImage().getHeight(null);
        chartHeight = Math.max(chartHeight, 50);
        int chartWidth = Math.min(chartHeight * 4, b.width);

        int dx = b.width / 2 - chartWidth / 2;
        int dy = 0;

        Image lastImage = lastChartImage;
        if ((lastImage != null) && (lastImage.getWidth(null) == chartWidth)
                && (lastImage.getHeight(null) == chartHeight)) {
            g2.translate(dx, dy);
            g2.drawImage(lastImage, 0, 0, null);
            g2.translate(-dx, -dy);
            return false;
        }

        for (int i = 0; i < infos.size(); i++) {
            SampleInfo info = infos.get(i);
            ValueAxis rangeAxis = new NumberAxis(info.getName());
            if (info.getRange() != null) {
                rangeAxis
                        .setRange(new org.jfree.data.Range(info.getRange().getMin(), info.getRange().getMax()));
            }
            dataset = new XYSeriesCollection();
            dataset.addSeries(info.getSeries());
            xyPlot.setRangeAxis(i, rangeAxis, false);
            xyPlot.setDataset(i, dataset);
            xyPlot.mapDatasetToRangeAxis(i, i);
            final Color color = COLORS[i % COLORS.length];
            XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true, false) {
                public Paint xgetItemPaint(final int row, final int column) {
                    return color;
                }
            };
            renderer.setSeriesPaint(0, color);
            xyPlot.setRenderer(i, renderer);
        }

        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(chartWidth, chartHeight));
        dummyFrame.setContentPane(chartPanel);
        dummyFrame.pack();
        Image image = ImageUtils.getImage(chartPanel);
        lastChartImage = image;
        g2.translate(dx, dy);
        g2.drawImage(image, 0, 0, null);
        g2.translate(-dx, -dy);
    } catch (Exception exc) {
        logException("Painting chart", exc);

    }

    return false;

}

From source file:ucar.unidata.idv.control.chart.TimeSeriesChart.java

/**
 * init plot/*from  w w w .j  av  a2s  .co m*/
 *
 * @param plot plot
 */
protected void initPlot(Plot plot) {
    XYPlot xyPlot = (XYPlot) plot;
    int count = xyPlot.getDatasetCount();
    for (int i = 0; i < count; i++) {
        xyPlot.setDataset(i, null);
        xyPlot.setRenderer(i, null);
    }
    xyPlot.clearRangeAxes();
    XYDataset dummyDataset = getDummyDataset();
    ValueAxis rangeAxis = new FixedWidthNumberAxis();
    xyPlot.setRangeAxis(0, rangeAxis, false);
    xyPlot.setDataset(0, dummyDataset);
    xyPlot.mapDatasetToRangeAxis(0, 0);
    xyPlot.setRenderer(0, new XYLineAndShapeRenderer());
}

From source file:org.ramadda.geodata.cdmdata.CdmDataOutputHandler.java

/**
 * Create the chart/* ww  w  .ja  v a2  s .  c o m*/
 *
 *
 * @param request  the request
 * @param entry    the entry
 * @param dataset  the dataset
 *
 * @return the chart
 */
private static JFreeChart createChart(Request request, Entry entry, XYDataset dataset) {
    LatLonPointImpl llp = new LatLonPointImpl(request.getLatOrLonValue(ARG_LOCATION + ".latitude", 0),
            request.getLatOrLonValue(ARG_LOCATION + ".longitude", 0));
    String title = entry.getName() + " at " + llp.toString();

    JFreeChart chart = ChartFactory.createTimeSeriesChart(
            //entry.getName(),  // title
            title, // title
            "Date", // x-axis label
            "", // y-axis label
            dataset, // data
            true, // create legend?
            true, // generate tooltips?
            false // generate URLs?
    );

    chart.setBackgroundPaint(Color.white);
    ValueAxis rangeAxis = new NumberAxis("");
    rangeAxis.setVisible(false);
    XYPlot plot = (XYPlot) chart.getPlot();
    if (request.get("gray", false)) {
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);
    } else {
        plot.setBackgroundPaint(Color.white);
        plot.setDomainGridlinePaint(Color.lightGray);
        plot.setRangeGridlinePaint(Color.lightGray);
    }
    plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
    plot.setDomainCrosshairVisible(true);
    plot.setRangeCrosshairVisible(true);
    plot.setRangeAxis(0, rangeAxis, false);

    XYItemRenderer r = plot.getRenderer();
    DateAxis axis = (DateAxis) plot.getDomainAxis();
    //axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));

    return chart;

}

From source file:OAT.ui.BarChartFrame.java

public void addDataset(ChartDataset dataset) {
    if (dataset == null || dataset.getSeriesCount() == 0) {
        return;/*from w  w w  .  java2s .c  om*/
    }

    XYPlot plot = getChart().getXYPlot();
    int i = plot.getDatasetCount();

    for (int j = 0; j < i; j++) {
        if (plot.getDataset(j).equals(dataset)) {
            //                System.out.println("eq " + i
            //                        + " " + ((ChartDataset) plot.getDataset(j)).getTitle()
            //                        + " " + dataset.getTitle());
            return;
        }
    }

    plot.setDataset(i, dataset);
    plot.setDatasetRenderingOrder(DatasetRenderingOrder.REVERSE);
    Double[] range = dataset.getAxisRange();

    //axis
    int axisId = 0;

    if (range != null) {
        //        if (range == null || range.length < 2) {
        //            plot.mapDatasetToRangeAxis(i, 0);
        //        } else {

        //scan for equal axis range, reuse if found
        boolean hasSameRange = false;

        if (range.length > 1) {
            for (int j = 1; j < plot.getRangeAxisCount(); j++) {
                Range otherRange = plot.getRangeAxis(j).getRange();

                if (otherRange != null && otherRange.getLowerBound() == range[0]
                        && otherRange.getUpperBound() == range[1]) {
                    axisId = j;
                    hasSameRange = true;
                    break;
                }
            }
        }

        if (!hasSameRange) {
            NumberAxis newAxis = new NumberAxis();

            if (range.length > 1) {
                newAxis.setAutoRange(false);
                newAxis.setRange(range[0], range[1]);
            }

            if (range.length > 2) {
                newAxis.setAutoTickUnitSelection(false, false);
                newAxis.setTickUnit(new NumberTickUnit(range[2]));
            }

            newAxis.setNumberFormatOverride(TextUtil.SIMPLE_FORMATTER);
            //                    newAxis.setAxisLinePaint(new Color(100, 0, 0));
            //                    newAxis.setLabelPaint(paints[i][0]);
            //                    newAxis.setTickLabelPaint(paints[i][0]);
            //                    newAxis.setTickMarkPaint(paints[i][0]);
            //                    newAxis.setTickLabelsVisible(true);

            axisId = plot.getRangeAxisCount();
            plot.setRangeAxis(axisId, newAxis, false);
            plot.setRangeAxisLocation(axisId, AxisLocation.BOTTOM_OR_LEFT, false);
        }
        //            plot.mapDatasetToRangeAxis(i, newAxisId);
    }
    plot.mapDatasetToRangeAxis(i, axisId);
    //

    //renderer
    XYLineAndShapeRenderer renderer;

    if (dataset instanceof TradeDataset) {
        renderer = new TradeRenderer();

        for (int j = 0; j < dataset.getSeriesCount(); j++) {
            renderer.setSeriesLinesVisible(j, false);
        }
    } else {

        Shape shape = Main.defaultShape;
        Paint[][] seriesPaints;
        Stroke stroke;

        if (dataset.getSource() instanceof Stopper && !(dataset.getSource() instanceof Calculator)) {
            seriesPaints = Main.greyPaints;
            stroke = Main.dottedStoke;
        } else {
            seriesPaints = Main.defaultPaints;
            stroke = Main.defaultStoke;
        }

        renderer = new IndicatorRenderer(seriesPaints[(i - 1) % seriesPaints.length], shape, stroke);
    }

    plot.setRenderer(i, renderer, false);
}

From source file:org.ramadda.geodata.cdmdata.PointDatabaseTypeHandler.java

/**
 * _more_// w w w.  j a  v  a2 s.  c  om
 *
 *
 * @param request _more_
 * @param entry _more_
 * @param dataset _more_
 *
 * @return _more_
 */
private static JFreeChart createChart(Request request, Entry entry, XYDataset dataset) {
    JFreeChart chart = ChartFactory.createTimeSeriesChart(
            request.getString(ARG_POINT_TIMESERIES_TITLE, entry.getName()), // title
            "Date", // x-axis label
            "", // y-axis label
            dataset, // data
            true, // create legend?
            true, // generate tooltips?
            false // generate URLs?
    );

    chart.setBackgroundPaint(Color.white);
    ValueAxis rangeAxis = new NumberAxis("");
    rangeAxis.setVisible(false);
    XYPlot plot = (XYPlot) chart.getPlot();
    if (request.get("gray", false)) {
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);
    } else {
        plot.setBackgroundPaint(Color.white);
        plot.setDomainGridlinePaint(Color.lightGray);
        plot.setRangeGridlinePaint(Color.lightGray);
    }
    plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
    plot.setDomainCrosshairVisible(true);
    plot.setRangeCrosshairVisible(true);
    plot.setRangeAxis(0, rangeAxis, false);

    XYItemRenderer r = plot.getRenderer();
    DateAxis axis = (DateAxis) plot.getDomainAxis();
    axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));

    return chart;

}

From source file:org.ramadda.geodata.cdmdata.GridPointOutputHandler.java

/**
 * Output the timeseries image/*from w  ww.j  a v a2  s  .  c  om*/
 *
 * @param request the request
 * @param entry  the entry
 * @param f  the file
 *
 * @return  the image
 *
 * @throws Exception  problem creating image
 */
private Result outputTimeSeriesImage(Request request, Entry entry, File f) throws Exception {

    StringBuffer sb = new StringBuffer();
    //sb.append(getHeader(request, entry));
    sb.append(header(msg("Chart")));

    TimeSeriesCollection dummy = new TimeSeriesCollection();
    JFreeChart chart = createChart(request, entry, dummy);
    XYPlot xyPlot = (XYPlot) chart.getPlot();

    Hashtable<String, MyTimeSeries> seriesMap = new Hashtable<String, MyTimeSeries>();
    List<MyTimeSeries> allSeries = new ArrayList<MyTimeSeries>();
    int paramCount = 0;
    int colorCount = 0;
    boolean axisLeft = true;
    Hashtable<String, List<ValueAxis>> axisMap = new Hashtable<String, List<ValueAxis>>();
    Hashtable<String, double[]> rangeMap = new Hashtable<String, double[]>();
    List<String> units = new ArrayList<String>();
    List<String> paramUnits = new ArrayList<String>();
    List<String> paramNames = new ArrayList<String>();

    long t1 = System.currentTimeMillis();
    String contents = IOUtil.readContents(getStorageManager().getFileInputStream(f));
    List<String> lines = StringUtil.split(contents, "\n", true, true);
    String header = lines.get(0);
    String[] headerToks = header.split(",");
    for (int i = 0; i < headerToks.length; i++) {
        paramNames.add(getParamName(headerToks[i]));
        paramUnits.add(getUnitFromName(headerToks[i]));
    }
    boolean hasLevel = paramNames.get(3).equals("vertCoord");

    boolean readHeader = false;
    for (String line : lines) {
        if (!readHeader) {
            readHeader = true;

            continue;
        }
        String[] lineTokes = line.split(",");
        Date date = DateUtil.parse(lineTokes[0]);
        int startIdx = hasLevel ? 4 : 3;
        for (int i = startIdx; i < lineTokes.length; i++) {
            double value = Double.parseDouble(lineTokes[i]);
            if (value != value) {
                continue;
            }
            List<ValueAxis> axises = null;
            double[] range = null;
            String u = paramUnits.get(i);
            String paramName = paramNames.get(i);
            String formatName = paramName.replaceAll("_", " ");
            String formatUnit = ((u == null) || (u.length() == 0)) ? "" : "[" + u + "]";
            if (u != null) {
                axises = axisMap.get(u);
                range = rangeMap.get(u);
                if (axises == null) {
                    axises = new ArrayList<ValueAxis>();
                    range = new double[] { value, value };
                    rangeMap.put(u, range);
                    axisMap.put(u, axises);
                    units.add(u);
                }
                range[0] = Math.min(range[0], value);
                range[1] = Math.max(range[1], value);
            }
            MyTimeSeries series = seriesMap.get(paramName);
            if (series == null) {
                paramCount++;
                TimeSeriesCollection dataset = new TimeSeriesCollection();
                series = new MyTimeSeries(formatName, FixedMillisecond.class);
                allSeries.add(series);
                ValueAxis rangeAxis = new NumberAxis(formatName + " " + formatUnit);
                if (axises != null) {
                    axises.add(rangeAxis);
                }
                XYItemRenderer renderer = new XYAreaRenderer(XYAreaRenderer.LINES);
                if (colorCount >= HtmlUtils.COLORS.length) {
                    colorCount = 0;
                }
                renderer.setSeriesPaint(0, HtmlUtils.COLORS[colorCount]);
                colorCount++;
                xyPlot.setRenderer(paramCount, renderer);
                xyPlot.setRangeAxis(paramCount, rangeAxis, false);
                AxisLocation side = (axisLeft ? AxisLocation.TOP_OR_LEFT : AxisLocation.BOTTOM_OR_RIGHT);
                axisLeft = !axisLeft;
                xyPlot.setRangeAxisLocation(paramCount, side);

                dataset.setDomainIsPointsInTime(true);
                dataset.addSeries(series);
                seriesMap.put(paramNames.get(i), series);
                xyPlot.setDataset(paramCount, dataset);
                xyPlot.mapDatasetToRangeAxis(paramCount, paramCount);
            }
            //series.addOrUpdate(new FixedMillisecond(pointData.date),value);
            TimeSeriesDataItem item = new TimeSeriesDataItem(new FixedMillisecond(date), value);
            series.addItem(item);
        }
    }

    for (MyTimeSeries timeSeries : allSeries) {
        timeSeries.finish();
    }

    for (String unit : units) {
        List<ValueAxis> axises = axisMap.get(unit);
        double[] range = rangeMap.get(unit);
        for (ValueAxis rangeAxis : axises) {
            rangeAxis.setRange(new org.jfree.data.Range(range[0], range[1]));
        }
    }

    long t2 = System.currentTimeMillis();

    BufferedImage newImage = chart.createBufferedImage(request.get(ARG_IMAGE_WIDTH, 1000),
            request.get(ARG_IMAGE_HEIGHT, 400));
    long t3 = System.currentTimeMillis();
    //System.err.println("timeseries image time:" + (t2 - t1) + " "
    //                   + (t3 - t2));

    File file = getStorageManager().getTmpFile(request, "point.png");
    ImageUtils.writeImageToFile(newImage, file);
    InputStream is = getStorageManager().getFileInputStream(file);
    Result result = new Result("", is, "image/png");

    return result;

}