Android之MPAndroidChart的动态柱形图
导入MPAndroidChart的jar包以后就写个工具类:package itsoha.com.tffic.Utils;import android.content.Context;import android.graphics.Color;import com.github.mikephil.charting.charts.BarChart;import com.github.mi...
·
导入MPAndroidChart的jar包以后就写个工具类:
package itsoha.com.tffic.Utils;
import android.content.Context;
import android.graphics.Color;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import java.util.ArrayList;
import java.util.Random;
public class BarEngine {
private String title;
private int max;
private BarChart mChart;
public BarEngine(String title) {
this.title = title;
}
public BarChart getView(Context context, int max) {
this.max = max;
mChart = new BarChart(context);
mChart.setDescription("");
mChart.setScaleEnabled(false);
BarData data = new BarData();
data.setValueTextColor(Color.BLACK);
mChart.setData(data);
Legend legend = mChart.getLegend();
legend.setTextSize(25f);
XAxis xAxis = mChart.getXAxis();
xAxis.setTextColor(Color.BLACK);
xAxis.setTextSize(25f);
xAxis.setDrawGridLines(false);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
YAxis axisLeft = mChart.getAxisLeft();
axisLeft.setDrawGridLines(false);
axisLeft.setAxisMinValue(0);
axisLeft.setAxisMinValue(max);
YAxis right = mChart.getAxisRight();
right.setDrawGridLines(false);
return mChart;
}
public void update(int value) {
if (value <= 0) {
value = new Random().nextInt(max);
}
BarData data = mChart.getBarData();
BarDataSet dataSet = data.getDataSetByIndex(0);
if (dataSet == null) {
dataSet = creaSet();
data.addDataSet(dataSet);
}
//显示当前时间
data.addXValue(MyUtils.getCurrentTime());
BarEntry barEntry = new BarEntry(value,dataSet.getEntryCount());
data.addEntry(barEntry,0);
mChart.notifyDataSetChanged();
mChart.setVisibleXRangeMaximum(5);
mChart.moveViewToX(mChart.getXValCount()-5);
}
private BarDataSet creaSet() {
BarDataSet dataSet = new BarDataSet(new ArrayList<BarEntry>(), title);
dataSet.setColor(Color.GREEN);
dataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
return dataSet;
}
}
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献1条内容
所有评论(0)