Search in sources :

Example 6 with Point

use of org.achartengine.model.Point in project Anki-Android by Ramblurr.

the class AbstractChart method drawPath.

/**
   * The graphical representation of a path.
   * 
   * @param canvas the canvas to paint to
   * @param points the points that are contained in the path to paint
   * @param paint the paint to be used for painting
   * @param circular if the path ends with the start point
   */
protected void drawPath(Canvas canvas, float[] points, Paint paint, boolean circular) {
    Path path = new Path();
    int height = canvas.getHeight();
    int width = canvas.getWidth();
    float[] tempDrawPoints;
    if (points.length < 4) {
        return;
    }
    tempDrawPoints = calculateDrawPoints(points[0], points[1], points[2], points[3], height, width);
    path.moveTo(tempDrawPoints[0], tempDrawPoints[1]);
    path.lineTo(tempDrawPoints[2], tempDrawPoints[3]);
    for (int i = 4; i < points.length; i += 2) {
        if ((points[i - 1] < 0 && points[i + 1] < 0) || (points[i - 1] > height && points[i + 1] > height)) {
            continue;
        }
        tempDrawPoints = calculateDrawPoints(points[i - 2], points[i - 1], points[i], points[i + 1], height, width);
        if (!circular) {
            path.moveTo(tempDrawPoints[0], tempDrawPoints[1]);
        }
        path.lineTo(tempDrawPoints[2], tempDrawPoints[3]);
    }
    if (circular) {
        path.lineTo(points[0], points[1]);
    }
    canvas.drawPath(path, paint);
}
Also used : Path(android.graphics.Path) Point(org.achartengine.model.Point) Paint(android.graphics.Paint)

Example 7 with Point

use of org.achartengine.model.Point in project Anki-Android by Ramblurr.

the class XYChart method draw.

/**
   * 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
   * @param width the width of the view to draw to
   * @param height the height of the view to draw to
   * @param paint the paint
   */
