标签: android库博客
2016-11-17 13:45 803人阅读 收藏 举报
Android图表库MPAndroidChart(四)——条形图的绘制过程过程,隐隐约约我看到了套路
在学习本课程之前我建议先把我之前的博客看完,这样对整体的流程有一个大致的了解
Android图表库MPAndroidChart(一)——了解他的本质,方能得心应手
Android图表库MPAndroidChart(二)——线形图的方方面面,看完你会回来感谢我的
Android图表库MPAndroidChart(三)——双重轴线形图的实现,这次就so easy了
Android图表库MPAndroidChart(四)——条形图的绘制过程过程,隐隐约约我看到了套路
Android图表库MPAndroidChart(五)——自定义MarkerView实现选中高亮
好的,那我们直接进入正题,我们今天要学习的是又一个比较常用的图形,那就是条形图,我们之前纸条介绍的,应该算是折线图的一种,我们来看下今天要实现的效果
一.基本实现
这就是最终要实现的效果,我们依葫芦画瓢,来实现以下,首先,我们先写下他的layout
<com.github.mikephil.charting.charts.BarChart android:id="@+id/mBarChart" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/>
他的名字叫做BarChart,我们还是按照一贯的套路,先去给他初始化
mBarChart = (BarChart) findViewById(R.id.mBarChart); mBarChart.setOnChartValueSelectedListener(this); mBarChart.setDrawBarShadow(false); mBarChart.setDrawValueAboveBar(true); mBarChart.getDescription().setEnabled(false); mBarChart.setMaxVisibleValueCount(60); mBarChart.setPinchZoom(false); mBarChart.setDrawGridBackground(false);
按照惯例,给他一些手势,点击之类的监听,然后绘制我们的x和y轴,这里我们可以绘制一条也可以绘制两条,因为定制型还是比较强的,所有我们这里绘制两条
XAxis xAxis = mBarChart.getXAxis(); xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); xAxis.setDrawGridLines(false); xAxis.setGranularity(1f); xAxis.setLabelCount(7); xAxis.setAxisMaximum(50f); xAxis.setAxisMinimum(0f); xAxis.setValueFormatter(xAxisFormatter); YAxis leftAxis = mBarChart.getAxisLeft(); leftAxis.setLabelCount(8, false); leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART); leftAxis.setSpaceTop(15f); leftAxis.setAxisMinimum(0f); leftAxis.setAxisMaximum(50f); YAxis rightAxis = mBarChart.getAxisRight(); rightAxis.setDrawGridLines(false); rightAxis.setLabelCount(8, false); rightAxis.setSpaceTop(15f); rightAxis.setAxisMinimum(0f); rightAxis.setAxisMaximum(50f);
- 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
- 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
然后依次绘制他的线条以及模拟一些数据
Legend l = mBarChart.getLegend(); l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM); l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT); l.setOrientation(Legend.LegendOrientation.HORIZONTAL); l.setDrawInside(false); l.setForm(Legend.LegendForm.SQUARE); l.setFormSize(9f); l.setTextSize(11f); l.setXEntrySpace(4f); ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>(); yVals1.add(new BarEntry(0, 10)); yVals1.add(new BarEntry(5, 20)); yVals1.add(new BarEntry(16, 30)); yVals1.add(new BarEntry(18, 40)); yVals1.add(new BarEntry(20, 50)); yVals1.add(new BarEntry(22, 10)); yVals1.add(new BarEntry(25, 20)); yVals1.add(new BarEntry(28, 30)); yVals1.add(new BarEntry(30, 40)); setData(yVals1);
- 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
- 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
现在再来看下怎么设置数据的
private void setData(ArrayList yVals1) { float start = 1f; BarDataSet set1; if (mBarChart.getData() != null && mBarChart.getData().getDataSetCount() > 0) { set1 = (BarDataSet) mBarChart.getData().getDataSetByIndex(0); set1.setValues(yVals1); mBarChart.getData().notifyDataChanged(); mBarChart.notifyDataSetChanged(); } else { set1 = new BarDataSet(yVals1, "2017年工资涨幅"); set1.setColors(ColorTemplate.MATERIAL_COLORS); ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>(); dataSets.add(set1); BarData data = new BarData(dataSets); data.setValueTextSize(10f); data.setBarWidth(0.9f); mBarChart.setData(data); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
和之前的图形一样,我们把数据集放进我们的图标里就行,可以看到,我这里描述的很轻描淡写,因为我们画图表都是一样的套路,而且你最后总结出来的api也是一样的,只是每次变换的图形不一样而已
二.显示顶点值
三.x轴动画
四.y轴动画
五.xy轴动画
六.显示边框
这就是基本的使用方法了,如果你不需要什么属性,你就删掉,如果你需要什么属性,你就增加,就是这样,非常的简单好用,现在把基本的代码奉上
activity_barchar.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.github.mikephil.charting.charts.BarChart android:id="@+id/mBarChart" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn_show_values" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="顶点显示值"/> <Button android:id="@+id/btn_anim_x" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="X轴动画"/> <Button android:id="@+id/btn_anim_y" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Y轴动画"/> <Button android:id="@+id/btn_anim_xy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="XY轴动画"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn_save_pic" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="保存到相册"/> <Button android:id="@+id/btn_auto_mix_max" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自动最大最小值"/> <Button android:id="@+id/btn_actionToggleHighlight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="高亮显示"/> <Button android:id="@+id/btn_actionToggleBarBorders" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示边框"/> </LinearLayout></LinearLayout>
- 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
- 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
BarChartActivity
public class BarChartActivity extends BaseActivity implements OnChartValueSelectedListener, View.OnClickListener { private BarChart mBarChart; private Button btn_show_values; private Button btn_anim_x; private Button btn_anim_y; private Button btn_anim_xy; private Button btn_save_pic; private Button btn_auto_mix_max; private Button btn_actionToggleHighlight; private Button btn_actionToggleBarBorders; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_barchar); initView(); } private void initView() { btn_show_values = (Button) findViewById(R.id.btn_show_values); btn_show_values.setOnClickListener(this); btn_anim_x = (Button) findViewById(R.id.btn_anim_x); btn_anim_x.setOnClickListener(this); btn_anim_y = (Button) findViewById(R.id.btn_anim_y); btn_anim_y.setOnClickListener(this); btn_anim_xy = (Button) findViewById(R.id.btn_anim_xy); btn_anim_xy.setOnClickListener(this); btn_save_pic = (Button) findViewById(R.id.btn_save_pic); btn_save_pic.setOnClickListener(this); btn_auto_mix_max = (Button) findViewById(R.id.btn_auto_mix_max); btn_auto_mix_max.setOnClickListener(this); btn_actionToggleHighlight = (Button) findViewById(R.id.btn_actionToggleHighlight); btn_actionToggleHighlight.setOnClickListener(this); btn_actionToggleBarBorders = (Button) findViewById(R.id.btn_actionToggleBarBorders); btn_actionToggleBarBorders.setOnClickListener(this); mBarChart = (BarChart) findViewById(R.id.mBarChart); mBarChart.setOnChartValueSelectedListener(this); mBarChart.setDrawBarShadow(false); mBarChart.setDrawValueAboveBar(true); mBarChart.getDescription().setEnabled(false); mBarChart.setMaxVisibleValueCount(60); mBarChart.setPinchZoom(false); mBarChart.setDrawGridBackground(false); IAxisValueFormatter xAxisFormatter = new DayAxisValueFormatter(mBarChart); XAxis xAxis = mBarChart.getXAxis(); xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); xAxis.setDrawGridLines(false); xAxis.setGranularity(1f); xAxis.setLabelCount(7); xAxis.setAxisMaximum(50f); xAxis.setAxisMinimum(0f); xAxis.setValueFormatter(xAxisFormatter); XYMarkerView mv = new XYMarkerView(this, xAxisFormatter); mv.setChartView(mBarChart); mBarChart.setMarker(mv); YAxis leftAxis = mBarChart.getAxisLeft(); leftAxis.setLabelCount(8, false); leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART); leftAxis.setSpaceTop(15f); leftAxis.setAxisMinimum(0f); leftAxis.setAxisMaximum(50f); YAxis rightAxis = mBarChart.getAxisRight(); rightAxis.setDrawGridLines(false); rightAxis.setLabelCount(8, false); rightAxis.setSpaceTop(15f); rightAxis.setAxisMinimum(0f); rightAxis.setAxisMaximum(50f); Legend l = mBarChart.getLegend(); l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM); l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT); l.setOrientation(Legend.LegendOrientation.HORIZONTAL); l.setDrawInside(false); l.setForm(Legend.LegendForm.SQUARE); l.setFormSize(9f); l.setTextSize(11f); l.setXEntrySpace(4f); ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>(); yVals1.add(new BarEntry(0, 10)); yVals1.add(new BarEntry(5, 20)); yVals1.add(new BarEntry(16, 30)); yVals1.add(new BarEntry(18, 40)); yVals1.add(new BarEntry(20, 50)); yVals1.add(new BarEntry(22, 10)); yVals1.add(new BarEntry(25, 20)); yVals1.add(new BarEntry(28, 30)); yVals1.add(new BarEntry(30, 40)); setData(yVals1); } private void setData(ArrayList yVals1) { float start = 1f; BarDataSet set1; if (mBarChart.getData() != null && mBarChart.getData().getDataSetCount() > 0) { set1 = (BarDataSet) mBarChart.getData().getDataSetByIndex(0); set1.setValues(yVals1); mBarChart.getData().notifyDataChanged(); mBarChart.notifyDataSetChanged(); } else { set1 = new BarDataSet(yVals1, "2017年工资涨幅"); set1.setColors(ColorTemplate.MATERIAL_COLORS); ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>(); dataSets.add(set1); BarData data = new BarData(dataSets); data.setValueTextSize(10f); data.setBarWidth(0.9f); mBarChart.setData(data); } } @Override public void onValueSelected(Entry e, Highlight h) { } @Override public void onNothingSelected() { } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_show_values: for (IDataSet set : mBarChart.getData().getDataSets()) set.setDrawValues(!set.isDrawValuesEnabled()); mBarChart.invalidate(); break; case R.id.btn_anim_x: mBarChart.animateX(3000); break; case R.id.btn_anim_y: mBarChart.animateY(3000); break; case R.id.btn_anim_xy: mBarChart.animateXY(3000, 3000); break; case R.id.btn_save_pic: if (mBarChart.saveToGallery("title" + System.currentTimeMillis(), 50)) { Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_SHORT).show(); } else Toast.makeText(getApplicationContext(), "保存失败", Toast.LENGTH_SHORT).show(); break; case R.id.btn_auto_mix_max: mBarChart.setAutoScaleMinMaxEnabled(!mBarChart.isAutoScaleMinMaxEnabled()); mBarChart.notifyDataSetChanged(); break; case R.id.btn_actionToggleHighlight: if (mBarChart.getData() != null) { mBarChart.getData().setHighlightEnabled( !mBarChart.getData().isHighlightEnabled()); mBarChart.invalidate(); } break; case R.id.btn_actionToggleBarBorders: for (IBarDataSet set : mBarChart.getData().getDataSets()) ((BarDataSet) set) .setBarBorderWidth(set.getBarBorderWidth() == 1.f ? 0.f : 1.f); mBarChart.invalidate(); break; } }
- 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
- 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
有兴趣的加群:555974449
Sample:http://download.csdn.net/detail/qq_26787115/9685567