This commit is contained in:
coco
2026-07-03 15:56:07 +08:00
commit caef23209c
5767 changed files with 1004268 additions and 0 deletions
+13
View File
@@ -0,0 +1,13 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
.externalNativeBuild
/.idea
/.gradle
/gradlew.bat
/gradlew
+201
View File
@@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright {yyyy} {name of copyright owner}
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
+104
View File
@@ -0,0 +1,104 @@
# Android-LineChart
![](https://img.shields.io/badge/platform-Android-brightgreen.svg) ![](https://img.shields.io/badge/language-java-yellow.svg) ![](https://img.shields.io/badge/license-Apache--2.0-blue.svg)
## 介绍
一个简单的折线,贝塞尔曲线图表控件,高度可扩展,支持动态显示。
## 效果图
![演示][1] ![演示][2] ![演示][3]
![演示][4] ![演示][5]
## 使用
1. 设置布局
```XML
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never"
android:scrollbars="none">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.jeanboy.linechart.LineChartView
android:id="@+id/line_chart_view"
android:layout_width="wrap_content"
android:layout_height="300dp"/>
</LinearLayout>
</HorizontalScrollView>
```
2. 添加数据
```Java
lineChartView.setData(datas);
```
3. 修改Y轴标尺间隔
```Java
lineChartView.setRulerYSpace(value);
```
4. 修改X轴标尺间隔(锚点间距)
```Java
lineChartView.setStepSpace(value);
```
5. 设置是否显示表格
```Java
lineChartView.setShowTable(isShowTable);
```
6. 设置是否为贝塞尔曲线
```Java
lineChartView.setBezierLine(isBezier);
```
7. 设置锚点是否为方形
```Java
lineChartView.setCubePoint(isCube);
```
8. 播放动画
```Java
lineChartView.playAnim();
```
## 关于我
如果对你有帮助,请 star 一下,然后 follow 我,给我增加一下分享动力,谢谢!
如果你有什么疑问或者问题,可以提交 issue 和 request,发邮件给我 jeanboy@foxmail.com 。
或者加入下面的 QQ 群来一起学习交流。
<a target="_blank" href="http://shang.qq.com/wpa/qunwpa?idkey=0b505511df9ead28ec678df4eeb7a1a8f994ea8b75f2c10412b57e667d81b50d"><img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="Android技术进阶:386463747" title="Android技术进阶:386463747"></a>
## License
Copyright 2017 jeanboy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
[1]:https://github.com/jeanboydev/Android-LineChart/blob/master/resources/anim.gif
[2]:https://github.com/jeanboydev/Android-LineChart/blob/master/resources/change.gif
[3]:https://github.com/jeanboydev/Android-LineChart/blob/master/resources/operate.gif
[4]:https://github.com/jeanboydev/Android-LineChart/blob/master/resources/Screenshot_20170613-183802.jpg
[5]:https://github.com/jeanboydev/Android-LineChart/blob/master/resources/Screenshot_20170613-183803.jpg
+1
View File
@@ -0,0 +1 @@
/build
+30
View File
@@ -0,0 +1,30 @@
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.jeanboy.linechart"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
+25
View File
@@ -0,0 +1,25 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in D:\Develop\Android\SDK/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
@@ -0,0 +1,26 @@
package com.jeanboy.linechart;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
/**
* Instrumentation test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
assertEquals("com.jeanboy.linechart", appContext.getPackageName());
}
}
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jeanboy.linechart">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
@@ -0,0 +1,577 @@
package com.jeanboy.linechart;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* Created by jeanboy on 2017/6/12.
*/
public class LineChartView extends View {
private Paint linePaint;//曲线画笔
private Paint pointPaint;//曲线上锚点画笔
private Paint tablePaint;//表格画笔
private Paint textRulerPaint;//标尺文本画笔
private Paint textPointPaint;//曲线上锚点文本画笔
private Path linePath;//曲线路径
private Path tablePath;//表格路径
private int mWidth, mHeight;
private List<Data> dataList = new ArrayList<>();
private Point[] linePoints;
private int stepStart;
private int stepEnd;
private int stepSpace;
private int stepSpaceDefault = 10;
private int stepSpaceDP = stepSpaceDefault;//item宽度默认dp
private int topSpace, bottomSpace;
private int tablePadding;
private int tablePaddingDP = 20;//view四周padding默认dp
private int maxValue, minValue;
private int rulerValueDefault = 10;
private int rulerValue = rulerValueDefault;//刻度单位跨度
private int rulerValuePadding;//刻度单位与轴的间距
private int rulerValuePaddingDP = 8;//刻度单位与轴的间距默认dp
private float heightPercent = 0.618f;
private int lineColor = Color.parseColor("#286DD4");//曲线颜色
private float lineWidthDP = 2f;//曲线宽度dp
private int pointColor = Color.parseColor("#FF4081");//锚点颜色
private float pointWidthDefault = 8f;
private float pointWidthDP = pointWidthDefault;//锚点宽度dp
private int tableColor = Color.parseColor("#BBBBBB");//表格线颜色
private float tableWidthDP = 0.5f;//表格线宽度dp
private int rulerTextColor = tableColor;//表格标尺文本颜色
private float rulerTextSizeSP = 10f;//表格标尺文本大小
private int pointTextColor = Color.parseColor("#009688");//锚点文本颜色
private float pointTextSizeSP = 10f;//锚点文本大小
private boolean isShowTable = false;
private boolean isBezierLine = false;
private boolean isCubePoint = false;
private boolean isInitialized = false;
private boolean isPlayAnim = false;
private ValueAnimator valueAnimator;
private float currentValue = 0f;
private boolean isAnimating = false;
public LineChartView(Context context) {
this(context, null);
}
public LineChartView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public LineChartView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setupView();
}
private void setupView() {
linePaint = new Paint();
linePaint.setAntiAlias(true);//抗锯齿
linePaint.setStyle(Paint.Style.STROKE);//STROKE描边FILL填充
linePaint.setColor(lineColor);
linePaint.setStrokeWidth(dip2px(lineWidthDP));//边框宽度
pointPaint = new Paint();
pointPaint.setAntiAlias(true);
pointPaint.setStyle(Paint.Style.FILL);
pointPaint.setColor(pointColor);
pointPaint.setStrokeWidth(dip2px(pointWidthDP));
tablePaint = new Paint();
tablePaint.setAntiAlias(true);
tablePaint.setStyle(Paint.Style.STROKE);
tablePaint.setColor(tableColor);
tablePaint.setStrokeWidth(dip2px(tableWidthDP));
textRulerPaint = new Paint();
textRulerPaint.setAntiAlias(true);
textRulerPaint.setStyle(Paint.Style.FILL);
textRulerPaint.setTextAlign(Paint.Align.CENTER);
textRulerPaint.setColor(rulerTextColor);//文本颜色
textRulerPaint.setTextSize(sp2px(rulerTextSizeSP));//字体大小
textPointPaint = new Paint();
textPointPaint.setAntiAlias(true);
textPointPaint.setStyle(Paint.Style.FILL);
textPointPaint.setTextAlign(Paint.Align.CENTER);
textPointPaint.setColor(pointTextColor);//文本颜色
textPointPaint.setTextSize(sp2px(pointTextSizeSP));//字体大小
linePath = new Path();
tablePath = new Path();
resetParam();
}
private void initAnim() {
valueAnimator = ValueAnimator.ofFloat(0f, 1f).setDuration(dataList.size() * 150);
valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
currentValue = (float) animation.getAnimatedValue();
postInvalidate();
}
});
valueAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
currentValue = 0f;
isAnimating = true;
}
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
currentValue = 1f;
isAnimating = false;
isPlayAnim = false;
}
});
valueAnimator.setStartDelay(500);
}
private void resetParam() {
linePath.reset();
tablePath.reset();
stepSpace = dip2px(stepSpaceDP);
tablePadding = dip2px(tablePaddingDP);
rulerValuePadding = dip2px(rulerValuePaddingDP);
stepStart = tablePadding * (isShowTable ? 2 : 1);
stepEnd = stepStart + stepSpace * (dataList.size() - 1);
topSpace = bottomSpace = tablePadding;
linePoints = new Point[dataList.size()];
initAnim();
isInitialized = false;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = tablePadding + getTableEnd() + getPaddingLeft() + getPaddingRight();//计算自己的宽度
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);//父类期望的高度
if (MeasureSpec.EXACTLY == heightMode) {
height = getPaddingTop() + getPaddingBottom() + height;
}
setMeasuredDimension(width, height);//设置自己的宽度和高度
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mWidth = w;
mHeight = h;
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.TRANSPARENT);//绘制背景颜色
canvas.translate(0f, mHeight / 2f + (getViewDrawHeight() + topSpace + bottomSpace) / 2f);//设置画布中心点垂直居中
if (!isInitialized) {
setupLine();
}
if (isShowTable) {
drawTable(canvas);//绘制表格
}
drawLine(canvas);//绘制曲线
drawLinePoints(canvas);//绘制曲线上的点
}
private void drawText(Canvas canvas, Paint textPaint, String text, float x, float y) {
canvas.drawText(text, x, y, textPaint);
}
/**
* 绘制标尺y轴文本
*
* @param canvas
* @param text
* @param x
* @param y
*/
private void drawRulerYText(Canvas canvas, String text, float x, float y) {
textRulerPaint.setTextAlign(Paint.Align.RIGHT);
Paint.FontMetrics fontMetrics = textRulerPaint.getFontMetrics();
float fontTotalHeight = fontMetrics.bottom - fontMetrics.top;
float offsetY = fontTotalHeight / 2 - fontMetrics.bottom;
float newY = y + offsetY;
float newX = x - rulerValuePadding;
drawText(canvas, textRulerPaint, text, newX, newY);
}
/**
* 绘制标尺x轴文本
*
* @param canvas
* @param text
* @param x
* @param y
*/
private void drawRulerXText(Canvas canvas, String text, float x, float y) {
textRulerPaint.setTextAlign(Paint.Align.CENTER);
Paint.FontMetrics fontMetrics = textRulerPaint.getFontMetrics();
float fontTotalHeight = fontMetrics.bottom - fontMetrics.top;
float offsetY = fontTotalHeight / 2 - fontMetrics.bottom;
float newY = y + offsetY + rulerValuePadding;
drawText(canvas, textRulerPaint, text, x, newY);
}
/**
* 绘制曲线上锚点文本
*
* @param canvas
* @param text
* @param x
* @param y
*/
private void drawLinePointText(Canvas canvas, String text, float x, float y) {
textPointPaint.setTextAlign(Paint.Align.CENTER);
float newY = y - rulerValuePadding;
drawText(canvas, textPointPaint, text, x, newY);
}
private int getTableStart() {
return isShowTable ? stepStart + tablePadding : stepStart;
}
private int getTableEnd() {
return isShowTable ? stepEnd + tablePadding : stepEnd;
}
/**
* 绘制背景表格
*
* @param canvas
*/
private void drawTable(Canvas canvas) {
int tableEnd = getTableEnd();
int rulerCount = maxValue / rulerValue;
int rulerMaxCount = maxValue % rulerValue > 0 ? rulerCount + 1 : rulerCount;
int rulerMax = rulerValue * rulerMaxCount + rulerValueDefault;
tablePath.moveTo(stepStart, -getValueHeight(rulerMax));//加上顶部的间隔
tablePath.lineTo(stepStart, 0);//标尺y轴
tablePath.lineTo(tableEnd, 0);//标尺x轴
int startValue = minValue - (minValue > 0 ? 0 : minValue % rulerValue);
int endValue = (maxValue + rulerValue);
//标尺y轴连接线
do {
int startHeight = -getValueHeight(startValue);
tablePath.moveTo(stepStart, startHeight);
tablePath.lineTo(tableEnd, startHeight);
//绘制y轴刻度单位
drawRulerYText(canvas, String.valueOf(startValue), stepStart, startHeight);
startValue += rulerValue;
} while (startValue < endValue);
canvas.drawPath(tablePath, tablePaint);
//绘制x轴刻度单位
drawRulerXValue(canvas);
}
/**
* 绘制标尺x轴上所有文本
*
* @param canvas
*/
private void drawRulerXValue(Canvas canvas) {
if (linePoints == null) return;
for (int i = 0; i < linePoints.length; i++) {
Point point = linePoints[i];
if (point == null) break;
drawRulerXText(canvas, String.valueOf(i), linePoints[i].x, 0);
}
}
/**
* 绘制曲线
*
* @param canvas
*/
private void drawLine(Canvas canvas) {
if (isPlayAnim) {
Path dst = new Path();
PathMeasure measure = new PathMeasure(linePath, false);
measure.getSegment(0, currentValue * measure.getLength(), dst, true);
canvas.drawPath(dst, linePaint);
} else {
canvas.drawPath(linePath, linePaint);
}
}
/**
* 绘制曲线上的锚点
*
* @param canvas
*/
private void drawLinePoints(Canvas canvas) {
if (linePoints == null) return;
float pointWidth = dip2px(pointWidthDP) / 2;
int pointCount = linePoints.length;
if (isPlayAnim) {
pointCount = Math.round(currentValue * linePoints.length);
}
for (int i = 0; i < pointCount; i++) {
Point point = linePoints[i];
if (point == null) break;
if (isCubePoint) {
canvas.drawPoint(point.x, point.y, pointPaint);
} else {
canvas.drawCircle(point.x, point.y, pointWidth, pointPaint);
}
//绘制点的文本
drawLinePointText(canvas, String.valueOf(dataList.get(i).getValue()), point.x, point.y);
}
}
/**
* 获取value值所占的view高度
*
* @param value
* @return
*/
private int getValueHeight(int value) {
float valuePercent = Math.abs(value - minValue) * 100f / (Math.abs(maxValue - minValue) * 100f);//计算value所占百分比
return (int) (getViewDrawHeight() * valuePercent + bottomSpace + 0.5f);//底部加上间隔
}
/**
* 获取绘制区域高度
*
* @return
*/
private float getViewDrawHeight() {
return getMeasuredHeight() * heightPercent;
}
/**
* 初始化曲线数据
*/
private void setupLine() {
if (dataList.isEmpty()) return;
int stepTemp = getTableStart();
Point pre = new Point();
pre.set(stepTemp, -getValueHeight(dataList.get(0).getValue()));//坐标系从0,0默认在第四象限绘制
linePoints[0] = pre;
linePath.moveTo(pre.x, pre.y);
if (dataList.size() == 1) {
isInitialized = true;
return;
}
for (int i = 1; i < dataList.size(); i++) {
Data data = dataList.get(i);
Point next = new Point();
next.set(stepTemp += stepSpace, -getValueHeight(data.getValue()));
if (isBezierLine) {
int cW = pre.x + stepSpace / 2;
Point p1 = new Point();//控制点1
p1.set(cW, pre.y);
Point p2 = new Point();//控制点2
p2.set(cW, next.y);
linePath.cubicTo(p1.x, p1.y, p2.x, p2.y, next.x, next.y);//创建三阶贝塞尔曲线
} else {
linePath.lineTo(next.x, next.y);
}
pre = next;
linePoints[i] = next;
}
isInitialized = true;
}
private int dip2px(float dipValue) {
final float scale = getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
private int sp2px(float spValue) {
final float fontScale = getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
}
private void refreshLayout() {
resetParam();
requestLayout();
postInvalidate();
}
/*-------------可操作方法---------------*/
/**
* 设置数据
*
* @param dataList
*/
public void setData(List<Data> dataList) {
if (dataList == null) {
throw new RuntimeException("dataList cannot is null!");
}
if (dataList.isEmpty()) return;
this.dataList.clear();
this.dataList.addAll(dataList);
maxValue = Collections.max(this.dataList, new Comparator<Data>() {
@Override
public int compare(Data o1, Data o2) {
return o1.getValue() - o2.getValue();
}
}).getValue();
minValue = Collections.min(this.dataList, new Comparator<Data>() {
@Override
public int compare(Data o1, Data o2) {
return o1.getValue() - o2.getValue();
}
}).getValue();
refreshLayout();
}
/**
* 设置是否显示标尺表格
*
* @param showTable
*/
public void setShowTable(boolean showTable) {
isShowTable = showTable;
refreshLayout();
}
/**
* 设置是否是贝塞尔曲线
*
* @param isBezier
*/
public void setBezierLine(boolean isBezier) {
isBezierLine = isBezier;
refreshLayout();
}
/**
* 设置锚点形状
*
* @param isCube
*/
public void setCubePoint(boolean isCube) {
isCubePoint = isCube;
refreshLayout();
}
/**
* 设置标尺y轴间距
*
* @param space
*/
public void setRulerYSpace(int space) {
if (space <= 0) {
space = rulerValueDefault;
}
this.rulerValue = space;
refreshLayout();
}
/**
* 设置曲线点的间距,标尺x轴间距
*
* @param dp
*/
public void setStepSpace(int dp) {
if (dp < stepSpaceDefault) {
dp = stepSpaceDefault;
}
this.stepSpaceDP = dp;
refreshLayout();
}
/**
* 设置锚点尺寸
*
* @param dp
*/
public void setPointWidth(float dp) {
if (dp <= 0) {
dp = pointWidthDefault;
}
this.pointWidthDP = dp;
refreshLayout();
}
/**
* 播放动画
*/
public void playAnim() {
this.isPlayAnim = true;
if (isAnimating) return;
if (valueAnimator != null) {
valueAnimator.start();
}
}
public static class Data {
int value;
public Data(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
}
@@ -0,0 +1,129 @@
package com.jeanboy.linechart;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
TextView tv_ruler_y;
SeekBar sb_ruler_space;
TextView tv_step_space;
SeekBar sb_step_space;
LineChartView lineChartView;
private int[] dataArr = new int[]{200, 100, 300, -20, 50, -80, 200, 100, 300, 50, 200, 150, 160, 100, 300, 50, 200, 150,
300, 50, 200, 100, 150, 150};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lineChartView = (LineChartView) findViewById(R.id.line_chart_view);
sb_ruler_space = (SeekBar) findViewById(R.id.sb_ruler_space);
tv_ruler_y = (TextView) findViewById(R.id.tv_ruler_y);
sb_step_space = (SeekBar) findViewById(R.id.sb_step_space);
tv_step_space = (TextView) findViewById(R.id.tv_step_space);
List<LineChartView.Data> datas = new ArrayList<>();
for (int value : dataArr) {
LineChartView.Data data = new LineChartView.Data(value);
datas.add(data);
}
lineChartView.setData(datas);
sb_ruler_space.setMax(70);
sb_ruler_space.setProgress(20);
if (lineChartView != null) {
lineChartView.setRulerYSpace(20);
tv_ruler_y.setText(String.valueOf(20));
}
sb_ruler_space.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (lineChartView != null) {
lineChartView.setRulerYSpace(progress);
tv_ruler_y.setText(String.valueOf(progress));
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
sb_step_space.setMax(70);
sb_step_space.setProgress(15);
if (lineChartView != null) {
lineChartView.setStepSpace(15);
tv_step_space.setText(String.valueOf(15));
}
sb_step_space.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (lineChartView != null) {
lineChartView.setStepSpace(progress);
tv_step_space.setText(String.valueOf(progress));
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
private boolean isShowTable = false;
public void tableToggle(View view) {
if (lineChartView != null) {
isShowTable = !isShowTable;
lineChartView.setShowTable(isShowTable);
}
}
private boolean isBezier = false;
public void bezierModelToggle(View view) {
if (lineChartView != null) {
isBezier = !isBezier;
lineChartView.setBezierLine(isBezier);
}
}
private boolean isCube = false;
public void pointModelToggle(View view) {
if (lineChartView != null) {
isCube = !isCube;
lineChartView.setCubePoint(isCube);
}
}
public void doAnimation(View view) {
if (lineChartView != null) {
lineChartView.playAnim();
}
}
}
@@ -0,0 +1,130 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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"
tools:context="com.jeanboy.linechart.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never"
android:scrollbars="none">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.jeanboy.linechart.LineChartView
android:id="@+id/line_chart_view"
android:layout_width="wrap_content"
android:layout_height="300dp"/>
</LinearLayout>
</HorizontalScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="doAnimation"
android:text="animation"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="bezierModelToggle"
android:text="bezier Toggle"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="pointModelToggle"
android:text="point Toggle"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="tableToggle"
android:text="table Toggle"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginTop="20dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="@dimen/activity_horizontal_margin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Y轴标尺间距:"/>
<TextView
android:id="@+id/tv_ruler_y"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20"/>
</LinearLayout>
<SeekBar
android:id="@+id/sb_ruler_space"
android:layout_width="match_parent"
android:layout_height="40dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="@dimen/activity_horizontal_margin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X轴标尺间距:"/>
<TextView
android:id="@+id/tv_step_space"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="15"/>
</LinearLayout>
<SeekBar
android:id="@+id/sb_step_space"
android:layout_width="match_parent"
android:layout_height="40dp"/>
</LinearLayout>
</FrameLayout>
Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
@@ -0,0 +1,5 @@
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">Android-LineChart</string>
</resources>
@@ -0,0 +1,11 @@
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
@@ -0,0 +1,17 @@
package com.jeanboy.linechart;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}
+23
View File
@@ -0,0 +1,23 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
+17
View File
@@ -0,0 +1,17 @@
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
Binary file not shown.
@@ -0,0 +1,6 @@
#Tue Jun 13 18:16:14 CST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
Binary file not shown.

After

Width:  |  Height:  |  Size: 68 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

+1
View File
@@ -0,0 +1 @@
include ':app'