public void draw(Canvas canvas, int x, int y, int width, int height, Paint paint) {
    paint.setAntiAlias(mRenderer.isAntialiasing());
    int legendSize = getLegendSize(mRenderer, height / 5, mRenderer.getAxisTitleTextSize());
    int[] margins = mRenderer.getMargins();
    int left = x + margins[1];
    int top = y + margins[0];
    int right = x + width - margins[3];
    int sLength = mDataset.getSeriesCount();
    String[] titles = new String[sLength];
    for (int i = 0; i < sLength; i++) {
        titles[i] = mDataset.getSeriesAt(i).getTitle();
    }
    if (mRenderer.isFitLegend() && mRenderer.isShowLegend()) {
        legendSize = drawLegend(canvas, mRenderer, titles, left, right, y, width, height, legendSize, paint, true);
    }
    int bottom = y + height - margins[2] - legendSize;
    if (mScreenR == null) {
        mScreenR = new Rect();
    }
    mScreenR.set(left, top, right, bottom);
    drawBackground(mRenderer, canvas, x, y, width, height, paint, false, DefaultRenderer.NO_COLOR);
    if (paint.getTypeface() == null || !paint.getTypeface().toString().equals(mRenderer.getTextTypefaceName()) || paint.getTypeface().getStyle() != mRenderer.getTextTypefaceStyle()) {
        paint.setTypeface(Typeface.create(mRenderer.getTextTypefaceName(), mRenderer.getTextTypefaceStyle()));
    }
    Orientation or = mRenderer.getOrientation();
    if (or == Orientation.VERTICAL) {
        right -= legendSize;
        bottom += legendSize - 20;
    }
    int angle = or.getAngle();
    boolean rotate = angle == 90;
    mScale = (float) (height) / width;
    mTranslate = Math.abs(width - height) / 2;
    if (mScale < 1) {
        mTranslate *= -1;
    }
    mCenter = new Point((x + width) / 2, (y + height) / 2);
    if (rotate) {
        transform(canvas, angle, false);
    }
    int maxScaleNumber = -Integer.MAX_VALUE;
    for (int i = 0; i < sLength; i++) {
        maxScaleNumber = Math.max(maxScaleNumber, mDataset.getSeriesAt(i).getScaleNumber());
    }
    maxScaleNumber++;
    if (maxScaleNumber < 0) {
        return;
    }
    double[] minX = new double[maxScaleNumber];
    double[] maxX = new double[maxScaleNumber];
    double[] minY = new double[maxScaleNumber];
    double[] maxY = new double[maxScaleNumber];
    boolean[] isMinXSet = new boolean[maxScaleNumber];
    boolean[] isMaxXSet = new boolean[maxScaleNumber];
    boolean[] isMinYSet = new boolean[maxScaleNumber];
    boolean[] isMaxYSet = new boolean[maxScaleNumber];
    for (int i = 0; i < maxScaleNumber; i++) {
        minX[i] = mRenderer.getXAxisMin(i);
        maxX[i] = mRenderer.getXAxisMax(i);
        minY[i] = mRenderer.getYAxisMin(i);
        maxY[i] = mRenderer.getYAxisMax(i);
        isMinXSet[i] = mRenderer.isMinXSet(i);
        isMaxXSet[i] = mRenderer.isMaxXSet(i);
        isMinYSet[i] = mRenderer.isMinYSet(i);
        isMaxYSet[i] = mRenderer.isMaxYSet(i);
        if (mCalcRange.get(i) == null) {
            mCalcRange.put(i, new double[4]);
        }
    }
    double[] xPixelsPerUnit = new double[maxScaleNumber];
    double[] yPixelsPerUnit = new double[maxScaleNumber];
    for (int i = 0; i < sLength; i++) {
        XYSeries series = mDataset.getSeriesAt(i);
        int scale = series.getScaleNumber();
        if (series.getItemCount() == 0) {
            continue;
        }
        if (!isMinXSet[scale]) {
            double minimumX = series.getMinX();
            minX[scale] = Math.min(minX[scale], minimumX);
            mCalcRange.get(scale)[0] = minX[scale];
        }
        if (!isMaxXSet[scale]) {
            double maximumX = series.getMaxX();
            maxX[scale] = Math.max(maxX[scale], maximumX);
            mCalcRange.get(scale)[1] = maxX[scale];
        }
        if (!isMinYSet[scale]) {
            double minimumY = series.getMinY();
            minY[scale] = Math.min(minY[scale], (float) minimumY);
            mCalcRange.get(scale)[2] = minY[scale];
        }
        if (!isMaxYSet[scale]) {
            double maximumY = series.getMaxY();
            maxY[scale] = Math.max(maxY[scale], (float) maximumY);
            mCalcRange.get(scale)[3] = maxY[scale];
        }
    }
    for (int i = 0; i < maxScaleNumber; i++) {
        if (maxX[i] - minX[i] != 0) {
            xPixelsPerUnit[i] = (right - left) / (maxX[i] - minX[i]);
        }
        if (maxY[i] - minY[i] != 0) {
            yPixelsPerUnit[i] = (float) ((bottom - top) / (maxY[i] - minY[i]));
        }
    }
    boolean hasValues = false;
    // use a linked list for these reasons:
    // 1) Avoid a large contiguous memory allocation
    // 2) We don't need random seeking, only sequential reading/writing, so
    // linked list makes sense
    clickableAreas = new HashMap<Integer, List<ClickableArea>>();
    for (int i = 0; i < sLength; i++) {
        XYSeries series = mDataset.getSeriesAt(i);
        int scale = series.getScaleNumber();
        if (series.getItemCount() == 0) {
            continue;
        }
        hasValues = true;
        SimpleSeriesRenderer seriesRenderer = mRenderer.getSeriesRendererAt(i);
        // int originalValuesLength = series.getItemCount();
        // int valuesLength = originalValuesLength;
        // int length = valuesLength * 2;
        List<Float> points = new ArrayList<Float>();
        List<Double> values = new ArrayList<Double>();
        float yAxisValue = Math.min(bottom, (float) (bottom + yPixelsPerUnit[scale] * minY[scale]));
        LinkedList<ClickableArea> clickableArea = new LinkedList<ClickableArea>();
        clickableAreas.put(i, clickableArea);
        SortedMap<Double, Double> range = series.getRange(minX[scale], maxX[scale], 1);
        int startIndex = -1;
        int intRange = range.lastKey().intValue() - range.firstKey().intValue();
        for (Entry<Double, Double> value : range.entrySet()) {
            double xValue = value.getKey();
            double yValue = value.getValue();
            if (startIndex < 0) {
                startIndex = series.getIndexForKey(xValue);
            }
            // points.add((float) (left + xPixelsPerUnit[scale]
            // * (value.getKey().floatValue() - minX[scale])));
            // points.add((float) (bottom - yPixelsPerUnit[scale]
            // * (value.getValue().floatValue() - minY[scale])));
            values.add(value.getKey());
            values.add(value.getValue());
            if (!isNullValue(yValue)) {
                points.add((float) (left + xPixelsPerUnit[scale] * (xValue - minX[scale])));
                points.add((float) (bottom - yPixelsPerUnit[scale] * (yValue - minY[scale])));
            } else if (isRenderNullValues()) {
                points.add((float) (left + xPixelsPerUnit[scale] * (xValue - minX[scale])));
                points.add((float) (bottom - yPixelsPerUnit[scale] * (-minY[scale])));
            } else {
                if (points.size() > 0) {
                    drawSeries(series, canvas, paint, points, seriesRenderer, yAxisValue, i, or, startIndex, intRange);
                    ClickableArea[] clickableAreasForSubSeries = clickableAreasForPoints(MathHelper.getFloats(points), MathHelper.getDoubles(values), yAxisValue, i, startIndex, intRange);
                    clickableArea.addAll(Arrays.asList(clickableAreasForSubSeries));
                    points.clear();
                    values.clear();
                }
                clickableArea.add(null);
            }
        }
        if (points.size() > 0) {
            drawSeries(series, canvas, paint, points, seriesRenderer, yAxisValue, i, or, startIndex, intRange);
            ClickableArea[] clickableAreasForSubSeries = clickableAreasForPoints(MathHelper.getFloats(points), MathHelper.getDoubles(values), yAxisValue, i, startIndex, intRange);
            clickableArea.addAll(Arrays.asList(clickableAreasForSubSeries));
        }
    }
    // draw stuff over the margins such as data doesn't render on these areas
    drawBackground(mRenderer, canvas, x, bottom, width, height - bottom, paint, true, mRenderer.getMarginsColor());
    drawBackground(mRenderer, canvas, x, y, width, margins[0], paint, true, mRenderer.getMarginsColor());
    if (or == Orientation.HORIZONTAL) {
        drawBackground(mRenderer, canvas, x, y, left - x, height - y, paint, true, mRenderer.getMarginsColor());
        drawBackground(mRenderer, canvas, right, y, margins[3], height - y, paint, true, mRenderer.getMarginsColor());
    } else if (or == Orientation.VERTICAL) {
        drawBackground(mRenderer, canvas, right, y, width - right, height - y, paint, true, mRenderer.getMarginsColor());
        drawBackground(mRenderer, canvas, x, y, left - x, height - y, paint, true, mRenderer.getMarginsColor());
    }
    boolean showLabels = mRenderer.isShowLabels() && hasValues;
    boolean showGridX = mRenderer.isShowGridX();
    boolean showCustomTextGrid = mRenderer.isShowCustomTextGrid();
    if (showLabels || showGridX) {
        List<Double> xLabels = getValidLabels(getXLabels(minX[0], maxX[0], mRenderer.getXLabels()));
        Map<Integer, List<Double>> 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;
            }
        }
        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);
        if (showLabels) {
            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) {
                                canvas.drawLine(left + getLabelLinePos(axisAlign), yLabel, left, yLabel, paint);
                                drawText(canvas, label, left, yLabel - 2, paint, mRenderer.getYLabelsAngle());
                            } else {
                                canvas.drawLine(right, yLabel, right + getLabelLinePos(axisAlign), yLabel, paint);
                                drawText(canvas, label, right, yLabel - 2, paint, mRenderer.getYLabelsAngle());
                            }
                            if (showCustomTextGrid) {
                                paint.setColor(mRenderer.getGridColor());
                                canvas.drawLine(left, yLabel, right, yLabel, paint);
                            }
                        } else {
                            canvas.drawLine(right - getLabelLinePos(axisAlign), yLabel, right, yLabel, paint);
                            drawText(canvas, label, right + 10, yLabel - 2, paint, mRenderer.getYLabelsAngle());
                            if (showCustomTextGrid) {
                                paint.setColor(mRenderer.getGridColor());
                                canvas.drawLine(right, yLabel, left, yLabel, paint);
                            }
                        }
                    }
                }
            }
        }
        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 + 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);
                    } 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, 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, width, height, legendSize, paint, false);
    } else if (or == Orientation.VERTICAL) {
        transform(canvas, angle, true);
        drawLegend(canvas, mRenderer, titles, left, right, y, width, height, legendSize, paint, false);
        transform(canvas, angle, false);
    }
    if (mRenderer.isShowAxes()) {
        paint.setColor(mRenderer.getAxesColor());
        canvas.drawLine(left, bottom, right, bottom, paint);
        boolean rightAxis = false;
        for (int i = 0; i < maxScaleNumber && !rightAxis; i++) {
            rightAxis = mRenderer.getYAxisAlign(i) == Align.RIGHT;
        }
        if (or == Orientation.HORIZONTAL) {
            canvas.drawLine(left, top, left, bottom, paint);
            if (rightAxis) {
                canvas.drawLine(right, top, right, bottom, paint);
            }
        } else if (or == Orientation.VERTICAL) {
            canvas.drawLine(right, top, right, bottom, paint);
        }
    }
    if (rotate) {
        transform(canvas, angle, true);
    }
}
Also used : XYSeries(org.achartengine.model.XYSeries) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) Rect(android.graphics.Rect) Align(android.graphics.Paint.Align) Point(org.achartengine.model.Point) Orientation(org.achartengine.renderer.XYMultipleSeriesRenderer.Orientation) Point(org.achartengine.model.Point) Paint(android.graphics.Paint) LinkedList(java.util.LinkedList) SimpleSeriesRenderer(org.achartengine.renderer.SimpleSeriesRenderer)

Example 8 with Point

use of org.achartengine.model.Point in project Anki-Android by Ramblurr.

the class CubicLineChart method drawPath.

@Override
protected void drawPath(Canvas canvas, float[] points, Paint paint, boolean circular) {
    Path p = new Path();
    float x = points[0];
    float y = points[1];
    p.moveTo(x, y);
    int length = points.length;
    if (circular) {
        length -= 4;
    }
    for (int i = 0; i < length; i += 2) {
        int nextIndex = i + 2 < length ? i + 2 : i;
        int nextNextIndex = i + 4 < length ? i + 4 : nextIndex;
        calc(points, p1, i, nextIndex, secondMultiplier);
        p2.setX(points[nextIndex]);
        p2.setY(points[nextIndex + 1]);
        calc(points, p3, nextIndex, nextNextIndex, firstMultiplier);
        // From last point, approaching x1/y1 and x2/y2 and ends up at x3/y3
        p.cubicTo(p1.getX(), p1.getY(), p2.getX(), p2.getY(), p3.getX(), p3.getY());
    }
    if (circular) {
        for (int i = length; i < length + 4; i += 2) {
            p.lineTo(points[i], points[i + 1]);
        }
        p.lineTo(points[0], points[1]);
    }
    canvas.drawPath(p, paint);
}
Also used : Path(android.graphics.Path) Point(org.achartengine.model.Point) Paint(android.graphics.Paint)

Aggregations

Paint (android.graphics.Paint)8 Point (org.achartengine.model.Point)8 Path (android.graphics.Path)5 Align (android.graphics.Paint.Align)2 Rect (android.graphics.Rect)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 XYSeries (org.achartengine.model.XYSeries)2 Orientation (org.achartengine.renderer.XYMultipleSeriesRenderer.Orientation)2 SimpleSeriesRenderer (org.achartengine.renderer.SimpleSeriesRenderer)1 XYSeriesRenderer (org.achartengine.renderer.XYSeriesRenderer)1