PlotDataSeries implements MinMaxChangeListener, AbstractPlotDataSeries { public void updateRegressionLine() { if (!updateRegressionLine) { return; } if (getLegendEntry() != null) { if (getLegendEntry().hasRegressionLine()) { if (regressionLine == null) { addRegressionLine(); } } else { if (regressionLine != null) { removeRegressionLine(); } return; } regressionLine.removeAllPoints(); if (getData().getPointCount() < legendEntry.getNumberRegressionPoints()) { return; } double[] xRegData = new double[legendEntry.getNumberRegressionPoints()]; double[] yRegData = new double[legendEntry.getNumberRegressionPoints()]; int xDataLength = getData().getXData().getLength(); int yDataLength = getData().getYData().getLength(); int shift = 0; for (int i = legendEntry.getNumberRegressionPoints(); i > 0; i--) { if (((xDataLength - (legendEntry.getNumberRegressionPoints() - i) - 1 - shift) < 0) || ((yDataLength - (legendEntry.getNumberRegressionPoints() - i) - 1 - shift) < 0)) { return; } while ( Double.isNaN(getData().getXData().get(xDataLength - (legendEntry.getNumberRegressionPoints() - i) - 1 - shift)) || Double.isNaN(getData().getYData().get(yDataLength - (legendEntry.getNumberRegressionPoints() - i) - 1 - shift))) { if ((yDataLength - (legendEntry.getNumberRegressionPoints() - i) - 1 - (shift + 1)) < 0 || (xDataLength - (legendEntry.getNumberRegressionPoints() - i) - 1 - (shift + 1)) < 0) { return; } else { shift++; } } xRegData[i-1] = getData().getXData().get(xDataLength - (legendEntry.getNumberRegressionPoints() - i) - 1 - shift); yRegData[i-1] = getData().getYData().get(yDataLength - (legendEntry.getNumberRegressionPoints() - i) - 1 - shift); } double x = xRegData[legendEntry.getNumberRegressionPoints() - 1], y=yRegData[legendEntry.getNumberRegressionPoints() - 1]; HighPrecisionLinearRegression lr = new HighPrecisionLinearRegression(xRegData, yRegData); if (lr.getSlopeAsBigDecimal() != null && lr.getIntercept() != null && !Double.isNaN(lr.getSlope()) && !Double.isInfinite(lr.getSlope()) && !Double.isInfinite(lr.getIntercept().doubleValue()) && !Double.isNaN(lr.getIntercept().doubleValue()) ) { regressionLine.add(x, y); if(linePlot.getIndependentDimension() == XYDimension.X) { regressionLine.add(Long.valueOf(plot.getMaxTime()).doubleValue(), lr.calculateY(Long.valueOf(plot.getMaxTime()).doubleValue()) + (y - lr.calculateY(x))); } else { regressionLine.add(lr.calculateX(Long.valueOf(plot.getMaxTime()).doubleValue()) + (x - lr.calculateX(y)),Long.valueOf(plot.getMaxTime()).doubleValue()); } } } } PlotDataSeries(PlotterPlot thePlot, String dataSetName, Color theColor); LinearXYPlotLine getRegressionLine(); void setRegressionLine(LinearXYPlotLine line); String getDataSetName(); LegendEntry getLegendEntry(); void compressByFiftyPercent(); @Override void minMaxChanged(CompressingXYDataset dataset, XYDimension dimension); @Override String toString(); void updateRegressionLine(); boolean isUpdateRegressionLine(); void setUpdateRegressionLine(boolean updateRegressionLine); @Override void setLegendEntry(AbstractLegendEntry entry); static final Stroke EMPTY_STROKE; }
@Test public void TestUpdateRegressionLine() { GregorianCalendar timeOne = new GregorianCalendar(); GregorianCalendar timeTwo = new GregorianCalendar(); timeTwo.setTimeInMillis(timeOne.getTimeInMillis()); timeTwo.add(Calendar.MINUTE, 30); PlotterPlot testPlot = new PlotterPlot(); plotAbstraction.setMinTime(timeOne.getTimeInMillis()); plotAbstraction.setMaxTime(timeTwo.getTimeInMillis()); testPlot.createChart( new Font("Arial", Font.PLAIN, 1), 1, Color.white, Color.white, 0, Color.white, Color.white, Color.white, "dd", Color.black, Color.white, 1, false, true, true, plotAbstraction, plotLabelingAlgorithm); testPlot.setCompressionEnabled(false); Assert.assertFalse(testPlot.isCompressionEnabled()); testPlot.addDataSet("dataSet1", Color.white); PlotDataSeries data = (PlotDataSeries) testPlot.getPlotDataManager().getNamedDataSeries("dataSet1"); data.dataset.setCompressionOffset(timeOne.getTimeInMillis()); data.dataset.setCompressionScale(1); Assert.assertEquals(data.getDataSetName(), "dataSet1"); GregorianCalendar insertTime = new GregorianCalendar(); insertTime.setTimeInMillis(timeOne.getTimeInMillis()); double[] xData = new double[4]; data.getData().add(insertTime.getTimeInMillis(), 0.0); xData[0] = Long.valueOf(insertTime.getTimeInMillis()).doubleValue(); insertTime.add(Calendar.MINUTE, 1); data.getData().add(insertTime.getTimeInMillis(), 10.0); xData[1] = Long.valueOf(insertTime.getTimeInMillis()).doubleValue(); insertTime.add(Calendar.MINUTE, 1); data.getData().add(insertTime.getTimeInMillis(), 20.0); xData[2] = Long.valueOf(insertTime.getTimeInMillis()).doubleValue(); Mockito.when(legendEntry.hasRegressionLine()).thenReturn(true); Mockito.when(legendEntry.getNumberRegressionPoints()).thenReturn(2); data.setLegend(legendEntry); data.updateRegressionLine(); Assert.assertEquals(data.getRegressionLine().getXData().getLength(), 2); insertTime.add(Calendar.MINUTE, 1); data.getData().add(insertTime.getTimeInMillis(), 30.0); xData[3] = Long.valueOf(insertTime.getTimeInMillis()).doubleValue(); data.updateRegressionLine(); HighPrecisionLinearRegression lr = new HighPrecisionLinearRegression( xData, new double[]{0.0, 10.0, 20.0, 30.0}); Assert.assertEquals(data.getRegressionLine().getXData().get(0), Long.valueOf(insertTime.getTimeInMillis()).doubleValue(), EPSILON); Assert.assertEquals(data.getRegressionLine().getYData().get(0), 30.0, EPSILON); Assert.assertEquals(data.getRegressionLine().getXData().get(1), Long.valueOf(timeTwo.getTimeInMillis()).doubleValue(), EPSILON); Assert.assertEquals(data.getRegressionLine().getYData().get(1), lr.calculateY( Long.valueOf(timeTwo.getTimeInMillis()).doubleValue()) + (30 - lr.calculateY(xData[3])), EPSILON); }