MPAndroidChart---曲线示例(提供完整代码)
2020-07-20 本文已影响0人
你的益达233
需求:绘制12个月份的温度曲线
效果图:
Screenshot_20200720_171342_com.cong.mpchartdemo.jpg完整源码:
public class LineChartActivity extends AppCompatActivity {
private LineChart mLineChart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_line_chart);
mLineChart = findViewById(R.id.lineChart);
/**
* 需求:绘制12个月份的温度曲线
*/
List<Float> temperatures = Arrays.asList(20f, 24f,30f,40f,50f,60f,40f,20f,80f,40f,20f,80f);
final List<String> xList = Arrays.asList("一月", "二月","三月","四月","五月", "六月","七月","八月","九月", "十月","十一月","十二月");
//设置曲线整体的配置
mLineChart.setNoDataText("暂无数据");
//设置绘制动画
mLineChart.animateXY(1000, 1000);
//隐藏说明
mLineChart.getDescription().setEnabled(false);
//设置X轴
XAxis xAxis = mLineChart.getXAxis();
xAxis.setAxisMinimum(0f);
xAxis.setLabelCount(12,true);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
//设置要不要X轴的网格,就是网格的竖线
xAxis.setDrawGridLines(false);
xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return xList.get((int)value);
}
});
//设置Y轴
YAxis rightYAxis = mLineChart.getAxisRight();
//隐藏右边Y轴
rightYAxis.setEnabled(false);
YAxis leftYAxis = mLineChart.getAxisLeft();
//设置它可以从0开始,并且Y轴开始就不会高于X轴
leftYAxis.setAxisMinimum(0f);
//leftYAxis.setAxisMaximum(100f);
//设置数据
List<Entry> entries = new ArrayList<>();
for (int i = 0; i < 12; i++) {
entries.add(new Entry(i, temperatures.get(i)));
}
LineDataSet lineDataSet = new LineDataSet(entries, "温度示例");
LineData data = new LineData(lineDataSet);
mLineChart.setData(data);
}}
有问题加我QQ:893151960 或者请加群:142739277