Search in sources :

Example 71 with Paint

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

the class BarChart method drawChartValuesText.

/**
 * 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
 * @param paint the paint to be used for drawing
 * @param points the array of points to be used for drawing the series
 * @param seriesIndex the index of the series currently being drawn
 * @param startIndex the start index of the rendering points
 */
protected void drawChartValuesText(Canvas canvas, XYSeries series, XYSeriesRenderer renderer, Paint paint, List<Float> points, int seriesIndex, int startIndex) {
    int seriesNr = mDataset.getSeriesCount();
    int length = points.size();
    float halfDiffX = getHalfDiffX(points, length, seriesNr);
    for (int i = 0; i < length; i += 2) {
        int index = startIndex + i / 2;
        double value = series.getY(index);
        if (!isNullValue(value)) {
            float x = points.get(i);
            if (mType == Type.DEFAULT) {
                x += seriesIndex * 2 * halfDiffX - (seriesNr - 1.5f) * halfDiffX;
            }
            if (value >= 0) {
                drawText(canvas, getLabel(renderer.getChartValuesFormat(), value), x, points.get(i + 1) - renderer.getChartValuesSpacing(), paint, 0);
            } else {
                drawText(canvas, getLabel(renderer.getChartValuesFormat(), value), x, points.get(i + 1) + renderer.getChartValuesTextSize() + renderer.getChartValuesSpacing() - 3, paint, 0);
            }
        }
    }
}
Also used : Paint(com.codename1.charts.compat.Paint)

Example 72 with Paint

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

the class DialChart method drawNeedle.

/**
 * Returns the angle for a specific chart value.
 *
 * @param canvas the canvas
 * @param angle the needle angle value
 * @param centerX the center x value
 * @param centerY the center y value
 * @param radius the radius
 * @param arrow if a needle or an arrow to be painted
 * @param paint the paint settings
 * @return the angle
 */
private void drawNeedle(Canvas canvas, double angle, int centerX, int centerY, double radius, boolean arrow, Paint paint) {
    double diff = Math.toRadians(90);
    int needleSinValue = (int) (NEEDLE_RADIUS * Math.sin(angle - diff));
    int needleCosValue = (int) (NEEDLE_RADIUS * Math.cos(angle - diff));
    int needleX = (int) (radius * Math.sin(angle));
    int needleY = (int) (radius * Math.cos(angle));
    int needleCenterX = centerX + needleX;
    int needleCenterY = centerY + needleY;
    float[] points;
    if (arrow) {
        int arrowBaseX = centerX + (int) (radius * 0.85 * Math.sin(angle));
        int arrowBaseY = centerY + (int) (radius * 0.85 * Math.cos(angle));
        points = new float[] { arrowBaseX - needleSinValue, arrowBaseY - needleCosValue, needleCenterX, needleCenterY, arrowBaseX + needleSinValue, arrowBaseY + needleCosValue };
        float width = paint.getStrokeWidth();
        paint.setStrokeWidth(5);
        canvas.drawLine(centerX, centerY, needleCenterX, needleCenterY, paint);
        paint.setStrokeWidth(width);
    } else {
        points = new float[] { centerX - needleSinValue, centerY - needleCosValue, needleCenterX, needleCenterY, centerX + needleSinValue, centerY + needleCosValue };
    }
    drawPath(canvas, points, paint, true);
}
Also used : Paint(com.codename1.charts.compat.Paint)

Example 73 with Paint

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

the class DoughnutChart method draw.

