MPAndroidChart(二)——BarChart
BarChart柱状图:
图示.png
界面布局
<com.github.mikephil.charting.charts.BarChart android:id="@+id/chart" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/seekBar1" android:layout_margin="20dp" />
首先在activity中对柱状图的属性进行设置
其中mChart是对柱状图的样式及效果进行设置,Legend是图表标题图例位置样式的设置
/***图表设置***/ //背景颜色 chart.setBackgroundColor(Color.WHITE); //不显示图表网格 chart.setDrawGridBackground(false); //背景阴影 chart.setDrawBarShadow(false); chart.setHighlightFullBarEnabled(false); //显示边框 chart.setDrawBorders(true); Matrix m=new Matrix(); m.postScale(20f,1f);//两个参数分别是x,y轴的缩放比例。例如:将x轴的数据放大为之前的1.5倍 chart.getViewPortHandler().refresh(m,chart,false);//将图表动画显示之前进行缩放 chart.animateX(1000); /***XY轴的设置***/ //X轴设置显示位置在底部 xAxis = chart.getXAxis(); xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); xAxis.setAxisMinimum(0f); xAxis.setGranularity(1f); leftAxis = chart.getAxisLeft(); rightAxis = chart.getAxisRight(); //保证Y轴从0开始,不然会上移一点 leftAxis.setAxisMinimum(0f); rightAxis.setAxisMinimum(0f); /***折线图例 标签 设置***/ legend = chart.getLegend(); legend.setForm(Legend.LegendForm.LINE); legend.setTextSize(11f); //显示位置 legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER); legend.setOrientation(Legend.LegendOrientation.HORIZONTAL); //能否绘制在图表里面 legend.setDrawInside(false);
在设置好柱状图的样式及效果之后,对其中的数据进行设置,list为数据值
属性为Name(名称),Num(数量)
List<BarEntry> barEntryList = new ArrayList(); for (int i = 0; i < list.size(); i++) { barEntryList.add(new BarEntry(i,list.get(i).getNum); } BarDataSet dataset = new BarDataSet(barEntryList,"第一组的名称"); dataset.setColor(Color.rgb(129, 216, 200));//设置第一组的颜色 final Entity entity = entity;//为了后续将X轴数值设置为汉字做准备 BarData data = new BarData(dataset);//假如未多组时传入的是X值以及List<BarDataSet> data.setValueTextSize(10f); data.setBarWidth(0.9f); chart.setData(data); chart.getBarData().setBarWidth(barWidth); chart.getXAxis().setAxisMinimum(0); chart.getXAxis().setValueFormatter(new IAxisValueFormatter() {//设置X值为汉字,格式化 @Override public String getFormattedValue(float value, AxisBase axis) { return double2String(value,townsNatureChartEntity1); } }); chart.invalidate(); //将图表重绘以显示设置的属性和数据
格式化的具体方法
public String double2String(float f, Entity entity) { return entity.get(int(f)).getName(); }
这样显示出来的柱状图,则Y轴为数值,X轴为汉字。
说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » MPAndroidChart(二)——BarChart
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » MPAndroidChart(二)——BarChart