📄 graph.java
字号:
import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.*;/*********Class Graph***********//* This class defines the basic panel on which the actual graph is painted. There are no components contained in the panel. The graph is first painted on an off-screen image and then that image is drawn on the panel. There is a choice between a line graph and a bar graph.*/public class Graph extends JPanel{ /*----constant class variables----*/ //----size of x-axis in pixels = graph_size.width //----size of y-axis in pixels = graph_size.height public final static int GRAPH_MARGIN = 20, //--constant pixel margin between graph and //--panel edge. LINE=1,BAR=2, //--constants for graph type. MIN_WIDTH = 512, FIXED_GRAPH_HEIGHT = 256, MIN_XLABL_SIZE = 60; /*----instance variables----*/ public Dimension graph_size = new Dimension(MIN_WIDTH,FIXED_GRAPH_HEIGHT); private Vector dataVec; private YAxisPanel YAxis; private XAxisEndPanel XAxisEnd; private int num_data_pts; private Color pre_defn_colors[] = { Color.blue, Color.green, Color.orange, Color.darkGray, Color.gray, Color.cyan, Color.magenta, Color.yellow, Color.pink, Color.black }; private double x_range_min, //--the lower time bound in time units. x_range_max, //--the upper time bound in time units. x_bin_time, //--the time allocated to each bin. y_range_min = 0, //--min y-value assumed to be 0. y_range_max, y_range_max_line, //--max y-value. y_range_max_bar; /* a bin is a pixel representation of the time interval between each value recorded. */ private int x_bin_gap, //--size of bin in pixels. graph_type = 2, //--default is line graph. x_labl_size = 60,//--min pixel gap between label on x-axis. y_bin_gap_min = 40, //--min pixel gap between labels on the //--y-axis. x_scale_index = 0, x_scale_string_width = 6, y_bin_gap=40, plotting_height = 256, num_in_group = 1, //-- number of data points to be grouped for display //-- default is one data point. zoom_level = 0, viewportWidth = 0; private boolean IsTimeUnitBigger = false; private double time_unit = 1000.0; private boolean reScaleFlag = true, drawAgain = true; private Dimension display_size; //--total size of panel. private Point origin, //--x,y pixel coordinates of origin on //--on panel. markd_view = new Point(0,0); private Image virtual_canvas; private Graphics brush; private Font font; //--font type. private int markd; //--flag if point selected on graph. It //--also contains the x coord for that //--point. /* constructor - requires data set, the lower time bound and upper time bound */ public Graph(double data[][], String data_labels[], Color data_colors[], double x_range_min, double x_range_max) { int i, k, max_index, min_index, color_counter; double cal_assist1, cal_assist2; String temp_string; /* addComponentListener(new ComponentAdapter() { public void componentResized(ComponentEvent e){ ((Graph)e.getSource()).repaint(); } }); */ dataVec = new Vector(); markd = 0; font = new Font("Dialog",Font.PLAIN,12); if(data.length < 1) { System.out.print("No data values passed."+ " Terminating program."); System.exit(1); } for(i=0, color_counter=0;i<data.length;i++) { /* if(color_counter < pre_defn_colors.length) dataVec.addElement(new Graph_data (data_labels[i],data[i],true, pre_defn_colors[color_counter++])); else dataVec.addElement(new Graph_data (data_labels[i],data[i])); */ dataVec.addElement( new Graph_data(data_labels[i],data[i],true,data_colors[i]) ); } this.x_range_max = x_range_max; this.x_range_min = x_range_min; if(x_range_max > time_unit) { IsTimeUnitBigger = true; this.x_range_min /= time_unit; this.x_range_max /= time_unit; } //** the following paragraph of code generates the scale of the graph //** in terms of an integer index of 10 //** for the case where max is either too large or too small. max_index = (int)(Math.log(this.x_range_max)/Math.log(10.0)); if(max_index > (x_scale_string_width-1)) x_scale_index = max_index - x_scale_string_width; else if( max_index < ((-1)*(x_scale_string_width-2))) x_scale_index = (x_scale_string_width-2)+max_index; reScale(); temp_string = Double.toString(y_range_max_bar); temp_string = temp_string.substring(0, temp_string.indexOf((int)'.') + 2); origin = new Point(0, GRAPH_MARGIN + graph_size.height); display_size = new Dimension(origin.x + graph_size.width + GRAPH_MARGIN, origin.y + (3 * GRAPH_MARGIN)); Dimension yaxis_dim = new Dimension (GRAPH_MARGIN + 11 + getFontMetrics(font).stringWidth(temp_string), display_size.height); Point originY = new Point(yaxis_dim.width - 6,origin.y); YAxis = new YAxisPanel(this,originY,yaxis_dim); XAxisEnd = new XAxisEndPanel(this, new Point(origin)); setPreferredSize(display_size); } /* this is where all the drawing takes place as well as the initialization for the graphics context. */ public void paint(Graphics g) { super.paint(g); g.setColor(Color.white); virtual_canvas = createImage(display_size.width,display_size.height); brush = virtual_canvas.getGraphics(); //** standardizing the font for the axis labels. font = brush.getFont(); reScale(); //** initializing the label size so as to be a multiple of the bin size //** and also to make sure that the labels dont overlap. x_labl_size = getFontMetrics(font).stringWidth("digitsize"); if(x_bin_gap >= MIN_XLABL_SIZE) x_labl_size = x_bin_gap; else x_labl_size = ((x_labl_size / x_bin_gap) + 1) * x_bin_gap; //** drawing the axis drawXAxis(); if(graph_type == Graph.BAR) drawBarGraph(); else drawLineGraph(); g.drawImage(virtual_canvas,0,0,Color.white,this); YAxis.repaint(); //** draw the marker if the point was selected if(markd != 0) { g.setColor(Color.red); g.drawLine(markd, origin.y + 6, markd, origin.y - graph_size.height - 6); } g.setColor(Color.black); g.drawLine(origin.x, origin.y,getWidth(), origin.y); g.setColor(Color.white); brush.dispose(); } /* draws both the x-axis and the y-axis with all the labels. */ private void drawXAxis() { int cal_assist1,cal_assist2; brush.setColor(Color.white); brush.fillRect (0,0,virtual_canvas.getWidth(this),virtual_canvas.getHeight(this)); brush.setColor(Color.black); //** draw labels on x-axis String temp; int i; for(i=origin.x; (((i-origin.x)/x_bin_gap)*x_bin_time+x_range_min) < x_range_max; i+=x_labl_size) { brush.drawLine(i,origin.y,i,origin.y+4); if(i>origin.x) centerLabelOnX(i); else { temp = calculateXValueString(origin.x,7); temp = temp.substring(0,temp.indexOf('.')); brush.drawString(temp, origin.x, origin.y + getFontMetrics(font).getMaxAscent() + 6); } } temp = "time ("; if(x_scale_index != 0) temp += " x10"; if( IsTimeUnitBigger ) temp += " x " + Double.toString( time_unit ); temp += " seconds )"; if((x_scale_index != 0)&&(x_scale_index != 1)) { brush.setFont(new Font(brush.getFont().getName(), brush.getFont().getStyle(), 9)); brush.drawString(Integer.toString(x_scale_index), origin.x + (graph_size.width/2) - (getFontMetrics(font).stringWidth(temp)/2)+ (getFontMetrics(font). stringWidth("time ( x10")), origin.y + 4*getFontMetrics(font).getMaxAscent()-4); brush.setFont(font); } brush.drawString(temp, origin.x + (graph_size.width/2) - (getFontMetrics(font).stringWidth(temp)/2), origin.y + 4*getFontMetrics(font).getMaxAscent()); //** draw the marker to show the max x-value brush.setColor(Color.red); brush.drawLine(origin.x + (x_bin_gap * num_data_pts),origin.y, origin.x + (x_bin_gap * num_data_pts),origin.y+8); temp = calculateXValueString((x_bin_gap*num_data_pts)+origin.x,6); brush.drawString(temp, origin.x + (x_bin_gap * num_data_pts)- (int) (0.75*getFontMetrics(font).stringWidth(temp)), origin.y + 2*getFontMetrics(font).getMaxAscent()+8); brush.setColor(Color.black); } /* this returns the dimensions of the panel */ public Dimension getDimension() { return (new Dimension(display_size.width + YAxis.getDimension().width, display_size.height)); } public int getPlotting_height() { return plotting_height; } public double getYRangeMax() { return y_range_max; } public int getYBinGap() { return y_bin_gap; } /* centers the label at the given position under the x-axis */ private void centerLabelOnX(int x_position) { String value_s = calculateXValueString(x_position,6); brush.drawString(value_s, x_position-(getFontMetrics(font).stringWidth(value_s)/2), (origin.y+6)+(getFontMetrics(font).getMaxAscent())); } /* draws the entire bar graph in one shot */ private void drawBarGraph() { int i,j,y_coord1=0,y_coord2=0,bar_width=1; double cal_assist1; brush.setColor(Color.blue); //** calculating bar width so as not to intersect any two bars if(x_bin_gap > 3) if(x_bin_gap%2 == 0) bar_width = (x_bin_gap-1); else bar_width = (x_bin_gap-2); for(i=0,j=origin.x+x_bin_gap; i<(num_data_pts * num_in_group);i+=num_in_group,j+=x_bin_gap) { int k; y_coord1 = origin.y; for(k=0;k<dataVec.size();k++) if(((Graph_data)dataVec.elementAt(k)).isEnabled()) { brush.setColor (((Graph_data)dataVec.elementAt(k)). getColor()); cal_assist1 = 0; int l; for(l=0;l<num_in_group;l++) cal_assist1 += ((Graph_data)dataVec.elementAt(k)). getElementAt(i+l); y_coord2 = y_coord1 - (int)((cal_assist1 / y_range_max) * plotting_height); brush.fillRect(j-bar_width+1, y_coord2, bar_width,y_coord1 - y_coord2); y_coord1 = y_coord2; } } brush.setColor(Color.black); } /* draws line graph in one shot */ private void drawLineGraph() { int i,j,k,y_coord1=0,y_coord2; double cal_assist1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -