Search in sources :

Example 11 with XYTextAnnotation

use of org.jfree.chart.annotations.XYTextAnnotation in project ma-core-public by infiniteautomation.

the class ImageChartUtils method writeChart.

public static void writeChart(PointTimeSeriesCollection pointTimeSeriesCollection, boolean showLegend, OutputStream out, int width, int height, long from, long to) throws IOException {
    JFreeChart chart = ChartFactory.createTimeSeriesChart(null, null, null, null, showLegend, false, false);
    chart.setBackgroundPaint(SystemSettingsDao.getColour(SystemSettingsDao.CHART_BACKGROUND_COLOUR));
    XYPlot plot = chart.getXYPlot();
    ((DateAxis) plot.getDomainAxis()).setTimeZone(pointTimeSeriesCollection.getTimeZone());
    plot.setBackgroundPaint(SystemSettingsDao.getColour(SystemSettingsDao.PLOT_BACKGROUND_COLOUR));
    Color gridlines = SystemSettingsDao.getColour(SystemSettingsDao.PLOT_GRIDLINE_COLOUR);
    plot.setDomainGridlinePaint(gridlines);
    plot.setRangeGridlinePaint(gridlines);
    ((NumberAxis) plot.getRangeAxis()).setAutoRangeStickyZero(false);
    double numericMin = 0;
    double numericMax = 1;
    int numericSeriesCount = pointTimeSeriesCollection.getNumericSeriesCount();
    if (pointTimeSeriesCollection.hasNumericData()) {
        for (int i = 0; i < numericSeriesCount; i++) {
            NumericTimeSeries nts = pointTimeSeriesCollection.getNumericTimeSeries(i);
            AbstractXYItemRenderer renderer;
            if (nts.getPlotType() == DataPointVO.PlotTypes.STEP)
                renderer = new XYStepRenderer();
            else if (nts.getPlotType() == DataPointVO.PlotTypes.LINE)
                renderer = new XYLineAndShapeRenderer(true, false);
            else {
                // XYCardinalSplineRenderer spline = new XYCardinalSplineRenderer(.5d, 16);
                // XYSmoothLineAndShapeRenderer spline = new XYSmoothLineAndShapeRenderer();
                // XYSplineRenderer spline = new XYSplineRenderer();
                // spline.setBaseShapesVisible(false);
                // renderer = spline;
                renderer = new XYLineAndShapeRenderer(true, false);
            }
            if (nts.getPaint() != null)
                renderer.setSeriesPaint(0, nts.getPaint(), false);
            if (nts.getStroke() != null)
                renderer.setSeriesStroke(0, nts.getStroke(), false);
            plot.setDataset(i, new TimeSeriesCollection(nts.getTimeSeries()));
            plot.setRenderer(i, renderer);
        }
        numericMin = plot.getRangeAxis().getLowerBound();
        numericMax = plot.getRangeAxis().getUpperBound();
        if (!pointTimeSeriesCollection.hasMultiplePoints()) {
            // If this chart displays a single point, check if there should be a range description.
            TimeSeries timeSeries = pointTimeSeriesCollection.getNumericTimeSeries(0).getTimeSeries();
            String desc = timeSeries.getRangeDescription();
            if (!StringUtils.isBlank(desc)) {
                // Replace any HTML entities with Java equivalents
                desc = StripEntities.stripHTMLEntities(desc, ' ');
                plot.getRangeAxis().setLabel(desc);
            }
        }
    } else
        plot.getRangeAxis().setVisible(false);
    if (pointTimeSeriesCollection.getRangeMarkers() != null) {
        boolean rangeAdjusted = false;
        for (Marker marker : pointTimeSeriesCollection.getRangeMarkers()) {
            plot.addRangeMarker(marker);
            if (marker instanceof ValueMarker) {
                ValueMarker vm = (ValueMarker) marker;
                if (numericMin > vm.getValue()) {
                    numericMin = vm.getValue();
                    rangeAdjusted = true;
                }
                if (numericMax < vm.getValue()) {
                    numericMax = vm.getValue();
                    rangeAdjusted = true;
                }
            }
        }
        if (rangeAdjusted) {
            double adj = (numericMax - numericMin);
            plot.getRangeAxis().setLowerBound(numericMin - adj * plot.getRangeAxis().getLowerMargin());
            plot.getRangeAxis().setUpperBound(numericMax + adj * plot.getRangeAxis().getUpperMargin());
        }
    }
    int discreteValueCount = pointTimeSeriesCollection.getDiscreteValueCount();
    double interval = (numericMax - numericMin) / (discreteValueCount + 1);
    int intervalIndex = 1;
    if (pointTimeSeriesCollection.hasDiscreteData()) {
        for (int i = 0; i < pointTimeSeriesCollection.getDiscreteSeriesCount(); i++) {
            DiscreteTimeSeries dts = pointTimeSeriesCollection.getDiscreteTimeSeries(i);
            XYStepRenderer renderer = new XYStepRenderer();
            TimeSeries ts = new TimeSeries(dts.getName(), null, null);
            for (IValueTime vt : dts.getValueTimes()) addMillisecond(ts, vt.getTime(), numericMin + (interval * (dts.getValueIndex(vt.getValue()) + intervalIndex)));
            if (dts.getPaint() != null)
                renderer.setSeriesPaint(0, dts.getPaint(), false);
            if (dts.getStroke() != null)
                renderer.setSeriesStroke(0, dts.getStroke(), false);
            plot.setDataset(numericSeriesCount + i, new TimeSeriesCollection(ts, pointTimeSeriesCollection.getTimeZone()));
            plot.setRenderer(numericSeriesCount + i, renderer);
            intervalIndex += dts.getDiscreteValueCount();
        }
    }
    if (from > 0)
        plot.getDomainAxis().setLowerBound(from);
    if (to > 0)
        plot.getDomainAxis().setUpperBound(to);
    if (pointTimeSeriesCollection.hasDiscreteData()) {
        // Add the value annotations.
        double annoX = plot.getDomainAxis().getLowerBound();
        intervalIndex = 1;
        for (int i = 0; i < pointTimeSeriesCollection.getDiscreteSeriesCount(); i++) {
            DiscreteTimeSeries dts = pointTimeSeriesCollection.getDiscreteTimeSeries(i);
            for (int j = 0; j < dts.getDiscreteValueCount(); j++) {
                XYTextAnnotation anno = new XYTextAnnotation(" " + dts.getValueText(j), annoX, numericMin + (interval * (j + intervalIndex)));
                if (!pointTimeSeriesCollection.hasNumericData() && intervalIndex + j == discreteValueCount)
                    // This prevents the top label from getting cut off
                    anno.setTextAnchor(TextAnchor.TOP_LEFT);
                else
                    anno.setTextAnchor(TextAnchor.BOTTOM_LEFT);
                anno.setPaint(((AbstractRenderer) plot.getRenderer(numericSeriesCount + i)).lookupSeriesPaint(0));
                plot.addAnnotation(anno);
            }
            intervalIndex += dts.getDiscreteValueCount();
        }
    }
    // Return the image.
    ChartUtilities.writeChartAsPNG(out, chart, width, height);
}
Also used : DateAxis(org.jfree.chart.axis.DateAxis) NumberAxis(org.jfree.chart.axis.NumberAxis) TimeSeries(org.jfree.data.time.TimeSeries) Color(java.awt.Color) AbstractXYItemRenderer(org.jfree.chart.renderer.xy.AbstractXYItemRenderer) XYLineAndShapeRenderer(org.jfree.chart.renderer.xy.XYLineAndShapeRenderer) IValueTime(com.serotonin.m2m2.view.stats.IValueTime) ValueMarker(org.jfree.chart.plot.ValueMarker) Marker(org.jfree.chart.plot.Marker) JFreeChart(org.jfree.chart.JFreeChart) XYTextAnnotation(org.jfree.chart.annotations.XYTextAnnotation) XYPlot(org.jfree.chart.plot.XYPlot) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) XYStepRenderer(org.jfree.chart.renderer.xy.XYStepRenderer) ValueMarker(org.jfree.chart.plot.ValueMarker)

Aggregations

XYTextAnnotation (org.jfree.chart.annotations.XYTextAnnotation)11 NumberAxis (org.jfree.chart.axis.NumberAxis)5 JFreeChart (org.jfree.chart.JFreeChart)4 XYPlot (org.jfree.chart.plot.XYPlot)3 Test (org.junit.Test)3 Color (java.awt.Color)2 Font (java.awt.Font)2 IOException (java.io.IOException)2 ChartPanel (org.jfree.chart.ChartPanel)2 NumberTickUnit (org.jfree.chart.axis.NumberTickUnit)2 StandardXYItemLabelGenerator (org.jfree.chart.labels.StandardXYItemLabelGenerator)2 StandardXYToolTipGenerator (org.jfree.chart.labels.StandardXYToolTipGenerator)2 ValueMarker (org.jfree.chart.plot.ValueMarker)2 StandardXYItemRenderer (org.jfree.chart.renderer.xy.StandardXYItemRenderer)2 XYItemRenderer (org.jfree.chart.renderer.xy.XYItemRenderer)2 XYLineAndShapeRenderer (org.jfree.chart.renderer.xy.XYLineAndShapeRenderer)2 TimeSeries (org.jfree.data.time.TimeSeries)2 TimeSeriesCollection (org.jfree.data.time.TimeSeriesCollection)2 XYDataset (org.jfree.data.xy.XYDataset)2 XYSeries (org.jfree.data.xy.XYSeries)2