当前位置:网站首页 > 更多 > 编程开发 > 正文

[安卓] 安卓MPAndroidChart绘制水平柱状图

作者:CC下载站 日期:2020-10-10 00:00:00 浏览:59 分类:编程开发

这个和垂直柱状图一模一样,只不过把控件名换了一下从barchart换成了HorizontalBarChart

XML文件

<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="水平柱状图"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="vertical">

<com.github.mikephil.charting.charts.HorizontalBarChart
android:id="@+id/horizontalBarChart"
android:layout_width="match_parent"
android:layout_height="150dp"/>
</LinearLayout>

MainActivity

//初始化水平柱状图
HorizontalBarCharthorizontalBarChart=findViewById(R.id.horizontalBarChart);
initBarChart(horizontalBarChart);
horizontalBarChart.setData(setBarData());
barChart.invalidate();

publicBarChartinitBarChart(BarChartbarChart){
barChart.setDrawBarShadow(false);//设置每条柱子的阴影不显示
barChart.setDrawValueAboveBar(true);//设置每条柱子的数值显示
barChart.setPinchZoom(false);
XAxisxAxis=barChart.getXAxis();//获取柱状图的x轴
YAxisyAxisLeft=barChart.getAxisLeft();//获取柱状图左侧的y轴
YAxisyAxisRight=barChart.getAxisRight();//获取柱状图右侧的y轴
setAxis(xAxis,yAxisLeft,yAxisRight);//调用方法设置柱状图的轴线
returnbarChart;
}

publicBarDatasetBarData(){
List<BarEntry>entries=newArrayList<>();//定义一个数据容器
//生成随机数数据
for(inti=0;i<=12;i++){
entries.add(newBarEntry(i,newRandom().nextInt(300)));
}
BarDataSetbarDataSet=newBarDataSet(entries,"测试数据");
BarDatabarData=newBarData(barDataSet);
returnbarData;//返回可用于柱状图的数据
}

文章参考:https://blog.csdn.net/weixin_43344890/article/details/103008320

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