Search in sources :

Example 26 with Paint

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

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, List<Float> points, Paint paint, boolean circular) {
    GeneralPath path = new GeneralPath();
    int height = canvas.getHeight();
    int width = canvas.getWidth();
    float[] tempDrawPoints;
    if (points.size() < 4) {
        return;
    }
    tempDrawPoints = calculateDrawPoints(points.get(0), points.get(1), points.get(2), points.get(3), height, width);
    path.moveTo(tempDrawPoints[0], tempDrawPoints[1]);
    path.lineTo(tempDrawPoints[2], tempDrawPoints[3]);
    int length = points.size();
    for (int i = 4; i < length; i += 2) {
        if ((points.get(i - 1) < 0 && points.get(i + 1) < 0) || (points.get(i - 1) > height && points.get(i + 1) > height)) {
            continue;
        }
        tempDrawPoints = calculateDrawPoints(points.get(i - 2), points.get(i - 1), points.get(i), points.get(i + 1), height, width);
        if (!circular) {
            path.moveTo(tempDrawPoints[0], tempDrawPoints[1]);
        }
        path.lineTo(tempDrawPoints[2], tempDrawPoints[3]);
    }
    if (circular) {
        path.lineTo(points.get(0), points.get(1));
    }
    canvas.drawPath(path, paint);
}
Also used : GeneralPath(com.codename1.ui.geom.GeneralPath) Point(com.codename1.charts.models.Point) Paint(com.codename1.charts.compat.Paint)

Example 27 with Paint

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

the class AbstractChart method drawLabel.

/**
 * Draws a text label.
 *
 * @param canvas the canvas
 * @param labelText the label text
 * @param renderer the renderer
 * @param prevLabelsBounds the previous rendered label bounds
 * @param centerX the round chart center on X axis
 * @param centerY the round chart center on Y axis
 * @param shortRadius the short radius for the round chart
 * @param longRadius the long radius for the round chart
 * @param currentAngle the current angle
 * @param angle the label extra angle
 * @param left the left side
 * @param right the right side
 * @param color the label color
 * @param paint the paint
 * @param line if a line to the label should be drawn
 * @param display display the label anyway
 */
protected void drawLabel(Canvas canvas, String labelText, DefaultRenderer renderer, List<Rectangle2D> prevLabelsBounds, int centerX, int centerY, float shortRadius, float longRadius, float currentAngle, float angle, int left, int right, int color, Paint paint, boolean line, boolean display) {
    if (renderer.isShowLabels() || display) {
        paint.setColor(color);
        double rAngle = Math.toRadians(90 - (currentAngle + angle / 2));
        double sinValue = Math.sin(rAngle);
        double cosValue = Math.cos(rAngle);
        int x1 = Math.round(centerX + (float) (shortRadius * sinValue));
        int y1 = Math.round(centerY + (float) (shortRadius * cosValue));
        int x2 = Math.round(centerX + (float) (longRadius * sinValue));
        int y2 = Math.round(centerY + (float) (longRadius * cosValue));
        float size = renderer.getLabelsTextSize();
        float extra = Math.max(size / 2, 10);
        paint.setTextAlign(Component.LEFT);
        if (x1 > x2) {
            extra = -extra;
            paint.setTextAlign(Component.RIGHT);
        }
        float xLabel = x2 + extra;
        float yLabel = y2;
        float width = right - xLabel;
        if (x1 > x2) {
            width = xLabel - left;
        }
        labelText = getFitText(labelText, width, paint);
        float widthLabel = paint.measureText(labelText);
        boolean okBounds = false;
        while (!okBounds && line) {
            boolean intersects = false;
            int length = prevLabelsBounds.size();
            for (int j = 0; j < length && !intersects; j++) {
                Rectangle2D prevLabelBounds = prevLabelsBounds.get(j);
                if (prevLabelBounds.intersects(xLabel, yLabel, widthLabel, size)) {
                    intersects = true;
                    yLabel = (float) Math.max(yLabel, prevLabelBounds.getY() + prevLabelBounds.getHeight());
                }
            }
            okBounds = !intersects;
        }
        if (line) {
            y2 = (int) (yLabel - size / 2);
            canvas.drawLine(x1, y1, x2, y2, paint);
            canvas.drawLine(x2, y2, x2 + extra, y2, paint);
        } else {
            paint.setTextAlign(Component.CENTER);
        }
        canvas.drawText(labelText, xLabel, yLabel, paint);
        if (line) {
            prevLabelsBounds.add(PkgUtils.makeRect(xLabel, yLabel, xLabel + widthLabel, yLabel + size));
        }
    }
}
Also used : Rectangle2D(com.codename1.ui.geom.Rectangle2D) Point(com.codename1.charts.models.Point) Paint(com.codename1.charts.compat.Paint)

