Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 9k
Getting Started
This chapter covers the basic setup for using this library.
As a first step, add a dependency to this library to your project. How to do that is described in the usage section of this repository. Gradle is the recommended way of using this library as a dependency.
For using a LineChart, BarChart, ScatterChart, CandleStickChart, PieChart, BubbleChart or RadarChart , define it in .xml:
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart"android:layout_width="match_parent"android:layout_height="match_parent" />And then retrieve it from your Activity, Fragment or whatever:
// in this example, a LineChart is initialized from xmlLineChartchart = (LineChart) findViewById(R.id.chart);or create it in code (and then add it to a layout):
// programmatically create a LineChartLineChartchart = newLineChart(Context);
// get a layout defined in xmlRelativeLayoutrl = (RelativeLayout) findViewById(R.id.relativeLayout);
rl.add(chart); // add the programmatically created chartAfter you have an instance of your chart, you can create data and add it to the chart. This example uses the LineChart, for which the Entry class represents a single entry in the chart with x- and y-coordinate. Other chart types, such as BarChart use other classes (e.g. BarEntry) for that purpose.
To add data to your chart, wrap each data object you have into an Entry object, like below:
YourData[] dataObjects = ...;
List<Entry> entries = newArrayList<Entry>();
for (YourDatadata : dataObjects) {
// turn your data into Entry objectsentries.add(newEntry(data.getValueX(), data.getValueY())); }As a next step, you need to add the List<Entry> you created to a LineDataSet object. DataSet objects hold data which belongs together, and allow individual styling of that data. The below used "Label" has only a descriptive purpose and shows up in the Legend, if enabled.
LineDataSetdataSet = newLineDataSet(entries, "Label"); // add entries to datasetdataSet.setColor(...);
dataSet.setValueTextColor(...); // styling, ...As a last step, you need to add the LineDataSet object (or objects) you created to a LineData object. This object holds all data that is represented by a Chart instance and allows further styling. After creating the data object, you can set it to the chart and refresh it:
LineDatalineData = newLineData(dataSet);
chart.setData(lineData);
chart.invalidate(); // refreshPlease consider the scenario above a very basic setup. For a more detailed explanation please refer to the setting data section, which explains how to add data to various Chart types based on examples.
For information about settings & styling of the chart surface and data, visit the general settings & styling section. Regarding more specific styling & settings for individual chart types, please have a look at the specific settings & styling wiki page.