美文网首页
MPAndroidChart画成交量图

MPAndroidChart画成交量图

作者: 就是这么简简单单 | 来源:发表于2017-07-24 17:49 被阅读0次

第一步:去GitHub上集成MPAndroidChart(https://github.com/PhilJay/MPAndroidChart)
在Project的build.gradle中依赖

image.png

在app的build.gradle中依赖

compile 'com.github.PhilJay:MPAndroidChart:v3.0.2'

第二步:
在xml布局文件中写上柱状图

<com.github.mikephil.charting.charts.BarChart
        android:id="@+id/chart1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
     />

第三步:开始撸代码
1.找到布局文件

mChart = findViewById(R.id.chart1);

2.开始用mChart 进行设置

        //不要网格线
        mChart.setDrawBarShadow(false);
        mChart.setDrawValueAboveBar(true);
        mChart.getDescription().setEnabled(false);
        mChart.setPinchZoom(false);
        mChart.setDrawGridBackground(false);
        //X轴
   `    XAxis xAxis = mChart.getXAxis();
        xAxis.setDrawAxisLine(false);
        xAxis.setDrawGridLines(false);
        xAxis.setEnabled(false);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);

        mChart.getAxisRight().setEnabled(false);// 右边Y轴
        leftAxis = mChart.getAxisLeft();// 左边Y轴
        leftAxis.setDrawAxisLine(false);
        leftAxis.setEnabled(false);
        mChart.getLegend().setEnabled(false);// 标签

第三步:从服务器获取到数据,解析出其中包含成交量的集合

leftAxis.setAxisMaximum((float) max);//设置左Y轴最大值
mChart.setMaxVisibleValueCount(volumeList.size());//显示的数目

写个方法将集合传出去 setData(volumeList);
开始绘制,成交量是一红一绿

  private void setData(VolumeList volumeList) {
        BarDataSet set1 ;
        BarDataSet set2 ;
        ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
        ArrayList<BarEntry> yVals2 = new ArrayList<BarEntry>();
        for (int i = 0; i < volumeList.size(); i++) {
            double volume22 = volumeList.get(i).getVolume();
            double currentClose = volumeList.get(i).getCurrentClose();
          //容错处理,防止后台传负数
            if (volume22 < 0) {
                volume22 = 0;
            }
          //这是判断什么条件显示红色和绿色
            if (i == 0) {
                if (currentClose >= previousClose) {
                    yVals1.add(new BarEntry(i, (float) volume22));
                } else {
                    yVals2.add(new BarEntry(i, (float) volume22));
                }
            } else {
                if (currentClose >= volumeList.get(i - 1).getCurrentClose()) {
                    yVals1.add(new BarEntry(i, (float) volume22));
                } else {
                    yVals2.add(new BarEntry(i, (float) volume22));
                }
            }
        }

      //绘制 红线
        if (mChart.getData() != null && mChart.getData().getDataSetCount() > 0) {
            set1 = (BarDataSet) mChart.getData().getDataSetByIndex(0);
            set1.setValues(yVals1);
            mChart.getData().notifyDataChanged();
            mChart.notifyDataSetChanged();
        } else {
            set1 = new BarDataSet(yVals1, "123");
            set1.setDrawIcons(false);
            set1.setDrawValues(false);
            set1.setColors(Color.parseColor(BaseUtils.Color_Red));
            dataSets = new ArrayList<IBarDataSet>();
            dataSets.add(set1);
        }
    //绘制 绿线
        if (mChart.getData() != null && mChart.getData().getDataSetCount() > 0) {
            set2 = (BarDataSet) mChart.getData().getDataSetByIndex(0);
            set2.setValues(yVals2);
            mChart.getData().notifyDataChanged();
            mChart.notifyDataSetChanged();
        } else {
            set2 = new BarDataSet(yVals2, "123");
            set2.setDrawIcons(false);
            set2.setDrawValues(false);
            set2.setColors(Color.parseColor(BaseUtils.Color_Green));
            dataSets.add(set2);
        }

        BarData data = new BarData(dataSets);
        data.setValueTextSize(10f);
        data.setBarWidth(0.7f);
        mChart.setData(data);
        mChart.animateXY(800, 800);//图表数据显示动画
        mChart.invalidate();
    }

最后效果图:

KDSM)FY)R{7~)$X5YKZ(A0Z.png

相关文章

网友评论

      本文标题:MPAndroidChart画成交量图

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