【Android开发--新手必看篇】图表汇总(MPAndroidChart2.1.5、MPAndroidChart3.0.3、HelloChart1.5.8)

    技术2022-07-11  99

    Android笔记

    ​ ——图表 【若对该知识点有更多想了解的,欢迎私信博主~~】

    图表:MPAndroidChart(2.1.5版)

    通用属性&类:
    控件属性:
    属性说明setDescription()添加描述setDescriptionPosition()设置描述的显示位置setDescriptionTextSize()设置描述的字体大小setTouchEnabled()是否支持触控手势setBackgroundColor()设置图表外层背景颜色setGridBackgroundColor()设置图表内层背景颜色
    set属性(值):
    属性说明setValueTextSize()设置值的字体大小setColors()设置颜色setDrawValues()是否显示图表上的数值setVisible()是否显示图setDrawFilled()内部是否填充setFillColor()内部填充颜色 //自定义值的显示文本 set.setValueFormatter(new ValueFormatter() { @Override public String getFormattedValue(float v, Entry entry, int i, ViewPortHandler viewPortHandler) { String n = String.valueOf(v); return n+"%"; } });
    X轴属性:

    获取X轴

    XAxis xAxis=lineChart.getXAxis(); 属性说明setPosition()设置X轴出现位置(//xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);)setTextSize()设置X轴的字体setDrawAxisLines()设置绘制轴线setDrawGridLines()设置绘制网格线setEnabled()设置是否显示X轴

    Y轴属性:

    获取Y轴

    YAxis yAxis=lineChart.getAxisRight();//右侧Y轴 YAxis yAxis1=lineChart.getAxisLeft();//左侧Y轴 属性说明setEnabled()设置是否显示setTextSize()设置Y轴字体大小setStartAtZero()设置是否从0开始setAxisMinValue()设置最小显示的值setAxisMaxValue()设置最大显示的值setPosition()设置显示位置setLabelCount()设置一共显示几个坐标值setYOffset()y轴的值向下偏离多少setDrawGridLines()设置绘制网格线setDrawAxisLines()设置绘制轴线 //自定义Y轴的显示文本样式 yAxis.setValueFormatter(new YAxisValueFormatter() { @Override public String getFormattedValue(float v, YAxis yAxis) { String n=String.valueOf(v); return n+"%"; } });
    图例属性:
    //获取图例 Legend legend=pieChart.getLegend(); 属性说明setFormSize()设置图例框的大小setTextSize()设置图例的文字大小setPosition()设置图例的显示位置setEnabled()设置是否显示图例setXEntrySpace()设置图例与图例X轴之间的间距setYEntrySpace()设置图例与图例Y轴之间的间距setFormToTextSpace()设置图例与文字之间的间距 //自定义图例 List<Integer> colors=new ArrayList<>(); colors.add(Color.parseColor("#ff0000")); colors.add(Color.parseColor("#00ff00")); List<String> msg=new ArrayList<>(); msg.add("男"); msg.add("女"); Legend legend = chart.getLegend(); legend.setCustom(colors,msg); legend.setTextSize(20); legend.setPosition(Legend.LegendPosition.RIGHT_OF_CHART_CENTER);
    使用:

    将jar包引入libs下,并加载(Add As Library)

    拖入控件

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff" android:orientation="vertical"> //饼状图 <com.github.mikephil.charting.charts.PieChart android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/pc1"> </com.github.mikephil.charting.charts.PieChart> //柱状图 <com.github.mikephil.charting.charts.BarChart android:id="@+id/bc1" android:layout_width="match_parent" android:layout_height="match_parent"> </com.github.mikephil.charting.charts.BarChart> //横向柱状图 <com.github.mikephil.charting.charts.HorizontalBarChart android:id="@+id/hbc" android:layout_width="match_parent" android:layout_height="match_parent"> </com.github.mikephil.charting.charts.HorizontalBarChart> //折线图 <com.github.mikephil.charting.charts.LineChart android:id="@+id/lc1" android:layout_width="match_parent" android:layout_height="match_parent"> </com.github.mikephil.charting.charts.LineChart> //雷达图 <com.github.mikephil.charting.charts.RadarChart android:id="@+id/rc" android:layout_width="match_parent" android:layout_height="match_parent"> </com.github.mikephil.charting.charts.RadarChart> </LinearLayout>

    初始化图标并运用

    package com.example.sirl.practice2; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import com.github.mikephil.charting.charts.BarChart; import com.github.mikephil.charting.charts.HorizontalBarChart; import com.github.mikephil.charting.charts.LineChart; import com.github.mikephil.charting.charts.PieChart; import com.github.mikephil.charting.data.BarData; import com.github.mikephil.charting.data.BarDataSet; import com.github.mikephil.charting.data.BarEntry; import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.data.LineData; import com.github.mikephil.charting.data.LineDataSet; import com.github.mikephil.charting.data.PieData; import com.github.mikephil.charting.data.PieDataSet; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { PieChart pieChart; BarChart barChart; HorizontalBarChart horizontalBarChart; LineChart lineChart; RadarChart radarChart; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //绑定控件 pieChart=findViewById(R.id.pc1); barChart=findViewById(R.id.bc1); horizontalBarChart=findViewById(R.id.hbc); lineChart=findViewById(R.id.lc1); radarChart=findViewById(R.id.rc); pieChart(); barChart(); horizontalBarChart(); lineChart(); radarChart(); } //饼形图 public void pieChart() { //建立存储x轴和y轴的数值集 ArrayList<String> xVals=new ArrayList<>(); ArrayList<Entry> yVals=new ArrayList<>(); xVals.add("男"); xVals.add("女"); yVals.add(new Entry(40,0)); yVals.add(new Entry(60,0)); //y轴数值集合 PieDataSet set=new PieDataSet(yVals," "); //x轴与y轴对应 PieData data=new PieData(xVals,set); //添加到控件中 pieChart.setData(data); } //柱状图 public void barChart() { //建立存储x轴和y轴的数值集 ArrayList<String> xVals=new ArrayList<>(); ArrayList<BarEntry> yVals=new ArrayList<>(); xVals.add("男"); xVals.add("女"); yVals.add(new BarEntry(40,0)); yVals.add(new BarEntry(60,1)); //y轴数值集合 BarDataSet set = new BarDataSet(yVals," "); //x轴与y轴对应 BarData data=new BarData(xVals,set); //添加到控件中 barChart.setData(data); } //横向柱状图 public void horizontalBarChart() { //建立存储x轴和y轴的数值集 ArrayList<String> xVals=new ArrayList<>(); ArrayList<BarEntry> yVals=new ArrayList<>(); xVals.add("男"); xVals.add("女"); yVals.add(new BarEntry(40,0)); yVals.add(new BarEntry(60,1)); //y轴数值集合 BarDataSet set = new BarDataSet(yVals," "); //x轴与y轴对应 BarData data=new BarData(xVals,set); //添加到控件中 horizontalBarChart.setData(data); } //折线图 public void lineChart() { //建立存储x轴和y轴的数值集 ArrayList<String> xVals=new ArrayList<>(); ArrayList<Entry> yVals=new ArrayList<>(); xVals.add("男"); xVals.add("女"); yVals.add(new Entry(40,0)); yVals.add(new Entry(60,1)); //y轴数值集合 LineDataSet set = new LineDataSet(yVals," "); //x轴与y轴对应 LineData data=new LineData(xVals,set); //添加到控件中 lineChart.setData(data); } //雷达图 public void radarChart() { //建立存储x轴和y轴的数值集 ArrayList<String> xVals=new ArrayList<>(); ArrayList<Entry> yVals=new ArrayList<>(); xVals.add("男"); xVals.add("女"); yVals.add(new Entry(40,0)); yVals.add(new Entry(60,1)); //y轴数值集合 RadarDataSet set = new RadarDataSet(yVals," "); //x轴与y轴对应 RadarData data=new RadarData(xVals,sets); //添加到控件中 radarChart.setData(data); } }
    动态更新图表
    //获取Y值集合中的值 set.getEntryForXIndex(i).getVal(); //获取X轴集合中的坐标值 data.getXVals().get(i); //移除Y值集合中第一个Y值 set.removeFirst(); //移除X轴坐标中的第一个 data.removeXValue(i); //清空Y值 set.clear(); //向Y值集合中添加值 set.addEntry(new Entry(a.get(i),i)); //向X轴中添加一个坐标 data.addXValue(b.get(i)); //当值改变时更新 set.notifyDataSetChanged(); //让DataSet知道数据发生变化 data.notifyDataChanged();// 让data知道数据发生变化 lineChart.invalidate();// 刷新
    特殊表
    堆积柱状图
    yVals.add(new Entry(Float集合, X轴坐标));
    对比柱状图
    final Data data = new Data(xVals, sets);//sets为set集合
    特殊属性:
    饼形图
    属性说明setHoleRadius()设置内部圆的半径setTransparentCircleRadius()设置内部圆的颜色透明度

    https://blog.csdn.net/zcmain/article/details/53611245

    柱状图
    //堆积柱状图 yVals.add(new BarEntry(new float[]{Float.parseFloat(String.valueOf(a[0])),Float.parseFloat(String.valueOf(a[2]))},0)); yVals.add(new BarEntry(new float[]{Float.parseFloat(String.valueOf(a[1])),Float.parseFloat(String.valueOf(a[3]))},1));
    折线图
    属性说明setCircleSize()设置折线图折点圆圈大小setCircleColorHole()设置折线图折点圆圈内部颜色setCircleColor()设置折线图折点圆圈颜色setColor()设置折线图线的颜色setLineWidth()设置折线图线的宽度

    图表:MPAndroidChart(3.0.3版)

    通用属性&类:
    控件属性:
    属性说明setTouchEnabled()是否支持触控手势setBackgroundColor()设置图表外层背景颜色setGridBackgroundColor()设置图表内层背景颜色
    set属性(值):
    属性说明setValueTextSize()设置值的字体大小setColors()设置颜色setDrawValues()是否显示图表上的数值setVisible()是否显示图setDrawFilled()内部是否填充setFillColor()内部填充颜色 //自定义值的显示文本 set.setValueFormatter(new IValueFormatter() { @Override public String getFormattedValue(float v, Entry entry, int i, ViewPortHandler viewPortHandler) { String n = String.valueOf(v); return n+"%"; } });
    X轴属性:

    获取X轴

    XAxis xAxis=lineChart.getXAxis(); 属性说明setPosition()设置X轴出现位置(//xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);)setTextSize()设置X轴的字体setDrawAxisLines()设置绘制轴线setDrawGridLines()设置绘制网格线setEnabled()设置是否显示X轴setLabelRotationAngle()设置标签 //自定义X轴的显示文本样式 xAxis.setValueFormatter(new IAxisValueFormatter() { @Override public String getFormattedValue(float value, AxisBase axis) { return null; } });
    Y轴属性:

    获取Y轴

    YAxis yAxis=lineChart.getAxisRight();//右侧Y轴 YAxis yAxis1=lineChart.getAxisLeft();//左侧Y轴 属性说明setEnabled()设置是否显示setTextSize()设置Y轴字体大小setStartAtZero()设置是否从0开始setAxisMinValue()设置最小显示的值setAxisMaxValue()设置最大显示的值setPosition()设置显示位置setLabelCount()设置一共显示几个坐标值setYOffset()y轴的值向下偏离多少setDrawAxisLines()设置绘制轴线setDrawGridLines()设置绘制网格线 //自定义Y轴的显示文本样式 yAxis.setValueFormatter(new IAxisValueFormatter() { @Override public String getFormattedValue(float v, YAxis yAxis) { String n=String.valueOf(v); return n+"%"; } });
    图例属性:
    //获取图例 Legend legend=lineChart.getLegend(); 属性说明setFormSize()设置图例框的大小setTextSize()设置图例的文字大小setPosition()设置图例的显示位置setEnabled()设置是否显示图例setXEntrySpace()设置图例与图例X轴之间的间距setYEntrySpace()设置图例与图例Y轴之间的间距setFormToTextSpace()设置图例与文字之间的间距 //自定义图例 List<Integer> colors=new ArrayList<>(); colors.add(Color.parseColor("#ff0000")); colors.add(Color.parseColor("#00ff00")); List<String> msg=new ArrayList<>(); msg.add("男"); msg.add("女"); Legend legend = chart.getLegend(); legend.setCustom(colors,msg); legend.setTextSize(20); legend.setPosition(Legend.LegendPosition.RIGHT_OF_CHART_CENTER);
    描述属性:
    //获取描述 Description description=lineChart.getDescription(); 属性说明setText()设置描述内容setTextSize()设置描述字体大小setTextColor()设置描述字体颜色setTypeface()设置描述字体样式setEnabled()设置是否显示描述setPosition()设置描述显示位置
    使用:

    将jar包引入libs下,并加载(Add As Library)

    拖入控件

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff" android:orientation="vertical"> //饼状图 <com.github.mikephil.charting.charts.PieChart android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/pc1"> </com.github.mikephil.charting.charts.PieChart> //柱状图 <com.github.mikephil.charting.charts.BarChart android:id="@+id/bc1" android:layout_width="match_parent" android:layout_height="match_parent"> </com.github.mikephil.charting.charts.BarChart> //横向柱状图 <com.github.mikephil.charting.charts.HorizontalBarChart android:id="@+id/hbc" android:layout_width="match_parent" android:layout_height="match_parent"> </com.github.mikephil.charting.charts.HorizontalBarChart> //折线图 <com.github.mikephil.charting.charts.LineChart android:id="@+id/lc1" android:layout_width="match_parent" android:layout_height="match_parent"> </com.github.mikephil.charting.charts.LineChart> //雷达图 <com.github.mikephil.charting.charts.RadarChart android:id="@+id/rc" android:layout_width="match_parent" android:layout_height="match_parent"> </com.github.mikephil.charting.charts.RadarChart> </LinearLayout>

    初始化图标并运用

    package com.lenovo.smarttraffic; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import com.github.mikephil.charting.charts.BarChart; import com.github.mikephil.charting.charts.HorizontalBarChart; import com.github.mikephil.charting.charts.LineChart; import com.github.mikephil.charting.charts.PieChart; import com.github.mikephil.charting.charts.RadarChart; import com.github.mikephil.charting.data.BarData; import com.github.mikephil.charting.data.BarDataSet; import com.github.mikephil.charting.data.BarEntry; import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.data.LineData; import com.github.mikephil.charting.data.LineDataSet; import com.github.mikephil.charting.data.PieData; import com.github.mikephil.charting.data.PieDataSet; import com.github.mikephil.charting.data.PieEntry; import com.github.mikephil.charting.data.RadarData; import com.github.mikephil.charting.data.RadarDataSet; import com.github.mikephil.charting.data.RadarEntry; import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; import com.github.mikephil.charting.interfaces.datasets.IRadarDataSet; import java.util.ArrayList; import java.util.List; public class Main2Activity extends AppCompatActivity { private PieChart pc1; private BarChart bc1; private HorizontalBarChart hbc; private LineChart lc1; private RadarChart rc; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); //绑定控件 initView(); pieChart(); barChart(); horizontalBarChart(); lineChart(); radarChart(); } private void pieChart() { //创建Y轴 List<PieEntry> yVals=new ArrayList<>(); for (int i = 0; i < 5; i++) { yVals.add(new PieEntry(i)); } //创建数值集 PieDataSet set=new PieDataSet(yVals,""); //创建数据 PieData data=new PieData(set); //将数据填充至图表中 pc1.setData(data); } private void barChart() { //创建数据集集合 List<IBarDataSet> sets=new ArrayList<>(); for (int i = 0; i < 2; i++) { //创建单个数据集合 List<BarEntry> yVals=new ArrayList<>(); for (int j = 0; j < 4; j++) { yVals.add(new BarEntry(j,i+j)); } //将单个数据集合填充至数据集 BarDataSet set=new BarDataSet(yVals,""); //将数据集填充至数据集集合 sets.add(set); } //创建数据 BarData data=new BarData(sets); //将数据填充至图表中 bc1.setData(data); } private void horizontalBarChart() { //创建数据集集合 List<IBarDataSet> sets=new ArrayList<>(); for (int i = 0; i < 2; i++) { //创建单个数据集合 List<BarEntry> yVals=new ArrayList<>(); for (int j = 0; j < 4; j++) { yVals.add(new BarEntry(j,i+j)); } //将单个数据集合填充至数据集 BarDataSet set=new BarDataSet(yVals,""); //将数据集填充至数据集集合 sets.add(set); } //创建数据 BarData data=new BarData(sets); //将数据填充至图表中 hbc.setData(data); } private void lineChart() { //创建数据集集合 List<ILineDataSet> sets=new ArrayList<>(); for (int i = 0; i < 2; i++) { //创建单个数据集合 List<Entry> yVals=new ArrayList<>(); for (int j = 0; j < 4; j++) { yVals.add(new Entry(j,i+j)); } //将单个数据集合填充至数据集 LineDataSet set=new LineDataSet(yVals,""); //将数据集填充至数据集集合 sets.add(set); } //创建数据 LineData data=new LineData(sets); //将数据填充至图表中 lc1.setData(data); } private void radarChart() { //创建数据集集合 List<IRadarDataSet> sets=new ArrayList<>(); for (int i = 0; i < 2; i++) { //创建单个数据集合 List<RadarEntry> yVals=new ArrayList<>(); for (int j = 0; j < 4; j++) { yVals.add(new RadarEntry(j,i+j)); } //将单个数据集合填充至数据集 RadarDataSet set=new RadarDataSet(yVals,""); //将数据集填充至数据集集合 sets.add(set); } //创建数据 RadarData data=new RadarData(sets); //将数据填充至图表中 rc.setData(data); } private void initView() { pc1 = (PieChart) findViewById(R.id.pc1); bc1 = (BarChart) findViewById(R.id.bc1); hbc = (HorizontalBarChart) findViewById(R.id.hbc); lc1 = (LineChart) findViewById(R.id.lc1); rc = (RadarChart) findViewById(R.id.rc); } }
    动态更新图表
    //获取set数据集 for (int i = 0; i < sets.size(); i++) { LineDataSet set= (LineDataSet) sets.get(i); } //获取Y值集合中的值 set.getEntryForXIndex(i).getVal(); //获取X轴集合中的坐标值 data.getXVals().get(i); //移除Y值集合中第一个Y值 set.removeFirst(); //移除X轴坐标中的第一个 data.removeXValue(i); //清空Y值 set.clear(); //向Y值集合中添加值 set.addEntry(new Entry(a.get(i),i)); //向X轴中添加一个坐标 data.addXValue(b.get(i)); //当值改变时更新 set.notifyDataSetChanged(); //让DataSet知道数据发生变化 data.notifyDataChanged();// 让data知道数据发生变化 lineChart.invalidate();// 刷新
    特殊表
    堆积柱状图
    yVals.add(new Entry(X轴坐标,Float集合));
    对比柱状图
    final Data data = new Data(sets);//sets为set集合
    特殊属性:
    饼形图
    属性说明setHoleRadius()设置内部圆的半径setTransparentCircleRadius()设置内部圆的颜色透明度

    https://blog.csdn.net/zcmain/article/details/53611245

    柱状图
    //堆积柱状图 yVals.add(new BarEntry(0,new float[]{Float.parseFloat(String.valueOf(a[0])),Float.parseFloat(String.valueOf(a[2]))})); yVals.add(new BarEntry(1,new float[]{Float.parseFloat(String.valueOf(a[1])),Float.parseFloat(String.valueOf(a[3]))}));
    折线图
    属性说明setCircleSize()设置折线图折点圆圈大小setCircleColorHole()设置折线图折点圆圈内部颜色setCircleColor()设置折线图折点圆圈颜色setColor()设置折线图线的颜色setLineWidth()设置折线图线的宽度

    图表:HelloChart(1.5.8版)

    通用属性&类
    标签(值):data
    属性说明setHasLabels()设置是否显示标签setHasLabelsOutside()设置标签是否在外侧显示setHasLabelsOnlyForSelected()设置标签是否选中才显示setValueLabelsTextColor()设置标签颜色setValueLabelTextSize()设置标签大小setValueLabelTypeface()设置标签样式setValueLabelBackgroundColor()设置标签背景颜色setValueLabelBackgroundEnabled()设置是否启动标签背景setAxisXBottom()设置底部X轴为哪个轴(其余轴同理)
    坐标轴
    //创建坐标轴 Axis axisX = new Axis(); //给图表添加坐标轴 data.setAxisXBottom(axisX); 属性说明setName设置坐标轴名称setTextColor设置坐标轴文字颜色setTextSize设置坐标轴文字大小setTypeface设置坐标轴文字样式setHasTiltedLabels设置坐标轴文字倾斜setHasLines设置是否有网格线setLineColor设置网格线的颜色setMaxLabelChars设置标签数setInside设置坐标轴是否在里面setAutoGenerated设置坐标轴是否自动生成
    图表数据点击事件
    //单击事件 bubbleChartView.setOnValueTouchListener(new BubbleChartOnValueSelectListener() { @Override public void onValueSelected(int i, BubbleValue bubbleValue) { } @Override public void onValueDeselected() { } });
    使用:

    将jar包引入libs下,并加载(Add As Library)

    拖入控件

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff" android:orientation="vertical"> //饼状图 <lecho.lib.hellocharts.view.PieChartView android:id="@+id/pcv" android:layout_width="match_parent" android:layout_height="100dp" /> //柱状图 <lecho.lib.hellocharts.view.ColumnChartView android:id="@+id/ccv" android:layout_width="match_parent" android:layout_height="100dp" /> //线形图 <lecho.lib.hellocharts.view.LineChartView android:id="@+id/lcv" android:layout_width="match_parent" android:layout_height="100dp" /> //柱线图 <lecho.lib.hellocharts.view.ComboLineColumnChartView android:id="@+id/clccv" android:layout_width="match_parent" android:layout_height="100dp" /> //气泡图 <lecho.lib.hellocharts.view.BubbleChartView android:id="@+id/bcv" android:layout_width="match_parent" android:layout_height="100dp" /> </LinearLayout>

    初始化图标并运用

    package com.example.sirl.demo1025.HelloChart; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import com.example.sirl.demo1025.R; import java.util.ArrayList; import java.util.List; import lecho.lib.hellocharts.model.BubbleChartData; import lecho.lib.hellocharts.model.BubbleValue; import lecho.lib.hellocharts.model.Column; import lecho.lib.hellocharts.model.ColumnChartData; import lecho.lib.hellocharts.model.ComboLineColumnChartData; import lecho.lib.hellocharts.model.Line; import lecho.lib.hellocharts.model.LineChartData; import lecho.lib.hellocharts.model.PieChartData; import lecho.lib.hellocharts.model.PointValue; import lecho.lib.hellocharts.model.SliceValue; import lecho.lib.hellocharts.model.SubcolumnValue; import lecho.lib.hellocharts.view.BubbleChartView; import lecho.lib.hellocharts.view.ColumnChartView; import lecho.lib.hellocharts.view.ComboLineColumnChartView; import lecho.lib.hellocharts.view.LineChartView; import lecho.lib.hellocharts.view.PieChartView; public class MainActivity extends AppCompatActivity { private PieChartView pcv; private ColumnChartView ccv; private LineChartView lcv; private ComboLineColumnChartView clccv; private BubbleChartView bcv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //绑定控件 initView(); pieChartView(); columnChartView(); lineChartView(); comboLineColumnChartView(); bubbleChartView(); } private void pieChartView() { //创建分片集合 List<SliceValue> sliceValueList=new ArrayList<>(); for (int i = 0; i < 5; i++) { //创建分片 SliceValue sliceValue=new SliceValue(i); /* * setValue 设置分片数值 * setColor 设置分片颜色 */ //将分片添加到分片集合中 sliceValueList.add(sliceValue); } //将分片集合添加到数据中 PieChartData pieChartData=new PieChartData(sliceValueList); //将数据添加到图表中 pcv.setPieChartData(pieChartData); } private void columnChartView() { //创建柱状图分区集合 List<Column> columnList=new ArrayList<>(); for (int i = 0; i < 5; i++) { //创建分区柱状集合 List<SubcolumnValue> subcolumnValueList=new ArrayList<>(); for (int j = 0; j < 2; j++) { //创建单个柱状 SubcolumnValue subcolumnValue=new SubcolumnValue(i); /* * setValue 设置分片数值 * setColor 设置分片颜色 */ //将单个柱状添加到分区柱状集合中 subcolumnValueList.add(subcolumnValue); } //创建分区 Column column=new Column(subcolumnValueList); /* * setHasLabels 设置是否显示标签 * setHasLabelsOnlyForSelected 是否选中才显示标签 */ //将分区添加到分区集合中 columnList.add(column); } //将分区集合添加到数据中 ColumnChartData columnChartData=new ColumnChartData(columnList); //将数据添加到图表中 ccv.setColumnChartData(columnChartData); } private void lineChartView() { //创建线性图分区集合 List<Line> lineList=new ArrayList<>(); for (int i = 0; i < 2; i++) { //创建分区线形集合 List<PointValue> pointValueList=new ArrayList<>(); for (int j = 0; j < 5; j++) { //创建单个点 PointValue pointValue=new PointValue(j,j); /* * setValue 设置分片数值 * setColor 设置分片颜色 */ //将点添加到分区线形集合中 pointValueList.add(pointValue); } //创建分区 Line line=new Line(pointValueList); /* * setHasPoints 设置是否有点 * setPointColor 设置点的颜色 * setPointRadius 设置点的大小 * setShape 设置点的样式 * setHasLines 设置是否有折线 * setColor 设置线条的颜色 * setCubic 设置线条是否圆滑 * setStrokeWidth 设置线的宽度 * setHasLabels 设置是否显示标签 * setHasLabelsOnlyForSelected 是否选中才显示标签 * setSquare 设置三角板直角化 */ //将分区添加到分区集合中 lineList.add(line); } //将分区集合添加到数据中 LineChartData lineChartData=new LineChartData(lineList); //将数据添加到图表中 lcv.setLineChartData(lineChartData); } private void comboLineColumnChartView() { //创建柱状图分区集合 List<Column> columnList=new ArrayList<>(); for (int i = 0; i < 5; i++) { //创建分区柱状集合 List<SubcolumnValue> subcolumnValueList=new ArrayList<>(); for (int j = 0; j < 2; j++) { //创建单个柱状 SubcolumnValue subcolumnValue=new SubcolumnValue(i); /* * setValue 设置分片数值 * setColor 设置分片颜色 */ //将单个柱状添加到分区柱状集合中 subcolumnValueList.add(subcolumnValue); } //创建分区 Column column=new Column(subcolumnValueList); //将分区添加到分区集合中 columnList.add(column); } //创建线性图分区集合 List<Line> lineList=new ArrayList<>(); for (int i = 0; i < 2; i++) { //创建分区线形集合 List<PointValue> pointValueList=new ArrayList<>(); for (int j = 0; j < 5; j++) { //创建单个点 PointValue pointValue=new PointValue(j,j); /* * setValue 设置分片数值 * setColor 设置分片颜色 */ //将点添加到分区线形集合中 pointValueList.add(pointValue); } //创建分区 Line line=new Line(pointValueList); //将分区添加到分区集合中 lineList.add(line); } //将柱状图分区集合添加到柱状图数据中 ColumnChartData columnChartData=new ColumnChartData(columnList); //将线形图分区集合添加到线形图数据中 LineChartData lineChartData=new LineChartData(lineList); //柱状图数据和线形图数据添加到柱线图数据中 ComboLineColumnChartData comboLineColumnChartData=new ComboLineColumnChartData(columnChartData,lineChartData); //将柱线图数据添加到图表中 clccv.setComboLineColumnChartData(comboLineColumnChartData); } private void bubbleChartView() { List<BubbleValue> bubbleValueList=new ArrayList<>(); for (int i = 0; i < 5; i++) { BubbleValue bubbleValue=new BubbleValue(i,i,i);//设置气泡的横纵坐标x、y,z为气泡的半径 /* * setValue 设置分片数值 * setColor 设置分片颜色 * setShape 设置分片形状 */ bubbleValueList.add(bubbleValue); } BubbleChartData bubbleChartData=new BubbleChartData(bubbleValueList); bcv.setBubbleChartData(bubbleChartData); } private void initView() { pcv = (PieChartView) findViewById(R.id.pcv); ccv = (ColumnChartView) findViewById(R.id.ccv); lcv = (LineChartView) findViewById(R.id.lcv); clccv = (ComboLineColumnChartView) findViewById(R.id.clccv); bcv = (BubbleChartView) findViewById(R.id.bcv); } }
    动态更新
    Timer timer=new Timer(); final Handler handler=new Handler(); timer.schedule(new TimerTask() { @Override public void run() { handler.post(new Runnable() { @Override public void run() { //数据清空(其余同理) data.getValues().clear(); List<SliceValue> list = new ArrayList<>(); for (int i = 0; i < 6; i++) { list.add(new SliceValue(i); } //重新添加数据 data.setValues(list); //将数据绑定到控件中 pieChartView.setPieChartData(data); } }); } },0,3000);
    特殊属性:
    饼状图
    属性说明setSlicesSpacing()设置间隔setCenterText1()设置中心文字1setCenterText2()设置中心文字2setCenterText1Color()设置颜色setCenterText1FontSize()设置字体大小setCenterText1Typeface()设置字体样式setCenterCircleColor()设置中心圆颜色setHasCenterCircle()设置是否有中心圆(承载中心文字,中心样式)
    柱状图
    属性说明setStacked()false 横向排列; true 竖向排列
    气泡图
    属性说明setBubbleScale()设置气泡间距
    Processed: 0.010, SQL: 9