From: Tuukka Lehtonen Date: Thu, 26 Sep 2019 20:24:12 +0000 (+0300) Subject: Two rendering glitch fixes for time series charts X-Git-Tag: v1.43.0~136^2~62 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=78d831a19c254d829e45d04c5ec6a3057680b7d7;p=simantics%2Fplatform.git Two rendering glitch fixes for time series charts 1. Grid values were not rendered properly when the [yMin,yMax] interval gets too small (under 1e-6). The calculations in GridSpacing.makeGridSpacing contain a capping logic for parameter viewboxSize which doesn't necessarily have to be there (IMHO). The problem is essentially that auto-scaling the Y-axis allows zooming to any small value interval, but the vertical ruler rendering just doesn't work properly in those cases. 2. GridUtil.calcLabelWidth calculated the rendered Y-value labels differently from the rendering logic in GridUtil.paintVerticalRuler and GridUtil.paintVerticalSlaveRuler, i.e. in a more inprecise manner which results in the y-axis reserved width not following properly what is rendered. gitlab #392 Change-Id: I30998d9336e50606956a78ad547ee437e0439f1a --- diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/utils/GridSpacing.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/utils/GridSpacing.java index 3702bc4b1..a692b09ea 100644 --- a/bundles/org.simantics.g2d/src/org/simantics/g2d/utils/GridSpacing.java +++ b/bundles/org.simantics.g2d/src/org/simantics/g2d/utils/GridSpacing.java @@ -79,7 +79,8 @@ public final class GridSpacing implements Serializable { double minPixels) { if (controlWidth==0) controlWidth = 1; - if (Math.abs(viewboxSize) < GRID_MIN_USER_SIZE) viewboxSize = GRID_MIN_USER_SIZE * Math.signum(viewboxSize); + // This prevents clients from getting the proper GridSpacing information. + //if (Math.abs(viewboxSize) < GRID_MIN_USER_SIZE) viewboxSize = GRID_MIN_USER_SIZE * Math.signum(viewboxSize); double unitsPerPixel = viewboxSize / Math.max(controlWidth, minPixels); double [] gridSpacing = getGridSpacing(unitsPerPixel, minPixels); @@ -115,4 +116,16 @@ public final class GridSpacing implements Serializable { return GridUtils.snapToGrid(pos, segment); } + @Override + public String toString() { + return String.format("%s [minPixels=%.15f, segment=%.15f, segmentExp=%.15f, pixelsPerUnit=%.15f, unitsPerPixel=%.15f, pixelsPerSegment=%.15f]", + getClass().getName(), + minPixels, + segment, + segmentExp, + pixelsPerUnit, + unitsPerPixel, + pixelsPerSegment); + } + } diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/utils/GridUtil.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/utils/GridUtil.java index 8ee6c6a71..5114f1a66 100644 --- a/bundles/org.simantics.g2d/src/org/simantics/g2d/utils/GridUtil.java +++ b/bundles/org.simantics.g2d/src/org/simantics/g2d/utils/GridUtil.java @@ -213,8 +213,7 @@ public class GridUtil { double x = startY / grid.pixelsPerSegment; return (int) Math.ceil( x ); } - - + /** * Estimate label width, by sampling values from xMin to xMax * using given font and format. @@ -226,22 +225,36 @@ public class GridUtil { */ public static double calcLabelWidth(double xMin, double xMax, Format format, GridSpacing grid) { - double width = 0.0; - double canvasDiff = GridUtils.distanceToNextGridCoord(xMin, grid.segment); - int c = 0; - for (double x=xMin + canvasDiff; x -0.000000000001); - if (nearZero) x = 0.0; - String label = format.format(x); - if (label==null || label.equals("")) continue; - GlyphVector glyphVector; - glyphVector = RULER_FONT.createGlyphVector(frc, label); + double width = 0.0; + double canvasDiff = GridUtils.distanceToNextGridCoord(xMin, grid.segment); + int c = 0; + double canvasStart = xMin + canvasDiff; + for (double x=canvasStart; x -0.000000000001); +// if (nearZero) x = 0.0; + + String label = format.format(x); + if (label==null || label.equals("")) continue; + GlyphVector glyphVector; + glyphVector = RULER_FONT.createGlyphVector(frc, label); double labelWidth = glyphVector.getVisualBounds().getWidth(); + //System.out.format("calcLabelWidth[%d](%s, %f, %f)%n", c, label, labelWidth, width); width = Math.max( width, labelWidth ); - if (c++ > 100) break; - } - return width * 1.05; + if (c++ > 100) break; + + // It is floating-point-wise more precise to calculate x like this than + // accumulating x += grid.segment. + // Also, this is how paintVerticalRuler does it and we want to match that + // in order to calculate the label width from the exact same values that + // paintVerticalRuler will draw. + x = canvasStart + grid.segment * c; + } + return width * 1.05; } - - + }