📄 graph.java
字号:
brush.setColor(Color.blue); //** making sure there is data for(k=0;k<dataVec.size();k++) if(((Graph_data)dataVec.elementAt(k)).isEnabled()) { brush.setColor (((Graph_data)dataVec.elementAt(k)).getColor()); if(num_data_pts > 0) y_coord1 = origin.y; for(i=0,j=origin.x;i<(num_data_pts * num_in_group); i+= num_in_group,j+=x_bin_gap) { 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 = origin.y - (int)((cal_assist1 / (y_range_max-y_range_min)) * plotting_height); brush.drawLine(j,y_coord1,j + x_bin_gap,y_coord2); y_coord1 = y_coord2; } } brush.setColor(Color.black); } public double getXBinTime() { return x_bin_time; } public int getXBinGap() { return x_bin_gap; } public Point getOrigin() { return new Point(origin.x,origin.y); } public double getMinTimeVal() { return x_range_min; } /* changes the graph representation */ public void changeGraphType(int type) { this.graph_type = type; repaint(); } /* returns the range of the graph in pixels from x_min to x_max */ public int getGraphRange() { return (x_bin_gap * num_data_pts); } public Color getColor(int data_id) { return ((Graph_data)dataVec.elementAt(data_id)).getColor(); } public void changeColor(int data_id, Color c) { ((Graph_data)dataVec.elementAt(data_id)).changeColor(c); } public void setState(int data_id,boolean state) { ((Graph_data)dataVec.elementAt(data_id)).setEnabled(state); } public boolean isEnabled(int i) { return ((Graph_data)dataVec.elementAt(i)).isEnabled(); } public String getLabel(int i) { return ((Graph_data)dataVec.elementAt(i)).getLabel(); } /* This function returns a string representing the scaled value. -precision is a positive number defining the max number of characters in the returned string. -x_pos is the position on the graph for which a string representation of the time is required. */ public String calculateXValueString(int x_pos,int precision) { String value_s,temp_string; double value; int indexValue, min_displyd_char = 2;//-- example: "0.0.......1" //-- the "0." is 2 char long. value = (((double)(x_pos-origin.x)/x_bin_gap)*x_bin_time)+ x_range_min; //**scaling the value obtained from the above operation value /= Math.pow(10,x_scale_index); temp_string = Double.toString(value); //** if the value is very small or very large the string representation //** is in scientific notation which poses a tricky problem when //** the string needs to be displayed on the graph. the solution is //** taken care of in the following if statement. if(temp_string.indexOf((int)'E') != -1) { indexValue = Integer.parseInt(temp_string.substring (temp_string.indexOf((int)'E')+1)); //** since the max value has already been scaled down, //** only negative indices are possible in the string //** representation of the time value. The following if //** statement checks to make sure if the exponent is within //** the prescribed length of the final string (precision). if(indexValue >= ((-1)*(precision-min_displyd_char))) //** { value_s ="0."; int zero_counter = (-1)*indexValue; while(zero_counter > 1) { value_s += "0"; zero_counter--; } int i=0; value_s += temp_string.charAt(i); i=2; while((value_s.length() < precision)&& (temp_string.charAt(i) != 'E')) { value_s += temp_string.charAt(i); i++; } } else value_s = "0.0"; } else { value_s = temp_string; if(value_s.length()>precision) value_s = value_s.substring(0,precision); if(value_s.indexOf((int)'.') == (value_s.length()-1)) value_s = value_s.substring(0,value_s.length()-1); } return new String(value_s); } /* This function accepts a time value and moves the marker on the graph to the cooresponding value on the graph. */ public void moveMarkerTo(double time) { if ( IsTimeUnitBigger ) time /= time_unit; int cal_assist1 = (int)((time/this.x_bin_time)*x_bin_gap)+origin.x; if(markd != 0) markd_view.setLocation(markd_view.x + (cal_assist1 - markd),0); this.markd = cal_assist1; repaint(); } private void reScale() { double cal_assist1, cal_assist2; int cal_assist3, cal_assist4; //** the following code performs the task of grouping data points //** together if the number of data points is greater than the minimum //** display width. We know there is at least one data set in //** dataVec and hence the index 0. We have already checked for this //** in an if statement at the beginning of the constructor. if((((Graph_data)dataVec.elementAt(0)).getLength() % graph_size.width) == 0) num_in_group = (((Graph_data)dataVec.elementAt(0)).getLength() / graph_size.width); else num_in_group = (((Graph_data)dataVec.elementAt(0)).getLength() / graph_size.width) + 1; num_data_pts = ((Graph_data)dataVec.elementAt(0)).getLength() / num_in_group; this.x_bin_gap = graph_size.width / (num_data_pts); if(this.x_bin_gap == 0) { System.out.println("Number of values to be graphed exceeds" + " the maximum display size of 500 pixels"); System.exit(1); } x_bin_time = ((double)x_bin_gap / ((num_data_pts) * x_bin_gap)) * (this.x_range_max - this.x_range_min); if(reScaleFlag) { y_range_min = 0; y_range_max_line = 0; y_range_max_bar = 0; //** finding the largest value in the data set to initialize //** the max y value. int i,k; for (i=0;i < (num_data_pts * num_in_group);i+=num_in_group) { int j; cal_assist1 = 0; for(j=0;j < dataVec.size();j++) { if(((Graph_data)dataVec.elementAt(j)). isEnabled()) { cal_assist2 = 0; for(k=0;k < num_in_group;k++) cal_assist2 += ((Graph_data)dataVec. elementAt(j)). getElementAt(i+k); cal_assist1 += cal_assist2; if(cal_assist2 > y_range_max_line) y_range_max_line = cal_assist2; } } if(cal_assist1 > y_range_max_bar) y_range_max_bar = (double)cal_assist1; } //** the max y value is always a bit higher than the actual //** max data value by 20 pixels. y_range_max_bar += (((double)20 / graph_size.height) * (y_range_max_bar-y_range_min)); y_range_max_line += (((double)20 / graph_size.height) * (y_range_max_line-y_range_min)); if(graph_type == Graph.BAR) y_range_max = y_range_max_bar; else y_range_max = y_range_max_line; //** whenever all data sets are disabled y_range_max gets //** calculated as zero and hence the following if statement. if(y_range_max < 1) y_range_max = 1; y_range_max = Math.rint(y_range_max + 0.5); if(y_range_max < graph_size.height) { plotting_height = (int) ((int)(graph_size.height/y_range_max) * y_range_max); cal_assist3 = (int)(plotting_height / y_range_max); cal_assist4 = (y_bin_gap_min/cal_assist3) * cal_assist3; if(cal_assist4 < y_bin_gap_min) y_bin_gap = cal_assist4 + cal_assist3; else y_bin_gap = y_bin_gap_min; } } } /* marks the point selected in the graph by setting the value of marker to the time value corresponding to that point. */ public double mark_time(Point marker, Point viewPosition) { int cal_assist1; markd_view.setLocation(viewPosition.x,0); markd = marker.x; repaint(); // if(x_range_max > time_unit) if( IsTimeUnitBigger ) { return ( ( (double)(marker.x-origin.x) * x_bin_time / x_bin_gap ) + x_range_min ) * time_unit; } else { return ( (double)(marker.x-origin.x) * x_bin_time / x_bin_gap ) + x_range_min; } } public boolean zoomIn() { int cal_assist1; if(graph_size.width < Math.pow(2,14)) { zoom_level++; cal_assist1 = markd - markd_view.x; markd*=2; markd_view.setLocation(markd - cal_assist1,0); setViewportWidth(viewportWidth); repaint(); } if(graph_size.width > Math.pow(2,13)) return false; else return true; } public boolean zoomOut() { int cal_assist1; if(zoom_level > 0) { zoom_level--; cal_assist1 = markd - markd_view.x; markd/=2; if(zoom_level != 0) markd_view.setLocation(markd - cal_assist1,0); else markd_view.setLocation(0,0); setViewportWidth(viewportWidth); repaint(); } if(zoom_level == 0) return false; else return true; } public void reset() { markd/=Math.pow(2,zoom_level); zoom_level = 0; markd_view.setLocation(0,0); setViewportWidth(viewportWidth); repaint(); } public YAxisPanel getYAxis() { return YAxis; } public Dimension getXAxisDimension() { return (new Dimension(display_size)); } public Point getMarkdView() { return new Point(markd_view); } public void setReScale(boolean value) { reScaleFlag = value; } public void setViewportWidth(int width) { int graph_width = MIN_WIDTH * (int)Math.pow(2,zoom_level), display_width = origin.x + graph_width + GRAPH_MARGIN; viewportWidth = width; if(display_width < viewportWidth) { graph_width = graph_width * viewportWidth / display_width; display_width = viewportWidth; } display_size = new Dimension(display_width, origin.y + (3 * GRAPH_MARGIN)); graph_size = new Dimension(graph_width, FIXED_GRAPH_HEIGHT); setSize(display_size); setPreferredSize(display_size); repaint(); } public XAxisEndPanel getXAxisEnd() { return XAxisEnd; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -