Android 自定義折線圖

自定義一個簡單的折線圖隆敢,足夠展示萬條數(shù)據(jù),(千條以上數(shù)據(jù)在1070的屏幕寬度上 只會是一片茫茫崔慧,顏色會擠到一塊兒筑公。。尊浪。匣屡。)


簡單說一下實現(xiàn)思路封救,

1、獲取當前View在當前屏幕中的位置(Android中屏幕的左上角為整個屏幕坐標的原點往右?捣作、往下遞增誉结,我們自己的坐標系是符合數(shù)學邏輯的二維坐標系,原點在整個折線圖的左下角)

2券躁、首先計算坐標點(比如惩坑,我們自己的坐標系的Y軸長度為畫布的高度canasWidth減去50[我們默認距頂50],我們把獲取到 ?的高度數(shù)值定為原點的Y軸坐標也拜,X的走向與Android原生一致以舒, ?所以想讓原點距離屏幕左邊多遠 ,就設置所多大的值為原點在屏幕的X坐標,記得留出足夠的空間慢哈,需要去繪制Y軸刻度)

3蔓钟、然后先繪制出X軸、Y軸卵贱,并根據(jù)數(shù)據(jù)的長度計算Y滥沫、X軸刻度(有十條數(shù)據(jù),X軸長度除以10就是X周刻度键俱,Y軸同理兰绣,根據(jù) ? ? ? 所有數(shù)據(jù)里的最大數(shù)值計算Y軸刻度)

4、別忘了繪制刻度數(shù)據(jù)编振,位置的計算在上一步中有相同之處缀辩,可以獲取上邊的值使用

5、繪制折線與虛線(X軸刻度)使用drawLines去繪制踪央,會節(jié)省資源臀玄,虛線也要用這個去繪制,若使用(drawLine)繪制杯瞻,會很慢

以下為自定義View核心類,在java代碼中添加炫掐,目前沒有做在布局中直接使用魁莉,另:提供demo供大家參考(點擊此處),若有問題歡迎添加QQ ?:1017726485.共同討論

csdn鏈接?Android自定義折線圖



import android.app.Activity;

import android.content.Context;

import android.content.res.Resources;

import android.graphics.Canvas;

import android.graphics.DashPathEffect;

import android.graphics.Paint;

import android.graphics.Path;

import android.support.annotation.ColorRes;

import android.util.Log;

import android.view.MotionEvent;

import android.view.View;

import android.view.ViewGroup;

import android.view.WindowManager;

import android.widget.LinearLayout;

import com.jjf.customchartline.R;

import com.jjf.customchartline.utils.Arith;

import com.jjf.customchartline.been.chartline.ChartResultData;

import com.jjf.customchartline.been.chartline.LineBeen;

import com.jjf.customchartline.been.chartline.LinePain;

import java.math.BigDecimal;

import java.util.ArrayList;

/**

* @author: jjf

* @date: 2018/4/19

* @describe:

*/

