柱状图使用实例

>> 代码:

private BarChart bc;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_bar);initView();initData();}private void initView() {bc = (BarChart) findViewById(R.id.bc);}private void initData() {
//        bc.setFitBars(true);bc.setExtraOffsets(24f,48f,24f,24f);setDescription("年龄群体车辆违章的占比统计");setLegend();setYAxis();setXAxis();setChartData();}private void setDescription(String descriptionStr) {Description description = new Description();description.setText(descriptionStr);description.setTextSize(18f);description.setTextAlign(Paint.Align.CENTER); // 文本居中对齐// 计算描述位置WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);DisplayMetrics outMetrics = new DisplayMetrics();wm.getDefaultDisplay().getMetrics(outMetrics);Paint paint = new Paint();paint.setTextSize(18f);paint.setTypeface(Typeface.DEFAULT_BOLD);float x = outMetrics.widthPixels / 2;float y =  Utils.calcTextHeight(paint, descriptionStr) + Utils.convertDpToPixel(24);description.setPosition(x, y);bc.setDescription(description);}private void setLegend() {Legend legend = bc.getLegend();legend.setTextSize(14f);legend.setXOffset(24f);legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); // 图例在水平线上向右对齐legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); // 图例在垂直线上向上对齐legend.setOrientation(Legend.LegendOrientation.VERTICAL); // 图例条目垂直方向排列legend.setDrawInside(true); // 绘制在图表内部}private void setYAxis() {// 左侧Y轴YAxis axisLeft = bc.getAxisLeft();axisLeft.setAxisMinimum(0); // 最小值为0axisLeft.setAxisMaximum(1200); // 最大值为1200axisLeft.setValueFormatter(new IAxisValueFormatter() { // 自定义值的格式@Overridepublic String getFormattedValue(float value, AxisBase axis) {return (int) value + "";}});// 右侧Y轴bc.getAxisRight().setEnabled(false); // 不启用}private void setXAxis() {// X轴XAxis xAxis = bc.getXAxis();xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); // 位于底部xAxis.setDrawGridLines(false); // 不绘制X轴网格线xAxis.setAxisMinimum(-0.3f); // 最小值-0.3f,为了使左侧留出点空间xAxis.setGranularity(1f); // 间隔尺寸1xAxis.setTextSize(14f); // 文本大小14xAxis.setTypeface(Typeface.DEFAULT_BOLD); // 加粗字体xAxis.setValueFormatter(new IAxisValueFormatter() { // 自定义值格式@Overridepublic String getFormattedValue(float value, AxisBase axis) {return 9 - (int) value + "0后";}});}private void setChartData() {final List<BarEntry> yVals1 = new ArrayList<>();yVals1.add(new BarEntry(0f, 396));yVals1.add(new BarEntry(1f, 1089));yVals1.add(new BarEntry(2f, 963));yVals1.add(new BarEntry(3f, 756));yVals1.add(new BarEntry(4f, 287));final List<BarEntry> yVals2 = new ArrayList<>();yVals2.add(new BarEntry(0f, 245));yVals2.add(new BarEntry(1f, 520));yVals2.add(new BarEntry(2f, 504));yVals2.add(new BarEntry(3f, 517));yVals2.add(new BarEntry(4f, 186));BarDataSet barDataSet1 = new BarDataSet(yVals1, "有违章");barDataSet1.setValueTextColor(Color.RED);barDataSet1.setColor(Color.GREEN);barDataSet1.setValueTextSize(14f);barDataSet1.setValueFormatter(new IValueFormatter() {@Overridepublic String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {return new DecimalFormat("##.0").format(value / (yVals2.get((int) entry.getX()).getY() + value) * 100) + "%";}});BarDataSet barDataSet2 = new BarDataSet(yVals2, "无违章");barDataSet2.setColor(Color.BLUE);barDataSet2.setDrawValues(false);List<IBarDataSet> dataSets = new ArrayList<>();dataSets.add(barDataSet1);dataSets.add(barDataSet2);BarData bardata = new BarData(dataSets);bardata.setBarWidth(0.4f);bc.setData(bardata);}

>> 效果图:

MPAndroidChart3使用详解4:BarChart(柱形图)相关推荐

  1. python 柱形图_Python 写入 Excel III 详解图形生成-柱形图

    ------------------------说在前面的话----------------------------- 一直觉得Excel的制图是最美的,尽管本人有多年Maltab使用经历,但是每次都 ...

  2. MPAndroidChart3使用详解3:LineChar(折线图)

    目录 1 简述 2 使用说明 2.1 使用 2.2 设置数据 2.3 折线数据集设置样式(LineDataSet) 2.4 折线数据设置样式(LineData) 2.5 Y轴(YAxis) 2.5 X ...

  3. 安卓图形之MPAndroidChart3.0详解一——折线图

    文章目录 1 前言 1.1 基本使用 1.1 默认样式 1.2 属性设置 Description设置 Legend设置 YAxis 设置 XAxis 设置 LineDataSet 设置 补充 1 前言 ...

  4. MPAndroidChart3使用详解8:RadarChart(雷达图)顶角显示圆点——额外设置一组顶点值数据方案

    目录 1 实现: 2 效果: 1 实现: Step 1:分别新建5个shape资源文件. Step 2:内容如下,根据个人需求更改颜色.半径.尺寸. <?xml version="1. ...

  5. MPAndroidChart3使用详解9:RadarChart(雷达图)顶角显示圆点——重写RadarChart控件添加顶角圆点绘制方案

    目录 1 思路: 2 实现: 3 效果: 1 思路: 查看RadarChart控件源码,找到绘制网线的相关源码,获取到各个顶点的坐标后,便可以在各个顶点绘制圆. Step 1:既然圆点是在网线各个顶点 ...

  6. Echarts数据可视化series-bar柱形图详解,开发全解+完美注释

    全栈工程师开发手册 (作者:栾鹏) Echarts数据可视化开发代码注释全解 Echarts数据可视化开发参数配置全解 6大公共组件详解(点击进入): title详解. tooltip详解.toolb ...

  7. python 堆叠柱状图,Python可视化matplotlib12-垂直|水平|堆积柱形图详解

    柱形图(又称柱状图.直方图.条形图)用于各组数据的数量比较,可以简单的分为水平柱形图.垂直柱形图及堆积柱形图. 本文详细介绍matpltlib中垂直.水平和堆积三种柱形图的绘制. 我的公众号:&quo ...

  8. MPAndroidChart使用详解--BarChart和PieChart

    MPAndroidChart使用详解--BarChart和PieChart使用实例 1. app的build.gradle中添加依赖: implementation 'com.github.PhilJ ...

  9. 【ECharts】ECharts配置项详解

    这是一个方便自己学习ECharts所编写的,如有遗漏和错误之处,欢迎各位指出 文章部分转载于用户:法海521的博客"echarts各个配置项详细说明总结",地址:https://b ...

最新文章

  1. php程序耗时是负数,php 代码测试,代码越在前面越耗时
  2. 自由意志不存在?神经科学能证明不?
  3. 【机器学习】快速入门简单线性回归 (SLR)
  4. BugKuCTF WEB web基础$_GET
  5. python requests返回值为200 但是text无内容_接口测试入门神器 - Requests
  6. oracle anonhugepage,案例:Oracle linux redhat检查Transparent HugePages状态并关闭
  7. redis内存知识点
  8. Linux 命令(27)—— echo 命令
  9. 【A little interesting】LeetCode 437. Path Sum III
  10. Keil3/4/5 For C51安装教程(附Baidu 云链接)
  11. MPU6050的数据获取、分析与处理
  12. 散文:dflow 是如何实现slice的
  13. 探究人工智能辅助新时代心理学研究
  14. Room的基本使用(一)
  15. iOS-内购注意 沙盒二次验证
  16. jses6 新特性,以及es7/8/9 或,常用特性
  17. 10 个可以副业赚钱的网站,总有一个适合你
  18. php db mssql 2008,php mssql 不能用 DB-Library(如 ISQL)或 ODBC 3.7 或更早版
  19. 国家为什么会失败-读书笔记
  20. c语言程序电压采样,单片机电压采集装置课程设计(AD转换及编程实现).doc

热门文章

  1. nginx sendfile什么作用
  2. JS--input不可编辑
  3. 简洁好用的3个研发项目管理工具
  4. 《python数据分析与挖掘实战》笔记第3章
  5. camera 之 createCaptureSession
  6. strstr 函数介绍
  7. Conflux项目进度报告 十月第一期
  8. 如何使用Redis缓存 ?
  9. java 高级面试题(借鉴)(上)
  10. 如何看待国企纷纷卸载微软Office改用金山WPS?