MPAndroidChart使用(BarChart为例)

1.准备工作

1.1.引用到项目中

在你的项目的build.gradle文件中配置:
 
   
  1. allprojects {
  2. repositories {
  3. jcenter()
  4. maven { url "https://jitpack.io" }
  5. }
  6. }
在app的build.gradle文件中配置:
 
    
  1. compile 'com.github.PhilJay:MPAndroidChart:v3.0.1'

1.2.jithub地址

       https://github.com/PhilJay/MPAndroidChart    

2.使用

2.1.布局文件

 
      
  1. <com.github.mikephil.charting.charts.BarChart
  2. android:id="@+id/bar_chart"
  3. android:layout_width="match_parent"
  4. android:layout_height="240dp">

2.2.使用举例

在依赖的activity或者fragment中找到控件,设置相关属性,做相应的处理
MPAndroidChart中提供了很多的方法,可以帮助我们轻松的搞定我们的项目,个人认为最好的掌握该控件的方法是去jithub下载作者的sample,
通过在自己的手机上运行,切实找到方法,到源码中理解运用。
在这里我仅仅对一些常用的方法解析:
 
       
  1. {
  2. ……
  3. mBarChart = (BarChart) findViewById(R.id.bar_chart);
  4. //设置背景颜色
  5. mBarChart.setBackgroundColor(getResources().getColor(R.color.colorAccent));
  6. //BarChart的点击事件
  7. mBarChart.setOnClickListener(new View.OnClickListener() {
  8. @Override public void onClick(View view) {
  9. }
  10. });
  11. //设置数值选择的监听
  12. mBarChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
  13. @Override public void onValueSelected(Entry e, Highlight h) {
  14. }
  15. @Override public void onNothingSelected() {
  16. }
  17. });
  18. //设置高亮显示
  19. mBarChart.setHighlightFullBarEnabled(true);
  20. mBarChart.setDrawValueAboveBar(true);
  21. //设置支持触控
  22. mBarChart.setTouchEnabled(true);
  23. //设置是否支持拖拽
  24. mBarChart.setDragEnabled(true);
  25. //设置能否缩放
  26. mBarChart.setScaleEnabled(true);
  27. //设置true支持两个指头向X、Y轴的缩放,如果为false,只能支持X或者Y轴的当方向缩放
  28. mBarChart.setPinchZoom(true);
  29. //获取图表右下角的描述性文字,setEnable()默认为true
  30. mBarChart.getDescription().setEnabled(true);
  31. Description description=new Description();
  32. description.setText("description");
  33. //设置右下角的描述文字
  34. mBarChart.setDescription(description);
  35. //设置阴影
  36. mBarChart.setDrawBarShadow(false);
  37. //设置所有的数值在图形的上面,而不是图形上
  38. mBarChart.setDrawValueAboveBar(true);
  39. //设置最大的能够在图表上显示数字的图数
  40. mBarChart.setMaxVisibleValueCount(60);
  41. //设置背景是否网格显示
  42. mBarChart.setDrawGridBackground(false);
  43. //X轴的数据格式
  44. IAxisValueFormatter xAxisFormatter = new DayAxisValueFormatter(mChart);
  45. //得到X轴,设置X轴的样式
  46. XAxis xAxis = mChart.getXAxis();
  47. //设置位置
  48. xAxis.setPosition(XAxisPosition.BOTTOM);
  49. //设置特定的标签类型
  50. xAxis.setTypeface(mTfLight);
  51. //设置是否绘制网格线
  52. xAxis.setDrawGridLines(false);
  53. //设置最小的区间,避免标签的迅速增多
  54. xAxis.setGranularity(1f); // only intervals of 1 day
  55. //设置进入时的标签数量
  56. xAxis.setLabelCount(7);
  57. //设置数据格式
  58. xAxis.setValueFormatter(xAxisFormatter);
  59. IAxisValueFormatter custom = new MyAxisValueFormatter();
  60. YAxis leftAxis = mChart.getAxisLeft();
  61. leftAxis.setTypeface(mTfLight);
  62. leftAxis.setLabelCount(8, false);
  63. leftAxis.setValueFormatter(custom);
  64. leftAxis.setPosition(YAxisLabelPosition.OUTSIDE_CHART);
  65. //Sets the top axis space in percent of the full range. Default 10f
  66. leftAxis.setSpaceTop(15f);
  67. //设置Y轴最小的值
  68. leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
  69. YAxis rightAxis = mChart.getAxisRight();
  70. rightAxis.setDrawGridLines(false);
  71. rightAxis.setTypeface(mTfLight);
  72. rightAxis.setLabelCount(8, false);
  73. rightAxis.setValueFormatter(custom);
  74. rightAxis.setSpaceTop(15f);
  75. rightAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
  76. //设置图例样式,默认可以显示,设置setEnabled(false);可以不绘制
  77. Legend l = mChart.getLegend();
  78. l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
  79. l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
  80. l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
  81. l.setDrawInside(false);
  82. l.setForm(LegendForm.SQUARE);
  83. l.setFormSize(9f);
  84. l.setTextSize(11f);
  85. l.setXEntrySpace(4f);
  86. //设置X轴和Y轴显示的刻度
  87. setData(12, 50);
  88. }
  89. private void setData(int count, float range) {
  90. float start = 1f;
  91. ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
  92. for (int i = (int) start; i < start + count + 1; i++) {
  93. float mult = (range + 1);
  94. float val = (float) (Math.random() * mult);
  95. yVals1.add(new BarEntry(i, val));
  96. }
  97. BarDataSet set1;
  98. if (mChart.getData() != null &&
  99. mChart.getData().getDataSetCount() > 0) {
  100. set1 = (BarDataSet) mChart.getData().getDataSetByIndex(0);
  101. set1.setValues(yVals1);
  102. mChart.getData().notifyDataChanged();
  103. mChart.notifyDataSetChanged();
  104. } else {
  105. set1 = new BarDataSet(yVals1, "The year 2017");
  106. set1.setColors(ColorTemplate.MATERIAL_COLORS);
  107. ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
  108. dataSets.add(set1);
  109. BarData data = new BarData(dataSets);
  110. data.setValueTextSize(10f);
  111. data.setValueTypeface(mTfLight);
  112. data.setBarWidth(0.9f);
  113. mChart.setData(data);
  114. }
  115. }
代码很多,但是你仔细阅读会发现这些代码比较有规律:
(1)找到控件,设置属性
(2)设置X轴样式(你也可以设置一些图表进入动画)
(3)设置Y轴样式 (你也可以设置一些图表进入动画)
(4)设置数据

猜你喜欢

转载自blog.csdn.net/PanADE/article/details/53541017