/** * Sets the data set that the graph displays. Depending on the log mode, the line will represents * the values of the <sup>10</sup>log() of the values. * * @param d the data set. * @see #getDataSet */ public void setDataSet(GraphDataSet d) { if (dataSet != null) { dataSet.removePropertyChangeListener(this); } dataSet = d; if (dataSet != null) { dataSet.addPropertyChangeListener(this); } }
/** * Returns the minimum size. * * @return the dimensions of the minimum size */ public Dimension getMinimumSize() { int w, h; int cap = 100; if (dataSet != null) { cap = dataSet.getCapacity(); } w = cap * 1 + 2; h = (int) (numYTicks * 2) + 2; return new Dimension(w, h); }
/** * Returns the preferred size. * * @return the dimensions of the preferred size */ public Dimension getPreferredSize() { int w, h; int cap = 100; if (dataSet != null) { cap = dataSet.getCapacity(); } w = cap * 2 + 5; h = (int) (numYTicks * 5) + 5; return new Dimension(w, h); }
/** * This method will draw the line. * * @param gc the graphics * @see #setLog */ void paintLine(Graphics g) { double dvalue; int[] values = dataSet.getValues(); // first decide the x scale factor; if ((dataSet == null) || (dataSet.getSize() < 1)) { return; // nothing to draw } // the maximum number of values we are every going to get int capacity = dataSet.getCapacity(); // the available number of values at the moment int nr = dataSet.getSize(); Rectangle space = g.getClipBounds(); // pixel width of x values int width = space.width; int xscale = (int) (width / capacity); if (xscale < 1) { xscale = 1; } width = xscale * capacity; if (width > space.width) { width = space.width; } // pixel width of y values float yscale = (space.height) / (float) (dmax - dmin); // offset into array int xstart = 0; if (width < (nr * xscale)) { // we are lacking space xstart = nr - (int) (width / xscale); } // if needed reallocate some space int totx = nr - xstart + 1; if (xline.length < totx) { xline = new int[totx + 100]; yline = new int[totx + 100]; } // now workout where to put the first point so that // the last one ends up on the right hand edge int x = width - (int) ((nr - xstart - 1) * xscale); if (x < 0) { x = 0; // can't be negative System.out.println("negative start point in graph."); } int y; int ox = x; x += xscale; dvalue = values[xstart]; if (doLog) { dvalue = log10(values[xstart]); } xstart++; int oy = space.height - (int) ((dvalue - dmin) * yscale); int pt = 0; for (int i = xstart; i < nr; i++) { dvalue = values[i]; if (doLog) { dvalue = log10(values[i]); } y = space.height - (int) ((dvalue - dmin) * yscale); xline[pt] = x; yline[pt] = y; pt++; ox = x; oy = y; x += xscale; } g.drawPolyline(xline, yline, pt); }