From f1654afc0316fef4cc48e6321d5749ea3bc8701d Mon Sep 17 00:00:00 2001 From: Gregor Horvat Date: Fri, 9 Feb 2018 10:31:06 +0100 Subject: [PATCH 1/4] separate show & hide labels (auto-labels/custom label) --- .../src/org/achartengine/chart/XYChart.java | 498 +++++++++++------- .../renderer/DefaultRenderer.java | 194 ++++--- 2 files changed, 407 insertions(+), 285 deletions(-) diff --git a/achartengine/src/org/achartengine/chart/XYChart.java b/achartengine/src/org/achartengine/chart/XYChart.java index f1d2296..5ee7666 100644 --- a/achartengine/src/org/achartengine/chart/XYChart.java +++ b/achartengine/src/org/achartengine/chart/XYChart.java @@ -1,12 +1,12 @@ /** * Copyright (C) 2009 - 2013 SC 4ViewSoft SRL - * + * * 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. @@ -15,14 +15,17 @@ */ package org.achartengine.chart; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.SortedMap; +import android.graphics.Canvas; +import android.graphics.DashPathEffect; +import android.graphics.Paint; +import android.graphics.Paint.Align; +import android.graphics.Paint.Cap; +import android.graphics.Paint.Join; +import android.graphics.Paint.Style; +import android.graphics.PathEffect; +import android.graphics.Rect; +import android.graphics.RectF; +import android.graphics.Typeface; import org.achartengine.model.Point; import org.achartengine.model.SeriesSelection; @@ -36,17 +39,14 @@ import org.achartengine.renderer.XYSeriesRenderer; import org.achartengine.util.MathHelper; -import android.graphics.Canvas; -import android.graphics.DashPathEffect; -import android.graphics.Paint; -import android.graphics.Paint.Align; -import android.graphics.Paint.Cap; -import android.graphics.Paint.Join; -import android.graphics.Paint.Style; -import android.graphics.PathEffect; -import android.graphics.Rect; -import android.graphics.RectF; -import android.graphics.Typeface; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.SortedMap; /** * The XY chart rendering class. @@ -80,7 +80,7 @@ protected XYChart() { /** * Builds a new XY chart instance. - * + * * @param dataset the multiple series dataset * @param renderer the multiple series renderer */ @@ -91,14 +91,14 @@ public XYChart(XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer rendere // TODO: javadoc protected void setDatasetRenderer(XYMultipleSeriesDataset dataset, - XYMultipleSeriesRenderer renderer) { + XYMultipleSeriesRenderer renderer) { mDataset = dataset; mRenderer = renderer; } /** * The graphical representation of the XY chart. - * + * * @param canvas the canvas to paint to * @param x the top left x value of the view to draw to * @param y the top left y value of the view to draw to @@ -120,7 +120,7 @@ public void draw(Canvas canvas, int x, int y, int width, int height, Paint paint } if (mRenderer.isFitLegend() && mRenderer.isShowLegend()) { legendSize = drawLegend(canvas, mRenderer, titles, left, right, y, width, height, legendSize, - paint, true); + paint, true); } int bottom = y + height - margins[2] - legendSize; if (mScreenR == null) { @@ -130,15 +130,15 @@ public void draw(Canvas canvas, int x, int y, int width, int height, Paint paint drawBackground(mRenderer, canvas, x, y, width, height, paint, false, DefaultRenderer.NO_COLOR); if (paint.getTypeface() == null - || (mRenderer.getTextTypeface() != null && paint.getTypeface().equals( + || (mRenderer.getTextTypeface() != null && paint.getTypeface().equals( mRenderer.getTextTypeface())) - || !paint.getTypeface().toString().equals(mRenderer.getTextTypefaceName()) - || paint.getTypeface().getStyle() != mRenderer.getTextTypefaceStyle()) { + || !paint.getTypeface().toString().equals(mRenderer.getTextTypefaceName()) + || paint.getTypeface().getStyle() != mRenderer.getTextTypefaceStyle()) { if (mRenderer.getTextTypeface() != null) { paint.setTypeface(mRenderer.getTextTypeface()); } else { paint.setTypeface(Typeface.create(mRenderer.getTextTypefaceName(), - mRenderer.getTextTypefaceStyle())); + mRenderer.getTextTypefaceStyle())); } } Orientation or = mRenderer.getOrientation(); @@ -232,18 +232,11 @@ public void draw(Canvas canvas, int x, int y, int width, int height, Paint paint } } - boolean hasValues = false; - for (int i = 0; i < sLength; i++) { - XYSeries series = mDataset.getSeriesAt(i); - if (series.getItemCount() == 0) { - continue; - } - hasValues = true; - } - - boolean showLabels = mRenderer.isShowLabels() && hasValues; boolean showGridX = mRenderer.isShowGridX(); boolean showGridY = mRenderer.isShowGridY(); + boolean showCustomTextXLabels = mRenderer.isShowCustomTextXLabels(); + boolean showCustomTextYLabels = mRenderer.isShowCustomTextYLabels(); + if (showGridX || showGridY) { // Draw the grid lines under everything else. List xLabels = getValidLabels(getXLabels(minX[0], maxX[0], mRenderer.getXLabels())); @@ -254,14 +247,16 @@ public void draw(Canvas canvas, int x, int y, int width, int height, Paint paint boolean showYLabels = mRenderer.isShowYLabels(); // Only draw the grid. mRenderer.setShowLabels(false); + mRenderer.setShowCustomTextLabels(false); if (mGridPaint == null) { mGridPaint = new Paint(Paint.ANTI_ALIAS_FLAG); } drawXLabels(xLabels, mRenderer.getXTextLabelLocations(), canvas, paint, xLabelsLeft, top, bottom, xPixelsPerUnit[0], minX[0], maxX[0]); - drawYLabels(allYLabels, canvas, paint, maxScaleNumber, left, right, bottom, yPixelsPerUnit, - minY); + drawYLabels(allYLabels, canvas, paint, maxScaleNumber, left, right, bottom, yPixelsPerUnit, minY); + mRenderer.setShowLabels(showXLabels, showYLabels); + mRenderer.setShowCustomTextLabels(showCustomTextXLabels, showCustomTextYLabels); } // use a linked list for these reasons: @@ -290,7 +285,7 @@ public void draw(Canvas canvas, int x, int y, int width, int height, Paint paint synchronized (series) { SortedMap range = series.getRange(minX[scale], maxX[scale], - seriesRenderer.isDisplayBoundingPoints()); + seriesRenderer.isDisplayBoundingPoints()); int startIndex = -1; for (Entry value : range.entrySet()) { @@ -316,9 +311,9 @@ public void draw(Canvas canvas, int x, int y, int width, int height, Paint paint } else { if (points.size() > 0) { drawSeries(series, canvas, paint, points, seriesRenderer, yAxisValue, i, or, - startIndex); + startIndex); ClickableArea[] clickableAreasForSubSeries = clickableAreasForPoints(points, values, - yAxisValue, i, startIndex); + yAxisValue, i, startIndex); clickableArea.addAll(Arrays.asList(clickableAreasForSubSeries)); points.clear(); values.clear(); @@ -336,11 +331,11 @@ public void draw(Canvas canvas, int x, int y, int width, int height, Paint paint Rect bound = new Rect(); for (int j = 0; j < count; j++) { float xS = (float) (left + xPixelsPerUnit[scale] - * (series.getAnnotationX(j) - minX[scale])); + * (series.getAnnotationX(j) - minX[scale])); float yS = (float) (bottom - yPixelsPerUnit[scale] - * (series.getAnnotationY(j) - minY[scale])); + * (series.getAnnotationY(j) - minY[scale])); paint.getTextBounds(series.getAnnotationAt(j), 0, series.getAnnotationAt(j).length(), - bound); + bound); if (xS < (xS + bound.width()) && yS < canvas.getHeight()) { drawString(canvas, series.getAnnotationAt(j), xS, yS, paint); } @@ -350,142 +345,235 @@ public void draw(Canvas canvas, int x, int y, int width, int height, Paint paint if (points.size() > 0) { drawSeries(series, canvas, paint, points, seriesRenderer, yAxisValue, i, or, startIndex); ClickableArea[] clickableAreasForSubSeries = clickableAreasForPoints(points, values, - yAxisValue, i, startIndex); + yAxisValue, i, startIndex); clickableArea.addAll(Arrays.asList(clickableAreasForSubSeries)); } } } // draw stuff over the margins so that data doesn't render on these areas drawBackground(mRenderer, canvas, x, bottom, width, height - bottom, paint, true, - mRenderer.getMarginsColor()); + mRenderer.getMarginsColor()); drawBackground(mRenderer, canvas, x, y, width, margins[0], paint, true, - mRenderer.getMarginsColor()); + mRenderer.getMarginsColor()); if (or == Orientation.HORIZONTAL) { drawBackground(mRenderer, canvas, x, y, left - x, height - y, paint, true, - mRenderer.getMarginsColor()); + mRenderer.getMarginsColor()); drawBackground(mRenderer, canvas, right, y, margins[3], height - y, paint, true, - mRenderer.getMarginsColor()); + mRenderer.getMarginsColor()); } else if (or == Orientation.VERTICAL) { drawBackground(mRenderer, canvas, right, y, width - right, height - y, paint, true, - mRenderer.getMarginsColor()); + mRenderer.getMarginsColor()); drawBackground(mRenderer, canvas, x, y, left - x, height - y, paint, true, - mRenderer.getMarginsColor()); + mRenderer.getMarginsColor()); + } + + boolean hasValues = false; + for (int i = 0; i < sLength; i++) { + XYSeries series = mDataset.getSeriesAt(i); + if (series.getItemCount() == 0) { + continue; + } + hasValues = true; } + boolean showXLabels = mRenderer.isShowXLabels() && hasValues; + boolean showYLabels = mRenderer.isShowYLabels() && hasValues; + + boolean showLabels = mRenderer.isShowLabels() && hasValues; boolean showTickMarks = mRenderer.isShowTickMarks(); // boolean showCustomTextGridX = mRenderer.isShowCustomTextGridX(); boolean showCustomTextGridY = mRenderer.isShowCustomTextGridY(); + + if (showXLabels) { + List xLabels = getValidLabels(getXLabels(minX[0], maxX[0], mRenderer.getXLabels())); + + paint.setColor(mRenderer.getXLabelsColor()); + paint.setTextSize(mRenderer.getLabelsTextSize()); + paint.setTextAlign(mRenderer.getXLabelsAlign()); + + // Draw just the labels and not the grid lines. + mRenderer.setShowGrid(false); + drawXLabels(xLabels, mRenderer.getXTextLabelLocations(), canvas, paint, left, top, + bottom, xPixelsPerUnit[0], minX[0], maxX[0]); + mRenderer.setShowGridX(showGridX); + + paint.setColor(mRenderer.getLabelsColor()); + float size = mRenderer.getAxisTitleTextSize(); + paint.setTextSize(size); + paint.setTextAlign(Align.CENTER); + if (or == Orientation.HORIZONTAL) { + drawText(canvas, + mRenderer.getXTitle(), + x + width / 2, + bottom + mRenderer.getLabelsTextSize() * 4 / 3 + mRenderer.getXLabelsPadding() + size, + paint, 0); + + } else if (or == Orientation.VERTICAL) { + drawText(canvas, mRenderer.getXTitle(), x + width / 2, + y + height - size + mRenderer.getXLabelsPadding(), paint, -90); + } + } + + if (showYLabels) { + Map> allYLabels = getYLabels(minY, maxY, maxScaleNumber); + + paint.setColor(mRenderer.getXLabelsColor()); + paint.setTextSize(mRenderer.getLabelsTextSize()); + paint.setTextAlign(mRenderer.getXLabelsAlign()); + + // Draw just the labels and not the grid lines. + mRenderer.setShowGrid(false); + drawYLabels(allYLabels, canvas, paint, maxScaleNumber, left, right, bottom, yPixelsPerUnit, minY); + mRenderer.setShowGridY(showGridY); + + paint.setColor(mRenderer.getLabelsColor()); + float size = mRenderer.getAxisTitleTextSize(); + paint.setTextSize(size); + paint.setTextAlign(Align.CENTER); + if (or == Orientation.HORIZONTAL) { + for (int i = 0; i < maxScaleNumber; i++) { + Align axisAlign = mRenderer.getYAxisAlign(i); + if (axisAlign == Align.LEFT) { + drawText(canvas, mRenderer.getYTitle(i), x + size, y + height / 2, paint, -90); + } else { + drawText(canvas, mRenderer.getYTitle(i), x + width, y + height / 2, paint, -90); + } + } + } else if (or == Orientation.VERTICAL) { + drawText(canvas, mRenderer.getYTitle(), right + 20, y + height / 2, paint, 0); + } + } + + if (showLabels) { + paint.setColor(mRenderer.getLabelsColor()); + float axisTitleTextSize = mRenderer.getAxisTitleTextSize(); + paint.setTextSize(axisTitleTextSize); + paint.setTextAlign(Align.CENTER); + paint.setTextSize(mRenderer.getChartTitleTextSize()); + + if (or == Orientation.HORIZONTAL) { + drawText(canvas, mRenderer.getChartTitle(), x + width / 2, + y + mRenderer.getChartTitleTextSize(), paint, 0); + + } else if (or == Orientation.VERTICAL) { + drawText(canvas, mRenderer.getChartTitle(), x + axisTitleTextSize, top + height / 2, paint, 0); + } + } + + if (showLabels) { List xLabels = getValidLabels(getXLabels(minX[0], maxX[0], mRenderer.getXLabels())); Map> allYLabels = getYLabels(minY, maxY, maxScaleNumber); int xLabelsLeft = left; - if (showLabels) { - paint.setColor(mRenderer.getXLabelsColor()); - paint.setTextSize(mRenderer.getLabelsTextSize()); - paint.setTextAlign(mRenderer.getXLabelsAlign()); - // if (mRenderer.getXLabelsAlign() == Align.LEFT) { - // xLabelsLeft += mRenderer.getLabelsTextSize() / 4; - // } - } + + paint.setColor(mRenderer.getXLabelsColor()); + paint.setTextSize(mRenderer.getLabelsTextSize()); + paint.setTextAlign(mRenderer.getXLabelsAlign()); + // if (mRenderer.getXLabelsAlign() == Align.LEFT) { + // xLabelsLeft += mRenderer.getLabelsTextSize() / 4; + // } + // Draw just the labels and not the grid lines. mRenderer.setShowGrid(false); drawXLabels(xLabels, mRenderer.getXTextLabelLocations(), canvas, paint, xLabelsLeft, top, - bottom, xPixelsPerUnit[0], minX[0], maxX[0]); - drawYLabels(allYLabels, canvas, paint, maxScaleNumber, left, right, bottom, yPixelsPerUnit, - minY); + bottom, xPixelsPerUnit[0], minX[0], maxX[0]); + drawYLabels(allYLabels, canvas, paint, maxScaleNumber, left, right, bottom, yPixelsPerUnit, minY); mRenderer.setShowGridX(showGridX); mRenderer.setShowGridY(showGridY); - if (showLabels) { - paint.setColor(mRenderer.getLabelsColor()); + paint.setColor(mRenderer.getLabelsColor()); + float size = mRenderer.getAxisTitleTextSize(); + paint.setTextSize(size); + paint.setTextAlign(Align.CENTER); + if (or == Orientation.HORIZONTAL) { + drawText( + canvas, + mRenderer.getXTitle(), + x + width / 2, + bottom + mRenderer.getLabelsTextSize() * 4 / 3 + mRenderer.getXLabelsPadding() + size, + paint, 0); for (int i = 0; i < maxScaleNumber; i++) { Align axisAlign = mRenderer.getYAxisAlign(i); - Double[] yTextLabelLocations = mRenderer.getYTextLabelLocations(i); - for (Double location : yTextLabelLocations) { - if (minY[i] <= location && location <= maxY[i]) { - float yLabel = (float) (bottom - yPixelsPerUnit[i] - * (location.doubleValue() - minY[i])); - String label = mRenderer.getYTextLabel(location, i); - paint.setColor(mRenderer.getYLabelsColor(i)); - paint.setTextAlign(mRenderer.getYLabelsAlign(i)); - if (or == Orientation.HORIZONTAL) { - if (axisAlign == Align.LEFT) { - if (showTickMarks) { - canvas.drawLine(left + getLabelLinePos(axisAlign), yLabel, left, yLabel, paint); - } - drawText(canvas, label, left - mRenderer.getYLabelsPadding(), - yLabel - mRenderer.getYLabelsVerticalPadding(), paint, - mRenderer.getYLabelsAngle()); - } else { - if (showTickMarks) { - canvas.drawLine(right, yLabel, right + getLabelLinePos(axisAlign), yLabel, - paint); - } - drawText(canvas, label, right - mRenderer.getYLabelsPadding(), - yLabel - mRenderer.getYLabelsVerticalPadding(), paint, - mRenderer.getYLabelsAngle()); - } + if (axisAlign == Align.LEFT) { + drawText(canvas, mRenderer.getYTitle(i), x + size, y + height / 2, paint, -90); + } else { + drawText(canvas, mRenderer.getYTitle(i), x + width, y + height / 2, paint, -90); + } + } + paint.setTextSize(mRenderer.getChartTitleTextSize()); + drawText(canvas, mRenderer.getChartTitle(), x + width / 2, + y + mRenderer.getChartTitleTextSize(), paint, 0); + } else if (or == Orientation.VERTICAL) { + drawText(canvas, mRenderer.getXTitle(), x + width / 2, + y + height - size + mRenderer.getXLabelsPadding(), paint, -90); + drawText(canvas, mRenderer.getYTitle(), right + 20, y + height / 2, paint, 0); + paint.setTextSize(mRenderer.getChartTitleTextSize()); + drawText(canvas, mRenderer.getChartTitle(), x + size, top + height / 2, paint, 0); + } + + } else if (showCustomTextXLabels) { + drawXTextLabels(mRenderer.getXTextLabelLocations(), canvas, paint, true, left, top, bottom, xPixelsPerUnit[0], minX[0], maxX[0]); + } - if (showCustomTextGridY) { - paint.setColor(mRenderer.getGridColor(i)); - canvas.drawLine(left, yLabel, right, yLabel, paint); + if (showCustomTextYLabels) { + paint.setColor(mRenderer.getLabelsColor()); + for (int i = 0; i < maxScaleNumber; i++) { + Align axisAlign = mRenderer.getYAxisAlign(i); + Double[] yTextLabelLocations = mRenderer.getYTextLabelLocations(i); + for (Double location : yTextLabelLocations) { + if (minY[i] <= location && location <= maxY[i]) { + float yLabel = (float) (bottom - yPixelsPerUnit[i] + * (location.doubleValue() - minY[i])); + String label = mRenderer.getYTextLabel(location, i); + paint.setColor(mRenderer.getYLabelsColor(i)); + paint.setTextAlign(mRenderer.getYLabelsAlign(i)); + if (or == Orientation.HORIZONTAL) { + if (axisAlign == Align.LEFT) { + if (showTickMarks) { + canvas.drawLine(left + getLabelLinePos(axisAlign), yLabel, left, yLabel, paint); } + drawText(canvas, label, left - mRenderer.getYLabelsPadding(), + yLabel - mRenderer.getYLabelsVerticalPadding(), paint, + mRenderer.getYLabelsAngle()); } else { if (showTickMarks) { - canvas.drawLine(right - getLabelLinePos(axisAlign), yLabel, right, yLabel, paint); - } - drawText(canvas, label, right + 10, yLabel - mRenderer.getYLabelsVerticalPadding(), - paint, mRenderer.getYLabelsAngle()); - if (showCustomTextGridY) { - paint.setColor(mRenderer.getGridColor(i)); - canvas.drawLine(right, yLabel, left, yLabel, paint); + canvas.drawLine(right, yLabel, right + getLabelLinePos(axisAlign), yLabel, + paint); } + drawText(canvas, label, right - mRenderer.getYLabelsPadding(), + yLabel - mRenderer.getYLabelsVerticalPadding(), paint, + mRenderer.getYLabelsAngle()); } - } - } - } - } - if (showLabels) { - paint.setColor(mRenderer.getLabelsColor()); - float size = mRenderer.getAxisTitleTextSize(); - paint.setTextSize(size); - paint.setTextAlign(Align.CENTER); - if (or == Orientation.HORIZONTAL) { - drawText( - canvas, - mRenderer.getXTitle(), - x + width / 2, - bottom + mRenderer.getLabelsTextSize() * 4 / 3 + mRenderer.getXLabelsPadding() + size, - paint, 0); - for (int i = 0; i < maxScaleNumber; i++) { - Align axisAlign = mRenderer.getYAxisAlign(i); - if (axisAlign == Align.LEFT) { - drawText(canvas, mRenderer.getYTitle(i), x + size, y + height / 2, paint, -90); + if (showCustomTextGridY) { + paint.setColor(mRenderer.getGridColor(i)); + canvas.drawLine(left, yLabel, right, yLabel, paint); + } } else { - drawText(canvas, mRenderer.getYTitle(i), x + width, y + height / 2, paint, -90); + if (showTickMarks) { + canvas.drawLine(right - getLabelLinePos(axisAlign), yLabel, right, yLabel, paint); + } + drawText(canvas, label, right + 10, yLabel - mRenderer.getYLabelsVerticalPadding(), + paint, mRenderer.getYLabelsAngle()); + if (showCustomTextGridY) { + paint.setColor(mRenderer.getGridColor(i)); + canvas.drawLine(right, yLabel, left, yLabel, paint); + } } } - paint.setTextSize(mRenderer.getChartTitleTextSize()); - drawText(canvas, mRenderer.getChartTitle(), x + width / 2, - y + mRenderer.getChartTitleTextSize(), paint, 0); - } else if (or == Orientation.VERTICAL) { - drawText(canvas, mRenderer.getXTitle(), x + width / 2, - y + height - size + mRenderer.getXLabelsPadding(), paint, -90); - drawText(canvas, mRenderer.getYTitle(), right + 20, y + height / 2, paint, 0); - paint.setTextSize(mRenderer.getChartTitleTextSize()); - drawText(canvas, mRenderer.getChartTitle(), x + size, top + height / 2, paint, 0); } } } + + if (or == Orientation.HORIZONTAL) { drawLegend(canvas, mRenderer, titles, left, right, y + (int) mRenderer.getXLabelsPadding(), - width, height, legendSize, paint, false); + width, height, legendSize, paint, false); } else if (or == Orientation.VERTICAL) { transform(canvas, angle, true); drawLegend(canvas, mRenderer, titles, left, right, y + (int) mRenderer.getXLabelsPadding(), - width, height, legendSize, paint, false); + width, height, legendSize, paint, false); transform(canvas, angle, false); } if (mRenderer.isShowAxes()) { @@ -518,7 +606,7 @@ protected Map> getYLabels(double[] minY, double[] maxY, in Map> allYLabels = new HashMap>(); for (int i = 0; i < maxScaleNumber; i++) { allYLabels.put(i, - getValidLabels(MathHelper.getLabels(minY[i], maxY[i], mRenderer.getYLabels()))); + getValidLabels(MathHelper.getLabels(minY[i], maxY[i], mRenderer.getYLabels()))); } return allYLabels; } @@ -543,7 +631,7 @@ private List getValidLabels(List labels) { /** * Draws the series. - * + * * @param series the series * @param canvas the canvas * @param paint the paint object @@ -555,8 +643,8 @@ private List getValidLabels(List labels) { * @param startIndex the start index of the rendering points */ protected void drawSeries(XYSeries series, Canvas canvas, Paint paint, List pointsList, - XYSeriesRenderer seriesRenderer, float yAxisValue, int seriesIndex, Orientation or, - int startIndex) { + XYSeriesRenderer seriesRenderer, float yAxisValue, int seriesIndex, Orientation or, + int startIndex) { BasicStroke stroke = seriesRenderer.getStroke(); Cap cap = paint.getStrokeCap(); Join join = paint.getStrokeJoin(); @@ -569,7 +657,7 @@ protected void drawSeries(XYSeries series, Canvas canvas, Paint paint, List pointsList, - XYSeriesRenderer seriesRenderer, float yAxisValue, int seriesIndex, int startIndex) { + XYSeriesRenderer seriesRenderer, float yAxisValue, int seriesIndex, int startIndex) { if (isRenderPoints(seriesRenderer)) { ScatterChart pointsChart = getPointsChart(); if (pointsChart != null) { pointsChart.drawSeries(canvas, paint, pointsList, seriesRenderer, yAxisValue, seriesIndex, - startIndex); + startIndex); } } } private void setStroke(Cap cap, Join join, float miter, Style style, PathEffect pathEffect, - Paint paint) { + Paint paint) { paint.setStrokeCap(cap); paint.setStrokeJoin(join); paint.setStrokeMiter(miter); @@ -623,7 +711,7 @@ private void setStroke(Cap cap, Join join, float miter, Style style, PathEffect /** * The graphical representation of the series values as text. - * + * * @param canvas the canvas to paint to * @param series the series to be painted * @param renderer the series renderer @@ -633,23 +721,23 @@ private void setStroke(Cap cap, Join join, float miter, Style style, PathEffect * @param startIndex the start index of the rendering points */ protected void drawChartValuesText(Canvas canvas, XYSeries series, XYSeriesRenderer renderer, - Paint paint, List points, int seriesIndex, int startIndex) { + Paint paint, List points, int seriesIndex, int startIndex) { if (points.size() > 2) { // there are more than one point // record the first point's position float previousPointX = points.get(0); float previousPointY = points.get(1); for (int k = 0; k < points.size(); k += 2) { if (k == 2) { // decide whether to display first two points' values or - // not + // not if (Math.abs(points.get(2) - points.get(0)) > renderer.getDisplayChartValuesDistance() - || Math.abs(points.get(3) - points.get(1)) > renderer.getDisplayChartValuesDistance()) { + || Math.abs(points.get(3) - points.get(1)) > renderer.getDisplayChartValuesDistance()) { // first point drawText(canvas, getLabel(renderer.getChartValuesFormat(), series.getY(startIndex)), - points.get(0), points.get(1) - renderer.getChartValuesSpacing(), paint, 0); + points.get(0), points.get(1) - renderer.getChartValuesSpacing(), paint, 0); // second point drawText(canvas, - getLabel(renderer.getChartValuesFormat(), series.getY(startIndex + 1)), - points.get(2), points.get(3) - renderer.getChartValuesSpacing(), paint, 0); + getLabel(renderer.getChartValuesFormat(), series.getY(startIndex + 1)), + points.get(2), points.get(3) - renderer.getChartValuesSpacing(), paint, 0); previousPointX = points.get(2); previousPointY = points.get(3); @@ -658,11 +746,11 @@ protected void drawChartValuesText(Canvas canvas, XYSeries series, XYSeriesRende // compare current point's position with the previous point's, if they // are not too close, display if (Math.abs(points.get(k) - previousPointX) > renderer.getDisplayChartValuesDistance() - || Math.abs(points.get(k + 1) - previousPointY) > renderer + || Math.abs(points.get(k + 1) - previousPointY) > renderer .getDisplayChartValuesDistance()) { drawText(canvas, - getLabel(renderer.getChartValuesFormat(), series.getY(startIndex + k / 2)), - points.get(k), points.get(k + 1) - renderer.getChartValuesSpacing(), paint, 0); + getLabel(renderer.getChartValuesFormat(), series.getY(startIndex + k / 2)), + points.get(k), points.get(k + 1) - renderer.getChartValuesSpacing(), paint, 0); previousPointX = points.get(k); previousPointY = points.get(k + 1); } @@ -671,8 +759,8 @@ protected void drawChartValuesText(Canvas canvas, XYSeries series, XYSeriesRende } else { // if only one point, display it for (int k = 0; k < points.size(); k += 2) { drawText(canvas, - getLabel(renderer.getChartValuesFormat(), series.getY(startIndex + k / 2)), - points.get(k), points.get(k + 1) - renderer.getChartValuesSpacing(), paint, 0); + getLabel(renderer.getChartValuesFormat(), series.getY(startIndex + k / 2)), + points.get(k), points.get(k + 1) - renderer.getChartValuesSpacing(), paint, 0); } } } @@ -680,7 +768,7 @@ protected void drawChartValuesText(Canvas canvas, XYSeries series, XYSeriesRende /** * The graphical representation of a text, to handle both HORIZONTAL and * VERTICAL orientations and extra rotation angles. - * + * * @param canvas the canvas to paint to * @param text the text to be rendered * @param x the X axis location of the text @@ -689,7 +777,7 @@ protected void drawChartValuesText(Canvas canvas, XYSeries series, XYSeriesRende * @param extraAngle the text angle */ protected void drawText(Canvas canvas, String text, float x, float y, Paint paint, - float extraAngle) { + float extraAngle) { float angle = -mRenderer.getOrientation().getAngle() + extraAngle; if (angle != 0) { // canvas.scale(1 / mScale, mScale); @@ -705,7 +793,7 @@ protected void drawText(Canvas canvas, String text, float x, float y, Paint pain /** * Transform the canvas such as it can handle both HORIZONTAL and VERTICAL * orientations. - * + * * @param canvas the canvas to paint to * @param angle the angle of rotation * @param inverse if the inverse transform needs to be applied @@ -724,7 +812,7 @@ private void transform(Canvas canvas, float angle, boolean inverse) { /** * The graphical representation of the labels on the X axis. - * + * * @param xLabels the X labels values * @param xTextLabelLocations the X text label locations * @param canvas the canvas to paint to @@ -737,35 +825,38 @@ private void transform(Canvas canvas, float angle, boolean inverse) { * @param maxX the maximum value on the X axis in the chart */ protected void drawXLabels(List xLabels, Double[] xTextLabelLocations, Canvas canvas, - Paint paint, int left, int top, int bottom, double xPixelsPerUnit, double minX, double maxX) { - int length = xLabels.size(); + Paint paint, int left, int top, int bottom, double xPixelsPerUnit, double minX, double maxX) { + boolean showXLabels = mRenderer.isShowXLabels(); + boolean showCustomXLabels = mRenderer.isShowCustomTextXLabels(); boolean showGridY = mRenderer.isShowGridY(); + if (showGridY) { mGridPaint.setStyle(Style.STROKE); mGridPaint.setStrokeWidth(mRenderer.getGridLineWidth()); } boolean showTickMarks = mRenderer.isShowTickMarks(); - for (int i = 0; i < length; i++) { + for (int i = 0; i < xLabels.size(); i++) { double label = xLabels.get(i); float xLabel = (float) (left + xPixelsPerUnit * (label - minX)); + if (showXLabels) { paint.setColor(mRenderer.getXLabelsColor()); + if (showTickMarks) { - canvas - .drawLine(xLabel, bottom, xLabel, bottom + mRenderer.getLabelsTextSize() / 3, paint); + canvas.drawLine(xLabel, bottom, xLabel, bottom + mRenderer.getLabelsTextSize() / 3, paint); } drawText(canvas, getLabel(mRenderer.getXLabelFormat(), label), xLabel, - bottom + mRenderer.getLabelsTextSize() * 4 / 3 + mRenderer.getXLabelsPadding(), paint, - mRenderer.getXLabelsAngle()); + bottom + mRenderer.getLabelsTextSize() * 4 / 3 + mRenderer.getXLabelsPadding(), paint, + mRenderer.getXLabelsAngle()); } if (showGridY) { mGridPaint.setColor(mRenderer.getGridColor(0)); canvas.drawLine(xLabel, bottom, xLabel, top, mGridPaint); } } - drawXTextLabels(xTextLabelLocations, canvas, paint, showXLabels, left, top, bottom, - xPixelsPerUnit, minX, maxX); + drawXTextLabels(xTextLabelLocations, canvas, paint, showCustomXLabels, left, top, bottom, + xPixelsPerUnit, minX, maxX); } /** @@ -782,7 +873,7 @@ protected void drawXLabels(List xLabels, Double[] xTextLabelLocations, C * @param minY the minimum value on the Y axis in the chart */ protected void drawYLabels(Map> allYLabels, Canvas canvas, Paint paint, - int maxScaleNumber, int left, int right, int bottom, double[] yPixelsPerUnit, double[] minY) { + int maxScaleNumber, int left, int right, int bottom, double[] yPixelsPerUnit, double[] minY) { Orientation or = mRenderer.getOrientation(); boolean showGridX = mRenderer.isShowGridX(); if (showGridX) { @@ -808,17 +899,17 @@ protected void drawYLabels(Map> allYLabels, Canvas canvas, canvas.drawLine(left + getLabelLinePos(axisAlign), yLabel, left, yLabel, paint); } drawText(canvas, getLabel(mRenderer.getYLabelFormat(i), label), - left - mRenderer.getYLabelsPadding(), - yLabel - mRenderer.getYLabelsVerticalPadding(), paint, - mRenderer.getYLabelsAngle()); + left - mRenderer.getYLabelsPadding(), + yLabel - mRenderer.getYLabelsVerticalPadding(), paint, + mRenderer.getYLabelsAngle()); } else { if (showTickMarks) { canvas.drawLine(right, yLabel, right + getLabelLinePos(axisAlign), yLabel, paint); } drawText(canvas, getLabel(mRenderer.getYLabelFormat(i), label), - right + mRenderer.getYLabelsPadding(), - yLabel - mRenderer.getYLabelsVerticalPadding(), paint, - mRenderer.getYLabelsAngle()); + right + mRenderer.getYLabelsPadding(), + yLabel - mRenderer.getYLabelsVerticalPadding(), paint, + mRenderer.getYLabelsAngle()); } } if (showGridX) { @@ -832,8 +923,8 @@ protected void drawYLabels(Map> allYLabels, Canvas canvas, canvas.drawLine(right - getLabelLinePos(axisAlign), yLabel, right, yLabel, paint); } drawText(canvas, getLabel(mRenderer.getLabelFormat(), label), - right + 10 + mRenderer.getYLabelsPadding(), - yLabel - mRenderer.getYLabelsVerticalPadding(), paint, mRenderer.getYLabelsAngle()); + right + 10 + mRenderer.getYLabelsPadding(), + yLabel - mRenderer.getYLabelsVerticalPadding(), paint, mRenderer.getYLabelsAngle()); } if (showGridX) { mGridPaint.setColor(mRenderer.getGridColor(i)); @@ -848,7 +939,7 @@ protected void drawYLabels(Map> allYLabels, Canvas canvas, /** * The graphical representation of the text labels on the X axis. - * + * * @param xTextLabelLocations the X text label locations * @param canvas the canvas to paint to * @param paint the paint to be used for drawing @@ -860,11 +951,12 @@ protected void drawYLabels(Map> allYLabels, Canvas canvas, * @param maxX the maximum value on the X axis in the chart */ protected void drawXTextLabels(Double[] xTextLabelLocations, Canvas canvas, Paint paint, - boolean showLabels, int left, int top, int bottom, double xPixelsPerUnit, double minX, - double maxX) { + boolean showCustomXLabels, int left, int top, int bottom, double xPixelsPerUnit, double minX, + double maxX) { boolean showCustomTextGridX = mRenderer.isShowCustomTextGridX(); boolean showTickMarks = mRenderer.isShowTickMarks(); - if (showLabels) { + + if (showCustomXLabels) { paint.setColor(mRenderer.getXLabelsColor()); for (Double location : xTextLabelLocations) { if (minX <= location && location <= maxX) { @@ -872,11 +964,11 @@ protected void drawXTextLabels(Double[] xTextLabelLocations, Canvas canvas, Pain paint.setColor(mRenderer.getXLabelsColor()); if (showTickMarks) { canvas.drawLine(xLabel, bottom, xLabel, bottom + mRenderer.getLabelsTextSize() / 3, - paint); + paint); } drawText(canvas, mRenderer.getXTextLabel(location), xLabel, - bottom + mRenderer.getLabelsTextSize() * 4 / 3 + mRenderer.getXLabelsPadding(), - paint, mRenderer.getXLabelsAngle()); + bottom + mRenderer.getLabelsTextSize() * 4 / 3 + mRenderer.getXLabelsPadding(), + paint, mRenderer.getXLabelsAngle()); if (showCustomTextGridX) { paint.setColor(mRenderer.getGridColor(0)); canvas.drawLine(xLabel, bottom, xLabel, top, paint); @@ -921,7 +1013,7 @@ private int getLabelLinePos(Align align) { /** * Transforms a screen point to a real coordinates point. - * + * * @param screenX the screen x axis value * @param screenY the screen y axis value * @return the real coordinates point @@ -932,7 +1024,7 @@ public double[] toRealPoint(float screenX, float screenY, int scale) { double realMinY = mRenderer.getYAxisMin(scale); double realMaxY = mRenderer.getYAxisMax(scale); if (!mRenderer.isMinXSet(scale) || !mRenderer.isMaxXSet(scale) || !mRenderer.isMinYSet(scale) - || !mRenderer.isMaxYSet(scale)) { + || !mRenderer.isMaxYSet(scale)) { double[] calcRange = getCalcRange(scale); if (calcRange != null) { realMinX = calcRange[0]; @@ -943,9 +1035,9 @@ public double[] toRealPoint(float screenX, float screenY, int scale) { } if (mScreenR != null) { return new double[] { - (screenX - mScreenR.left) * (realMaxX - realMinX) / mScreenR.width() + realMinX, - (mScreenR.top + mScreenR.height() - screenY) * (realMaxY - realMinY) / mScreenR.height() - + realMinY }; + (screenX - mScreenR.left) * (realMaxX - realMinX) / mScreenR.width() + realMinX, + (mScreenR.top + mScreenR.height() - screenY) * (realMaxY - realMinY) / mScreenR.height() + + realMinY }; } else { return new double[] { screenX, screenY }; } @@ -957,7 +1049,7 @@ public double[] toScreenPoint(double[] realPoint, int scale) { double realMinY = mRenderer.getYAxisMin(scale); double realMaxY = mRenderer.getYAxisMax(scale); if (!mRenderer.isMinXSet(scale) || !mRenderer.isMaxXSet(scale) || !mRenderer.isMinYSet(scale) - || !mRenderer.isMaxYSet(scale)) { + || !mRenderer.isMaxYSet(scale)) { double[] calcRange = getCalcRange(scale); realMinX = calcRange[0]; realMaxX = calcRange[1]; @@ -966,8 +1058,8 @@ public double[] toScreenPoint(double[] realPoint, int scale) { } if (mScreenR != null) { return new double[] { - (realPoint[0] - realMinX) * mScreenR.width() / (realMaxX - realMinX) + mScreenR.left, - (realMaxY - realPoint[1]) * mScreenR.height() / (realMaxY - realMinY) + mScreenR.top }; + (realPoint[0] - realMinX) * mScreenR.width() / (realMaxX - realMinX) + mScreenR.left, + (realMaxY - realPoint[1]) * mScreenR.height() / (realMaxY - realMinY) + mScreenR.top }; } else { return realPoint; } @@ -999,7 +1091,7 @@ public SeriesSelection getSeriesAndPointForScreenCoordinate(final Point screenPo /** * The graphical representation of a series. - * + * * @param canvas the canvas to paint to * @param paint the paint to be used for drawing * @param points the array of points to be used for drawing the series @@ -1009,11 +1101,11 @@ public SeriesSelection getSeriesAndPointForScreenCoordinate(final Point screenPo * @param startIndex the start index of the rendering points */ public abstract void drawSeries(Canvas canvas, Paint paint, List points, - XYSeriesRenderer seriesRenderer, float yAxisValue, int seriesIndex, int startIndex); + XYSeriesRenderer seriesRenderer, float yAxisValue, int seriesIndex, int startIndex); /** * Returns the clickable areas for all passed points - * + * * @param points the array of points * @param values the array of values of each point * @param yAxisValue the minimum value of the y axis @@ -1022,11 +1114,11 @@ public abstract void drawSeries(Canvas canvas, Paint paint, List points, * @param startIndex the start index of the rendering points */ protected abstract ClickableArea[] clickableAreasForPoints(List points, - List values, float yAxisValue, int seriesIndex, int startIndex); + List values, float yAxisValue, int seriesIndex, int startIndex); /** * Returns if the chart should display the null values. - * + * * @return if null values should be rendered */ protected boolean isRenderNullValues() { @@ -1035,7 +1127,7 @@ protected boolean isRenderNullValues() { /** * Returns if the chart should display the points as a certain shape. - * + * * @param renderer the series renderer */ public boolean isRenderPoints(SimpleSeriesRenderer renderer) { @@ -1044,7 +1136,7 @@ public boolean isRenderPoints(SimpleSeriesRenderer renderer) { /** * Returns the default axis minimum. - * + * * @return the default axis minimum */ public double getDefaultMinimum() { @@ -1053,7 +1145,7 @@ public double getDefaultMinimum() { /** * Returns the scatter chart to be used for drawing the data points. - * + * * @return the data points scatter chart */ public ScatterChart getPointsChart() { @@ -1062,7 +1154,7 @@ public ScatterChart getPointsChart() { /** * Returns the chart type identifier. - * + * * @return the chart type */ public abstract String getChartType(); diff --git a/achartengine/src/org/achartengine/renderer/DefaultRenderer.java b/achartengine/src/org/achartengine/renderer/DefaultRenderer.java index 6c7c59b..238c4c6 100644 --- a/achartengine/src/org/achartengine/renderer/DefaultRenderer.java +++ b/achartengine/src/org/achartengine/renderer/DefaultRenderer.java @@ -1,12 +1,12 @@ /** * Copyright (C) 2009 - 2013 SC 4ViewSoft SRL - * + * * 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. @@ -38,7 +38,7 @@ public class DefaultRenderer implements Serializable { public static final int TEXT_COLOR = Color.LTGRAY; /** A text font for regular text, like the chart labels. */ private static final Typeface REGULAR_TEXT_FONT = Typeface - .create(Typeface.SERIF, Typeface.NORMAL); + .create(Typeface.SERIF, Typeface.NORMAL); /** The typeface name for the texts. */ private String mTextTypefaceName = REGULAR_TEXT_FONT.toString(); /** The typeface style for the texts. */ @@ -81,6 +81,10 @@ public class DefaultRenderer implements Serializable { private boolean mShowCustomTextGridX = false; /** If the custom text grid should be displayed on the Y axis. */ private boolean mShowCustomTextGridY = false; + /** If the custom text should be displayed on the X axis. */ + private boolean mShowCustomTextXLabels = true; + /** If the custom text should be displayed on the Y axis. */ + private boolean mShowCustomTextYLabels = true; /** The simple renderers that are included in this multiple series renderer. */ private List mRenderers = new ArrayList(); /** The antialiasing flag. */ @@ -120,7 +124,7 @@ public class DefaultRenderer implements Serializable { /** * Returns the chart title. - * + * * @return the chart title */ public String getChartTitle() { @@ -129,7 +133,7 @@ public String getChartTitle() { /** * Sets the chart title. - * + * * @param title the chart title */ public void setChartTitle(String title) { @@ -138,7 +142,7 @@ public void setChartTitle(String title) { /** * Returns the chart title text size. - * + * * @return the chart title text size */ public float getChartTitleTextSize() { @@ -147,7 +151,7 @@ public float getChartTitleTextSize() { /** * Sets the chart title text size. - * + * * @param textSize the chart title text size */ public void setChartTitleTextSize(float textSize) { @@ -156,7 +160,7 @@ public void setChartTitleTextSize(float textSize) { /** * Adds a simple renderer to the multiple renderer. - * + * * @param renderer the renderer to be added */ public void addSeriesRenderer(SimpleSeriesRenderer renderer) { @@ -165,7 +169,7 @@ public void addSeriesRenderer(SimpleSeriesRenderer renderer) { /** * Adds a simple renderer to the multiple renderer. - * + * * @param index the index in the renderers list * @param renderer the renderer to be added */ @@ -175,7 +179,7 @@ public void addSeriesRenderer(int index, SimpleSeriesRenderer renderer) { /** * Removes a simple renderer from the multiple renderer. - * + * * @param renderer the renderer to be removed */ public void removeSeriesRenderer(SimpleSeriesRenderer renderer) { @@ -191,7 +195,7 @@ public void removeAllRenderers() { /** * Returns the simple renderer from the multiple renderer list. - * + * * @param index the index in the simple renderers list * @return the simple renderer at the specified index */ @@ -201,7 +205,7 @@ public SimpleSeriesRenderer getSeriesRendererAt(int index) { /** * Returns the simple renderers count in the multiple renderer list. - * + * * @return the simple renderers count */ public int getSeriesRendererCount() { @@ -210,7 +214,7 @@ public int getSeriesRendererCount() { /** * Returns an array of the simple renderers in the multiple renderer list. - * + * * @return the simple renderers array */ public SimpleSeriesRenderer[] getSeriesRenderers() { @@ -219,7 +223,7 @@ public SimpleSeriesRenderer[] getSeriesRenderers() { /** * Returns the background color. - * + * * @return the background color */ public int getBackgroundColor() { @@ -228,7 +232,7 @@ public int getBackgroundColor() { /** * Sets the background color. - * + * * @param color the background color */ public void setBackgroundColor(int color) { @@ -237,7 +241,7 @@ public void setBackgroundColor(int color) { /** * Returns if the background color should be applied. - * + * * @return the apply flag for the background color. */ public boolean isApplyBackgroundColor() { @@ -246,7 +250,7 @@ public boolean isApplyBackgroundColor() { /** * Sets if the background color should be applied. - * + * * @param apply the apply flag for the background color */ public void setApplyBackgroundColor(boolean apply) { @@ -255,7 +259,7 @@ public void setApplyBackgroundColor(boolean apply) { /** * Returns the axes color. - * + * * @return the axes color */ public int getAxesColor() { @@ -268,7 +272,7 @@ public int getAxesColor() { /** * Sets the axes color. - * + * * @param color the axes color */ public void setAxesColor(int color) { @@ -278,7 +282,7 @@ public void setAxesColor(int color) { /** * Returns the color of the Y axis - * + * * @return the Y axis color */ public int getYAxisColor() { @@ -287,7 +291,7 @@ public int getYAxisColor() { /** * Sets the Y axis color. - * + * * @param color the Y axis color */ public void setYAxisColor(int color) { @@ -296,7 +300,7 @@ public void setYAxisColor(int color) { /** * Returns the color of the X axis - * + * * @return the X axis color */ public int getXAxisColor() { @@ -305,7 +309,7 @@ public int getXAxisColor() { /** * Sets the X axis color. - * + * * @param color the X axis color */ public void setXAxisColor(int color) { @@ -314,7 +318,7 @@ public void setXAxisColor(int color) { /** * Returns the labels color. - * + * * @return the labels color */ public int getLabelsColor() { @@ -323,7 +327,7 @@ public int getLabelsColor() { /** * Sets the labels color. - * + * * @param color the labels color */ public void setLabelsColor(int color) { @@ -332,7 +336,7 @@ public void setLabelsColor(int color) { /** * Returns the labels text size. - * + * * @return the labels text size */ public float getLabelsTextSize() { @@ -341,7 +345,7 @@ public float getLabelsTextSize() { /** * Sets the labels text size. - * + * * @param textSize the labels text size */ public void setLabelsTextSize(float textSize) { @@ -350,7 +354,7 @@ public void setLabelsTextSize(float textSize) { /** * Returns if the axes should be visible. - * + * * @return the visibility flag for the axes */ public boolean isShowAxes() { @@ -359,7 +363,7 @@ public boolean isShowAxes() { /** * Sets if the axes should be visible. - * + * * @param showAxes the visibility flag for the axes */ public void setShowAxes(boolean showAxes) { @@ -416,7 +420,7 @@ public void setShowLabels(boolean showLabels) { /** * Returns if the tick marks should be visible. - * + * * @return */ public boolean isShowTickMarks() { @@ -425,7 +429,7 @@ public boolean isShowTickMarks() { /** * Sets if the tick marks should be visible. - * + * * @param showTickMarks the visibility flag for the tick marks */ public void setShowTickMarks(boolean mShowTickMarks) { @@ -434,7 +438,7 @@ public void setShowTickMarks(boolean mShowTickMarks) { /** * Returns if the X axis grid should be visible. - * + * * @return the visibility flag for the X axis grid */ public boolean isShowGridX() { @@ -443,7 +447,7 @@ public boolean isShowGridX() { /** * Returns if the Y axis grid should be visible. - * + * * @return the visibility flag for the Y axis grid */ public boolean isShowGridY() { @@ -452,7 +456,7 @@ public boolean isShowGridY() { /** * Sets if the X axis grid should be visible. - * + * * @param showGrid the visibility flag for the X axis grid */ public void setShowGridX(boolean showGrid) { @@ -479,7 +483,7 @@ public float getGridLineWidth() { /** * Sets if the Y axis grid should be visible. - * + * * @param showGrid the visibility flag for the Y axis grid */ public void setShowGridY(boolean showGrid) { @@ -488,7 +492,7 @@ public void setShowGridY(boolean showGrid) { /** * Sets if the grid should be visible. - * + * * @param showGrid the visibility flag for the grid */ public void setShowGrid(boolean showGrid) { @@ -498,7 +502,7 @@ public void setShowGrid(boolean showGrid) { /** * Returns if the X axis custom text grid should be visible. - * + * * @return the visibility flag for the X axis custom text grid */ public boolean isShowCustomTextGridX() { @@ -507,7 +511,7 @@ public boolean isShowCustomTextGridX() { /** * Returns if the Y axis custom text grid should be visible. - * + * * @return the visibility flag for the custom text Y axis grid */ public boolean isShowCustomTextGridY() { @@ -516,7 +520,7 @@ public boolean isShowCustomTextGridY() { /** * Sets if the X axis custom text grid should be visible. - * + * * @param showGrid the visibility flag for the X axis custom text grid */ public void setShowCustomTextGridX(boolean showGrid) { @@ -525,16 +529,42 @@ public void setShowCustomTextGridX(boolean showGrid) { /** * Sets if the Y axis custom text grid should be visible. - * + * * @param showGrid the visibility flag for the Y axis custom text grid */ public void setShowCustomTextGridY(boolean showGrid) { mShowCustomTextGridY = showGrid; } + public boolean isShowCustomTextXLabels() { + return mShowCustomTextXLabels; + } + + public void setShowCustomTextXLabels(boolean mShowCustomTextXLabels) { + this.mShowCustomTextXLabels = mShowCustomTextXLabels; + } + + public boolean isShowCustomTextYLabels() { + return mShowCustomTextYLabels; + } + + public void setShowCustomTextYLabels(boolean mShowCustomTextYLabels) { + this.mShowCustomTextYLabels = mShowCustomTextYLabels; + } + + public void setShowCustomTextLabels(boolean mShowCustomTextLabels) { + this.mShowCustomTextXLabels = mShowCustomTextLabels; + this.mShowCustomTextYLabels = mShowCustomTextLabels; + } + + public void setShowCustomTextLabels(boolean showCustomTextXLabels, boolean showCustomTextYLabels) { + this.mShowCustomTextXLabels = showCustomTextXLabels; + this.mShowCustomTextYLabels = showCustomTextYLabels; + } + /** * Sets if the grid for custom X or Y labels should be visible. - * + * * @param showGrid the visibility flag for the custom text grid */ public void setShowCustomTextGrid(boolean showGrid) { @@ -544,7 +574,7 @@ public void setShowCustomTextGrid(boolean showGrid) { /** * Returns if the legend should be visible. - * + * * @return the visibility flag for the legend */ public boolean isShowLegend() { @@ -553,7 +583,7 @@ public boolean isShowLegend() { /** * Sets if the legend should be visible. - * + * * @param showLegend the visibility flag for the legend */ public void setShowLegend(boolean showLegend) { @@ -562,7 +592,7 @@ public void setShowLegend(boolean showLegend) { /** * Returns if the legend should size to fit. - * + * * @return the fit behavior */ public boolean isFitLegend() { @@ -571,7 +601,7 @@ public boolean isFitLegend() { /** * Sets if the legend should size to fit. - * + * * @param fit the fit behavior */ public void setFitLegend(boolean fit) { @@ -580,7 +610,7 @@ public void setFitLegend(boolean fit) { /** * Returns the text typeface name. - * + * * @return the text typeface name */ public String getTextTypefaceName() { @@ -589,7 +619,7 @@ public String getTextTypefaceName() { /** * Returns the text typeface style. - * + * * @return the text typeface style */ public int getTextTypefaceStyle() { @@ -598,7 +628,7 @@ public int getTextTypefaceStyle() { /** * Returns the text typeface. - * + * * @return the text typeface */ public Typeface getTextTypeface() { @@ -607,7 +637,7 @@ public Typeface getTextTypeface() { /** * Returns the legend text size. - * + * * @return the legend text size */ public float getLegendTextSize() { @@ -616,7 +646,7 @@ public float getLegendTextSize() { /** * Sets the legend text size. - * + * * @param textSize the legend text size */ public void setLegendTextSize(float textSize) { @@ -625,7 +655,7 @@ public void setLegendTextSize(float textSize) { /** * Sets the text typeface name and style. - * + * * @param typefaceName the text typeface name * @param style the text typeface style */ @@ -636,7 +666,7 @@ public void setTextTypeface(String typefaceName, int style) { /** * Sets the text typeface. - * + * * @param typeface the typeface */ public void setTextTypeface(Typeface typeface) { @@ -645,7 +675,7 @@ public void setTextTypeface(Typeface typeface) { /** * Returns the antialiasing flag value. - * + * * @return the antialiasing value */ public boolean isAntialiasing() { @@ -654,7 +684,7 @@ public boolean isAntialiasing() { /** * Sets the antialiasing value. - * + * * @param antialiasing the antialiasing */ public void setAntialiasing(boolean antialiasing) { @@ -663,7 +693,7 @@ public void setAntialiasing(boolean antialiasing) { /** * Returns the value to be used for scaling the chart. - * + * * @return the scale value */ public float getScale() { @@ -672,7 +702,7 @@ public float getScale() { /** * Returns the original value to be used for scaling the chart. - * + * * @return the original scale value */ public float getOriginalScale() { @@ -682,7 +712,7 @@ public float getOriginalScale() { /** * Sets the value to be used for scaling the chart. It works on some charts * like pie, doughnut, dial. - * + * * @param scale the scale value */ public void setScale(float scale) { @@ -691,7 +721,7 @@ public void setScale(float scale) { /** * Returns the enabled state of the zoom. - * + * * @return if zoom is enabled */ public boolean isZoomEnabled() { @@ -700,7 +730,7 @@ public boolean isZoomEnabled() { /** * Sets the enabled state of the zoom. - * + * * @param enabled zoom enabled */ public void setZoomEnabled(boolean enabled) { @@ -709,7 +739,7 @@ public void setZoomEnabled(boolean enabled) { /** * Returns the visible state of the zoom buttons. - * + * * @return if zoom buttons are visible */ public boolean isZoomButtonsVisible() { @@ -718,7 +748,7 @@ public boolean isZoomButtonsVisible() { /** * Sets the visible state of the zoom buttons. - * + * * @param visible if the zoom buttons are visible */ public void setZoomButtonsVisible(boolean visible) { @@ -727,7 +757,7 @@ public void setZoomButtonsVisible(boolean visible) { /** * Returns the enabled state of the external (application implemented) zoom. - * + * * @return if external zoom is enabled */ public boolean isExternalZoomEnabled() { @@ -736,7 +766,7 @@ public boolean isExternalZoomEnabled() { /** * Sets the enabled state of the external (application implemented) zoom. - * + * * @param enabled external zoom enabled */ public void setExternalZoomEnabled(boolean enabled) { @@ -745,7 +775,7 @@ public void setExternalZoomEnabled(boolean enabled) { /** * Returns the zoom rate. - * + * * @return the zoom rate */ public float getZoomRate() { @@ -754,7 +784,7 @@ public float getZoomRate() { /** * Returns the enabled state of the pan. - * + * * @return if pan is enabled */ public boolean isPanEnabled() { @@ -763,7 +793,7 @@ public boolean isPanEnabled() { /** * Sets the enabled state of the pan. - * + * * @param enabled pan enabled */ public void setPanEnabled(boolean enabled) { @@ -772,7 +802,7 @@ public void setPanEnabled(boolean enabled) { /** * Sets the zoom rate. - * + * * @param rate the zoom rate */ public void setZoomRate(float rate) { @@ -781,7 +811,7 @@ public void setZoomRate(float rate) { /** * Returns the enabled state of the click. - * + * * @return if click is enabled */ public boolean isClickEnabled() { @@ -790,7 +820,7 @@ public boolean isClickEnabled() { /** * Sets the enabled state of the click. - * + * * @param enabled click enabled */ public void setClickEnabled(boolean enabled) { @@ -799,7 +829,7 @@ public void setClickEnabled(boolean enabled) { /** * Returns the selectable radius value around clickable points. - * + * * @return the selectable radius */ public int getSelectableBuffer() { @@ -808,7 +838,7 @@ public int getSelectableBuffer() { /** * Sets the selectable radius value around clickable points. - * + * * @param buffer the selectable radius */ public void setSelectableBuffer(int buffer) { @@ -817,7 +847,7 @@ public void setSelectableBuffer(int buffer) { /** * Returns the legend height. - * + * * @return the legend height */ public int getLegendHeight() { @@ -826,7 +856,7 @@ public int getLegendHeight() { /** * Sets the legend height, in pixels. - * + * * @param height the legend height */ public void setLegendHeight(int height) { @@ -836,7 +866,7 @@ public void setLegendHeight(int height) { /** * Returns the margin sizes. An array containing the margins in this order: * top, left, bottom, right - * + * * @return the margin sizes */ public int[] getMargins() { @@ -845,7 +875,7 @@ public int[] getMargins() { /** * Sets the margins, in pixels. - * + * * @param margins an array containing the margin size values, in this order: * top, left, bottom, right */ @@ -855,7 +885,7 @@ public void setMargins(int[] margins) { /** * Returns if the chart is inside a scroll view and doesn't need to shrink. - * + * * @return if it is inside a scroll view */ public boolean isInScroll() { @@ -865,7 +895,7 @@ public boolean isInScroll() { /** * To be set if the chart is inside a scroll view and doesn't need to shrink * when not enough space. - * + * * @param inScroll if it is inside a scroll view */ public void setInScroll(boolean inScroll) { @@ -876,7 +906,7 @@ public void setInScroll(boolean inScroll) { * Returns the start angle for circular charts such as pie, doughnut. An angle * of 0 degrees correspond to the geometric angle of 0 degrees (3 o'clock on a * watch.) - * + * * @return the start angle in degrees */ public float getStartAngle() { @@ -887,7 +917,7 @@ public float getStartAngle() { * Sets the start angle for circular charts such as pie, doughnut, etc. An * angle of 0 degrees correspond to the geometric angle of 0 degrees (3 * o'clock on a watch.) - * + * * @param startAngle the start angle in degrees */ public void setStartAngle(float startAngle) { @@ -899,7 +929,7 @@ public void setStartAngle(float startAngle) { /** * Returns if the values should be displayed as text. - * + * * @return if the values should be displayed as text */ public boolean isDisplayValues() { @@ -908,7 +938,7 @@ public boolean isDisplayValues() { /** * Sets if the values should be displayed as text (supported by pie chart). - * + * * @param display if the values should be displayed as text */ public void setDisplayValues(boolean display) { From d0e243391545210690a710ea166d5b7e56ab0243 Mon Sep 17 00:00:00 2001 From: grgaaa Date: Fri, 9 Feb 2018 16:27:30 +0100 Subject: [PATCH 2/4] XYChart showGridX and showGridY flag used in opposite way --- .../src/org/achartengine/chart/XYChart.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/achartengine/src/org/achartengine/chart/XYChart.java b/achartengine/src/org/achartengine/chart/XYChart.java index 5ee7666..48f1580 100644 --- a/achartengine/src/org/achartengine/chart/XYChart.java +++ b/achartengine/src/org/achartengine/chart/XYChart.java @@ -829,9 +829,9 @@ protected void drawXLabels(List xLabels, Double[] xTextLabelLocations, C boolean showXLabels = mRenderer.isShowXLabels(); boolean showCustomXLabels = mRenderer.isShowCustomTextXLabels(); - boolean showGridY = mRenderer.isShowGridY(); + boolean showGridX = mRenderer.isShowGridX(); - if (showGridY) { + if (showGridX) { mGridPaint.setStyle(Style.STROKE); mGridPaint.setStrokeWidth(mRenderer.getGridLineWidth()); } @@ -850,7 +850,7 @@ protected void drawXLabels(List xLabels, Double[] xTextLabelLocations, C bottom + mRenderer.getLabelsTextSize() * 4 / 3 + mRenderer.getXLabelsPadding(), paint, mRenderer.getXLabelsAngle()); } - if (showGridY) { + if (showGridX) { mGridPaint.setColor(mRenderer.getGridColor(0)); canvas.drawLine(xLabel, bottom, xLabel, top, mGridPaint); } @@ -875,8 +875,8 @@ protected void drawXLabels(List xLabels, Double[] xTextLabelLocations, C protected void drawYLabels(Map> allYLabels, Canvas canvas, Paint paint, int maxScaleNumber, int left, int right, int bottom, double[] yPixelsPerUnit, double[] minY) { Orientation or = mRenderer.getOrientation(); - boolean showGridX = mRenderer.isShowGridX(); - if (showGridX) { + boolean showGridY = mRenderer.isShowGridY(); + if (showGridY) { mGridPaint.setStyle(Style.STROKE); mGridPaint.setStrokeWidth(mRenderer.getGridLineWidth()); } @@ -912,7 +912,7 @@ protected void drawYLabels(Map> allYLabels, Canvas canvas, mRenderer.getYLabelsAngle()); } } - if (showGridX) { + if (showGridY) { mGridPaint.setColor(mRenderer.getGridColor(i)); canvas.drawLine(left, yLabel, right, yLabel, mGridPaint); } @@ -926,7 +926,7 @@ protected void drawYLabels(Map> allYLabels, Canvas canvas, right + 10 + mRenderer.getYLabelsPadding(), yLabel - mRenderer.getYLabelsVerticalPadding(), paint, mRenderer.getYLabelsAngle()); } - if (showGridX) { + if (showGridY) { mGridPaint.setColor(mRenderer.getGridColor(i)); if (showTickMarks) { canvas.drawLine(right, yLabel, left, yLabel, mGridPaint); From dbdfd2ff0b875c7bd1584c68323ad436dbef7718 Mon Sep 17 00:00:00 2001 From: Gregor Horvat Date: Wed, 14 Feb 2018 11:10:22 +0100 Subject: [PATCH 3/4] separate show & hide labels (auto-labels/custom label) --- .../src/org/achartengine/chart/XYChart.java | 105 +++++------------- 1 file changed, 30 insertions(+), 75 deletions(-) diff --git a/achartengine/src/org/achartengine/chart/XYChart.java b/achartengine/src/org/achartengine/chart/XYChart.java index 5ee7666..e43843f 100644 --- a/achartengine/src/org/achartengine/chart/XYChart.java +++ b/achartengine/src/org/achartengine/chart/XYChart.java @@ -414,72 +414,20 @@ public void draw(Canvas canvas, int x, int y, int width, int height, Paint paint } } - if (showYLabels) { - Map> allYLabels = getYLabels(minY, maxY, maxScaleNumber); - - paint.setColor(mRenderer.getXLabelsColor()); - paint.setTextSize(mRenderer.getLabelsTextSize()); - paint.setTextAlign(mRenderer.getXLabelsAlign()); - - // Draw just the labels and not the grid lines. - mRenderer.setShowGrid(false); - drawYLabels(allYLabels, canvas, paint, maxScaleNumber, left, right, bottom, yPixelsPerUnit, minY); - mRenderer.setShowGridY(showGridY); - - paint.setColor(mRenderer.getLabelsColor()); - float size = mRenderer.getAxisTitleTextSize(); - paint.setTextSize(size); - paint.setTextAlign(Align.CENTER); - if (or == Orientation.HORIZONTAL) { - for (int i = 0; i < maxScaleNumber; i++) { - Align axisAlign = mRenderer.getYAxisAlign(i); - if (axisAlign == Align.LEFT) { - drawText(canvas, mRenderer.getYTitle(i), x + size, y + height / 2, paint, -90); - } else { - drawText(canvas, mRenderer.getYTitle(i), x + width, y + height / 2, paint, -90); - } - } - } else if (or == Orientation.VERTICAL) { - drawText(canvas, mRenderer.getYTitle(), right + 20, y + height / 2, paint, 0); - } - } - - if (showLabels) { - paint.setColor(mRenderer.getLabelsColor()); - float axisTitleTextSize = mRenderer.getAxisTitleTextSize(); - paint.setTextSize(axisTitleTextSize); - paint.setTextAlign(Align.CENTER); - paint.setTextSize(mRenderer.getChartTitleTextSize()); - - if (or == Orientation.HORIZONTAL) { - drawText(canvas, mRenderer.getChartTitle(), x + width / 2, - y + mRenderer.getChartTitleTextSize(), paint, 0); - - } else if (or == Orientation.VERTICAL) { - drawText(canvas, mRenderer.getChartTitle(), x + axisTitleTextSize, top + height / 2, paint, 0); - } + if (showCustomTextXLabels) { + drawXTextLabels(mRenderer.getXTextLabelLocations(), canvas, paint, true, left, top, bottom, xPixelsPerUnit[0], minX[0], maxX[0]); } - - if (showLabels) { - List xLabels = getValidLabels(getXLabels(minX[0], maxX[0], mRenderer.getXLabels())); + if (showYLabels) { Map> allYLabels = getYLabels(minY, maxY, maxScaleNumber); - int xLabelsLeft = left; - paint.setColor(mRenderer.getXLabelsColor()); paint.setTextSize(mRenderer.getLabelsTextSize()); paint.setTextAlign(mRenderer.getXLabelsAlign()); - // if (mRenderer.getXLabelsAlign() == Align.LEFT) { - // xLabelsLeft += mRenderer.getLabelsTextSize() / 4; - // } // Draw just the labels and not the grid lines. mRenderer.setShowGrid(false); - drawXLabels(xLabels, mRenderer.getXTextLabelLocations(), canvas, paint, xLabelsLeft, top, - bottom, xPixelsPerUnit[0], minX[0], maxX[0]); drawYLabels(allYLabels, canvas, paint, maxScaleNumber, left, right, bottom, yPixelsPerUnit, minY); - mRenderer.setShowGridX(showGridX); mRenderer.setShowGridY(showGridY); paint.setColor(mRenderer.getLabelsColor()); @@ -487,12 +435,6 @@ public void draw(Canvas canvas, int x, int y, int width, int height, Paint paint paint.setTextSize(size); paint.setTextAlign(Align.CENTER); if (or == Orientation.HORIZONTAL) { - drawText( - canvas, - mRenderer.getXTitle(), - x + width / 2, - bottom + mRenderer.getLabelsTextSize() * 4 / 3 + mRenderer.getXLabelsPadding() + size, - paint, 0); for (int i = 0; i < maxScaleNumber; i++) { Align axisAlign = mRenderer.getYAxisAlign(i); if (axisAlign == Align.LEFT) { @@ -501,23 +443,16 @@ public void draw(Canvas canvas, int x, int y, int width, int height, Paint paint drawText(canvas, mRenderer.getYTitle(i), x + width, y + height / 2, paint, -90); } } - paint.setTextSize(mRenderer.getChartTitleTextSize()); - drawText(canvas, mRenderer.getChartTitle(), x + width / 2, - y + mRenderer.getChartTitleTextSize(), paint, 0); } else if (or == Orientation.VERTICAL) { - drawText(canvas, mRenderer.getXTitle(), x + width / 2, - y + height - size + mRenderer.getXLabelsPadding(), paint, -90); drawText(canvas, mRenderer.getYTitle(), right + 20, y + height / 2, paint, 0); - paint.setTextSize(mRenderer.getChartTitleTextSize()); - drawText(canvas, mRenderer.getChartTitle(), x + size, top + height / 2, paint, 0); } - - } else if (showCustomTextXLabels) { - drawXTextLabels(mRenderer.getXTextLabelLocations(), canvas, paint, true, left, top, bottom, xPixelsPerUnit[0], minX[0], maxX[0]); } if (showCustomTextYLabels) { paint.setColor(mRenderer.getLabelsColor()); + paint.setTextSize(mRenderer.getLabelsTextSize()); + paint.setTextAlign(mRenderer.getXLabelsAlign()); + for (int i = 0; i < maxScaleNumber; i++) { Align axisAlign = mRenderer.getYAxisAlign(i); Double[] yTextLabelLocations = mRenderer.getYTextLabelLocations(i); @@ -566,6 +501,22 @@ public void draw(Canvas canvas, int x, int y, int width, int height, Paint paint } } + if (showLabels) { + paint.setColor(mRenderer.getLabelsColor()); + float axisTitleTextSize = mRenderer.getAxisTitleTextSize(); + paint.setTextSize(axisTitleTextSize); + paint.setTextAlign(Align.CENTER); + paint.setTextSize(mRenderer.getChartTitleTextSize()); + + if (or == Orientation.HORIZONTAL) { + drawText(canvas, mRenderer.getChartTitle(), x + width / 2, + y + mRenderer.getChartTitleTextSize(), paint, 0); + + } else if (or == Orientation.VERTICAL) { + drawText(canvas, mRenderer.getChartTitle(), x + axisTitleTextSize, top + height / 2, paint, 0); + } + } + if (or == Orientation.HORIZONTAL) { drawLegend(canvas, mRenderer, titles, left, right, y + (int) mRenderer.getXLabelsPadding(), @@ -953,11 +904,16 @@ protected void drawYLabels(Map> allYLabels, Canvas canvas, protected void drawXTextLabels(Double[] xTextLabelLocations, Canvas canvas, Paint paint, boolean showCustomXLabels, int left, int top, int bottom, double xPixelsPerUnit, double minX, double maxX) { - boolean showCustomTextGridX = mRenderer.isShowCustomTextGridX(); - boolean showTickMarks = mRenderer.isShowTickMarks(); + if (!showCustomXLabels) { + return; + } + boolean showCustomTextGridX = mRenderer.isShowCustomTextGridX(); + boolean showTickMarks = mRenderer.isShowTickMarks(); - if (showCustomXLabels) { paint.setColor(mRenderer.getXLabelsColor()); + paint.setTextSize(mRenderer.getLabelsTextSize()); + paint.setTextAlign(mRenderer.getXLabelsAlign()); + for (Double location : xTextLabelLocations) { if (minX <= location && location <= maxX) { float xLabel = (float) (left + xPixelsPerUnit * (location.doubleValue() - minX)); @@ -975,7 +931,6 @@ protected void drawXTextLabels(Double[] xTextLabelLocations, Canvas canvas, Pain } } } - } } // TODO: docs From 812157b33d9003c8b0c4a8f815af4489c07a83be Mon Sep 17 00:00:00 2001 From: Gregor Horvat Date: Tue, 20 Feb 2018 10:51:00 +0100 Subject: [PATCH 4/4] added methods to set grid lines stroke (replaced canvas.drawLine with canvas.drawPath because dashed strokes don't work with hardware acceleration enabled + canvas.drawLine) --- .../src/org/achartengine/chart/XYChart.java | 151 ++++++++++-------- .../renderer/DefaultRenderer.java | 49 +++++- 2 files changed, 134 insertions(+), 66 deletions(-) diff --git a/achartengine/src/org/achartengine/chart/XYChart.java b/achartengine/src/org/achartengine/chart/XYChart.java index ce2f704..a47b10f 100644 --- a/achartengine/src/org/achartengine/chart/XYChart.java +++ b/achartengine/src/org/achartengine/chart/XYChart.java @@ -15,17 +15,9 @@ */ package org.achartengine.chart; -import android.graphics.Canvas; -import android.graphics.DashPathEffect; -import android.graphics.Paint; +import android.graphics.*; import android.graphics.Paint.Align; -import android.graphics.Paint.Cap; -import android.graphics.Paint.Join; import android.graphics.Paint.Style; -import android.graphics.PathEffect; -import android.graphics.Rect; -import android.graphics.RectF; -import android.graphics.Typeface; import org.achartengine.model.Point; import org.achartengine.model.SeriesSelection; @@ -75,6 +67,11 @@ public abstract class XYChart extends AbstractChart { */ private Map> clickableAreas = new HashMap>(); + { + mGridPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + mGridPaint.setStyle(Style.STROKE); + } + protected XYChart() { } @@ -242,16 +239,13 @@ public void draw(Canvas canvas, int x, int y, int width, int height, Paint paint List xLabels = getValidLabels(getXLabels(minX[0], maxX[0], mRenderer.getXLabels())); Map> allYLabels = getYLabels(minY, maxY, maxScaleNumber); - int xLabelsLeft = left; boolean showXLabels = mRenderer.isShowXLabels(); boolean showYLabels = mRenderer.isShowYLabels(); // Only draw the grid. mRenderer.setShowLabels(false); mRenderer.setShowCustomTextLabels(false); - if (mGridPaint == null) { - mGridPaint = new Paint(Paint.ANTI_ALIAS_FLAG); - } - drawXLabels(xLabels, mRenderer.getXTextLabelLocations(), canvas, paint, xLabelsLeft, top, + + drawXLabels(xLabels, mRenderer.getXTextLabelLocations(), canvas, paint, left, top, bottom, xPixelsPerUnit[0], minX[0], maxX[0]); drawYLabels(allYLabels, canvas, paint, maxScaleNumber, left, right, bottom, yPixelsPerUnit, minY); @@ -450,8 +444,8 @@ public void draw(Canvas canvas, int x, int y, int width, int height, Paint paint if (showCustomTextYLabels) { paint.setColor(mRenderer.getLabelsColor()); - paint.setTextSize(mRenderer.getLabelsTextSize()); - paint.setTextAlign(mRenderer.getXLabelsAlign()); + paint.setTextSize(mRenderer.getLabelsTextSize()); + paint.setTextAlign(mRenderer.getXLabelsAlign()); for (int i = 0; i < maxScaleNumber; i++) { Align axisAlign = mRenderer.getYAxisAlign(i); @@ -463,6 +457,7 @@ public void draw(Canvas canvas, int x, int y, int width, int height, Paint paint String label = mRenderer.getYTextLabel(location, i); paint.setColor(mRenderer.getYLabelsColor(i)); paint.setTextAlign(mRenderer.getYLabelsAlign(i)); + if (or == Orientation.HORIZONTAL) { if (axisAlign == Align.LEFT) { if (showTickMarks) { @@ -480,10 +475,16 @@ public void draw(Canvas canvas, int x, int y, int width, int height, Paint paint yLabel - mRenderer.getYLabelsVerticalPadding(), paint, mRenderer.getYLabelsAngle()); } - if (showCustomTextGridY) { - paint.setColor(mRenderer.getGridColor(i)); - canvas.drawLine(left, yLabel, right, yLabel, paint); + setStroke(mRenderer.getGridLineYStroke(), mGridPaint); + mGridPaint.setStrokeWidth(mRenderer.getGridLineWidth()); + mGridPaint.setColor(mRenderer.getGridColor(i)); + + Path gridLinePath = new Path(); + gridLinePath.moveTo(left, yLabel); + gridLinePath.lineTo(right, yLabel); + canvas.drawPath(gridLinePath, mGridPaint); + } } else { if (showTickMarks) { @@ -492,8 +493,14 @@ public void draw(Canvas canvas, int x, int y, int width, int height, Paint paint drawText(canvas, label, right + 10, yLabel - mRenderer.getYLabelsVerticalPadding(), paint, mRenderer.getYLabelsAngle()); if (showCustomTextGridY) { - paint.setColor(mRenderer.getGridColor(i)); - canvas.drawLine(right, yLabel, left, yLabel, paint); + setStroke(mRenderer.getGridLineYStroke(), mGridPaint); + mGridPaint.setStrokeWidth(mRenderer.getGridLineWidth()); + mGridPaint.setColor(mRenderer.getGridColor(i)); + + Path gridLinePath = new Path(); + gridLinePath.moveTo(right, yLabel); + gridLinePath.lineTo(left, yLabel); + canvas.drawPath(gridLinePath, mGridPaint); } } } @@ -596,36 +603,27 @@ private List getValidLabels(List labels) { protected void drawSeries(XYSeries series, Canvas canvas, Paint paint, List pointsList, XYSeriesRenderer seriesRenderer, float yAxisValue, int seriesIndex, Orientation or, int startIndex) { + + Paint paintCopy = new Paint(paint); + BasicStroke stroke = seriesRenderer.getStroke(); - Cap cap = paint.getStrokeCap(); - Join join = paint.getStrokeJoin(); - float miter = paint.getStrokeMiter(); - PathEffect pathEffect = paint.getPathEffect(); - Style style = paint.getStyle(); + if (stroke != null) { - PathEffect effect = null; - if (stroke.getIntervals() != null) { - effect = new DashPathEffect(stroke.getIntervals(), stroke.getPhase()); - } - setStroke(stroke.getCap(), stroke.getJoin(), stroke.getMiter(), Style.FILL_AND_STROKE, - effect, paint); + setStroke(stroke, paintCopy); } // float[] points = MathHelper.getFloats(pointsList); - drawSeries(canvas, paint, pointsList, seriesRenderer, yAxisValue, seriesIndex, startIndex); - drawPoints(canvas, paint, pointsList, seriesRenderer, yAxisValue, seriesIndex, startIndex); - paint.setTextSize(seriesRenderer.getChartValuesTextSize()); + drawSeries(canvas, paintCopy, pointsList, seriesRenderer, yAxisValue, seriesIndex, startIndex); + drawPoints(canvas, paintCopy, pointsList, seriesRenderer, yAxisValue, seriesIndex, startIndex); + + paintCopy.setTextSize(seriesRenderer.getChartValuesTextSize()); if (or == Orientation.HORIZONTAL) { - paint.setTextAlign(Align.CENTER); + paintCopy.setTextAlign(Align.CENTER); } else { - paint.setTextAlign(Align.LEFT); + paintCopy.setTextAlign(Align.LEFT); } if (seriesRenderer.isDisplayChartValues()) { - paint.setTextAlign(seriesRenderer.getChartValuesTextAlign()); - drawChartValuesText(canvas, series, seriesRenderer, paint, pointsList, seriesIndex, - startIndex); - } - if (stroke != null) { - setStroke(cap, join, miter, style, pathEffect, paint); + paintCopy.setTextAlign(seriesRenderer.getChartValuesTextAlign()); + drawChartValuesText(canvas, series, seriesRenderer, paintCopy, pointsList, seriesIndex, startIndex); } } @@ -651,13 +649,19 @@ protected void drawPoints(Canvas canvas, Paint paint, List pointsList, } } - private void setStroke(Cap cap, Join join, float miter, Style style, PathEffect pathEffect, - Paint paint) { - paint.setStrokeCap(cap); - paint.setStrokeJoin(join); - paint.setStrokeMiter(miter); - paint.setPathEffect(pathEffect); - paint.setStyle(style); + private void setStroke(BasicStroke stroke, Paint paint) { + if (stroke == null || paint == null) { + return; + } + paint.setStrokeCap(stroke.getCap()); + paint.setStrokeJoin(stroke.getJoin()); + paint.setStrokeMiter(stroke.getMiter()); + + if (stroke.getIntervals() != null) { + DashPathEffect effect = new DashPathEffect(stroke.getIntervals(), stroke.getPhase()); + paint.setPathEffect(effect); + } + paint.setStyle(Style.FILL_AND_STROKE); } /** @@ -781,12 +785,8 @@ protected void drawXLabels(List xLabels, Double[] xTextLabelLocations, C boolean showXLabels = mRenderer.isShowXLabels(); boolean showCustomXLabels = mRenderer.isShowCustomTextXLabels(); boolean showGridX = mRenderer.isShowGridX(); - - if (showGridX) { - mGridPaint.setStyle(Style.STROKE); - mGridPaint.setStrokeWidth(mRenderer.getGridLineWidth()); - } boolean showTickMarks = mRenderer.isShowTickMarks(); + for (int i = 0; i < xLabels.size(); i++) { double label = xLabels.get(i); float xLabel = (float) (left + xPixelsPerUnit * (label - minX)); @@ -802,8 +802,14 @@ protected void drawXLabels(List xLabels, Double[] xTextLabelLocations, C mRenderer.getXLabelsAngle()); } if (showGridX) { + setStroke(mRenderer.getGridLineXStroke(), mGridPaint); + mGridPaint.setStrokeWidth(mRenderer.getGridLineWidth()); mGridPaint.setColor(mRenderer.getGridColor(0)); - canvas.drawLine(xLabel, bottom, xLabel, top, mGridPaint); + + Path gridLinePath = new Path(); + gridLinePath.moveTo(xLabel, bottom); + gridLinePath.lineTo(xLabel, top); + canvas.drawPath(gridLinePath, mGridPaint); } } drawXTextLabels(xTextLabelLocations, canvas, paint, showCustomXLabels, left, top, bottom, @@ -825,23 +831,23 @@ protected void drawXLabels(List xLabels, Double[] xTextLabelLocations, C */ protected void drawYLabels(Map> allYLabels, Canvas canvas, Paint paint, int maxScaleNumber, int left, int right, int bottom, double[] yPixelsPerUnit, double[] minY) { + Orientation or = mRenderer.getOrientation(); boolean showGridY = mRenderer.isShowGridY(); - if (showGridY) { - mGridPaint.setStyle(Style.STROKE); - mGridPaint.setStrokeWidth(mRenderer.getGridLineWidth()); - } boolean showYLabels = mRenderer.isShowYLabels(); boolean showTickMarks = mRenderer.isShowTickMarks(); + for (int i = 0; i < maxScaleNumber; i++) { paint.setTextAlign(mRenderer.getYLabelsAlign(i)); List yLabels = allYLabels.get(i); int length = yLabels.size(); + for (int j = 0; j < length; j++) { double label = yLabels.get(j); Align axisAlign = mRenderer.getYAxisAlign(i); boolean textLabel = mRenderer.getYTextLabel(label, i) != null; float yLabel = (float) (bottom - yPixelsPerUnit[i] * (label - minY[i])); + if (or == Orientation.HORIZONTAL) { if (showYLabels && !textLabel) { paint.setColor(mRenderer.getYLabelsColor(i)); @@ -864,8 +870,14 @@ protected void drawYLabels(Map> allYLabels, Canvas canvas, } } if (showGridY) { + setStroke(mRenderer.getGridLineYStroke(), mGridPaint); + mGridPaint.setStrokeWidth(mRenderer.getGridLineWidth()); mGridPaint.setColor(mRenderer.getGridColor(i)); - canvas.drawLine(left, yLabel, right, yLabel, mGridPaint); + + Path gridLinePath = new Path(); + gridLinePath.moveTo(left, yLabel); + gridLinePath.lineTo(right, yLabel); + canvas.drawPath(gridLinePath, mGridPaint); } } else if (or == Orientation.VERTICAL) { if (showYLabels && !textLabel) { @@ -914,7 +926,7 @@ protected void drawXTextLabels(Double[] xTextLabelLocations, Canvas canvas, Pain paint.setTextSize(mRenderer.getLabelsTextSize()); paint.setTextAlign(mRenderer.getXLabelsAlign()); - for (Double location : xTextLabelLocations) { + for (Double location : xTextLabelLocations) { if (minX <= location && location <= maxX) { float xLabel = (float) (left + xPixelsPerUnit * (location.doubleValue() - minX)); paint.setColor(mRenderer.getXLabelsColor()); @@ -925,9 +937,20 @@ protected void drawXTextLabels(Double[] xTextLabelLocations, Canvas canvas, Pain drawText(canvas, mRenderer.getXTextLabel(location), xLabel, bottom + mRenderer.getLabelsTextSize() * 4 / 3 + mRenderer.getXLabelsPadding(), paint, mRenderer.getXLabelsAngle()); + if (showCustomTextGridX) { - paint.setColor(mRenderer.getGridColor(0)); - canvas.drawLine(xLabel, bottom, xLabel, top, paint); + if (minX == location) { + setStroke(BasicStroke.SOLID, mGridPaint); + } else { + setStroke(mRenderer.getGridLineXStroke(), mGridPaint); + } + mGridPaint.setStrokeWidth(mRenderer.getGridLineWidth()); + mGridPaint.setColor(mRenderer.getGridColor(0)); + + Path gridLinePath = new Path(); + gridLinePath.moveTo(xLabel, bottom); + gridLinePath.lineTo(xLabel, top); + canvas.drawPath(gridLinePath, mGridPaint); } } } diff --git a/achartengine/src/org/achartengine/renderer/DefaultRenderer.java b/achartengine/src/org/achartengine/renderer/DefaultRenderer.java index 238c4c6..84f26b7 100644 --- a/achartengine/src/org/achartengine/renderer/DefaultRenderer.java +++ b/achartengine/src/org/achartengine/renderer/DefaultRenderer.java @@ -77,6 +77,10 @@ public class DefaultRenderer implements Serializable { private boolean mShowGridY = false; /** The grid width. */ private float mGridLineWidth; + /** Grid X stroke */ + private BasicStroke mGridLineXStroke; + /** Grid Y stroke */ + private BasicStroke mGridLineYStroke; /** If the custom text grid should be displayed on the X axis. */ private boolean mShowCustomTextGridX = false; /** If the custom text grid should be displayed on the Y axis. */ @@ -432,8 +436,8 @@ public boolean isShowTickMarks() { * * @param showTickMarks the visibility flag for the tick marks */ - public void setShowTickMarks(boolean mShowTickMarks) { - this.mShowTickMarks = mShowTickMarks; + public void setShowTickMarks(boolean showTickMarks) { + this.mShowTickMarks = showTickMarks; } /** @@ -481,6 +485,47 @@ public float getGridLineWidth() { return mGridLineWidth; } + /** + * Gets the grid line X stroke. + * @return the grid line X stroke + */ + public BasicStroke getGridLineXStroke() { + return mGridLineXStroke; + } + + /** + * Sets the grid line X stroke. + * @param gridLineXStroke grid line X stroke + */ + public void setGridLineXStroke(BasicStroke gridLineXStroke) { + this.mGridLineXStroke = gridLineXStroke; + } + + /** + * Gets the grid line Y stroke. + * @return the grid line Y stroke + */ + public BasicStroke getGridLineYStroke() { + return mGridLineYStroke; + } + + /** + * Sets the grid line Y stroke. + * @param gridLineYStroke grid line Y stroke + */ + public void setGridLineYStroke(BasicStroke gridLineYStroke) { + this.mGridLineYStroke = gridLineYStroke; + } + + /** + * Sets the stroke for X and Y grid lines. + * @param gridLineStroke grid line stroke for X and Y + */ + public void setGridLineStroke(BasicStroke gridLineStroke) { + this.mGridLineXStroke = gridLineStroke; + this.mGridLineYStroke = gridLineStroke; + } + /** * Sets if the Y axis grid should be visible. *