/** * Return the axis values for the given pixel location on this graph. * * @param x The x pixel location on the graph. * @param y The y pixel location on the graph. * @return the x and y values for the given pixel location. */ public double[] getValueAt(int x, int y) { Insets insets = getInsets(); double v[] = new double[2]; v[0] = xAxis != null ? xAxis.getValue(x - insets.left) : 0.; v[1] = yAxis != null ? yAxis.getValue(getHeight() - y - insets.bottom) : 0.; return v; }
public void componentResized(ComponentEvent e) { Dimension dim = getSize(); Insets insets = getInsets(); Rectangle dataArea = new Rectangle( insets.left, insets.top, dim.width - insets.left - insets.right - 1, dim.height - insets.top - insets.bottom - 1); if (xAxis.getSize() != dataArea.width - 2) { xAxis.setSize(dataArea.width); } if (yAxis.getSize() != dataArea.height - 2) { yAxis.setSize(dataArea.height); } }
/** * Called by the paint method to draw the graph and its graph items. * * @param g the graphics context. */ public void paintComponent(Graphics g) { Dimension dim = getSize(); Insets insets = getInsets(); dataArea = new Rectangle( insets.left, insets.top, dim.width - insets.left - insets.right - 1, dim.height - insets.top - insets.bottom - 1); // background if (isOpaque()) { g.setColor(getBackground()); g.fillRect(0, 0, dim.width, dim.height); } g.setColor(getForeground()); // get axis tickmarks double xticks[] = xAxis.getTicks(); double yticks[] = yAxis.getTicks(); int yb = dataArea.y + dataArea.height; // draw grid if (showGrid) { g.setColor(gridColor != null ? gridColor : getBackground().darker()); // vertical x grid lines for (int i = 0; i < xticks.length; i += 2) { int x = dataArea.x + (int) Math.round(xticks[i]); g.drawLine(x, dataArea.y, x, dataArea.y + dataArea.height); } // horizontal y grid lines for (int i = 0; i < yticks.length; i += 2) { int y = yb - (int) Math.round(yticks[i]); g.drawLine(dataArea.x, y, dataArea.x + dataArea.width, y); } } for (int i = 0; i < graphItems.size(); i++) { ((GraphItem) graphItems.elementAt(i)).draw(this, g, dataArea, xAxis, yAxis); } if (sPt != null && ePt != null) { g.setColor(getForeground()); g.drawRect( Math.min(sPt.x, ePt.x), Math.min(sPt.y, ePt.y), Math.abs(ePt.x - sPt.x), Math.abs(ePt.y - sPt.y)); } }
public int showDialog(JChart chart) { JOptionPane optionPane = new JOptionPane( optionPanel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, null, null); optionPane.setBorder(new EmptyBorder(12, 12, 12, 12)); Axis xAxis = chart.getXAxis(); Axis yAxis = chart.getYAxis(); if (canLogXAxis) { logXAxis.setSelected(xAxis instanceof LogAxis); } if (canLogYAxis) { logYAxis.setSelected(yAxis instanceof LogAxis); } if (!manualXAxis.isSelected()) { minXValue.setValue(xAxis.getMinAxis()); maxXValue.setValue(xAxis.getMaxAxis()); } if (!manualYAxis.isSelected()) { minYValue.setValue(yAxis.getMinAxis()); maxYValue.setValue(yAxis.getMaxAxis()); } final JDialog dialog = optionPane.createDialog(frame, "Setup Chart"); dialog.pack(); dialog.setVisible(true); final Integer value = (Integer) optionPane.getValue(); final int result = (value != null && value != -1) ? value : JOptionPane.CANCEL_OPTION; if (result == JOptionPane.OK_OPTION) { applySettings(chart); } return result; }
public void applySettings(JChart chart) { Axis xAxis = chart.getXAxis(); Axis yAxis = chart.getYAxis(); if (canLogXAxis) { if (logXAxis.isSelected()) { xAxis = new LogAxis(); } else { xAxis = new LinearAxis(); } chart.setXAxis(xAxis); } if (manualXAxis.isSelected()) { xAxis.setManualRange(minXValue.getValue(), maxXValue.getValue()); xAxis.setAxisFlags(Axis.AT_VALUE, Axis.AT_VALUE); } else { xAxis.setAxisFlags(defaultMinXAxisFlag, defaultMaxXAxisFlag); } if (canLogYAxis) { if (logYAxis.isSelected()) { yAxis = new LogAxis(); } else { yAxis = new LinearAxis(); } chart.setYAxis(yAxis); } if (manualYAxis.isSelected()) { yAxis.setManualRange(minYValue.getValue(), maxYValue.getValue()); yAxis.setAxisFlags(Axis.AT_VALUE, Axis.AT_VALUE); } else { yAxis.setAxisFlags(defaultMinYAxisFlag, defaultMaxYAxisFlag); } }
/** * high-level method for drawing a chart axis line plus labeled tic marks. introduces a dependancy * on AWT because it takes a Graphics parameter. perhaps this method belongs in some higher-level * class but i added it here since it's highly related with the tic lable generation code. * * @author Melinda Green * @param axis is one of Axis.X_AXIS or Axis.Y_AXIS. * @param maxTics is the maximum number of labeled tics to draw. note: the actual number drawn may * be less. * @param lowVal is the smallest value tic mark that may be drawn. note: the lowest valued tic * label may be greater than this limit. * @param highVal is the largest value tic mark that may be drawn. note: the highest valued tic * label may be less than this limit. * @param screenStart is the coordinate in the low valued direction. * @param screenEnd is the coordinate in the high valued direction. * @param offset is the coordinate in the direction perpendicular to the specified direction. * @param logScale is true if a log scale axis is to be drawn, false for a linear scale. * @param screenHeight is needed to flip Y coordinates. * @param g is the AWT Graphics object to draw into. note: all drawing will be done in the current * color of the given Graphics object. */ public static void drawAxis( int axis, int maxTics, int ticLength, float lowVal, float highVal, int screenStart, int screenEnd, int screenOffset, boolean logScale, int screenHeight, Graphics g) { if (logScale && (lowVal == 0 || highVal == 0)) throw new IllegalArgumentException("Axis.drawAxis: zero range value not allowed in log axes"); if (axis == X_AXIS) // horizontal baseline g.drawLine(screenStart, screenHeight - screenOffset, screenEnd, screenHeight - screenOffset); else // vertical baseline g.drawLine(screenOffset, screenStart, screenOffset, screenEnd); Vector tics = Axis.computeTicks(lowVal, highVal, maxTics, logScale); // nice // round // numbers // for // tic // labels int last_label_end = axis == X_AXIS ? -88888 : 88888; String dbgstr = "tics: "; for (Enumeration e = tics.elements(); e.hasMoreElements(); ) { String ticstr = (String) e.nextElement(); if (DEBUG) dbgstr += ticstr + ", "; float ticval = Float.parseFloat(ticstr); int tic_coord = screenStart; Dimension str_size = stringSize(ticstr, g); tic_coord += plotValue(ticval, lowVal, highVal, screenStart, screenEnd, logScale, screenHeight); if (axis == X_AXIS) { // horizontal axis == vertical tics g.drawLine( tic_coord, screenHeight - screenOffset, tic_coord, screenHeight - screenOffset + ticLength); if (tic_coord - str_size.width / 2 > last_label_end) { g.drawString( ticstr, tic_coord - str_size.width / 2, screenHeight - screenOffset + str_size.height + 5); last_label_end = tic_coord + str_size.width / 2 + str_size.height / 2; } } else { // vertical axis == horizontal tics tic_coord = screenHeight - tic_coord; // flips Y coordinates g.drawLine(screenOffset - ticLength, tic_coord, screenOffset, tic_coord); if (tic_coord - str_size.height / 3 < last_label_end) { g.drawString( ticstr, screenOffset - ticLength - str_size.width - 5, tic_coord + str_size.height / 3); last_label_end = tic_coord - str_size.height; } } } if (DEBUG) System.out.println(dbgstr); } // end drawAxis