2017.10.19
效果
可以添加多條折線(xiàn),動(dòng)態(tài)的獲取加載數(shù)據(jù)。
//畫(huà)圖表工具M(jìn)PAndroidChart
compile'com.github.PhilJay:MPAndroidChart:v3.0.1'
1.在app的build.gradle中添加
2.根據(jù)網(wǎng)上的資料寫(xiě)了一個(gè)DynamicLineChartManager
/**
* Created by wxz 2017/10/16.
*/
public classDynamicLineChartManager {
privateLineChartlineChart;
privateYAxisleftAxis;
privateYAxisrightAxis;
privateXAxisxAxis;
privateLineDatalineData;
privateLineDataSetlineDataSet;
privateListlineDataSets=newArrayList<>();
privateSimpleDateFormatdf=newSimpleDateFormat("HH:mm:ss");//設(shè)置日期格式
privateListtimeList=newArrayList<>();//存儲(chǔ)x軸的時(shí)間
//一條曲線(xiàn)
publicDynamicLineChartManager(LineChart mLineChart, String name,intcolor) {
this.lineChart= mLineChart;
leftAxis=lineChart.getAxisLeft();
rightAxis=lineChart.getAxisRight();
xAxis=lineChart.getXAxis();
initLineChart();
initLineDataSet(name, color);
}
//多條曲線(xiàn)
publicDynamicLineChartManager(LineChart mLineChart, List names, List colors) {
this.lineChart= mLineChart;
leftAxis=lineChart.getAxisLeft();
rightAxis=lineChart.getAxisRight();
xAxis=lineChart.getXAxis();
initLineChart();
initLineDataSet(names, colors);
}
/**
* 初始化LineChar
*/
private voidinitLineChart() {
lineChart.setDrawGridBackground(false);
//顯示邊界
lineChart.setDrawBorders(true);
//折線(xiàn)圖例 標(biāo)簽 設(shè)置
Legend legend =lineChart.getLegend();
legend.setForm(Legend.LegendForm.LINE);
legend.setTextSize(11f);
//顯示位置
legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
legend.setDrawInside(false);
//X軸設(shè)置顯示位置在底部
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setGranularity(1f);
xAxis.setLabelCount(10);
xAxis.setValueFormatter(newIAxisValueFormatter() {
@Override
publicString getFormattedValue(floatvalue, AxisBase axis) {
returntimeList.get((int) value %timeList.size());
}
});
//保證Y軸從0開(kāi)始毫捣,不然會(huì)上移一點(diǎn)
leftAxis.setAxisMinimum(0f);
rightAxis.setAxisMinimum(0f);
}
/**
* 初始化折線(xiàn)(一條線(xiàn))
*
*@paramname
*@paramcolor
*/
private voidinitLineDataSet(String name,intcolor) {
lineDataSet=newLineDataSet(null, name);
lineDataSet.setLineWidth(1.5f);
lineDataSet.setCircleRadius(1.5f);
lineDataSet.setColor(color);
lineDataSet.setCircleColor(color);
lineDataSet.setHighLightColor(color);
//設(shè)置曲線(xiàn)填充
lineDataSet.setDrawFilled(false);
lineDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
lineDataSet.setValueTextSize(10f);
lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
//添加一個(gè)空的 LineData
lineData=newLineData();
lineChart.setData(lineData);
lineChart.invalidate();
}
/**
* 初始化折線(xiàn)(多條線(xiàn))
*
*@paramnames
*@paramcolors
*/
private voidinitLineDataSet(List names, List colors) {
for(inti =0; i < names.size(); i++) {
lineDataSet=newLineDataSet(null, names.get(i));
lineDataSet.setColor(colors.get(i));
lineDataSet.setLineWidth(1.5f);
lineDataSet.setCircleRadius(1.5f);
lineDataSet.setColor(colors.get(i));
lineDataSet.setDrawFilled(true);
lineDataSet.setCircleColor(colors.get(i));
lineDataSet.setHighLightColor(colors.get(i));
lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
lineDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
lineDataSet.setValueTextSize(10f);
lineDataSets.add(lineDataSet);
}
//添加一個(gè)空的 LineData
lineData=newLineData();
lineChart.setData(lineData);
lineChart.invalidate();
}
/**
* 動(dòng)態(tài)添加數(shù)據(jù)(一條折線(xiàn)圖,單個(gè)數(shù)據(jù)添加)
*
*@param number
*/
public voidaddEntry(intnumber) {
//最開(kāi)始的時(shí)候才添加 lineDataSet(一個(gè)lineDataSet 代表一條線(xiàn))
if(lineDataSet.getEntryCount() ==0) {
lineData.addDataSet(lineDataSet);
}
lineChart.setData(lineData);
//避免集合數(shù)據(jù)過(guò)多,及時(shí)清空(做這樣的處理培漏,并不知道有沒(méi)有用溪厘,但還是這樣做了)
if(timeList.size() >11) {
timeList.clear();
}
timeList.add(df.format(System.currentTimeMillis()));
Entry entry =newEntry(lineDataSet.getEntryCount(), number);
lineData.addEntry(entry,0);
//通知數(shù)據(jù)已經(jīng)改變
lineData.notifyDataChanged();
lineChart.notifyDataSetChanged();
//設(shè)置在曲線(xiàn)圖中顯示的最大數(shù)量
lineChart.setVisibleXRangeMaximum(10);
//移到某個(gè)位置
lineChart.moveViewToX(lineData.getEntryCount() -5);
}
/**
* 動(dòng)態(tài)添加數(shù)據(jù)(一條條折線(xiàn)圖,多個(gè)數(shù)據(jù)添加)
*@paramnumbers
*/
public voidaddEntry(List numbers) {
if(lineDataSet.getEntryCount() ==0) {
lineData.addDataSet(lineDataSet);
}
//? ? ? ? if (lineDataSets.get(0).getEntryCount() == 0) {
//? ? ? ? ? ? lineData = new LineData(lineDataSets);
//? ? ? ? ? ? lineChart.setData(lineData);
//? ? ? ? }
if(timeList.size() >11) {
timeList.clear();
}
timeList.add(df.format(System.currentTimeMillis()));
for(inti =0; i < numbers.size(); i++) {
Entry entry =newEntry(lineDataSet.getEntryCount(), numbers.get(i));
lineData.addEntry(entry,0);
}
lineData.notifyDataChanged();
lineChart.notifyDataSetChanged();
lineChart.setVisibleXRangeMaximum(50);
lineChart.moveViewToX(lineData.getEntryCount() -5);
}
/**
* 設(shè)置Y軸值
*@parammax
*@parammin
*@paramlabelCount
*/
public voidsetYAxis(floatmax,floatmin,intlabelCount) {
if(max < min) {
return;
}
leftAxis.setAxisMaximum(max);
leftAxis.setAxisMinimum(min);
leftAxis.setLabelCount(labelCount,false);
rightAxis.setAxisMaximum(max);
rightAxis.setAxisMinimum(min);
rightAxis.setLabelCount(labelCount,false);
lineChart.invalidate();
}
/**
* 設(shè)置高限制線(xiàn)
*
*@paramhigh
*@paramname
*/
public voidsetHightLimitLine(floathigh, String name,intcolor) {
if(name ==null) {
name ="高限制線(xiàn)";
}
LimitLine hightLimit =newLimitLine(high, name);
hightLimit.setLineWidth(4f);
hightLimit.setTextSize(10f);
hightLimit.setLineColor(color);
hightLimit.setTextColor(color);
leftAxis.addLimitLine(hightLimit);
lineChart.invalidate();
}
/**
* 設(shè)置低限制線(xiàn)
*
*@paramlow
*@paramname
*/
public voidsetLowLimitLine(intlow, String name) {
if(name ==null) {
name ="低限制線(xiàn)";
}
LimitLine hightLimit =newLimitLine(low, name);
hightLimit.setLineWidth(4f);
hightLimit.setTextSize(10f);
leftAxis.addLimitLine(hightLimit);
lineChart.invalidate();
}
/**
* 設(shè)置描述信息
*
*@paramstr
*/
public voidsetDescription(String str) {
Description description =newDescription();
description.setText(str);
lineChart.setDescription(description);
lineChart.invalidate();
}
}
在Activity中的使用
private voidinitLineChart() {
LineChart mLineChart = (LineChart) findViewById(R.id.ppg_chart);
//折線(xiàn)名稱(chēng)
names.add("測(cè)試");
//? ? ? ? names.add("測(cè)試二");
//? ? ? ? names.add("測(cè)試三");
//折線(xiàn)顏色
colour.add(Color.CYAN);
//? ? ? ? colour.add(Color.GREEN);
//? ? ? ? colour.add(Color.BLUE);
dynamicLineChartManager1=newDynamicLineChartManager(mLineChart,names.get(0),colour.get(0));
//? ? ? ? dynamicLineChartManager1.setYAxis(100, 0, 10);
//死循環(huán)添加數(shù)據(jù)
new Thread(newRunnable() {
@Override
public voidrun() {
while(true) {
try{
Thread.sleep(1000);
}catch(InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(newRunnable() {
@Override
public voidrun() {
//? ? ? ? ? ? ? ? ? ? ? ? ? ? list.add((int) (Math.random() * 50) + 10);
//? ? ? ? ? ? ? ? ? ? ? ? ? ? list.add((int) (Math.random() * 80) + 10);
//? ? ? ? ? ? ? ? ? ? ? ? ? ? list.add((int) (Math.random() * 100));
//? ? ? ? ? ? ? ? ? ? ? ? ? ? list.add((int) (Math.random() * 50) + 10);
//? ? ? ? ? ? ? ? ? ? ? ? ? ? list.add((int) (Math.random() * 80) + 10);
//? ? ? ? ? ? ? ? ? ? ? ? ? ? list.add((int) (Math.random() * 200));
list.add((int) (Math.random() *50) +10);
list.add((int) (Math.random() *80) +10);
list.add((int) (Math.random() *100));
list.add((int) (Math.random() *50) +10);
dynamicLineChartManager1.addEntry(list);
list.clear();
}
});
}
}
}).start();
}