Update of /cvsroot/jrobin/src/jrobin/graph In directory sc8-pr-cvs1:/tmp/cvs-serv4323/src/jrobin/graph Modified Files: TimeAxisUnit.java RrdGraphDef.java ChartGraphics.java Grapher.java ValueAxisUnit.java PlotDef.java Log Message: - value / time axis scaling modified - added hrule legend - fine tuned grid plotting - UNK values don't get drawn anymore - added line widths Index: TimeAxisUnit.java =================================================================== RCS file: /cvsroot/jrobin/src/jrobin/graph/TimeAxisUnit.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** TimeAxisUnit.java 22 Sep 2003 17:15:17 -0000 1.1 --- TimeAxisUnit.java 25 Sep 2003 21:58:05 -0000 1.2 *************** *** 43,47 **** Calendar.YEAR }; ! final String[] bla = {"second", "minute", "hour", "day", "week", "month", "year"}; // Indices in the calendarUnit table static final int SECOND = 0; --- 43,47 ---- Calendar.YEAR }; ! // Indices in the calendarUnit table static final int SECOND = 0; *************** *** 106,112 **** // Find first visible grid point ! long minPoint, majPoint; ! while ( (majPoint = getNextPoint(cMaj, mGridTime, mGridUnits)) < start ); ! while ( (minPoint = getNextPoint(cMin, gridTime, gridUnits)) < start ); ArrayList markerList = new ArrayList(); --- 106,116 ---- // Find first visible grid point ! long minPoint = cMin.getTimeInMillis(); ! long majPoint = cMaj.getTimeInMillis(); ! ! while ( majPoint < start ) ! majPoint = getNextPoint(cMaj, mGridTime, mGridUnits); ! while ( minPoint < start ) ! minPoint = getNextPoint(cMin, gridTime, gridUnits); ArrayList markerList = new ArrayList(); *************** *** 118,122 **** // // In short: the first while() loop is not *necessary* to get correct results ! while ( minPoint < stop && majPoint < stop ) { if ( minPoint < majPoint ) --- 122,126 ---- // // In short: the first while() loop is not *necessary* to get correct results ! while ( minPoint <= stop && majPoint <= stop ) { if ( minPoint < majPoint ) *************** *** 138,142 **** } ! while ( minPoint < stop ) { markerList.add( new TimeMarker( minPoint, "", false ) ); --- 142,146 ---- } ! while ( minPoint <= stop ) { markerList.add( new TimeMarker( minPoint, "", false ) ); *************** *** 144,148 **** } ! while ( majPoint < stop ) { markerList.add( new TimeMarker( majPoint, df.format(cMaj.getTime()), true ) ); --- 148,152 ---- } ! while ( majPoint <= stop ) { markerList.add( new TimeMarker( majPoint, df.format(cMaj.getTime()), true ) ); *************** *** 151,154 **** --- 155,169 ---- return (TimeMarker[]) markerList.toArray( new TimeMarker[0] ); + } + + + public long getMajorGridWidth() + { + Calendar c = Calendar.getInstance(); + long now = c.getTimeInMillis() / 1000; + + c.add( calendarUnit[mGridTime], mGridUnits ); + + return (c.getTimeInMillis() / 1000) - now; } } Index: RrdGraphDef.java =================================================================== RCS file: /cvsroot/jrobin/src/jrobin/graph/RrdGraphDef.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** RrdGraphDef.java 22 Sep 2003 17:15:17 -0000 1.2 --- RrdGraphDef.java 25 Sep 2003 21:58:05 -0000 1.3 *************** *** 218,222 **** graphs.add(new OverlayGraph(plotDef)); // Add comment line for the legend ! addComment( new Legend(plotDef.getColor(), plotDef.getLegend()) ); } --- 218,223 ---- graphs.add(new OverlayGraph(plotDef)); // Add comment line for the legend ! if ( plotDef.getLegend() != null ) ! addComment( new Legend(plotDef.getColor(), plotDef.getLegend()) ); } *************** *** 235,238 **** --- 236,241 ---- graphs.add(new OverlayGraph(hruleDef)); sources.add(hruleDef.getSource()); + if ( hruleDef.getLegend() != null ) + addComment( new Legend(hruleDef.getColor(), hruleDef.getLegend()) ); } Index: ChartGraphics.java =================================================================== RCS file: /cvsroot/jrobin/src/jrobin/graph/ChartGraphics.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** ChartGraphics.java 22 Sep 2003 17:15:17 -0000 1.1 --- ChartGraphics.java 25 Sep 2003 21:58:05 -0000 1.2 *************** *** 92,95 **** --- 92,97 ---- int getY( double value ) { + if ( Double.isNaN(value) ) return Integer.MIN_VALUE; + int tmp = new Double(value * heightDelta).intValue(); return ( tmp > value * heightDelta ? tmp - 1 : tmp ); Index: Grapher.java =================================================================== RCS file: /cvsroot/jrobin/src/jrobin/graph/Grapher.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Grapher.java 22 Sep 2003 17:15:17 -0000 1.2 --- Grapher.java 25 Sep 2003 21:58:05 -0000 1.3 *************** *** 53,57 **** private static final int LINE_PADDING = 4; ! static final Font TITLE_FONT = new Font("Lucida Sans Typewriter", Font.BOLD, 12); static final Font SUBTITLE_FONT = new Font("Lucida Sans Typewriter", Font.PLAIN, 10); --- 53,57 ---- private static final int LINE_PADDING = 4; ! static final Font TITLE_FONT = new Font("Lucida Sans Typewriter", Font.BOLD, 12); static final Font SUBTITLE_FONT = new Font("Lucida Sans Typewriter", Font.PLAIN, 10); *************** *** 61,65 **** static final String GRAPH_RESPECT = "niboRJ htiw detaerC"; private int numPoints = GRAPH_RESOLUTION; ! private int imgWidth, imgHeight; // Dimensions of the entire image private int chartWidth, chartHeight; // Dimensions of the chart area within the image --- 61,68 ---- static final String GRAPH_RESPECT = "niboRJ htiw detaerC"; private int numPoints = GRAPH_RESOLUTION; ! ! private boolean vLabelCentered = false; ! private long vLabelGridWidth = 0; ! private int imgWidth, imgHeight; // Dimensions of the entire image private int chartWidth, chartHeight; // Dimensions of the chart area within the image *************** *** 70,73 **** --- 73,77 ---- private double lowerValue = 0.0d, upperValue = 0.0d; + private RrdGraphDef graphDef; *************** *** 99,102 **** --- 103,108 ---- chartHeight = ( cHeight == 0 ? DEFAULT_HEIGHT : cHeight ); + if ( cWidth > GRAPH_RESOLUTION ) numPoints = cWidth; + // Calculate the complete image dimensions for the creation of the bufferedimage font_height = SUBTITLE_FONT.getSize(); // Determine font dimensions for regular comment font *************** *** 137,141 **** catch (Exception e) { ! throw new RrdException( e.getMessage() ); } --- 143,148 ---- catch (Exception e) { ! //throw new RrdException( e.getMessage() ); ! e.printStackTrace(); } *************** *** 169,179 **** g.drawLine( tmpx + 4, tmpy + 3, tmpx + 9, tmpy); for (int i = 0; i < timeList.length; i++) { ! secTime = timeList[i].timestamp / 1000; ! int posRel = chartGraph.getX(secTime); ! int pos = lux + posRel; ! if ( posRel > 2 ) { if ( timeList[i].isLabel() ) { --- 176,190 ---- g.drawLine( tmpx + 4, tmpy + 3, tmpx + 9, tmpy); + int pixWidth = 0; + if (vLabelCentered) + pixWidth = (chartGraph.getX( vLabelGridWidth ) - chartGraph.getX( 0 )); + for (int i = 0; i < timeList.length; i++) { ! secTime = timeList[i].timestamp / 1000; ! int posRel = chartGraph.getX(secTime); ! int pos = lux + posRel; ! if ( posRel >= 0 ) { if ( timeList[i].isLabel() ) { *************** *** 187,191 **** // Use extra 2 pixel padding (3 pixels from border total at least) int txtDistance = (timeList[i].text.length() * font_width) / 2; ! if ( (pos - lux > txtDistance + 2) && (pos + txtDistance + 2 < lux + chartWidth) ) { g.setColor( Color.BLACK ); --- 198,210 ---- // Use extra 2 pixel padding (3 pixels from border total at least) int txtDistance = (timeList[i].text.length() * font_width) / 2; ! ! if ( vLabelCentered ) ! { ! if ( pos + pixWidth <= lux + chartWidth ) { ! g.setColor( Color.BLACK ); ! g.drawString( timeList[i].text, pos + 2 + pixWidth/2 - txtDistance, luy + chartHeight + font_height + LINE_PADDING ); ! } ! } ! else if ( (pos - lux > txtDistance + 2) && (pos + txtDistance + 2 < lux + chartWidth) ) { g.setColor( Color.BLACK ); *************** *** 364,368 **** g.setYRange( lowerValue, upperValue ); ! plotChartGrid( g, tlist, vlist ); graphics.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); --- 383,387 ---- g.setYRange( lowerValue, upperValue ); ! //plotChartGrid( g, tlist, vlist ); graphics.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); *************** *** 380,389 **** g.setColor( plotDefs[i].getColor() ); ! switch ( plotDefs[i].getType() ) { case PlotDef.PLOT_LINE: drawLine( g, parentSeries, source, false ); lastPlotType = PlotDef.PLOT_LINE; break; case PlotDef.PLOT_AREA: --- 399,410 ---- g.setColor( plotDefs[i].getColor() ); ! switch ( plotDefs[i].getType() ) { case PlotDef.PLOT_LINE: + graphics.setStroke( new BasicStroke(plotDefs[i].getLineWidth()) ); drawLine( g, parentSeries, source, false ); lastPlotType = PlotDef.PLOT_LINE; + graphics.setStroke( new BasicStroke() ); break; case PlotDef.PLOT_AREA: *************** *** 404,407 **** --- 425,430 ---- graphics.setClip( 0, 0, imgWidth, imgHeight); graphics.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF ); + + plotChartGrid( g, tlist, vlist ); } *************** *** 416,423 **** int ax = 0, ay = 0; int nx = 0, ny = 0, last = -1; ! RrdSecond[] times = (RrdSecond[]) s.getSeries().times.toArray( new RrdSecond[0] ); Double[] values = (Double[]) s.getSeries().values.toArray( new Double[0] ); ! for (int i = 0; i < times.length; i++) { --- 439,446 ---- int ax = 0, ay = 0; int nx = 0, ny = 0, last = -1; ! RrdSecond[] times = (RrdSecond[]) s.getSeries().times.toArray( new RrdSecond[0] ); Double[] values = (Double[]) s.getSeries().values.toArray( new Double[0] ); ! for (int i = 0; i < times.length; i++) { *************** *** 427,431 **** ny += p[i]; ! g.drawLine( ax, ay, nx, ny ); p[i] = ny; --- 450,455 ---- ny += p[i]; ! if ( ax != 0 && ay != Integer.MIN_VALUE && ny != Integer.MIN_VALUE ) ! g.drawLine( ax, ay, nx, ny ); p[i] = ny; *************** *** 461,465 **** } ! g.drawLine( nx, py, nx, ny ); p[i] = ny; --- 485,490 ---- } ! if ( ax != 0 && py != Integer.MIN_VALUE && ny != Integer.MIN_VALUE ) ! g.drawLine( nx, py, nx, ny ); p[i] = ny; *************** *** 779,789 **** long endTime = graphDef.getEndTime(); double days = (endTime - startTime) / 86400.0; if(days <= 2.0 / 24.0) { t = new TimeAxisUnit( TimeAxisUnit.MINUTE, 10, new SimpleDateFormat("mm")); } ! else if (days <= 1) { ! //t = new TimeAxisUnit( TimeAxisUnit.HOUR, 6, new SimpleDateFormat("HH:mm")); t = new TimeAxisUnit( TimeAxisUnit.HOUR, 1, TimeAxisUnit.HOUR, 6, new SimpleDateFormat("HH:mm")); - } else if(days <= 2) { t = new TimeAxisUnit( TimeAxisUnit.HOUR, 2, new SimpleDateFormat("HH")); --- 804,814 ---- long endTime = graphDef.getEndTime(); double days = (endTime - startTime) / 86400.0; + + vLabelCentered = false; if(days <= 2.0 / 24.0) { t = new TimeAxisUnit( TimeAxisUnit.MINUTE, 10, new SimpleDateFormat("mm")); } ! else if (days <= 1) t = new TimeAxisUnit( TimeAxisUnit.HOUR, 1, TimeAxisUnit.HOUR, 6, new SimpleDateFormat("HH:mm")); else if(days <= 2) { t = new TimeAxisUnit( TimeAxisUnit.HOUR, 2, new SimpleDateFormat("HH")); *************** *** 795,803 **** t = new TimeAxisUnit( TimeAxisUnit.DAY, 1, new SimpleDateFormat("EEE dd MMM")); } ! else if(days <= 32) { ! t = new TimeAxisUnit( TimeAxisUnit.WEEK, 1, new SimpleDateFormat("'week' ww")); ! } else if(days <= 63) { ! t = new TimeAxisUnit( TimeAxisUnit.WEEK, 2, new SimpleDateFormat("dd")); } else if(days <= 120) { --- 820,831 ---- t = new TimeAxisUnit( TimeAxisUnit.DAY, 1, new SimpleDateFormat("EEE dd MMM")); } ! else if (days <= 32) { ! //t = new TimeAxisUnit( TimeAxisUnit.HOUR, 24, TimeAxisUnit.DAY, 1, new SimpleDateFormat("dd")); ! t = new TimeAxisUnit( TimeAxisUnit.DAY, 1, TimeAxisUnit.WEEK, 1, new SimpleDateFormat("'week' ww")); ! vLabelCentered = true; ! } else if(days <= 63) { ! t = new TimeAxisUnit( TimeAxisUnit.DAY, 1, TimeAxisUnit.WEEK, 1, new SimpleDateFormat("'week' ww")); ! //t = new TimeAxisUnit( TimeAxisUnit.WEEK, 2, new SimpleDateFormat("dd")); } else if(days <= 120) { *************** *** 807,811 **** t = new TimeAxisUnit( TimeAxisUnit.MONTH, 1, new SimpleDateFormat("MMM")); } ! return t.getTimeMarkers( graphDef.getStartTime(), graphDef.getEndTime() ); } --- 835,841 ---- t = new TimeAxisUnit( TimeAxisUnit.MONTH, 1, new SimpleDateFormat("MMM")); } ! ! vLabelGridWidth = t.getMajorGridWidth(); ! return t.getTimeMarkers( graphDef.getStartTime(), graphDef.getEndTime() ); } *************** *** 815,842 **** ValueAxisUnit v = null; - int shifted = 1; double range = upperValue - lowerValue; ! if ( range <= 5 ) ! v = new ValueAxisUnit( 1, 1, 1 ); ! else if ( range <= 20 ) ! v = new ValueAxisUnit( 5d, 2.5, 5 ); ! else if ( range <= 40 ) ! v = new ValueAxisUnit( 5d, 2.5, 5 ); ! else if ( range <= 80 ) ! v = new ValueAxisUnit( 20d, 10d, 20 ); ! else if ( range <= 100 ) ! v = new ValueAxisUnit( 1, 10d, 5, 10d ); ! //v = new ValueAxisUnit( 25d, 12.5, 20 ); ! else if ( range <= 2500 ) ! v = new ValueAxisUnit( 500d, 250d, 500 ); ! else if ( range <= 5000 ) ! v = new ValueAxisUnit( 500d, 125d, 500 ); else ! v = new ValueAxisUnit( 1000d, 500d, 1000 ); - upperValue = v.getNiceHigher( upperValue ); - lowerValue = v.getNiceLower( lowerValue ); - return v.getValueMarkers( lowerValue, upperValue ); } --- 845,873 ---- ValueAxisUnit v = null; double range = upperValue - lowerValue; ! double shifted = ( Math.abs(upperValue) > Math.abs(lowerValue) ? Math.abs(upperValue) : Math.abs(lowerValue) ); ! double mod = 1.0; ! while ( shifted > 10 ) { ! shifted /= 10; ! mod *= 10; ! } ! while ( shifted < 1 ) { ! shifted *= 10; ! mod /= 10; ! } ! ! if ( shifted <= 3 ) ! v = new ValueAxisUnit( 1, 0.2*mod, 1, 1.0*mod ); ! else if ( shifted <= 5 ) ! v = new ValueAxisUnit( 1, 0.5*mod, 1, 1.0*mod ); ! else if ( shifted <= 9 ) ! v = new ValueAxisUnit( 1, 0.5*mod, 1, 2.0*mod ); else ! v = new ValueAxisUnit( 1, 1.0*mod, 1, 5.0*mod ); ! ! upperValue = v.getNiceHigher( upperValue ); ! lowerValue = v.getNiceLower( lowerValue ); return v.getValueMarkers( lowerValue, upperValue ); } Index: ValueAxisUnit.java =================================================================== RCS file: /cvsroot/jrobin/src/jrobin/graph/ValueAxisUnit.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** ValueAxisUnit.java 22 Sep 2003 17:15:17 -0000 1.1 --- ValueAxisUnit.java 25 Sep 2003 21:58:05 -0000 1.2 *************** *** 63,67 **** this.mGridUnit = mGridUnit; this.mGridParts = mGridParts; ! gridStep = gridUnit * gridParts; mGridStep = mGridUnit * mGridParts; --- 63,67 ---- this.mGridUnit = mGridUnit; this.mGridParts = mGridParts; ! gridStep = gridUnit * gridParts; mGridStep = mGridUnit * mGridParts; *************** *** 86,90 **** } - System.out.println( gridStep + "::" + mGridStep ); // Now get all time markers. // Again we choose to use a series of loops as to avoid unnecessary drawing. --- 86,89 ---- *************** *** 147,150 **** --- 146,173 ---- public double getNiceLower( double value ) { + int valueInt = new Double(value).intValue(); + int roundStep = new Double(gridUnit * gridParts).intValue(); + int num = valueInt / roundStep; + int mod = valueInt % roundStep; + double gridValue = (roundStep * (num - 1)) * 1.0d; + if ( gridValue - value < (gridParts * gridUnit) / 4 ) + gridValue -= roundStep; + + roundStep = new Double(mGridUnit * mGridParts).intValue(); + num = valueInt / roundStep; + mod = valueInt % roundStep; + double mGridValue = (roundStep * (num - 1)) * 1.0d; + + if ( value != 0.0d ) + { + if ( mGridValue - gridValue < (mGridParts * mGridUnit) / 2) + return mGridValue; + else + return gridValue; + } + + return value; + + /* int valueInt = new Double(value).intValue(); int num = valueInt / roundStep; *************** *** 155,168 **** return value; } public double getNiceHigher( double value ) { ! int valueInt = new Double(value).intValue(); ! int num = valueInt / roundStep; ! int mod = valueInt % roundStep; if ( value != 0.0d ) ! return (roundStep * (num + 1)) * 1.0d; return value; --- 178,206 ---- return value; + */ } public double getNiceHigher( double value ) { ! int valueInt = new Double(value).intValue(); ! int roundStep = new Double(gridUnit * gridParts).intValue(); ! int num = valueInt / roundStep; ! int mod = valueInt % roundStep; ! double gridValue = (roundStep * (num + 1)) * 1.0d; ! if ( gridValue - value < (gridParts * gridUnit) / 4 ) ! gridValue += roundStep; ! ! roundStep = new Double(mGridUnit * mGridParts).intValue(); ! num = valueInt / roundStep; ! mod = valueInt % roundStep; ! double mGridValue = (roundStep * (num + 1)) * 1.0d; if ( value != 0.0d ) ! { ! if ( mGridValue - gridValue < (mGridParts * mGridUnit) / 2) ! return mGridValue; ! else ! return gridValue; ! } return value; Index: PlotDef.java =================================================================== RCS file: /cvsroot/jrobin/src/jrobin/graph/PlotDef.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** PlotDef.java 22 Sep 2003 17:15:17 -0000 1.2 --- PlotDef.java 25 Sep 2003 21:58:05 -0000 1.3 *************** *** 48,52 **** this.color = color; this.legend = legend; - System.out.println( legend ); } --- 48,51 ---- |