一:配置
参考上篇博客
二:模板代码
MainActivity:
public class MainActivity extends AppCompatActivity {
private LineChart mChart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mChart = findViewById(R.id.line_chart);
mChart.setTouchEnabled(false);
mChart.setDragEnabled(false);
mChart.setScaleEnabled(false);
mChart.setPinchZoom(false);
mChart.setDrawGridBackground(false);
mChart.getDescription().setEnabled(false);
Legend legend = mChart.getLegend();
legend.setEnabled(false);
XAxis xAxis = mChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(false);
xAxis.setGranularity(1f);
xAxis.setAxisMinimum(0f);
xAxis.setAxisMaximum(6f);
final String[] labels = new String[7];
for (int i = 0; i <7; i++) {
labels[i] = getPastDate(i);
}
xAxis.setValueFormatter(new IndexAxisValueFormatter(labels));
YAxis yAxis = mChart.getAxisLeft();
yAxis.setDrawGridLines(false);
yAxis.setGranularity(1f);
yAxis.setAxisMinimum(0);
yAxis.setAxisMaximum(1000);
//数据
ArrayList<Entry> values = new ArrayList<>();
values.add(new Entry(0, 5f));
values.add(new Entry(1, 4f));
values.add(new Entry(2, 6f));
values.add(new Entry(3, 7f));
values.add(new Entry(4, 8f));
values.add(new Entry(5, 3f));
values.add(new Entry(6, 2f));
LineDataSet dataSet = new LineDataSet(values, "");
dataSet.setLineWidth(2f);
dataSet.setColor(Color.BLUE);
dataSet.setCircleColor(Color.BLUE);
dataSet.setDrawCircleHole(false);
dataSet.setDrawValues(false);
ArrayList<ILineDataSet> dataSets = new ArrayList<>();
dataSets.add(dataSet);
LineData data = new LineData(dataSets);
mChart.setData(data);
mChart.invalidate();
}
private String getPastDate(int daysAgo) {
DateFormat dateFormat = new SimpleDateFormat("M/d", Locale.ENGLISH);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, -daysAgo);
Date date = calendar.getTime();
return dateFormat.format(date);
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/title_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="最近一周消费情况"
android:textSize="18sp"
android:textStyle="bold"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp" />
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/line_chart"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="@id/title_textview"
android:layout_marginTop="16dp"
android:padding="16dp" />
</RelativeLayout>
三:具体属性
以上的代码是没有注释,理解相对不易:
详细属性参看:AndroidMPChart之LineChart详细总结 - 简书 (jianshu.com)
标签:layout,实现,false,mChart,add,折线图,new,android From: https://www.cnblogs.com/JIANGzihao0222/p/17338538.html