赞
踩
一图胜千文,纯粹的数据枯燥、无聊,让人看不下去,改变一下形式,用图表装饰一下,立马有趣多了。既然有这样的外部需求,Android世界里肯定要有图表库才行,今天解析的就是其中最强大的一个MPAndroidChart。
GitHub地址:https://github.com/PhilJay/MPAndroidChart
总共有四种方式集成,推荐直接用Gradle依赖
- allprojects {
- repositories {
- maven { url "https://jitpack.io" }
- }
- }
在项目build.gradle
中添加相关依赖:
- dependencies {
- compile 'com.github.PhilJay:MPAndroidChart:v3.0.0-beta1'
- }
在xml文件定义图表类型,比如LineChart, BarChart, ScatterChart, CandleStickChart, PieChart, BubbleChart or RadarChart
- <com.github.mikephil.charting.charts.LineChart
- android:id="@+id/chart1"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_above="@+id/seekBar1" />
基础设置
- mChart = (LineChart) findViewById(R.id.chart1);
- mChart.setOnChartGestureListener(this);
- mChart.setOnChartValueSelectedListener(this);
- mChart.setDrawGridBackground(false);
-
- // no description text
- mChart.setDescription("");
- mChart.setNoDataTextDescription("You need to provide data for the chart.");
-
- // enable touch gestures
- mChart.setTouchEnabled(true);
-
- // enable scaling and dragging
- mChart.setDragEnabled(true);
- mChart.setScaleEnabled(true);
- // mChart.setScaleXEnabled(true);
- // mChart.setScaleYEnabled(true);
-
- // if disabled, scaling can be done on x- and y-axis separately
- mChart.setPinchZoom(true);
-
- // set an alternative background color
- // mChart.setBackgroundColor(Color.GRAY);
-
- // create a custom MarkerView (extend MarkerView) and specify the layout
- // to use for it
- MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker_view);
-
- // set the marker to the chart
- mChart.setMarkerView(mv);
设置数据源:各种图表的数据不太一致,对于LineChart而言,就是一系列的(x,y)
- /**
- * 设置模拟数据
- * @param count 模拟的个数
- * @param range 数据的范围
- */
- private void setData(int count, float range) {
-
- ArrayList<Entry> values = new ArrayList<Entry>();
-
- for (int i = 0; i < count; i++) {
-
- float val = (float) (Math.random() * range) + 3;
- values.add(new Entry(i, val));
- }
-
- LineDataSet set1;
-
- if (mChart.getData() != null &&
- mChart.getData().getDataSetCount() > 0) {
- set1 = (LineDataSet)mChart.getData().getDataSetByIndex(0);
- set1.setValues(values);
- mChart.getData().notifyDataChanged();
- mChart.notifyDataSetChanged();
- } else {
- // create a dataset and give it a type
- set1 = new LineDataSet(values, "DataSet 1");
-
- // set the line to be drawn like this "- - - - - -"
- set1.enableDashedLine(10f, 5f, 0f);
- set1.enableDashedHighlightLine(10f, 5f, 0f);
- set1.setColor(Color.BLACK);
- set1.setCircleColor(Color.BLACK);
- set1.setLineWidth(1f);
- set1.setCircleRadius(3f);
- set1.setDrawCircleHole(false);
- set1.setValueTextSize(9f);
- set1.setDrawFilled(true);
-
- if (Utils.getSDKInt() >= 18) {
- // fill drawable only supported on api level 18 and above
- Drawable drawable = ContextCompat.getDrawable(this, R.drawable.fade_red);
- set1.setFillDrawable(drawable);
- }
- else {
- set1.setFillColor(Color.BLACK);
- }
-
- ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
- dataSets.add(set1); // add the datasets
-
- // create a data object with the datasets
- LineData data = new LineData(dataSets);
-
- // set data
- mChart.setData(data);
- }
- }
此处源码,参考Demo中的LineChartActivity1
最后样式如下:
Demo提供32个样例:
Panda
2016-08-03
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。