public abstract class MYChartLine extends View {

? ? private float canasWidth;? ? ? ? ? ? ? ? //畫布寬

? ? private float canasHeight;

? ? private float XPoint;? ? ? ? ? ? ? ? ? //定義原點

? ? private float YPoint;

? ? private double XScale;? ? ? ? ? ? ? ? ? //刻度間距

? ? private int YScale;

? ? private int topDim = 30;//距離頂部

? ? private int surplusHeight = 50;//Y軸超出最大刻度距離(計算Y軸時總長減去超出距離)

? ? private int Ylenth = 6;//Y軸刻度數(shù)量

? ? private int surplusWhith = 50;//X軸超出最大刻度距離

? ? private String TAG = "MYChartLine";

? ? private Context context;

? ? //容器寬高

? ? private int viewGroupWidth;

? ? private int viewGroupHeight;

? ? //虛線間隙

? ? int dottedspace = 3;//虛線間隙

? ? int dottedLength = 10;//虛線長度

? ? //坐標軸寬度

? ? private int axisWidth = 1;

? ? //坐標軸顏色

? ? private @ColorRes

? ? int axisColor = R.color.zuobiao;//#a6a6a6

? ? Resources resources;

? ? int color;//

? ? int colorBlue;

? ? public int getYlenth() {

? ? ? ? return Ylenth;

? ? }

? ? public void setYlenth(int ylenth) {

? ? ? ? Ylenth = ylenth;

? ? }

? ? //坐標軸線的寬度

? ? public void setAxisWidth(int axisWidth) {

? ? ? ? this.axisWidth = axisWidth;

? ? }

? ? //軸顏色

? ? public void setAxisColor(@ColorRes int axisColor) {

? ? ? ? this.axisColor = axisColor;

? ? ? ? color = context.getResources().getColor(axisColor);

? ? }

? ? boolean isRefresh = true;//? false? 刷新數(shù)據(jù)? true? 添加數(shù)據(jù)

? ? Activity activity;

? ? public MYChartLine(Context context, ViewGroup viewGroup) {

? ? ? ? super(context);

? ? ? ? this.context = context;

? ? ? ? if (context instanceof Activity) {

? ? ? ? ? ? activity = (Activity) context;

? ? ? ? }

? ? ? ? resources = context.getResources();

? ? ? ? //獲取屏幕寬(自適應屏幕寬度)

? ? ? ? WindowManager wm = ((Activity) context).getWindowManager();

? ? ? ? viewGroupWidth = wm.getDefaultDisplay().getWidth();

? ? ? ? // 取父控件高度(為了在布局中可以設置高度募胃,不獲取屏幕)

? ? ? ? LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) viewGroup

? ? ? ? ? ? ? ? .getLayoutParams();

? ? ? ? viewGroupHeight = linearParams.height;

? ? ? ? //畫布寬高

? ? ? ? canasWidth = viewGroupWidth - 10;

? ? ? ? canasHeight = viewGroupHeight;

? ? ? ? //原點坐標

? ? ? ? XPoint = canasWidth / 12.0F;

? ? ? ? YPoint = canasHeight - surplusHeight;

? ? ? ? color = context.getResources().getColor(axisColor);

? ? ? ? colorBlue = context.getResources().getColor(R.color.zhexian_blue);

? ? ? ? //初始化畫筆

? ? ? ? //標記線(隨手勢移動)

? ? ? ? paintYTag = new Paint();

? ? ? ? paintYTag.setColor(colorBlue);

? ? ? ? paintYTag.setStrokeWidth(2);

? ? ? ? paintYTag.setAntiAlias(true);

? ? ? ? paintYTag.setStyle(Paint.Style.STROKE);//設置畫直線格式

? ? ? ? paintYTag.setPathEffect(new DashPathEffect(new float[]{dottedLength, dottedspace}, 0));//畫虛線關鍵代碼

? ? ? ? //軸畫筆

? ? ? ? paintYX = new Paint();

? ? ? ? paintYX.setColor(color);

? ? ? ? paintYX.setTextSize(30);

? ? ? ? paintYX.setStrokeWidth(axisWidth);

? ? ? ? paintYX.setAntiAlias(true);

? ? ? ? //垂直虛線

? ? ? ? paintDottenY = new Paint();

? ? ? ? paintDottenY.setColor(color);

? ? ? ? paintDottenY.setTextSize(30);

? ? ? ? paintDottenY.setStrokeWidth(axisWidth);

? ? ? ? paintDottenY.setAntiAlias(true);

? ? ? ? paintDottenY.setStyle(Paint.Style.STROKE);//設置畫直線格式

? ? ? ? paintDottenY.setPathEffect(new DashPathEffect(new float[]{5, 3}, 1));//畫虛線關鍵代碼

? ? ? ? //X軸數(shù)據(jù)

? ? ? ? paintXText = new Paint();

? ? ? ? paintXText.setColor(color);

? ? ? ? paintXText.setTextSize(30);

? ? ? ? paintXText.setAntiAlias(true);

? ? ? ? YScale = (int) ((canasHeight - canasHeight / Ylenth) / Ylenth);//Y軸刻度間距

? ? ? ? effectiveLenthX = (int) (canasWidth - XPoint - surplusWhith);

? ? }

