You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For getting started, rely on the **"MPChartExample"** folder and check out the examples in that project. The example project is also [**available in the Google PlayStore**](https://play.google.com/store/apps/details?id=com.xxmassdeveloper.mpchartexample).
159
159
160
-
Furthermore, here is some code to begin with.
160
+
For a **detailed documentation**, please refer the [**Wiki**](https://github.com/PhilJay/MPAndroidChart/wiki).
161
161
162
-
**Setup:**
163
-
164
-
For using a <code>LineChart, BarChart, ScatterChart, CandleStickChart or PieChart </code>, define it in .xml:
or create it in code (and then **add it to a layout**):
176
-
```java
177
-
LineChart chart =newLineChart(Context);
178
-
```
179
-
180
-
**Styling:**
181
-
182
-
- <code>setDescription(String desc)</code>: Set a description text that appears in the bottom right corner of the chart.
183
-
- <code>setDescriptionTypeface(Typeface t)</code>: Sets the <code>Typeface</code> used for drawing the description text.
184
-
- <code>setDrawYValues(boolean enabled)</code>: If set to true, the actual drawn values will be drawn next to the points, bars, or pie slices of the chart.
185
-
- <code>setValuePaintColor(int color)</code>: Sets the color used for drawing the values if <code>setDrawYValues(...)</code> is enabled.
186
-
- <code>setValueTypeface(Typeface t)</code>: Sets the <code>Typeface</code> used for drawing the values if <code>setDrawYValues(...)</code> is enabled.
187
-
- <code>setValueFormatter(DecimalFormat format)</code>: Sets a new `DecimalFormat` object that will be used to format all values inside the chart. If nothing is set, formatting (digits) is done automatically.
188
-
- <code>setPaint(Paint p, int which)</code>: Replaces the specified default <code>Paint</code> object with a new one. This method can be used to replace any predefined <code>Paint</code> object with an own <code>Paint</code> object and develop a completely personalized design.
189
-
190
-
**Getters and convenience:**
191
-
- <code>public ChartData getDataCurrent()</code>: Returns the <code>ChartData</code> object the chart currently displays. It contains all information concerning the displayed values such as minimum and maximum values, value counts, value sums, ...
192
-
- <code>public float getYChartMin()</code>: Returns the current minimum y-value that can be displayed by the chart - bottom line.
193
-
- <code>public float getYChartMax()</code>: Returns the current maximum y-value that can be displayed by the chart - top line.
194
-
- <code>public float getAverage()</code>: Returns the average value across all values the chart holds.
195
-
- <code>public float getAverage(int type)</code>: Returns the average value for a specific DataSet type in the chart. This type is the type provided in the <code>DataSet</code> constructor.
196
-
- <code>public PointF getCenter()</code>: Returns the center point of the chart in pixels.
197
-
- <code>public Paint getPaint(int which)</code>: Returns the requested Paint object from the chart.
198
-
199
-
200
-
**Gestures & Selecting:**
201
-
202
-
- <code>setTouchEnabled(boolean enabled)</code>: If set to ture, touch gestures (such as scaling and dragging) will be possible on the chart. NOTE: If touch-gestures are disabled, highlighting on touch is disabled as well. Default: true.
203
-
- <code>setDragEnabled(boolean enabled)</code>: Enables/disables dragging for the chart.
204
-
- <code>setScaleEnabled(boolean enabled)</code>: Enables/disables scaling for the chart.
205
-
- <code>setOnChartValueSelectedListener(OnChartValueSelectedListener l)</code>: Sets a selection-listener to the chart that will generate callbacks when values are selected or unselected. The callback contains the selected values and their indices.
206
-
- <code>setHighlightEnabled(boolean enabled)</code>: If set to true, highlighting/selecting values is possible on the chart. Default: true.
207
-
- <code>public void highlightValues(Highlight[] highs)</code>: Highlights the specified entries in the chart.
208
-
209
162
**Other:**
210
163
- <code>saveToGallery(String title)</code>: Saves the current chart state as an image to the gallery.
211
164
- <code>saveToPath(String title, String pathOnSD)</code>: Saves the current chart state as an image to the specified path.
212
165
- <code>setScaleMinima(float x, float y)</code>: Sets the minimum scale factors for x- and y-axis. If set for example to 3f, the user will not be able to fully zoom out.
213
166
- <code>centerViewPort(int xIndex, float val)</code>: This method makes it possible to aim the center of the view (what you can see from the chart) to a specific position inside the chart, described by the index on the x-axis and the value on the y-axis. This also works very well in combination with the `setScaleMinima(...)` method.
214
167
- <code>fitScreen()</code>: Resets all zooming and dragging and makes the chart fit exactly it's bounds.
215
168
216
-
**Adding data:**
217
-
218
-
If you want to add values (data) to the chart, it has to be done via the
219
-
220
-
```java
221
-
setData(ChartData data);
222
-
```
223
-
method. The baseclass <code>ChartData</code> class encapsulates all data and information that is needed for the chart during rendering. For each type of chart, a different subclass of `ChartData` (e.g. `LineData`) exists that should be used for setting data for the chart. In the constructor, you can hand over an <code>ArrayList<? extends DataSet></code> as the values to display, and an additional <code>ArrayList</code> of <code>String</code> that will describe the labels on the x-axis. Example with the class `LineData` (extends `ChartData`), which is used for adding data to a `LineChart`:
224
-
225
-
```java
226
-
public LineData(ArrayList<String> xVals, ArrayList<LineDataSet> sets) { ... }
227
-
```
228
-
229
-
So, what is a <code>DataSet</code> and why do you need it? That is actually pretty simple. One <code>DataSet</code> object represents a group of entries (datatype <code>Entry</code>) inside the chart that belong together. It is designed to **logically separate different groups of values in the chart**. For each type of chart, a differnt object that extends `DataSet` (e.g. `LineDataSet`) exists that allows specific styling.
230
-
231
-
As an example, you might want to display the quarterly revenue of two different companies over one year in a `LineChart`. In that case, it would be recommended to create two different <code>LineDataSet</code> objects, each containing four values (one for each quarter). As an <code>ArrayList<String></code> to describe the labels on the x-axis, you would simply provide the four Strings "1.Q", "2.Q", "3.Q", "4.Q".
232
-
233
-
Of course, it is also possible to provide just one <code>LineDataSet</code> object containing all 8 values for the two companys.
234
-
235
-
So how to setup a <code>LineDataSet</code> object?
236
-
```java
237
-
public LineDataSet(ArrayList<Entry> yVals, String label) { ... }
238
-
```
239
-
240
-
When looking at the constructor, it is visible that the <code>LineDataSet</code> needs an <code>ArrayList</code> of type <code>Entry</code> and a `String` used to describe the `LineDataSet` and as a label used for the `Legend`. Furthermore this label can be used to find the `LineDataSet` amongst other `LineDataSet` objects in the `LineData` object.
241
-
242
-
The <code>ArrayList</code> of type <code>Entry</code> encapsulates all values of the chart. A <code>Entry</code> object is an additional wrapper around a value and holds the value itself, and it's position on the x-axis (the index inside the <code>ArrayList</code> of <code>String</code> of the <code>LineData</code> object the value is mapped to):
243
-
```java
244
-
public Entry(float val, int xIndex) { ... }
245
-
```
246
-
247
-
Putting it all together (example of two companies with quarterly revenue over one year):
248
-
249
-
At first, create the lists of type <code>Entry</code> that will hold your values:
Then, fill the lists with <code>Entry</code> objects. Make sure the entry objects contain the correct indices to the x-axis. (of course, a loop can be used here, in that case, the counter variable of the loop could be the index on the x-axis).
Since release [v1.4.0](https://github.com/PhilJay/MPAndroidChart/releases/tag/v1.4.0), the `ColorTemplate` object that was responsible for setting colors in previous releases is no longer needed. Nevertheless, it still holds all predefined color arrays (e.g. `ColorTemplate.VORDIPLOM_COLORS` and provides convenience methods for transforming colors from the resources (resource integers) into "real" colors.
293
-
294
-
Instead of the `ColorTemplate`, colors can now be specified directly via `DataSet` object, which allows separate styling for each `DataSet`.
295
-
296
-
In this short example, we have our two different `LineDataSet` objects representing the quarterly revenues of two companies (previously mentioned in the **Adding data** tutorial), for which we now want to set different colors.
297
-
298
-
What we want:
299
-
300
-
- the values of "Company 1" should be represented by four different variations of the color "red"
301
-
- the values of "Company 2" should be represented by four different variations of the color "green"
Besides that, there are many other ways for setting colors for a `DataSet`. Here is the full documentation:
316
-
317
-
-`setColors(int [] colors, Context c)`: Sets the colors that should be used fore this DataSet. Colors are reused as soon as the number of Entries the DataSet represents is higher than the size of the colors array. You can use "new int[] { R.color.red, R.color.green, ... }" to provide colors for this method. Internally, the colors are resolved using getResources().getColor(...).
318
-
-`setColors(int [] colors)`: Sets the colors that should be used fore this DataSet. Colors are reused as soon as the number of Entries the DataSet represents is higher than the size of the colors array. Make sure that the colors are already prepared (by calling getResources().getColor(...)) before adding them to the DataSet.
319
-
-`setColors(ArrayList<Integer> colors)`: Sets the colors that should be used fore this DataSet. Colors are reused as soon as the number of Entries the DataSet represents is higher than the size of the colors array. Make sure that the colors are already prepared (by calling getResources().getColor(...)) before adding them to the DataSet.
320
-
-`setColor(int color)`: Sets the one and ONLY color that should be used for this DataSet. Internally, this recreates the colors array and adds the specified color.
321
-
322
-
`ColorTemplate` example:
323
-
324
-
```java
325
-
LineDataSet set =newLineDataSet(...);
326
-
set.setColors(ColorTemplate.VORDIPLOM_COLORS);
327
-
```
328
-
329
-
If no colors are set for a `DataSet`, default colors are used.
330
-
331
-
332
169
**Displaying / styling legends:**
333
170
334
171
By default, all chart types support legends and will automatically generate and draw a legend after setting data for the chart. If a legend should be drawn or not can be enabled/disabled using the
0 commit comments