快速使用MPAndroidChart实现图表制作

来源:互联网 发布:英文软件 编辑:程序博客网 时间:2024/06/05 15:43

MPAndroidChart快速开发项目中的图表

GitHub地址:https://github.com/PhilJay/MPAndroidChart


在我的项目中用到最多的就是柱状图,饼状图,折线图,还有少部分的横向柱状图,为了方便使用,上网也找了一段时间的关于二次封装的,感觉不是很好用,不适合我,

索性自己把属性设置等一些共同的东西抽取出来,自己封装方便项目中使用

1、依赖注入

compile'com.github.PhilJay:MPAndroidChart:+'

2、折线图

布局界面:

<includelayout="@layout/default_linechart"/>

 

应用:

@BindView(R.id.mLineChart)
LineChart mChart;

List<String>xLists;
List<Entry>values;

/**
 * 增加数据
 */
values =newArrayList<>();
for (inti =0;i <100;i++) {

//下标、展示值、点击事件中展示的值
    values.add(newEntry(i,i,i));
}
xLists =newArrayList<>();
for (intb =1;b <100;b++) {
    xLists.add(b +"");
}
LineChartItem.getInstance()
        .Content(this)      //上下文
        .YValue(values)     //Y轴数据集合
        .XValue(xLists)     //X轴数据集合
        .setWidget(mChart)  //控件
        .setLimitLines(false) //是否展示平均线
        .showMean(50.f)//平均值
        .show();//展示

 

//点击事件  getData()里面的值为Entry中第三个值
mChart.setOnChartValueSelectedListener(newOnChartValueSelectedListener() {
    @Override
    public voidonValueSelected(Entry e,Highlight h) {
        ToastUtils.showLongToast(e.getData() +"");
    }
});

 

3、饼状图

布局界面:

<includelayout="@layout/default_piechart"/>

 

应用:

@BindView(R.id.mPieChart)
PieChart pieChart;
@BindView(R.id.mRecyclerView)
MyRecycleView mRecyclerView;

privateList<PieEntry>pieEntries =newArrayList<>();

 

pieEntries.add(newPieEntry(10f,"投资","投资"));
pieEntries.add(newPieEntry(20f,"消费","消费"));
pieEntries.add(newPieEntry(30f,"理财","理财"));
pieEntries.add(newPieEntry(20f,"保险","保险"));
pieEntries.add(newPieEntry(20f,"基金","基金"));
pieEntries.add(newPieEntry(10f,"股票","股票"));

PieChartsItem.getInstance()
        .Content(this)//上下文
        .setWidget(pieChart)                //控件
        .setValue(pieEntries)               //集合
        .setRecycler(mRecyclerView)         //如果使用recycler展示传入控件
        .setCenterEnabled(true)             //是否展示中间文字
        .setLegendEnabled(false)            //是否展示原来的标注图
        .setRotationEnabled(true)           //是否可以滚动
        .setDrawHoleEnabled(true)           //是否是环形
        .setRecyclerEnabled(true)           //是否使用RecyclerView展示set
        .setCenterText("所有指标")           //中间白色圆形中心文字说明
        .show();                             //展示

 

柱状图:

使用方法:布局文件

<<include layout="@layout/default_barchart"/>

 

应用:

 

@BindView(R.id.mBarChart)
BarChart mChart;

 

List<BarEntry>yVals1=newArrayList<>();   y轴数据
List<BarEntry>yVals2=newArrayList<>();
List<String>xVals=newArrayList<>();   x轴数据

 

//增加测试数据

for(inti = 1;i <101;i++) {
    yVals1.add(newBarEntry(i,i+1,i+1));
    yVals2.add(newBarEntry(i,i+3,i+3));
}
for (intb =1;b <101;b++) {
    xVals.add(b +"");
}

 

BarChartsItem.getInstance()
        .setWidget(mChart)//控件
        .addYValue(yVals1)//如果传递一个数据集合就是单柱图
        .addYValue(yVals2)//增加两个就是双柱图
        .XValue(xVals)//增加X轴的数据

 .ColorList(new int[]{Color.rgb(104,241,175),

                         Color.rgb(164,228,251)})//颜色数组Bar的颜色

.setList(newString[]{"消费","支出"})//set(下方文字说明)的数据数组
        .setLimitLines(false)  //是否展示平均线
        .showMean(13.2f)   //平均值
        .groupSpace(0.4)   //没组之间的距离
        .setLocation(BarChartsItem.HCENTER)    //位置


        .show();

横向柱状图


布局:

<includelayout="@layout/default_horizontal_barchart"/>

应用:

@BindView(R.id.mHorizontalBarChart)
HorizontalBarChart mChart;
List<BarEntry> yVals1 =newArrayList<>();
List<String> xVals =newArrayList<>();

 

or(inti = 1;i <9;i++) {
    yVals1.add(newBarEntry(i,i,i));
}
for (intb =0;b <9;b++) {
    xVals.add(b +"");
}

protected voidinitView() {
    HorizontalBarChartItem
            .getInstance()
            .setWidget(mChart)
            .setText("消费")
            .XValue(xVals)
            .YValue(yVals1)
            .show();

 

 以上就是关于图表的使用,资源已经上传CSDN

http://download.csdn.net/download/qq_35970118/10048628