美文网首页
MPAndroidChart 3.0的简单使用——柱状图

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

作者: zj_King | 来源:发表于2017-09-21 16:49 被阅读222次

一、导入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>

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

相关文章

网友评论

      本文标题:MPAndroidChart 3.0的简单使用——柱状图

      本文链接:https://www.haomeiwen.com/subject/kjhtextx.html