1 2 package com.github.mikephil.charting.renderer; 3 4 import android.graphics.Canvas; 5 import android.graphics.Color; 6 import android.graphics.Paint; 7 import android.graphics.Paint.Align; 8 import android.graphics.Paint.Style; 9 import android.graphics.drawable.Drawable; 10 11 import com.github.mikephil.charting.animation.ChartAnimator; 12 import com.github.mikephil.charting.data.Entry; 13 import com.github.mikephil.charting.formatter.IValueFormatter; 14 import com.github.mikephil.charting.highlight.Highlight; 15 import com.github.mikephil.charting.interfaces.dataprovider.ChartInterface; 16 import com.github.mikephil.charting.interfaces.datasets.IDataSet; 17 import com.github.mikephil.charting.utils.MPPointF; 18 import com.github.mikephil.charting.utils.Utils; 19 import com.github.mikephil.charting.utils.ViewPortHandler; 20 21 /** 22 * Superclass of all render classes for the different data types (line, bar, ...). 23 * 24 * @author Philipp Jahoda 25 */ 26 public abstract class DataRenderer extends Renderer { 27 28 /** 29 * the animator object used to perform animations on the chart data 30 */ 31 protected ChartAnimator mAnimator; 32 33 /** 34 * main paint object used for rendering 35 */ 36 protected Paint mRenderPaint; 37 38 /** 39 * paint used for highlighting values 40 */ 41 protected Paint mHighlightPaint; 42 43 protected Paint mDrawPaint; 44 45 /** 46 * paint object for drawing values (text representing values of chart 47 * entries) 48 */ 49 protected Paint mValuePaint; 50 DataRenderer(ChartAnimator animator, ViewPortHandler viewPortHandler)51 public DataRenderer(ChartAnimator animator, ViewPortHandler viewPortHandler) { 52 super(viewPortHandler); 53 this.mAnimator = animator; 54 55 mRenderPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 56 mRenderPaint.setStyle(Style.FILL); 57 58 mDrawPaint = new Paint(Paint.DITHER_FLAG); 59 60 mValuePaint = new Paint(Paint.ANTI_ALIAS_FLAG); 61 mValuePaint.setColor(Color.rgb(63, 63, 63)); 62 mValuePaint.setTextAlign(Align.CENTER); 63 mValuePaint.setTextSize(Utils.convertDpToPixel(9f)); 64 65 mHighlightPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 66 mHighlightPaint.setStyle(Paint.Style.STROKE); 67 mHighlightPaint.setStrokeWidth(2f); 68 mHighlightPaint.setColor(Color.rgb(255, 187, 115)); 69 } 70 isDrawingValuesAllowed(ChartInterface chart)71 protected boolean isDrawingValuesAllowed(ChartInterface chart) { 72 return chart.getData().getEntryCount() < chart.getMaxVisibleCount() 73 * mViewPortHandler.getScaleX(); 74 } 75 76 /** 77 * Returns the Paint object this renderer uses for drawing the values 78 * (value-text). 79 * 80 * @return 81 */ getPaintValues()82 public Paint getPaintValues() { 83 return mValuePaint; 84 } 85 86 /** 87 * Returns the Paint object this renderer uses for drawing highlight 88 * indicators. 89 * 90 * @return 91 */ getPaintHighlight()92 public Paint getPaintHighlight() { 93 return mHighlightPaint; 94 } 95 96 /** 97 * Returns the Paint object used for rendering. 98 * 99 * @return 100 */ getPaintRender()101 public Paint getPaintRender() { 102 return mRenderPaint; 103 } 104 105 /** 106 * Applies the required styling (provided by the DataSet) to the value-paint 107 * object. 108 * 109 * @param set 110 */ applyValueTextStyle(IDataSet set)111 protected void applyValueTextStyle(IDataSet set) { 112 113 mValuePaint.setTypeface(set.getValueTypeface()); 114 mValuePaint.setTextSize(set.getValueTextSize()); 115 } 116 117 /** 118 * Initializes the buffers used for rendering with a new size. Since this 119 * method performs memory allocations, it should only be called if 120 * necessary. 121 */ initBuffers()122 public abstract void initBuffers(); 123 124 /** 125 * Draws the actual data in form of lines, bars, ... depending on Renderer subclass. 126 * 127 * @param c 128 */ drawData(Canvas c)129 public abstract void drawData(Canvas c); 130 131 /** 132 * Loops over all Entrys and draws their values. 133 * 134 * @param c 135 */ drawValues(Canvas c)136 public abstract void drawValues(Canvas c); 137 138 /** 139 * Draws the value of the given entry by using the provided IValueFormatter. 140 * 141 * @param c canvas 142 * @param formatter formatter for custom value-formatting 143 * @param value the value to be drawn 144 * @param entry the entry the value belongs to 145 * @param dataSetIndex the index of the DataSet the drawn Entry belongs to 146 * @param x position 147 * @param y position 148 * @param color 149 */ drawValue(Canvas c, IValueFormatter formatter, float value, Entry entry, int dataSetIndex, float x, float y, int color)150 public void drawValue(Canvas c, IValueFormatter formatter, float value, Entry entry, int dataSetIndex, float x, float y, int color) { 151 mValuePaint.setColor(color); 152 c.drawText(formatter.getFormattedValue(value, entry, dataSetIndex, mViewPortHandler), x, y, mValuePaint); 153 } 154 155 /** 156 * Draws any kind of additional information (e.g. line-circles). 157 * 158 * @param c 159 */ drawExtras(Canvas c)160 public abstract void drawExtras(Canvas c); 161 162 /** 163 * Draws all highlight indicators for the values that are currently highlighted. 164 * 165 * @param c 166 * @param indices the highlighted values 167 */ drawHighlighted(Canvas c, Highlight[] indices)168 public abstract void drawHighlighted(Canvas c, Highlight[] indices); 169 } 170