MPAndroidChart 教程:开始 Getting Started
入門
本章介紹使用此庫的基本設(shè)置。
添加依賴
首先,將此庫的依賴項添加到項目中。如何執(zhí)行此操作在此存儲庫的用法部分中進(jìn)行了描述。Gradle是使用此庫作為依賴項的推薦方法。
創(chuàng)建視圖
要使用LineChart, BarChart, ScatterChart, CandleStickChart, PieChart, BubbleChart or RadarChart?,請在.xml中定義它:
<com.github.mikephil.charting.charts.LineChartandroid:id="@+id/chart"android:layout_width="match_parent"android:layout_height="match_parent" />然后從您的Activity,Fragment或其他內(nèi)容中檢索它:
// in this example, a LineChart is initialized from xmlLineChart chart = (LineChart) findViewById(R.id.chart);或者在代碼中創(chuàng)建它(然后將其添加到布局中):
// programmatically create a LineChartLineChart chart = new LineChart(Context);// get a layout defined in xmlRelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout);rl.add(chart); // add the programmatically created chart添加數(shù)據(jù)
擁有圖表實例后,您可以創(chuàng)建數(shù)據(jù)并將其添加到圖表中。此示例使用LineChart,其中Entry類表示圖表中具有x和y坐標(biāo)的單個條目。其他圖表類型(例如BarChart)使用其他類(例如BarEntry)。
要將數(shù)據(jù)添加到圖表中,請將您擁有的每個數(shù)據(jù)對象包裝到Entry對象中,如下所示:
YourData[] dataObjects = ...; List<Entry> entries = new ArrayList<Entry>(); for (YourData data : dataObjects) {// turn your data into Entry objectsentries.add(new Entry(data.getValueX(), data.getValueY())); }下一步,您需要將創(chuàng)建的List<Entry>添加到LineDataSet對象中。DataSet對象保存屬于一起的數(shù)據(jù),并允許對該數(shù)據(jù)進(jìn)行單獨設(shè)計。以下使用的“Label ”僅具有描述性目的,并在Legend中顯示(如果已啟用)。
LineDataSet dataSet = new LineDataSet(entries, "Label"); // add entries to dataset dataSet.setColor(...); dataSet.setValueTextColor(...); // styling, ...最后一步,您需要將創(chuàng)建的LineDataSet對象(或多個對象)添加到LineData對象中。此對象包含由Chart實例表示的所有數(shù)據(jù),并允許進(jìn)一步樣式化。創(chuàng)建數(shù)據(jù)對象后,您可以將其設(shè)置為圖表并刷新它:
LineData lineData = new LineData(dataSet); chart.setData(lineData); chart.invalidate(); // refresh請考慮上面的場景一個非常基本的設(shè)置。有關(guān)更詳細(xì)的說明,請參閱設(shè)置數(shù)據(jù)部分,它解釋了如何根據(jù)示例將數(shù)據(jù)添加到各種圖表類型。
造型
有關(guān)圖表表面和數(shù)據(jù)的設(shè)置和樣式的信息,請訪問常規(guī)設(shè)置和樣式部分。有關(guān)各個圖表類型的更具體的樣式和設(shè)置,請查看特定設(shè)置和樣式?Wiki頁面。
?
參考:
https://github.com/PhilJay/MPAndroidChart/wiki/Getting-Started
https://blog.csdn.net/u014136472/article/details/50293767
?
?
總結(jié)
以上是生活随笔為你收集整理的MPAndroidChart 教程:开始 Getting Started的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2018年9月份GitHub上最热门的P
- 下一篇: Kotlin 继续助力 Android