|
| 1 | +/* |
| 2 | + * Copyright 2016 AndroidPlot.com |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.androidplot.xy; |
| 18 | + |
| 19 | +import android.graphics.*; |
| 20 | +import com.androidplot.exception.PlotRenderException; |
| 21 | +import com.androidplot.ui.RenderStack; |
| 22 | +import com.androidplot.ui.SeriesRenderer; |
| 23 | +import com.androidplot.util.ValPixConverter; |
| 24 | +import java.util.ArrayList; |
| 25 | + |
| 26 | +/** |
| 27 | + * This is an experimental (but stable) implementation of an {@link XYSeriesRenderer} that provides instrumentation |
| 28 | + * allowing advanced behaviors like dynamically coloring / styling individual segments of a series, etc. This class |
| 29 | + * may be removed or renamed in future releases. |
| 30 | + * Currently has the following constraints: |
| 31 | + * - Interpolation is not supported |
| 32 | + * - Only draws lines; no points or fill |
| 33 | + * - Draws series lines using simple Canvas.drawLine(...) invocations. |
| 34 | + * @since 0.9.9 |
| 35 | + */ |
| 36 | +public class AdvancedLineAndPointRenderer extends XYSeriesRenderer<XYSeries, AdvancedLineAndPointRenderer.Formatter> { |
| 37 | + |
| 38 | + private int latestIndex; |
| 39 | + |
| 40 | + public AdvancedLineAndPointRenderer(XYPlot plot) { |
| 41 | + super(plot); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + protected void onRender(Canvas canvas, RectF plotArea, XYSeries series, Formatter formatter, RenderStack stack) throws PlotRenderException { |
| 46 | + PointF thisPoint; |
| 47 | + PointF lastPoint = null; |
| 48 | + ArrayList<PointF> points = new ArrayList<>(series.size()); |
| 49 | + for (int i = 0; i < series.size(); i++) { |
| 50 | + Number y = series.getY(i); |
| 51 | + Number x = series.getX(i); |
| 52 | + |
| 53 | + if (y != null && x != null) { |
| 54 | + thisPoint = ValPixConverter.valToPix( |
| 55 | + x, y, |
| 56 | + plotArea, |
| 57 | + getPlot().getCalculatedMinX(), |
| 58 | + getPlot().getCalculatedMaxX(), |
| 59 | + getPlot().getCalculatedMinY(), |
| 60 | + getPlot().getCalculatedMaxY()); |
| 61 | + points.add(thisPoint); |
| 62 | + } else { |
| 63 | + thisPoint = null; |
| 64 | + } |
| 65 | + |
| 66 | + // don't need to do any of this if the line isnt going to be drawn: |
| 67 | + if(formatter.getLinePaint() != null) { |
| 68 | + if (thisPoint != null && lastPoint != null) { |
| 69 | + canvas.drawLine(lastPoint.x, lastPoint.y, thisPoint.x, thisPoint.y, formatter.getLinePaint(i, latestIndex, series.size())); |
| 70 | + } |
| 71 | + } |
| 72 | + lastPoint = thisPoint; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + protected void doDrawLegendIcon(Canvas canvas, RectF rect, Formatter formatter) { |
| 78 | + if(formatter.getLinePaint() != null) { |
| 79 | + canvas.drawLine(rect.left, rect.bottom, rect.right, rect.top, formatter.getLinePaint()); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public void setLatestIndex(int latestIndex) { |
| 84 | + this.latestIndex = latestIndex; |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + /** |
| 89 | + * Formatter designed to work in tandem with {@link AdvancedLineAndPointRenderer}. |
| 90 | + * @since 0.9.9 |
| 91 | + */ |
| 92 | + public static class Formatter extends XYSeriesFormatter<XYRegionFormatter> { |
| 93 | + |
| 94 | + private Paint linePaint; |
| 95 | + |
| 96 | + { |
| 97 | + linePaint = new Paint(); |
| 98 | + linePaint.setStrokeWidth(3); |
| 99 | + linePaint.setColor(Color.RED); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public Class<? extends SeriesRenderer> getRendererClass() { |
| 104 | + return AdvancedLineAndPointRenderer.class; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public SeriesRenderer getRendererInstance(XYPlot plot) { |
| 109 | + return new AdvancedLineAndPointRenderer(plot); |
| 110 | + } |
| 111 | + |
| 112 | + public Paint getLinePaint() { |
| 113 | + return linePaint; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * By default, simply returns the line paint as-is. May be overridden to provide custom behavior based |
| 118 | + * on input params. |
| 119 | + * @param thisIndex |
| 120 | + * @param latestIndex |
| 121 | + * @param seriesSize |
| 122 | + * @return |
| 123 | + */ |
| 124 | + public Paint getLinePaint(int thisIndex, int latestIndex, int seriesSize) { |
| 125 | + return getLinePaint(); |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + public void setLinePaint(Paint linePaint) { |
| 130 | + this.linePaint = linePaint; |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments