您好,登錄后才能下訂單哦!
在項(xiàng)目當(dāng)中很多時(shí)候要對(duì)數(shù)據(jù)進(jìn)行分析就要用到圖表,在gitHub上有很多優(yōu)秀的圖表開源庫(kù),今天給大家分享的就是MPAndroidChart中的柱狀圖。簡(jiǎn)單介紹一下MPAndroidChart:他可以實(shí)現(xiàn)圖表的拖動(dòng),3D,局部查看,數(shù)據(jù)動(dòng)態(tài)展示等功能。
官方源碼地址:github.com/PhilJay/MPA…
廢話就不多說(shuō)了,先給看大家看看效果圖
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0-alpha'
implementation 'com.google.android.material:material:1.0.0'
???<com.github.mikephil.charting.charts.BarChart ????android:id="@+id/chart1" ????android:layout_width="match_parent" ????android:layout_height="300dp" ????/>
??/** ?*?Class?to?format?all?values?before?they?are?drawn?as?labels. ?*/ ?public?abstract?class?ValueFormatter?implements?IAxisValueFormatter,?IValueFormatter?{/** ?*?<b>DO?NOT?USE</b>,?only?for?backwards?compatibility?and?will?be?removed?in?future?versions. ?* ?*?@param?value?the?value?to?be?formatted ?*?@param?axis??the?axis?the?value?belongs?to ?*?@return?formatted?string?label ?*/@Override@Deprecatedpublic?String?getFormattedValue(float?value,?AxisBase?axis)?{????return?getFormattedValue(value); }/** ?*?<b>DO?NOT?USE</b>,?only?for?backwards?compatibility?and?will?be?removed?in?future?versions. ?*?@param?value???????????the?value?to?be?formatted ?*?@param?entry???????????the?entry?the?value?belongs?to?-?in?e.g.?BarChart,?this?is?of?class?BarEntry ?*?@param?dataSetIndex????the?index?of?the?DataSet?the?entry?in?focus?belongs?to ?*?@param?viewPortHandler?provides?information?about?the?current?chart?state?(scale,?translation,?...) ?*?@return?formatted?string?label ?*/@Override@Deprecatedpublic?String?getFormattedValue(float?value,?Entry?entry,?int?dataSetIndex,?ViewPortHandler?viewPortHandler)?{????return?getFormattedValue(value); }/** ?*?Called?when?drawing?any?label,?used?to?change?numbers?into?formatted?strings. ?* ?*?@param?value?float?to?be?formatted ?*?@return?formatted?string?label ?*/public?String?getFormattedValue(float?value)?{????return?String.valueOf(value); }/** ?*?Used?to?draw?axis?labels,?calls?{@link?#getFormattedValue(float)}?by?default. ?* ?*?@param?value?float?to?be?formatted ?*?@param?axis??axis?being?labeled ?*?@return?formatted?string?label ?*/public?String?getAxisLabel(float?value,?AxisBase?axis)?{????return?getFormattedValue(value); }/** ?*?Used?to?draw?bar?labels,?calls?{@link?#getFormattedValue(float)}?by?default. ?* ?*?@param?barEntry?bar?being?labeled ?*?@return?formatted?string?label ?*/public?String?getBarLabel(BarEntry?barEntry)?{????return?getFormattedValue(barEntry.getY()); }/** ?*?Used?to?draw?stacked?bar?labels,?calls?{@link?#getFormattedValue(float)}?by?default. ?* ?*?@param?value????????current?value?to?be?formatted ?*?@param?stackedEntry?stacked?entry?being?labeled,?contains?all?Y?values ?*?@return?formatted?string?label ?*/public?String?getBarStackedLabel(float?value,?BarEntry?stackedEntry)?{????return?getFormattedValue(value); }/** ?*?Used?to?draw?line?and?scatter?labels,?calls?{@link?#getFormattedValue(float)}?by?default. ?* ?*?@param?entry?point?being?labeled,?contains?X?value ?*?@return?formatted?string?label ?*/public?String?getPointLabel(Entry?entry)?{????return?getFormattedValue(entry.getY()); }/** ?*?Used?to?draw?pie?value?labels,?calls?{@link?#getFormattedValue(float)}?by?default. ?* ?*?@param?value????float?to?be?formatted,?may?have?been?converted?to?percentage ?*?@param?pieEntry?slice?being?labeled,?contains?original,?non-percentage?Y?value ?*?@return?formatted?string?label ?*/public?String?getPieLabel(float?value,?PieEntry?pieEntry)?{????return?getFormattedValue(value); }/** ?*?Used?to?draw?radar?value?labels,?calls?{@link?#getFormattedValue(float)}?by?default. ?* ?*?@param?radarEntry?entry?being?labeled ?*?@return?formatted?string?label ?*/public?String?getRadarLabel(RadarEntry?radarEntry)?{????return?getFormattedValue(radarEntry.getY()); }/** ?*?Used?to?draw?bubble?size?labels,?calls?{@link?#getFormattedValue(float)}?by?default. ?* ?*?@param?bubbleEntry?bubble?being?labeled,?also?contains?X?and?Y?values ?*?@return?formatted?string?label ?*/public?String?getBubbleLabel(BubbleEntry?bubbleEntry)?{????return?getFormattedValue(bubbleEntry.getSize()); }/** ?*?Used?to?draw?high?labels,?calls?{@link?#getFormattedValue(float)}?by?default. ?* ?*?@param?candleEntry?candlestick?being?labeled ?*?@return?formatted?string?label ?*/public?String?getCandleLabel(CandleEntry?candleEntry)?{????return?getFormattedValue(candleEntry.getHigh()); } }
????public?class?MyValueFormatter?extends?ValueFormatter{private?final?DecimalFormat?mFormat;private?String?suffix;public?MyValueFormatter(String?suffix)?{ ????mFormat?=?new?DecimalFormat("0000");????this.suffix?=?suffix; }@Overridepublic?String?getFormattedValue(float?value)?{????return?mFormat.format(value)?+?suffix; }@Overridepublic?String?getAxisLabel(float?value,?AxisBase?axis)?{????if?(axis?instanceof?XAxis)?{????????return?mFormat.format(value); ????}?else?if?(value?>?0)?{????????return?mFormat.format(value)?+?suffix; ????}?else?{????????return?mFormat.format(value); ????} } } 復(fù)制代碼
??package?detongs.hbqianze.him.linechart;??import?android.os.Bundle;??import?android.util.Log;??import?android.view.WindowManager;??import?android.widget.TextView;??import?androidx.appcompat.app.AppCompatActivity;??import?com.github.mikephil.charting.charts.BarChart;??import?com.github.mikephil.charting.components.XAxis;??import?com.github.mikephil.charting.components.YAxis;??import?com.github.mikephil.charting.data.BarData;??import?com.github.mikephil.charting.data.BarDataSet;??import?com.github.mikephil.charting.data.BarEntry;??import?com.github.mikephil.charting.interfaces.datasets.IBarDataSet;??import??com.github.mikephil.charting.interfaces.datasets.IDataSet;??import?com.github.mikephil.charting.utils.ColorTemplate;??import?java.util.ArrayList;??import?detongs.hbqianze.him.linechart.chart.MyValueFormatter;??import?detongs.hbqianze.him.linechart.chart.ValueFormatter;??public?class?MainActivity?extends?AppCompatActivity?{private?BarChart?chart;private?TextView?te_cache;@Overrideprotected?void?onCreate(Bundle?savedInstanceState)?{????super.onCreate(savedInstanceState); ????getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, ????????????WindowManager.LayoutParams.FLAG_FULLSCREEN); ????setContentView(R.layout.activity_main); ????chart?=?findViewById(R.id.chart1); ????te_cache?=?findViewById(R.id.te_cache); ????chart.getDescription().setEnabled(false);????//設(shè)置最大值條目,超出之后不會(huì)有值 ????chart.setMaxVisibleValueCount(60);????//分別在x軸和y軸上進(jìn)行縮放 ????chart.setPinchZoom(true);????//設(shè)置剩余統(tǒng)計(jì)圖的陰影 ????chart.setDrawBarShadow(false);????//設(shè)置網(wǎng)格布局 ????chart.setDrawGridBackground(true);????//通過(guò)自定義一個(gè)x軸標(biāo)簽來(lái)實(shí)現(xiàn)2,015?有分割符符bug ????ValueFormatter?custom?=?new?MyValueFormatter("?");????//獲取x軸線 ????XAxis?xAxis?=?chart.getXAxis();????//設(shè)置x軸的顯示位置 ????xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);????//設(shè)置網(wǎng)格布局 ????xAxis.setDrawGridLines(true);????//圖表將避免第一個(gè)和最后一個(gè)標(biāo)簽條目被減掉在圖表或屏幕的邊緣 ????xAxis.setAvoidFirstLastClipping(false);????//繪制標(biāo)簽??指x軸上的對(duì)應(yīng)數(shù)值?默認(rèn)true ????xAxis.setDrawLabels(true); ????xAxis.setValueFormatter(custom);????//縮放后x?軸數(shù)據(jù)重疊問題 ????xAxis.setGranularityEnabled(true);????//獲取右邊y標(biāo)簽 ????YAxis?axisRight?=?chart.getAxisRight(); ????axisRight.setStartAtZero(true);????//獲取左邊y軸的標(biāo)簽 ????YAxis?axisLeft?=?chart.getAxisLeft();????//設(shè)置Y軸數(shù)值?從零開始 ????axisLeft.setStartAtZero(true); ????chart.getAxisLeft().setDrawGridLines(false);????//設(shè)置動(dòng)畫時(shí)間 ?????chart.animateXY(600,600); ?????chart.getLegend().setEnabled(true); ????getData();????//設(shè)置柱形統(tǒng)計(jì)圖上的值 ????chart.getData().setValueTextSize(10);????for?(IDataSet?set?:?chart.getData().getDataSets()){ ????????set.setDrawValues(!set.isDrawValuesEnabled()); ????} }public?void?getData(){ ArrayList<BarEntry>?values?=?new?ArrayList<>(); ????Float?aFloat?=?Float.valueOf("2015"); ????Log.v("xue","aFloat+++++"+aFloat); ????BarEntry?barEntry?=?new?BarEntry(aFloat,Float.valueOf("100")); BarEntry?barEntry1?=?new?BarEntry(Float.valueOf("2016"),Float.valueOf("210")); BarEntry?barEntry2?=?new?BarEntry(Float.valueOf("2017"),Float.valueOf("300")); BarEntry?barEntry3?=?new?BarEntry(Float.valueOf("2018"),Float.valueOf("450")); BarEntry?barEntry4?=?new?BarEntry(Float.valueOf("2019"),Float.valueOf("300")); ????BarEntry?barEntry5?=?new?BarEntry(Float.valueOf("2020"),Float.valueOf("650")); ????BarEntry?barEntry6?=?new?BarEntry(Float.valueOf("2021"),Float.valueOf("740")); values.add(barEntry); values.add(barEntry1); values.add(barEntry2); values.add(barEntry3); values.add(barEntry4); values.add(barEntry5); ????values.add(barEntry6); BarDataSet?set1;if?(chart.getData()?!=?null?&& ????????chart.getData().getDataSetCount()?>?0)?{ ????set1?=?(BarDataSet)?chart.getData().getDataSetByIndex(0); ????set1.setValues(values); ????chart.getData().notifyDataChanged(); ????chart.notifyDataSetChanged(); }?else?{ ????set1?=?new?BarDataSet(values,?"點(diǎn)折水"); ????set1.setColors(ColorTemplate.VORDIPLOM_COLORS); ????set1.setDrawValues(false); ????ArrayList<IBarDataSet>?dataSets?=?new?ArrayList<>(); ????dataSets.add(set1); ????BarData?data?=?new?BarData(dataSets); ????chart.setData(data); ????chart.setFitBars(true); }????//繪制圖表chart.invalidate(); } }
文章寫到這里就結(jié)束了,如果你覺得文章寫得不錯(cuò)就給個(gè)關(guān)注唄?你的支持是我最大的動(dòng)力!如果還有想了解MPAndroidChart其他屬性的小伙伴也可以給我留言。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。