Search in sources :

Example 1 with FillOutsideLine

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

Aggregations

Paint (com.codename1.charts.compat.Paint)1 FillOutsideLine (com.codename1.charts.renderers.XYSeriesRenderer.FillOutsideLine)1 ArrayList (java.util.ArrayList)1