From 437a739898ba0eb782c36a834b0b83c3b27c2be4 Mon Sep 17 00:00:00 2001 From: yu_xh <940151214@qq.com> Date: Fri, 24 Sep 2021 14:25:29 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E5=AD=97=E5=A4=A7=E5=B0=8F=E4=B8=BA=E8=B4=9F=E6=95=B0?= =?UTF-8?q?=E6=97=B6=E5=AF=BC=E8=87=B4=E7=9A=84=E7=A8=8B=E5=BA=8F=E5=B4=A9?= =?UTF-8?q?=E6=BA=83=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jjoe64/graphview/GridLabelRenderer.java | 2704 +++++++++-------- .../com/jjoe64/graphview/LegendRenderer.java | 37 +- 2 files changed, 1378 insertions(+), 1363 deletions(-) diff --git a/graphveiw/src/main/java/com/jjoe64/graphview/GridLabelRenderer.java b/graphveiw/src/main/java/com/jjoe64/graphview/GridLabelRenderer.java index de79c6b..8e534e8 100644 --- a/graphveiw/src/main/java/com/jjoe64/graphview/GridLabelRenderer.java +++ b/graphveiw/src/main/java/com/jjoe64/graphview/GridLabelRenderer.java @@ -374,1556 +374,1566 @@ public class GridLabelRenderer { int size; int size2; - color1 = Color.BLACK.getValue(); - color2 = Color.GRAY.getValue(); - size = 20; - size2 = 20; - - mStyles.verticalLabelsColor = color1; - mStyles.verticalLabelsSecondScaleColor = color1; - mStyles.horizontalLabelsColor = color1; - mStyles.gridColor = color2; - mStyles.textSize = size; - mStyles.padding = size2; - mStyles.labelsSpace = (int) mStyles.textSize / 5; - - mStyles.verticalLabelsAlign = 2; - mStyles.verticalLabelsSecondScaleAlign = 0; - mStyles.highlightZeroLines = true; - - mStyles.verticalAxisTitleColor = mStyles.verticalLabelsColor; - mStyles.horizontalAxisTitleColor = mStyles.horizontalLabelsColor; - mStyles.verticalAxisTitleTextSize = mStyles.textSize; - mStyles.horizontalAxisTitleTextSize = mStyles.textSize; - - mStyles.horizontalLabelsVisible = true; - mStyles.verticalLabelsVisible = true; - - mStyles.horizontalLabelsAngle = 0f; - - mStyles.gridStyle = GridStyle.BOTH; - reloadStyles(); - } + color1 = Color.BLACK.getValue(); + color2 = Color.GRAY.getValue(); + size = 20; + size2 = 20; + + mStyles.verticalLabelsColor = color1; + mStyles.verticalLabelsSecondScaleColor = color1; + mStyles.horizontalLabelsColor = color1; + mStyles.gridColor = color2; + mStyles.textSize = size; + mStyles.padding = size2; + mStyles.labelsSpace = (int) mStyles.textSize / 5; + + mStyles.verticalLabelsAlign = 2; + mStyles.verticalLabelsSecondScaleAlign = 0; + mStyles.highlightZeroLines = true; + + mStyles.verticalAxisTitleColor = mStyles.verticalLabelsColor; + mStyles.horizontalAxisTitleColor = mStyles.horizontalLabelsColor; + mStyles.verticalAxisTitleTextSize = mStyles.textSize; + mStyles.horizontalAxisTitleTextSize = mStyles.textSize; + + mStyles.horizontalLabelsVisible = true; + mStyles.verticalLabelsVisible = true; + + mStyles.horizontalLabelsAngle = 0f; + + mStyles.gridStyle = GridStyle.BOTH; + reloadStyles(); + } - /** - * will load the styles to the internal - * paint objects (color, text size, text align) - */ - public void reloadStyles () { - mPaintLine = new Paint(); - mPaintLine.setColor(new Color(mStyles.gridColor)); - mPaintLine.setStrokeWidth(0); - - mPaintLabel = new Paint(); - mPaintLabel.setTextSize((int) getTextSize()); - mPaintLabel.setAntiAlias(true); - - mPaintAxisTitle = new Paint(); - mPaintAxisTitle.setTextSize((int) getTextSize()); - mPaintAxisTitle.setTextAlign(1); - } + /** + * will load the styles to the internal + * paint objects (color, text size, text align) + */ + public void reloadStyles() { + mPaintLine = new Paint(); + mPaintLine.setColor(new Color(mStyles.gridColor)); + mPaintLine.setStrokeWidth(0); + + mPaintLabel = new Paint(); + mPaintLabel.setTextSize((int) getTextSize()); + mPaintLabel.setAntiAlias(true); + + mPaintAxisTitle = new Paint(); + mPaintAxisTitle.setTextSize((int) getTextSize()); + mPaintAxisTitle.setTextAlign(1); + } - /** - * GraphView tries to fit the labels - * to display numbers that can be divided by 1, 2, or 5. - *
- * By default this is enabled. It makes sense to deactivate it - * when using Dates on the x axis. - * - * @return if human rounding is enabled - */ - public boolean isHumanRoundingX () { - return mHumanRoundingX; - } + /** + * GraphView tries to fit the labels + * to display numbers that can be divided by 1, 2, or 5. + *
+ * By default this is enabled. It makes sense to deactivate it + * when using Dates on the x axis. + * + * @return if human rounding is enabled + */ + public boolean isHumanRoundingX() { + return mHumanRoundingX; + } - /** - * GraphView tries to fit the labels - * to display numbers that can be divided by 1, 2, or 5. - * - * @return if human rounding is enabled - */ - public boolean isHumanRoundingY () { - return mHumanRoundingY; - } + /** + * GraphView tries to fit the labels + * to display numbers that can be divided by 1, 2, or 5. + * + * @return if human rounding is enabled + */ + public boolean isHumanRoundingY() { + return mHumanRoundingY; + } - /** - * activate or deactivate human rounding of the - * horizontal axis. GraphView tries to fit the labels - * to display numbers that can be divided by 1, 2, or 5. - *
- * By default this is enabled. It makes sense to deactivate it - * when using Dates on the x axis. - * - * @param humanRoundingX false to deactivate - * @param humanRoundingY false to deactivate - */ - public void setHumanRounding ( boolean humanRoundingX, boolean humanRoundingY){ - this.mHumanRoundingX = humanRoundingX; - this.mHumanRoundingY = humanRoundingY; - } + /** + * activate or deactivate human rounding of the + * horizontal axis. GraphView tries to fit the labels + * to display numbers that can be divided by 1, 2, or 5. + *
+ * By default this is enabled. It makes sense to deactivate it + * when using Dates on the x axis. + * + * @param humanRoundingX false to deactivate + * @param humanRoundingY false to deactivate + */ + public void setHumanRounding(boolean humanRoundingX, boolean humanRoundingY) { + this.mHumanRoundingX = humanRoundingX; + this.mHumanRoundingY = humanRoundingY; + } - /** - * activate or deactivate human rounding of the - * horizontal axis. GraphView tries to fit the labels - * to display numbers that can be divided by 1, 2, or 5. - *
- * By default this is enabled. - * - * @param humanRoundingBoth false to deactivate on both axises - */ - public void setHumanRounding ( boolean humanRoundingBoth){ - this.mHumanRoundingX = humanRoundingBoth; - this.mHumanRoundingY = humanRoundingBoth; - } + /** + * activate or deactivate human rounding of the + * horizontal axis. GraphView tries to fit the labels + * to display numbers that can be divided by 1, 2, or 5. + *
+ * By default this is enabled.
+ *
+ * @param humanRoundingBoth false to deactivate on both axises
+ */
+ public void setHumanRounding(boolean humanRoundingBoth) {
+ this.mHumanRoundingX = humanRoundingBoth;
+ this.mHumanRoundingY = humanRoundingBoth;
+ }
- /**
- * ss
- *
- * @return the general text size for the axis titles
- */
- public float getTextSize () {
- return mStyles.textSize;
- }
+ /**
+ * ss
+ *
+ * @return the general text size for the axis titles
+ */
+ public float getTextSize() {
+ return mStyles.textSize;
+ }
- /**
- * ss
- *
- * @return the font color of the vertical labels
- */
- public int getVerticalLabelsColor () {
- return mStyles.verticalLabelsColor;
- }
+ /**
+ * ss
+ *
+ * @return the font color of the vertical labels
+ */
+ public int getVerticalLabelsColor() {
+ return mStyles.verticalLabelsColor;
+ }
- /**
- * ss
- *
- * @return the alignment of the text of the
- * vertical labels
- */
- public int getVerticalLabelsAlign () {
- return mStyles.verticalLabelsAlign;
- }
+ /**
+ * ss
+ *
+ * @return the alignment of the text of the
+ * vertical labels
+ */
+ public int getVerticalLabelsAlign() {
+ return mStyles.verticalLabelsAlign;
+ }
- /**
- * ss
- *
- * @return the font color of the horizontal labels
- */
- public int getHorizontalLabelsColor () {
- return mStyles.horizontalLabelsColor;
- }
+ /**
+ * ss
+ *
+ * @return the font color of the horizontal labels
+ */
+ public int getHorizontalLabelsColor() {
+ return mStyles.horizontalLabelsColor;
+ }
- /**
- * ss
- *
- * @return the angle of the horizontal labels
- */
- public float getHorizontalLabelsAngle () {
- return mStyles.horizontalLabelsAngle;
- }
+ /**
+ * ss
+ *
+ * @return the angle of the horizontal labels
+ */
+ public float getHorizontalLabelsAngle() {
+ return mStyles.horizontalLabelsAngle;
+ }
- /**
- * clears the internal cache and forces
- * to redraw the grid and labels.
- * Normally you should always call {@link GraphView#onDataChanged(boolean, boolean)}
- * which will call this method.
- *
- * @param keepLabelsSize true if you don't want
- * to recalculate the size of
- * the labels. It is recommended
- * to use "true" because this will
- * improve performance and prevent
- * a flickering.
- * @param keepViewport true if you don't want that
- * the viewport will be recalculated.
- * It is recommended to use "true" for
- * performance.
- */
- public void invalidate ( boolean keepLabelsSize, boolean keepViewport){
- if (!keepViewport) {
- mIsAdjusted = false;
- }
- if (!keepLabelsSize) {
- if (!mLabelVerticalWidthFixed) {
- mLabelVerticalWidth = null;
- }
- mLabelVerticalHeight = null;
- mLabelVerticalSecondScaleWidth = null;
- mLabelVerticalSecondScaleHeight = null;
+ /**
+ * clears the internal cache and forces
+ * to redraw the grid and labels.
+ * Normally you should always call {@link GraphView#onDataChanged(boolean, boolean)}
+ * which will call this method.
+ *
+ * @param keepLabelsSize true if you don't want
+ * to recalculate the size of
+ * the labels. It is recommended
+ * to use "true" because this will
+ * improve performance and prevent
+ * a flickering.
+ * @param keepViewport true if you don't want that
+ * the viewport will be recalculated.
+ * It is recommended to use "true" for
+ * performance.
+ */
+ public void invalidate(boolean keepLabelsSize, boolean keepViewport) {
+ if (!keepViewport) {
+ mIsAdjusted = false;
+ }
+ if (!keepLabelsSize) {
+ if (!mLabelVerticalWidthFixed) {
+ mLabelVerticalWidth = null;
}
- //reloadStyles();
+ mLabelVerticalHeight = null;
+ mLabelVerticalSecondScaleWidth = null;
+ mLabelVerticalSecondScaleHeight = null;
}
+ //reloadStyles();
+ }
- /**
- * calculates the vertical steps of
- * the second scale.
- * This will not do any automatically update
- * of the bounds.
- * Use always manual bounds for the second scale.
- *
- * @return true if it is ready
- */
- protected boolean adjustVerticalSecondScale () {
- if (mLabelHorizontalHeight == null) {
- return false;
- }
- if (mGraphView.mSecondScale == null) {
- return true;
- }
+ /**
+ * calculates the vertical steps of
+ * the second scale.
+ * This will not do any automatically update
+ * of the bounds.
+ * Use always manual bounds for the second scale.
+ *
+ * @return true if it is ready
+ */
+ protected boolean adjustVerticalSecondScale() {
+ if (mLabelHorizontalHeight == null) {
+ return false;
+ }
+ if (mGraphView.mSecondScale == null) {
+ return true;
+ }
- double minY = mGraphView.mSecondScale.getMinY(false);
- double maxY = mGraphView.mSecondScale.getMaxY(false);
+ double minY = mGraphView.mSecondScale.getMinY(false);
+ double maxY = mGraphView.mSecondScale.getMaxY(false);
- // TODO find the number of labels
- int numVerticalLabels = mNumVerticalLabels;
+ // TODO find the number of labels
+ int numVerticalLabels = mNumVerticalLabels;
- double newMinY;
- double exactSteps;
+ double newMinY;
+ double exactSteps;
- if (mGraphView.mSecondScale.isYAxisBoundsManual()) {
- // split range into equal steps
- exactSteps = (maxY - minY) / (numVerticalLabels - 1);
+ if (mGraphView.mSecondScale.isYAxisBoundsManual()) {
+ // split range into equal steps
+ exactSteps = (maxY - minY) / (numVerticalLabels - 1);
- // round because of floating error
- exactSteps = Math.round(exactSteps * 1000000d) / 1000000d;
- } else {
- // TODO auto adjusting
- throw new IllegalStateException("Not yet implemented");
- }
+ // round because of floating error
+ exactSteps = Math.round(exactSteps * 1000000d) / 1000000d;
+ } else {
+ // TODO auto adjusting
+ throw new IllegalStateException("Not yet implemented");
+ }
- if (mStepsVerticalSecondScale != null && mStepsVerticalSecondScale.size() > 1) {
- // else choose other nice steps that previous
- // steps are included (divide to have more, or multiplicate to have less)
+ if (mStepsVerticalSecondScale != null && mStepsVerticalSecondScale.size() > 1) {
+ // else choose other nice steps that previous
+ // steps are included (divide to have more, or multiplicate to have less)
- double d1 = 0, d2 = 0;
- int i = 0;
- for (Double v : mStepsVerticalSecondScale.values()) {
- if (i == 0) {
- d1 = v;
- } else {
- d2 = v;
- break;
- }
- i++;
+ double d1 = 0, d2 = 0;
+ int i = 0;
+ for (Double v : mStepsVerticalSecondScale.values()) {
+ if (i == 0) {
+ d1 = v;
+ } else {
+ d2 = v;
+ break;
+ }
+ i++;
+ }
+ double oldSteps = d2 - d1;
+ if (oldSteps > 0) {
+ double newSteps = Double.NaN;
+
+ if (oldSteps > exactSteps) {
+ newSteps = oldSteps / 2;
+ } else if (oldSteps < exactSteps) {
+ newSteps = oldSteps * 2;
}
- double oldSteps = d2 - d1;
- if (oldSteps > 0) {
- double newSteps = Double.NaN;
-
- if (oldSteps > exactSteps) {
- newSteps = oldSteps / 2;
- } else if (oldSteps < exactSteps) {
- newSteps = oldSteps * 2;
- }
- // only if there wont be more than numLabels
- // and newSteps will be better than oldSteps
- int numStepsOld = (int) ((maxY - minY) / oldSteps);
- int numStepsNew = (int) ((maxY - minY) / newSteps);
+ // only if there wont be more than numLabels
+ // and newSteps will be better than oldSteps
+ int numStepsOld = (int) ((maxY - minY) / oldSteps);
+ int numStepsNew = (int) ((maxY - minY) / newSteps);
- boolean shouldChange;
+ boolean shouldChange;
- // avoid switching between 2 steps
- if (numStepsOld <= numVerticalLabels && numStepsNew <= numVerticalLabels) {
- // both are possible
- // only the new if it hows more labels
- shouldChange = numStepsNew > numStepsOld;
- } else {
- shouldChange = true;
- }
+ // avoid switching between 2 steps
+ if (numStepsOld <= numVerticalLabels && numStepsNew <= numVerticalLabels) {
+ // both are possible
+ // only the new if it hows more labels
+ shouldChange = numStepsNew > numStepsOld;
+ } else {
+ shouldChange = true;
+ }
- if (newSteps != Double.NaN && shouldChange && numStepsNew <= numVerticalLabels) {
- exactSteps = newSteps;
- } else {
- // try to stay to the old steps
- exactSteps = oldSteps;
- }
+ if (newSteps != Double.NaN && shouldChange && numStepsNew <= numVerticalLabels) {
+ exactSteps = newSteps;
+ } else {
+ // try to stay to the old steps
+ exactSteps = oldSteps;
}
- } else {
- // first time
- LogUtil.debug("find","find");
}
+ } else {
+ // first time
+ LogUtil.debug("find", "find");
+ }
- // find the first data point that is relevant to display
- // starting from 1st datapoint so that the steps have nice numbers
- // goal is to start with the minY or 1 step before
- newMinY = mGraphView.getSecondScale().mReferenceY;
- // must be down-rounded
- double count = Math.floor((minY - newMinY) / exactSteps);
- newMinY = count * exactSteps + newMinY;
+ // find the first data point that is relevant to display
+ // starting from 1st datapoint so that the steps have nice numbers
+ // goal is to start with the minY or 1 step before
+ newMinY = mGraphView.getSecondScale().mReferenceY;
+ // must be down-rounded
+ double count = Math.floor((minY - newMinY) / exactSteps);
+ newMinY = count * exactSteps + newMinY;
- // it can happen that we need to add some more labels to fill the complete screen
- numVerticalLabels = (int) ((mGraphView.getSecondScale().mCurrentViewport.height() * -1 / exactSteps)) + 2;
+ // it can happen that we need to add some more labels to fill the complete screen
+ numVerticalLabels = (int) ((mGraphView.getSecondScale().mCurrentViewport.height() * -1 / exactSteps)) + 2;
- // ensure that the value is valid (minimum 2)
- numVerticalLabels = Math.max(numVerticalLabels, 2);
+ // ensure that the value is valid (minimum 2)
+ numVerticalLabels = Math.max(numVerticalLabels, 2);
- if (mStepsVerticalSecondScale != null) {
- mStepsVerticalSecondScale.clear();
- } else {
- mStepsVerticalSecondScale = new LinkedHashMap<>(numVerticalLabels);
- }
+ if (mStepsVerticalSecondScale != null) {
+ mStepsVerticalSecondScale.clear();
+ } else {
+ mStepsVerticalSecondScale = new LinkedHashMap<>(numVerticalLabels);
+ }
- int height = mGraphView.getGraphContentHeight();
- // convert data-y to pixel-y in current viewport
- double pixelPerData = height / mGraphView.getSecondScale().mCurrentViewport.height() * -1;
+ int height = mGraphView.getGraphContentHeight();
+ // convert data-y to pixel-y in current viewport
+ double pixelPerData = height / mGraphView.getSecondScale().mCurrentViewport.height() * -1;
- for (int i = 0; i < numVerticalLabels; i++) {
- // dont draw if it is top of visible screen
- if (newMinY + (i * exactSteps) > mGraphView.getSecondScale().mCurrentViewport.top) {
- continue;
- }
- // dont draw if it is below of visible screen
- if (newMinY + (i * exactSteps) < mGraphView.getSecondScale().mCurrentViewport.bottom) {
- continue;
- }
+ for (int i = 0; i < numVerticalLabels; i++) {
+ // dont draw if it is top of visible screen
+ if (newMinY + (i * exactSteps) > mGraphView.getSecondScale().mCurrentViewport.top) {
+ continue;
+ }
+ // dont draw if it is below of visible screen
+ if (newMinY + (i * exactSteps) < mGraphView.getSecondScale().mCurrentViewport.bottom) {
+ continue;
+ }
- // where is the data point on the current screen
- double dataPointPos = newMinY + (i * exactSteps);
- double relativeToCurrentViewport = dataPointPos - mGraphView.getSecondScale().mCurrentViewport.bottom;
+ // where is the data point on the current screen
+ double dataPointPos = newMinY + (i * exactSteps);
+ double relativeToCurrentViewport = dataPointPos - mGraphView.getSecondScale().mCurrentViewport.bottom;
- double pixelPos = relativeToCurrentViewport * pixelPerData;
- mStepsVerticalSecondScale.put((int) pixelPos, dataPointPos);
- }
+ double pixelPos = relativeToCurrentViewport * pixelPerData;
+ mStepsVerticalSecondScale.put((int) pixelPos, dataPointPos);
+ }
- return true;
+ return true;
+ }
+
+ /**
+ * calculates the vertical steps. This will
+ * automatically change the bounds to nice
+ * human-readable min/max.
+ *
+ * @param changeBounds str
+ * @return true if it is ready
+ */
+ protected boolean adjustVertical(boolean changeBounds) {
+ if (mLabelHorizontalHeight == null) {
+ return false;
}
- /**
- * calculates the vertical steps. This will
- * automatically change the bounds to nice
- * human-readable min/max.
- *
- * @param changeBounds str
- * @return true if it is ready
- */
- protected boolean adjustVertical ( boolean changeBounds){
- if (mLabelHorizontalHeight == null) {
- return false;
- }
+ double minY = mGraphView.getViewport().getMinY(false);
+ double maxY = mGraphView.getViewport().getMaxY(false);
- double minY = mGraphView.getViewport().getMinY(false);
- double maxY = mGraphView.getViewport().getMaxY(false);
+ if (minY == maxY) {
+ return false;
+ }
- if (minY == maxY) {
- return false;
- }
+ // TODO find the number of labels
+ int numVerticalLabels = mNumVerticalLabels;
- // TODO find the number of labels
- int numVerticalLabels = mNumVerticalLabels;
+ double newMinY;
+ double exactSteps;
- double newMinY;
- double exactSteps;
+ // split range into equal steps
+ exactSteps = (maxY - minY) / (numVerticalLabels - 1);
- // split range into equal steps
- exactSteps = (maxY - minY) / (numVerticalLabels - 1);
+ // round because of floating error
+ exactSteps = Math.round(exactSteps * 1000000d) / 1000000d;
- // round because of floating error
- exactSteps = Math.round(exactSteps * 1000000d) / 1000000d;
+ // smallest viewport
+ if (exactSteps == 0d) {
+ exactSteps = 0.0000001d;
+ maxY = minY + exactSteps * (numVerticalLabels - 1);
+ }
- // smallest viewport
- if (exactSteps == 0d) {
- exactSteps = 0.0000001d;
- maxY = minY + exactSteps * (numVerticalLabels - 1);
- }
+ // human rounding to have nice numbers (1, 2, 5, ...)
+ if (isHumanRoundingY()) {
+ exactSteps = humanRound(exactSteps, changeBounds);
+ } else if (mStepsVertical != null && mStepsVertical.size() > 1) {
+ // else choose other nice steps that previous
+ // steps are included (divide to have more, or multiplicate to have less)
- // human rounding to have nice numbers (1, 2, 5, ...)
- if (isHumanRoundingY()) {
- exactSteps = humanRound(exactSteps, changeBounds);
- } else if (mStepsVertical != null && mStepsVertical.size() > 1) {
- // else choose other nice steps that previous
- // steps are included (divide to have more, or multiplicate to have less)
-
- double d1 = 0, d2 = 0;
- int i = 0;
- for (Double v : mStepsVertical.values()) {
- if (i == 0) {
- d1 = v;
- } else {
- d2 = v;
- break;
- }
- i++;
+ double d1 = 0, d2 = 0;
+ int i = 0;
+ for (Double v : mStepsVertical.values()) {
+ if (i == 0) {
+ d1 = v;
+ } else {
+ d2 = v;
+ break;
+ }
+ i++;
+ }
+ double oldSteps = d2 - d1;
+ if (oldSteps > 0) {
+ double newSteps = Double.NaN;
+
+ if (oldSteps > exactSteps) {
+ newSteps = oldSteps / 2;
+ } else if (oldSteps < exactSteps) {
+ newSteps = oldSteps * 2;
}
- double oldSteps = d2 - d1;
- if (oldSteps > 0) {
- double newSteps = Double.NaN;
-
- if (oldSteps > exactSteps) {
- newSteps = oldSteps / 2;
- } else if (oldSteps < exactSteps) {
- newSteps = oldSteps * 2;
- }
- // only if there wont be more than numLabels
- // and newSteps will be better than oldSteps
- int numStepsOld = (int) ((maxY - minY) / oldSteps);
- int numStepsNew = (int) ((maxY - minY) / newSteps);
+ // only if there wont be more than numLabels
+ // and newSteps will be better than oldSteps
+ int numStepsOld = (int) ((maxY - minY) / oldSteps);
+ int numStepsNew = (int) ((maxY - minY) / newSteps);
- boolean shouldChange;
+ boolean shouldChange;
- // avoid switching between 2 steps
- if (numStepsOld <= numVerticalLabels && numStepsNew <= numVerticalLabels) {
- // both are possible
- // only the new if it hows more labels
- shouldChange = numStepsNew > numStepsOld;
- } else {
- shouldChange = true;
- }
+ // avoid switching between 2 steps
+ if (numStepsOld <= numVerticalLabels && numStepsNew <= numVerticalLabels) {
+ // both are possible
+ // only the new if it hows more labels
+ shouldChange = numStepsNew > numStepsOld;
+ } else {
+ shouldChange = true;
+ }
- if (newSteps != Double.NaN && shouldChange && numStepsNew <= numVerticalLabels) {
- exactSteps = newSteps;
- } else {
- // try to stay to the old steps
- exactSteps = oldSteps;
- }
+ if (newSteps != Double.NaN && shouldChange && numStepsNew <= numVerticalLabels) {
+ exactSteps = newSteps;
+ } else {
+ // try to stay to the old steps
+ exactSteps = oldSteps;
}
- } else {
- // first time
- LogUtil.debug("find","find");
}
+ } else {
+ // first time
+ LogUtil.debug("find", "find");
+ }
- // find the first data point that is relevant to display
- // starting from 1st datapoint so that the steps have nice numbers
- // goal is to start with the minX or 1 step before
- newMinY = mGraphView.getViewport().getReferenceY();
- // must be down-rounded
- double count = Math.floor((minY - newMinY) / exactSteps);
- newMinY = count * exactSteps + newMinY;
-
- // now we have our labels bounds
- if (changeBounds) {
- mGraphView.getViewport().setMinY(newMinY);
- mGraphView.getViewport().setMaxY(Math.max(maxY, newMinY + (numVerticalLabels - 1) * exactSteps));
- mGraphView.getViewport().mYAxisBoundsStatus = Viewport.AxisBoundsStatus.AUTO_ADJUSTED;
- }
+ // find the first data point that is relevant to display
+ // starting from 1st datapoint so that the steps have nice numbers
+ // goal is to start with the minX or 1 step before
+ newMinY = mGraphView.getViewport().getReferenceY();
+ // must be down-rounded
+ double count = Math.floor((minY - newMinY) / exactSteps);
+ newMinY = count * exactSteps + newMinY;
- // it can happen that we need to add some more labels to fill the complete screen
- numVerticalLabels = (int) ((mGraphView.getViewport().mCurrentViewport.height() * -1 / exactSteps)) + 2;
+ // now we have our labels bounds
+ if (changeBounds) {
+ mGraphView.getViewport().setMinY(newMinY);
+ mGraphView.getViewport().setMaxY(Math.max(maxY, newMinY + (numVerticalLabels - 1) * exactSteps));
+ mGraphView.getViewport().mYAxisBoundsStatus = Viewport.AxisBoundsStatus.AUTO_ADJUSTED;
+ }
- if (mStepsVertical != null) {
- mStepsVertical.clear();
- } else {
- mStepsVertical = new LinkedHashMap<>((int) numVerticalLabels);
- }
+ // it can happen that we need to add some more labels to fill the complete screen
+ numVerticalLabels = (int) ((mGraphView.getViewport().mCurrentViewport.height() * -1 / exactSteps)) + 2;
- int height = mGraphView.getGraphContentHeight();
- // convert data-y to pixel-y in current viewport
- double pixelPerData = height / mGraphView.getViewport().mCurrentViewport.height() * -1;
+ if (mStepsVertical != null) {
+ mStepsVertical.clear();
+ } else {
+ mStepsVertical = new LinkedHashMap<>((int) numVerticalLabels);
+ }
- for (int i = 0; i < numVerticalLabels; i++) {
- // dont draw if it is top of visible screen
- if (newMinY + (i * exactSteps) > mGraphView.getViewport().mCurrentViewport.top) {
- continue;
- }
- // dont draw if it is below of visible screen
- if (newMinY + (i * exactSteps) < mGraphView.getViewport().mCurrentViewport.bottom) {
- continue;
- }
+ int height = mGraphView.getGraphContentHeight();
+ // convert data-y to pixel-y in current viewport
+ double pixelPerData = height / mGraphView.getViewport().mCurrentViewport.height() * -1;
+ for (int i = 0; i < numVerticalLabels; i++) {
+ // dont draw if it is top of visible screen
+ if (newMinY + (i * exactSteps) > mGraphView.getViewport().mCurrentViewport.top) {
+ continue;
+ }
+ // dont draw if it is below of visible screen
+ if (newMinY + (i * exactSteps) < mGraphView.getViewport().mCurrentViewport.bottom) {
+ continue;
+ }
- // where is the data point on the current screen
- double dataPointPos = newMinY + (i * exactSteps);
- double relativeToCurrentViewport = dataPointPos - mGraphView.getViewport().mCurrentViewport.bottom;
- double pixelPos = relativeToCurrentViewport * pixelPerData;
- mStepsVertical.put((int) pixelPos, dataPointPos);
- }
+ // where is the data point on the current screen
+ double dataPointPos = newMinY + (i * exactSteps);
+ double relativeToCurrentViewport = dataPointPos - mGraphView.getViewport().mCurrentViewport.bottom;
- return true;
+ double pixelPos = relativeToCurrentViewport * pixelPerData;
+ mStepsVertical.put((int) pixelPos, dataPointPos);
}
- /**
- * calculates the horizontal steps.
- *
- * @param changeBounds This will automatically change the
- * bounds to nice human-readable min/max.
- * @return true if it is ready
- */
- protected boolean adjustHorizontal ( boolean changeBounds){
- if (mLabelVerticalWidth == null) {
- return false;
- }
+ return true;
+ }
- double minX = mGraphView.getViewport().getMinX(false);
- double maxX = mGraphView.getViewport().getMaxX(false);
- if (minX == maxX) return false;
+ /**
+ * calculates the horizontal steps.
+ *
+ * @param changeBounds This will automatically change the
+ * bounds to nice human-readable min/max.
+ * @return true if it is ready
+ */
+ protected boolean adjustHorizontal(boolean changeBounds) {
+ if (mLabelVerticalWidth == null) {
+ return false;
+ }
- // TODO find the number of labels
- int numHorizontalLabels = mNumHorizontalLabels;
+ double minX = mGraphView.getViewport().getMinX(false);
+ double maxX = mGraphView.getViewport().getMaxX(false);
+ if (minX == maxX) return false;
- double newMinX;
- double exactSteps;
+ // TODO find the number of labels
+ int numHorizontalLabels = mNumHorizontalLabels;
- // split range into equal steps
- exactSteps = (maxX - minX) / (numHorizontalLabels - 1);
+ double newMinX;
+ double exactSteps;
- // round because of floating error
- exactSteps = Math.round(exactSteps * 1000000d) / 1000000d;
+ // split range into equal steps
+ exactSteps = (maxX - minX) / (numHorizontalLabels - 1);
- // smallest viewport
- if (exactSteps == 0d) {
- exactSteps = 0.0000001d;
- maxX = minX + exactSteps * (numHorizontalLabels - 1);
- }
+ // round because of floating error
+ exactSteps = Math.round(exactSteps * 1000000d) / 1000000d;
- // human rounding to have nice numbers (1, 2, 5, ...)
- if (isHumanRoundingX()) {
- exactSteps = humanRound(exactSteps, false);
- } else if (mStepsHorizontal != null && mStepsHorizontal.size() > 1) {
- // else choose other nice steps that previous
- // steps are included (divide to have more, or multiplicate to have less)
-
- double d1 = 0, d2 = 0;
- int i = 0;
- for (Double v : mStepsHorizontal.values()) {
- if (i == 0) {
- d1 = v;
- } else {
- d2 = v;
- break;
- }
- i++;
+ // smallest viewport
+ if (exactSteps == 0d) {
+ exactSteps = 0.0000001d;
+ maxX = minX + exactSteps * (numHorizontalLabels - 1);
+ }
+
+ // human rounding to have nice numbers (1, 2, 5, ...)
+ if (isHumanRoundingX()) {
+ exactSteps = humanRound(exactSteps, false);
+ } else if (mStepsHorizontal != null && mStepsHorizontal.size() > 1) {
+ // else choose other nice steps that previous
+ // steps are included (divide to have more, or multiplicate to have less)
+
+ double d1 = 0, d2 = 0;
+ int i = 0;
+ for (Double v : mStepsHorizontal.values()) {
+ if (i == 0) {
+ d1 = v;
+ } else {
+ d2 = v;
+ break;
+ }
+ i++;
+ }
+ double oldSteps = d2 - d1;
+ if (oldSteps > 0) {
+ double newSteps = Double.NaN;
+
+ if (oldSteps > exactSteps) {
+ newSteps = oldSteps / 2;
+ } else if (oldSteps < exactSteps) {
+ newSteps = oldSteps * 2;
}
- double oldSteps = d2 - d1;
- if (oldSteps > 0) {
- double newSteps = Double.NaN;
-
- if (oldSteps > exactSteps) {
- newSteps = oldSteps / 2;
- } else if (oldSteps < exactSteps) {
- newSteps = oldSteps * 2;
- }
- // only if there wont be more than numLabels
- // and newSteps will be better than oldSteps
- int numStepsOld = (int) ((maxX - minX) / oldSteps);
- int numStepsNew = (int) ((maxX - minX) / newSteps);
+ // only if there wont be more than numLabels
+ // and newSteps will be better than oldSteps
+ int numStepsOld = (int) ((maxX - minX) / oldSteps);
+ int numStepsNew = (int) ((maxX - minX) / newSteps);
- boolean shouldChange;
+ boolean shouldChange;
- // avoid switching between 2 steps
- if (numStepsOld <= numHorizontalLabels && numStepsNew <= numHorizontalLabels) {
- // both are possible
- // only the new if it hows more labels
- shouldChange = numStepsNew > numStepsOld;
- } else {
- shouldChange = true;
- }
+ // avoid switching between 2 steps
+ if (numStepsOld <= numHorizontalLabels && numStepsNew <= numHorizontalLabels) {
+ // both are possible
+ // only the new if it hows more labels
+ shouldChange = numStepsNew > numStepsOld;
+ } else {
+ shouldChange = true;
+ }
- if (newSteps != Double.NaN && shouldChange && numStepsNew <= numHorizontalLabels) {
- exactSteps = newSteps;
- } else {
- // try to stay to the old steps
- exactSteps = oldSteps;
- }
+ if (newSteps != Double.NaN && shouldChange && numStepsNew <= numHorizontalLabels) {
+ exactSteps = newSteps;
+ } else {
+ // try to stay to the old steps
+ exactSteps = oldSteps;
}
- } else {
- // first time
- LogUtil.debug("find","find");
}
+ } else {
+ // first time
+ LogUtil.debug("find", "find");
+ }
- // starting from 1st datapoint
- // goal is to start with the minX or 1 step before
- newMinX = mGraphView.getViewport().getReferenceX();
- // must be down-rounded
- double count = Math.floor((minX - newMinX) / exactSteps);
- newMinX = count * exactSteps + newMinX;
+ // starting from 1st datapoint
+ // goal is to start with the minX or 1 step before
+ newMinX = mGraphView.getViewport().getReferenceX();
+ // must be down-rounded
+ double count = Math.floor((minX - newMinX) / exactSteps);
+ newMinX = count * exactSteps + newMinX;
- // now we have our labels bounds
- if (changeBounds) {
- mGraphView.getViewport().setMinX(newMinX);
- mGraphView.getViewport().setMaxX(newMinX + (numHorizontalLabels - 1) * exactSteps);
- mGraphView.getViewport().mXAxisBoundsStatus = Viewport.AxisBoundsStatus.AUTO_ADJUSTED;
- }
+ // now we have our labels bounds
+ if (changeBounds) {
+ mGraphView.getViewport().setMinX(newMinX);
+ mGraphView.getViewport().setMaxX(newMinX + (numHorizontalLabels - 1) * exactSteps);
+ mGraphView.getViewport().mXAxisBoundsStatus = Viewport.AxisBoundsStatus.AUTO_ADJUSTED;
+ }
+
+ // it can happen that we need to add some more labels to fill the complete screen
+ numHorizontalLabels = (int) ((mGraphView.getViewport().mCurrentViewport.width() / exactSteps)) + 1;
+
+ if (mStepsHorizontal != null) {
+ mStepsHorizontal.clear();
+ } else {
+ mStepsHorizontal = new LinkedHashMap<>((int) numHorizontalLabels);
+ }
- // it can happen that we need to add some more labels to fill the complete screen
- numHorizontalLabels = (int) ((mGraphView.getViewport().mCurrentViewport.width() / exactSteps)) + 1;
+ int width = mGraphView.getGraphContentWidth();
+ // convert data-x to pixel-x in current viewport
+ double pixelPerData = width / mGraphView.getViewport().mCurrentViewport.width();
- if (mStepsHorizontal != null) {
- mStepsHorizontal.clear();
- } else {
- mStepsHorizontal = new LinkedHashMap<>((int) numHorizontalLabels);
+ for (int i = 0; i < numHorizontalLabels; i++) {
+ // dont draw if it is left of visible screen
+ if (newMinX + (i * exactSteps) < mGraphView.getViewport().mCurrentViewport.left) {
+ continue;
}
- int width = mGraphView.getGraphContentWidth();
- // convert data-x to pixel-x in current viewport
- double pixelPerData = width / mGraphView.getViewport().mCurrentViewport.width();
+ // where is the data point on the current screen
+ double dataPointPos = newMinX + (i * exactSteps);
+ double relativeToCurrentViewport = dataPointPos - mGraphView.getViewport().mCurrentViewport.left;
- for (int i = 0; i < numHorizontalLabels; i++) {
- // dont draw if it is left of visible screen
- if (newMinX + (i * exactSteps) < mGraphView.getViewport().mCurrentViewport.left) {
- continue;
- }
+ double pixelPos = relativeToCurrentViewport * pixelPerData;
+ mStepsHorizontal.put((int) pixelPos, dataPointPos);
+ }
- // where is the data point on the current screen
- double dataPointPos = newMinX + (i * exactSteps);
- double relativeToCurrentViewport = dataPointPos - mGraphView.getViewport().mCurrentViewport.left;
+ return true;
+ }
- double pixelPos = relativeToCurrentViewport * pixelPerData;
- mStepsHorizontal.put((int) pixelPos, dataPointPos);
- }
+ /**
+ * adjusts the grid and labels to match to the data
+ * this will automatically change the bounds to
+ * nice human-readable values, except the bounds
+ * are manual.
+ */
+ protected void adjustSteps() {
+ mIsAdjusted = adjustVertical(!Viewport.AxisBoundsStatus.FIX.equals(mGraphView.getViewport().mYAxisBoundsStatus));
+ mIsAdjusted &= adjustVerticalSecondScale();
+ mIsAdjusted &= adjustHorizontal(!Viewport.AxisBoundsStatus.FIX.equals(mGraphView.getViewport().mXAxisBoundsStatus));
+ }
- return true;
- }
+ /**
+ * calculates the vertical label size
+ *
+ * @param canvas canvas
+ */
+ protected void calcLabelVerticalSize(Canvas canvas) {
+ // test label with first and last label
+ String testLabel = mLabelFormatter.formatLabel(mGraphView.getViewport().getMaxY(false), false);
+ if (testLabel == null) testLabel = "";
+ LogUtil.error("calcLabelVerticalSize--1->", testLabel);
+ Rect textBounds = new Rect();
+ mPaintLabel.getTextBounds(testLabel);
+ mLabelVerticalWidth = textBounds.getWidth();
+ mLabelVerticalHeight = textBounds.getHeight();
+
+ testLabel = mLabelFormatter.formatLabel(mGraphView.getViewport().getMinY(false), false);
+ if (testLabel == null) testLabel = "";
+ LogUtil.error("calcLabelVerticalSize--2->", testLabel);
+ mPaintLabel.getTextBounds(testLabel);
+ mLabelVerticalWidth = Math.max(mLabelVerticalWidth, textBounds.getWidth());
+
+ // add some pixel to get a margin
+ mLabelVerticalWidth += 6;
+
+ // space between text and graph content
+ mLabelVerticalWidth += mStyles.labelsSpace;
+
+ // multiline
+ int lines = 1;
+ for (byte c : testLabel.getBytes()) {
+ if (c == '\n') lines++;
+ }
+ mLabelVerticalHeight *= lines;
+ }
- /**
- * adjusts the grid and labels to match to the data
- * this will automatically change the bounds to
- * nice human-readable values, except the bounds
- * are manual.
- */
- protected void adjustSteps () {
- mIsAdjusted = adjustVertical(!Viewport.AxisBoundsStatus.FIX.equals(mGraphView.getViewport().mYAxisBoundsStatus));
- mIsAdjusted &= adjustVerticalSecondScale();
- mIsAdjusted &= adjustHorizontal(!Viewport.AxisBoundsStatus.FIX.equals(mGraphView.getViewport().mXAxisBoundsStatus));
+ /**
+ * calculates the vertical second scale
+ * label size
+ *
+ * @param canvas canvas
+ */
+ protected void calcLabelVerticalSecondScaleSize(Canvas canvas) {
+ if (mGraphView.mSecondScale == null) {
+ mLabelVerticalSecondScaleWidth = 0;
+ mLabelVerticalSecondScaleHeight = 0;
+ return;
+ }
+
+ // test label
+ double testY = ((mGraphView.mSecondScale.getMaxY(false) - mGraphView.mSecondScale.getMinY(false)) * 0.783) + mGraphView.mSecondScale.getMinY(false);
+ String testLabel = mGraphView.mSecondScale.getLabelFormatter().formatLabel(testY, false);
+ Rect textBounds = new Rect();
+ mPaintLabel.getTextBounds(testLabel);
+ mLabelVerticalSecondScaleWidth = textBounds.getWidth();
+ mLabelVerticalSecondScaleHeight = textBounds.getHeight();
+
+ // multiline
+ int lines = 1;
+ for (byte c : testLabel.getBytes()) {
+ if (c == '\n') lines++;
+ }
+ mLabelVerticalSecondScaleHeight *= lines;
+ }
+
+ /**
+ * calculates the horizontal label size
+ *
+ * @param canvas canvas
+ */
+ protected void calcLabelHorizontalSize(Canvas canvas) {
+ // test label
+ double testX = ((mGraphView.getViewport().getMaxX(false) - mGraphView.getViewport().getMinX(false)) * 0.783) + mGraphView.getViewport().getMinX(false);
+ String testLabel = mLabelFormatter.formatLabel(testX, true);
+ if (testLabel == null) {
+ testLabel = "";
}
+ Rect textBounds = new Rect();
+ mPaintLabel.getTextBounds(testLabel);
+ mLabelHorizontalWidth = textBounds.getWidth();
- /**
- * calculates the vertical label size
- *
- * @param canvas canvas
- */
- protected void calcLabelVerticalSize (Canvas canvas){
- // test label with first and last label
- String testLabel = mLabelFormatter.formatLabel(mGraphView.getViewport().getMaxY(false), false);
- if (testLabel == null) testLabel = "";
- LogUtil.error("calcLabelVerticalSize--1->",testLabel);
- Rect textBounds = new Rect();
- mPaintLabel.getTextBounds(testLabel);
- mLabelVerticalWidth = textBounds.getWidth();
- mLabelVerticalHeight = textBounds.getHeight();
-
- testLabel = mLabelFormatter.formatLabel(mGraphView.getViewport().getMinY(false), false);
- if (testLabel == null) testLabel = "";
- LogUtil.error("calcLabelVerticalSize--2->",testLabel);
- mPaintLabel.getTextBounds(testLabel);
- mLabelVerticalWidth = Math.max(mLabelVerticalWidth, textBounds.getWidth());
-
- // add some pixel to get a margin
- mLabelVerticalWidth += 6;
-
- // space between text and graph content
- mLabelVerticalWidth += mStyles.labelsSpace;
+ if (!mLabelHorizontalHeightFixed) {
+ mLabelHorizontalHeight = textBounds.getHeight();
// multiline
int lines = 1;
for (byte c : testLabel.getBytes()) {
if (c == '\n') lines++;
}
- mLabelVerticalHeight *= lines;
- }
+ mLabelHorizontalHeight *= lines;
- /**
- * calculates the vertical second scale
- * label size
- *
- * @param canvas canvas
- */
- protected void calcLabelVerticalSecondScaleSize (Canvas canvas){
- if (mGraphView.mSecondScale == null) {
- mLabelVerticalSecondScaleWidth = 0;
- mLabelVerticalSecondScaleHeight = 0;
- return;
- }
+ mLabelHorizontalHeight = (int) Math.max(mLabelHorizontalHeight, mStyles.textSize);
+ }
- // test label
- double testY = ((mGraphView.mSecondScale.getMaxY(false) - mGraphView.mSecondScale.getMinY(false)) * 0.783) + mGraphView.mSecondScale.getMinY(false);
- String testLabel = mGraphView.mSecondScale.getLabelFormatter().formatLabel(testY, false);
- Rect textBounds = new Rect();
- mPaintLabel.getTextBounds(testLabel);
- mLabelVerticalSecondScaleWidth = textBounds.getWidth();
- mLabelVerticalSecondScaleHeight = textBounds.getHeight();
+ if (mStyles.horizontalLabelsAngle > 0f && mStyles.horizontalLabelsAngle <= 180f) {
+ int adjHorizontalHeightH = (int) Math.round(Math.abs(mLabelHorizontalHeight * Math.cos(Math.toRadians(mStyles.horizontalLabelsAngle))));
+ int adjHorizontalHeightW = (int) Math.round(Math.abs(mLabelHorizontalWidth * Math.sin(Math.toRadians(mStyles.horizontalLabelsAngle))));
+ int adjHorizontalWidthH = (int) Math.round(Math.abs(mLabelHorizontalHeight * Math.sin(Math.toRadians(mStyles.horizontalLabelsAngle))));
+ int adjHorizontalWidthW = (int) Math.round(Math.abs(mLabelHorizontalWidth * Math.cos(Math.toRadians(mStyles.horizontalLabelsAngle))));
- // multiline
- int lines = 1;
- for (byte c : testLabel.getBytes()) {
- if (c == '\n') lines++;
- }
- mLabelVerticalSecondScaleHeight *= lines;
+ mLabelHorizontalHeight = adjHorizontalHeightH + adjHorizontalHeightW;
+ mLabelHorizontalWidth = adjHorizontalWidthH + adjHorizontalWidthW;
}
- /**
- * calculates the horizontal label size
- *
- * @param canvas canvas
- */
- protected void calcLabelHorizontalSize (Canvas canvas){
- // test label
- double testX = ((mGraphView.getViewport().getMaxX(false) - mGraphView.getViewport().getMinX(false)) * 0.783) + mGraphView.getViewport().getMinX(false);
- String testLabel = mLabelFormatter.formatLabel(testX, true);
- if (testLabel == null) {
- testLabel = "";
- }
- Rect textBounds = new Rect();
- mPaintLabel.getTextBounds(testLabel);
- mLabelHorizontalWidth = textBounds.getWidth();
+ // space between text and graph content
+ mLabelHorizontalHeight += mStyles.labelsSpace;
+ }
- if (!mLabelHorizontalHeightFixed) {
- mLabelHorizontalHeight = textBounds.getHeight();
+ /**
+ * do the drawing of the grid
+ * and labels
+ *
+ * @param width str
+ * @param height str
+ * @param canvas canvas
+ */
+ public void draw(Canvas canvas, int width, int height) {
- // multiline
- int lines = 1;
- for (byte c : testLabel.getBytes()) {
- if (c == '\n') lines++;
- }
- mLabelHorizontalHeight *= lines;
+ boolean labelSizeChanged = false;
+ if (mLabelHorizontalWidth == null) {
+ calcLabelHorizontalSize(canvas);
+ labelSizeChanged = true;
+ }
+ if (mLabelVerticalWidth == null) {
+ calcLabelVerticalSize(canvas);
+ labelSizeChanged = true;
+ }
+ if (mLabelVerticalSecondScaleWidth == null) {
+ calcLabelVerticalSecondScaleSize(canvas);
+ labelSizeChanged = true;
+ }
+ if (labelSizeChanged) {
+ // redraw directly
+ mGraphView.drawGraphElements(canvas);
+ return;
+ }
- mLabelHorizontalHeight = (int) Math.max(mLabelHorizontalHeight, mStyles.textSize);
- }
-
- if (mStyles.horizontalLabelsAngle > 0f && mStyles.horizontalLabelsAngle <= 180f) {
- int adjHorizontalHeightH = (int) Math.round(Math.abs(mLabelHorizontalHeight * Math.cos(Math.toRadians(mStyles.horizontalLabelsAngle))));
- int adjHorizontalHeightW = (int) Math.round(Math.abs(mLabelHorizontalWidth * Math.sin(Math.toRadians(mStyles.horizontalLabelsAngle))));
- int adjHorizontalWidthH = (int) Math.round(Math.abs(mLabelHorizontalHeight * Math.sin(Math.toRadians(mStyles.horizontalLabelsAngle))));
- int adjHorizontalWidthW = (int) Math.round(Math.abs(mLabelHorizontalWidth * Math.cos(Math.toRadians(mStyles.horizontalLabelsAngle))));
-
- mLabelHorizontalHeight = adjHorizontalHeightH + adjHorizontalHeightW;
- mLabelHorizontalWidth = adjHorizontalWidthH + adjHorizontalWidthW;
- }
-
- // space between text and graph content
- mLabelHorizontalHeight += mStyles.labelsSpace;
+ if (!mIsAdjusted) {
+ adjustSteps();
}
- /**
- * do the drawing of the grid
- * and labels
- *
- * @param width str
- * @param height str
- * @param canvas canvas
- */
- public void draw (Canvas canvas,int width,int height){
-
- boolean labelSizeChanged = false;
- if (mLabelHorizontalWidth == null) {
- calcLabelHorizontalSize(canvas);
- labelSizeChanged = true;
- }
- if (mLabelVerticalWidth == null) {
- calcLabelVerticalSize(canvas);
- labelSizeChanged = true;
- }
- if (mLabelVerticalSecondScaleWidth == null) {
- calcLabelVerticalSecondScaleSize(canvas);
- labelSizeChanged = true;
- }
- if (labelSizeChanged) {
- // redraw directly
- mGraphView.drawGraphElements(canvas);
- return;
- }
-
- if (!mIsAdjusted) {
- adjustSteps();
- }
-
- if (mIsAdjusted) {
- //Y轴数据
- drawVerticalSteps(canvas);
- drawVerticalStepsSecondScale(canvas);
- //X轴数据
- drawHorizontalSteps(canvas,width,height);
- } else {
- // we can not draw anything
- return;
- }
+ if (mIsAdjusted) {
+ //Y轴数据
+ drawVerticalSteps(canvas);
+ drawVerticalStepsSecondScale(canvas);
+ //X轴数据
+ drawHorizontalSteps(canvas, width, height);
+ } else {
+ // we can not draw anything
+ return;
+ }
- drawHorizontalAxisTitle(canvas,width,height);
- drawVerticalAxisTitle(canvas,width,height);
+ drawHorizontalAxisTitle(canvas, width, height);
+ drawVerticalAxisTitle(canvas, width, height);
- // draw second scale axis title if it exists
- if (mGraphView.mSecondScale != null) {
- mGraphView.mSecondScale.drawVerticalAxisTitle(canvas,width,height);
- }
+ // draw second scale axis title if it exists
+ if (mGraphView.mSecondScale != null) {
+ mGraphView.mSecondScale.drawVerticalAxisTitle(canvas, width, height);
}
+ }
- /**
- * draws the horizontal axis title if
- * it is set
- *
- * @param canvas canvas
- * @param width str
- * @param height str
- */
- protected void drawHorizontalAxisTitle (Canvas canvas,int width,int height){
- if (mHorizontalAxisTitle != null && mHorizontalAxisTitle.length() > 0) {
- mPaintAxisTitle.setColor(new Color(getHorizontalAxisTitleColor()));
- mPaintAxisTitle.setTextSize((int) getHorizontalAxisTitleTextSize());
- float x = width / 2;
- float y = height - mStyles.padding;
- canvas.drawText(mPaintAxisTitle, mHorizontalAxisTitle, x, y);
- }
+ /**
+ * draws the horizontal axis title if
+ * it is set
+ *
+ * @param canvas canvas
+ * @param width str
+ * @param height str
+ */
+ protected void drawHorizontalAxisTitle(Canvas canvas, int width, int height) {
+ if (mHorizontalAxisTitle != null && mHorizontalAxisTitle.length() > 0) {
+ mPaintAxisTitle.setColor(new Color(getHorizontalAxisTitleColor()));
+ mPaintAxisTitle.setTextSize((int) getHorizontalAxisTitleTextSize());
+ float x = width / 2;
+ float y = height - mStyles.padding;
+ canvas.drawText(mPaintAxisTitle, mHorizontalAxisTitle, x, y);
}
+ }
- /**
- * draws the vertical axis title if
- * it is set
- *
- * @param canvas canvas
- * @param width str
- * @param height str
- */
- protected void drawVerticalAxisTitle (Canvas canvas,int width,int height){
- if (mVerticalAxisTitle != null && mVerticalAxisTitle.length() > 0) {
- mPaintAxisTitle.setColor(new Color(getVerticalAxisTitleColor()));
- mPaintAxisTitle.setTextSize((int) getVerticalAxisTitleTextSize());
- float x = getVerticalAxisTitleWidth();
- float y = height / 2;
- canvas.save();
- canvas.rotate(-90, x, y);
- canvas.drawText(mPaintAxisTitle, mVerticalAxisTitle, x, y);
- canvas.restore();
- }
+ /**
+ * draws the vertical axis title if
+ * it is set
+ *
+ * @param canvas canvas
+ * @param width str
+ * @param height str
+ */
+ protected void drawVerticalAxisTitle(Canvas canvas, int width, int height) {
+ if (mVerticalAxisTitle != null && mVerticalAxisTitle.length() > 0) {
+ mPaintAxisTitle.setColor(new Color(getVerticalAxisTitleColor()));
+ mPaintAxisTitle.setTextSize((int) getVerticalAxisTitleTextSize());
+ float x = getVerticalAxisTitleWidth();
+ float y = height / 2;
+ canvas.save();
+ canvas.rotate(-90, x, y);
+ canvas.drawText(mPaintAxisTitle, mVerticalAxisTitle, x, y);
+ canvas.restore();
}
+ }
- /**
- * ss
- *
- * @return the horizontal axis title height
- * or 0 if there is no title
- */
- public int getHorizontalAxisTitleHeight () {
- if (mHorizontalAxisTitle != null && mHorizontalAxisTitle.length() > 0) {
- return (int) getHorizontalAxisTitleTextSize();
- } else {
- return 0;
- }
+ /**
+ * ss
+ *
+ * @return the horizontal axis title height
+ * or 0 if there is no title
+ */
+ public int getHorizontalAxisTitleHeight() {
+ if (mHorizontalAxisTitle != null && mHorizontalAxisTitle.length() > 0) {
+ return (int) getHorizontalAxisTitleTextSize();
+ } else {
+ return 0;
}
+ }
- /**
- * ss
- *
- * @return the vertical axis title width
- * or 0 if there is no title
- */
- public int getVerticalAxisTitleWidth () {
- if (mVerticalAxisTitle != null && mVerticalAxisTitle.length() > 0) {
- return (int) getVerticalAxisTitleTextSize();
- } else {
- return 0;
- }
+ /**
+ * ss
+ *
+ * @return the vertical axis title width
+ * or 0 if there is no title
+ */
+ public int getVerticalAxisTitleWidth() {
+ if (mVerticalAxisTitle != null && mVerticalAxisTitle.length() > 0) {
+ return (int) getVerticalAxisTitleTextSize();
+ } else {
+ return 0;
}
+ }
- /**
- * draws the horizontal steps
- * vertical lines and horizontal labels
- * TODO 画X轴数据
- *
- * @param canvas canvas
- * @param height str
- * @param width str
- */
- protected void drawHorizontalSteps (Canvas canvas,int width,int height){
- // draw horizontal steps (vertical lines and horizontal labels)
- mPaintLabel.setColor(new Color(getHorizontalLabelsColor()));
- int i = 0;
- for (Map.Entry
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -153,7 +153,7 @@ public class LegendRenderer {
mPaint.setTextSize((int) mStyles.textSize);
- int shapeSize = (int) (mStyles.textSize*0.8d);
+ int shapeSize = (int) (mStyles.textSize * 0.8d);
List