? ? /**

? ? * 桌布

? ? */

? ? private Canvas canvas;

? ? //Y軸

? ? Paint paintYX;

? ? //Y軸數(shù)據(jù)

? ? Paint paintYText;

? ? //X軸數(shù)據(jù)

? ? Paint paintXText;

? ? //垂直虛線

? ? Paint paintDottenY;

? ? //與Y軸平行的旗唁、可移動標記線

? ? Paint paintYTag;

? ? //標記線

? ? Path tagPath = new Path();

? ? //清除畫筆

? ? @Override

? ? protected void onDraw(Canvas canvas) {

? ? ? ? super.onDraw(canvas);

? ? ? ? this.canvas = canvas;

? ? ? ? //標記線(隨手勢移動)

? ? ? ? canvas.drawPath(tagPath, paintYTag);

? ? ? ? //軸畫筆

? ? ? ? //Y軸

? ? ? ? canvas.drawLine(XPoint, topDim, XPoint, YPoint, paintYX);

? ? ? ? //X軸

? ? ? ? canvas.drawLine(XPoint, YPoint, canasWidth, YPoint, paintYX);

? ? ? ? //X軸數(shù)據(jù)

? ? ? ? YScale = (int) ((canasHeight - canasHeight / Ylenth) / Ylenth);//Y軸刻度間距

? ? ? ? this.onMDraw();

? ? }

? ? //畫筆集合? 記錄每一條折線 //VALUE==折線與折線標注字體

? ? ArrayList<Paint> painLines = new ArrayList<>();

? ? //是否設置Y軸數(shù)據(jù)為10、100痹束、1000倍數(shù)

? ? private boolean isYDataToMultiple = true;

? ? private LinePain maxLenthlinePain;//折線圖最長的折線

? ? /**

? ? * 控制Y軸數(shù)據(jù)為10检疫、100、1000的倍數(shù)(只有Y軸當前數(shù)據(jù)大與10倍才會抹去尾數(shù)? 如 12 不會變成10? ? 112會變成110祷嘶,1234會變成1200屎媳,12345會變成12000)

? ? */

? ? public void setYDataToMultiple(boolean isYDataToMultiple) {

? ? ? ? this.isYDataToMultiple = isYDataToMultiple;

? ? }

? ? int xuLineNum = 0; //虛線個數(shù)夺溢、、X軸分割區(qū)數(shù)

? ? LinePain[] dataList;//所有條線的數(shù)據(jù)

? ? double maxValue = 0;

? ? /**

? ? * 添加折線

? ? * isRefresh true 刷新數(shù)據(jù) 全部重新繪制

? ? * false 添加數(shù)據(jù)? 增加畫筆

? ? */

? ? public void addLine(LinePain... linePain) {

? ? ? ? System.out.println("開始---");

? ? ? ? //onDraw 方法會運行兩次烛谊,這里控制一段代碼 只運行一次

? ? ? ? if (isRefresh) {

? ? ? ? ? ? chartResultData.setDatasY(chartYList);

? ? ? ? ? ? dataList = new LinePain[linePain.length];

? ? ? ? }

? ? ? ? try {

? ? ? ? ? ? //虛線個數(shù)风响、、X軸分割區(qū)數(shù)

? ? ? ? ? ? xuLineNum = 0;

? ? ? ? ? ? //所有Y軸數(shù)據(jù)中最大一條數(shù)據(jù)值--用最大值計算Y軸刻度平均值

? ? ? ? ? ? maxValue = 0;

? ? ? ? ? ? painLines.clear();

? ? ? ? ? ? //初始化丹禀、創(chuàng)建數(shù)據(jù)

? ? ? ? ? ? createData(linePain);

? ? ? ? ? ? //四舍五入求最大位數(shù)的整數(shù)

? ? ? ? ? ? //返回數(shù)據(jù)

? ? ? ? ? ? chartResultData.setDataX(maxLenthlinePain.getLineBeens().get(maxLenthlinePain.getLineBeens().size() - 1).getX() + "");

? ? ? ? ? ? //單位像素 代表的數(shù)據(jù)大凶辞凇(Y軸)

? ? ? ? ? ? //Y軸實際單位長度(對應數(shù)據(jù))

? ? ? ? ? ? double heightY = Arith.div(maxY - minY, maxValue, 10).doubleValue();

? ? ? ? ? ? //X軸刻度間距

? ? ? ? ? ? if (xuLineNum > 1) {

? ? ? ? ? ? ? ? XScale =? ( canasWidth - XPoint - surplusWhith)/(xuLineNum-1);

? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? XScale = 1;

? ? ? ? ? ? }

? ? ? ? ? ? System.out.println("刻畫Y軸---");

? ? ? ? ? ? //畫 Y軸數(shù)據(jù)

? ? ? ? ? ? drawYData(linePain);

? ? ? ? ? ? System.out.println("刻畫Y軸結束---");

? ? ? ? ? ? //計算日期間隔(最多顯示7個日期,計算相鄰兩個日期中間間隔的單位個數(shù))

? ? ? ? ? ? int inDex = xuLineNum / 6;

? ? ? ? ? ? System.out.println("計算虛線双泪,折線開始---");

? ? ? ? ? ? //折線位置

? ? ? ? ? ? ArrayList<float[]> lineschart = new ArrayList<>();

? ? ? ? ? ? //計算虛線有多少截

? ? ? ? ? ? int dottedNum = (int) ((YPoint - topDim) / (dottedspace + dottedLength));

? ? ? ? ? ? //(每一條虛線的每一截都要計算出來)虛線位置

? ? ? ? ? ? float[] dottedLine = new float[xuLineNum * dottedNum * 4];

? ? ? ? ? ? //虛線個數(shù)--代表單條折線數(shù)據(jù)長度

? ? ? ? ? ? for (int j = 0; j < xuLineNum; j++) {

? ? ? ? ? ? ? ? int X = (int) (XPoint + j * XScale);? //X軸刻度位置

? ? ? ? ? ? ? ? //折線上數(shù)值 以及X軸刻度

? ? ? ? ? ? ? ? if (xuLineNum > 6) {

? ? ? ? ? ? ? ? ? ? if (j % inDex == 0) {

? ? ? ? ? ? ? ? ? ? ? ? //X軸坐標

? ? ? ? ? ? ? ? ? ? ? ? canvas.drawText(maxLenthlinePain.getLineBeens().get(j).getX() + "",

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (int) (XPoint + (j * XScale) - surplusWhith),

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? YPoint + 30, paintXText);

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? ? ? //X軸坐標

? ? ? ? ? ? ? ? ? ? canvas.drawText(maxLenthlinePain.getLineBeens().get(j).getX() + "",

? ? ? ? ? ? ? ? ? ? ? ? ? ? (int) (XPoint + (j * XScale) - maxLenthlinePain.getLineBeens().get(j).getX().length() * 8),

? ? ? ? ? ? ? ? ? ? ? ? ? ? YPoint + 30, paintXText);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? //每條虛線的每段虛線節(jié)點

? ? ? ? ? ? ? ? double dottedPosition = 0;

? ? ? ? ? ? ? ? if (j < xuLineNum - 1) {

? ? ? ? ? ? ? ? ? ? //計算每條虛線的每一節(jié)

? ? ? ? ? ? ? ? ? ? for (int i = 0; i < dottedNum; i++) {

? ? ? ? ? ? ? ? ? ? ? ? //Y軸平行虛線

? ? ? ? ? ? ? ? ? ? ? ? dottedLine[j * dottedNum * 4 + i * 4] = (float) (X + XScale);

? ? ? ? ? ? ? ? ? ? ? ? dottedLine[j * dottedNum * 4 + i * 4 + 1] = (float) (topDim + dottedPosition);

? ? ? ? ? ? ? ? ? ? ? ? dottedLine[j * dottedNum * 4 + i * 4 + 2] = (float) (X + XScale);

? ? ? ? ? ? ? ? ? ? ? ? dottedLine[j * dottedNum * 4 + i * 4 + 3] = (float) (topDim + dottedPosition + dottedLength);

? ? ? ? ? ? ? ? ? ? ? ? dottedPosition += (dottedLength + dottedspace);

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? for (int c = 0; c < linePain.length; c++) {

? ? ? ? ? ? ? ? ? ? if (lineschart.size() < c + 1) {//添加一條折線的容器(有幾條折線持搜,會添加幾次)

? ? ? ? ? ? ? ? ? ? ? ? lineschart.add(new float[xuLineNum * 4]);

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? //? 折線Y點位置

? ? ? ? ? ? ? ? ? ? double startY = 0;

? ? ? ? ? ? ? ? ? ? //當前折線長度是否到達? j? 腳標(折線長度不一定相等 對比是否達到最長折線)

? ? ? ? ? ? ? ? ? ? if (linePain[c].getLineBeens().size() > j) {

? ? ? ? ? ? ? ? ? ? ? ? // 折線Y點位置

? ? ? ? ? ? ? ? ? ? ? ? startY = YPoint - Arith.mul(heightY + "", linePain[c].getLineBeens().get(j).getY() + "");

? ? ? ? ? ? ? ? ? ? ? ? //折線上數(shù)值 以及X軸刻度

? ? ? ? ? ? ? ? ? ? ? ? if (xuLineNum > 6) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? if (j % inDex == 0) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //查看本折線是否展示連接點值,true顯示折線上的值

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (linePain[c].isShowPrompt()) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? canvas.drawText(linePain[c].getLineBeens().get(j).getY() + "", X - 10, (float) (startY - 20), painLines.get(c));

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? ? ? ? ? ? ? if (linePain[c].isShowPrompt()) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //顯示線上的值

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? canvas.drawText(linePain[c].getLineBeens().get(j).getY() + "", X - 15, (float) (startY - 20), painLines.get(c));

? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? //折線(與虛線)在倒數(shù)第二次已經(jīng)完成繪制----折線數(shù)據(jù)計算

? ? ? ? ? ? ? ? ? ? ? ? if (j < linePain[c].getLineBeens().size() - 1) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? //實際高度

? ? ? ? ? ? ? ? ? ? ? ? ? ? double z1 = Arith.mul(heightY + "", linePain[c].getLineBeens().get(j + 1).getY() + "");

? ? ? ? ? ? ? ? ? ? ? ? ? ? lineschart.get(c)[j * 4] = X;

? ? ? ? ? ? ? ? ? ? ? ? ? ? lineschart.get(c)[j * 4 + 1] = (float) startY;

? ? ? ? ? ? ? ? ? ? ? ? ? ? lineschart.get(c)[j * 4 + 2] = (float) (XPoint + (j + 1) * XScale);

? ? ? ? ? ? ? ? ? ? ? ? ? ? lineschart.get(c)[j * 4 + 3] = YPoint - ((float) z1);

? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? System.out.println("渲染虛線焙矛,折線開始---");

? ? ? ? ? ? //畫虛線

? ? ? ? ? ? canvas.drawLines(dottedLine, paintDottenY);

? ? ? ? ? ? //? 折線

? ? ? ? ? ? for (int i = 0; i < painLines.size(); i++) {

? ? ? ? ? ? ? ? canvas.drawLines(lineschart.get(i), painLines.get(i));

? ? ? ? ? ? }

? ? ? ? ? ? System.out.println("渲染虛線葫盼,折線結束---" + System.currentTimeMillis());

? ? ? ? ? ? this.postInvalidate();

? ? ? ? ? ? isRefresh = false;

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? Log.i(TAG, "---------" + e.toString());

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? }

? ? private int lastX = 0;//記錄標記線的位置

? ? private int lastY = 0;

? ? private int startYLineX = 0;//Y軸的X坐標

? ? private int effectiveLenthY = 0;//Y軸有效長度

? ? private int effectiveLenthX = 0;//X軸有效長度

? ? @Override

? ? public boolean onTouchEvent(MotionEvent event) {

? ? ? ? //獲取到手指處的橫坐標和縱坐標

? ? ? ? int x = (int) event.getX();

? ? ? ? int y = (int) event.getY();

? ? ? ? switch (event.getAction()) {

? ? ? ? ? ? case MotionEvent.ACTION_DOWN:

? ? ? ? ? ? ? ? try {

? ? ? ? ? ? ? ? ? ? getParent().requestDisallowInterceptTouchEvent(false);

? ? ? ? ? ? ? ? } catch (Exception e) {

? ? ? ? ? ? ? ? }

? ? ? ? ? ? case MotionEvent.ACTION_MOVE:

? ? ? ? ? ? ? ? System.out.println("effectiveLenthX=="+effectiveLenthX);

? ? ? ? ? ? ? ? if ((x > XPoint && x < effectiveLenthX + XPoint) && (y < YPoint && y > topDim)) {

? ? ? ? ? ? ? ? ? ? lastX = x;

? ? ? ? ? ? ? ? ? ? tagPath.reset();

? ? ? ? ? ? ? ? ? ? tagPath.moveTo(lastX, topDim - 5);

? ? ? ? ? ? ? ? ? ? tagPath.lineTo(lastX, YPoint);

//? ? ? ? ? ? ? ? ? ? postInvalidate();

//

? ? ? ? ? ? ? ? ? ? setToData(x);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case MotionEvent.ACTION_UP:

? ? ? ? ? ? ? ? try {

? ? ? ? ? ? ? ? ? ? getParent().requestDisallowInterceptTouchEvent(true);

? ? ? ? ? ? ? ? } catch (Exception e) {

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? break;

? ? ? ? }

? ? ? ? return true;

? ? }

? ? //計算集合中最大值與最小值

? ? //最大值

? ? public double ArrayListMax(ArrayList<LineBeen> sampleList) {

? ? ? ? try {

? ? ? ? ? ? double maxDevation = 0.0;

? ? ? ? ? ? int totalCount = sampleList.size();

? ? ? ? ? ? if (totalCount >= 1) {

? ? ? ? ? ? ? ? double max = strToDouble(sampleList.get(0).getY());

? ? ? ? ? ? ? ? for (int i = 0; i < totalCount; i++) {

? ? ? ? ? ? ? ? ? ? double temp = strToDouble(sampleList.get(i).getY());

? ? ? ? ? ? ? ? ? ? if (temp > max) {

? ? ? ? ? ? ? ? ? ? ? ? max = temp;

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? maxDevation = max;

? ? ? ? ? ? }

? ? ? ? ? ? return maxDevation;

? ? ? ? } catch (Exception ex) {

? ? ? ? ? ? throw ex;

? ? ? ? }

? ? }

? ? //調(diào)用本類的其他 需要實現(xiàn)本方法、在本方法去調(diào)用

? ? public abstract void onMDraw();

? ? private int minY = 0;

? ? private int maxY = 0;

? ? //監(jiān)聽手勢 返回觸摸到位置的值

? ? private ChartResultData chartResultData = new ChartResultData();

? ? //所有折線的Y值? 添加到 chartResultData 中

? ? ArrayList<String> chartYList = new ArrayList();

? ? /**

? ? * \

? ? * <p>

? ? * 返回手勢選中數(shù)據(jù)

? ? *

? ? * @return

? ? */

? ? public abstract ChartResultData setData(ChartResultData chartResultData);

? ? /**

? ? * 初始化數(shù)據(jù)

? ? * 創(chuàng)建薄扁、計算相關變量

? ? */

? ? private void createData(LinePain... linePain) throws IllegalAccessException {

? ? ? ? int x = 0;

? ? ? ? for (int i = 0; i < linePain.length; i++) {

? ? ? ? ? ? dataList[i] = linePain[i];

? ? ? ? ? ? Paint paint = new Paint();

? ? ? ? ? ? paint.setStrokeWidth(linePain[i].getLineWidth());

? ? ? ? ? ? paint.setAntiAlias(true);

? ? ? ? ? ? paint.setColor(resources.getColor(linePain[i].getColorId()));

? ? ? ? ? ? paint.setTextSize(linePain[i].getNumberPromptSize());

? ? ? ? ? ? if (linePain[i].getLineBeens() != null && linePain[i].getLineBeens().size() > 0) {

? ? ? ? ? ? ? ? //找出最多數(shù)據(jù)的折線

? ? ? ? ? ? ? ? if (xuLineNum < linePain[i].getLineBeens().size()) {

? ? ? ? ? ? ? ? ? ? xuLineNum = linePain[i].getLineBeens().size();

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? //Y軸最大刻度值

? ? ? ? ? ? ? ? if (maxValue < ArrayListMax(linePain[i].getLineBeens())) {

? ? ? ? ? ? ? ? ? ? maxValue = ArrayListMax(linePain[i].getLineBeens());

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? int size = linePain[i].getLineBeens().size();

? ? ? ? ? ? ? ? if (x < size) {//找出最長的一條折線(多條折線不一定一樣長短)

? ? ? ? ? ? ? ? ? ? x = size;

? ? ? ? ? ? ? ? ? ? maxLenthlinePain = linePain[i];

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? if (maxLenthlinePain == null) {

? ? ? ? ? ? ? ? ? ? maxLenthlinePain = linePain[0];

? ? ? ? ? ? ? ? }

? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? return;

? ? ? ? ? ? }

? ? ? ? ? ? painLines.add(paint);

? ? ? ? }

? ? ? ? if (maxValue > 1000) {

? ? ? ? ? ? maxValue = Arith.div(maxValue, (double) 1000, 3).setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue() * 1000;

? ? ? ? } else if (maxValue > 100) {

? ? ? ? ? ? maxValue = Arith.div(maxValue, (double) 100, 3).setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue() * 100;

? ? ? ? } else if (maxValue > 10) {

? ? ? ? ? ? maxValue = Arith.div(maxValue, (double) 10, 3).setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue() * 10;

? ? ? ? } else if (maxValue < 5) {

? ? ? ? ? ? maxValue = 5;

? ? ? ? }

? ? }

? ? public double strToDouble(String str) {

? ? ? ? return Double.parseDouble(str);

? ? }

? ? /**

? ? * 刻畫Y軸數(shù)據(jù)

? ? */

? ? public void drawYData(LinePain... linePain) {

? ? ? ? //Y軸數(shù)據(jù)

? ? ? ? if (paintYText == null) {

? ? ? ? ? ? paintYText = new Paint();

? ? ? ? }

? ? ? ? paintYText.setColor(color);

? ? ? ? paintYText.setTextSize(30);

? ? ? ? paintYText.setAntiAlias(true);

? ? ? ? String strY;

? ? ? ? //Y軸一個刻度 等于的數(shù)據(jù)大小

? ? ? ? int v1 = (int) (new BigDecimal(maxValue / (Ylenth - 1)).setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue());

? ? ? ? //Y軸刻度數(shù)據(jù)

? ? ? ? for (int i = 0; i < Ylenth; i++) {

? ? ? ? ? ? int i1 = (int) (maxValue - i * v1);

? ? ? ? ? ? if (i1 < 0) {

? ? ? ? ? ? ? ? i1 = 0;

? ? ? ? ? ? }

? ? ? ? ? ? /**

? ? ? ? ? ? *使數(shù)據(jù)為10剪返、100、1000的倍數(shù)---純屬為了數(shù)據(jù)好看邓梅,

? ? ? ? ? ? * 可以通過 {@link com.jjf.customchartline.view.MYChartLine#setYDataToMultiple(boolean)} 設置

? ? ? ? ? ? */

? ? ? ? ? ? if (isYDataToMultiple) {

? ? ? ? ? ? ? ? if (i1 > 1000) {

? ? ? ? ? ? ? ? ? ? strY = i1 / 100 * 100 + "";

? ? ? ? ? ? ? ? } else if (i1 > 100) {

? ? ? ? ? ? ? ? ? ? strY = i1 / 10 * 10 + "";

? ? ? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? ? ? strY = i1 + "";

? ? ? ? ? ? ? ? }

? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? strY = i1 + "";

? ? ? ? ? ? }

? ? ? ? ? ? // Y軸文字

? ? ? ? ? ? int heigY = i * YScale + (linePain[0].getNumberPromptSize() / 2) + topDim + surplusHeight;

? ? ? ? ? ? if (i < Ylenth - 1) {

? ? ? ? ? ? ? ? float v = XPoint - strY.length() * (linePain[0].getNumberPromptSize() / 2) - 10;

? ? ? ? ? ? ? ? if (v < 0) {

? ? ? ? ? ? ? ? ? ? v = 0;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? canvas.drawText(strY, v, heigY, paintYText);

? ? ? ? ? ? }

? ? ? ? ? ? if (i == 0) {

? ? ? ? ? ? ? ? minY = heigY;

? ? ? ? ? ? } else if (i == Ylenth - 1) {

? ? ? ? ? ? ? ? maxY = heigY;

? ? ? ? ? ? }

? ? ? ? }

? ? }


? ? //根據(jù)手勢位置返回數(shù)據(jù)到頁面

? ? public void setToData(int x) {

? ? ? ? try {

? ? ? ? ? ? double mx = XScale / 2;

? ? ? ? ? ? x = (int) (x + mx);

? ? ? ? ? ? int div = (int) Arith.div((x - XPoint), XScale == 0 ? 1 : XScale, 2).doubleValue();

? ? ? ? ? ? chartResultData.getDatasY().clear();

? ? ? ? ? ? for (int j = 0; j < dataList.length; j++) {

? ? ? ? ? ? ? ? if (dataList[j].getLineBeens().size() > div) {

? ? ? ? ? ? ? ? ? ? chartResultData.setDataX(dataList[j].getLineBeens().get(div).getX());

? ? ? ? ? ? ? ? ? ? chartResultData.getDatasY().add(dataList[j].getLineBeens().get(div).getY() + "");

? ? ? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? ? ? chartResultData.getDatasY().add("0");

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? setData(chartResultData);

//? ? ? ? ? ? MYChartLine.this.postInvalidate();

? ? ? ? } catch (IllegalAccessException e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? }

}

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末脱盲,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子日缨,更是在濱河造成了極大的恐慌钱反,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件匣距,死亡現(xiàn)場離奇詭異面哥,居然都是意外死亡,警方通過查閱死者的電腦和手機毅待,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進店門尚卫,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人尸红,你說我怎么就攤上這事吱涉。” “怎么了外里?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵怎爵,是天一觀的道長。 經(jīng)常有香客問我盅蝗,道長鳖链,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任墩莫,我火速辦了婚禮芙委,結果婚禮上逞敷,老公的妹妹穿的比我還像新娘。我一直安慰自己题山,他們只是感情好兰粉,可當我...
    茶點故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著顶瞳,像睡著了一般玖姑。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上慨菱,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天焰络,我揣著相機與錄音,去河邊找鬼符喝。 笑死闪彼,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的协饲。 我是一名探鬼主播畏腕,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼茉稠!你這毒婦竟也來了描馅?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤而线,失蹤者是張志新(化名)和其女友劉穎铭污,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體膀篮,經(jīng)...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡嘹狞,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了誓竿。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片磅网。...
    茶點故事閱讀 40,096評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖筷屡,靈堂內(nèi)的尸體忽然破棺而出涧偷,到底是詐尸還是另有隱情,我是刑警寧澤速蕊,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布嫂丙,位于F島的核電站娘赴,受9級特大地震影響规哲,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜诽表,卻給世界環(huán)境...
    茶點故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一唉锌、第九天 我趴在偏房一處隱蔽的房頂上張望隅肥。 院中可真熱鬧,春花似錦袄简、人聲如沸腥放。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽秃症。三九已至,卻和暖如春吕粹,著一層夾襖步出監(jiān)牢的瞬間种柑,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工匹耕, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留聚请,地道東北人。 一個月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓稳其,卻偏偏與公主長得像驶赏,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子既鞠,可洞房花燭夜當晚...
    茶點故事閱讀 45,037評論 2 355

推薦閱讀更多精彩內(nèi)容