Search in sources :

Example 6 with XYSeriesRenderer

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

use of com.codename1.charts.renderers.XYSeriesRenderer 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 8 with XYSeriesRenderer

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

Example 9 with XYSeriesRenderer

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

the class RangeBarChart 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);
    int start = 0;
    if (startIndex > 0) {
        start = 2;
    }
    for (int i = start; i < length; i += 4) {
        if (points.size() > i + 3) {
            float xMin = points.get(i);
            float yMin = points.get(i + 1);
            // xMin = xMax
            float xMax = points.get(i + 2);
            float yMax = points.get(i + 3);
            drawBar(canvas, xMin, yMin, xMax, yMax, halfDiffX, seriesNr, seriesIndex, paint);
        }
    }
    paint.setColor(seriesRenderer.getColor());
}
Also used : Paint(com.codename1.charts.compat.Paint)

Example 10 with XYSeriesRenderer

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

the class XYChart method drawSeries.

/**
 * Draws the series.
 *
 * @param series the series
 * @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 or the orientation
 * @param startIndex the start index of the rendering points
 */
protected void drawSeries(XYSeries series, Canvas canvas, Paint paint, List<Float> pointsList, XYSeriesRenderer seriesRenderer, float yAxisValue, int seriesIndex, Orientation or, int startIndex) {
    BasicStroke stroke = seriesRenderer.getStroke();
    int cap = paint.getStrokeCap();
    int join = paint.getStrokeJoin();
    float miter = paint.getStrokeMiter();
    // PathEffect pathEffect = paint.getPathEffect();
    Style style = paint.getStyle();
    if (stroke != null) {
        setStroke(stroke.getCap(), stroke.getJoin(), stroke.getMiter(), Style.FILL_AND_STROKE, paint);
    }
    // float[] points = MathHelper.getFloats(pointsList);
    drawSeries(canvas, paint, pointsList, seriesRenderer, yAxisValue, seriesIndex, startIndex);
    drawPoints(canvas, paint, pointsList, seriesRenderer, yAxisValue, seriesIndex, startIndex);
    paint.setTextSize(seriesRenderer.getChartValuesTextSize());
    if (or == Orientation.HORIZONTAL) {
        paint.setTextAlign(Align.CENTER);
    } else {
        paint.setTextAlign(Align.LEFT);
    }
    if (seriesRenderer.isDisplayChartValues()) {
        paint.setTextAlign(seriesRenderer.getChartValuesTextAlign());
        drawChartValuesText(canvas, series, seriesRenderer, paint, pointsList, seriesIndex, startIndex);
    }
    if (stroke != null) {
        setStroke(cap, join, miter, style, paint);
    }
}
Also used : BasicStroke(com.codename1.charts.renderers.BasicStroke) Style(com.codename1.charts.compat.Paint.Style) Point(com.codename1.charts.models.Point) Paint(com.codename1.charts.compat.Paint)

Aggregations

Paint (com.codename1.charts.compat.Paint)11 Point (com.codename1.charts.models.Point)4 ArrayList (java.util.ArrayList)2 Style (com.codename1.charts.compat.Paint.Style)1 XYSeries (com.codename1.charts.models.XYSeries)1 XYValueSeries (com.codename1.charts.models.XYValueSeries)1 BasicStroke (com.codename1.charts.renderers.BasicStroke)1 Orientation (com.codename1.charts.renderers.XYMultipleSeriesRenderer.Orientation)1 XYSeriesRenderer (com.codename1.charts.renderers.XYSeriesRenderer)1 FillOutsideLine (com.codename1.charts.renderers.XYSeriesRenderer.FillOutsideLine)1 Rectangle (com.codename1.ui.geom.Rectangle)1 Rectangle2D (com.codename1.ui.geom.Rectangle2D)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1