|
| 1 | + |
| 2 | +package com.github.mikephil.charting.components; |
| 3 | + |
| 4 | +import android.graphics.Color; |
| 5 | +import android.graphics.DashPathEffect; |
| 6 | +import android.graphics.Paint; |
| 7 | + |
| 8 | +import com.github.mikephil.charting.utils.Utils; |
| 9 | + |
| 10 | +/** |
| 11 | + * The limit line is an additional feature for all Line-, Bar- and |
| 12 | + * ScatterCharts. It allows the displaying of an additional line in the chart |
| 13 | + * that marks a certain maximum / limit on the specified axis (x- or y-axis). |
| 14 | + */ |
| 15 | +public class LimitRange extends ComponentBase { |
| 16 | + |
| 17 | + public static class Range { |
| 18 | + private final float mLow; |
| 19 | + private final float mHigh; |
| 20 | + |
| 21 | + Range(float r1, float r2) { |
| 22 | + if (r1 < r2) { |
| 23 | + mLow = r1; |
| 24 | + mHigh = r2; |
| 25 | + } else { |
| 26 | + mLow = r2; |
| 27 | + mHigh = r1; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + public float getLow() { |
| 32 | + return mLow; |
| 33 | + } |
| 34 | + |
| 35 | + public float getHigh() { |
| 36 | + return mHigh; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * limit / maximum (the y-value or xIndex) |
| 42 | + */ |
| 43 | + private Range mLimit; |
| 44 | + |
| 45 | + /** |
| 46 | + * the width of the limit line |
| 47 | + */ |
| 48 | + private float mLineWidth = 0f; |
| 49 | + |
| 50 | + /** |
| 51 | + * the color of the limit line |
| 52 | + */ |
| 53 | + private int mLineColor = Color.rgb(237, 91, 91); |
| 54 | + |
| 55 | + /** |
| 56 | + * the color of the Range |
| 57 | + */ |
| 58 | + private int mRangeColor = Color.rgb(128, 128, 128); |
| 59 | + |
| 60 | + /** |
| 61 | + * the style of the label text |
| 62 | + */ |
| 63 | + private Paint.Style mTextStyle = Paint.Style.FILL; |
| 64 | + |
| 65 | + /** |
| 66 | + * label string that is drawn next to the limit line |
| 67 | + */ |
| 68 | + private String mLabel = ""; |
| 69 | + |
| 70 | + /** |
| 71 | + * the path effect of this LimitLine that makes dashed lines possible |
| 72 | + */ |
| 73 | + private DashPathEffect mDashPathEffect = null; |
| 74 | + |
| 75 | + /** |
| 76 | + * indicates the position of the LimitLine label |
| 77 | + */ |
| 78 | + private LimitLine.LimitLabelPosition mLabelPosition = LimitLine.LimitLabelPosition.RIGHT_TOP; |
| 79 | + |
| 80 | + /** |
| 81 | + * Constructor with limit. |
| 82 | + * |
| 83 | + * @param firstLimit - the position (the value) on the y-axis (y-value) or x-axis |
| 84 | + * (xIndex) where this line should appear |
| 85 | + * @param secondLimit - the position (the value) on the y-axis (y-value) or x-axis |
| 86 | + * (xIndex) where this line should appear |
| 87 | + */ |
| 88 | + public LimitRange(float firstLimit, float secondLimit) { |
| 89 | + mLimit = new Range(firstLimit, secondLimit); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Constructor with limit and label. |
| 94 | + * |
| 95 | + * @param firstLimit - the position (the value) on the y-axis (y-value) or x-axis |
| 96 | + * (xIndex) where this line should appear |
| 97 | + * @param secondLimit - the position (the value) on the y-axis (y-value) or x-axis |
| 98 | + * (xIndex) where this line should appear |
| 99 | + * @param label - provide "" if no label is required |
| 100 | + */ |
| 101 | + public LimitRange(float firstLimit, float secondLimit, String label) { |
| 102 | + mLimit = new Range(firstLimit, secondLimit); |
| 103 | + mLabel = label; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Returns the limit that is set for this line. |
| 108 | + * |
| 109 | + * @return |
| 110 | + */ |
| 111 | + public Range getLimit() { |
| 112 | + return mLimit; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * set the line width of the chart (min = 0.2f, max = 12f); default 2f NOTE: |
| 117 | + * thinner line == better performance, thicker line == worse performance |
| 118 | + * |
| 119 | + * @param width |
| 120 | + */ |
| 121 | + public void setLineWidth(float width) { |
| 122 | + if (width > 12.0f) { |
| 123 | + width = 12.0f; |
| 124 | + } |
| 125 | + mLineWidth = Utils.convertDpToPixel(width); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * returns the width of limit line |
| 130 | + * |
| 131 | + * @return |
| 132 | + */ |
| 133 | + public float getLineWidth() { |
| 134 | + return mLineWidth; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Sets the linecolor for this LimitLine. Make sure to use |
| 139 | + * getResources().getColor(...) |
| 140 | + * |
| 141 | + * @param color |
| 142 | + */ |
| 143 | + public void setLineColor(int color) { |
| 144 | + mLineColor = color; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Sets the range color for this LimitRange. Make sure to use |
| 149 | + * getResources().getColor(...) |
| 150 | + * |
| 151 | + * @param color |
| 152 | + */ |
| 153 | + public void setRangeColor(int color) { |
| 154 | + mRangeColor = color; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Returns the color that is used for this LimitLine |
| 159 | + * |
| 160 | + * @return |
| 161 | + */ |
| 162 | + public int getLineColor() { |
| 163 | + return mLineColor; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Returns the color that is used for this LimitRange |
| 168 | + * |
| 169 | + * @return |
| 170 | + */ |
| 171 | + public int getRangeColor() { |
| 172 | + return mRangeColor; |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Enables the line to be drawn in dashed mode, e.g. like this "- - - - - -" |
| 177 | + * |
| 178 | + * @param lineLength the length of the line pieces |
| 179 | + * @param spaceLength the length of space inbetween the pieces |
| 180 | + * @param phase offset, in degrees (normally, use 0) |
| 181 | + */ |
| 182 | + public void enableDashedLine(float lineLength, float spaceLength, float phase) { |
| 183 | + mDashPathEffect = new DashPathEffect(new float[]{ |
| 184 | + lineLength, spaceLength |
| 185 | + }, phase); |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Disables the line to be drawn in dashed mode. |
| 190 | + */ |
| 191 | + public void disableDashedLine() { |
| 192 | + mDashPathEffect = null; |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Returns true if the dashed-line effect is enabled, false if not. Default: |
| 197 | + * disabled |
| 198 | + * |
| 199 | + * @return |
| 200 | + */ |
| 201 | + public boolean isDashedLineEnabled() { |
| 202 | + return mDashPathEffect == null ? false : true; |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * returns the DashPathEffect that is set for this LimitLine |
| 207 | + * |
| 208 | + * @return |
| 209 | + */ |
| 210 | + public DashPathEffect getDashPathEffect() { |
| 211 | + return mDashPathEffect; |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Sets the color of the value-text that is drawn next to the LimitLine. |
| 216 | + * Default: Paint.Style.FILL_AND_STROKE |
| 217 | + * |
| 218 | + * @param style |
| 219 | + */ |
| 220 | + public void setTextStyle(Paint.Style style) { |
| 221 | + this.mTextStyle = style; |
| 222 | + } |
| 223 | + |
| 224 | + /** |
| 225 | + * Returns the color of the value-text that is drawn next to the LimitLine. |
| 226 | + * |
| 227 | + * @return |
| 228 | + */ |
| 229 | + public Paint.Style getTextStyle() { |
| 230 | + return mTextStyle; |
| 231 | + } |
| 232 | + |
| 233 | + /** |
| 234 | + * Sets the position of the LimitLine value label (either on the right or on |
| 235 | + * the left edge of the chart). Not supported for RadarChart. |
| 236 | + * |
| 237 | + * @param pos |
| 238 | + */ |
| 239 | + public void setLabelPosition(LimitLine.LimitLabelPosition pos) { |
| 240 | + mLabelPosition = pos; |
| 241 | + } |
| 242 | + |
| 243 | + /** |
| 244 | + * Returns the position of the LimitLine label (value). |
| 245 | + * |
| 246 | + * @return |
| 247 | + */ |
| 248 | + public LimitLine.LimitLabelPosition getLabelPosition() { |
| 249 | + return mLabelPosition; |
| 250 | + } |
| 251 | + |
| 252 | + /** |
| 253 | + * Sets the label that is drawn next to the limit line. Provide "" if no |
| 254 | + * label is required. |
| 255 | + * |
| 256 | + * @param label |
| 257 | + */ |
| 258 | + public void setLabel(String label) { |
| 259 | + mLabel = label; |
| 260 | + } |
| 261 | + |
| 262 | + /** |
| 263 | + * Returns the label that is drawn next to the limit line. |
| 264 | + * |
| 265 | + * @return |
| 266 | + */ |
| 267 | + public String getLabel() { |
| 268 | + return mLabel; |
| 269 | + } |
| 270 | +} |
0 commit comments