001 /* 002 * Copyright (C) 2008-2010 by Holger Arndt 003 * 004 * This file is part of the Universal Java Matrix Package (UJMP). 005 * See the NOTICE file distributed with this work for additional 006 * information regarding copyright ownership and licensing. 007 * 008 * UJMP is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation; either version 2 011 * of the License, or (at your option) any later version. 012 * 013 * UJMP is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public 019 * License along with UJMP; if not, write to the 020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 021 * Boston, MA 02110-1301 USA 022 */ 023 024 package org.ujmp.gui.plot; 025 026 import java.awt.Graphics; 027 import java.awt.Graphics2D; 028 029 public class RunningAveragePlot { 030 031 private PlotSettings plotSettings = null; 032 033 public RunningAveragePlot(PlotSettings plotSettings) { 034 this.plotSettings = plotSettings; 035 } 036 037 public void paintComponent(Graphics g) { 038 try { 039 long t0 = System.currentTimeMillis(); 040 041 Graphics2D g2d = (Graphics2D) g; 042 043 double xf = plotSettings.getXFactor(); 044 double yf = plotSettings.getYFactor(); 045 046 if (plotSettings.getMatrixGUIObject().getRowCount() < 2) { 047 return; 048 } 049 050 for (int t = 0; t < Math.min(10, plotSettings.getMatrixGUIObject().getColumnCount()); t++) { 051 052 if (plotSettings.isShowTrace(t)) { 053 054 long column = t; 055 056 g2d.setStroke(plotSettings.getRunningAverageStroke()); 057 g2d.setColor(plotSettings.getRunningAverageLineColor()); 058 059 double xs = plotSettings.getXStepSize(); 060 061 double sum = plotSettings.getMatrixGUIObject().getDoubleValueAt((long) plotSettings.getMinXValue(), 062 column); 063 double average = sum; 064 double oldAverage = average; 065 double firstPoint = plotSettings.getMinXValue(); 066 double nmbOfPoints = 1; 067 long dots = 0; 068 069 for (double xr = plotSettings.getMinXValue() + xs; xr <= plotSettings.getMaxXValue(); xr += xs) { 070 dots++; 071 long row1 = (long) (xr - xs); 072 long row2 = (long) xr; 073 long rowRA = (long) (firstPoint); 074 075 double yv1 = plotSettings.getMatrixGUIObject().getDoubleValueAt(row1, column); 076 double yv2 = plotSettings.getMatrixGUIObject().getDoubleValueAt(row2, column); 077 double yvRA = plotSettings.getMatrixGUIObject().getDoubleValueAt(rowRA, column); 078 079 sum += yv2; 080 nmbOfPoints++; 081 if (nmbOfPoints > (plotSettings.getRunningAverageLength()) / xs) { 082 sum = sum - yvRA; 083 firstPoint += xs; 084 nmbOfPoints--; 085 } 086 average = sum / nmbOfPoints; 087 088 int x1 = (int) ((xr - xs) * xf); 089 int x2 = (int) (xr * xf); 090 x2 = (x2 == x1) ? x2++ : x2; 091 092 int y1 = (int) (plotSettings.getHeight() - 1 - oldAverage * yf + plotSettings.getMinYValue() 093 * yf); 094 int y2 = (int) (plotSettings.getHeight() - 1 - average * yf + plotSettings.getMinYValue() * yf); 095 096 g2d.drawLine(x1, y1, x2, y2); 097 oldAverage = average; 098 } 099 100 } 101 102 long t1 = System.currentTimeMillis(); 103 if (t1 - t0 > plotSettings.getTimeLimit()) { 104 return; 105 } 106 107 } 108 } catch (Exception e) { 109 e.printStackTrace(); 110 } 111 } 112 113 }