001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Library License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
006: //
007: // $Id: ChartCanvas.java,v 1.1 2001/12/18 10:31:31 per_nyfelt Exp $
008:
009: package org.ozoneDB.tools;
010:
011: import java.awt.*;
012: import java.awt.event.*;
013: import java.util.*;
014:
015: /**
016: */
017: public class ChartCanvas extends Canvas {
018: /** */
019: public static int DEFAULT_TIME_RANGE = 10000;
020:
021: /** */
022: private Image dbImage = null;
023: private Graphics dbGraphics = null;
024:
025: /** */
026: private int timeRange = DEFAULT_TIME_RANGE;
027: private int[] times = null;
028: private int pointCounter = 1;
029: private boolean shiftLeftOnEnd = true;
030: private int lowerBorder = 15;
031:
032: /** */
033: private int averageCount = 3;
034: private int minTime = 0x7FFFFFFF;
035: private int maxTime = 0;
036: private boolean[] showAverage = { true, true, true };
037: private int[] lastPoint = { 0, 0, 0 };
038: private int[] lastAverage = { 0, 0, 0 };
039: private int[] averageRange = { 1, 10, 30 };
040: private Color[] averageColor = { Color.black, Color.red, Color.blue };
041:
042: /** */
043: public ChartCanvas() {
044: setBackground(Color.white);
045: addComponentListener(new ComponentAdapter() {
046:
047: public void componentResized(ComponentEvent e) {
048: clear();
049: }
050: });
051: }
052:
053: /** */
054: protected void clear() {
055: dbImage = null;
056: dbGraphics = null;
057: }
058:
059: /** */
060: protected void shiftLeft() {
061: dbGraphics.copyArea(0, 0, getSize().width, getSize().height
062: - lowerBorder, -1, 0);
063: dbGraphics.setColor(getBackground());
064: dbGraphics
065: .fillRect(getSize().width - 1, 0, 1, getSize().height);
066: }
067:
068: /** */
069: protected int timeToPixel(int time) {
070: int height = getSize().height - lowerBorder;
071: return height - height * time / timeRange - 1;
072: }
073:
074: /** */
075: protected void drawTimePoint(int timeIndex) {
076: for (int i = 0; i < averageCount; i++) {
077: int average = lastAverage[i]
078: - (timeIndex - averageRange[i] < 0 ? 0
079: : times[timeIndex - averageRange[i]])
080: + times[timeIndex];
081: int newPoint = timeToPixel(average / averageRange[i]);
082: if (showAverage[i]) {
083: dbGraphics.setColor(averageColor[i]);
084: dbGraphics.drawLine(timeIndex - 1, lastPoint[i],
085: timeIndex, newPoint);
086: }
087: lastPoint[i] = newPoint;
088: lastAverage[i] = average;
089: }
090: }
091:
092: /** */
093: protected void drawStatus() {
094: dbGraphics.setColor(getBackground());
095: dbGraphics.fillRect(0, getSize().height - lowerBorder,
096: getSize().width, lowerBorder);
097:
098: dbGraphics.setColor(Color.black);
099: dbGraphics.drawLine(0, getSize().height - lowerBorder,
100: getSize().width, getSize().height - lowerBorder);
101: dbGraphics.drawString("t: " + lastAverage[0] / averageRange[0]
102: + " am: " + lastAverage[1] / averageRange[1] + " al: "
103: + lastAverage[2] / averageRange[2] + " min: " + minTime
104: + " max: " + maxTime, 5, getSize().height - 4);
105: }
106:
107: /** */
108: public void paint(Graphics g) {
109: if (dbImage == null) {
110: dbImage = createImage(getSize().width, getSize().height);
111: dbGraphics = dbImage.getGraphics();
112: dbGraphics.setColor(getBackground());
113: dbGraphics
114: .fillRect(0, 0, getSize().width, getSize().height);
115: Font fontNorm = dbGraphics.getFont();
116: dbGraphics.setFont(new Font("Courier", fontNorm.getStyle(),
117: fontNorm.getSize() - 1));
118: }
119: g.drawImage(dbImage, 0, 0, this );
120: }
121:
122: /** */
123: public void update(Graphics g) {
124: paint(g);
125: }
126:
127: /** */
128: public void appendTime(long time) {
129: if (times == null) {
130: times = new int[getSize().width];
131: lastPoint[0] = lastPoint[1] = lastPoint[2] = getSize().height;
132: }
133:
134: if (pointCounter == times.length) {
135: if (shiftLeftOnEnd) {
136: int[] help = new int[times.length];
137: System.arraycopy(times, 1, help, 0, help.length - 1);
138: times = help;
139: pointCounter--;
140: shiftLeft();
141: } else {
142: pointCounter = 1;
143: clear();
144: }
145: }
146:
147: times[pointCounter] = (int) time;
148: minTime = Math.min(minTime, (int) time);
149: maxTime = Math.max(maxTime, (int) time);
150: drawTimePoint(pointCounter);
151: drawStatus();
152: paint(getGraphics());
153: pointCounter++;
154: }
155:
156: /** */
157: public void redrawAll() {
158: paint(getGraphics());
159: dbGraphics.setColor(getBackground());
160: dbGraphics.fillRect(0, 0, getSize().width, getSize().height);
161: dbGraphics.setColor(Color.black);
162: for (int i = 1; i < pointCounter; i++) {
163: drawTimePoint(i);
164: }
165: drawStatus();
166: }
167:
168: /** */
169: public void reset() {
170: timeRange = DEFAULT_TIME_RANGE;
171: times = null;
172: pointCounter = 1;
173: shiftLeftOnEnd = true;
174: lowerBorder = 15;
175: minTime = 0x7FFFFFFF;
176: maxTime = 0;
177: showAverage = new boolean[] { true, true, true };
178: lastPoint = new int[] { 0, 0, 0 };
179: lastAverage = new int[] { 0, 0, 0 };
180: averageRange = new int[] { 1, 10, 30 };
181: averageColor = new Color[] { Color.black, Color.red, Color.blue };
182: clear();
183: paint(getGraphics());
184: }
185:
186: /** */
187: public void setTimeRange(int time) {
188: timeRange = time;
189: }
190:
191: /** */
192: public int timeRange() {
193: return timeRange;
194: }
195:
196: /** */
197: public void setShiftLeftOnEnd(boolean value) {
198: shiftLeftOnEnd = value;
199: }
200:
201: /** */
202: public boolean shiftLeftOnEnd() {
203: return shiftLeftOnEnd;
204: }
205:
206: /** */
207: public void enableAverages(boolean[] values) {
208: showAverage = values;
209: }
210:
211: /** */
212: public boolean[] averageEnabled() {
213: return showAverage;
214: }
215:
216: /** */
217: public void setAverageRanges(int[] values) {
218: averageRange = values;
219: }
220:
221: /** */
222: public int[] averageRanges() {
223: return averageRange;
224: }
225:
226: /** */
227: public void setAverageColors(Color[] colors) {
228: averageColor = colors;
229: }
230:
231: /** */
232: public Color[] averageColors() {
233: return averageColor;
234: }
235:
236: /** */
237: public String[] filterAndApplyArgs(String[] args) {
238: Vector remainingArgs = new Vector();
239: for (int i = 0; i < args.length; i++) {
240: if (args[i].startsWith("-timeRange=")) {
241: timeRange = new Integer(args[i].substring(11))
242: .intValue();
243: } else if (args[i].startsWith("-shortRange=")) {
244: averageRange[0] = new Integer(args[i].substring(12))
245: .intValue();
246: } else if (args[i].startsWith("-mediumRange=")) {
247: averageRange[1] = new Integer(args[i].substring(13))
248: .intValue();
249: } else if (args[i].startsWith("-longRange=")) {
250: averageRange[2] = new Integer(args[i].substring(11))
251: .intValue();
252: } else if (args[i].startsWith("-shortAverage=")) {
253: showAverage[0] = args[i].substring(14).equals("on");
254: } else if (args[i].startsWith("-mediumAverage=")) {
255: showAverage[1] = args[i].substring(15).equals("on");
256: } else if (args[i].startsWith("-longAverage=")) {
257: showAverage[2] = args[i].substring(13).equals("on");
258: } else if (args[i].startsWith("-shiftLeft=")) {
259: shiftLeftOnEnd = args[i].substring(11).equals("on");
260: } else {
261: remainingArgs.addElement(args[i]);
262: }
263: }
264:
265: redrawAll();
266:
267: String[] result = new String[remainingArgs.size()];
268: for (int i = 0; i < result.length; i++) {
269: result[i] = (String) remainingArgs.elementAt(i);
270: }
271:
272: return result;
273: }
274:
275: /** */
276: public static String availableArgs() {
277: return "[-timeRange=<millisecs>] [-shortRange=<range>] [-mediumRange=<range>] [-longRange=<range>] "
278: + "[-shortAverage=(on|off)] [-mediumAverage=(on|off)] [-longAverage=(on|off)] [-shiftLeft=(on|off)]";
279: }
280:
281: /** */
282: public static String helpText() {
283: return " -timeRange= : the maximum time to be shown on the y-axis in milli seconds, default: 20000\n"
284: + " -shortRange= : the range of the short average, default: last 1\n"
285: + " -mediumRange= : the range of the medium average, default: last 10\n"
286: + " -longRange= : the range of the long average, default: last 30\n"
287: + " -shortAverage= : switch the short average line on or off\n"
288: + "-mediumAverage= : switch the medium average line on or off\n"
289: + " -longAverage= : switch the long average line on or off\n"
290: + " -shiftLeft= : shift the chart left, if the line touchs the right border\n";
291:
292: }
293: }
|