Search in sources :

Example 1 with SimpleSeriesRenderer

use of com.codename1.charts.renderers.SimpleSeriesRenderer 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 SimpleSeriesRenderer

use of com.codename1.charts.renderers.SimpleSeriesRenderer 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)

Example 3 with SimpleSeriesRenderer

use of com.codename1.charts.renderers.SimpleSeriesRenderer in project CodenameOne by codenameone.

the class AbstractChart method drawLegend.

/**
 * Draws the chart legend.
 *
 * @param canvas the canvas to paint to
 * @param renderer the series renderer
 * @param titles the titles to go to the legend
 * @param left the left X value of the area to draw to
 * @param right the right X value of the area to draw to
 * @param y the y value of the area to draw to
 * @param width the width of the area to draw to
 * @param height the height of the area to draw to
 * @param legendSize the legend size
 * @param paint the paint to be used for drawing
 * @param calculate if only calculating the legend size
 *
 * @return the legend height
 */
protected int drawLegend(Canvas canvas, DefaultRenderer renderer, String[] titles, int left, int right, int y, int width, int height, int legendSize, Paint paint, boolean calculate) {
    float size = 32;
    if (renderer.isShowLegend()) {
        float currentX = left;
        float currentY = y + height - legendSize + size;
        paint.setTextAlign(Component.LEFT);
        paint.setTextSize(renderer.getLegendTextSize());
        int sLength = Math.min(titles.length, renderer.getSeriesRendererCount());
        for (int i = 0; i < sLength; i++) {
            SimpleSeriesRenderer r = renderer.getSeriesRendererAt(i);
            final float lineSize = getLegendShapeWidth(i);
            if (r.isShowLegendItem()) {
                String text = titles[i];
                if (titles.length == renderer.getSeriesRendererCount()) {
                    paint.setColor(r.getColor());
                } else {
                    paint.setColor(ColorUtil.LTGRAY);
                }
                float[] widths = new float[text.length()];
                paint.getTextWidths(text, widths);
                float sum = 0;
                for (float value : widths) {
                    sum += value;
                }
                float extraSize = lineSize + 10 + sum;
                float currentWidth = currentX + extraSize;
                if (i > 0 && getExceed(currentWidth, renderer, right, width)) {
                    currentX = left;
                    currentY += renderer.getLegendTextSize();
                    size += renderer.getLegendTextSize();
                    currentWidth = currentX + extraSize;
                }
                if (getExceed(currentWidth, renderer, right, width)) {
                    float maxWidth = right - currentX - lineSize - 10;
                    if (isVertical(renderer)) {
                        maxWidth = width - currentX - lineSize - 10;
                    }
                    int nr = paint.breakText(text, true, maxWidth, widths);
                    text = text.substring(0, nr) + "...";
                }
                if (!calculate) {
                    drawLegendShape(canvas, r, currentX, currentY, i, paint);
                    drawString(canvas, text, currentX + lineSize + 5, currentY + 5, paint);
                }
                currentX += extraSize;
            }
        }
    }
    return Math.round(size + renderer.getLegendTextSize());
}
Also used : SimpleSeriesRenderer(com.codename1.charts.renderers.SimpleSeriesRenderer) Point(com.codename1.charts.models.Point) Paint(com.codename1.charts.compat.Paint)

Aggregations

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