MPAndroidChart 3.0的简单使用——柱状图

一、导入MPAndroidChart 3.0包

在项目中的build.gradle中导入compile'com.github.PhilJay:MPAndroidChart:v3.0.1'。

二、项目中的使用(以柱状图为例)
新建布局文件activity_barchart。
<pre>

    <com.github.mikephil.charting.charts.BarChart
    android:id="@+id/check_bc"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:paddingTop="10dp"
    />

</pre>
三、项目中使用

  1. 获取BarChart的id,并设置一些相关的属性
    <pre>

       private void initCheckParChart(){
             check_bc = (BarChart) getView().findViewById(R.id.check_bc);
             check_bc.getDescription().setEnabled(false);
              Description description = new Description();
             description.setText("验收统计");//设置右下角的描述信息
             description.setTextSize(14f);//设置描述信息字体大小
             check_bc.setDescription(description);
    
             check_bc.setNoDataText("暂无数据"); //设置空表的描述信息
             check_bc.setDrawGridBackground(false);//是否绘制网格背景
             check_bc.setDrawValueAboveBar(true);//将Y数据显示在点的上方
             check_bc.setPinchZoom(true);//挤压缩放
             check_bc.setDrawBarShadow(false);
             check_bc.setDrawGridBackground(false);
             check_bc.setScaleYEnabled(false);
             check_bc.setDoubleTapToZoomEnabled(false);//双击缩放
             check_bc.animateY(2500);
             check_bc.getLegend().setEnabled(false);
             initCheckData();
      }
    

</pre>
2.解析统计数据
数据使用的是自己写的json串,后期可以修改成从服务器获取的json。
<pre>

   private String CHECKJSON = "{\n" +
        "    \"rows\":[\n" +
        "        {\n" +
        "            \"NAME\":\"立项\",\n" +
        "            \"NUM\":\"10\"\n" +
        "        },\n" +
        "        {\n" +
        "            \"NAME\":\"招标\",\n" +
        "            \"NUM\":\"26\"\n" +
        "        },\n" +
        "        {\n" +
        "            \"NAME\":\"签合同\",\n" +
        "            \"NUM\":\"30\"\n" +
        "        },\n" +
        "        {\n" +
        "            \"NAME\":\"验收\",\n" +
        "            \"NUM\":\"24\"\n" +
        "        },\n" +
        "        {\n" +
        "            \"NAME\":\"结项\",\n" +
        "            \"NUM\":\"15\"\n" +
        "        }\n" +
        "    ]\n" +
        "}";

</pre>
<pre>

public void initCheckData(){
    try {
        JSONObject object = new JSONObject(CHECKJSON);
        JSONArray array = object.getJSONArray("rows");
        ArrayList<BarEntry> entries = new ArrayList<BarEntry>();
        ArrayList<String> xVals = new ArrayList<String>();
        for(int j = 0;j<array.length();j++){
            JSONObject jsonObject = array.getJSONObject(j);
            BarEntry barEntry = new BarEntry(j,Float.parseFloat(jsonObject.optString("NUM")));
            xVals.add(jsonObject.optString("NAME"));
            entries.add(barEntry);
        }
       setData(entries,xVals);

    } catch (JSONException e) {
        e.printStackTrace();
    }

}

</pre>
3.给柱状图添加数据
最需要注意的是3.0以后给X轴赋值为汉字的时候不能使用BarData bardata = new BarData( xVals,dataSets)方法了,我使用以下方法给X轴添加汉字。在使用的同时需要注意一定要添加此方法xAxis.setLabelCount(xVals.size())来设置X轴的个数,如果不使用会出现多个相同的名称。
<pre>

         xAxis.setValueFormatter(new IAxisValueFormatter() {
           @Override
           public String getFormattedValue(float value, AxisBase axis) {
               return String.valueOf(xVals.get((int) value));
            }
         });

</pre>
<pre>

 private void setData(ArrayList<BarEntry> entries,final ArrayList<String> xVals) {
    XAxis xAxis =  check_bc.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setDrawGridLines(false);
    //设置x轴的数据
    xAxis.setValueFormatter(new IAxisValueFormatter() {
        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            return String.valueOf(xVals.get((int) value));
        }
    });
    //显示个数
    xAxis.setLabelCount(xVals.size());

   BarDataSet dataSet = new BarDataSet(entries, "验收统计");
    //数据和颜色
    ArrayList<Integer> colors = new ArrayList<Integer>();
    // 柱状图颜色
    colors.add(Color.rgb(44,120,221));
    colors.add(Color.rgb(239,167,52));
    colors.add(Color.rgb(71,183,61));
    colors.add(Color.rgb(87,162,253));
    colors.add(Color.rgb(255,125,39));
    dataSet.setColors(colors);
 //   dataSet.setValueTextColors(colors);

    ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
    dataSets.add(dataSet);

    BarData bardata = new BarData(dataSets);
    bardata.setDrawValues(true);
    bardata.setValueTextColor(Color.BLACK);
    bardata.setValueTextSize(13);

    check_bc.setData(bardata);
    check_bc.animateXY(800,800);//图表数据显示动画
    check_bc.setVisibleXRangeMaximum(15);//设置屏幕显示条数
    check_bc.invalidate();
    //刷新
    check_bc.invalidate();
}

</pre>

作者水平有限,如有错误,请多多指教。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,282评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 177,266评论 25 709
  • Swift版本点击这里欢迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh阅读 25,785评论 7 249
  • 先生又加班睡单位了,离家太远,回来十一点,早上六点多起床就走,太折腾,真想在单位附近有处房子,加班到多晚随时都可以...
    薄荷的自由未来阅读 1,481评论 0 0
  • 对于一个积极进取的人而言,面对压力自问,“如果没做成又如何?”这样的想法并非找借口,而是一种有效疏解压力的方式。但...
    澹然四海清阅读 2,720评论 0 0

友情链接更多精彩内容