Search in sources :

Example 21 with Paint

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

the class DialChart method drawTicks.

/**
 * Draws the chart tick lines.
 *
 * @param canvas the canvas
 * @param min the minimum chart value
 * @param max the maximum chart value
 * @param minAngle the minimum chart angle value
 * @param maxAngle the maximum chart angle value
 * @param centerX the center x value
 * @param centerY the center y value
 * @param longRadius the long radius
 * @param shortRadius the short radius
 * @param ticks the tick spacing
 * @param paint the paint settings
 * @param labels paint the labels
 * @return the angle
 */
private void drawTicks(Canvas canvas, double min, double max, double minAngle, double maxAngle, int centerX, int centerY, double longRadius, double shortRadius, double ticks, Paint paint, boolean labels) {
    for (double i = min; i <= max; i += ticks) {
        double angle = getAngleForValue(i, minAngle, maxAngle, min, max);
        double sinValue = Math.sin(angle);
        double cosValue = Math.cos(angle);
        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));
        canvas.drawLine(x1, y1, x2, y2, paint);
        if (labels) {
            paint.setTextAlign(Component.LEFT);
            if (x1 <= x2) {
                paint.setTextAlign(Component.RIGHT);
            }
            String text = i + "";
            if (Math.round(i) == (long) i) {
                text = (long) i + "";
            }
            canvas.drawText(text, x1, y1, paint);
        }
    }
}
Also used : Paint(com.codename1.charts.compat.Paint)

Example 22 with Paint

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

the class DialChart method draw.

