美文网首页
MPAndroidChart:柱形统计图使用记录-横向

MPAndroidChart:柱形统计图使用记录-横向

作者: smallestt | 来源:发表于2018-03-29 14:47 被阅读0次

    1、引入 build.gradle

    allprojects {
        repositories {
            maven { url "https://jitpack.io" }
        }
    }
    dependencies{
        compile 'com.github.PhilJay:MPAndroidChart:v3.0.2'
     }
    

    2、布局文件

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

    3、 Activity

      mChart.setEnabled(true);
            mChart.setDrawValueAboveBar(true);
            mChart.setDrawBarShadow(false);
            mChart.setDrawValueAboveBar(true);
            mChart.getDescription().setEnabled(false);
            mChart.setMaxVisibleValueCount(31);
            mChart.setPinchZoom(false);
            mChart.setDrawGridBackground(false);
            XAxis xl = mChart.getXAxis();
            xl.setPosition(XAxis.XAxisPosition.BOTTOM);
            xl.setTypeface(mTfLight);
    
            xl.setDrawAxisLine(true);
            xl.setDrawGridLines(false);
            xl.setAxisMinimum(1); // this replaces setStartAtZero(true)
            xl.setAxisMaximum(31);
            xl.setLabelCount(31, false);
    
    
            YAxis yl = mChart.getAxisLeft();
            yl.setTypeface(mTfLight);
            yl.setDrawAxisLine(true);
            yl.setDrawGridLines(true);
            yl.setAxisMinimum(1f); // this replaces setStartAtZero(true)
            yl.setAxisMaximum(300f);
    //        yl.setInverted(true);
    
    
            YAxis yr = mChart.getAxisRight();
            yr.setTypeface(mTfLight);
            yr.setDrawAxisLine(true);
            yr.setDrawGridLines(false);
            yr.setAxisMinimum(1f); // this replaces setStartAtZero(true)
            yr.setAxisMaximum(300f);
    //        yr.setInverted(true);
    
            setData(31, 300);//设置数据
            mChart.setFitBars(true);
            mChart.animateXY(3000, 3000);//为X轴Y轴设置动画
            Legend l = mChart.getLegend();
            l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
            l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
            l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
            l.setDrawInside(false);
            l.setFormSize(8f);
            l.setXEntrySpace(14f);
    

    4、填充数据

        private void setData(int count, float range) {
            float barWidth = 2f;
            float spaceForBar = 10f;
            ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
    
            for (int i = 1; i <= count; i++) {
                float val = (float) (Math.random() * range);
                yVals1.add(new BarEntry(i, val));
    
            }
    
            BarDataSet set1;
    
            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, "数量/捆");//Y轴名称
    
    //            set1.setDrawIcons(false);
    
                ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
                dataSets.add(set1);
    
                BarData data = new BarData(dataSets);
                data.setValueTextSize(10f);
                data.setValueTypeface(mTfLight);
    //            data.setBarWidth(barWidth);
                mChart.setData(data);
            }
        }
    
    

    相关文章

      网友评论

          本文标题:MPAndroidChart:柱形统计图使用记录-横向

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