Android利用HelloChart繪制曲線
本文實(shí)例為大家分享了Android利用HelloChart繪制曲線的具體代碼,供大家參考,具體內(nèi)容如下
1、將jar包放到app下的libs文件夾中
2、build.gradle(app):
implementation files('libs\\hellocharts-library-1.5.8.jar')3、MainActivity.java
package com.dj.drawlinestest;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import lecho.lib.hellocharts.gesture.ContainerScrollType;
import lecho.lib.hellocharts.gesture.ZoomType;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.ValueShape;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.LineChartView;
public class MainActivity extends AppCompatActivity {
? ? LineChartView lineChart;
? ? Random mRandom;
? ? Handler mHandler;
? ? MyRunnable mRunnable;
? ? DrawLinesUtil mDrawLinesUtil;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? mRandom = new Random();
? ? ? ? lineChart = findViewById(R.id.LineChart);
? ? ? ? mDrawLinesUtil = new DrawLinesUtil(lineChart);
? ? ? ? mRunnable = new MyRunnable();
? ? ? ? mHandler = new Handler();
? ? ? ? mHandler.postDelayed(mRunnable, 0);
? ? }
? ? private class MyRunnable implements Runnable {
? ? ? ? @Override
? ? ? ? public void run() {
? ? ? ? ? ? Integer number = Math.abs(mRandom.nextInt()) % 50 + 50;
? ? ? ? ? ? mDrawLinesUtil.updateLocNumList(number);
? ? ? ? ? ? mDrawLinesUtil.display_chart();
? ? ? ? ? ? mHandler.postDelayed(this, 1000);
? ? ? ? }
? ? }
}4、activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:orientation="vertical" ? ? tools:context=".MainActivity"> ? ? <lecho.lib.hellocharts.view.LineChartView ? ? ? ? android:id="@+id/LineChart" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="500dp" ? ? ? ? android:layout_alignParentStart="true" ? ? ? ? android:layout_alignParentTop="true" ? ? ? ? android:alpha="0.6"></lecho.lib.hellocharts.view.LineChartView> </LinearLayout>
5、工具類:
DrawLinesUtil.java
package com.dj.drawlinestest;
import android.graphics.Color;
import android.view.View;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import lecho.lib.hellocharts.gesture.ContainerScrollType;
import lecho.lib.hellocharts.gesture.ZoomType;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.ValueShape;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.LineChartView;
public class DrawLinesUtil {
? ? LineChartView lineChart;
? ? private boolean inited;
? ? private int pointNumber;
? ? private List<PointValue> mPointValues ;
? ? private List<AxisValue> ?mAxisXValues ;
? ? private List<Integer> ? ?mLocNumList ?;
? ? /**
? ? ?* 構(gòu)造函數(shù)
? ? ?* @param lineChart
? ? ?*/
? ? public DrawLinesUtil(LineChartView lineChart) {
? ? ? ? this.lineChart = lineChart;
? ? ? ? this.inited = false;
? ? ? ? this.pointNumber = 60; // +默認(rèn)顯示60個(gè)點(diǎn)
? ? ? ? this.mPointValues ? ?= new ArrayList<>(); ?// 數(shù)據(jù)點(diǎn)
? ? ? ? this.mAxisXValues ? ?= new ArrayList<>(); ?// X軸標(biāo)注
? ? ? ? this.mLocNumList ? ? = new LinkedList<>(); // 數(shù)據(jù)源
? ? }
? ? /**
? ? ?* set ?get
? ? ?* @return
? ? ?*/
? ? public int getPointNumber() {
? ? ? ? return pointNumber;
? ? }
? ? public void setPointNumber(int pointNumber) {
? ? ? ? this.pointNumber = pointNumber;
? ? }
? ? public LineChartView getLineChart() {
? ? ? ? return lineChart;
? ? }
? ? public void setLineChart(LineChartView lineChart) {
? ? ? ? this.lineChart = lineChart;
? ? }
? ? public List<Integer> getLocNumList() {
? ? ? ? return mLocNumList;
? ? }
? ? /**
? ? ?* 供外部調(diào)用 添加數(shù)據(jù)
? ? ?* @param number
? ? ?*/
? ? public void updateLocNumList(Integer number){
? ? ? ? if (mLocNumList.size() >= pointNumber) {
? ? ? ? ? ? mLocNumList.remove(0);
? ? ? ? }
? ? ? ? mLocNumList.add(number);
? ? }
? ? /**
? ? ?* 供外部調(diào)用 畫曲線
? ? ?*/
? ? public void display_chart()
? ? {
? ? ? ? if( ! inited ){
? ? ? ? ? ? setAttribute();
? ? ? ? }
? ? ? ? getXLables();
? ? ? ? getPoints();
? ? ? ? draw_linechart();
? ? }
? ? /**
? ? ?* 設(shè)置行為屬性,支持縮放、滑動(dòng)以及平移
? ? ?*/
? ? private void setAttribute(){
? ? ? ? lineChart.setInteractive(true);
? ? ? ? lineChart.setZoomType(ZoomType.HORIZONTAL);
? ? ? ? lineChart.setMaxZoom((float) 2);//最大方法比例
? ? ? ? lineChart.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL);
? ? ? ? lineChart.setVisibility(View.GONE);
? ? ? ? inited = true;
? ? }
? ? /**
? ? ?* 設(shè)置X軸的顯示
? ? ?*/
? ? private void getXLables(){
? ? ? ? mAxisXValues.clear();
? ? ? ? for (int i = 0; i < mLocNumList.size(); i++) {
? ? ? ? ? ? mAxisXValues.add(new AxisValue(i).setLabel(""));//這里設(shè)置x軸的內(nèi)容
? ? ? ? }
? ? }
? ? /**
? ? ?* 圖表的每個(gè)點(diǎn)的顯示
? ? ?*/
? ? private void getPoints() {
? ? ? ? mPointValues.clear();
? ? ? ? //Log.v(TAG,"getPoints"+mLocNumList.size());
? ? ? ? for (int i = 0; i < mLocNumList.size(); i++) {
? ? ? ? ? ? mPointValues.add(new PointValue(i, mLocNumList.get(i)));
? ? ? ? }
? ? }
? ? /**
? ? ?* 內(nèi)部方法 畫曲線
? ? ?*/
? ? private void draw_linechart(){
? ? ? ? Line line = new Line(mPointValues).setColor(Color.parseColor("#FFCD41"));//("#FFCD41")); ?//折線的顏色(橙色)
? ? ? ? List<Line> lines = new ArrayList<>();
? ? ? ? line.setShape(ValueShape.CIRCLE);//折線圖上每個(gè)數(shù)據(jù)點(diǎn)的形狀 ?這里是圓形 (有三種 :ValueShape.SQUARE ?ValueShape.CIRCLE ?ValueShape.DIAMOND)
? ? ? ? line.setCubic(true);//曲線是否平滑,即是曲線還是折線
? ? ? ? line.setFilled(true);//是否填充曲線的面積
? ? ? ? line.setHasLabels(false);//曲線的數(shù)據(jù)坐標(biāo)是否加上備注
? ? ? ? line.setHasLabelsOnlyForSelected(true);//點(diǎn)擊數(shù)據(jù)坐標(biāo)提示數(shù)據(jù)(設(shè)置了這個(gè)line.setHasLabels(true);就無(wú)效)
? ? ? ? line.setHasLines(true);//是否用線顯示。如果為false 則沒(méi)有曲線只有點(diǎn)顯示
? ? ? ? line.setHasPoints(true);//是否顯示圓點(diǎn) 如果為false 則沒(méi)有原點(diǎn)只有點(diǎn)顯示(每個(gè)數(shù)據(jù)點(diǎn)都是個(gè)大的圓點(diǎn))
? ? ? ? line.setPointRadius(3);
? ? ? ? lines.add(line);
? ? ? ? LineChartData data = new LineChartData();
? ? ? ? data.setLines(lines);
? ? ? ? //坐標(biāo)軸
? ? ? ? Axis axisX = new Axis(); //X軸
? ? ? ? axisX.setTextSize(0);//設(shè)置字體大小
? ? ? ? //axisX.setMaxLabelChars(20); //最多幾個(gè)X軸坐標(biāo),意思就是你的縮放讓X軸上數(shù)據(jù)的個(gè)數(shù)7<=x<=mAxisXValues.length
? ? ? ? axisX.setValues(mAxisXValues); ?//填充X軸的坐標(biāo)名稱
? ? ? ? data.setAxisXBottom(axisX); //x 軸在底部
? ? ? ? //data.setAxisXTop(axisX); ?//x 軸在頂部
? ? ? ? // axisX.setHasLines(true); //x 軸分割線
? ? ? ? // Y軸是根據(jù)數(shù)據(jù)的大小自動(dòng)設(shè)置Y軸上限(在下面我會(huì)給出固定Y軸數(shù)據(jù)個(gè)數(shù)的解決方案)
? ? ? ? Axis axisY = new Axis(); ?//Y軸
? ? ? ? axisY.setName("");//y軸標(biāo)注
? ? ? ? axisY.setTextSize(8);//設(shè)置字體大小
? ? ? ? data.setAxisYLeft(axisY); ?//Y軸設(shè)置在左邊
? ? ? ? //axisY.setHasLines(true); //Y軸分割線
? ? ? ? //設(shè)置行為屬性,支持縮放、滑動(dòng)以及平移
? ? ? ? lineChart.setInteractive(true);
? ? ? ? lineChart.setZoomType(ZoomType.HORIZONTAL);
? ? ? ? lineChart.setMaxZoom((float) 2);//最大方法比例
? ? ? ? lineChart.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL);
? ? ? ? lineChart.setLineChartData(data);
? ? ? ? lineChart.setVisibility(View.VISIBLE);
? ? ? ? Viewport v2 = new Viewport(lineChart.getMaximumViewport());
? ? ? ? v2.top ? ?= 100;
? ? ? ? v2.bottom = 0;
? ? ? ? if (mLocNumList.size()>24) {
? ? ? ? ? ? lineChart.setMaximumViewport(v2);
? ? ? ? ? ? Viewport v = new Viewport(lineChart.getMaximumViewport());
? ? ? ? ? ? v.left = mLocNumList.size() - 24-1;
? ? ? ? ? ? lineChart.setCurrentViewport(v);
? ? ? ? }else
? ? ? ? {
? ? ? ? ? ? v2.right ?= 24;
? ? ? ? ? ? lineChart.setMaximumViewport(v2);
? ? ? ? ? ? lineChart.setCurrentViewport(v2);
? ? ? ? }
? ? }
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android把商品添加到購(gòu)物車的動(dòng)畫效果(貝塞爾曲線)
- Android 曲線圖的繪制示例代碼
- Android利用MPAndroidChart繪制曲線圖表的基礎(chǔ)教程
- Android編程之canvas繪制各種圖形(點(diǎn),直線,弧,圓,橢圓,文字,矩形,多邊形,曲線,圓角矩形)
- Android實(shí)現(xiàn)價(jià)格走勢(shì)自定義曲線圖
- Android LineChart繪制多條曲線的方法
- Android 利用三階貝塞爾曲線繪制運(yùn)動(dòng)軌跡的示例
- Android貝塞爾曲線初步學(xué)習(xí)第二課 仿QQ未讀消息氣泡拖拽黏連效果
- android貝塞爾曲線實(shí)現(xiàn)波浪效果
- Android中貝塞爾曲線的繪制方法示例代碼
相關(guān)文章
Android實(shí)現(xiàn)簡(jiǎn)單手機(jī)震動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)手機(jī)震動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
android基礎(chǔ)教程之a(chǎn)ndroid的listview與edittext沖突解決方法
這篇文章主要介紹了android的listview與edittext沖突解決方法,需要的朋友可以參考下2014-02-02
Android實(shí)現(xiàn)代碼畫虛線邊框背景效果
可能之前遇到這樣的需求大家都會(huì)想到用圖片背景來(lái)解決,下面這篇文章將給大家介紹Android如何利用代碼畫虛線邊框背景的效果,有需要的朋友們可以參考借鑒,下面來(lái)跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。2016-12-12
RecyclerView優(yōu)雅實(shí)現(xiàn)復(fù)雜列表布局
這篇文章主要為大家詳細(xì)介紹了RecyclerView優(yōu)雅實(shí)現(xiàn)復(fù)雜列表布局,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
詳解flutter如何實(shí)現(xiàn)局部導(dǎo)航管理
這篇文章主要為大家介紹了詳解flutter如何實(shí)現(xiàn)局部導(dǎo)航管理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
淺析Android手機(jī)衛(wèi)士之號(hào)碼歸屬地查詢
這篇文章主要介紹了淺析Android手機(jī)衛(wèi)士之號(hào)碼歸屬地查詢的相關(guān)資料,需要的朋友可以參考下2016-04-04
Android開(kāi)發(fā)中用Kotlin編寫LiveData組件教程
LiveData是Jetpack組件的一部分,更多的時(shí)候是搭配ViewModel來(lái)使用,相對(duì)于Observable,LiveData的最大優(yōu)勢(shì)是其具有生命感知的,換句話說(shuō),LiveData可以保證只有在組件( Activity、Fragment、Service)處于活動(dòng)生命周期狀態(tài)的時(shí)候才會(huì)更新數(shù)據(jù)2022-12-12