/**
 * The graphical representation of the dial 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();
    String[] titles = new String[sLength];
    for (int i = 0; i < sLength; 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);
    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;
    }
    float shortRadius = radius * 0.9f;
    float longRadius = radius * 1.1f;
    double min = mRenderer.getMinValue();
    double max = mRenderer.getMaxValue();
    double angleMin = mRenderer.getAngleMin();
    double angleMax = mRenderer.getAngleMax();
    if (!mRenderer.isMinValueSet() || !mRenderer.isMaxValueSet()) {
        int count = mRenderer.getSeriesRendererCount();
        for (int i = 0; i < count; i++) {
            double value = mDataset.getValue(i);
            if (!mRenderer.isMinValueSet()) {
                min = min == MathHelper.NULL_VALUE ? value : Math.min(min, value);
            }
            if (!mRenderer.isMaxValueSet()) {
                max = max == MathHelper.NULL_VALUE ? value : Math.max(max, value);
            }
        }
    }
    if (min == max) {
        min = min * 0.5;
        max = max * 1.5;
    }
    paint.setColor(mRenderer.getLabelsColor());
    double minorTicks = mRenderer.getMinorTicksSpacing();
    double majorTicks = mRenderer.getMajorTicksSpacing();
    if (minorTicks == MathHelper.NULL_VALUE) {
        minorTicks = (max - min) / 30;
    }
    if (majorTicks == MathHelper.NULL_VALUE) {
        majorTicks = (max - min) / 10;
    }
    drawTicks(canvas, min, max, angleMin, angleMax, mCenterX, mCenterY, longRadius, radius, minorTicks, paint, false);
    drawTicks(canvas, min, max, angleMin, angleMax, mCenterX, mCenterY, longRadius, shortRadius, majorTicks, paint, true);
    int count = mRenderer.getSeriesRendererCount();
    for (int i = 0; i < count; i++) {
        double angle = getAngleForValue(mDataset.getValue(i), angleMin, angleMax, min, max);
        paint.setColor(mRenderer.getSeriesRendererAt(i).getColor());
        boolean type = mRenderer.getVisualTypeForIndex(i) == Type.ARROW;
        drawNeedle(canvas, angle, mCenterX, mCenterY, shortRadius, type, paint);
    }
    drawLegend(canvas, mRenderer, titles, left, right, y, width, height, legendSize, paint, false);
    drawTitle(canvas, x, y, width, paint);
}
Also used : Paint(com.codename1.charts.compat.Paint)

Example 23 with Paint

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

the class LineChart 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) {
    float lineWidth = paint.getStrokeWidth();
    paint.setStrokeWidth(renderer.getLineWidth());
    final FillOutsideLine[] fillOutsideLine = renderer.getFillOutsideLine();
    for (FillOutsideLine fill : fillOutsideLine) {
        if (fill.getType() != FillOutsideLine.Type.NONE) {
            paint.setColor(fill.getColor());
            // TODO: find a way to do area charts without duplicating data
            List<Float> fillPoints = new ArrayList<Float>();
            int[] range = fill.getFillRange();
            if (range == null) {
                fillPoints.addAll(points);
            } else {
                if (points.size() > range[0] * 2 && points.size() > range[1] * 2) {
                    fillPoints.addAll(points.subList(range[0] * 2, range[1] * 2));
                }
            }
            final float referencePoint;
            // switch on ENUM's generates reflection code that screws up J2ME
            FillOutsideLine.Type tt = fill.getType();
            if (tt == FillOutsideLine.Type.BOUNDS_ALL || tt == FillOutsideLine.Type.BOUNDS_BELOW || tt == FillOutsideLine.Type.BOUNDS_ABOVE) {
                referencePoint = yAxisValue;
            } else {
                if (tt == FillOutsideLine.Type.BELOW) {
                    referencePoint = canvas.getHeight();
                } else {
                    if (tt == FillOutsideLine.Type.ABOVE) {
                        referencePoint = 0;
                    } else {
                        throw new RuntimeException("You have added a new type of filling but have not implemented.");
                    }
                }
            }
            /*switch (fill.getType()) {
        case BOUNDS_ALL:
          referencePoint = yAxisValue;
          break;
        case BOUNDS_BELOW:
          referencePoint = yAxisValue;
          break;
        case BOUNDS_ABOVE:
          referencePoint = yAxisValue;
          break;
        case BELOW:
          referencePoint = canvas.getHeight();
          break;
        case ABOVE:
          referencePoint = 0;
          break;
        default:
          throw new RuntimeException(
              "You have added a new type of filling but have not implemented.");
        }*/
            if (fill.getType() == FillOutsideLine.Type.BOUNDS_ABOVE || fill.getType() == FillOutsideLine.Type.BOUNDS_BELOW) {
                List<Float> boundsPoints = new ArrayList<Float>();
                boolean add = false;
                int length = fillPoints.size();
                if (length > 0 && fill.getType() == FillOutsideLine.Type.BOUNDS_ABOVE && fillPoints.get(1) < referencePoint || fill.getType() == FillOutsideLine.Type.BOUNDS_BELOW && fillPoints.get(1) > referencePoint) {
                    boundsPoints.add(fillPoints.get(0));
                    boundsPoints.add(fillPoints.get(1));
                    add = true;
                }
                for (int i = 3; i < length; i += 2) {
                    float prevValue = fillPoints.get(i - 2);
                    float value = fillPoints.get(i);
                    if (prevValue < referencePoint && value > referencePoint || prevValue > referencePoint && value < referencePoint) {
                        float prevX = fillPoints.get(i - 3);
                        float x = fillPoints.get(i - 1);
                        boundsPoints.add(prevX + (x - prevX) * (referencePoint - prevValue) / (value - prevValue));
                        boundsPoints.add(referencePoint);
                        if (fill.getType() == FillOutsideLine.Type.BOUNDS_ABOVE && value > referencePoint || fill.getType() == FillOutsideLine.Type.BOUNDS_BELOW && value < referencePoint) {
                            i += 2;
                            add = false;
                        } else {
                            boundsPoints.add(x);
                            boundsPoints.add(value);
                            add = true;
                        }
                    } else {
                        if (add || fill.getType() == FillOutsideLine.Type.BOUNDS_ABOVE && value < referencePoint || fill.getType() == FillOutsideLine.Type.BOUNDS_BELOW && value > referencePoint) {
                            boundsPoints.add(fillPoints.get(i - 1));
                            boundsPoints.add(value);
                        }
                    }
                }
                fillPoints.clear();
                fillPoints.addAll(boundsPoints);
            }
            int length = fillPoints.size();
            if (length > 0) {
                fillPoints.set(0, fillPoints.get(0) + 1);
                fillPoints.add(fillPoints.get(length - 2));
                fillPoints.add(referencePoint);
                fillPoints.add(fillPoints.get(0));
                fillPoints.add(fillPoints.get(length + 1));
                for (int i = 0; i < length + 4; i += 2) {
                    if (fillPoints.get(i + 1) < 0) {
                        fillPoints.set(i + 1, 0f);
                    }
                }
                paint.setStyle(Style.FILL);
                drawPath(canvas, fillPoints, paint, true);
            }
        }
    }
    paint.setColor(renderer.getColor());
    paint.setStyle(Style.STROKE);
    drawPath(canvas, points, paint, false);
    paint.setStrokeWidth(lineWidth);
}
Also used : FillOutsideLine(com.codename1.charts.renderers.XYSeriesRenderer.FillOutsideLine) ArrayList(java.util.ArrayList) Paint(com.codename1.charts.compat.Paint)

Example 24 with Paint

use of com.codename1.charts.compat.Paint 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 25 with Paint

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

the class OnOffSwitch method animateTo.

private void animateTo(final boolean value, final int position) {
    int switchButtonPadInt = UIManager.getInstance().getThemeConstant("switchButtonPadInt", 16);
    if (Display.getInstance().getDisplayWidth() > 480) {
        // is retina
        switchButtonPadInt *= 2;
    }
    final Motion current = Motion.createEaseInOutMotion(Math.abs(position), switchMaskImage.getWidth() - 2 * switchButtonPadInt, 100);
    current.start();
    deltaX = position;
    getComponentForm().registerAnimated(new Animation() {

        public boolean animate() {
            deltaX = current.getValue();
            if (value) {
                deltaX *= -1;
            }
            dragged = true;
            if (current.isFinished()) {
                dragged = false;
                Form f = getComponentForm();
                if (f != null) {
                    f.deregisterAnimated(this);
                }
                OnOffSwitch.this.setValue(value);
            }
            repaint();
            return false;
        }

        public void paint(Graphics g) {
        }
    });
    dragged = true;
}
Also used : Graphics(com.codename1.ui.Graphics) Motion(com.codename1.ui.animations.Motion) Form(com.codename1.ui.Form) Animation(com.codename1.ui.animations.Animation)

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