MPAndroidChart Details - Set Data

This issue is about how to add data to the chart

Tips: This issue did not follow the official document to write, purely a method of their own learning process of learning, individuals feel more simple to use than the official document
Previous: MPAndroidChart detailed use - axis (X-axis, Y-axis, the zero line)
next: Details and use --ValueFormatter MPAndroidChart interfaces AxisValueFormatter

FIG fold line (plurality of data)
//首先实例化一个 List  用来储存你的数据
List<Entry>list=new ArrayList<>();          
List<Entry>list2=new ArrayList<>(); //第二条线

//然后向List中添加数据
//其中两个参数对应的分别是   X轴   Y轴
list.add(new Entry(1,10));
list.add(new Entry(2,12));
list.add(new Entry(3,6));
//为第二条线添加数据
list2.add(new Entry(1,8));
list2.add(new Entry(2,10));
list2.add(new Entry(3,7));

//然后创建一个DataSet对象
//list是你这条线的数据  "语文" 是你对这条线的描述
LineDataSet lineDataSet=new LineDataSet(list,"语文");  
//再创建一个DataSet对象,把第二条数据添加进DataSet对象
LineDataSet lineDataSet2=new LineDataSet(list2,"数学");  

//创建Data对象
LineData lineData=new LineData(lineDataSet);//把第一个DataSet对象添加到Data
lineData.addDataSet(lineDataSet2);      //第二个
line.setData(lineData);                //setData()方法把Data传入
Order entry

If the X axis is not the correct data sequence, it may cause some errors, you can use the sort Collections.sort

List<Entry>list=new ArrayList<>();
Collections.sort(list, new EntryXComparator());
Bar chart

FIG bar and line setting data is almost the same form, except when instantiating List is not BarEntry Entry, and a variety of types of bar

//注意<>中间是BarEntry,这是和折线图不同的
List<BarEntry>list=new ArrayList<>();

//添加数据
list.add(new BarEntry(1,7));     //其中两个数字对应的分别是   X轴   Y轴
list.add(new BarEntry(2,10));
list.add(new BarEntry(3,12));
list.add(new BarEntry(4,6));
list.add(new BarEntry(5,3));

BarDataSet barDataSet=new BarDataSet(list,"语文");   //list是你这条线的数据  "语文" 是你对这条线的描述
BarData barData=new BarData(barDataSet);
bar.setData(barData);

data.setBarWidth(0.9f);   //设置柱子的宽度
chart.setFitBars(true);   //使X轴与所有条形适配,防止两侧的柱子显示不全
Bar Group
List<BarEntry>list=new ArrayList<>();
List<BarEntry>list2=new ArrayList<>();

//为第一组添加数据
list.add(new BarEntry(1,5));
list.add(new BarEntry(2,8));
list.add(new BarEntry(3,4));

//为第二组添加数据
list2.add(new BarEntry(1,6));
list2.add(new BarEntry(2,8));
list2.add(new BarEntry(3,5));

BarDataSet barDataSet=new BarDataSet(list,"男");
barDataSet.setColor(Color.RED);    //为第一组柱子设置颜色
BarDataSet barDataSet2=new BarDataSet(list2,"女");
barDataSet2.setColor(Color.BLUE);   //为第二组柱子设置颜色
BarData barData=new BarData(barDataSet);   //加上第一组
barData.addDataSet(barDataSet2);    //加上第二组   (多组也可以用同样的方法)
barchart.setData(barData);

float groupSpace = 0.06f; //群组间的间隔
float barSpace = 0.02f; // 每一个柱状条间隔  计算时需要  *DataSet的数量  这里是2
float barWidth = 0.45f; // 每一个柱状条的宽度   计算时需要  *DataSet的数量  这里是2
// (0.02 + 0.45) * 2 + 0.06 = 1.00  保证最后总和是1就行

//重点:
barData.setBarWidth(barWidth);//柱子的宽度
//三个参数   分别代表   X轴起点     组与组之间的间隔      组内柱子的间隔
barData.groupBars(1f,groupSpace,barSpace);

XAxis xAxis = chart.getXAxis();
xAxis.setCenterAxisLabels(true);//此方法可以让X轴标签数据显示在图组的中心
Stacked bar chart

The bar and almost normal, the difference lies in the different types of data added when the fill parameter
common BarChart two simple float parameters, while FIG stacked is filled in the Y-axis where the data is an array of float
height on the chart thus displayed three pieces of data 10, 20, the overall height is 60

List<BarEntry>list=new ArrayList<>();
list.add(new BarEntry(1,new float[]{10,20,30}));
Pie

Pie charts and other data bit is not the same, i.e., the first parameter is the size of the area occupied by the current data, the second parameter for describing the information of the current area of ​​the pie without the X-axis, the display order data to the order of addition decide

List<PieEntry> list= new ArrayList<>();

list.add(new PieEntry(20, "软件设计"));
list.add(new PieEntry(30, "英语"));
list.add(new PieEntry(35, "数学"));
list.add(new PieEntry(15, "语文"));

PieDataSet pieDataSet= new PieDataSet(list, "饼图");
PieData pieData = new PieData(pieDataSet);
pieChart.setData(pieData );
Data (the DataSet) disposed Color

Color can be provided by two methods, so as to facilitate distinguish between two groups of data

setColor(int color) Set single color, or create arrays in the color inside and add
setColors(ArrayList colors) Color provides a collection of type List
setColors(int [] colors) By using the new Int [] {R.color.red, ...} provided in the form of color data to dataset

Sample code:

lineDataSet.setColor(Color.GREEN);  //折线的颜色

List<Integer>list_color=new ArrayList<>();
list_color.add(Color.RED);
list_color.add(Color.YELLOW);
list_color.add(Color.BLUE);
lineDataSet.setColors(list_color);

lineDataSet.setColors(new int[]{Color.RED,Color.BLUE,Color.YELLOW});
发布了23 篇原创文章 · 获赞 59 · 访问量 8546

Guess you like

Origin blog.csdn.net/qq_44720366/article/details/104734117