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:org.ramadda.geodata.cdmdata.PointDatabaseTypeHandler.java

/**
 * _more_//from w w  w. j  ava2  s.  c  om
 *
 * @param request _more_
 * @param entry _more_
 * @param columnsToUse _more_
 * @param list _more_
 *
 * @return _more_
 *
 * @throws Exception _more_
 */
private Result makeSearchResultsTimeSeries(Request request, Entry entry, List<PointDataMetadata> columnsToUse,
        List<PointData> list) throws Exception {

    StringBuffer sb = new StringBuffer();
    sb.append(getHeader(request, entry));
    sb.append(header(msg("Point Data Search Results")));
    if (list.size() == 0) {
        sb.append(msg("No results found"));

        return new Result("Point Search Results", sb);
    }

    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>();

    long t1 = System.currentTimeMillis();

    for (PointData pointData : list) {
        for (PointDataMetadata pdm : columnsToUse) {
            if (!pdm.isNumeric()) {
                continue;
            }
            double value = ((Double) pointData.getValue(pdm)).doubleValue();
            if (value != value) {
                continue;
            }
            List<ValueAxis> axises = null;
            double[] range = null;
            if (pdm.hasUnit()) {
                axises = axisMap.get(pdm.unit);
                range = rangeMap.get(pdm.unit);
                if (axises == null) {
                    axises = new ArrayList<ValueAxis>();
                    range = new double[] { value, value };
                    rangeMap.put(pdm.unit, range);
                    axisMap.put(pdm.unit, axises);
                    units.add(pdm.unit);
                }
                range[0] = Math.min(range[0], value);
                range[1] = Math.max(range[1], value);
            }
            MyTimeSeries series = seriesMap.get(pdm.getColumnName());
            if (series == null) {
                paramCount++;
                TimeSeriesCollection dataset = new TimeSeriesCollection();
                series = new MyTimeSeries(pdm.formatName(), FixedMillisecond.class);
                allSeries.add(series);
                ValueAxis rangeAxis = new NumberAxis(pdm.formatName() + " " + pdm.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(pdm.getColumnName(), series);
                xyPlot.setDataset(paramCount, dataset);
                xyPlot.mapDatasetToRangeAxis(paramCount, paramCount);
            }
            //series.addOrUpdate(new FixedMillisecond(pointData.date),value);
            TimeSeriesDataItem item = new TimeSeriesDataItem(new FixedMillisecond(pointData.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_POINT_IMAGE_WIDTH, 1000),
            request.get(ARG_POINT_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;

}

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

/**
 * Output the timeseries image/* w ww. j a va 2s. 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 >= GuiUtils.COLORS.length) {
                    colorCount = 0;
                }
                renderer.setSeriesPaint(0, GuiUtils.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;

}