Example 28 with Paint

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

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) {
    GeneralPath path = new GeneralPath();
    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]);
    int length = points.length;
    for (int i = 4; i < 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 : GeneralPath(com.codename1.ui.geom.GeneralPath) Point(com.codename1.charts.models.Point) Paint(com.codename1.charts.compat.Paint)

Example 29 with Paint

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

the class BubbleChart method drawSeries.

/**
 * 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
 * @param seriesRenderer the series renderer
 * @param yAxisValue the minimum value of the y axis
 * @param seriesIndex the index of the series currently being drawn
 * @param startIndex the start index of the rendering points
 */
@Override
public void drawSeries(Canvas canvas, Paint paint, List<Float> points, XYSeriesRenderer renderer, float yAxisValue, int seriesIndex, int startIndex) {
    paint.setColor(renderer.getColor());
    paint.setStyle(Style.FILL);
    int length = points.size();
    XYValueSeries series = (XYValueSeries) mDataset.getSeriesAt(seriesIndex);
    double max = series.getMaxValue();
    double coef = MAX_BUBBLE_SIZE / max;
    for (int i = 0; i < length; i += 2) {
        double size = series.getValue(startIndex + i / 2) * coef + MIN_BUBBLE_SIZE;
        drawCircle(canvas, paint, points.get(i), points.get(i + 1), (float) size);
    }
}
Also used : Paint(com.codename1.charts.compat.Paint) XYValueSeries(com.codename1.charts.models.XYValueSeries)

Example 30 with Paint

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

the class BarChart method drawSeries.

/**
 * 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
 * @param seriesRenderer the series renderer
 * @param yAxisValue the minimum value of the y axis
 * @param seriesIndex the index of the series currently being drawn
 * @param startIndex the start index of the rendering points
 */
@Override
public void drawSeries(Canvas canvas, Paint paint, List<Float> points, XYSeriesRenderer seriesRenderer, float yAxisValue, int seriesIndex, int startIndex) {
    int seriesNr = mDataset.getSeriesCount();
    int length = points.size();
    paint.setColor(seriesRenderer.getColor());
    paint.setStyle(Style.FILL);
    float halfDiffX = getHalfDiffX(points, length, seriesNr);
    Point[] yvals = new Point[length / 2];
    for (int i = 0; i < length; i += 2) {
        Point p = new Point();
        p.seriesIndex = i / 2;
        p.yval = points.get(i + 1);
        yvals[i / 2] = p;
    }
    for (int i = 0; i < length; i += 2) {
        float x = points.get(i);
        float y = points.get(i + 1);
        if (mType == Type.HEAPED && seriesIndex > 0) {
            float lastY = mPreviousSeriesPoints.get(i + 1);
            y = y + (lastY - yAxisValue);
            points.set(i + 1, y);
            drawBar(canvas, x, lastY, x, y, halfDiffX, seriesNr, seriesIndex, paint);
        } else {
            drawBar(canvas, x, yAxisValue, x, y, halfDiffX, seriesNr, seriesIndex, paint);
        }
    }
    paint.setColor(seriesRenderer.getColor());
    mPreviousSeriesPoints = points;
}
Also used : 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