1. 程式人生 > >MPAndroidChart開源圖表《總》之折線圖、統計圖、扇形圖

MPAndroidChart開源圖表《總》之折線圖、統計圖、扇形圖

<span style="font-size:14px;">package com.test.demo.demo;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.test.demo.demo.widget.MyMarkView;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 作者:zengtao
 * 郵箱:
[email protected]
* 時間:2015/10/13 0013 20:01 */ public class LineChartActivity extends AppCompatActivity { @InjectView(R.id.lineChart) LineChart mChart; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_line_chart); ButterKnife.inject(this); initChart(); } /** * 1.初始化LineChart * 2.新增資料x,y軸資料 * 3.重新整理圖表 */ private void initChart() { /** * ====================1.初始化-自由配置=========================== */ // 是否在折線圖上新增邊框 mChart.setDrawGridBackground(false); mChart.setDrawBorders(false); // 設定右下角描述 mChart.setDescription(""); //設定透明度 mChart.setAlpha(0.8f); //設定網格底下的那條線的顏色 mChart.setBorderColor(Color.rgb(213, 216, 214)); //設定高亮顯示 mChart.setHighlightEnabled(true); //設定是否可以觸控,如為false,則不能拖動,縮放等 mChart.setTouchEnabled(true); //設定是否可以拖拽 mChart.setDragEnabled(false); //設定是否可以縮放 mChart.setScaleEnabled(false); //設定是否能擴大擴小 mChart.setPinchZoom(false); /** * ====================2.佈局點新增資料-自由佈局=========================== */ // 折線圖的點,點選戰士的佈局和資料 MyMarkView mv = new MyMarkView(this); mChart.setMarkerView(mv); // 載入資料 LineData data = getLineData(); mChart.setData(data); /** * ====================3.x,y動畫效果和重新整理圖表等=========================== */ //從X軸進入的動畫 mChart.animateX(4000); mChart.animateY(3000); //從Y軸進入的動畫 mChart.animateXY(3000, 3000); //從XY軸一起進入的動畫 //設定最小的縮放 mChart.setScaleMinima(0.5f, 1f); Legend l = mChart.getLegend(); l.setForm(Legend.LegendForm.LINE); //設定圖最下面顯示的型別 l.setTextSize(15); l.setTextColor(Color.rgb(104, 241, 175)); l.setFormSize(30f); // 重新整理圖表 mChart.invalidate(); } private LineData getLineData() { String[] xx = {"2", "4", "6", "8", "10", "12", "14", "16", "18"}; String[] yy = {"20", "80", "10", "60", "30", "70", "55", "22", "40"}; ArrayList<String> xVals = new ArrayList<String>(); for (int i = 0; i < xx.length; i++) { xVals.add(xx[i]); } ArrayList<Entry> yVals = new ArrayList<Entry>(); for (int i = 0; i < yy.length; i++) { yVals.add(new Entry(Float.parseFloat(yy[i]), i)); } LineDataSet set1 = new LineDataSet(yVals, "LineChart Test"); set1.setDrawCubic(true); //設定曲線為圓滑的線 set1.setCubicIntensity(0.2f); set1.setDrawFilled(false); //設定包括的範圍區域填充顏色 set1.setDrawCircles(true); //設定有圓點 set1.setLineWidth(2f); //設定線的寬度 set1.setCircleSize(5f); //設定小圓的大小 set1.setHighLightColor(Color.rgb(244, 117, 117)); set1.setColor(Color.rgb(104, 241, 175)); //設定曲線的顏色 return new LineData(xVals, set1); } } </span>
以上,就完成了一個折線圖的打造,其實也就很簡單,就三點