Search in sources :

Example 1 with GradientDrawable

use of com.codename1.charts.compat.GradientDrawable in project CodenameOne by codenameone.

the class PieChart method draw.

/**
 * The graphical representation of the pie 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
 */
@Override
public void draw(Canvas canvas, int x, int y, int width, int height, Paint paint) {
    paint.setAntiAlias(mRenderer.isAntialiasing());
    paint.setStyle(Style.FILL);
    paint.setTextSize(mRenderer.getLabelsTextSize());
    int legendSize = getLegendSize(mRenderer, height / 5, 0);
    int left = x;
    int top = y;
    int right = x + width;
    int sLength = mDataset.getItemCount();
    double total = 0;
    String[] titles = new String[sLength];
    for (int i = 0; i < sLength; i++) {
        total += mDataset.getValue(i);
        titles[i] = mDataset.getCategory(i);
    }
    if (mRenderer.isFitLegend()) {
        legendSize = drawLegend(canvas, mRenderer, titles, left, right, y, width, height, legendSize, paint, true);
    }
    int bottom = y + height - legendSize;
    drawBackground(mRenderer, canvas, x, y, width, height, paint, false, DefaultRenderer.NO_COLOR);
    float currentAngle = mRenderer.getStartAngle();
    int mRadius = Math.min(Math.abs(right - left), Math.abs(bottom - top));
    int radius = (int) (mRadius * 0.35 * mRenderer.getScale());
    if (autoCalculateCenter || mCenterX == NO_VALUE) {
        mCenterX = (left + right) / 2;
    }
    if (autoCalculateCenter || mCenterY == NO_VALUE) {
        mCenterY = (bottom + top) / 2;
    }
    // Hook in clip detection after center has been calculated
    mPieMapper.setDimensions(radius, mCenterX, mCenterY);
    boolean loadPieCfg = !mPieMapper.areAllSegmentPresent(sLength);
    if (loadPieCfg) {
        mPieMapper.clearPieSegments();
    }
    float shortRadius = radius * 0.9f;
    float longRadius = radius * 1.1f;
    Rectangle2D oval = PkgUtils.makeRect(mCenterX - radius, mCenterY - radius, mCenterX + radius, mCenterY + radius);
    List<Rectangle2D> prevLabelsBounds = new ArrayList<Rectangle2D>();
    for (int i = 0; i < sLength; i++) {
        SimpleSeriesRenderer seriesRenderer = mRenderer.getSeriesRendererAt(i);
        boolean gradient = false;
        GradientDrawable gradientDrawable = null;
        if (seriesRenderer.isGradientEnabled()) {
            gradient = true;
            gradientDrawable = new GradientDrawable(Orientation.TOP_BOTTOM, new int[] { seriesRenderer.getGradientStartColor(), seriesRenderer.getGradientStopColor() });
            paint.setColor(seriesRenderer.getGradientStartColor());
        } else {
            paint.setColor(seriesRenderer.getColor());
        }
        float value = (float) mDataset.getValue(i);
        float angle = (float) (value / total * 360);
        if (seriesRenderer.isHighlighted()) {
            double rAngle = Math.toRadians(90 - (currentAngle + angle / 2));
            float translateX = (float) (radius * 0.1 * Math.sin(rAngle));
            float translateY = (float) (radius * 0.1 * Math.cos(rAngle));
            oval.translate(translateX, translateY);
            if (gradient) {
                canvas.drawArcWithGradient(oval, currentAngle, angle, true, paint, gradientDrawable);
            } else {
                canvas.drawArc(oval, currentAngle, angle, true, paint);
            }
            oval.translate(-translateX, -translateY);
        } else {
            if (gradient) {
                canvas.drawArcWithGradient(oval, currentAngle, angle, true, paint, gradientDrawable);
            } else {
                canvas.drawArc(oval, currentAngle, angle, true, paint);
            }
        }
        paint.setColor(seriesRenderer.getColor());
        // paint.setShader(null);
        drawLabel(canvas, mDataset.getCategory(i), mRenderer, prevLabelsBounds, mCenterX, mCenterY, shortRadius, longRadius, currentAngle, angle, left, right, mRenderer.getLabelsColor(), paint, true, false);
        if (mRenderer.isDisplayValues()) {
            drawLabel(canvas, getLabel(mRenderer.getSeriesRendererAt(i).getChartValuesFormat(), mDataset.getValue(i)), mRenderer, prevLabelsBounds, mCenterX, mCenterY, shortRadius / 2, longRadius / 2, currentAngle, angle, left, right, mRenderer.getLabelsColor(), paint, false, true);
        }
        // Save details for getSeries functionality
        if (loadPieCfg) {
            mPieMapper.addPieSegment(i, value, currentAngle, angle);
        }
        currentAngle += angle;
    }
    prevLabelsBounds.clear();
    drawLegend(canvas, mRenderer, titles, left, right, y, width, height, legendSize, paint, false);
    drawTitle(canvas, x, y, width, paint);
}
Also used : SimpleSeriesRenderer(com.codename1.charts.renderers.SimpleSeriesRenderer) Rectangle2D(com.codename1.ui.geom.Rectangle2D) ArrayList(java.util.ArrayList) Point(com.codename1.charts.models.Point) Paint(com.codename1.charts.compat.Paint) GradientDrawable(com.codename1.charts.compat.GradientDrawable)

