Android 上自定義的複式折線圖(一)
轉發請註明出處:http://blog.csdn.net/qq_30552993/article/details/51546919
這裡也給出後續的兩篇文章
最近公司想做一個表格統計圖,而且要符合可以多條折線的那種,自己也是翻閱了好多資料也沒有找到很合適的一種,後來果斷決定還是自己定義一個咯,下面先附上效果圖.
這裡有三條折線,其實裡面是有兩個列表:一個是管顏色的,另一個是管折線上的資料.同時這裡的控制元件外面也包了一個HorizontalScrollView,當超過螢幕時就可以進行水平滾動.而且這裡的控制元件LineView也提供了很過設定屬性的方法.
轉發請註明出處: http://blog.csdn.net/qq_30552993/article/details/51546919
其實在這裡之前也有很多好有的圖表框架,這裡我列舉幾個在github上面比較好的:
MPAndroidChart:
https://github.com/PhilJay/MPAndroidChart
hellocharts-android:
https://github.com/lecho/hellocharts-android
XCL-Charts:
https://github.com/xcltapestry/XCL-Charts
Android-Charts:
https://github.com/limccn/Android-Charts
那這裡或許有些人就奇怪了,那你都說這裡的框架好用,那你為什麼不用.其實東西好用可是也有不合適自己的對吧!所以自己就自定義一個咯,就是這麼任性.
LineView.java
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.util.AttributeSet;
import android.view.View;
import java.util.ArrayList;
@SuppressLint("DrawAllocation")
public class LineView extends View {
private static final String TAG = "LineView";
// 預設邊距
private int Margin = 40;
// 原點座標
private int Xpoint;
private int Ypoint;
// X,Y軸的單位長度,即表格中的正方形的寬高
private int Yscale = 32;
private int Xscale = Yscale * 2;
//是否顯示錶格
private boolean isShowGrid = false;
//是否展示表格為虛線
private boolean isDottedLine = false;
// X,Y軸 線的顏色
private int XYColor = Color.WHITE;
// X軸字型的顏色
private int XTextColor = Color.WHITE;
// Y軸字型的顏色
private int YTextColor = Color.WHITE;
// 表格的顏色
private int GridColor = Color.WHITE;
// 預設折線資料的顏色
private int DataColor = Color.WHITE;
private int XTextSize = 16;
private int YTextSize = 16;
// X軸上面的顯示文字
private ArrayList<String> XLabel;
// Y軸上面的顯示文字
private ArrayList<String> YLabel;
private int left = 0; // Y軸與Y軸上的文字的距離
private int bottom = 0; //X軸與X軸上的文字的距離
// 折線資料列表
private ArrayList<ArrayList<Integer>> dataLists;
//折線顏色列表
private ArrayList<Integer> dataColorList;
// 單前資料
private ArrayList<Integer> dataList;
private int dataColor;
public LineView(Context context) {
super(context);
}
public LineView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setXYColor(int XYColor) {
this.XYColor = XYColor;
}
public void setXTextColor(int XTextColor) {
this.XTextColor = XTextColor;
}
public void setYTextColor(int YTextColor) {
this.YTextColor = YTextColor;
}
public void setGridColor(int gridColor) {
this.GridColor = gridColor;
}
public void setDataColor(int dataColor) {
this.DataColor = dataColor;
}
public void setDataColorList(ArrayList<Integer> dataColorList) {
this.dataColorList = dataColorList;
}
public void setXTextSize(int XTextSize) {
this.XTextSize = XTextSize;
}
public void setYTextSize(int YTextSize) {
this.YTextSize = YTextSize;
}
public void setShowGrid(boolean isShowGrid) {
this.isShowGrid = isShowGrid;
}
public void setDottedLine(boolean isDottedLine) {
this.isDottedLine = isDottedLine;
}
public void setXYLabel(ArrayList<String> xlabel, ArrayList<String> ylabel) {
this.XLabel = xlabel;
this.YLabel = ylabel;
}
public void setScale(int scale) {
this.Yscale = scale;
this.Xscale = this.Yscale * 2;
}
public void setYToYTextSpace(int left) {
this.left = left;
}
public void setXToXTextSpace(int bottom) {
this.bottom = bottom;
}
public void setDataList(ArrayList<ArrayList<Integer>> dataLists) {
this.dataLists = dataLists;
postInvalidate();
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (Yscale <= 24) {
setMeasuredDimension(Xscale * (XLabel.size() + 2) + left,
Yscale * (YLabel.size() + 2) + bottom);
} else {
setMeasuredDimension(Xscale * (XLabel.size() + 1) + left,
Yscale * (YLabel.size() + 1) + bottom);
}
}
// 初始化資料值
public void init() {
Xpoint = this.Margin + left;
Ypoint = this.getHeight() - this.Margin - bottom;
/*Xscale = (this.getWidth() - 2 * this.Margin) / (this.XLabel.size());
Yscale = (this.getHeight() - 2 * this.Margin) / (this.YLabel.size() - 1);*/
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.TRANSPARENT);
Paint p1 = new Paint();
p1.setStyle(Paint.Style.STROKE);
p1.setAntiAlias(true);
p1.setColor(XYColor);
p1.setStrokeWidth(2);
init();
if (isShowGrid) {
this.drawTable(canvas);
} else {
this.drawXLine(canvas, p1);
this.drawYLine(canvas, p1);
}
if (dataLists != null) {
for (int i = 0; i < dataLists.size(); i++) {
this.drawData(canvas, i);
}
}
}
// 畫表格
private void drawTable(Canvas canvas) {
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(GridColor);
Path path = new Path();
if (isDottedLine) {
PathEffect effects = new DashPathEffect(new float[]{5, 5, 5, 5}, 1);//畫虛線
paint.setPathEffect(effects);
}
int startX = 0;
int startY = 0;
int stopX = 0;
int stopY = 0;
// 縱向線
for (int i = 0; i <= XLabel.size(); i++) {
startX = Xpoint + i * Xscale;
startY = Ypoint;
stopY = Ypoint - (this.YLabel.size() - 1) * Yscale;
if (i != 0) {
path.moveTo(startX - Xscale / 2, startY);
path.lineTo(startX - Xscale / 2, stopY);
canvas.drawPath(path, paint);
}
path.moveTo(startX, startY);
path.lineTo(startX, stopY);
canvas.drawPath(path, paint);
}
// 橫向線
for (int i = 0; i < YLabel.size(); i++) {
startX = Xpoint;
startY = Ypoint - i * Yscale;
stopX = Xpoint + (this.XLabel.size()) * Xscale;
path.moveTo(startX, startY);
path.lineTo(stopX, startY);
canvas.drawPath(path, paint);
}
}
//畫縱軸
private void drawXLine(Canvas canvas, Paint p) {
p.setColor(XYColor);
float stopX = Xpoint;
float stopY = Ypoint - Yscale * (YLabel.size() - 1);
canvas.drawLine(Xpoint, Ypoint, stopX, stopY, p);
// Y軸最後是否有箭頭
canvas.drawLine(stopX, stopY, stopX - Xscale / 6, stopY + Yscale / 3, p);
canvas.drawLine(stopX, stopY, stopX + Xscale / 6, stopY + Yscale / 3, p);
}
// 畫橫軸
private void drawYLine(Canvas canvas, Paint p) {
p.setColor(XYColor);
float stopX = Xpoint + Xscale * XLabel.size();
float stopY = Ypoint;
canvas.drawLine(Xpoint, Ypoint, stopX, stopY, p);
// X軸最後是否有箭頭
canvas.drawLine(stopX, stopY, stopX - Xscale / 6, stopY - Yscale / 3, p);
canvas.drawLine(stopX, stopY, stopX - Xscale / 6, stopY + Yscale / 3, p);
}
// 畫資料
private void drawData(Canvas canvas, int pos) {
dataList = dataLists.get(pos);
dataColor = pos < dataColorList.size() ? dataColorList.get(pos) : DataColor;
Paint p = new Paint();
p.setAntiAlias(true);
// 縱軸資料
for (int i = 0; i < YLabel.size(); i++) {
int startY = Ypoint - i * Yscale;
p.setColor(YTextColor);
p.setTextSize(XTextSize);
canvas.drawText(this.YLabel.get(i), this.Margin / 4,
startY + this.Margin / 4, p);
}
//橫軸資料
for (int i = 0; i < XLabel.size(); i++) {
int startX = Xpoint + i * Xscale;
p.setColor(XTextColor);
p.setTextSize(YTextSize);
canvas.drawText(this.XLabel.get(i), startX - this.Margin / 4 + Xscale / 2,
this.getHeight() - this.Margin / 4, p);
p.setColor(dataColorList.size() > 0 ? dataColor : DataColor);
canvas.drawCircle(startX + Xscale / 2, calY(dataList.get(i)), 4, p);
if (i < XLabel.size() - 1) {
canvas.drawLine(startX + Xscale / 2, calY(dataList.get(i)),
Xpoint + (i + 1) * Xscale + Xscale / 2,
calY(dataList.get(i + 1)), p);
}
}
}
/**
* @param y
* @return
*/
private int calY(int y) {
int y0 = 0;
int y1 = 0;
// Log.i("zzzz", "y:"+y);
try {
y0 = Integer.parseInt(YLabel.get(0));
// Log.i("zzzz", "y0"+y0);
y1 = Integer.parseInt(YLabel.get(1));
// Log.i("zzzz","y1"+y1);
} catch (Exception e) {
// Log.i("zzzz", "string changed is err");
return 0;
}
try {
// Log.i("zzzz", "返回資料"+(Ypoint-(y-y0)*Yscale/(y1-y0)) );
return Ypoint - ((y - y0) * Yscale / (y1 - y0));
} catch (Exception e) {
// Log.i("zzzz", "return is err");
return 0;
}
}
}
這裡先來一個列子,說明怎麼使用LineView
MainActivity.java
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import java.util.ArrayList;
public class MainActivity extends Activity {
private LineView mSkinLineView;
private ArrayList<Integer> colorList = new ArrayList<>();//折線的顏色列表
private ArrayList<String> XLabel = new ArrayList<>(); //X軸上的標籤資料列表
private ArrayList<String> YLabel = new ArrayList<>(); //Y軸上的標籤資料列表
private ArrayList<ArrayList<Integer>> dataLists;//折線上的資料列表
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews() {
colorList.add(getResources().getColor(R.color.using_before));
colorList.add(getResources().getColor(R.color.using_after));
for (int i = 0; i < 10; i++) {
XLabel.add(String.valueOf(i));
}
for (int i = 0; i < 11; i++) {
YLabel.add(String.valueOf(i * 10));
}
mSkinLineView = (LineView) findViewById(R.id.skinLineView);
mSkinLineView.setDataColorList(colorList);
mSkinLineView.setXYLabel(XLabel, YLabel); //設定設定X,Y軸的資料
mSkinLineView.setShowGrid(true);//true是展示表格,false是顯示帶箭頭的X,Y軸
// mSkinLineView.setDottedLine(true); // 表格展示微虛線
mSkinLineView.setXYColor(Color.LTGRAY); // X,Y軸線與資料的顏色
mSkinLineView.setGridColor(Color.LTGRAY);// 表格的顏色
/* mSkinLineView.setScale(32);//表格中的正方形的單位,預設是32
mSkinLineView.setXTextSize(30); // X軸字型的顏色
mSkinLineView.setYTextSize(30); // Y軸字型的顏色
mSkinLineView.setXToXTextSpace(20);
mSkinLineView.setYToYTextSpace(40);
mSkinLineView.setXTextColor(Color.RED);
mSkinLineView.setYTextColor(Color.RED);
mSkinLineView.setDataColor(Color.BLUE);*/
randSet();
findViewById(R.id.resetBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
randSet();
}
});
}
private void randSet() {
dataLists = new ArrayList<>();
ArrayList<Integer> dataList1 = new ArrayList<Integer>();
int random1 = (int) (Math.random() * 99 + 1);
for (int i = 0; i < XLabel.size(); i++) {
dataList1.add((int) (Math.random() * random1));
}
ArrayList<Integer> dataList2 = new ArrayList<Integer>();
int random2 = (int) (Math.random() * 99 + 1);
for (int i = 0; i < XLabel.size(); i++) {
dataList2.add((int) (Math.random() * random2));
}
ArrayList<Integer> dataList3 = new ArrayList<Integer>();
int random3 = (int) (Math.random() * 99 + 1);
for (int i = 0; i < XLabel.size(); i++) {
dataList3.add((int) (Math.random() * random3));
}
dataLists.add(dataList1);
dataLists.add(dataList2);
dataLists.add(dataList3);
mSkinLineView.setDataList(dataLists);
}
}
佈局檔案activity_main.xml<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099FF"
android:orientation="vertical">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<sl.myapplication.LineView
android:id="@+id/skinLineView"
android:layout_width="match_parent"
android:layout_height="200dip" />
</LinearLayout>
</HorizontalScrollView>
<Button
android:id="@+id/resetBtn"
android:text="重新整理"
android:textColor="@color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
上面設計到的顏色XML,在res-->values的目錄下新增colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#ffffff</color>
<color name="using_before">#f6e749</color>
<color name="using_after">#7ae6fe</color>
</resources>
相關文章
- Android 上自定義的複式折線圖(二)Android
- Android開發自定義控制元件實現一個折線圖Android控制元件
- 自定義View:畫布實現自定義View(折線圖的實現)View
- Flutter自定義折線圖並新增點選事件Flutter事件
- 自定義View之顏色漸變折線圖View
- 自定義控制元件之kotlin繪製折線圖和曲線圖控制元件Kotlin
- Android自定義ImageView 在圖片上新增一個圖層AndroidView
- Android之自定義控制元件實現天氣溫度折線圖和餅狀圖Android控制元件
- excel折線圖自定x軸y軸 excel折線圖xy設定Excel
- Android 折線圖的實現Android
- 第十七篇:複製控制( 上 ) --- 自定義複製函式函式
- 【詳解】純 React Native 程式碼自定義折線圖元件 (譯)React Native元件
- Android 自定義一個輪播圖Android
- android自定義View&自定義ViewGroup(上)AndroidView
- Android自定義View,畫一個好看帶延長線的餅狀圖AndroidView
- Android 折線圖之hellocharts (餅狀圖)餅圖Android
- android中自定義屬性重複定義Android
- android自定義button樣式Android
- 自定義上傳圖片拼圖遊戲遊戲
- (Android自定義控制元件)Android自定義狀態提示圖表Android控制元件
- 微信小程式折線圖表折線圖加區域圖微信小程式
- R : 折線圖
- 如何使用Android自定義複合檢視Android
- Android 自定義 地圖 室內Android地圖
- Android 自定義帶動畫的柱狀圖Android動畫
- echart折線圖異常多出一條連線線
- Android 自定義貝塞爾曲線工具Android
- 自定義餅狀圖(一)
- 自定義條柱圖(一)
- 一個自定義函式函式
- echarts 折線圖拼接Echarts
- Android自定義ViewGroup(一)AndroidView
- Label--自定義可貼上複製的Label
- Android 自定義 View 繪圖基礎AndroidView繪圖
- android 自定義帶動畫的統計餅圖Android動畫
- excel折線圖自定x軸y軸 excel做xy軸座標圖Excel
- Typora中自定義命令上傳圖片
- canvas圖表(2) - 折線圖Canvas