1. 将mpandroidchartlibrary-2-0-8.jar包copy到项目的libs中
2. 定义xml文件
3. 主要Java逻辑代码如下,注释已经都添加上了。
- package com.example.mpandroidlinechart;
-
- import java.util.ArrayList;
-
- import com.github.mikephil.charting.charts.LineChart;
- import com.github.mikephil.charting.components.Legend;
- import com.github.mikephil.charting.components.Legend.LegendForm;
- import com.github.mikephil.charting.data.Entry;
- import com.github.mikephil.charting.data.LineData;
- import com.github.mikephil.charting.data.LineDataSet;
-
- import android.support.v7.app.ActionBarActivity;
- import android.graphics.Color;
- import android.os.Bundle;
-
- public class MainActivity extends ActionBarActivity {
-
- private LineChart mLineChart;
-
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- mLineChart = (LineChart) findViewById(R.id.spread_line_chart);
-
-
-
- LineData mLineData = getLineData(36, 100);
- showChart(mLineChart, mLineData, Color.rgb(114, 188, 223));
- }
-
-
- private void showChart(LineChart lineChart, LineData lineData, int color) {
- lineChart.setDrawBorders(false);
-
-
- lineChart.setDescription("");
-
- lineChart.setNoDataTextDescription("You need to provide data for the chart.");
-
-
- lineChart.setDrawGridBackground(false);
- lineChart.setGridBackgroundColor(Color.WHITE & 0x70FFFFFF);
-
-
- lineChart.setTouchEnabled(true);
-
-
- lineChart.setDragEnabled(true);
- lineChart.setScaleEnabled(true);
-
-
- lineChart.setPinchZoom(false);
-
- lineChart.setBackgroundColor(color);
-
-
- lineChart.setData(lineData);
-
-
- Legend mLegend = lineChart.getLegend();
-
-
-
- mLegend.setForm(LegendForm.CIRCLE);
- mLegend.setFormSize(6f);
- mLegend.setTextColor(Color.WHITE);
-
-
- lineChart.animateX(2500);
- }
-
-
-
-
-
-
-
- private LineData getLineData(int count, float range) {
- ArrayList<String> xValues = new ArrayList<String>();
- for (int i = 0; i < count; i++) {
-
- xValues.add("" + i);
- }
-
-
- ArrayList<Entry> yValues = new ArrayList<Entry>();
- for (int i = 0; i < count; i++) {
- float value = (float) (Math.random() * range) + 3;
- yValues.add(new Entry(value, i));
- }
-
-
-
- LineDataSet lineDataSet = new LineDataSet(yValues, "测试折线图" );
-
-
-
-
- lineDataSet.setLineWidth(1.75f);
- lineDataSet.setCircleSize(3f);
- lineDataSet.setColor(Color.WHITE);
- lineDataSet.setCircleColor(Color.WHITE);
- lineDataSet.setHighLightColor(Color.WHITE);
-
- ArrayList<LineDataSet> lineDataSets = new ArrayList<LineDataSet>();
- lineDataSets.add(lineDataSet);
-
-
- LineData lineData = new LineData(xValues, lineDataSets);
-
- return lineData;
- }
- }
效果图如下:
折线图还有另外一种表现形式,就是折线平滑,然后折线与X轴之间可以任意填充自己想要的颜色,其实就是一些属性设置的问题,代码如下:
在上面的getLineData()函数中添加自己的设置:
效果图如下:
关于MPAndroidChart填充式的折线图网上的帖子很少,基本没有。这个是自己在网上搜索其他开源图表库,如JFreeChart...加上自己看源码才总结出来的,不知道对不对,但是看效果,基本上没问题。如果大家发现有问题,欢迎大家指正!
转载自:http://blog.csdn.net/shineflowers/article/details/44704723 继续学习学习~~