作者:甜蜜棉羽 | 来源:互联网 | 2023-05-19 15:00
噗滋噗滋,最近在项目中要用到曲线图,于是乎我就在网上找了很多很多,有AChartengine,MPAndroidChart,helloChart等等,我还用过基于html5的jsChart来做过,不过
噗滋噗滋,最近在项目中要用到曲线图,于是乎我就在网上找了很多很多,有AChartengine,MPAndroidChart,helloChart等等,我还用过基于html5的jsChart来做过,不过最终还是选择了MPAndroidChart来做,因为AChartengine的扩展性不够好,jsChart跟网页交互也不咋好弄,其实用helloChart更漂亮的,不过个人用不到那么高档的,哈哈哈。。。。省略省略。。。。 哇咔咔,先盗用网上的图看看这个东西的神奇,究竟是神马。。。。。
噗滋,终于偷完图了,是不是看着感觉很牛逼很爽呢,下来我们一起完成这高大上的图表,go go go。。。
首先我们要把这个开源项目MPAndroidChart导入你新建的项目目录中,如下图:
然后我们要写xml文件,也只是加了个显示这个图标的控件,如下所示:
[html] view plaincopy
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
-
- <com.github.mikephil.charting.charts.LineChart
- android:id="@+id/chart1"
- android:layout_width="match_parent"
- android:layout_height="400dp"
- android:layout_marginTop="20dp" />
-
-
- RelativeLayout>
下面我们来看代码吧,其实也是一目了然:
[java] view plaincopy
- package com.example.mpchart;
-
- import java.util.ArrayList;
-
- import android.app.Activity;
- import android.graphics.Color;
- import android.graphics.Typeface;
- import android.os.Bundle;
- import android.view.WindowManager;
-
- import com.github.mikephil.charting.charts.BarLineChartBase.BorderPosition;
- import com.github.mikephil.charting.charts.LineChart;
- import com.github.mikephil.charting.data.Entry;
- import com.github.mikephil.charting.data.LineData;
- import com.github.mikephil.charting.data.LineDataSet;
- import com.github.mikephil.charting.utils.Legend;
- import com.github.mikephil.charting.utils.Legend.LegendForm;
- import com.github.mikephil.charting.utils.XLabels;
- import com.github.mikephil.charting.utils.XLabels.XLabelPosition;
- import com.github.mikephil.charting.utils.YLabels;
-
- public class MainActivity extends Activity {
-
- private LineChart mChart;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
- setContentView(R.layout.activity_main);
-
- mChart = (LineChart) findViewById(R.id.chart1);
-
-
- mChart.setStartAtZero(true);
-
- mChart.setDrawYValues(true);
-
- mChart.setDrawBorder(true);
- mChart.setBorderPositions(new BorderPosition[] {
- BorderPosition.BOTTOM});
-
- mChart.setDescription("曲线图");
-
- mChart.setUnit("¥");
-
- mChart.setAlpha(0.8f);
-
- mChart.setBorderColor(Color.rgb(213, 216, 214));
-
- mChart.setInvertYAxisEnabled(false);
-
- mChart.setHighlightEnabled(true);
-
- mChart.setTouchEnabled(true);
-
- mChart.setDragEnabled(true);
- mChart.setScaleEnabled(true);
-
- mChart.setPinchZoom(true);
-
-
-
- MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker_view);
-
-
- mv.setOffsets(-mv.getMeasuredWidth() / 2, -mv.getMeasuredHeight());
-
- mChart.setMarkerView(mv);
-
-
- mChart.setHighlightIndicatorEnabled(false);
-
- Typeface tf = Typeface.createFromAsset(getAssets(),
- "OpenSans-Regular.ttf");
- mChart.setValueTypeface(tf);
-
- XLabels xl = mChart.getXLabels();
-
-
- xl.setPosition(XLabelPosition.BOTTOM);
- xl.setTypeface(tf);
- xl.setTextSize(10f);
- xl.setSpaceBetweenLabels(3);
-
- YLabels yl = mChart.getYLabels();
-
- yl.setTypeface(tf);
- yl.setTextSize(10f);
- yl.setLabelCount(5);
-
- setData();
-
- mChart.animateX(4000);
- mChart.animateY(3000);
- mChart.animateXY(3000, 3000);
-
-
- mChart.setScaleMinima(0.5f, 1f);
-
-
-
-
- Legend l = mChart.getLegend();
- l.setForm(LegendForm.LINE);
- l.setTypeface(tf);
- l.setTextSize(15);
- l.setTextColor(Color.rgb(104, 241, 175));
- l.setFormSize(30f);
-
-
- mChart.invalidate();
- }
-
- private void setData() {
- String[] aa = {"12","14","15","17","18","19","20"};
- String[] bb = {"122.00","234.34","85.67","117.90","332.33","113.33","120.78"};
-
- ArrayList xVals = new ArrayList();
- for (int i = 0; i < aa.length; i++) {
- xVals.add(aa[i]);
- }
-
- ArrayList yVals = new ArrayList();
-
- for (int i = 0; i < bb.length; i++) {
- yVals.add(new Entry(Float.parseFloat(bb[i]), i));
- }
-
-
- LineDataSet set1 = new LineDataSet(yVals, "DataSet Line");
-
- 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));
-
-
- LineData data = new LineData(xVals, set1);
-
-
- mChart.setData(data);
- }
- }
还有上面代码有说到的数据上可以显示一个标注,代码如下:
[java] view plaincopy
- package com.example.mpchart;
-
- import android.content.Context;
- import android.widget.TextView;
-
- import com.github.mikephil.charting.data.CandleEntry;
- import com.github.mikephil.charting.data.Entry;
- import com.github.mikephil.charting.utils.MarkerView;
- import com.github.mikephil.charting.utils.Utils;
-
- public class MyMarkerView extends MarkerView {
-
- private TextView tvContent;
-
- public MyMarkerView(Context context, int layoutResource) {
- super(context, layoutResource);
-
- tvContent = (TextView) findViewById(R.id.tvContent);
- }
-
-
-
- @Override
- public void refreshContent(Entry e, int dataSetIndex) {
-
- if (e instanceof CandleEntry) {
-
- CandleEntry ce = (CandleEntry) e;
-
- tvContent.setText("" + Utils.formatNumber(ce.getHigh(), 0, true));
- } else {
-
-
- tvContent.setText("" +e.getVal());
- }
- }
- }
还有这个标注的xml代码:
[html] view plaincopy
- xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="40dp"
- android:background="@drawable/marker2" >
-
- <TextView
- android:id="@+id/tvContent"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="7dp"
- android:layout_marginLeft="5dp"
- android:layout_marginRight="5dp"
- android:text=""
- android:textSize="12dp"
- android:textColor="@android:color/white"
- android:ellipsize="end"
- android:singleLine="true"
- android:textAppearance="?android:attr/textAppearanceSmall" />
-
- RelativeLayout>
还有设置字体格式的文件,要放到这个文件夹下面,如图:
其实上面的代码也没什么的,很简单,看注释一目了然,很清楚,上面我只是简单的列举了曲线一部分的使用方法,MPAndroidChart还有很多等着我们去挖掘,研究。。。。。
我这些代码基本可以实现上面贴出来的前四张图的效果,下面我贴一张做的效果图看看,想要实现上面的效果,在代码上改一下就行了,具体看注释
效果不是很好,想更好的效果,继续研究吧,不喜勿喷哇,我是码渣,对此我感到很忧伤,这里只是简单的演示一下这个东西是神马。MPAndroidChart的扩展性很好,要想什么效果可以自己扩展实现。。。。。。。。好了,码渣吹牛逼吹到这里了,继续撸码。。。。。
源码:飞翔取码