1 /* 2 * Copyright 2012 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.ui; 18 19 import android.graphics.Canvas; 20 import android.graphics.RectF; 21 import android.graphics.Region; 22 import com.androidplot.Series; 23 import com.androidplot.exception.PlotRenderException; 24 import com.androidplot.Plot; 25 26 public abstract class SeriesRenderer 27 <PlotType extends Plot, SeriesType extends Series, SeriesFormatterType extends Formatter> { 28 private PlotType plot; 29 SeriesRenderer(PlotType plot)30 public SeriesRenderer(PlotType plot) { 31 this.plot = plot; 32 } 33 getPlot()34 public PlotType getPlot() { 35 return plot; 36 } 37 setPlot(PlotType plot)38 public void setPlot(PlotType plot) { 39 this.plot = plot; 40 } 41 getSeriesAndFormatterList()42 public SeriesAndFormatterList<SeriesType,SeriesFormatterType> getSeriesAndFormatterList() { 43 return plot.getSeriesAndFormatterListForRenderer(getClass()); 44 } 45 getFormatter(SeriesType series)46 public SeriesFormatterType getFormatter(SeriesType series) { 47 return (SeriesFormatterType) plot.getFormatter(series, getClass()); 48 } 49 render(Canvas canvas, RectF plotArea)50 public void render(Canvas canvas, RectF plotArea) throws PlotRenderException { 51 onRender(canvas, plotArea); 52 } onRender(Canvas canvas, RectF plotArea)53 public abstract void onRender(Canvas canvas, RectF plotArea) throws PlotRenderException; 54 55 /** 56 * Draw the legend icon in the rect passed in. 57 * @param canvas 58 * @param rect 59 */ doDrawLegendIcon(Canvas canvas, RectF rect, SeriesFormatterType formatter)60 protected abstract void doDrawLegendIcon(Canvas canvas, RectF rect, SeriesFormatterType formatter); 61 drawSeriesLegendIcon(Canvas canvas, RectF rect, SeriesFormatterType formatter)62 public void drawSeriesLegendIcon(Canvas canvas, RectF rect, SeriesFormatterType formatter) { 63 //int state = canvas.save(Canvas.CLIP_SAVE_FLAG); 64 try { 65 canvas.save(); 66 canvas.clipRect(rect); 67 doDrawLegendIcon(canvas, rect, formatter); 68 //canvas.restoreToCount(state); 69 } finally { 70 canvas.restore(); 71 } 72 } 73 } 74