/**
 * The graphical representation of the doughnut 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 cLength = mDataset.getCategoriesCount();
    String[] categories = new String[cLength];
    for (int category = 0; category < cLength; category++) {
        categories[category] = mDataset.getCategory(category);
    }
    if (mRenderer.isFitLegend()) {
        legendSize = drawLegend(canvas, mRenderer, categories, 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);
    mStep = SHAPE_WIDTH * 3 / 4;
    int mRadius = Math.min(Math.abs(right - left), Math.abs(bottom - top));
    double rCoef = 0.35 * mRenderer.getScale();
    double decCoef = 0.2 / cLength;
    int radius = (int) (mRadius * rCoef);
    if (autoCalculateCenter || mCenterX == NO_VALUE) {
        mCenterX = (left + right) / 2;
    }
    if (autoCalculateCenter || mCenterY == NO_VALUE) {
        mCenterY = (bottom + top) / 2;
    }
    float shortRadius = radius * 0.9f;
    float longRadius = radius * 1.1f;
    List<Rectangle2D> prevLabelsBounds = new ArrayList<Rectangle2D>();
    for (int category = 0; category < cLength; category++) {
        int sLength = mDataset.getItemCount(category);
        double total = 0;
        String[] titles = new String[sLength];
        for (int i = 0; i < sLength; i++) {
            total += mDataset.getValues(category)[i];
            titles[i] = mDataset.getTitles(category)[i];
        }
        float currentAngle = mRenderer.getStartAngle();
        Rectangle2D oval = PkgUtils.makeRect(mCenterX - radius, mCenterY - radius, mCenterX + radius, mCenterY + radius);
        for (int i = 0; i < sLength; i++) {
            paint.setColor(mRenderer.getSeriesRendererAt(i).getColor());
            float value = (float) mDataset.getValues(category)[i];
            float angle = (float) (value / total * 360);
            canvas.drawArc(oval, currentAngle, angle, true, paint);
            drawLabel(canvas, mDataset.getTitles(category)[i], mRenderer, prevLabelsBounds, mCenterX, mCenterY, shortRadius, longRadius, currentAngle, angle, left, right, mRenderer.getLabelsColor(), paint, true, false);
            currentAngle += angle;
        }
        radius -= (int) mRadius * decCoef;
        shortRadius -= mRadius * decCoef - 2;
        if (mRenderer.getBackgroundColor() != 0) {
            paint.setColor(mRenderer.getBackgroundColor());
        } else {
            paint.setColor(ColorUtil.WHITE);
        }
        paint.setStyle(Style.FILL);
        oval = PkgUtils.makeRect(mCenterX - radius, mCenterY - radius, mCenterX + radius, mCenterY + radius);
        canvas.drawArc(oval, 0, 360, true, paint);
        radius -= 1;
    }
    prevLabelsBounds.clear();
    drawLegend(canvas, mRenderer, categories, left, right, y, width, height, legendSize, paint, false);
    drawTitle(canvas, x, y, width, paint);
}
Also used : Rectangle2D(com.codename1.ui.geom.Rectangle2D) ArrayList(java.util.ArrayList) Paint(com.codename1.charts.compat.Paint)

Example 74 with Paint

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

the class AbstractChart method getFitText.

/**
 * Calculates the best text to fit into the available space.
 *
 * @param text the entire text
 * @param width the width to fit the text into
 * @param paint the paint
 * @return the text to fit into the space
 */
private String getFitText(String text, float width, Paint paint) {
    if (UIManager.getInstance().getLookAndFeel().isDefaultEndsWith3Points()) {
        String newText = text;
        int length = text.length();
        int diff = 0;
        while (paint.measureText(newText) > width && diff < length) {
            diff++;
            newText = text.substring(0, length - diff) + "...";
        }
        if (diff == length) {
            newText = "...";
        }
        return newText;
    }
    return text;
}
Also used : Point(com.codename1.charts.models.Point) Paint(com.codename1.charts.compat.Paint)

Example 75 with Paint

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

the class AbstractChart method drawString.

/**
 * Draw a multiple lines string.
 *
 * @param canvas the canvas to paint to
 * @param text the text to be painted
 * @param x the x value of the area to draw to
 * @param y the y value of the area to draw to
 * @param paint the paint to be used for drawing
 */
protected void drawString(Canvas canvas, String text, float x, float y, Paint paint) {
    if (text != null) {
        String[] lines = split(text, "\n");
        Rectangle2D rect = new Rectangle2D();
        int yOff = 0;
        int llen = lines.length;
        for (int i = 0; i < llen; ++i) {
            canvas.drawText(lines[i], x, y + yOff, paint);
            paint.getTextBounds(lines[i], 0, lines[i].length(), rect);
            // space between lines is 5
            yOff = yOff + (int) rect.getHeight() + 5;
        }
    }
}
Also used : Rectangle2D(com.codename1.ui.geom.Rectangle2D) Point(com.codename1.charts.models.Point) Paint(com.codename1.charts.compat.Paint)

Aggregations

Paint (com.codename1.charts.compat.Paint)28 Component (com.codename1.ui.Component)16 Point (com.codename1.charts.models.Point)14 Style (com.codename1.ui.plaf.Style)13 Form (com.codename1.ui.Form)12 Image (com.codename1.ui.Image)11 Graphics (com.codename1.ui.Graphics)10 Animation (com.codename1.ui.animations.Animation)10 Rectangle (com.codename1.ui.geom.Rectangle)9 Dialog (com.codename1.ui.Dialog)7 GeneralPath (com.codename1.ui.geom.GeneralPath)6 Point (com.codename1.ui.geom.Point)6 Paint (android.graphics.Paint)5 ActionEvent (com.codename1.ui.events.ActionEvent)5 Rectangle2D (com.codename1.ui.geom.Rectangle2D)5 IOException (java.io.IOException)5 TextArea (com.codename1.ui.TextArea)4 ActionListener (com.codename1.ui.events.ActionListener)4 BorderLayout (com.codename1.ui.layouts.BorderLayout)4 Motion (com.codename1.ui.animations.Motion)3