MPAndroidChart开源图表库(二)之折线图

承接上一篇文章,请参考

1. 将mpandroidchartlibrary-2-0-8.jar包copy到项目的libs中

2. 定义xml文件

3. 主要Java逻辑代码如下,注释已经都添加上了。

<span style="font-family:SimSun;font-size:14px;">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.graphics.Typeface;import android.os.Bundle;public class MainActivity extends ActionBarActivity {private LineChart mLineChart;private Typeface mTf;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mLineChart = (LineChart) findViewById(R.id.spread_line_chart);mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Bold.ttf");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); //是否在折线图上添加边框// no description textlineChart.setDescription("折线图");// 数据描述// 如果没有数据的时候,,会显示这个,类似listview的emtpyviewlineChart.setNoDataTextDescription("You need to provide data for the chart.");// enable / disable grid backgroundlineChart.setDrawGridBackground(false); // 是否显示表格颜色lineChart.setGridBackgroundColor(Color.WHITE & 0x70FFFFFF); // 表格的的颜色,在这里是是给颜色设置一个透明度// enable touch gestureslineChart.setTouchEnabled(true); // 设置是否可以触摸// enable scaling and dragginglineChart.setDragEnabled(true);// 是否可以拖拽lineChart.setScaleEnabled(true);// 是否可以缩放// if disabled, scaling can be done on x- and y-axis separatelylineChart.setPinchZoom(false);//lineChart.setBackgroundColor(color);// 设置背景// add datalineChart.setData(lineData); // 设置数据// get the legend (only possible after setting data)Legend mLegend = lineChart.getLegend(); // 设置比例图标示,就是那个一组y的value的// modify the legend …// mLegend.setPosition(LegendPosition.LEFT_OF_CHART);mLegend.setForm(LegendForm.CIRCLE);// 样式mLegend.setFormSize(6f);// 字体mLegend.setTextColor(Color.WHITE);// 颜色mLegend.setTypeface(mTf);mLegend.setTypeface(mTf);// 字体lineChart.animateX(2500); // 立即执行的动画,x轴 }// 生成一个数据,private LineData getLineData(int count, float range) {ArrayList<String> xValues = new ArrayList<String>();for (int i = 0; i < count; i++) {// x轴显示的数据,这里默认使用数字下标显示xValues.add("" + i);}// y轴的数据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));}// create a dataset and give it a type// y轴的数据集合LineDataSet lineDataSet = new LineDataSet(yValues, "测试折线图" /*显示在比例图上*/);// mLineDataSet.setFillAlpha(110);// mLineDataSet.setFillColor(Color.RED);//用y轴的集合来设置参数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); // add the datasets// create a data object with the datasetsLineData lineData = new LineData(xValues, lineDataSets);return lineData;}}</span>效果图如下;

回避现实的人,未来将更不理想。

MPAndroidChart开源图表库(二)之折线图

相关文章:

你感兴趣的文章:

标签云: