在build.gradle(:app)的dependencies里添加MPAndroidChart
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
在layout布局文件里添加图表控件,折线图LineChart
<com.github.mikephil.charting.charts.LineChart android:id="@+id/lc_temchange" android:layout_width="match_parent" android:layout_height="match_parent" /> <!--该折线图就当表现温度变化吧-->
对布局文件对应的java文件添加修改
public class StatisticsFragment extends android.app.Fragment { private LineChart tempchart;
//创建一个temchart变量
//......省略其他代码......
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view=inflater.inflate(R.layout.fragment_statistics, container, false); //画温度折线图 tempchart=view.findViewById(R.id.lc_temchange);
tempchart.getDescription().setEnabled(false);//图表的文本描述,先不管这个设置成false吧 tempchart.getAxisRight().setEnabled(false);//禁用右轴 ArrayList<Entry> values1=new ArrayList<>(); values1.add(new Entry(3,27)); values1.add(new Entry(4,26)); values1.add(new Entry(5,27)); values1.add(new Entry(6,25)); values1.add(new Entry(7,24)); values1.add(new Entry(8,25)); values1.add(new Entry(9,25)); values1.add(new Entry(10,25)); LineDataSet set1=new LineDataSet(values1,"最近7小时室内温度变化"); set1.setMode(LineDataSet.Mode.CUBIC_BEZIER);//让折线图的曲线平滑 LineData data1=new LineData(set1); tempchart.setData(data1); return view; } }
效果
标签:values1,MPAndroidChart,tempchart,图表,add,Entry,折线图,new From: https://www.cnblogs.com/soaring27221/p/16869494.html