Search in sources :

Example 1 with IValueTime

use of com.serotonin.m2m2.view.stats.IValueTime in project ma-core-public by infiniteautomation.

the class AnalogStatisticsTest method testIntegral.

@Test
public void testIntegral() {
    long periodStart = 0;
    // 30 Days in ms
    long periodEnd = 30l * 24l * 60l * 60l * 1000l;
    AnalogStatistics s;
    List<PointValueTime> values;
    Double pointStartValue;
    long pointChangeTime;
    double pointChangeValue;
    Double expectedIntegral;
    // No values no start and end values either
    s = new AnalogStatistics(periodStart, periodEnd, (Double) null, new ArrayList<IValueTime>());
    assertEquals(new Double(0), s.getIntegral(), 0.001D);
    // Start with 0 but no other values
    s = new AnalogStatistics(periodStart, periodEnd, (Double) 0d, new ArrayList<IValueTime>());
    assertEquals(new Double(0), s.getIntegral(), 0.001D);
    // Start with 0, one change on day 25 at midnight and no end value
    values = new ArrayList<PointValueTime>();
    pointChangeTime = 25l * 24l * 60l * 60l * 1000l;
    pointChangeValue = 200d;
    values.add(new PointValueTime(pointChangeValue, pointChangeTime));
    s = new AnalogStatistics(periodStart, periodEnd, (Double) 0d, values);
    expectedIntegral = (pointChangeValue * (new Double(periodEnd) - new Double(pointChangeTime))) / 1000D;
    assertEquals(expectedIntegral, s.getIntegral(), 0.001D);
    // Start with 0, one change on day 25 at midnight and have end value of 0
    values = new ArrayList<PointValueTime>();
    pointChangeTime = 25l * 24l * 60l * 60l * 1000l;
    pointChangeValue = 200d;
    pointStartValue = 100d;
    values.add(new PointValueTime(pointChangeValue, pointChangeTime));
    s = new AnalogStatistics(periodStart, periodEnd, (Double) pointStartValue, values);
    expectedIntegral = ((new Double(pointChangeTime) - new Double(periodStart)) * new Double(pointStartValue)) / 1000D;
    expectedIntegral += (pointChangeValue * (new Double(periodEnd) - new Double(pointChangeTime))) / 1000D;
    assertEquals(expectedIntegral, s.getIntegral(), 0.001D);
}
Also used : PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 2 with IValueTime

use of com.serotonin.m2m2.view.stats.IValueTime in project ma-core-public by infiniteautomation.

the class DiscreteTimeSeries method addValueTime.

@SuppressWarnings("unchecked")
public void addValueTime(IValueTime pvt) {
    DataValue value = pvt.getValue();
    if (value == null)
        return;
    valueTimes.add(pvt);
    if (getValueIndex(value) == -1) {
        String text;
        if (textRenderer == null)
            text = value.toString();
        else
            text = textRenderer.getText(value, TextRenderer.HINT_FULL);
        ValueDescription vd = new ValueDescription((Comparable<Object>) value, text);
        int index = Collections.binarySearch(valueDescriptions, vd);
        valueDescriptions.add(-index - 1, vd);
    }
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) Paint(java.awt.Paint)

Example 3 with IValueTime

use of com.serotonin.m2m2.view.stats.IValueTime 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)

Example 4 with IValueTime

use of com.serotonin.m2m2.view.stats.IValueTime in project ma-core-public by infiniteautomation.

the class MultistateDataQuantizer method periodData.

@Override
protected void periodData(IValueTime vt) {
    int i = vt.getValue().getIntegerValue();
    if (startValue == null)
        startValue = vt;
    boolean found = false;
    for (IValueTime v : values) {
        if (v.getValue().getIntegerValue() == i) {
            found = true;
            break;
        }
    }
    if (!found)
        values.add(vt);
    endValue = vt;
}
Also used : IValueTime(com.serotonin.m2m2.view.stats.IValueTime)

Aggregations

IValueTime (com.serotonin.m2m2.view.stats.IValueTime)2 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)1 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)1 Color (java.awt.Color)1 Paint (java.awt.Paint)1 ArrayList (java.util.ArrayList)1 JFreeChart (org.jfree.chart.JFreeChart)1 XYTextAnnotation (org.jfree.chart.annotations.XYTextAnnotation)1 DateAxis (org.jfree.chart.axis.DateAxis)1 NumberAxis (org.jfree.chart.axis.NumberAxis)1 Marker (org.jfree.chart.plot.Marker)1 ValueMarker (org.jfree.chart.plot.ValueMarker)1 XYPlot (org.jfree.chart.plot.XYPlot)1 AbstractXYItemRenderer (org.jfree.chart.renderer.xy.AbstractXYItemRenderer)1 XYLineAndShapeRenderer (org.jfree.chart.renderer.xy.XYLineAndShapeRenderer)1 XYStepRenderer (org.jfree.chart.renderer.xy.XYStepRenderer)1 TimeSeries (org.jfree.data.time.TimeSeries)1 TimeSeriesCollection (org.jfree.data.time.TimeSeriesCollection)1 Test (org.junit.Test)1