|
| 1 | +package com.androidplot.ui.widget; |
| 2 | + |
| 3 | +import android.graphics.Canvas; |
| 4 | +import android.graphics.Color; |
| 5 | +import android.graphics.Paint; |
| 6 | +import android.graphics.RectF; |
| 7 | +import android.support.annotation.NonNull; |
| 8 | +import android.support.annotation.Nullable; |
| 9 | + |
| 10 | +import com.androidplot.exception.PlotRenderException; |
| 11 | +import com.androidplot.ui.LayoutManager; |
| 12 | +import com.androidplot.ui.Size; |
| 13 | +import com.androidplot.ui.TableModel; |
| 14 | +import com.androidplot.util.FontUtils; |
| 15 | +import com.androidplot.util.PixelUtils; |
| 16 | + |
| 17 | +import java.util.Collections; |
| 18 | +import java.util.Comparator; |
| 19 | +import java.util.Iterator; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +/** |
| 23 | + * Provides core functionality for displaying a legend widget within a {@link com.androidplot.Plot}. |
| 24 | + * @param <ItemT> |
| 25 | + */ |
| 26 | +public abstract class LegendWidget<ItemT extends LegendItem> extends Widget { |
| 27 | + |
| 28 | + private static final float DEFAULT_TEXT_SIZE_DP = 20; |
| 29 | + |
| 30 | + private TableModel tableModel; |
| 31 | + private Size iconSize; |
| 32 | + |
| 33 | + private Paint textPaint; |
| 34 | + private Paint iconBackgroundPaint; |
| 35 | + private Paint iconBorderPaint; |
| 36 | + |
| 37 | + private boolean drawIconBackgroundEnabled = true; |
| 38 | + private boolean drawIconBorderEnabled = true; |
| 39 | + |
| 40 | + private Comparator<ItemT> legendItemComparator; |
| 41 | + |
| 42 | + { |
| 43 | + textPaint = new Paint(); |
| 44 | + textPaint.setColor(Color.LTGRAY); |
| 45 | + textPaint.setTextSize(PixelUtils.spToPix(DEFAULT_TEXT_SIZE_DP)); |
| 46 | + textPaint.setAntiAlias(true); |
| 47 | + |
| 48 | + iconBackgroundPaint = new Paint(); |
| 49 | + iconBackgroundPaint.setColor(Color.BLACK); |
| 50 | + |
| 51 | + iconBorderPaint = new Paint(); |
| 52 | + iconBorderPaint.setColor(Color.TRANSPARENT); |
| 53 | + iconBorderPaint.setStyle(Paint.Style.STROKE); |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + public LegendWidget(@NonNull TableModel tableModel, @NonNull LayoutManager layoutManager, |
| 58 | + @NonNull Size size, @NonNull Size iconSize) { |
| 59 | + super(layoutManager, size); |
| 60 | + setTableModel(tableModel); |
| 61 | + this.iconSize = iconSize; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + protected void doOnDraw(Canvas canvas, RectF widgetRect) throws PlotRenderException { |
| 66 | + final List<ItemT> items = getLegendItems(); |
| 67 | + if(legendItemComparator != null) { |
| 68 | + Collections.sort(items, legendItemComparator); |
| 69 | + } |
| 70 | + final Iterator<RectF> cellRectIterator = tableModel.getIterator(widgetRect, items.size()); |
| 71 | + for(ItemT item : items) { |
| 72 | + final RectF cellRect = cellRectIterator.next(); |
| 73 | + final RectF iconRect = getIconRect(cellRect); |
| 74 | + beginDrawingCell(canvas, iconRect); |
| 75 | + drawItem(canvas, iconRect, item); |
| 76 | + finishDrawingCell(canvas, cellRect, iconRect, item); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + protected void drawItem(@NonNull Canvas canvas, @NonNull RectF iconRect, @NonNull ItemT item) { |
| 81 | + drawIcon(canvas, iconRect, item); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Draw the icon representing the legend item |
| 86 | + * @param canvas |
| 87 | + * @param iconRect The space to be occupied by the icon. |
| 88 | + * @param item |
| 89 | + */ |
| 90 | + protected abstract void drawIcon(@NonNull Canvas canvas, @NonNull RectF iconRect, @NonNull ItemT item); |
| 91 | + |
| 92 | + /** |
| 93 | + * |
| 94 | + * @return The list of legend items to be drawn. This is used to calculate table dimensions etc. |
| 95 | + */ |
| 96 | + protected abstract List<ItemT> getLegendItems(); |
| 97 | + |
| 98 | + private RectF getIconRect(RectF cellRect) { |
| 99 | + float cellRectCenterY = cellRect.top + (cellRect.height()/2); |
| 100 | + RectF iconRect = iconSize.getRectF(cellRect); |
| 101 | + |
| 102 | + // center the icon rect vertically |
| 103 | + float centeredIconOriginY = cellRectCenterY - (iconRect.height()/2); |
| 104 | + iconRect.offsetTo(cellRect.left + 1, centeredIconOriginY); |
| 105 | + return iconRect; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Done at the start of rendering a new cell. Whatever is drawn here will be beneath the rest |
| 110 | + * of the cell content; typically used to draw backgrounds. |
| 111 | + * @param canvas |
| 112 | + * @param iconRect |
| 113 | + */ |
| 114 | + protected void beginDrawingCell(Canvas canvas, RectF iconRect) { |
| 115 | + |
| 116 | + if(drawIconBackgroundEnabled && iconBackgroundPaint != null) { |
| 117 | + canvas.drawRect(iconRect, iconBackgroundPaint); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Done at the end of rendering a new cell. Whatever is drawn here will be on top of |
| 123 | + * the rest of the cell content; typically used to draw borders and text. |
| 124 | + * @param canvas |
| 125 | + * @param cellRect |
| 126 | + * @param iconRect |
| 127 | + * @param legendItem |
| 128 | + */ |
| 129 | + protected void finishDrawingCell(Canvas canvas, RectF cellRect, RectF iconRect, LegendItem legendItem) { |
| 130 | + |
| 131 | + if(drawIconBorderEnabled && iconBorderPaint != null) { |
| 132 | + canvas.drawRect(iconRect, iconBorderPaint); |
| 133 | + } |
| 134 | + |
| 135 | + float centeredTextOriginY = getRectCenterY(cellRect) + (FontUtils.getFontHeight(textPaint)/2); |
| 136 | + |
| 137 | + if (textPaint.getTextAlign().equals(Paint.Align.RIGHT)) { |
| 138 | + canvas.drawText(legendItem.getTitle(), iconRect.left - 2, centeredTextOriginY, textPaint); |
| 139 | + } else { |
| 140 | + canvas.drawText(legendItem.getTitle(), iconRect.right + 2, centeredTextOriginY, textPaint); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + protected static float getRectCenterY(RectF cellRect) { |
| 145 | + return cellRect.top + (cellRect.height()/2); |
| 146 | + } |
| 147 | + |
| 148 | + public synchronized void setTableModel(TableModel tableModel) { |
| 149 | + this.tableModel = tableModel; |
| 150 | + } |
| 151 | + |
| 152 | + public Paint getTextPaint() { |
| 153 | + return textPaint; |
| 154 | + } |
| 155 | + |
| 156 | + public void setTextPaint(Paint textPaint) { |
| 157 | + this.textPaint = textPaint; |
| 158 | + } |
| 159 | + |
| 160 | + public boolean isDrawIconBackgroundEnabled() { |
| 161 | + return drawIconBackgroundEnabled; |
| 162 | + } |
| 163 | + |
| 164 | + public void setDrawIconBackgroundEnabled(boolean drawIconBackgroundEnabled) { |
| 165 | + this.drawIconBackgroundEnabled = drawIconBackgroundEnabled; |
| 166 | + } |
| 167 | + |
| 168 | + public boolean isDrawIconBorderEnabled() { |
| 169 | + return drawIconBorderEnabled; |
| 170 | + } |
| 171 | + |
| 172 | + public void setDrawIconBorderEnabled(boolean drawIconBorderEnabled) { |
| 173 | + this.drawIconBorderEnabled = drawIconBorderEnabled; |
| 174 | + } |
| 175 | + |
| 176 | + public Size getIconSize() { |
| 177 | + return iconSize; |
| 178 | + } |
| 179 | + |
| 180 | + public void setIconSize(Size iconSize) { |
| 181 | + this.iconSize = iconSize; |
| 182 | + } |
| 183 | + |
| 184 | + public Comparator<ItemT> getLegendItemComparator() { |
| 185 | + return legendItemComparator; |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Set a scheme for sorting the display order or legend items. By default no sorting is applied |
| 190 | + * and {@link com.androidplot.Series} items typically appear in the order which the series was |
| 191 | + * added to the {@link com.androidplot.Plot}. |
| 192 | + * @param legendItemComparator |
| 193 | + */ |
| 194 | + public void setLegendItemComparator(Comparator<ItemT> legendItemComparator) { |
| 195 | + this.legendItemComparator = legendItemComparator; |
| 196 | + } |
| 197 | +} |
0 commit comments