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