📄 chartcanvas.java
字号:
// You can redistribute this software and/or modify it under the terms of// the Ozone Library License version 1 published by ozone-db.org.//// The original code and portions created by SMB are// Copyright (C) 1997-2000 by SMB GmbH. All rights reserved.//// $Id: ChartCanvas.java,v 1.4 2000/10/28 16:55:20 daniela Exp $package org.ozoneDB.tools;import java.awt.*;import java.awt.event.*;import java.util.*;/** */public class ChartCanvas extends Canvas { /** */ public static int DEFAULT_TIME_RANGE = 10000; /** */ private Image dbImage = null; private Graphics dbGraphics = null; /** */ private int timeRange = DEFAULT_TIME_RANGE; private int[] times = null; private int pointCounter = 1; private boolean shiftLeftOnEnd = true; private int lowerBorder = 15; /** */ private int averageCount = 3; private int minTime = 0x7FFFFFFF; private int maxTime = 0; private boolean[] showAverage = {true, true, true}; private int[] lastPoint = {0, 0, 0}; private int[] lastAverage = {0, 0, 0}; private int[] averageRange = {1, 10, 30}; private Color[] averageColor = {Color.black, Color.red, Color.blue}; /** */ public ChartCanvas() { setBackground( Color.white ); addComponentListener( new ComponentAdapter() { public void componentResized( ComponentEvent e ) { clear(); } } ); } /** */ protected void clear() { dbImage = null; dbGraphics = null; } /** */ protected void shiftLeft() { dbGraphics.copyArea( 0, 0, getSize().width, getSize().height - lowerBorder, -1, 0 ); dbGraphics.setColor( getBackground() ); dbGraphics.fillRect( getSize().width - 1, 0, 1, getSize().height ); } /** */ protected int timeToPixel( int time ) { int height = getSize().height - lowerBorder; return height - height * time / timeRange - 1; } /** */ protected void drawTimePoint( int timeIndex ) { for (int i = 0; i < averageCount; i++) { int average = lastAverage[i] - (timeIndex - averageRange[i] < 0 ? 0 : times[timeIndex - averageRange[i]]) + times[timeIndex]; int newPoint = timeToPixel( average / averageRange[i] ); if (showAverage[i]) { dbGraphics.setColor( averageColor[i] ); dbGraphics.drawLine( timeIndex - 1, lastPoint[i], timeIndex, newPoint ); } lastPoint[i] = newPoint; lastAverage[i] = average; } } /** */ protected void drawStatus() { dbGraphics.setColor( getBackground() ); dbGraphics.fillRect( 0, getSize().height - lowerBorder, getSize().width, lowerBorder ); dbGraphics.setColor( Color.black ); dbGraphics.drawLine( 0, getSize().height - lowerBorder, getSize().width, getSize().height - lowerBorder ); dbGraphics.drawString( "t: " + lastAverage[0] / averageRange[0] + " am: " + lastAverage[1] / averageRange[1] + " al: " + lastAverage[2] / averageRange[2] + " min: " + minTime + " max: " + maxTime, 5, getSize().height - 4 ); } /** */ public void paint( Graphics g ) { if (dbImage == null) { dbImage = createImage( getSize().width, getSize().height ); dbGraphics = dbImage.getGraphics(); dbGraphics.setColor( getBackground() ); dbGraphics.fillRect( 0, 0, getSize().width, getSize().height ); Font fontNorm = dbGraphics.getFont(); dbGraphics.setFont( new Font( "Courier", fontNorm.getStyle(), fontNorm.getSize() - 1 ) ); } g.drawImage( dbImage, 0, 0, this ); } /** */ public void update( Graphics g ) { paint( g ); } /** */ public void appendTime( long time ) { if (times == null) { times = new int[getSize().width]; lastPoint[0] = lastPoint[1] = lastPoint[2] = getSize().height; } if (pointCounter == times.length) { if (shiftLeftOnEnd) { int[] help = new int[times.length]; System.arraycopy( times, 1, help, 0, help.length - 1 ); times = help; pointCounter--; shiftLeft(); } else { pointCounter = 1; clear(); } } times[pointCounter] = (int)time; minTime = Math.min( minTime, (int)time ); maxTime = Math.max( maxTime, (int)time ); drawTimePoint( pointCounter ); drawStatus(); paint( getGraphics() ); pointCounter++; } /** */ public void redrawAll() { paint( getGraphics() ); dbGraphics.setColor( getBackground() ); dbGraphics.fillRect( 0, 0, getSize().width, getSize().height ); dbGraphics.setColor( Color.black ); for (int i = 1; i < pointCounter; i++) { drawTimePoint( i ); } drawStatus(); } /** */ public void reset() { timeRange = DEFAULT_TIME_RANGE; times = null; pointCounter = 1; shiftLeftOnEnd = true; lowerBorder = 15; minTime = 0x7FFFFFFF; maxTime = 0; showAverage = new boolean[] {true, true, true}; lastPoint = new int[] {0, 0, 0}; lastAverage = new int[] {0, 0, 0}; averageRange = new int[] {1, 10, 30}; averageColor = new Color[] {Color.black, Color.red, Color.blue}; clear(); paint( getGraphics() ); } /** */ public void setTimeRange( int time ) { timeRange = time; } /** */ public int timeRange() { return timeRange; } /** */ public void setShiftLeftOnEnd( boolean value ) { shiftLeftOnEnd = value; } /** */ public boolean shiftLeftOnEnd() { return shiftLeftOnEnd; } /** */ public void enableAverages( boolean[] values ) { showAverage = values; } /** */ public boolean[] averageEnabled() { return showAverage; } /** */ public void setAverageRanges( int[] values ) { averageRange = values; } /** */ public int[] averageRanges() { return averageRange; } /** */ public void setAverageColors( Color[] colors ) { averageColor = colors; } /** */ public Color[] averageColors() { return averageColor; } /** */ public String[] filterAndApplyArgs( String[] args ) { Vector remainingArgs = new Vector(); for (int i = 0; i < args.length; i++) { if (args[i].startsWith( "-timeRange=" )) { timeRange = new Integer( args[i].substring( 11 ) ).intValue(); } else if (args[i].startsWith( "-shortRange=" )) { averageRange[0] = new Integer( args[i].substring( 12 ) ).intValue(); } else if (args[i].startsWith( "-mediumRange=" )) { averageRange[1] = new Integer( args[i].substring( 13 ) ).intValue(); } else if (args[i].startsWith( "-longRange=" )) { averageRange[2] = new Integer( args[i].substring( 11 ) ).intValue(); } else if (args[i].startsWith( "-shortAverage=" )) { showAverage[0] = args[i].substring( 14 ).equals( "on" ); } else if (args[i].startsWith( "-mediumAverage=" )) { showAverage[1] = args[i].substring( 15 ).equals( "on" ); } else if (args[i].startsWith( "-longAverage=" )) { showAverage[2] = args[i].substring( 13 ).equals( "on" ); } else if (args[i].startsWith( "-shiftLeft=" )) { shiftLeftOnEnd = args[i].substring( 11 ).equals( "on" ); } else { remainingArgs.addElement( args[i] ); } } redrawAll(); String[] result = new String[remainingArgs.size()]; for (int i = 0; i < result.length; i++) { result[i] = (String)remainingArgs.elementAt( i ); } return result; } /** */ public static String availableArgs() { return "[-timeRange=<millisecs>] [-shortRange=<range>] [-mediumRange=<range>] [-longRange=<range>] " + "[-shortAverage=(on|off)] [-mediumAverage=(on|off)] [-longAverage=(on|off)] [-shiftLeft=(on|off)]"; } /** */ public static String helpText() { return " -timeRange= : the maximum time to be shown on the y-axis in milli seconds, default: 20000\n" + " -shortRange= : the range of the short average, default: last 1\n" + " -mediumRange= : the range of the medium average, default: last 10\n" + " -longRange= : the range of the long average, default: last 30\n" + " -shortAverage= : switch the short average line on or off\n" + "-mediumAverage= : switch the medium average line on or off\n" + " -longAverage= : switch the long average line on or off\n" + " -shiftLeft= : shift the chart left, if the line touchs the right border\n"; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -