Android图表库MPAndroidChart(八)——饼状图的扩展:折线饼状图
来源:互联网 发布:thinkphp5商城源码 编辑:程序博客网 时间:2024/06/06 09:38
Android图表库MPAndroidChart(八)——饼状图的扩展:折线饼状图
我们接着上文,饼状图的扩展,增加折现的说明,来看下我们要实现的效果
因为之前对MPAndroidChart的熟悉,所有我们就可以直接来实现了
一.基本实现
首先,就是我们的来看下他的定义布局
<com.github.mikephil.charting.charts.PieChart android:id="@+id/mPieChart" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/>
- 1
- 2
- 3
- 4
- 5
嗯,这里我们还是用PieChart,那现在该干嘛就干嘛了
//折现饼状图 mPieChart = (PieChart) findViewById(R.id.mPieChart); mPieChart.setUsePercentValues(true); mPieChart.getDescription().setEnabled(false); mPieChart.setExtraOffsets(5, 10, 5, 5); mPieChart.setDragDecelerationFrictionCoef(0.95f); //绘制中间文字 mPieChart.setCenterText(generateCenterSpannableText()); mPieChart.setExtraOffsets(20.f, 0.f, 20.f, 0.f); mPieChart.setDrawHoleEnabled(true); mPieChart.setHoleColor(Color.WHITE); mPieChart.setTransparentCircleColor(Color.WHITE); mPieChart.setTransparentCircleAlpha(110); mPieChart.setHoleRadius(58f); mPieChart.setTransparentCircleRadius(61f); mPieChart.setDrawCenterText(true); mPieChart.setRotationAngle(0); // 触摸旋转 mPieChart.setRotationEnabled(true); mPieChart.setHighlightPerTapEnabled(true); // 添加一个选择监听器 mPieChart.setOnChartValueSelectedListener(this); //模拟数据 ArrayList<PieEntry> entries = new ArrayList<PieEntry>(); entries.add(new PieEntry(40, "优秀")); entries.add(new PieEntry(20, "满分")); entries.add(new PieEntry(30, "及格")); entries.add(new PieEntry(10, "不及格")); //设置数据 setData(entries); //默认动画 mPieChart.animateY(1400, Easing.EasingOption.EaseInOutQuad); Legend l = mPieChart.getLegend(); l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); l.setOrientation(Legend.LegendOrientation.VERTICAL); l.setDrawInside(false); l.setEnabled(false);
- 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
少不了我们的设置数据
//设置数据 private void setData(ArrayList<PieEntry> entries) { PieDataSet dataSet = new PieDataSet(entries, "三年级一班"); dataSet.setSliceSpace(3f); dataSet.setSelectionShift(5f); ArrayList<Integer> colors = new ArrayList<Integer>(); for (int c : ColorTemplate.VORDIPLOM_COLORS) colors.add(c); for (int c : ColorTemplate.JOYFUL_COLORS) colors.add(c); for (int c : ColorTemplate.COLORFUL_COLORS) colors.add(c); for (int c : ColorTemplate.LIBERTY_COLORS) colors.add(c); for (int c : ColorTemplate.PASTEL_COLORS) colors.add(c); colors.add(ColorTemplate.getHoloBlue()); dataSet.setColors(colors); dataSet.setValueLinePart1OffsetPercentage(80.f); dataSet.setValueLinePart1Length(0.2f); dataSet.setValueLinePart2Length(0.4f); dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE); PieData data = new PieData(dataSet); data.setValueFormatter(new PercentFormatter()); data.setValueTextSize(11f); data.setValueTextColor(Color.BLACK); mPieChart.setData(data); // 撤销所有的亮点 mPieChart.highlightValues(null); mPieChart.invalidate(); }
- 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
这里,可不能把中间文字给丢了
//绘制中心文字 private SpannableString generateCenterSpannableText() { SpannableString s = new SpannableString("刘某人程序员\n我仿佛听到有人说我帅"); //s.setSpan(new RelativeSizeSpan(1.5f), 0, 14, 0); //s.setSpan(new StyleSpan(Typeface.NORMAL), 14, s.length() - 15, 0); //s.setSpan(new ForegroundColorSpan(Color.GRAY), 14, s.length() - 15, 0); //s.setSpan(new RelativeSizeSpan(.65f), 14, s.length() - 15, 0); //s.setSpan(new StyleSpan(Typeface.ITALIC), s.length() - 14, s.length(), 0); //s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), s.length()-17, s.length(), 0); return s; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
现在运行一下就可以了,这个不难,我们继续看他的实现
二.显示百分比
三.显示类型
四.x轴动画
五.y轴动画
六.xy轴动画
七.显示中间文字
八.旋转动画
这里代码贴上
activity_pie_line.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.PieChart android:id="@+id/mPieChart" 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_percentage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示百分比"/> <Button android:id="@+id/btn_show_type" 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轴动画"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn_anim_xy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="XY轴动画"/> <Button android:id="@+id/btn_show_center_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示中间文字"/> <Button android:id="@+id/btn_save_pic" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="保存画廊"/> <Button android:id="@+id/btn_anim_rotating" 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
PiePolylineChartActivity
public class PiePolylineChartActivity extends BaseActivity implements OnChartValueSelectedListener, View.OnClickListener { private PieChart mPieChart; //显示百分比 private Button btn_show_percentage; //显示类型 private Button btn_show_type; //x轴动画 private Button btn_anim_x; //y轴动画 private Button btn_anim_y; //xy轴动画 private Button btn_anim_xy; //保存到sd卡 private Button btn_save_pic; //显示中间文字 private Button btn_show_center_text; //旋转动画 private Button btn_anim_rotating; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pie_line); initView(); } //初始化 private void initView() { btn_show_percentage = (Button) findViewById(R.id.btn_show_percentage); btn_show_percentage.setOnClickListener(this); btn_show_type = (Button) findViewById(R.id.btn_show_type); btn_show_type.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_show_center_text = (Button) findViewById(R.id.btn_show_center_text); btn_show_center_text.setOnClickListener(this); btn_anim_rotating = (Button) findViewById(R.id.btn_anim_rotating); btn_anim_rotating.setOnClickListener(this); //折现饼状图 mPieChart = (PieChart) findViewById(R.id.mPieChart); mPieChart.setUsePercentValues(true); mPieChart.getDescription().setEnabled(false); mPieChart.setExtraOffsets(5, 10, 5, 5); mPieChart.setDragDecelerationFrictionCoef(0.95f); //绘制中间文字 mPieChart.setCenterText(generateCenterSpannableText()); mPieChart.setExtraOffsets(20.f, 0.f, 20.f, 0.f); mPieChart.setDrawHoleEnabled(true); mPieChart.setHoleColor(Color.WHITE); mPieChart.setTransparentCircleColor(Color.WHITE); mPieChart.setTransparentCircleAlpha(110); mPieChart.setHoleRadius(58f); mPieChart.setTransparentCircleRadius(61f); mPieChart.setDrawCenterText(true); mPieChart.setRotationAngle(0); // 触摸旋转 mPieChart.setRotationEnabled(true); mPieChart.setHighlightPerTapEnabled(true); // 添加一个选择监听器 mPieChart.setOnChartValueSelectedListener(this); //模拟数据 ArrayList<PieEntry> entries = new ArrayList<PieEntry>(); entries.add(new PieEntry(40, "优秀")); entries.add(new PieEntry(20, "满分")); entries.add(new PieEntry(30, "及格")); entries.add(new PieEntry(10, "不及格")); //设置数据 setData(entries); //默认动画 mPieChart.animateY(1400, Easing.EasingOption.EaseInOutQuad); Legend l = mPieChart.getLegend(); l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); l.setOrientation(Legend.LegendOrientation.VERTICAL); l.setDrawInside(false); l.setEnabled(false); } //设置数据 private void setData(ArrayList<PieEntry> entries) { PieDataSet dataSet = new PieDataSet(entries, "三年级一班"); dataSet.setSliceSpace(3f); dataSet.setSelectionShift(5f); ArrayList<Integer> colors = new ArrayList<Integer>(); for (int c : ColorTemplate.VORDIPLOM_COLORS) colors.add(c); for (int c : ColorTemplate.JOYFUL_COLORS) colors.add(c); for (int c : ColorTemplate.COLORFUL_COLORS) colors.add(c); for (int c : ColorTemplate.LIBERTY_COLORS) colors.add(c); for (int c : ColorTemplate.PASTEL_COLORS) colors.add(c); colors.add(ColorTemplate.getHoloBlue()); dataSet.setColors(colors); dataSet.setValueLinePart1OffsetPercentage(80.f); dataSet.setValueLinePart1Length(0.2f); dataSet.setValueLinePart2Length(0.4f); dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE); PieData data = new PieData(dataSet); data.setValueFormatter(new PercentFormatter()); data.setValueTextSize(11f); data.setValueTextColor(Color.BLACK); mPieChart.setData(data); // 撤销所有的亮点 mPieChart.highlightValues(null); mPieChart.invalidate(); } //绘制中心文字 private SpannableString generateCenterSpannableText() { SpannableString s = new SpannableString("刘某人程序员\n我仿佛听到有人说我帅"); //s.setSpan(new RelativeSizeSpan(1.5f), 0, 14, 0); //s.setSpan(new StyleSpan(Typeface.NORMAL), 14, s.length() - 15, 0); //s.setSpan(new ForegroundColorSpan(Color.GRAY), 14, s.length() - 15, 0); //s.setSpan(new RelativeSizeSpan(.65f), 14, s.length() - 15, 0); //s.setSpan(new StyleSpan(Typeface.ITALIC), s.length() - 14, s.length(), 0); //s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), s.length()-17, s.length(), 0); return s; } @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_percentage: for (IDataSet<?> set : mPieChart.getData().getDataSets()) set.setDrawValues(!set.isDrawValuesEnabled()); mPieChart.invalidate(); break; //显示类型 case R.id.btn_show_type: if (mPieChart.isDrawHoleEnabled()) mPieChart.setDrawHoleEnabled(false); else mPieChart.setDrawHoleEnabled(true); mPieChart.invalidate(); break; //x轴动画 case R.id.btn_anim_x: mPieChart.animateX(1400); break; //y轴动画 case R.id.btn_anim_y: mPieChart.animateY(1400); break; //xy轴动画 case R.id.btn_anim_xy: mPieChart.animateXY(1400, 1400); break; //保存到sd卡 case R.id.btn_save_pic: mPieChart.saveToPath("title" + System.currentTimeMillis(), ""); break; //显示中间文字 case R.id.btn_show_center_text: if (mPieChart.isDrawCenterTextEnabled()) mPieChart.setDrawCenterText(false); else mPieChart.setDrawCenterText(true); mPieChart.invalidate(); break; //旋转动画 case R.id.btn_anim_rotating: mPieChart.spin(1000, mPieChart.getRotationAngle(), mPieChart.getRotationAngle() + 360, Easing.EasingOption .EaseInCubic); 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
- 214
- 215
- 216
好的,是不是很简单
有兴趣的加群:555974449
Sample:http://download.csdn.net/detail/qq_26787115/9689868
转自:http://blog.csdn.net/qq_26787115/article/details/53258500
阅读全文
0 0
- Android图表库MPAndroidChart(八)——饼状图的扩展:折线饼状图
- Android图表库MPAndroidChart(八)——饼状图的扩展:折线饼状图
- Android图表库MPAndroidChart(八)——饼状图的扩展:折线饼状图
- Android图表库MPAndroidChart(六)饼状图 -折线百分比
- Android图表库MPAndroidChart(七)—饼状图
- Android图表 MPAndroidChart折线图
- Android图表 MPAndroidChart折线图
- MPAndroidChart开源图表库的使用介绍之饼状图、折线图和柱状图
- 【1】MPAndroidChart图表库的使用—来创建我们的第一张折线图
- Android图表库MPAndroidChart(五)饼状图
- Android——图表库MPAndroidChart的使用
- android图表——MPAndroidchart
- Android图表超简单实现柱状图、折线图、饼状图(基于MpAndroidChart)
- Android图表超简单实现柱状图、折线图、饼状图(基于MpAndroidChart)
- Android图表库MPAndroidChart(十四)——在ListView种使用相同的图表
- Android图表库MPAndroidChart(十四)——在ListView种使用相同的图表
- MPAndroidChart图表库之折线图
- Android图表库MPAndroidChart(七)—饼状图可以再简单一点
- openSession出错:蓝萝卜blu Hibernate4中使用getCurrentSession报Could not obtain transaction-synchronized Sessio
- 前断笔试变态题
- Mac 下载mysql安装包, 超级快的速度从官网下载
- Linux 守护进程的启动方法
- 我的 Web API学习之路(更新中)
- Android图表库MPAndroidChart(八)——饼状图的扩展:折线饼状图
- 抽象类
- python不定几率出现httplib.BadStatusLine: ''
- Gradle入门系列(4):创建二进制发布版本
- AI及相关领域国际会议级别
- 拷贝构造函数的特殊调用方式+陷阱
- callback使用与闭包
- 负载均衡架构及实现
- LintCode 第491题 回文数