AndroidLineChart繪制多條曲線(xiàn)的方法

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

成都創(chuàng)新互聯(lián)長(zhǎng)期為千余家客戶(hù)提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為瑪曲企業(yè)提供專(zhuān)業(yè)的網(wǎng)站設(shè)計(jì)制作、成都做網(wǎng)站,瑪曲網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。

目標(biāo)效果:

Android LineChart繪制多條曲線(xiàn)的方法Android LineChart繪制多條曲線(xiàn)的方法

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重寫(xiě)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開(kāi)始顯示
 chartTall.setStartAtZero(true);
 //是否在Y軸顯示數(shù)據(jù),就是曲線(xiàn)上的數(shù)據(jù)
 chartTall.setDrawYValues(true);
 //設(shè)置網(wǎng)格
 chartTall.setDrawBorder(true);
 chartTall.setBorderPositions(new BarLineChartBase.BorderPosition[] {
 BorderPosition.BOTTOM});
 //在chart上的右下角加描述
 chartTall.setDescription("身高曲線(xiàn)圖");
 //設(shè)置Y軸上的單位
 chartTall.setUnit("cm");
 //設(shè)置透明度
 chartTall.setAlpha(0.8f);
 //設(shè)置網(wǎng)格底下的那條線(xiàn)的顏色
 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)畫(huà)
 chartTall.animateX(4000);
 chartTall.animateY(3000); //從Y軸進(jìn)入的動(dòng)畫(huà)
 chartTall.animateXY(3000, 3000); //從XY軸一起進(jìn)入的動(dòng)畫(huà)
 
 //設(shè)置最小的縮放
 chartTall.setScaleMinima(0.5f, 1f);
 
 }
 
 private void setData() {
 String[] babAge = {"0","1","2","3","4","5","6"}; //連線(xiàn)的x軸數(shù)據(jù)
 String[] babyTall = {"50","60","90","110","130","135","140"};
 String[] usuaTall = {"55","65","95","115","125","135","145"};//連線(xiàn)的y軸數(shù)據(jù)
 
 LineData data=new LineData(babAge,setLine(babAge,babyTall,1,"寶寶身高")); //創(chuàng)建LineData實(shí)體類(lèi)并添加第一條曲線(xiàn)
 data.addDataSet(setLine(babAge,usuaTall,2,"正常身高")); //添加第二條曲線(xiàn)
 chartTall.setData(data);
 }
 
 //畫(huà)線(xiàn)
 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)曲線(xiàn)
 LineDataSet setData = new LineDataSet(yValsBabyTall,name);
 setData.setDrawCubic(true); //設(shè)置曲線(xiàn)為圓滑的線(xiàn)
 setData.setCubicIntensity(0.2f);
 setData.setDrawFilled(false); //設(shè)置包括的范圍區(qū)域填充顏色
 setData.setDrawCircles(true); //設(shè)置有圓點(diǎn)
 setData.setLineWidth(2f); //設(shè)置線(xiàn)的寬度
 setData.setCircleSize(5f); //設(shè)置小圓的大小
 setData.setHighLightColor(Color.rgb(244, 117, 117));
 //設(shè)置曲線(xiàn)顏色
 if(flag==1)
 setData.setColor(Color.rgb(104, 241, 175)); //寶寶身高曲線(xiàn)顏色
 else if(flag==2)
 setData.setColor(Color.rgb(255, 0, 0)); //普通身高曲線(xiàn)顏色
 return setData; //返回曲線(xiàn)
 }
}

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

當(dāng)前文章:AndroidLineChart繪制多條曲線(xiàn)的方法
本文地址:http://muchs.cn/article16/jepogg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站移動(dòng)網(wǎng)站建設(shè)、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、標(biāo)簽優(yōu)化、搜索引擎優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營(yíng)