Example 2 with GradientDrawable

use of com.codename1.charts.compat.GradientDrawable in project CodenameOne by codenameone.

the class Canvas method drawGradient.

void drawGradient(GradientDrawable gradient) {
    Orientation o = gradient.orientation;
    Rectangle r = gradient.bounds;
    int[] colors = gradient.colors;
    int clen = colors.length;
    if (Orientation.TOP_BOTTOM.equals(o) || Orientation.BOTTOM_TOP.equals(o)) {
        if (Orientation.BOTTOM_TOP.equals(o)) {
            colors = new int[clen];
            for (int i = 0; i < clen; i++) {
                colors[i] = gradient.colors[clen - i - 1];
            }
        }
        g.fillLinearGradient(colors[0], colors[clen - 1], r.getX(), r.getY(), r.getWidth(), r.getHeight(), false);
    } else if (Orientation.LEFT_RIGHT.equals(o)) {
        g.fillLinearGradient(gradient.colors[0], gradient.colors[clen - 1], r.getX(), r.getY(), r.getWidth(), r.getHeight(), true);
    } else {
        Log.p("Gradient with type " + o + " not implemented yet.  Just filling solid rect");
        g.setColor(gradient.colors[0]);
        g.fillRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
    }
}
Also used : Rectangle(com.codename1.ui.geom.Rectangle) Orientation(com.codename1.charts.compat.GradientDrawable.Orientation)

Example 3 with GradientDrawable

use of com.codename1.charts.compat.GradientDrawable in project CodenameOne by codenameone.

the class RoundChart method drawLegendShape.

/**
 * The graphical representation of the legend shape.
 *
 * @param canvas the canvas to paint to
 * @param renderer the series renderer
 * @param x the x value of the point the shape should be drawn at
 * @param y the y value of the point the shape should be drawn at
 * @param seriesIndex the series index
 * @param paint the paint to be used for drawing
 */
public void drawLegendShape(Canvas canvas, SimpleSeriesRenderer renderer, float x, float y, int seriesIndex, Paint paint) {
    if (renderer.isGradientEnabled() && canvas.isShapeClipSupported()) {
        GradientDrawable gr = new GradientDrawable(Orientation.TOP_BOTTOM, new int[] { renderer.getGradientStartColor(), renderer.getGradientStopColor() });
        gr.setBounds((int) x, (int) (y - SHAPE_WIDTH / 2), (int) (x + SHAPE_WIDTH), (int) (y + SHAPE_WIDTH / 2));
        gr.draw(canvas);
    } else {
        canvas.drawRect(x, y - SHAPE_WIDTH / 2, x + SHAPE_WIDTH, y + SHAPE_WIDTH / 2, paint);
    }
}
Also used : GradientDrawable(com.codename1.charts.compat.GradientDrawable)

Aggregations

GradientDrawable (com.codename1.charts.compat.GradientDrawable)2 Orientation (com.codename1.charts.compat.GradientDrawable.Orientation)1 Paint (com.codename1.charts.compat.Paint)1 Point (com.codename1.charts.models.Point)1 SimpleSeriesRenderer (com.codename1.charts.renderers.SimpleSeriesRenderer)1 Rectangle (com.codename1.ui.geom.Rectangle)1 Rectangle2D (com.codename1.ui.geom.Rectangle2D)1 ArrayList (java.util.ArrayList)1