溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android LineChart繪制多條曲線的方法

發(fā)布時(shí)間:2020-09-27 09:45:14 來(lái)源:腳本之家 閱讀:143 作者:Vivinia_Vivinia 欄目:移動(dòng)開發(fā)

本文實(shí)例為大家分享了Android LineChart繪制多條曲線的具體代碼,供大家參考,具體內(nèi)容如下

目標(biāo)效果:

Android LineChart繪制多條曲線的方法Android LineChart繪制多條曲線的方法

1.新建custom_marker_view.xml頁(yè)面作為點(diǎn)擊彈出框的頁(yè)面:

<?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>

2.activity_main.xml頁(yè)面:

<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="10dp"
 android:paddingLeft="10dp"
 android:paddingRight="10dp"
 android:paddingTop="10dp"
 tools:context=".MainActivity" >
 
 <com.github.mikephil.charting.charts.LineChart
 android:id="@+id/chartTall"
 android:layout_width="match_parent"
 android:layout_height="400dp"
 android:layout_marginTop="20dp" />
 
 
</RelativeLayout>

3.新建MyMarkerView.java重寫MarkView控件:

package com.example.weixu.drawline;
 
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());
 }
 }
}

4.MainActivity.java頁(yè)面:

package com.example.weixu.drawline;
 
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;
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 chartTall;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
 WindowManager.LayoutParams.FLAG_FULLSCREEN);
 setContentView(R.layout.activity_main);
 
 chartTall = (LineChart) findViewById(R.id.chartTall);
 
 setType();
 
 // 刷新圖表
 chartTall.invalidate();
 }
 
 private void setType() {
 // 設(shè)置在Y軸上是否是從0開始顯示
 chartTall.setStartAtZero(true);
 //是否在Y軸顯示數(shù)據(jù),就是曲線上的數(shù)據(jù)
 chartTall.setDrawYValues(true);
 //設(shè)置網(wǎng)格
 chartTall.setDrawBorder(true);
 chartTall.setBorderPositions(new BarLineChartBase.BorderPosition[] {
 BorderPosition.BOTTOM});
 //在chart上的右下角加描述
 chartTall.setDescription("身高曲線圖");
 //設(shè)置Y軸上的單位
 chartTall.setUnit("cm");
 //設(shè)置透明度
 chartTall.setAlpha(0.8f);
 //設(shè)置網(wǎng)格底下的那條線的顏色
 chartTall.setBorderColor(Color.rgb(213, 216, 214));
 //設(shè)置Y軸前后倒置
 chartTall.setInvertYAxisEnabled(false);
 //設(shè)置高亮顯示
 chartTall.setHighlightEnabled(true);
 //設(shè)置是否可以觸摸,如為false,則不能拖動(dòng),縮放等
 chartTall.setTouchEnabled(true);
 //設(shè)置是否可以拖拽,縮放
 chartTall.setDragEnabled(true);
 chartTall.setScaleEnabled(true);
 //設(shè)置是否能擴(kuò)大擴(kuò)小
 chartTall.setPinchZoom(true);
 //設(shè)置點(diǎn)擊chart圖對(duì)應(yīng)的數(shù)據(jù)彈出標(biāo)注
 MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker_view);
 mv.setOffsets(-mv.getMeasuredWidth() / 2, -mv.getMeasuredHeight());
 chartTall.setMarkerView(mv);
 chartTall.setHighlightIndicatorEnabled(false);
 //設(shè)置字體格式,如正楷
 Typeface tf = Typeface.createFromAsset(getAssets(),
 "OpenSans-Regular.ttf");
 chartTall.setValueTypeface(tf);
 
 XLabels xl = chartTall.getXLabels();
 xl.setPosition(XLabelPosition.BOTTOM); // 設(shè)置X軸的數(shù)據(jù)在底部顯示
 xl.setTypeface(tf); // 設(shè)置字體
 xl.setTextSize(10f); // 設(shè)置字體大小
 xl.setSpaceBetweenLabels(3); // 設(shè)置數(shù)據(jù)之間的間距
 
 YLabels yl = chartTall.getYLabels();
 yl.setTypeface(tf); // 設(shè)置字體
 yl.setTextSize(10f); // s設(shè)置字體大小
 yl.setLabelCount(5); // 設(shè)置Y軸最多顯示的數(shù)據(jù)個(gè)數(shù)
 // 加載數(shù)據(jù)
 setData();
 //從X軸進(jìn)入的動(dòng)畫
 chartTall.animateX(4000);
 chartTall.animateY(3000); //從Y軸進(jìn)入的動(dòng)畫
 chartTall.animateXY(3000, 3000); //從XY軸一起進(jìn)入的動(dòng)畫
 
 //設(shè)置最小的縮放
 chartTall.setScaleMinima(0.5f, 1f);
 
 }
 
 private void setData() {
 String[] babAge = {"0","1","2","3","4","5","6"}; //連線的x軸數(shù)據(jù)
 String[] babyTall = {"50","60","90","110","130","135","140"};
 String[] usuaTall = {"55","65","95","115","125","135","145"};//連線的y軸數(shù)據(jù)
 
 LineData data=new LineData(babAge,setLine(babAge,babyTall,1,"寶寶身高")); //創(chuàng)建LineData實(shí)體類并添加第一條曲線
 data.addDataSet(setLine(babAge,usuaTall,2,"正常身高")); //添加第二條曲線
 chartTall.setData(data);
 }
 
 //畫線
 private LineDataSet setLine(String[] babAge, String[] Tall,int flag,String name) {
 ArrayList<String> xValsAge = new ArrayList<String>();
 for (int i = 0; i < babAge.length; i++) {
 xValsAge.add(babAge[i]);
 }
 ArrayList<Entry> yValsBabyTall = new ArrayList<Entry>();
 for (int i = 0; i < Tall.length; i++) {
 yValsBabyTall.add(new Entry(Float.parseFloat(Tall[i]), i));
 }
 //設(shè)置baby的成長(zhǎng)曲線
 LineDataSet setData = new LineDataSet(yValsBabyTall,name);
 setData.setDrawCubic(true); //設(shè)置曲線為圓滑的線
 setData.setCubicIntensity(0.2f);
 setData.setDrawFilled(false); //設(shè)置包括的范圍區(qū)域填充顏色
 setData.setDrawCircles(true); //設(shè)置有圓點(diǎn)
 setData.setLineWidth(2f); //設(shè)置線的寬度
 setData.setCircleSize(5f); //設(shè)置小圓的大小
 setData.setHighLightColor(Color.rgb(244, 117, 117));
 //設(shè)置曲線顏色
 if(flag==1)
 setData.setColor(Color.rgb(104, 241, 175)); //寶寶身高曲線顏色
 else if(flag==2)
 setData.setColor(Color.rgb(255, 0, 0)); //普通身高曲線顏色
 return setData; //返回曲線
 }
}

源碼:點(diǎn)擊打開鏈接

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI