Search in sources :

Example 31 with List

use of com.codename1.ui.List in project CodenameOne by codenameone.

the class CubicLineChart method drawPoints.

/**
 * Draws the series points.
 *
 * @param canvas the canvas
 * @param paint the paint object
 * @param pointsList the points to be rendered
 * @param seriesRenderer the series renderer
 * @param yAxisValue the y axis value in pixels
 * @param seriesIndex the series index
 * @param startIndex the start index of the rendering points
 */
protected void drawPoints(Canvas canvas, Paint paint, List<Float> pointsList, XYSeriesRenderer seriesRenderer, float yAxisValue, int seriesIndex, int startIndex) {
    if (isRenderPoints(seriesRenderer)) {
        ScatterChart pointsChart = getPointsChart();
        if (pointsChart != null) {
            int length = (int) mPathMeasure.getLength();
            int pointsLength = pointsList.size();
            float[] coords = new float[2];
            for (int i = 0; i < length; i++) {
                mPathMeasure.getPosTan(i, coords, null);
                double prevDiff = Double.MAX_VALUE;
                boolean ok = true;
                for (int j = 0; j < pointsLength && ok; j += 2) {
                    double diff = Math.abs(pointsList.get(j) - coords[0]);
                    if (diff < 1) {
                        pointsList.set(j + 1, coords[1]);
                        prevDiff = diff;
                    }
                    ok = prevDiff > diff;
                }
            }
            pointsChart.drawSeries(canvas, paint, pointsList, seriesRenderer, yAxisValue, seriesIndex, startIndex);
        }
    }
}
Also used : Point(com.codename1.charts.models.Point) Paint(com.codename1.charts.compat.Paint)

Example 32 with List

use of com.codename1.ui.List in project CodenameOne by codenameone.

the class CubicLineChart method drawPath.

@Override
protected void drawPath(Canvas canvas, List<Float> points, Paint paint, boolean circular) {
    GeneralPath p = new GeneralPath();
    float x = points.get(0);
    float y = points.get(1);
    p.moveTo(x, y);
    int length = points.size();
    if (circular) {
        length -= 4;
    }
    Point p1 = new Point();
    Point p2 = new Point();
    Point p3 = new Point();
    for (int i = 0; i < length; i += 2) {
        int nextIndex = i + 2 < length ? i + 2 : i;
        int nextNextIndex = i + 4 < length ? i + 4 : nextIndex;
        calc(points, p1, i, nextIndex, mSecondMultiplier);
        p2.setX(points.get(nextIndex));
        p2.setY(points.get(nextIndex + 1));
        calc(points, p3, nextIndex, nextNextIndex, mFirstMultiplier);
        // From last point, approaching x1/y1 and x2/y2 and ends up at x3/y3
        p.curveTo(p1.getX(), p1.getY(), p2.getX(), p2.getY(), p3.getX(), p3.getY());
    }
    mPathMeasure = new PathMeasure(p, false);
    if (circular) {
        for (int i = length; i < length + 4; i += 2) {
            p.lineTo(points.get(i), points.get(i + 1));
        }
        p.lineTo(points.get(0), points.get(1));
    }
    canvas.drawPath(p, paint);
}
Also used : GeneralPath(com.codename1.ui.geom.GeneralPath) PathMeasure(com.codename1.charts.compat.PathMeasure) Point(com.codename1.charts.models.Point) Point(com.codename1.charts.models.Point) Paint(com.codename1.charts.compat.Paint)

Example 33 with List

use of com.codename1.ui.List in project CodenameOne by codenameone.

the class LineChart method clickableAreasForPoints.

@Override
protected ClickableArea[] clickableAreasForPoints(List<Float> points, List<Double> values, float yAxisValue, int seriesIndex, int startIndex) {
    int length = points.size();
    ClickableArea[] ret = new ClickableArea[length / 2];
    for (int i = 0; i < length; i += 2) {
        int selectableBuffer = mRenderer.getSelectableBuffer();
        ret[i / 2] = new ClickableArea(PkgUtils.makeRect(points.get(i) - selectableBuffer, points.get(i + 1) - selectableBuffer, points.get(i) + selectableBuffer, points.get(i + 1) + selectableBuffer), values.get(i), values.get(i + 1));
    }
    return ret;
}
Also used : Paint(com.codename1.charts.compat.Paint)

Example 34 with List

use of com.codename1.ui.List 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 35 with List

use of com.codename1.ui.List 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)

Aggregations

Paint (com.codename1.charts.compat.Paint)26 ArrayList (java.util.ArrayList)24 Component (com.codename1.ui.Component)16 List (com.codename1.ui.List)13 Point (com.codename1.charts.models.Point)11 BorderLayout (com.codename1.ui.layouts.BorderLayout)11 Hashtable (java.util.Hashtable)11 Container (com.codename1.ui.Container)9 ContainerList (com.codename1.ui.list.ContainerList)9 Button (com.codename1.ui.Button)8 List (java.util.List)8 TextArea (com.codename1.ui.TextArea)7 Dimension (com.codename1.ui.geom.Dimension)7 EncodedImage (com.codename1.ui.EncodedImage)6 Label (com.codename1.ui.Label)6 RadioButton (com.codename1.ui.RadioButton)5 ActionListener (com.codename1.ui.events.ActionListener)5 JList (javax.swing.JList)4 FileEncodedImage (com.codename1.components.FileEncodedImage)3 StorageImage (com.codename1.components.StorageImage)3