Android筆記:MPAndroidChart使用

      網友投稿 1058 2022-05-30

      Gradle

      Project level build.gradle(在project的build.gradle中添加依賴)

      1

      allprojects { repositories { maven { url 'https://jitpack.io' } } }

      1

      2

      3

      4

      5

      App level build.gradle(在app的build.gradle中添加依賴)

      1

      dependencies { implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3' }

      1

      2

      3

      二、layout布局,我這里實現了三個圖形,可以根據自己的需要,添加圖形控件

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      40

      41

      42

      43

      Android筆記:MPAndroidChart使用

      44

      45

      46

      47

      48

      49

      50

      三、實現代碼

      圖形橫縱坐標默認為float形式,如果想展示文字形式,需要自定義適配器。自定義適配器會在“四”中列出

      public class FifteenActivity extends AppCompatActivity implements OnChartValueSelectedListener { private String[] mMonths = new String[]{ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"}; private String[] mParties = new String[]{ "Party A", "Party B", "Party C", "Party D", "Party E", "Party F", "Party G", "Party H", "Party I", "Party J", "Party K", "Party L", "Party M", "Party N", "Party O", "Party P", "Party Q", "Party R", "Party S", "Party T", "Party U", "Party V", "Party W", "Party X", "Party Y", "Party Z" }; private Typeface mTfRegular; private Typeface mTfLight; protected BarChart mChart; private HorizontalBarChart hBarChart; private LineChart lineChart; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fifteen_layout); mTfRegular = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf"); mTfLight = Typeface.createFromAsset(getAssets(), "OpenSans-Light.ttf"); mChart = findViewById(R.id.chart1); hBarChart = findViewById(R.id.hBarChart); lineChart = findViewById(R.id.lineChart); initBarChart(); initHBarChart(); initLineChart(); } /** * 初始化柱形圖控件屬性 */ private void initBarChart() { mChart.setOnChartValueSelectedListener(this); mChart.setDrawBarShadow(false); mChart.setDrawValueAboveBar(true); mChart.getDescription().setEnabled(false); // if more than 60 entries are displayed in the chart, no values will be // drawn mChart.setMaxVisibleValueCount(60); // scaling can now only be done on x- and y-axis separately mChart.setPinchZoom(false); mChart.setDrawGridBackground(false); // IAxisValueFormatter xAxisFormatter = new DayAxisValueFormatter(mChart); //自定義坐標軸適配器,配置在X軸,xAxis.setValueFormatter(xAxisFormatter); IAxisValueFormatter xAxisFormatter = new XAxisValueFormatter(); XAxis xAxis = mChart.getXAxis(); xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); xAxis.setTypeface(mTfLight);//可以去掉,沒什么用 xAxis.setDrawAxisLine(false); xAxis.setGranularity(1f); xAxis.setValueFormatter(xAxisFormatter); //自定義坐標軸適配器,配置在Y軸。leftAxis.setValueFormatter(custom); IAxisValueFormatter custom = new MyAxisValueFormatter(); //設置限制臨界線 LimitLine limitLine = new LimitLine(3f, "臨界點"); limitLine.setLineColor(Color.GREEN); limitLine.setLineWidth(1f); limitLine.setTextColor(Color.GREEN); //獲取到圖形左邊的Y軸 YAxis leftAxis = mChart.getAxisLeft(); leftAxis.addLimitLine(limitLine); leftAxis.setTypeface(mTfLight);//可以去掉,沒什么用 leftAxis.setLabelCount(8, false); leftAxis.setValueFormatter(custom); leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART); leftAxis.setSpaceTop(15f); leftAxis.setAxisMinimum(0f); //獲取到圖形右邊的Y軸,并設置為不顯示 mChart.getAxisRight().setEnabled(false); //圖例設置 Legend legend = mChart.getLegend(); legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM); legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT); legend.setOrientation(Legend.LegendOrientation.HORIZONTAL); legend.setDrawInside(false); legend.setForm(Legend.LegendForm.SQUARE); legend.setFormSize(9f); legend.setTextSize(11f); legend.setXEntrySpace(4f); //如果點擊柱形圖,會彈出pop提示框.XYMarkerView為自定義彈出框 XYMarkerView mv = new XYMarkerView(this, xAxisFormatter); mv.setChartView(mChart); mChart.setMarker(mv); setBarChartData(); } /** * 初始化水平柱形圖圖控件屬性 */ private void initHBarChart() { hBarChart.setOnChartValueSelectedListener(this); hBarChart.setDrawBarShadow(false); hBarChart.setDrawValueAboveBar(true); hBarChart.getDescription().setEnabled(false); // if more than 60 entries are displayed in the chart, no values will be // drawn hBarChart.setMaxVisibleValueCount(60); // scaling can now only be done on x- and y-axis separately hBarChart.setPinchZoom(false); // draw shadows for each bar that show the maximum value // mChart.setDrawBarShadow(true); hBarChart.setDrawGridBackground(false); //自定義坐標軸適配器,設置在X軸 DecimalFormatter formatter = new DecimalFormatter(); XAxis xl = hBarChart.getXAxis(); xl.setPosition(XAxis.XAxisPosition.BOTTOM); xl.setTypeface(mTfLight); xl.setLabelRotationAngle(-45f); xl.setDrawAxisLine(true); xl.setDrawGridLines(false); xl.setGranularity(1f); // xl.setAxisMinimum(0); xl.setValueFormatter(formatter); //對Y軸進行設置 YAxis yl = hBarChart.getAxisLeft(); yl.setTypeface(mTfLight); yl.setDrawAxisLine(true); yl.setDrawGridLines(true); yl.setAxisMinimum(0f); // this replaces setStartAtZero(true) // yl.setInverted(true); hBarChart.getAxisRight().setEnabled(false); //圖例設置 Legend l = hBarChart.getLegend(); l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM); l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT); l.setOrientation(Legend.LegendOrientation.HORIZONTAL); l.setDrawInside(false); l.setFormSize(8f); l.setXEntrySpace(4f); setHBarChartData(); hBarChart.setFitBars(true); hBarChart.animateY(2500); } /** * 初始化折線圖控件屬性 */ private void initLineChart() { lineChart.setOnChartValueSelectedListener(this); lineChart.getDescription().setEnabled(false); lineChart.setBackgroundColor(Color.WHITE); //自定義適配器,適配于X軸 IAxisValueFormatter xAxisFormatter = new XAxisValueFormatter(); XAxis xAxis = lineChart.getXAxis(); xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); xAxis.setTypeface(mTfLight); xAxis.setGranularity(1f); xAxis.setValueFormatter(xAxisFormatter); //自定義適配器,適配于Y軸 IAxisValueFormatter custom = new MyAxisValueFormatter(); YAxis leftAxis = lineChart.getAxisLeft(); leftAxis.setTypeface(mTfLight); leftAxis.setLabelCount(8, false); leftAxis.setValueFormatter(custom); leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART); leftAxis.setSpaceTop(15f); leftAxis.setAxisMinimum(0f); lineChart.getAxisRight().setEnabled(false); setLineChartData(); } private float getRandom(float range, float startsfrom) { return (float) (Math.random() * range) + startsfrom; } @Override public void onValueSelected(Entry e, Highlight h) { } @Override public void onNothingSelected() { } private void setBarChartData() { ArrayList yVals1 = new ArrayList(); //在這里設置自己的數據源,BarEntry 只接收float的參數, //圖形橫縱坐標默認為float形式,如果想展示文字形式,需要自定義適配器, yVals1.add(new BarEntry(0, 4)); yVals1.add(new BarEntry(1, 2)); yVals1.add(new BarEntry(2, 6)); yVals1.add(new BarEntry(3, 1)); BarDataSet set1; if (mChart.getData() != null && mChart.getData().getDataSetCount() > 0) { set1 = (BarDataSet) mChart.getData().getDataSetByIndex(0); set1.setValues(yVals1); mChart.getData().notifyDataChanged(); mChart.notifyDataSetChanged(); } else { set1 = new BarDataSet(yVals1, "The year 2017"); set1.setDrawIcons(false); ArrayList dataSets = new ArrayList(); dataSets.add(set1); BarData data = new BarData(dataSets); data.setValueTextSize(10f); data.setValueTypeface(mTfLight);//可以去掉,沒什么用 data.setBarWidth(0.9f); mChart.setData(data); } } /** * 設置水平柱形圖數據的方法 */ private void setHBarChartData() { //填充數據,在這里換成自己的數據源 ArrayList yVals1 = new ArrayList(); yVals1.add(new BarEntry(0, 4)); yVals1.add(new BarEntry(1, 2)); yVals1.add(new BarEntry(2, 6)); yVals1.add(new BarEntry(3, 1)); BarDataSet set1; if (hBarChart.getData() != null && hBarChart.getData().getDataSetCount() > 0) { set1 = (BarDataSet) hBarChart.getData().getDataSetByIndex(0); set1.setValues(yVals1); hBarChart.getData().notifyDataChanged(); hBarChart.notifyDataSetChanged(); } else { set1 = new BarDataSet(yVals1, "DataSet 1"); set1.setDrawIcons(false); ArrayList dataSets = new ArrayList(); dataSets.add(set1); BarData data = new BarData(dataSets); data.setValueTextSize(10f); data.setValueTypeface(mTfLight);//可以去掉,沒什么用 data.setBarWidth(0.5f); hBarChart.setData(data); } } /** * 設置折線圖的數據 */ private void setLineChartData() { //填充數據,在這里換成自己的數據源 List valsComp1 = new ArrayList<>(); List valsComp2 = new ArrayList<>(); valsComp1.add(new Entry(0, 2)); valsComp1.add(new Entry(1, 4)); valsComp1.add(new Entry(2, 0)); valsComp1.add(new Entry(3, 2)); valsComp2.add(new Entry(0, 2)); valsComp2.add(new Entry(1, 0)); valsComp2.add(new Entry(2, 4)); valsComp2.add(new Entry(3, 2)); //這里,每重新new一個LineDataSet,相當于重新畫一組折線 //每一個LineDataSet相當于一組折線。比如:這里有兩個LineDataSet:setComp1,setComp2。 //則在圖像上會有兩條折線圖,分別表示公司1 和 公司2 的情況.還可以設置更多 LineDataSet setComp1 = new LineDataSet(valsComp1, "Company 1 "); setComp1.setAxisDependency(YAxis.AxisDependency.LEFT); setComp1.setColor(getResources().getColor(R.color.light_blue)); setComp1.setDrawCircles(false); setComp1.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER); LineDataSet setComp2 = new LineDataSet(valsComp2, "Company 2 "); setComp2.setAxisDependency(YAxis.AxisDependency.LEFT); setComp2.setDrawCircles(true); setComp2.setColor(getResources().getColor(R.color.red)); setComp2.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER); List dataSets = new ArrayList<>(); dataSets.add(setComp1); dataSets.add(setComp2); LineData lineData = new LineData(dataSets); lineChart.setData(lineData); lineChart.invalidate(); } public static void startActivity(Context context) { Intent intent = new Intent(); intent.setClass(context, FifteenActivity.class); context.startActivity(intent); } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      40

      41

      42

      43

      44

      45

      46

      47

      48

      49

      50

      51

      52

      53

      54

      55

      56

      57

      58

      59

      60

      61

      62

      63

      64

      65

      66

      67

      68

      69

      70

      71

      72

      73

      74

      75

      76

      77

      78

      79

      80

      81

      82

      83

      84

      85

      86

      87

      88

      89

      90

      91

      92

      93

      94

      95

      96

      97

      98

      99

      100

      101

      102

      103

      104

      105

      106

      107

      108

      109

      110

      111

      112

      113

      114

      115

      116

      117

      118

      119

      120

      121

      122

      123

      124

      125

      126

      127

      128

      129

      130

      131

      132

      133

      134

      135

      136

      137

      138

      139

      140

      141

      142

      143

      144

      145

      146

      147

      148

      149

      150

      151

      152

      153

      154

      155

      156

      157

      158

      159

      160

      161

      162

      163

      164

      165

      166

      167

      168

      169

      170

      171

      172

      173

      174

      175

      176

      177

      178

      179

      180

      181

      182

      183

      184

      185

      186

      187

      188

      189

      190

      191

      192

      193

      194

      195

      196

      197

      198

      199

      200

      201

      202

      203

      204

      205

      206

      207

      208

      209

      210

      211

      212

      213

      214

      215

      216

      217

      218

      219

      220

      221

      222

      223

      224

      225

      226

      227

      228

      229

      230

      231

      232

      233

      234

      235

      236

      237

      238

      239

      240

      241

      242

      243

      244

      245

      246

      247

      248

      249

      250

      251

      252

      253

      254

      255

      256

      257

      258

      259

      260

      261

      262

      263

      264

      265

      266

      267

      268

      269

      270

      271

      272

      273

      274

      275

      276

      277

      278

      279

      280

      281

      282

      283

      284

      285

      286

      287

      288

      289

      290

      291

      292

      293

      294

      295

      296

      297

      298

      299

      300

      301

      302

      303

      304

      305

      306

      307

      308

      309

      310

      311

      312

      313

      314

      315

      316

      317

      318

      319

      320

      321

      322

      323

      324

      325

      326

      327

      四、自定義適配器類

      XAxisValueFormatter:

      public class XAxisValueFormatter implements IAxisValueFormatter { private String[] xStrs = new String[]{"春", "夏", "秋", "冬"}; @Override public String getFormattedValue(float value, AxisBase axis) { int position = (int) value; if (position >= 4) { position = 0; } return xStrs[position]; }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      MyAxisValueFormatter:

      public class MyAxisValueFormatter implements IAxisValueFormatter { private DecimalFormat mFormat; public MyAxisValueFormatter() { mFormat = new DecimalFormat("###,###,###,##0.000"); } @Override public String getFormattedValue(float value, AxisBase axis) { return mFormat.format(value) + " $"; } } DecimalFormatter: public class DecimalFormatter implements IAxisValueFormatter { private DecimalFormat format; public DecimalFormatter() { format = new DecimalFormat("###,###,##0.00"); } @Override public String getFormattedValue(float value, AxisBase axis) { return format.format(value) + "$"; } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      五、自定義MarkerView

      public class XYMarkerView extends MarkerView { private TextView tvContent; private IAxisValueFormatter xAxisValueFormatter; private DecimalFormat format; public XYMarkerView(Context context, IAxisValueFormatter xAxisValueFormatter) { super(context, R.layout.custom_marker_view); this.xAxisValueFormatter = xAxisValueFormatter; tvContent = findViewById(R.id.tvContent); format = new DecimalFormat("###.000"); } @Override public void refreshContent(Entry e, Highlight highlight) { tvContent.setText("x:" + xAxisValueFormatter.getFormattedValue(e.getX(), null) + ",y:" + format.format(e.getY())); super.refreshContent(e, highlight); } @Override public MPPointF getOffset() { return new MPPointF(-(getWidth() / 2), -getHeight()); } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      custom_marker_view:

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      六、這里只實現了些基本功能,更多的API使用,請查看官方文檔

      官方文檔:https://github.com/PhilJay/MPAndroidChart

      MPAndroidChart中文文檔-:https://download.csdn.net/download/android157/11188758

      博客:https://blog.csdn.net/koma025/article/details/53886832

      設置多組y值:https://blog.csdn.net/u011125199/article/details/52797439

      修改源代碼:https://blog.csdn.net/dt235201314/article/details/70142117

      折線圖設置文檔:https://zhuanlan.zhihu.com/p/25672390

      Android

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:0基礎如何轉行自學軟件測試
      下一篇:沃通參展第六屆世界互聯網大會,大力推動國密證書應用產業發展
      相關文章
      亚洲男人在线无码视频| 亚洲综合伊人久久大杳蕉| 亚洲AV色香蕉一区二区| 国产AV无码专区亚洲精品| 亚洲伊人色欲综合网| 亚洲综合亚洲综合网成人| 亚洲色婷婷综合开心网| 亚洲午夜无码AV毛片久久| 中文字幕亚洲激情| 亚洲综合色婷婷七月丁香| 在线观看亚洲精品福利片| 亚洲自偷自偷在线制服| 亚洲精品成人无限看| 亚洲va久久久噜噜噜久久| 亚洲av日韩av不卡在线观看 | 久久久久亚洲av无码尤物| 亚洲av无码乱码国产精品| 亚洲AV无一区二区三区久久| 亚洲v高清理论电影| 久久精品国产亚洲77777| 亚洲视频免费观看| 亚洲国产精品张柏芝在线观看| 亚洲乱码卡一卡二卡三| 亚洲一区二区三区成人网站 | 亚洲va在线va天堂va不卡下载| 亚洲欧洲国产日韩精品| 亚洲经典在线观看| 亚洲日本乱码卡2卡3卡新区| 亚洲国产成人久久精品软件| 国产精品亚洲一区二区三区在线观看| 日韩色日韩视频亚洲网站| 亚洲最大av无码网址| 亚洲高清专区日韩精品| 亚洲综合激情视频| 最新国产精品亚洲| 亚洲精品无码久久不卡| 亚洲成av人片天堂网| 亚洲国产情侣一区二区三区| 亚洲人av高清无码| 亚洲一区视频在线播放| 久久精品视频亚洲|