├── .idea
├── .name
├── copyright
│ └── profiles_settings.xml
├── encodings.xml
├── vcs.xml
├── modules.xml
├── runConfigurations.xml
├── gradle.xml
├── compiler.xml
└── misc.xml
├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── mipmap-hdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── drawable-hdpi
│ │ │ │ └── ico_popincome.png
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── dimens.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── layout
│ │ │ │ ├── activity_chart_in_fragment.xml
│ │ │ │ ├── activity_main.xml
│ │ │ │ ├── activity_linev2.xml
│ │ │ │ ├── fragment_chart.xml
│ │ │ │ ├── activity_outside_line_chart.xml
│ │ │ │ ├── activity_move_select_line_chart.xml
│ │ │ │ └── activity_leaf_chart.xml
│ │ │ ├── values-w820dp
│ │ │ │ └── dimens.xml
│ │ │ └── menu
│ │ │ │ └── menu_leaf_chart.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── beiing
│ │ │ │ └── leafchartdemo
│ │ │ │ ├── ChartInFragmentActivity.java
│ │ │ │ ├── MainActivity.java
│ │ │ │ ├── OutsideLineChartActivity.java
│ │ │ │ ├── SlideSelectLineChartActivity.java
│ │ │ │ ├── ChartFragment.java
│ │ │ │ └── LeafChartActivity.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── beiing
│ │ │ └── leafchartdemo
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── beiing
│ │ └── leafchartdemo
│ │ └── ApplicationTest.java
├── proguard-rules.pro
└── build.gradle
├── leafchart
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ └── values
│ │ │ │ ├── strings.xml
│ │ │ │ └── attrs.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── beiing
│ │ │ │ └── leafchart
│ │ │ │ ├── support
│ │ │ │ ├── OnChartSelectedListener.java
│ │ │ │ ├── Chart.java
│ │ │ │ ├── OnPointSelectListener.java
│ │ │ │ ├── Mode.java
│ │ │ │ └── LeafUtil.java
│ │ │ │ ├── bean
│ │ │ │ ├── ChartData.java
│ │ │ │ ├── AxisValue.java
│ │ │ │ ├── Square.java
│ │ │ │ ├── PointValue.java
│ │ │ │ ├── SlidingLine.java
│ │ │ │ ├── Line.java
│ │ │ │ └── Axis.java
│ │ │ │ ├── renderer
│ │ │ │ ├── LeafSquareRenderer.java
│ │ │ │ ├── SlideSelectLineRenderer.java
│ │ │ │ ├── LeafLineRenderer.java
│ │ │ │ ├── AbsRenderer.java
│ │ │ │ └── OutsideLineRenderer.java
│ │ │ │ ├── LeafSquareChart.java
│ │ │ │ ├── LeafLineChart.java
│ │ │ │ ├── OutsideLineChart.java
│ │ │ │ ├── SlideSelectLineChart.java
│ │ │ │ └── AbsLeafChart.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── beiing
│ │ │ └── leafchart
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── beiing
│ │ └── leafchart
│ │ └── ApplicationTest.java
├── build.gradle
└── proguard-rules.pro
├── settings.gradle
├── screenshot
├── mode.png
├── square.png
├── square2.png
├── cubic_filled.png
├── multi_lines.png
├── slide_select.gif
├── animate_line1.gif
├── animate_line2.gif
└── fold_not_filled.png
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── gradle.properties
├── gradlew.bat
├── 类介绍.md
├── gradlew
├── README.md
└── LICENSE
/.idea/.name:
--------------------------------------------------------------------------------
1 | LeafChart
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/leafchart/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':leafchart'
2 |
--------------------------------------------------------------------------------
/screenshot/mode.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LineChen/LeafChart/HEAD/screenshot/mode.png
--------------------------------------------------------------------------------
/screenshot/square.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LineChen/LeafChart/HEAD/screenshot/square.png
--------------------------------------------------------------------------------
/screenshot/square2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LineChen/LeafChart/HEAD/screenshot/square2.png
--------------------------------------------------------------------------------
/screenshot/cubic_filled.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LineChen/LeafChart/HEAD/screenshot/cubic_filled.png
--------------------------------------------------------------------------------
/screenshot/multi_lines.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LineChen/LeafChart/HEAD/screenshot/multi_lines.png
--------------------------------------------------------------------------------
/screenshot/slide_select.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LineChen/LeafChart/HEAD/screenshot/slide_select.gif
--------------------------------------------------------------------------------
/screenshot/animate_line1.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LineChen/LeafChart/HEAD/screenshot/animate_line1.gif
--------------------------------------------------------------------------------
/screenshot/animate_line2.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LineChen/LeafChart/HEAD/screenshot/animate_line2.gif
--------------------------------------------------------------------------------
/screenshot/fold_not_filled.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LineChen/LeafChart/HEAD/screenshot/fold_not_filled.png
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LineChen/LeafChart/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/leafchart/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | leafchart
3 |
4 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LineChen/LeafChart/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LineChen/LeafChart/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LineChen/LeafChart/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LineChen/LeafChart/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LineChen/LeafChart/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-hdpi/ico_popincome.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LineChen/LeafChart/HEAD/app/src/main/res/drawable-hdpi/ico_popincome.png
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | LeafChart
3 |
4 |
5 | Hello blank fragment
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Fri Mar 24 10:03:47 CST 2017
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
7 |
--------------------------------------------------------------------------------
/leafchart/src/main/java/com/beiing/leafchart/support/OnChartSelectedListener.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchart.support;
2 |
3 | /**
4 | * Created by Administrator on 2017/5/9.
5 | */
6 |
7 | public interface OnChartSelectedListener {
8 | void onChartSelected(boolean isChartSelected);
9 | }
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 | #00000000
7 |
8 |
--------------------------------------------------------------------------------
/leafchart/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_chart_in_fragment.xml:
--------------------------------------------------------------------------------
1 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/leafchart/src/main/java/com/beiing/leafchart/support/Chart.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchart.support;
2 |
3 | import com.beiing.leafchart.bean.Axis;
4 |
5 | /**
6 | * Created by chenliu on 2016/7/15.
7 | * 描述:
8 | *
9 | */
10 | public interface Chart {
11 |
12 | void setAxisX(Axis axisX);
13 |
14 | void setAxisY(Axis axisY);
15 |
16 | Axis getAxisX();
17 |
18 | Axis getAxisY();
19 | }
20 |
--------------------------------------------------------------------------------
/app/src/test/java/com/beiing/leafchartdemo/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchartdemo;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants chartView.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/leafchart/src/test/java/com/beiing/leafchart/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchart;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants chartView.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/beiing/leafchartdemo/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchartdemo;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/leafchart/src/androidTest/java/com/beiing/leafchart/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchart;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/leafchart/src/main/java/com/beiing/leafchart/support/OnPointSelectListener.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchart.support;
2 |
3 | /**
4 | * Created by chenliu on 2016/12/12.
5 | * 描述:
6 | *
7 | */
8 | public interface OnPointSelectListener {
9 |
10 | /**
11 | * @param position x轴位置
12 | * @param xLabel x轴对应刻度值
13 | * @param value 对应点数值
14 | */
15 | void onPointSelect(int position, String xLabel, String value);
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/app/src/main/java/com/beiing/leafchartdemo/ChartInFragmentActivity.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchartdemo;
2 |
3 | import android.support.v7.app.AppCompatActivity;
4 | import android.os.Bundle;
5 |
6 | public class ChartInFragmentActivity extends AppCompatActivity {
7 |
8 | @Override
9 | protected void onCreate(Bundle savedInstanceState) {
10 | super.onCreate(savedInstanceState);
11 | setContentView(R.layout.activity_chart_in_fragment);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/leafchart/src/main/java/com/beiing/leafchart/support/Mode.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchart.support;
2 |
3 | /**
4 | * Created by chenliu on 2016/11/24.
5 | * 描述:
6 | *
7 | */
8 | public interface Mode {
9 | /**
10 | * 交叉,x、y轴都超出0点
11 | */
12 | int ACROSS = 1;
13 |
14 | /**
15 | * 相交, x、y轴交于0点
16 | */
17 | int INTERSECT = 2;
18 |
19 | /**
20 | * x轴超出0点
21 | */
22 | int X_ACROSS = 3;
23 |
24 | /**
25 | * y轴超出0点
26 | */
27 | int Y_ACROSS = 4;
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/leafchart/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion '25.0.0'
6 |
7 | defaultConfig {
8 | minSdkVersion 15
9 | targetSdkVersion 23
10 | versionCode 1
11 | versionName "1.0"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | compile fileTree(dir: 'libs', include: ['*.jar'])
23 | testCompile 'junit:junit:4.12'
24 | compile 'com.android.support:appcompat-v7:23.3.0'
25 | }
26 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_leaf_chart.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/app/src/main/java/com/beiing/leafchartdemo/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchartdemo;
2 |
3 | import android.content.Intent;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.os.Bundle;
6 | import android.view.View;
7 |
8 | import java.util.ArrayList;
9 | import java.util.List;
10 |
11 | public class MainActivity extends AppCompatActivity {
12 |
13 |
14 | @Override
15 | protected void onCreate(Bundle savedInstanceState) {
16 | super.onCreate(savedInstanceState);
17 | setContentView(R.layout.activity_main);
18 | }
19 |
20 |
21 | public void gotoFuckChart(View view) {
22 | startActivity( new Intent(this, LeafChartActivity.class));
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in D:\develope_software\AndroidSDK/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/leafchart/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in D:\develope_software\AndroidSDK/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_linev2.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion '25.0.0'
6 |
7 | defaultConfig {
8 | applicationId "com.beiing.linechartdemo"
9 | minSdkVersion 15
10 | targetSdkVersion 23
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(dir: 'libs', include: ['*.jar'])
24 | testCompile 'junit:junit:4.12'
25 | compile project(path: ':leafchart')
26 | compile 'com.android.support:appcompat-v7:23.4.0'
27 | compile 'com.android.support:support-v4:23.4.0'
28 | }
29 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/leafchart/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_chart.xml:
--------------------------------------------------------------------------------
1 |
8 |
9 |
19 |
20 |
21 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_outside_line_chart.xml:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
22 |
23 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/leafchart/src/main/java/com/beiing/leafchart/bean/ChartData.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchart.bean;
2 |
3 | import android.graphics.Color;
4 |
5 | import java.util.ArrayList;
6 | import java.util.List;
7 |
8 | /**
9 | * Created by chenliu on 2016/7/17 0017
.
10 | * 描述:不同类型图表相同属性
11 | */
12 | public class ChartData {
13 |
14 | protected List values = new ArrayList<>();
15 |
16 | protected boolean hasLabels = false;// 是否画标签
17 |
18 | protected int labelColor = Color.DKGRAY;//标签背景色
19 |
20 | protected float labelRadius = 3; //dp
21 |
22 | public List getValues() {
23 | return values;
24 | }
25 |
26 | public ChartData setValues(List values) {
27 | this.values = values;
28 | return this;
29 | }
30 |
31 | public boolean isHasLabels() {
32 | return hasLabels;
33 | }
34 |
35 | public ChartData setHasLabels(boolean hasLabels) {
36 | this.hasLabels = hasLabels;
37 | return this;
38 | }
39 |
40 | public int getLabelColor() {
41 | return labelColor;
42 | }
43 |
44 | public ChartData setLabelColor(int labelColor) {
45 | this.labelColor = labelColor;
46 | return this;
47 | }
48 |
49 | public float getLabelRadius() {
50 | return labelRadius;
51 | }
52 |
53 | public ChartData setLabelRadius(float labelRadius) {
54 | this.labelRadius = labelRadius;
55 | return this;
56 | }
57 | }
58 |
59 |
60 |
--------------------------------------------------------------------------------
/leafchart/src/main/java/com/beiing/leafchart/bean/AxisValue.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchart.bean;
2 |
3 | /**
4 | * Created by chenliu on 2016/7/14.
5 | * 描述:坐标轴上点的值
6 | *
7 | */
8 | public class AxisValue {
9 | /**刻度值**/
10 | private String label;
11 |
12 | /**x坐标**/
13 | private float pointX;
14 |
15 | /**y坐标**/
16 | private float pointY;
17 |
18 | /**是否显示刻度值**/
19 | private boolean isShowLabel = true;
20 |
21 | public AxisValue(){
22 |
23 | }
24 |
25 | public AxisValue(String label){
26 | this.label = label;
27 | }
28 |
29 | public String getLabel() {
30 | return label;
31 | }
32 |
33 | public void setLabel(String label) {
34 | this.label = label;
35 | }
36 |
37 | public float getPointX() {
38 | return pointX;
39 | }
40 |
41 | public void setPointX(float pointX) {
42 | this.pointX = pointX;
43 | }
44 |
45 | public float getPointY() {
46 | return pointY;
47 | }
48 |
49 | public void setPointY(float pointY) {
50 | this.pointY = pointY;
51 | }
52 |
53 | public boolean isShowLabel() {
54 | return isShowLabel;
55 | }
56 |
57 | public void setShowLabel(boolean showLabel) {
58 | isShowLabel = showLabel;
59 | }
60 |
61 | @Override
62 | public String toString() {
63 | return "AxisValue{" +
64 | "label='" + label + '\'' +
65 | ", pointX=" + pointX +
66 | ", pointY=" + pointY +
67 | '}';
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/leafchart/src/main/java/com/beiing/leafchart/renderer/LeafSquareRenderer.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchart.renderer;
2 |
3 | import android.content.Context;
4 | import android.graphics.Canvas;
5 | import android.graphics.Paint;
6 | import android.graphics.RectF;
7 | import android.view.View;
8 |
9 | import com.beiing.leafchart.bean.Axis;
10 | import com.beiing.leafchart.bean.PointValue;
11 | import com.beiing.leafchart.bean.Square;
12 | import com.beiing.leafchart.support.LeafUtil;
13 |
14 | import java.util.List;
15 |
16 | /**
17 | * Created by chenliu on 2017/1/10.
18 | * 描述:
19 | *
20 | */
21 |
22 | public class LeafSquareRenderer extends AbsRenderer {
23 |
24 | public LeafSquareRenderer(Context context, View view) {
25 | super(context, view);
26 | }
27 |
28 | public void drawSquares(Canvas canvas, Square square, Axis axisX) {
29 | if (square != null) {
30 | //1.画直方图边界
31 | linePaint.setColor(square.getBorderColor());
32 | if(!square.isFill()){
33 | linePaint.setStrokeWidth(LeafUtil.dp2px(mContext, square.getBorderWidth()));
34 | linePaint.setStyle(Paint.Style.STROKE);
35 | }
36 | List values = square.getValues();
37 | float width = LeafUtil.dp2px(mContext, square.getWidth());
38 | for (PointValue point : values) {
39 | RectF rectF = new RectF(point.getOriginX() - width / 2,
40 | point.getOriginY(), point.getOriginX() + width / 2, axisX.getStartY());
41 |
42 | canvas.drawRect(rectF, linePaint);
43 | }
44 | }
45 | }
46 |
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/leafchart/src/main/java/com/beiing/leafchart/bean/Square.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchart.bean;
2 |
3 | import android.graphics.Color;
4 |
5 | import java.util.List;
6 |
7 | /**
8 | * Created by chenliu on 2016/9/10.
9 | * 描述:直方图
10 | *
11 | */
12 | public class Square extends ChartData {
13 | /**
14 | * 直方图宽度
15 | */
16 | private int width = 10;
17 |
18 | /**
19 | * 边框宽度
20 | */
21 | private int borderWidth = 1;
22 |
23 | /**
24 | * 边框颜色
25 | */
26 | private int borderColor = Color.GRAY;
27 |
28 | /**
29 | * 是否填充
30 | */
31 | private boolean isFill = false;
32 |
33 |
34 | public Square(List values){
35 | this.values = values;
36 | }
37 |
38 |
39 | public int getWidth() {
40 | return width;
41 | }
42 |
43 | public Square setWidth(int width) {
44 | this.width = width;
45 | return this;
46 | }
47 |
48 | public int getBorderWidth() {
49 | return borderWidth;
50 | }
51 |
52 | public Square setBorderWidth(int borderWidth) {
53 | this.borderWidth = borderWidth;
54 | return this;
55 | }
56 |
57 | public int getBorderColor() {
58 | return borderColor;
59 | }
60 |
61 | public Square setBorderColor(int borderColor) {
62 | this.borderColor = borderColor;
63 | return this;
64 | }
65 |
66 | public boolean isFill() {
67 | return isFill;
68 | }
69 |
70 | public Square setFill(boolean fill) {
71 | isFill = fill;
72 | return this;
73 | }
74 | }
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_move_select_line_chart.xml:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
22 |
23 |
32 |
33 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/leafchart/src/main/java/com/beiing/leafchart/bean/PointValue.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchart.bean;
2 |
3 | /**
4 | * Created by chenliu on 2016/7/14.
5 | * 描述:点
6 | *
7 | */
8 | public class PointValue {
9 |
10 | private float x; //占x轴总长度的权重
11 | private float y;
12 | private float diffX;
13 | private float diffY;
14 | private float originX; // 点坐标
15 | private float originY;
16 | private String label;
17 |
18 | private boolean isShowLabel; //是否显示label
19 |
20 | public PointValue() {
21 |
22 | }
23 |
24 | public PointValue(String label) {
25 | this.label = label;
26 | }
27 |
28 | public float getOriginX() {
29 | return originX;
30 | }
31 |
32 | public PointValue setOriginX(float originX) {
33 | this.originX = originX;
34 | return this;
35 | }
36 |
37 | public float getOriginY() {
38 | return originY;
39 | }
40 |
41 | public PointValue setOriginY(float originY) {
42 | this.originY = originY;
43 | return this;
44 | }
45 |
46 | public float getX() {
47 | return x;
48 | }
49 |
50 | public void setX(float x) {
51 | this.x = x;
52 | }
53 |
54 | public float getY() {
55 | return y;
56 | }
57 |
58 | public void setY(float y) {
59 | this.y = y;
60 | }
61 |
62 | public float getDiffX() {
63 | return diffX;
64 | }
65 |
66 | public void setDiffX(float diffX) {
67 | this.diffX = diffX;
68 | }
69 |
70 | public float getDiffY() {
71 | return diffY;
72 | }
73 |
74 | public void setDiffY(float diffY) {
75 | this.diffY = diffY;
76 | }
77 |
78 | public String getLabel() {
79 | return label;
80 | }
81 |
82 | public void setLabel(String label) {
83 | this.label = label;
84 | }
85 |
86 | public boolean isShowLabel() {
87 | return isShowLabel;
88 | }
89 |
90 | public void setShowLabel(boolean showLabel) {
91 | isShowLabel = showLabel;
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/leafchart/src/main/java/com/beiing/leafchart/bean/SlidingLine.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchart.bean;
2 |
3 | import android.graphics.Color;
4 |
5 | /**
6 | * Created by chenliu on 2016/12/19.
7 | * 描述:滑动标尺线
8 | *
9 | */
10 |
11 | public class SlidingLine {
12 |
13 | /**是否开启滑动标尺**/
14 | private boolean isOpenSlideSelect = true;
15 |
16 | /**是否是虚线**/
17 | private boolean isDash = true;
18 |
19 | /**移动标尺线颜色**/
20 | private int slideLineColor = Color.YELLOW;
21 |
22 | /**移动标尺线宽度**/
23 | private float slideLineWidth = 1;
24 |
25 | /**移动标尺线顶端圆点半径**/
26 | private float slidePointRadius = 3;
27 |
28 | /**移动标尺线顶端圆点颜色**/
29 | private int slidePointColor = Color.YELLOW;
30 |
31 | public SlidingLine setOpenSlideSelect(boolean openSlideSelect) {
32 | isOpenSlideSelect = openSlideSelect;
33 | return this;
34 | }
35 |
36 | public SlidingLine setDash(boolean dash) {
37 | isDash = dash;
38 | return this;
39 | }
40 |
41 | public SlidingLine setSlideLineColor(int slideLineColor) {
42 | this.slideLineColor = slideLineColor;
43 | return this;
44 | }
45 |
46 | public SlidingLine setSlideLineWidth(float slideLineWidth) {
47 | this.slideLineWidth = slideLineWidth;
48 | return this;
49 | }
50 |
51 | public SlidingLine setSlidePointRadius(float slidePointRadius) {
52 | this.slidePointRadius = slidePointRadius;
53 | return this;
54 | }
55 |
56 | public SlidingLine setSlidePointColor(int slidePointColor) {
57 | this.slidePointColor = slidePointColor;
58 | return this;
59 | }
60 |
61 | public boolean isOpenSlideSelect() {
62 | return isOpenSlideSelect;
63 | }
64 |
65 | public boolean isDash() {
66 | return isDash;
67 | }
68 |
69 | public int getSlideLineColor() {
70 | return slideLineColor;
71 | }
72 |
73 | public float getSlideLineWidth() {
74 | return slideLineWidth;
75 | }
76 |
77 | public float getSlidePointRadius() {
78 | return slidePointRadius;
79 | }
80 |
81 | public int getSlidePointColor() {
82 | return slidePointColor;
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/leafchart/src/main/java/com/beiing/leafchart/LeafSquareChart.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchart;
2 |
3 | import android.content.Context;
4 | import android.graphics.Canvas;
5 | import android.util.AttributeSet;
6 |
7 | import com.beiing.leafchart.bean.ChartData;
8 | import com.beiing.leafchart.bean.Square;
9 | import com.beiing.leafchart.renderer.LeafSquareRenderer;
10 | import com.beiing.leafchart.support.LeafUtil;
11 |
12 |
13 | /**
14 | * Created by chenliu on 2016/9/10.
15 | * 描述:直方图
16 | *
17 | */
18 | public class LeafSquareChart extends AbsLeafChart {
19 |
20 | private Square square;
21 |
22 | LeafSquareRenderer leafSquareRenderer;
23 |
24 | public LeafSquareChart(Context context) {
25 | this(context, null, 0);
26 | }
27 |
28 | public LeafSquareChart(Context context, AttributeSet attrs) {
29 | this(context, attrs, 0);
30 | }
31 |
32 | public LeafSquareChart(Context context, AttributeSet attrs, int defStyleAttr) {
33 | super(context, attrs, defStyleAttr);
34 |
35 | if(startMarginX == 0) startMarginX = (int) LeafUtil.dp2px(context, 20);
36 | }
37 |
38 | @Override
39 | protected void initRenderer() {
40 | leafSquareRenderer = new LeafSquareRenderer(mContext, this);
41 | }
42 |
43 | @Override
44 | protected void setRenderer() {
45 | super.setRenderer(leafSquareRenderer);
46 | }
47 |
48 | @Override
49 | protected void onSizeChanged(int w, int h, int oldw, int oldh) {
50 | super.onSizeChanged(w, h, oldw, oldh);
51 | }
52 |
53 | @Override
54 | protected void onDraw(Canvas canvas) {
55 | super.onDraw(canvas);
56 |
57 | leafSquareRenderer.drawSquares(canvas, square, axisX);
58 |
59 | if (square != null && square.isHasLabels()) {
60 | leafSquareRenderer.drawLabels(canvas, square, axisY);
61 | }
62 | }
63 |
64 | @Override
65 | protected void resetPointWeight() {
66 | super.resetPointWeight(square);
67 | }
68 |
69 | public void setChartData(ChartData chartData) {
70 | this.square = (Square) chartData;
71 | resetPointWeight();
72 | }
73 |
74 | public ChartData getChartData() {
75 | return square;
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/leafchart/src/main/java/com/beiing/leafchart/renderer/SlideSelectLineRenderer.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchart.renderer;
2 |
3 | import android.content.Context;
4 | import android.graphics.Canvas;
5 | import android.graphics.Color;
6 | import android.graphics.DashPathEffect;
7 | import android.graphics.Paint;
8 | import android.graphics.Path;
9 | import android.view.View;
10 |
11 | import com.beiing.leafchart.bean.Axis;
12 | import com.beiing.leafchart.bean.SlidingLine;
13 | import com.beiing.leafchart.support.LeafUtil;
14 |
15 | /**
16 | * Created by chenliu on 2017/1/9.
17 | * 描述:
18 | *
19 | */
20 |
21 | public class SlideSelectLineRenderer extends LeafLineRenderer {
22 | /**移动标尺线**/
23 | private Paint slidePaint;
24 |
25 | public SlideSelectLineRenderer(Context context, View view) {
26 | super(context, view);
27 | }
28 |
29 | @Override
30 | protected void initPaint() {
31 | super.initPaint();
32 | slidePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
33 | }
34 |
35 | /**
36 | * 竖直滑动标尺线
37 | * @param canvas
38 | */
39 | public void drawSlideLine(Canvas canvas, Axis axisX, SlidingLine slidingLine, float moveX, float moveY) {
40 | slidePaint.setStrokeWidth(LeafUtil.dp2px(mContext, 1));
41 | slidePaint.setColor(slidingLine.getSlideLineColor());
42 | if(slidingLine.isDash()){
43 | float dash = LeafUtil.dp2px(mContext, 2);
44 | slidePaint.setPathEffect(new DashPathEffect(new float[]{dash, dash, dash, dash}, 0));
45 | }
46 | Path path = new Path();
47 | path.moveTo(moveX, moveY);
48 | path.lineTo(moveX, axisX.getStartY());
49 | canvas.drawPath(path, slidePaint);
50 |
51 | slidePaint.setPathEffect(null);
52 | slidePaint.setStyle(Paint.Style.FILL);
53 | slidePaint.setColor(Color.WHITE);
54 | float slidePointRadius = slidingLine.getSlidePointRadius();
55 | canvas.drawCircle(moveX, moveY, LeafUtil.dp2px(mContext, slidePointRadius) , slidePaint);
56 | slidePaint.setStyle(Paint.Style.STROKE);
57 | slidePaint.setStrokeWidth(LeafUtil.dp2px(mContext, 2));
58 | slidePaint.setColor(slidingLine.getSlidePointColor());
59 | canvas.drawCircle(moveX, moveY, LeafUtil.dp2px(mContext, slidePointRadius) , slidePaint);
60 | if(slidingLine.getSlidePointColor() != 0){
61 | slidePaint.setAlpha(100);
62 | canvas.drawCircle(moveX, moveY, LeafUtil.dp2px(mContext, slidePointRadius + 2) , slidePaint);
63 | }
64 | }
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_leaf_chart.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
21 |
22 |
28 |
29 |
35 |
36 |
42 |
43 |
46 |
50 |
51 |
57 |
58 |
66 |
67 |
68 |
69 |
70 |
71 |
--------------------------------------------------------------------------------
/类介绍.md:
--------------------------------------------------------------------------------
1 |
2 | >AxisValue 坐标轴刻度
3 |
4 | | 类型 | 属性 | 介绍 |
5 | |--------|--------|--------|
6 | | String | label | 刻度值 |
7 | | float | pointX | x 坐标 |
8 | | float | pointY | y 坐标 |
9 | | boolean | isShowLabel | 是否显示刻度值 |
10 |
11 | _ _ _
12 |
13 |
14 | >Axis 坐标轴
15 |
16 | | 类型 | 属性 | 介绍 |
17 | |--------|--------|--------|
18 | | List | values | 刻度集合 |
19 | | boolean | hasLines | 是否连线 |
20 | | boolean | isShowText | 是否显示坐标轴刻度值 |
21 | | Typeface | typeface | 刻度字体 |
22 | | int | textSize | 刻度字体大小 |
23 | | int | textColor | 刻度字体颜色 |
24 | | int | axisColor | x/y轴颜色 |
25 | | float | axisWidth | x/y轴宽度 |
26 | | int | axisLineColor | 平行于x/y轴的坐标轴颜色 |
27 | | float | axisLineWidth | 平行于x/y轴的坐标轴宽度 |
28 | | float | startX | 坐标轴起点x坐标 |
29 | | float | startY | 坐标轴起点y坐标 |
30 | | float | stopX | 坐标轴终点x坐标 |
31 | | float | stopY | 坐标轴终点y坐标 |
32 |
33 | _ _ _
34 |
35 | >PointValue 点
36 |
37 | | 类型 | 属性 | 介绍 |
38 | |--------|--------|--------|
39 | | float | x | 占x轴总长度权重 |
40 | | float | y | 占x轴总长度权重 |
41 | | float | diffX | 距离原点长度 |
42 | | float | diffY | 距离原点长度 |
43 | | float | originX | x坐标 |
44 | | float | originY | y坐标 |
45 | | String | label | 标签 |
46 |
47 | _ _ _
48 |
49 | >ChartData 图表数据
50 |
51 | | 类型 | 属性 | 介绍 |
52 | |--------|--------|--------|
53 | | List | values | 点集合 |
54 | | boolean | hasLabels | 是否显示标签 |
55 | | int | labelColor | 标签背景色 |
56 | | float | labelRadius | 标签背景弧度 |
57 |
58 |
59 | _ _ _
60 |
61 | > Line extends ChartData 折线图
62 |
63 | | 类型 | 属性 | 介绍 |
64 | |--------|--------|--------|
65 | | int | lineColor | 折线颜色 |
66 | | float | lineWidth | 折线的宽度 |
67 | | boolean | hasPoints | 是否画圆点 |
68 | | boolean | hasLines | 是否画线条 |
69 | | int | pointColor | 圆点颜色 |
70 | | float | pointRadius | 圆点半径 |
71 | | boolean | isCubic | 是否是曲线 |
72 | | boolean | isFill | 是否填充 |
73 | | int | fillColor | 填充色 |
74 |
75 | > SlidingLine
76 |
77 | | 类型 | 属性 | 介绍 |
78 | |--------|--------|--------|
79 | |boolean|isOpenSlideSelect|是否开启滑动标尺|
80 | |boolean|isDash|是否是虚线|
81 | |int|slideLineColor|移动标尺线颜色|
82 | |float|slideLineWidth|移动标尺线宽度|
83 | |float|slidePointRadius|移动标尺线顶端圆点半径|
84 | |int|slidePointColor|移动标尺线顶端圆点颜色|
85 |
86 |
87 | _ _ _
88 |
89 | > Square extends ChartData 直方图
90 |
91 | | 类型 | 属性 | 介绍 |
92 | |--------|--------|--------|
93 | | int | width | 直方图宽度 |
94 | | int | borderWidth | 边界宽度 |
95 | | int | borderColor | 边界颜色 |
96 | | boolean | isFill | 是否填充 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
--------------------------------------------------------------------------------
/leafchart/src/main/java/com/beiing/leafchart/support/LeafUtil.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchart.support;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.content.res.Resources;
6 | import android.graphics.Bitmap;
7 | import android.graphics.BitmapFactory;
8 | import android.graphics.Canvas;
9 | import android.graphics.Color;
10 | import android.graphics.Paint;
11 | import android.graphics.Rect;
12 | import android.view.WindowManager;
13 |
14 | /**
15 | * Created by chenliu on 2016/5/9.
16 | * 描述:
17 | *
18 | */
19 | public class LeafUtil {
20 |
21 | /**
22 | * 将sp值转换为px值,保证文字大小不变
23 | *
24 | * @param context
25 | * @param spValue
26 | * (DisplayMetrics类中属性scaledDensity)
27 | * @return
28 | */
29 | public static float sp2px(Context context, float spValue) {
30 | final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
31 | return spValue * fontScale;
32 | }
33 |
34 |
35 | public static float dp2px(Context context, float dpValue){
36 | float density = context.getResources().getDisplayMetrics().density;
37 | return dpValue * density;
38 | }
39 |
40 | /**
41 | *
42 | * @param activity
43 | * @param alpha
44 | */
45 | public static void setWindowAlpha(Activity activity, float alpha){
46 | WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
47 | lp.alpha = alpha;
48 | activity.getWindow().setAttributes(lp);
49 | }
50 |
51 |
52 | /**
53 | * 给定背景图,得到一个带有文字的Bitmap
54 | * @param gContext
55 | * @param gResId
56 | * @param gText
57 | * @return
58 | */
59 | public static Bitmap getBitMapWithText(Context gContext, int gResId, String gText) {
60 | Resources resources = gContext.getResources();
61 | float scale = resources.getDisplayMetrics().density;
62 | Bitmap bitmap = BitmapFactory.decodeResource(resources, gResId);
63 |
64 | Bitmap.Config bitmapConfig =
65 | bitmap.getConfig();
66 | // set default bitmap config if none
67 | if (bitmapConfig == null) {
68 | bitmapConfig = Bitmap.Config.ARGB_8888;
69 | }
70 | // resource bitmaps are imutable,
71 | // so we need to convert it to mutable one
72 | bitmap = bitmap.copy(bitmapConfig, true);
73 | Canvas canvas = new Canvas(bitmap);
74 | // new antialised Paint
75 | Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
76 | // text color - #3D3D3D
77 | paint.setColor(Color.WHITE);
78 | // text size in pixels
79 | paint.setTextSize((int) (12 * scale));
80 | // text shadow
81 | // paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);
82 | // draw text to the Canvas center
83 | Rect bounds = new Rect();
84 | paint.getTextBounds(gText, 0, gText.length(), bounds);
85 | int x = (bitmap.getWidth() - bounds.width()) / 2;
86 | int y = (bitmap.getHeight() + bounds.height()) / 2 - 5;
87 | canvas.drawText(gText, x, y, paint);
88 | return bitmap;
89 | }
90 |
91 |
92 |
93 |
94 | }
95 |
--------------------------------------------------------------------------------
/leafchart/src/main/java/com/beiing/leafchart/LeafLineChart.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchart;
2 |
3 | import android.content.Context;
4 | import android.graphics.Canvas;
5 | import android.util.AttributeSet;
6 |
7 | import com.beiing.leafchart.bean.Line;
8 | import com.beiing.leafchart.renderer.LeafLineRenderer;
9 |
10 | import java.util.List;
11 |
12 | /**
13 | * Created by chenliu on 2016/7/15.
14 | * 描述:折线图
15 | *
16 | */
17 | public class LeafLineChart extends AbsLeafChart {
18 |
19 | private List lines;
20 |
21 | private LeafLineRenderer leafChartRenderer;
22 |
23 | public LeafLineChart(Context context) {
24 | this(context, null, 0);
25 | }
26 |
27 | public LeafLineChart(Context context, AttributeSet attrs) {
28 | this(context, attrs, 0);
29 | }
30 |
31 | public LeafLineChart(Context context, AttributeSet attrs, int defStyleAttr) {
32 | super(context, attrs, defStyleAttr);
33 | }
34 |
35 | @Override
36 | protected void initRenderer() {
37 | leafChartRenderer = new LeafLineRenderer(mContext, this);
38 | }
39 |
40 | @Override
41 | protected void setRenderer() {
42 | super.setRenderer(leafChartRenderer);
43 | }
44 |
45 | @Override
46 | protected void onSizeChanged(int w, int h, int oldw, int oldh) {
47 | super.onSizeChanged(w, h, oldw, oldh);
48 | }
49 |
50 | /**
51 | * 设置点所占比重
52 | */
53 | @Override
54 | protected void resetPointWeight() {
55 | if (lines != null) {
56 | for (int i = 0, size = lines.size(); i < size; i++) {
57 | super.resetPointWeight(lines.get(i));
58 | }
59 | }
60 | }
61 |
62 | @Override
63 | protected void onDraw(Canvas canvas) {
64 | super.onDraw(canvas);
65 |
66 | if (lines != null && lines.size() > 0) {
67 | Line line;
68 | for (int i = 0, size = lines.size(); i < size; i++) {
69 | line = lines.get(i);
70 | if(line != null){
71 | if(line.isCubic()) {
72 | leafChartRenderer.drawCubicPath(canvas, line);
73 | } else {
74 | leafChartRenderer.drawLines(canvas, line);
75 | }
76 | if(line.isFill()){
77 | //填充
78 | leafChartRenderer.drawFillArea(canvas, line, axisX);
79 | }
80 |
81 | leafChartRenderer.drawPoints(canvas, line);
82 | }
83 |
84 | if (line != null && line.isHasLabels()) {
85 | leafChartRenderer.drawLabels(canvas, line, axisY);
86 | }
87 | }
88 | }
89 | }
90 |
91 | /**
92 | * 带动画的绘制
93 | * @param duration
94 | */
95 | public void showWithAnimation(int duration){
96 | leafChartRenderer.showWithAnimation(duration);
97 | }
98 |
99 | public void show(){
100 | showWithAnimation(0);
101 | }
102 |
103 | public void setChartData(List chartDatas) {
104 | lines = chartDatas;
105 | resetPointWeight();
106 | }
107 |
108 | public List getChartData() {
109 | return lines;
110 | }
111 |
112 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/beiing/leafchartdemo/OutsideLineChartActivity.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchartdemo;
2 |
3 | import android.graphics.Color;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.os.Bundle;
6 |
7 | import com.beiing.leafchart.OutsideLineChart;
8 | import com.beiing.leafchart.bean.Axis;
9 | import com.beiing.leafchart.bean.AxisValue;
10 | import com.beiing.leafchart.bean.Line;
11 | import com.beiing.leafchart.bean.PointValue;
12 |
13 | import java.util.ArrayList;
14 | import java.util.List;
15 |
16 | public class OutsideLineChartActivity extends AppCompatActivity {
17 | OutsideLineChart outsideLineChart;
18 | @Override
19 | protected void onCreate(Bundle savedInstanceState) {
20 | super.onCreate(savedInstanceState);
21 | setContentView(R.layout.activity_outside_line_chart);
22 |
23 | outsideLineChart = (OutsideLineChart) findViewById(R.id.outside_linechart);
24 |
25 | initLineChart();
26 | }
27 |
28 |
29 | private void initLineChart() {
30 | Axis axisX = new Axis(getAxisValuesX());
31 | axisX.setAxisColor(Color.parseColor("#00000000")).setTextColor(Color.DKGRAY).setHasLines(true);
32 | Axis axisY = new Axis(getAxisValuesY());
33 | axisY.setAxisColor(Color.parseColor("#33B5E5")).setTextColor(Color.DKGRAY).setHasLines(false).setShowText(true);
34 | outsideLineChart.setAxisX(axisX);
35 | outsideLineChart.setAxisY(axisY);
36 |
37 | outsideLineChart.setChartData(getFoldLine());
38 |
39 | outsideLineChart.showWithAnimation(1000);
40 |
41 | }
42 |
43 | private List getAxisValuesX(){
44 | List axisValues = new ArrayList<>();
45 | for (int i = 1; i <= 30; i++) {
46 | AxisValue value = new AxisValue();
47 | value.setLabel(i + "日");
48 | axisValues.add(value);
49 | }
50 | return axisValues;
51 | }
52 |
53 | private List getAxisValuesY(){
54 | List axisValues = new ArrayList<>();
55 | for (int i = 0; i < 11; i++) {
56 | AxisValue value = new AxisValue();
57 | value.setLabel(String.valueOf(i * 10));
58 | axisValues.add(value);
59 | }
60 | return axisValues;
61 | }
62 |
63 | private Line getFoldLine(){
64 | List pointValues = new ArrayList<>();
65 |
66 | PointValue p = new PointValue();
67 | p.setX( (1 - 1) / 11f);
68 | p.setLabel(String.valueOf(90));
69 | p.setY(90 / 100f);
70 | pointValues.add(p);
71 | for (int i = 2; i <= 12; i++) {
72 | PointValue pointValue = new PointValue();
73 | pointValue.setX( (i - 1) / 11f);
74 | int var = (int) (Math.random() * 100);
75 | // int var = 20;
76 | pointValue.setLabel(String.valueOf(var));
77 | pointValue.setY(var / 100f);
78 | pointValues.add(pointValue);
79 | }
80 |
81 |
82 | Line line = new Line(pointValues);
83 | line.setLineColor(Color.parseColor("#33B5E5"))
84 | .setLineWidth(3)
85 | .setPointColor(Color.RED)
86 | .setPointRadius(3)
87 | .setHasPoints(true)
88 | .setFill(true)
89 | .setFillColor(Color.parseColor("#33B5E5"))
90 | .setHasLabels(true)
91 | .setLabelColor(Color.parseColor("#33B5E5"));
92 | return line;
93 | }
94 |
95 | }
96 |
--------------------------------------------------------------------------------
/leafchart/src/main/java/com/beiing/leafchart/bean/Line.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchart.bean;
2 |
3 | import android.graphics.Color;
4 | import android.graphics.Path;
5 |
6 | import java.util.List;
7 |
8 | /**
9 | * Created by chenliu on 2016/7/14.
10 | * 描述:线
11 | *
12 | */
13 | public class Line extends ChartData {
14 | private int lineColor = Color.GRAY; //折线颜色
15 | private float lineWidth = 1; // 折线的宽度dp
16 | private boolean hasPoints = true; //是否画圆点
17 |
18 | private boolean hasLines = true; // 是否画线条
19 |
20 | private int pointColor = Color.GRAY;//圆点颜色
21 |
22 | private int pointRadius = 1;//圆点半径dp
23 |
24 | private boolean isCubic; //是否是曲线
25 |
26 | private boolean isFill; // 是否填充
27 |
28 | private int fillColor = 0; // 填充色
29 |
30 | private Path path = new Path();//折线路径
31 |
32 | public Line(List values) {
33 | this.values = values;
34 | }
35 |
36 | public boolean isCubic() {
37 | return isCubic;
38 | }
39 |
40 | public Line setCubic(boolean cubic) {
41 | isCubic = cubic;
42 | return this;
43 | }
44 |
45 | public boolean isFill() {
46 | return isFill;
47 | }
48 |
49 | public Line setFill(boolean fill) {
50 | isFill = fill;
51 | return this;
52 | }
53 |
54 | public int getFillColor() {
55 | return fillColor;
56 | }
57 |
58 | public Line setFillColor(int fillColor) {
59 | this.fillColor = fillColor;
60 | return this;
61 | }
62 |
63 | public float getLabelRadius() {
64 | return labelRadius;
65 | }
66 |
67 | public int getPointColor() {
68 | return pointColor;
69 | }
70 |
71 | public Line setPointColor(int pointColor) {
72 | this.pointColor = pointColor;
73 | return this;
74 | }
75 |
76 | public float getPointRadius() {
77 | return pointRadius;
78 | }
79 |
80 | public Line setPointRadius(int pointRadius) {
81 | this.pointRadius = pointRadius;
82 | return this;
83 | }
84 |
85 | public int getLineColor() {
86 | return lineColor;
87 | }
88 |
89 | public Line setLineColor(int lineColor) {
90 | this.lineColor = lineColor;
91 | return this;
92 | }
93 |
94 | public float getLineWidth() {
95 | return lineWidth;
96 | }
97 |
98 | public Line setLineWidth(float lineWidth) {
99 | this.lineWidth = lineWidth;
100 | return this;
101 | }
102 |
103 | public List getValues() {
104 | return values;
105 | }
106 |
107 | public Line setValues(List values) {
108 | this.values = values;
109 | return this;
110 | }
111 |
112 | public boolean isHasPoints() {
113 | return hasPoints;
114 | }
115 |
116 | public Line setHasPoints(boolean hasPoints) {
117 | this.hasPoints = hasPoints;
118 | return this;
119 | }
120 |
121 | public boolean isHasLines() {
122 | return hasLines;
123 | }
124 |
125 | public Line setHasLines(boolean hasLines) {
126 | this.hasLines = hasLines;
127 | return this;
128 | }
129 |
130 | public Path getPath() {
131 | return path;
132 | }
133 |
134 | public void setPath(Path path) {
135 | this.path = path;
136 | }
137 |
138 |
139 | }
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
--------------------------------------------------------------------------------
/leafchart/src/main/java/com/beiing/leafchart/bean/Axis.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchart.bean;
2 |
3 | import android.graphics.Color;
4 | import android.graphics.Typeface;
5 |
6 | import java.util.ArrayList;
7 | import java.util.List;
8 |
9 | /**
10 | * Created by chenliu on 2016/7/14.
11 | * 描述:坐标轴
12 | *
13 | */
14 | public class Axis {
15 | public static final int DEFAULT_TEXT_SIZE_SP = 12;
16 |
17 | public static final float DEFAULT_AXIS_WIDTH_DP = 1;
18 |
19 | /**
20 | * 刻度集合
21 | */
22 | private List values = new ArrayList<>();
23 |
24 | /**
25 | * 是否画平行于x、y坐标轴
26 | */
27 | private boolean hasLines = true;
28 |
29 | /**
30 | * 刻度值字体
31 | */
32 | private Typeface typeface;
33 |
34 | /**
35 | * 刻度字体颜色
36 | */
37 | private int textColor = Color.LTGRAY;
38 |
39 | /**
40 | * 刻度值字体大小
41 | */
42 | private int textSize = DEFAULT_TEXT_SIZE_SP;
43 |
44 | /**
45 | * 坐标轴颜色 - x y 轴
46 | */
47 | private int axisColor = Color.LTGRAY;
48 |
49 | /**
50 | * 坐标轴宽度
51 | */
52 | private float axisWidth = DEFAULT_AXIS_WIDTH_DP;
53 |
54 |
55 | /**
56 | * 平行于 x 或 y 轴 的坐标轴颜色
57 | */
58 | private int axisLineColor = Color.LTGRAY;
59 |
60 | private float axisLineWidth = DEFAULT_AXIS_WIDTH_DP;
61 |
62 |
63 | // 坐标轴起点终点位置
64 | private float startX;
65 | private float startY;
66 | private float stopX;
67 | private float stopY;
68 |
69 | /**
70 | * 是否显示刻度
71 | */
72 | private boolean isShowText = true;
73 |
74 | public boolean isShowText() {
75 | return isShowText;
76 | }
77 |
78 | public Axis setShowText(boolean showText) {
79 | isShowText = showText;
80 | return this;
81 | }
82 |
83 | public int getAxisLineColor() {
84 | return axisLineColor;
85 | }
86 |
87 | public Axis setAxisLineColor(int axisLineColor) {
88 | this.axisLineColor = axisLineColor;
89 | return this;
90 | }
91 |
92 | public float getAxisLineWidth() {
93 | return axisLineWidth;
94 | }
95 |
96 | public Axis setAxisLineWidth(float axisLineWidth) {
97 | this.axisLineWidth = axisLineWidth;
98 | return this;
99 | }
100 |
101 | public float getStartX() {
102 | return startX;
103 | }
104 |
105 | public Axis setStartX(float startX) {
106 | this.startX = startX;
107 | return this;
108 | }
109 |
110 | public float getStartY() {
111 | return startY;
112 | }
113 |
114 | public Axis setStartY(float startY) {
115 | this.startY = startY;
116 | return this;
117 | }
118 |
119 | public float getStopX() {
120 | return stopX;
121 | }
122 |
123 | public Axis setStopX(float stopX) {
124 | this.stopX = stopX;
125 | return this;
126 | }
127 |
128 | public float getStopY() {
129 | return stopY;
130 | }
131 |
132 | public Axis setStopY(float stopY) {
133 | this.stopY = stopY;
134 | return this;
135 | }
136 |
137 | public Axis(List values) {
138 | this.values = values;
139 | }
140 |
141 | public List getValues() {
142 | return values;
143 | }
144 |
145 | public void setValues(List values) {
146 | this.values = values;
147 | }
148 |
149 | public boolean isHasLines() {
150 | return hasLines;
151 | }
152 |
153 | public Axis setHasLines(boolean hasLines) {
154 | this.hasLines = hasLines;
155 | return this;
156 | }
157 |
158 | public Typeface getTypeface() {
159 | return typeface;
160 | }
161 |
162 | public void setTypeface(Typeface typeface) {
163 | this.typeface = typeface;
164 | }
165 |
166 | public int getTextColor() {
167 | return textColor;
168 | }
169 |
170 | public Axis setTextColor(int textColor) {
171 | this.textColor = textColor;
172 | return this;
173 | }
174 |
175 | public int getTextSize() {
176 | return textSize;
177 | }
178 |
179 | public Axis setTextSize(int textSize) {
180 | this.textSize = textSize;
181 | return this;
182 | }
183 |
184 | public int getAxisColor() {
185 | return axisColor;
186 | }
187 |
188 | public Axis setAxisColor(int axisColor) {
189 | this.axisColor = axisColor;
190 | return this;
191 | }
192 |
193 | public float getAxisWidth() {
194 | return axisWidth;
195 | }
196 |
197 | public Axis setAxisWidth(float axisWidth) {
198 | this.axisWidth = axisWidth;
199 | return this;
200 | }
201 | }
202 |
--------------------------------------------------------------------------------
/app/src/main/java/com/beiing/leafchartdemo/SlideSelectLineChartActivity.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchartdemo;
2 |
3 | import android.graphics.Color;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.os.Bundle;
6 | import android.util.Log;
7 | import android.widget.TextView;
8 |
9 | import com.beiing.leafchart.SlideSelectLineChart;
10 | import com.beiing.leafchart.bean.Axis;
11 | import com.beiing.leafchart.bean.AxisValue;
12 | import com.beiing.leafchart.bean.Line;
13 | import com.beiing.leafchart.bean.PointValue;
14 | import com.beiing.leafchart.bean.SlidingLine;
15 | import com.beiing.leafchart.support.OnChartSelectedListener;
16 | import com.beiing.leafchart.support.OnPointSelectListener;
17 |
18 | import java.util.ArrayList;
19 | import java.util.List;
20 |
21 | public class SlideSelectLineChartActivity extends AppCompatActivity {
22 |
23 | SlideSelectLineChart slideSelectLineChart;
24 | TextView tvSelectPoint;
25 | @Override
26 | protected void onCreate(Bundle savedInstanceState) {
27 | super.onCreate(savedInstanceState);
28 | setContentView(R.layout.activity_move_select_line_chart);
29 | slideSelectLineChart = (SlideSelectLineChart) findViewById(R.id.move_select_chart);
30 | tvSelectPoint = (TextView) findViewById(R.id.tv_select_point);
31 |
32 | initLineChart();
33 |
34 | slideSelectLineChart.setOnPointSelectListener(new OnPointSelectListener() {
35 | @Override
36 | public void onPointSelect(int position, String xLabel, String value) {
37 | String point = xLabel + ":" + value;
38 | tvSelectPoint.setText(point);
39 | }
40 | });
41 |
42 | slideSelectLineChart.setOnChartSelectedListener(new OnChartSelectedListener() {
43 | @Override
44 | public void onChartSelected(boolean isChartSelected) {
45 | Log.e("====", "isChartSelected: " + isChartSelected);
46 | }
47 | });
48 | }
49 |
50 | private void initLineChart() {
51 | Axis axisX = new Axis(getAxisValuesX());
52 | axisX.setAxisColor(Color.parseColor("#33B5E5")).setTextColor(Color.DKGRAY).setHasLines(false).setShowText(false);
53 | Axis axisY = new Axis(getAxisValuesY());
54 | axisY.setAxisColor(Color.parseColor("#33B5E5")).setTextColor(Color.DKGRAY).setHasLines(false).setShowText(true);
55 | slideSelectLineChart.setAxisX(axisX);
56 | slideSelectLineChart.setAxisY(axisY);
57 |
58 | slideSelectLineChart.setSlideLine(getSlideingLine());
59 | slideSelectLineChart.setChartData(getFoldLine());
60 | slideSelectLineChart.show();
61 | }
62 |
63 |
64 | int days = 90;
65 |
66 | private List getAxisValuesX(){
67 | List axisValues = new ArrayList<>();
68 | for (int i = 1; i <= days; i++) {
69 | AxisValue value = new AxisValue();
70 | value.setLabel(i + "日");
71 | axisValues.add(value);
72 | }
73 | return axisValues;
74 | }
75 |
76 | private List getAxisValuesY(){
77 | List axisValues = new ArrayList<>();
78 | for (int i = 0; i < 11; i++) {
79 | AxisValue value = new AxisValue();
80 | value.setLabel(String.valueOf(i * 10));
81 | axisValues.add(value);
82 | }
83 | return axisValues;
84 | }
85 |
86 | private Line getFoldLine(){
87 | List pointValues = new ArrayList<>();
88 | for (int i = 1; i <= days; i++) {
89 | PointValue pointValue = new PointValue();
90 | pointValue.setX( (i - 1) / (days - 1f));
91 | int var = 5 + i + (int) (Math.random() * 10);
92 | pointValue.setLabel(String.valueOf(var));
93 | pointValue.setY(var / 100f);
94 | pointValues.add(pointValue);
95 | }
96 |
97 | Line line = new Line(pointValues);
98 | line.setLineColor(Color.parseColor("#33B5E5"))
99 | .setLineWidth(1.5f)
100 | .setPointColor(Color.parseColor("#33B5E5"))
101 | .setCubic(true)
102 | .setPointRadius(2)
103 | .setFill(true)
104 | .setHasPoints(true)
105 | .setFillColor(Color.parseColor("#33B5E5"))
106 | .setLabelColor(Color.parseColor("#33B5E5"))
107 | .setHasLabels(true);
108 | return line;
109 | }
110 |
111 | private SlidingLine getSlideingLine(){
112 | SlidingLine slidingLine = new SlidingLine();
113 | slidingLine.setSlideLineColor(getResources().getColor(R.color.colorAccent))
114 | .setSlidePointColor(getResources().getColor(R.color.colorAccent))
115 | .setSlidePointRadius(3);
116 | return slidingLine;
117 | }
118 | }
119 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/app/src/main/java/com/beiing/leafchartdemo/ChartFragment.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchartdemo;
2 |
3 |
4 | import android.graphics.Color;
5 | import android.os.Bundle;
6 | import android.os.Handler;
7 | import android.os.Message;
8 | import android.support.annotation.Nullable;
9 | import android.support.v4.app.Fragment;
10 | import android.view.LayoutInflater;
11 | import android.view.View;
12 | import android.view.ViewGroup;
13 |
14 | import com.beiing.leafchart.LeafLineChart;
15 | import com.beiing.leafchart.bean.Axis;
16 | import com.beiing.leafchart.bean.AxisValue;
17 | import com.beiing.leafchart.bean.Line;
18 | import com.beiing.leafchart.bean.PointValue;
19 |
20 | import java.util.ArrayList;
21 | import java.util.List;
22 |
23 | /**
24 | * A simple {@link Fragment} subclass.
25 | * Use the {@link ChartFragment#newInstance} factory method to
26 | * create an instance of this fragment.
27 | */
28 | public class ChartFragment extends Fragment {
29 |
30 | LeafLineChart lineChart;
31 |
32 | Handler handler = new Handler(){
33 | @Override
34 | public void handleMessage(Message msg) {
35 | super.handleMessage(msg);
36 | }
37 | };
38 |
39 | public ChartFragment() {
40 | // Required empty public constructor
41 | }
42 |
43 | public static ChartFragment newInstance(String param1, String param2) {
44 | ChartFragment fragment = new ChartFragment();
45 | return fragment;
46 | }
47 |
48 | @Override
49 | public void onCreate(Bundle savedInstanceState) {
50 | super.onCreate(savedInstanceState);
51 | }
52 |
53 | @Override
54 | public View onCreateView(LayoutInflater inflater, ViewGroup container,
55 | Bundle savedInstanceState) {
56 | View view = inflater.inflate(R.layout.fragment_chart, container, false);
57 | lineChart = (LeafLineChart) view.findViewById(R.id.leaf_chart);
58 | return view;
59 | }
60 |
61 | @Override
62 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
63 | super.onViewCreated(view, savedInstanceState);
64 |
65 | handler.postDelayed(new Runnable() {
66 | @Override
67 | public void run() {
68 | initLineChart();
69 | }
70 | }, 2000);
71 |
72 | }
73 |
74 | private void initLineChart() {
75 | Axis axisX = new Axis(getAxisValuesX());
76 | axisX.setAxisColor(Color.parseColor("#33B5E5")).setTextColor(Color.DKGRAY).setHasLines(true);
77 | Axis axisY = new Axis(getAxisValuesY());
78 | axisY.setAxisColor(Color.parseColor("#33B5E5")).setTextColor(Color.DKGRAY).setHasLines(true).setShowText(true);
79 | lineChart.setAxisX(axisX);
80 | lineChart.setAxisY(axisY);
81 | List lines = new ArrayList<>();
82 | lines.add(getFoldLine());
83 | lines.add(getCompareLine());
84 | lineChart.setChartData(lines);
85 |
86 | lineChart.showWithAnimation(3000);
87 |
88 | }
89 |
90 | private List getAxisValuesX(){
91 | List axisValues = new ArrayList<>();
92 | for (int i = 1; i <= 12; i++) {
93 | AxisValue value = new AxisValue();
94 | value.setLabel(i + "月");
95 | axisValues.add(value);
96 | }
97 | return axisValues;
98 | }
99 |
100 | private List getAxisValuesY(){
101 | List axisValues = new ArrayList<>();
102 | for (int i = 0; i < 11; i++) {
103 | AxisValue value = new AxisValue();
104 | value.setLabel(String.valueOf(i * 10));
105 | axisValues.add(value);
106 | }
107 | return axisValues;
108 | }
109 |
110 | private Line getFoldLine(){
111 | List pointValues = new ArrayList<>();
112 | for (int i = 1; i <= 12; i++) {
113 | PointValue pointValue = new PointValue();
114 | pointValue.setX( (i - 1) / 11f);
115 | int var = (int) (Math.random() * 100);
116 | pointValue.setLabel(String.valueOf(var));
117 | pointValue.setY(var / 100f);
118 | pointValues.add(pointValue);
119 | }
120 |
121 | Line line = new Line(pointValues);
122 | line.setLineColor(Color.parseColor("#33B5E5"))
123 | .setLineWidth(3)
124 | .setPointColor(Color.YELLOW)
125 | .setCubic(true)
126 | .setPointRadius(3)
127 | .setFill(false)
128 | .setHasLabels(true)
129 | .setLabelColor(Color.parseColor("#33B5E5"));
130 | return line;
131 | }
132 |
133 | private Line getCompareLine(){
134 | List pointValues = new ArrayList<>();
135 | for (int i = 1; i <= 12; i++) {
136 | PointValue pointValue = new PointValue();
137 | pointValue.setX( (i - 1) / 11f);
138 | int var = (int) (Math.random() * 100);
139 | pointValue.setLabel(String.valueOf(var));
140 | pointValue.setY(var / 100f);
141 | pointValues.add(pointValue);
142 | }
143 |
144 | Line line = new Line(pointValues);
145 | line.setLineColor(Color.MAGENTA)
146 | .setLineWidth(3)
147 | .setPointColor(Color.MAGENTA)
148 | .setCubic(true)
149 | .setPointRadius(3)
150 | .setFill(false)
151 | .setHasLabels(false);
152 | return line;
153 | }
154 |
155 | }
156 |
--------------------------------------------------------------------------------
/app/src/main/java/com/beiing/leafchartdemo/LeafChartActivity.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchartdemo;
2 |
3 | import android.content.Intent;
4 | import android.graphics.Color;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.os.Bundle;
7 | import android.view.View;
8 |
9 | import com.beiing.leafchart.LeafLineChart;
10 | import com.beiing.leafchart.LeafSquareChart;
11 | import com.beiing.leafchart.bean.Axis;
12 | import com.beiing.leafchart.bean.AxisValue;
13 | import com.beiing.leafchart.bean.Line;
14 | import com.beiing.leafchart.bean.PointValue;
15 | import com.beiing.leafchart.bean.Square;
16 |
17 | import java.util.ArrayList;
18 | import java.util.List;
19 |
20 | public class LeafChartActivity extends AppCompatActivity {
21 |
22 | LeafLineChart leafLineChart;
23 |
24 | LeafSquareChart leafSquareChart;
25 |
26 | @Override
27 | protected void onCreate(Bundle savedInstanceState) {
28 | super.onCreate(savedInstanceState);
29 | setContentView(R.layout.activity_leaf_chart);
30 | leafLineChart = (LeafLineChart) findViewById(R.id.leaf_chart);
31 |
32 | leafSquareChart = (LeafSquareChart) findViewById(R.id.leaf_square_chart);
33 |
34 | //测试折线图
35 | initLineChart();
36 |
37 | //测试直方图
38 | initSquareChart();
39 | }
40 |
41 | private void initSquareChart() {
42 | Axis axisX = new Axis(getAxisValuesX());
43 | axisX.setAxisColor(Color.parseColor("#FF4081")).setTextColor(Color.DKGRAY).setHasLines(false);
44 | Axis axisY = new Axis(getAxisValuesY());
45 | axisY.setAxisColor(Color.parseColor("#FF4081")).setTextColor(Color.DKGRAY).setHasLines(false).setShowText(true);
46 |
47 | leafSquareChart.setAxisX(axisX);
48 | leafSquareChart.setAxisY(axisY);
49 | leafSquareChart.setChartData(getSquares());
50 | }
51 |
52 | private void initLineChart() {
53 | Axis axisX = new Axis(getAxisValuesX());
54 | axisX.setAxisColor(Color.parseColor("#33B5E5")).setTextColor(Color.DKGRAY).setHasLines(true);
55 | Axis axisY = new Axis(getAxisValuesY());
56 | axisY.setAxisColor(Color.parseColor("#33B5E5")).setTextColor(Color.DKGRAY).setHasLines(true).setShowText(true);
57 | leafLineChart.setAxisX(axisX);
58 | leafLineChart.setAxisY(axisY);
59 |
60 | List lines = new ArrayList<>();
61 | lines.add(getFoldLine());
62 | leafLineChart.setChartData(lines);
63 |
64 | leafLineChart.showWithAnimation(1000);
65 |
66 | // leafLineChart.show();
67 | }
68 |
69 | private List getAxisValuesX(){
70 | List axisValues = new ArrayList<>();
71 | for (int i = 1; i <= 12; i++) {
72 | AxisValue value = new AxisValue();
73 | value.setLabel(i + "月");
74 | axisValues.add(value);
75 | }
76 | return axisValues;
77 | }
78 |
79 | private List getAxisValuesY(){
80 | List axisValues = new ArrayList<>();
81 | for (int i = 0; i < 11; i++) {
82 | AxisValue value = new AxisValue();
83 | value.setLabel(String.valueOf(i * 10));
84 | axisValues.add(value);
85 | }
86 | return axisValues;
87 | }
88 |
89 | private Line getFoldLine(){
90 | List pointValues = new ArrayList<>();
91 |
92 | PointValue p = new PointValue();
93 | p.setX( (1 - 1) / 11f);
94 | p.setLabel(String.valueOf(90));
95 | p.setY(90 / 100f);
96 | pointValues.add(p);
97 | for (int i = 2; i <= 12; i++) {
98 | PointValue pointValue = new PointValue();
99 | pointValue.setX( (i - 1) / 11f);
100 | // int var = (int) (Math.random() * 100);
101 | int var = 20;
102 | pointValue.setLabel(String.valueOf(var));
103 | pointValue.setY(var / 100f);
104 | pointValues.add(pointValue);
105 | }
106 |
107 |
108 | Line line = new Line(pointValues);
109 | line.setLineColor(Color.parseColor("#33B5E5"))
110 | .setLineWidth(3)
111 | .setPointColor(Color.YELLOW)
112 | .setCubic(true)
113 | .setPointRadius(3)
114 | .setFill(true)
115 | .setFillColor(Color.parseColor("#33B5E5"))
116 | .setHasLabels(true)
117 | .setLabelColor(Color.parseColor("#33B5E5"));
118 | return line;
119 | }
120 |
121 | private Square getSquares(){
122 | List pointValues = new ArrayList<>();
123 | for (int i = 1; i <= 12; i++) {
124 | PointValue pointValue = new PointValue();
125 | pointValue.setX( (i - 1) / 11f);
126 | int var = (int) (Math.random() * 100);
127 | pointValue.setLabel(String.valueOf(var));
128 | pointValue.setY(var / 100f);
129 | pointValues.add(pointValue);
130 | }
131 |
132 | Square square = new Square(pointValues);
133 | square.setBorderColor(Color.parseColor("#FF4081"))
134 | .setWidth(20)
135 | .setFill(false)
136 | .setHasLabels(true)
137 | .setLabelColor(Color.parseColor("#FF4081"));
138 | return square;
139 | }
140 |
141 | public void toChartInFragment(View view) {
142 | startActivity(new Intent(this, ChartInFragmentActivity.class));
143 | }
144 |
145 | public void SlideSelectLineChart(View view) {
146 | startActivity(new Intent(this, SlideSelectLineChartActivity.class));
147 | }
148 |
149 | public void OutsideLineChart(View view) {
150 | startActivity(new Intent(this, OutsideLineChartActivity.class));
151 | }
152 | }
153 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # LeafChart
2 |
3 |
4 | ##一、折线图
5 |
6 |
7 | ###设置
8 |
9 | >坐标轴
10 |
11 | - `Axis.setAxisColor` 坐标轴颜色
12 | - `Axis.setAxisWidth` 坐标轴宽度
13 | - `Axis.setTextSize` 坐标轴刻度字体大小
14 | - `Axis.setTextColor` 坐标轴刻度字体颜色
15 |
16 | >折线@LeafLineChart
17 |
18 | - `Line.setLineWidth` 折线宽度
19 | - `Line.setLineColor` 折线颜色
20 | - `Line.setPointRadius` 点的大小
21 | - `Line.setPointColor` 点的颜色
22 | - `Line.setHasLabels` 是否有标签
23 | - `Line.setLabelColor` 标签背景色
24 | - `Line.setLabelRadius` 标签弧度
25 | - `Line.setFill` 是否填充
26 | - `Line.setFillColor` 填充颜色(默认为有透明度的折线颜色)
27 |
28 |
29 | >SlideSelectLineChart
30 |
31 | - `SlidingLine.setOpenSlideSelect` 是否开启滑动选值
32 | - `SlidingLine.setDash` 移动标尺线是否是虚线
33 | - `SlidingLine.setSlideLineColor` 移动标尺线颜色
34 | - `SlidingLine.setSlideLineWidth` 移动标尺线宽度
35 | - `SlidingLine.setSlidePointRadius` 移动标尺线顶端圆点半径
36 | - `SlidingLine.setSlidePointColor` 移动标尺线顶端圆点颜色
37 |
38 | ###自定义属性
39 | | 属性名 | 说明 | 默认值 |
40 | |--------|--------|--------|
41 | | lc_leftPadding | 左间距 | 20dp |
42 | | lc_topPadding | 上间距 | 10dp |
43 | | lc_rightPadding | 右间距 | 10dp|
44 | | lc_bottomPadding | 下间距 | 20dp |
45 | | lc_startMarginX | 第一个点x轴方向起始位置 |0dp |
46 | | lc_startMarginY | 第一个点y轴方向起始位置 |0dp |
47 | | lc_coordinateMode | 坐标轴相交模式 |Mode.INTERSEC |
48 |
49 |
50 | ###效果图
51 |
52 | - 坐标轴相交模式
53 | 
54 |
55 | - LeafChart
56 |
57 | 
58 |
59 | 
60 |
61 | - 多线条支持
62 |
63 | 
64 |
65 | - 移动选值
66 |
67 | 
68 |
69 |
70 | ###LeafLineChart使用
71 |
72 | ``` java
73 |
78 |
79 | ```
80 |
81 |
82 | 初始化X轴数据:
83 | ``` java
84 | private List getAxisValuesX(){
85 | List axisValues = new ArrayList<>();
86 | for (int i = 1; i <= 12; i++) {
87 | AxisValue value = new AxisValue();
88 | value.setLabel(i + "月");
89 | axisValues.add(value);
90 | }
91 | return axisValues;
92 | }
93 | ```
94 |
95 |
96 | 初始化Y轴数据:
97 | ```java
98 | private List getAxisValuesY(){
99 | List axisValues = new ArrayList<>();
100 | for (int i = 0; i < 11; i++) {
101 | AxisValue value = new AxisValue();
102 | value.setLabel(String.valueOf(i * 10));
103 | axisValues.add(value);
104 | }
105 | return axisValues;
106 | }
107 | ```
108 |
109 | 初始化点数据和相关设置:
110 | ```java
111 | private Line getFoldLine(){
112 | List pointValues = new ArrayList<>();
113 | for (int i = 1; i <= 12; i++) {
114 | PointValue pointValue = new PointValue();
115 | pointValue.setX( (i - 1) / 11f);
116 | float var = (float) (Math.random() * 100);
117 | pointValue.setLabel(String.valueOf(var));
118 | pointValue.setY(var / 100);
119 | pointValues.add(pointValue);
120 | }
121 |
122 | Line line = new Line(pointValues);
123 | line.setLineColor(Color.parseColor("#33B5E5")).setPointColor(Color.YELLOW).
124 | setCubic(false).setPointRadius(3).setHasLabels(true)
125 | .setFill(false);
126 | return line;
127 | }
128 | ```
129 |
130 | 图表设置数据:
131 | ```java
132 |
133 | Axis axisX = new Axis(getAxisValuesX());
134 | axisX.setAxisColor(Color.parseColor("#33B5E5")).setTextColor(Color.DKGRAY).setHasLines(true);
135 | Axis axisY = new Axis(getAxisValuesY());
136 | axisY.setAxisColor(Color.parseColor("#33B5E5")).setTextColor(Color.DKGRAY).setHasLines(true).setShowText(true);
137 | lineChart.setAxisX(axisX);
138 | lineChart.setAxisY(axisY);
139 | List lines = new ArrayList<>();
140 | lines.add(getFoldLine());
141 | lines.add(getCompareLine());
142 | lineChart.setChartData(lines);
143 |
144 | lineChart.showWithAnimation(3000);
145 |
146 | //无动画
147 | //lineChart.show();
148 | ```
149 |
150 | ###SlideSelectLineChart使用
151 |
152 | 初始化X轴数据(注意:如果想在滑动选值的时候能正确返回点对应x轴刻度,不要忘了调用`setLabel`方法):
153 | ``` java
154 | private List getAxisValuesX(){
155 | List axisValues = new ArrayList<>();
156 | for (int i = 1; i <= 12; i++) {
157 | AxisValue value = new AxisValue();
158 | value.setLabel(i + "月");
159 | axisValues.add(value);
160 | }
161 | return axisValues;
162 | }
163 | ```
164 |
165 |
166 | 初始化Y轴数据:
167 | (同上)
168 |
169 | 初始化点数据和相关设置:
170 | (同上)
171 |
172 | 初始化标尺线:
173 |
174 | ```java
175 | private SlidingLine getSlideingLine(){
176 | SlidingLine slidingLine = new SlidingLine();
177 | slidingLine.setSlideLineColor(getResources().getColor(R.color.colorAccent))
178 | .setSlidePointColor(getResources().getColor(R.color.colorAccent))
179 | .setSlidePointRadius(3);
180 | return slidingLine;
181 | }
182 |
183 | ```
184 |
185 | 图表设置数据:
186 |
187 | ```java
188 |
189 | private void initLineChart() {
190 | Axis axisX = new Axis(getAxisValuesX());
191 | axisX.setAxisColor(Color.parseColor("#33B5E5")).setTextColor(Color.DKGRAY).setHasLines(false).setShowText(false);
192 | Axis axisY = new Axis(getAxisValuesY());
193 | axisY.setAxisColor(Color.parseColor("#33B5E5")).setTextColor(Color.DKGRAY).setHasLines(false).setShowText(true);
194 | slideSelectLineChart.setAxisX(axisX);
195 | slideSelectLineChart.setAxisY(axisY);
196 |
197 | slideSelectLineChart.setSlideLine(getSlideingLine());
198 | slideSelectLineChart.setChartData(getFoldLine());
199 | slideSelectLineChart.show();
200 | }
201 |
202 | ```
203 |
204 |
205 |
206 |
207 | ## 二、直方图
208 |
209 | 使用类似折线图
210 |
211 | ### 设置
212 | - `Square.setWidth` 直方图宽度
213 | - `Square.setBorderWidth` 边框宽度
214 | - `Square.setBorderColor` 边框颜色
215 | - `Square.setFill` 是否填充
216 | - `Square.setHasLabels` 是否有标签
217 | - `Square.setLabelColor` 标签背景色
218 | - `Square.setLabelRadius` 标签弧度
219 |
220 |
221 | ### 效果图
222 |
223 | 
224 |
225 |
226 | 
227 |
228 |
229 |
230 | #License
231 |
232 | ```
233 | Copyright 2016 LineChen <15764230067@163.com>
234 |
235 | Licensed under the Apache License, Version 2.0 (the "License");
236 | you may not use this file except in compliance with the License.
237 | You may obtain a copy of the License at
238 |
239 | http://www.apache.org/licenses/LICENSE-2.0
240 |
241 | Unless required by applicable law or agreed to in writing, software
242 | distributed under the License is distributed on an "AS IS" BASIS,
243 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
244 | See the License for the specific language governing permissions and
245 | limitations under the License.
246 | ```
247 |
248 |
--------------------------------------------------------------------------------
/leafchart/src/main/java/com/beiing/leafchart/OutsideLineChart.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchart;
2 |
3 | import android.content.Context;
4 | import android.content.res.TypedArray;
5 | import android.graphics.Canvas;
6 | import android.support.v4.view.GestureDetectorCompat;
7 | import android.util.AttributeSet;
8 | import android.view.GestureDetector;
9 | import android.view.MotionEvent;
10 | import android.widget.Scroller;
11 |
12 | import com.beiing.leafchart.bean.AxisValue;
13 | import com.beiing.leafchart.bean.Line;
14 | import com.beiing.leafchart.bean.PointValue;
15 | import com.beiing.leafchart.renderer.OutsideLineRenderer;
16 | import com.beiing.leafchart.support.LeafUtil;
17 | import com.beiing.leafchart.support.Mode;
18 |
19 | import java.util.List;
20 |
21 | /**
22 | * Created by chenliu on 2017/1/12.
23 | * 描述: to be continued
24 | *
25 | */
26 |
27 | public class OutsideLineChart extends AbsLeafChart {
28 |
29 | private Line line;
30 |
31 | private OutsideLineRenderer outsideLineRenderer;
32 |
33 | /**
34 | * mMove为偏移量
35 | */
36 | private int mLastX, mMove;
37 |
38 | /**两个点之间间隔**/
39 | private int mStep;
40 |
41 | /**滑动到第一个点或最后一个点时,还能继续滑动的距离**/
42 | private int maxOverMove;
43 |
44 | private Scroller mScroller;
45 |
46 | private GestureDetectorCompat gestureDetector;
47 |
48 | public OutsideLineChart(Context context) {
49 | this(context, null, 0);
50 | }
51 |
52 | public OutsideLineChart(Context context, AttributeSet attrs) {
53 | this(context, attrs, 0);
54 | }
55 |
56 | public OutsideLineChart(Context context, AttributeSet attrs, int defStyleAttr) {
57 | super(context, attrs, defStyleAttr);
58 | mScroller = new Scroller(getContext());
59 | gestureDetector = new GestureDetectorCompat(getContext(), new SimpleGestureListener());
60 | maxOverMove = (int) LeafUtil.dp2px(mContext, 100);
61 | }
62 |
63 | @Override
64 | protected void initAttrs(AttributeSet attrs) {
65 | super.initAttrs(attrs);
66 | TypedArray ta = mContext.obtainStyledAttributes(attrs, R.styleable.OutsideLineChart);
67 | try{
68 | mStep = (int) ta.getDimension(R.styleable.OutsideLineChart_lc_step, LeafUtil.dp2px(mContext, 30));
69 | } finally {
70 | ta.recycle();
71 | }
72 |
73 | }
74 |
75 | @Override
76 | protected void onSizeChanged(int w, int h, int oldw, int oldh) {
77 | super.onSizeChanged(w, h, oldw, oldh);
78 | }
79 |
80 | @Override
81 | protected void initRenderer() {
82 | outsideLineRenderer = new OutsideLineRenderer(mContext, this);
83 | }
84 |
85 | @Override
86 | protected void setRenderer() {
87 | super.setRenderer(outsideLineRenderer);
88 | }
89 |
90 | @Override
91 | protected void resetPointWeight() {
92 | if(line != null){
93 | super.resetPointWeight(line);
94 | }
95 | }
96 |
97 | @Override
98 | protected void resetAsixX() {
99 | if(axisX != null){
100 | List values = axisX.getValues();
101 | int sizeX = values.size(); //几条y轴
102 | float xStep = mStep;
103 | for (int i = 0; i < sizeX; i++) {
104 | AxisValue axisValue = values.get(i);
105 | axisValue.setPointY(mHeight);
106 | if(i == 0){
107 | axisValue.setPointX(leftPadding + startMarginX);
108 | } else {
109 | axisValue.setPointX(leftPadding + startMarginX + xStep * i);
110 | }
111 | }
112 | switch (coordinateMode){
113 | case Mode.ACROSS:
114 | case Mode.X_ACROSS:
115 | axisX.setStartX(leftPadding * 0.5f);
116 | break;
117 |
118 | case Mode.INTERSECT:
119 | case Mode.Y_ACROSS:
120 | axisX.setStartX(leftPadding);
121 | break;
122 | }
123 | axisX.setStartY(mHeight - bottomPadding).setStopX(mWidth).setStopY(mHeight - bottomPadding);
124 | }
125 | }
126 |
127 |
128 | @Override
129 | protected void onDraw(Canvas canvas) {
130 | outsideLineRenderer.drawCoordinateLines(canvas, axisX, axisY);
131 | outsideLineRenderer.drawCoordinateText(canvas, axisX, axisY, mMove);
132 |
133 | if(line != null){
134 | outsideLineRenderer.drawLines(canvas, line, axisY, mMove);
135 |
136 | if(line.isFill()){
137 | //填充
138 | outsideLineRenderer.drawFillArea(canvas, line, axisX, mMove);
139 | }
140 |
141 | outsideLineRenderer.drawPoints(canvas, line, axisY, mMove);
142 | }
143 |
144 | if (line != null && line.isHasLabels()) {
145 | outsideLineRenderer.drawLabels(canvas, line, axisY, mMove);
146 | }
147 |
148 | }
149 |
150 | @Override
151 | public boolean onTouchEvent(MotionEvent event) {
152 | gestureDetector.onTouchEvent(event);
153 | int xPosition = (int) event.getX();
154 | switch (event.getAction()) {
155 | case MotionEvent.ACTION_DOWN:
156 | mScroller.abortAnimation();
157 | mLastX = xPosition;
158 | return true;
159 | case MotionEvent.ACTION_MOVE:
160 | if(mMove >= 0 && mMove <= maxOverMove || mMove <= 0 && mMove >= -getMinMove()){
161 | smoothScrollBy(xPosition - mLastX, 0);
162 | }
163 | break;
164 | case MotionEvent.ACTION_UP:
165 | if(mMove > 0){
166 | smoothScrollTo(startMarginX, 0);
167 | } else if(mMove <= -getMinMove()) {
168 | smoothScrollTo(-getMinMove(), 0);
169 | }
170 | break;
171 | }
172 | mLastX = xPosition;
173 | return true;
174 | }
175 |
176 | //调用此方法设置滚动的相对偏移
177 | public void smoothScrollBy(int dx, int dy) {
178 | //设置mScroller的滚动偏移量
179 | mScroller.startScroll(mScroller.getFinalX(), mScroller.getFinalY(), dx, dy);
180 | invalidate();//这里必须调用invalidate()才能保证computeScroll()会被调用,否则不一定会刷新界面,看不到滚动效果
181 | }
182 |
183 | //调用此方法滚动到目标位置
184 | public void smoothScrollTo(int fx, int fy) {
185 | int dx = fx - mScroller.getFinalX();
186 | int dy = fy - mScroller.getFinalY();
187 | smoothScrollBy(dx, dy);
188 | }
189 |
190 | @Override
191 | public void computeScroll() {
192 | if (mScroller.computeScrollOffset()) {
193 | //判断左右边界
194 | mMove = mScroller.getCurrX();
195 | postInvalidate();
196 | }
197 | }
198 |
199 | private class SimpleGestureListener extends
200 | GestureDetector.SimpleOnGestureListener {
201 | @Override
202 | public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
203 | int minMove;
204 | minMove = getMinMove();
205 | mScroller.fling(mMove, 0, (int)velocityX, (int)velocityY, -minMove, startMarginX, 0, 0);
206 | return true;
207 | }
208 | }
209 |
210 | private int getMinMove() {
211 | int minMove = Integer.MIN_VALUE;
212 | if (line != null) {
213 | List values = line.getValues();
214 | if (values != null && values.size() > 0) {
215 | PointValue pointValue = values.get(values.size() - 1);
216 | minMove = (int) (pointValue.getOriginX() - mWidth + maxOverMove);
217 | }
218 | }
219 | return minMove;
220 | }
221 |
222 | /**
223 | * 带动画的绘制
224 | * @param duration
225 | */
226 | public void showWithAnimation(int duration){
227 | outsideLineRenderer.showWithAnimation(duration);
228 | }
229 |
230 | public void show(){
231 | showWithAnimation(0);
232 | }
233 |
234 | public void setChartData(Line chartData) {
235 | line = chartData;
236 | resetPointWeight();
237 | }
238 |
239 | public Line getChartData() {
240 | return line;
241 | }
242 | }
243 |
--------------------------------------------------------------------------------
/leafchart/src/main/java/com/beiing/leafchart/SlideSelectLineChart.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchart;
2 |
3 | import android.app.Service;
4 | import android.content.Context;
5 | import android.graphics.Canvas;
6 | import android.os.Vibrator;
7 | import android.util.AttributeSet;
8 | import android.view.MotionEvent;
9 | import android.view.View;
10 | import android.view.ViewConfiguration;
11 |
12 | import com.beiing.leafchart.bean.AxisValue;
13 | import com.beiing.leafchart.bean.Line;
14 | import com.beiing.leafchart.bean.PointValue;
15 | import com.beiing.leafchart.bean.SlidingLine;
16 | import com.beiing.leafchart.renderer.SlideSelectLineRenderer;
17 | import com.beiing.leafchart.support.LeafUtil;
18 | import com.beiing.leafchart.support.OnChartSelectedListener;
19 | import com.beiing.leafchart.support.OnPointSelectListener;
20 |
21 | import java.util.List;
22 |
23 | /**
24 | * Created by chenliu on 2016/12/13.
25 | * 描述:滑动选择手指竖直方向最近的点
26 | *
27 | */
28 |
29 | public class SlideSelectLineChart extends AbsLeafChart {
30 | private Line line;
31 | private SlidingLine slidingLine;
32 |
33 | private float moveX;
34 | private float moveY;
35 | private boolean isDrawMoveLine;
36 | private OnPointSelectListener onPointSelectListener;
37 |
38 | float downX;
39 | float downY;
40 | int scaledTouchSlop;
41 |
42 | private boolean isCanSelected;
43 |
44 | SlideSelectLineRenderer slideRenderer;
45 |
46 | private OnChartSelectedListener mOnChartSelectedListener;
47 |
48 | public SlideSelectLineChart(Context context) {
49 | this(context, null, 0);
50 | }
51 |
52 | public SlideSelectLineChart(Context context, AttributeSet attrs) {
53 | this(context, attrs, 0);
54 | }
55 |
56 | public SlideSelectLineChart(Context context, AttributeSet attrs, int defStyleAttr) {
57 | super(context, attrs, defStyleAttr);
58 |
59 | initDefaultSlidingLine();
60 |
61 | scaledTouchSlop = ViewConfiguration.get(mContext).getScaledTouchSlop();
62 |
63 | setOnLongClickListener(new OnLongClickListener() {
64 | @Override
65 | public boolean onLongClick(View v) {
66 | setCanSelected(true);
67 | if (null != mOnChartSelectedListener) {
68 | mOnChartSelectedListener.onChartSelected(true);
69 | }
70 | return false;
71 | }
72 | });
73 | }
74 |
75 | @Override
76 | protected void initRenderer() {
77 | slideRenderer = new SlideSelectLineRenderer(mContext, this);
78 | }
79 |
80 | @Override
81 | protected void setRenderer() {
82 | super.setRenderer(slideRenderer);
83 | }
84 |
85 | private void initDefaultSlidingLine() {
86 | slidingLine = new SlidingLine();
87 | slidingLine.setDash(true).setSlideLineWidth(1).setSlidePointRadius(3);
88 | }
89 |
90 | @Override
91 | protected void onSizeChanged(int w, int h, int oldw, int oldh) {
92 | super.onSizeChanged(w, h, oldw, oldh);
93 | }
94 |
95 | /**
96 | * 设置点所占比重
97 | */
98 | @Override
99 | public void resetPointWeight() {
100 | if (line != null) {
101 | super.resetPointWeight(line);
102 | }
103 | }
104 |
105 | @Override
106 | protected void onDraw(Canvas canvas) {
107 | super.onDraw(canvas);
108 | if (line != null) {
109 | if (line.isCubic()) {
110 | slideRenderer.drawCubicPath(canvas, line);
111 | } else {
112 | slideRenderer.drawLines(canvas, line);
113 | }
114 |
115 | if (line.isFill()) {
116 | //填充
117 | slideRenderer.drawFillArea(canvas, line, axisX);
118 | }
119 |
120 | slideRenderer.drawPoints(canvas, line);
121 |
122 | if (line.isHasLabels()) {
123 | slideRenderer.drawLabels(canvas, line, axisY);
124 | }
125 |
126 | }
127 |
128 | if (slidingLine != null && slidingLine.isOpenSlideSelect()) {
129 | //绘制移动标尺线
130 | if (isDrawMoveLine) {
131 | slideRenderer.drawSlideLine(canvas, axisX, slidingLine, moveX, moveY);
132 | }
133 | }
134 | }
135 |
136 | /**
137 | * 带动画的绘制
138 | *
139 | * @param duration
140 | */
141 | public void showWithAnimation(int duration) {
142 | slideRenderer.showWithAnimation(duration);
143 | }
144 |
145 | public void show() {
146 | showWithAnimation(0);
147 | }
148 |
149 | @Override
150 | public boolean onTouchEvent(MotionEvent event) {
151 | if (!isCanSelected)
152 | return super.onTouchEvent(event);
153 |
154 | float x = event.getX();
155 | float y = event.getY();
156 | switch (event.getAction()) {
157 | case MotionEvent.ACTION_DOWN:
158 | downX = event.getX();
159 | downY = event.getY();
160 | break;
161 | case MotionEvent.ACTION_MOVE:
162 | if (downX - x != 0 && Math.abs(y - downY) < scaledTouchSlop) {
163 | getParent().requestDisallowInterceptTouchEvent(true);
164 | }
165 | break;
166 | case MotionEvent.ACTION_UP:
167 | isDrawMoveLine = false;
168 | isCanSelected = false;
169 | if (null != mOnChartSelectedListener) {
170 | mOnChartSelectedListener.onChartSelected(false);
171 | }
172 | break;
173 | case MotionEvent.ACTION_CANCEL:
174 | isCanSelected = false;
175 | if (null != mOnChartSelectedListener) {
176 | mOnChartSelectedListener.onChartSelected(false);
177 | }
178 | break;
179 | }
180 | countRoundPoint(x);
181 | invalidate();
182 |
183 | if (slidingLine != null) {
184 | if (slidingLine.isOpenSlideSelect()) {
185 | return true;
186 | }
187 | }
188 | return false;
189 | }
190 |
191 | /**
192 | * 计算最接近的点
193 | *
194 | * @param x
195 | */
196 | private void countRoundPoint(float x) {
197 | if (line != null) {
198 | List axisXValues = axisX.getValues();
199 | int sizeX = axisXValues.size(); //几条y轴
200 | float xStep = (mWidth - leftPadding - startMarginX) / sizeX;
201 | int loc = Math.round((x - leftPadding - startMarginX) / xStep);
202 | List values = line.getValues();
203 | for (int i = 0, size = values.size(); i < size; i++) {
204 | PointValue pointValue = values.get(i);
205 | pointValue.setShowLabel(false);
206 | int ploc = Math.round(pointValue.getDiffX() / xStep);
207 | if (ploc == loc) {
208 | pointValue.setShowLabel(true);
209 | moveX = pointValue.getOriginX();
210 | moveY = pointValue.getOriginY() + LeafUtil.dp2px(mContext, line.getPointRadius());
211 | isDrawMoveLine = true;
212 | if (onPointSelectListener != null) {
213 | onPointSelectListener.onPointSelect(loc, axisXValues.get(loc).getLabel(), pointValue.getLabel());
214 | }
215 | // break;
216 | }
217 | }
218 | }
219 | }
220 |
221 | public void setChartData(Line chartData) {
222 | line = chartData;
223 | resetPointWeight();
224 | }
225 |
226 | public void setSlideLine(SlidingLine slideLine) {
227 | this.slidingLine = slideLine;
228 | }
229 |
230 | public Line getChartData() {
231 | return line;
232 | }
233 |
234 | public void setOnPointSelectListener(OnPointSelectListener onPointSelectListener) {
235 | this.onPointSelectListener = onPointSelectListener;
236 | }
237 |
238 | public void setOnChartSelectedListener(OnChartSelectedListener mOnChartSelectedListener) {
239 | this.mOnChartSelectedListener = mOnChartSelectedListener;
240 | }
241 |
242 | public void setCanSelected(boolean canSelected) {
243 | isCanSelected = canSelected;
244 | Vibrator vib = (Vibrator) getContext().getSystemService(Service.VIBRATOR_SERVICE);
245 | vib.vibrate(40);
246 | }
247 | }
248 |
--------------------------------------------------------------------------------
/leafchart/src/main/java/com/beiing/leafchart/AbsLeafChart.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchart;
2 |
3 | import android.content.Context;
4 | import android.content.res.TypedArray;
5 | import android.graphics.Canvas;
6 | import android.util.AttributeSet;
7 | import android.view.View;
8 |
9 | import com.beiing.leafchart.bean.Axis;
10 | import com.beiing.leafchart.bean.AxisValue;
11 | import com.beiing.leafchart.bean.ChartData;
12 | import com.beiing.leafchart.bean.PointValue;
13 | import com.beiing.leafchart.renderer.AbsRenderer;
14 | import com.beiing.leafchart.support.Chart;
15 | import com.beiing.leafchart.support.LeafUtil;
16 | import com.beiing.leafchart.support.Mode;
17 |
18 | import java.util.List;
19 |
20 | /**
21 | * Created by chenliu on 2016/7/15.
22 | * 描述:
23 | *
24 | */
25 | public abstract class AbsLeafChart extends View implements Chart {
26 |
27 | static final String TAG = SlideSelectLineChart.class.getName();
28 |
29 | /**
30 | * 坐标轴相交模式
31 | */
32 | protected int coordinateMode;
33 |
34 | /**
35 | * 第一个点x轴方向起始位置
36 | */
37 | protected int startMarginX = 0;
38 | /**
39 | * 第一个点y轴方向起始位置
40 | */
41 | protected int startMarginY = 0;
42 |
43 | protected Axis axisX;
44 | protected Axis axisY;
45 |
46 | protected float mWidth;//控件宽度
47 | protected float mHeight;//控件高度
48 | protected float leftPadding, topPadding, rightPadding, bottomPadding;//控件内部间隔
49 |
50 | protected Context mContext;
51 |
52 | private AbsRenderer absRenderer;
53 |
54 | public AbsLeafChart(Context context) {
55 | this(context, null, 0);
56 | }
57 |
58 | public AbsLeafChart(Context context, AttributeSet attrs) {
59 | this(context, attrs, 0);
60 | }
61 |
62 | public AbsLeafChart(Context context, AttributeSet attrs, int defStyleAttr) {
63 | super(context, attrs, defStyleAttr);
64 | mContext = context;
65 |
66 | initRenderer();
67 |
68 | setRenderer();
69 |
70 | initAttrs(attrs);
71 | }
72 |
73 | protected void initAttrs(AttributeSet attrs) {
74 | TypedArray ta = mContext.obtainStyledAttributes(attrs, R.styleable.AbsLeafChart);
75 | try {
76 | leftPadding = ta.getDimension(R.styleable.AbsLeafChart_lc_leftPadding, LeafUtil.dp2px(mContext, 20));
77 | topPadding = ta.getDimension(R.styleable.AbsLeafChart_lc_topPadding, LeafUtil.dp2px(mContext, 10));
78 | rightPadding = ta.getDimension(R.styleable.AbsLeafChart_lc_rightPadding, LeafUtil.dp2px(mContext, 10));
79 | bottomPadding = ta.getDimension(R.styleable.AbsLeafChart_lc_bottomPadding, LeafUtil.dp2px(mContext, 20));
80 | startMarginX = (int) ta.getDimension(R.styleable.AbsLeafChart_lc_startMarginX, 0);
81 | startMarginY = (int) ta.getDimension(R.styleable.AbsLeafChart_lc_startMarginY, 0);
82 | coordinateMode = ta.getInteger(R.styleable.AbsLeafChart_lc_coordinateMode, Mode.INTERSECT);
83 | } finally {
84 | ta.recycle();
85 | }
86 | }
87 |
88 | public void setRenderer(AbsRenderer renderer) {
89 | this.absRenderer = renderer;
90 | }
91 |
92 | ///////////////////子类必须重写的方法////////////////////////////
93 | protected abstract void initRenderer();
94 |
95 | protected abstract void setRenderer();
96 |
97 | protected abstract void resetPointWeight();
98 |
99 | /**
100 | * 不重写改方法,在布局中使用 wrap_content 显示效果是 match_parent
101 | *
102 | * @param widthMeasureSpec
103 | * @param heightMeasureSpec
104 | */
105 | @Override
106 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
107 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
108 | int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
109 | int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
110 | int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
111 | int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
112 |
113 | if (widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST) {
114 | setMeasuredDimension((int) LeafUtil.dp2px(mContext, 300), (int) LeafUtil.dp2px(mContext, 300));
115 | } else if (widthSpecMode == MeasureSpec.AT_MOST) {
116 | setMeasuredDimension((int) mWidth, heightSpecSize);
117 | } else if (heightSpecMode == MeasureSpec.AT_MOST) {
118 | setMeasuredDimension(widthSpecSize, (int) mHeight);
119 | }
120 |
121 | }
122 |
123 | @Override
124 | protected void onSizeChanged(int w, int h, int oldw, int oldh) {
125 | super.onSizeChanged(w, h, oldw, oldh);
126 |
127 | initViewSize();
128 |
129 | resetAsixSize();
130 |
131 | resetPointWeight();
132 |
133 | }
134 |
135 | protected void initViewSize() {
136 | mWidth = getMeasuredWidth();
137 | mHeight = getMeasuredHeight();
138 | absRenderer.setWH(mWidth, mHeight);
139 | absRenderer.setPadding(leftPadding, topPadding, rightPadding, bottomPadding);
140 | }
141 |
142 | /**
143 | * 设置坐标轴位置
144 | */
145 | public void resetAsixSize() {
146 |
147 | resetAsixX();
148 |
149 | resetAsixY();
150 |
151 | }
152 |
153 | protected void resetAsixY() {
154 | if (axisY != null) {
155 | List values = axisY.getValues();
156 | int sizeY = values.size(); //几条x轴
157 | float yStep = (mHeight - topPadding - bottomPadding - startMarginY) / sizeY;
158 | for (int i = 0; i < sizeY; i++) {
159 | AxisValue axisValue = values.get(i);
160 | axisValue.setPointX(leftPadding);
161 | if (i == 0) {
162 | axisValue.setPointY(mHeight - bottomPadding - startMarginY);
163 | } else {
164 | axisValue.setPointY(mHeight - bottomPadding - startMarginY - yStep * i);
165 | }
166 | }
167 | switch (coordinateMode) {
168 | case Mode.ACROSS:
169 | case Mode.Y_ACROSS:
170 | axisY.setStartY(mHeight - bottomPadding * 0.5f);
171 | break;
172 |
173 | case Mode.INTERSECT:
174 | case Mode.X_ACROSS:
175 | axisY.setStartY(mHeight - bottomPadding);
176 | break;
177 | }
178 | axisY.setStartX(leftPadding).setStopX(leftPadding).setStopY(0);
179 |
180 | }
181 | }
182 |
183 | protected void resetAsixX() {
184 | if (axisX != null) {
185 | List values = axisX.getValues();
186 | int sizeX = values.size(); //几条y轴
187 | float xStep = (mWidth - leftPadding - startMarginX) / sizeX;
188 | for (int i = 0; i < sizeX; i++) {
189 | AxisValue axisValue = values.get(i);
190 | axisValue.setPointY(mHeight);
191 | if (i == 0) {
192 | axisValue.setPointX(leftPadding + startMarginX);
193 | } else {
194 | axisValue.setPointX(leftPadding + startMarginX + xStep * i);
195 | }
196 | }
197 |
198 |
199 | switch (coordinateMode) {
200 | case Mode.ACROSS:
201 | case Mode.X_ACROSS:
202 | axisX.setStartX(leftPadding * 0.5f);
203 | break;
204 |
205 | case Mode.INTERSECT:
206 | case Mode.Y_ACROSS:
207 | axisX.setStartX(leftPadding);
208 | break;
209 | }
210 | axisX.setStartY(mHeight - bottomPadding).setStopX(mWidth).setStopY(mHeight - bottomPadding);
211 | }
212 | }
213 |
214 | @Override
215 | protected void onDraw(Canvas canvas) {
216 | super.onDraw(canvas);
217 | absRenderer.drawCoordinateLines(canvas, axisX, axisY);
218 | absRenderer.drawCoordinateText(canvas, axisX, axisY);
219 | }
220 |
221 | protected void resetPointWeight(ChartData chartData) {
222 | if (chartData != null && axisX != null && axisY != null) {
223 | List values = chartData.getValues();
224 | int size = values.size();
225 |
226 | List axisValuesX = axisX.getValues();
227 | List axisValuesY = axisY.getValues();
228 | float totalWidth = Math.abs(axisValuesX.get(0).getPointX() - axisValuesX.get(axisValuesX.size() - 1).getPointX());
229 |
230 | float totalHeight = Math.abs(axisValuesY.get(0).getPointY() - axisValuesY.get(axisValuesY.size() - 1).getPointY());
231 | for (int i = 0; i < size; i++) {
232 | PointValue pointValue = values.get(i);
233 | float diffX = pointValue.getX() * totalWidth;
234 | pointValue.setDiffX(diffX);
235 |
236 | float diffY = pointValue.getY() * totalHeight;
237 | pointValue.setDiffY(diffY);
238 |
239 | float originX1 = diffX + leftPadding + startMarginX;
240 | float originY1 = mHeight - bottomPadding - diffY - startMarginY;
241 | pointValue.setOriginX(originX1).setOriginY(originY1);
242 | }
243 | }
244 | }
245 |
246 | @Override
247 | public void setAxisX(Axis axisX) {
248 | this.axisX = axisX;
249 | resetAsixX();
250 | invalidate();
251 | }
252 |
253 | @Override
254 | public void setAxisY(Axis axisY) {
255 | this.axisY = axisY;
256 | resetAsixY();
257 | invalidate();
258 | }
259 |
260 | @Override
261 | public Axis getAxisX() {
262 | return axisX;
263 | }
264 |
265 | @Override
266 | public Axis getAxisY() {
267 | return axisY;
268 | }
269 | }
270 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/leafchart/src/main/java/com/beiing/leafchart/renderer/LeafLineRenderer.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchart.renderer;
2 |
3 | import android.animation.Animator;
4 | import android.animation.ObjectAnimator;
5 | import android.animation.ValueAnimator;
6 | import android.content.Context;
7 | import android.graphics.Canvas;
8 | import android.graphics.Color;
9 | import android.graphics.DashPathEffect;
10 | import android.graphics.LinearGradient;
11 | import android.graphics.Paint;
12 | import android.graphics.Path;
13 | import android.graphics.PathEffect;
14 | import android.graphics.PathMeasure;
15 | import android.graphics.Shader;
16 | import android.view.View;
17 |
18 | import com.beiing.leafchart.bean.Axis;
19 | import com.beiing.leafchart.bean.ChartData;
20 | import com.beiing.leafchart.bean.Line;
21 | import com.beiing.leafchart.bean.PointValue;
22 | import com.beiing.leafchart.support.LeafUtil;
23 |
24 | import java.util.List;
25 |
26 | /**
27 | * Created by chenliu on 2017/1/9.
28 | * 描述:
29 | *
30 | */
31 |
32 | public class LeafLineRenderer extends AbsRenderer {
33 | private static final float LINE_SMOOTHNESS = 0.16f;
34 |
35 | /**
36 | * 填充画笔
37 | **/
38 | private Paint fillPaint;
39 |
40 | private PathMeasure measure;
41 |
42 | /**
43 | * 动画结束标志
44 | */
45 | private boolean isAnimateEnd;
46 |
47 | /**
48 | * 是否开始绘制,防止动画绘制之前绘制一次
49 | */
50 | private boolean isShow;
51 |
52 | private float phase;
53 |
54 | private LinearGradient fillShader;
55 |
56 | public LeafLineRenderer(Context context, View view) {
57 | super(context, view);
58 | }
59 |
60 | @Override
61 | protected void initPaint() {
62 | super.initPaint();
63 | fillPaint = new Paint();
64 | fillPaint.setStyle(Paint.Style.FILL);
65 | }
66 |
67 | /**
68 | * 画折线
69 | *
70 | * @param canvas
71 | */
72 | public void drawLines(Canvas canvas, Line line) {
73 | if (line != null && isShow) {
74 | linePaint.setColor(line.getLineColor());
75 | linePaint.setStrokeWidth(LeafUtil.dp2px(mContext, line.getLineWidth()));
76 | linePaint.setStyle(Paint.Style.STROKE);
77 | List values = line.getValues();
78 | Path path = line.getPath();
79 | int size = values.size();
80 | for (int i = 0; i < size; i++) {
81 | PointValue point = values.get(i);
82 | if (i == 0) path.moveTo(point.getOriginX(), point.getOriginY());
83 | else path.lineTo(point.getOriginX(), point.getOriginY());
84 | }
85 |
86 | measure = new PathMeasure(path, false);
87 | linePaint.setPathEffect(createPathEffect(measure.getLength(), phase, 0.0f));
88 | canvas.drawPath(path, linePaint);
89 |
90 | }
91 | }
92 |
93 |
94 | /**
95 | * 画曲线
96 | *
97 | * @param canvas
98 | */
99 | public void drawCubicPath(Canvas canvas, Line line) {
100 | if (line != null && isShow) {
101 | linePaint.setColor(line.getLineColor());
102 | linePaint.setStrokeWidth(LeafUtil.dp2px(mContext, line.getLineWidth()));
103 | linePaint.setStyle(Paint.Style.STROKE);
104 | Path path = line.getPath();
105 |
106 | float prePreviousPointX = Float.NaN;
107 | float prePreviousPointY = Float.NaN;
108 | float previousPointX = Float.NaN;
109 | float previousPointY = Float.NaN;
110 | float currentPointX = Float.NaN;
111 | float currentPointY = Float.NaN;
112 | float nextPointX = Float.NaN;
113 | float nextPointY = Float.NaN;
114 |
115 | List values = line.getValues();
116 | final int lineSize = values.size();
117 | for (int valueIndex = 0; valueIndex < lineSize; ++valueIndex) {
118 | if (Float.isNaN(currentPointX)) {
119 | PointValue linePoint = values.get(valueIndex);
120 | currentPointX = linePoint.getOriginX();
121 | currentPointY = linePoint.getOriginY();
122 | }
123 | if (Float.isNaN(previousPointX)) {
124 | if (valueIndex > 0) {
125 | PointValue linePoint = values.get(valueIndex - 1);
126 | previousPointX = linePoint.getOriginX();
127 | previousPointY = linePoint.getOriginY();
128 | } else {
129 | previousPointX = currentPointX;
130 | previousPointY = currentPointY;
131 | }
132 | }
133 |
134 | if (Float.isNaN(prePreviousPointX)) {
135 | if (valueIndex > 1) {
136 | PointValue linePoint = values.get(valueIndex - 2);
137 | prePreviousPointX = linePoint.getOriginX();
138 | prePreviousPointY = linePoint.getOriginY();
139 | } else {
140 | prePreviousPointX = previousPointX;
141 | prePreviousPointY = previousPointY;
142 | }
143 | }
144 |
145 | // nextPoint is always new one or it is equal currentPoint.
146 | if (valueIndex < lineSize - 1) {
147 | PointValue linePoint = values.get(valueIndex + 1);
148 | nextPointX = linePoint.getOriginX();
149 | nextPointY = linePoint.getOriginY();
150 | } else {
151 | nextPointX = currentPointX;
152 | nextPointY = currentPointY;
153 | }
154 |
155 | if (valueIndex == 0) {
156 | // Move to start point.
157 | path.moveTo(currentPointX, currentPointY);
158 | } else {
159 | // Calculate control points.
160 | final float firstDiffX = (currentPointX - prePreviousPointX);
161 | final float firstDiffY = (currentPointY - prePreviousPointY);
162 | final float secondDiffX = (nextPointX - previousPointX);
163 | final float secondDiffY = (nextPointY - previousPointY);
164 | final float firstControlPointX = previousPointX + (LINE_SMOOTHNESS * firstDiffX);
165 | final float firstControlPointY = previousPointY + (LINE_SMOOTHNESS * firstDiffY);
166 | final float secondControlPointX = currentPointX - (LINE_SMOOTHNESS * secondDiffX);
167 | final float secondControlPointY = currentPointY - (LINE_SMOOTHNESS * secondDiffY);
168 |
169 | if (currentPointY == previousPointY) {
170 | path.lineTo(currentPointX, currentPointY);
171 | } else {
172 | path.cubicTo(firstControlPointX, firstControlPointY, secondControlPointX, secondControlPointY,
173 | currentPointX, currentPointY);
174 | }
175 | }
176 |
177 | // Shift values by one back to prevent recalculation of values that have
178 | // been already calculated.
179 | prePreviousPointX = previousPointX;
180 | prePreviousPointY = previousPointY;
181 | previousPointX = currentPointX;
182 | previousPointY = currentPointY;
183 | currentPointX = nextPointX;
184 | currentPointY = nextPointY;
185 | }
186 |
187 | measure = new PathMeasure(path, false);
188 | linePaint.setPathEffect(createPathEffect(measure.getLength(), phase, 0.0f));
189 | canvas.drawPath(path, linePaint);
190 | }
191 | }
192 |
193 |
194 | /**
195 | * 填充
196 | *
197 | * @param canvas
198 | */
199 | public void drawFillArea(Canvas canvas, Line line, Axis axisX) {
200 | //继续使用前面的 path
201 | if (line != null && line.getValues().size() > 1 && isShow) {
202 | List values = line.getValues();
203 | PointValue firstPoint = values.get(0);
204 | float firstX = firstPoint.getOriginX();
205 |
206 | Path path = line.getPath();
207 | PointValue lastPoint = values.get(values.size() - 1);
208 | float lastX = lastPoint.getOriginX();
209 | path.lineTo(lastX, axisX.getStartY());
210 | path.lineTo(firstX, axisX.getStartY());
211 | path.close();
212 |
213 | if (fillShader == null) {
214 | fillShader = new LinearGradient(0, 0, 0, mHeight, line.getFillColor(), Color.TRANSPARENT, Shader.TileMode.CLAMP);
215 | fillPaint.setShader(fillShader);
216 | }
217 |
218 | if (line.getFillColor() == 0)
219 | fillPaint.setAlpha(100);
220 | else
221 | fillPaint.setColor(line.getFillColor());
222 |
223 | canvas.save(Canvas.CLIP_SAVE_FLAG);
224 | canvas.clipRect(firstX, 0, phase * (lastX - firstX) + firstX, mHeight);
225 | canvas.drawPath(path, fillPaint);
226 | canvas.restore();
227 | path.reset();
228 | }
229 | }
230 |
231 | /**
232 | * 画圆点
233 | *
234 | * @param canvas
235 | */
236 | public void drawPoints(Canvas canvas, Line line) {
237 | if (line != null && line.isHasPoints() && isShow) {
238 | List values = line.getValues();
239 | float radius = LeafUtil.dp2px(mContext, line.getPointRadius());
240 | float strokeWidth = LeafUtil.dp2px(mContext, 1);
241 | PointValue point;
242 | for (int i = 0, size = values.size(); i < size; i++) {
243 | point = values.get(i);
244 | labelPaint.setStyle(Paint.Style.FILL);
245 | labelPaint.setColor(line.getPointColor());
246 | canvas.drawCircle(point.getOriginX(), point.getOriginY(),
247 | radius, labelPaint);
248 | labelPaint.setStyle(Paint.Style.STROKE);
249 | labelPaint.setColor(Color.WHITE);
250 | labelPaint.setStrokeWidth(strokeWidth);
251 | canvas.drawCircle(point.getOriginX(), point.getOriginY(),
252 | radius, labelPaint);
253 | }
254 | }
255 | }
256 |
257 | /**
258 | * 带动画的绘制
259 | *
260 | * @param duration
261 | */
262 | public void showWithAnimation(int duration) {
263 | isAnimateEnd = false;
264 | ObjectAnimator animator = ObjectAnimator.ofFloat(this, "phase", 0.0f, 1.0f);
265 | animator.setDuration(duration);
266 | animator.start();
267 | isShow = true;
268 |
269 | animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
270 | @Override
271 | public void onAnimationUpdate(ValueAnimator animation) {
272 | phase = (float) animation.getAnimatedValue();
273 | }
274 | });
275 |
276 | animator.addListener(new Animator.AnimatorListener() {
277 | @Override
278 | public void onAnimationStart(Animator animation) {
279 | }
280 |
281 | @Override
282 | public void onAnimationEnd(Animator animation) {
283 | isAnimateEnd = true;
284 | }
285 |
286 | @Override
287 | public void onAnimationCancel(Animator animation) {
288 |
289 | }
290 |
291 | @Override
292 | public void onAnimationRepeat(Animator animation) {
293 |
294 | }
295 | });
296 | }
297 |
298 |
299 | //showWithAnimation动画开启后会调用该方法
300 | public void setPhase(float phase) {
301 | chartView.invalidate();
302 | }
303 |
304 | private PathEffect createPathEffect(float pathLength, float phase, float offset) {
305 | return new DashPathEffect(new float[]{phase * pathLength, pathLength}, 0);
306 | }
307 |
308 |
309 | @Override
310 | public void drawLabels(Canvas canvas, ChartData chartData, Axis axisY) {
311 | if (isAnimateEnd)
312 | super.drawLabels(canvas, chartData, axisY);
313 | }
314 | }
315 |
--------------------------------------------------------------------------------
/leafchart/src/main/java/com/beiing/leafchart/renderer/AbsRenderer.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchart.renderer;
2 |
3 | import android.content.Context;
4 | import android.graphics.Canvas;
5 | import android.graphics.Color;
6 | import android.graphics.Paint;
7 | import android.graphics.Path;
8 | import android.graphics.Rect;
9 | import android.graphics.RectF;
10 | import android.view.View;
11 |
12 | import com.beiing.leafchart.bean.Axis;
13 | import com.beiing.leafchart.bean.AxisValue;
14 | import com.beiing.leafchart.bean.ChartData;
15 | import com.beiing.leafchart.bean.PointValue;
16 | import com.beiing.leafchart.support.LeafUtil;
17 |
18 | import java.util.List;
19 |
20 | /**
21 | * Created by chenliu on 2017/1/9.
22 | * 描述:渲染器
23 | *
24 | */
25 |
26 | public class AbsRenderer {
27 |
28 | public static final String TAG = AbsRenderer.class.getName();
29 |
30 | protected Context mContext;
31 |
32 | protected View chartView;
33 |
34 | /**
35 | * 控件宽度
36 | **/
37 | protected float mWidth;
38 | /**
39 | * 控件高度
40 | **/
41 | protected float mHeight;
42 | /**
43 | * 控件内部间隔
44 | **/
45 | protected float leftPadding, topPadding, rightPadding, bottomPadding;
46 |
47 | /**
48 | * 坐标轴
49 | */
50 | protected Paint coordPaint;
51 |
52 | /**
53 | * 折线图、直方图
54 | */
55 | protected Paint linePaint;
56 |
57 | /**
58 | * 标签
59 | */
60 | protected Paint labelPaint;
61 |
62 | /**
63 | * 标签下面的小三角
64 | */
65 | protected Paint trianglePaint;
66 |
67 |
68 | public AbsRenderer(Context context, View view) {
69 | this.mContext = context;
70 | this.chartView = view;
71 | initPaint();
72 | }
73 |
74 | protected void initPaint() {
75 | coordPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
76 | labelPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
77 | trianglePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
78 | linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
79 | linePaint.setStrokeCap(Paint.Cap.ROUND);
80 | }
81 |
82 | public void setWH(float width, float height) {
83 | this.mWidth = width;
84 | this.mHeight = height;
85 | }
86 |
87 | public void setPadding(float l, float t, float r, float b) {
88 | this.leftPadding = l;
89 | this.topPadding = t;
90 | this.rightPadding = r;
91 | this.bottomPadding = b;
92 | }
93 |
94 | /**
95 | * 坐标轴
96 | *
97 | * @param canvas
98 | */
99 | public void drawCoordinateLines(Canvas canvas, Axis axisX, Axis axisY) {
100 | if (axisX != null && axisY != null) {
101 | // 平行于y 轴的坐标轴
102 | if (axisY.isHasLines()) {
103 | coordPaint.setColor(axisY.getAxisLineColor());
104 | coordPaint.setStrokeWidth(LeafUtil.dp2px(mContext, axisY.getAxisLineWidth()));
105 | List valuesX = axisX.getValues();
106 | int sizeX = valuesX.size();
107 | for (int i = 0; i < sizeX; i++) {
108 | AxisValue value = valuesX.get(i);
109 | canvas.drawLine(value.getPointX(),
110 | axisY.getStartY() - LeafUtil.dp2px(mContext, axisY.getAxisWidth()),
111 | value.getPointX(), axisY.getStopY(), coordPaint);
112 | }
113 | }
114 |
115 | // 平行于x轴的坐标轴
116 | if (axisX.isHasLines()) {
117 | coordPaint.setColor(axisX.getAxisLineColor());
118 | coordPaint.setStrokeWidth(LeafUtil.dp2px(mContext, axisX.getAxisLineWidth()));
119 | List valuesY = axisY.getValues();
120 | int sizeY = valuesY.size();
121 | for (int i = 0; i < sizeY; i++) {
122 | AxisValue value = valuesY.get(i);
123 | canvas.drawLine(axisY.getStartX() + LeafUtil.dp2px(mContext, axisX.getAxisWidth()),
124 | value.getPointY(),
125 | axisX.getStopX(),
126 | value.getPointY(), coordPaint);
127 | }
128 | }
129 |
130 | //X坐标轴
131 | coordPaint.setColor(axisX.getAxisColor());
132 | coordPaint.setStrokeWidth(LeafUtil.dp2px(mContext, axisX.getAxisWidth()));
133 | canvas.drawLine(axisX.getStartX(), axisX.getStartY(), axisX.getStopX(), axisX.getStopY(), coordPaint);
134 |
135 | //Y坐标轴
136 | coordPaint.setColor(axisY.getAxisColor());
137 | coordPaint.setStrokeWidth(LeafUtil.dp2px(mContext, axisY.getAxisWidth()));
138 | canvas.drawLine(axisY.getStartX(),
139 | axisY.getStartY(), axisY.getStopX(), axisY.getStopY(), coordPaint);
140 | }
141 | }
142 |
143 | /**
144 | * 画坐标轴 刻度值
145 | *
146 | * @param canvas
147 | */
148 | public void drawCoordinateText(Canvas canvas, Axis axisX, Axis axisY) {
149 | if (axisX != null && axisY != null) {
150 | //////// X 轴
151 | // 1.刻度
152 | coordPaint.setColor(axisX.getTextColor());
153 | coordPaint.setTextSize(LeafUtil.sp2px(mContext, axisX.getTextSize()));
154 |
155 | Paint.FontMetrics fontMetrics = coordPaint.getFontMetrics(); // 获取标题文字的高度(fontMetrics.descent - fontMetrics.ascent)
156 | float textH = fontMetrics.descent - fontMetrics.ascent;
157 |
158 | List valuesX = axisX.getValues();
159 | if (axisX.isShowText()) {
160 | for (int i = 0; i < valuesX.size(); i++) {
161 | AxisValue value = valuesX.get(i);
162 | if (value.isShowLabel()) {
163 | float textW = coordPaint.measureText(value.getLabel());
164 | canvas.drawText(value.getLabel(), value.getPointX() - textW / 2, value.getPointY() - textH / 2, coordPaint);
165 | }
166 | }
167 | }
168 |
169 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
170 | /////// Y 轴
171 | coordPaint.setColor(axisY.getTextColor());
172 | coordPaint.setTextSize(LeafUtil.sp2px(mContext, axisY.getTextSize()));
173 |
174 | List valuesY = axisY.getValues();
175 | if (axisY.isShowText()) {
176 | for (AxisValue value : valuesY) {
177 | float textW = coordPaint.measureText(value.getLabel());
178 | float pointx = value.getPointX() - 1.1f * textW;
179 | canvas.drawText(value.getLabel(), pointx, value.getPointY(), coordPaint);
180 | }
181 | }
182 | }
183 | }
184 |
185 | /**
186 | * 画标签
187 | *
188 | * @param canvas
189 | * @param chartData
190 | */
191 | public void drawLabels(Canvas canvas, ChartData chartData, Axis axisY) {
192 | if (chartData != null) {
193 | if (chartData.isHasLabels()) {
194 | labelPaint.setTextSize(LeafUtil.sp2px(mContext, 12));
195 | List values = chartData.getValues();
196 | int size = values.size();
197 | float labelRadius = LeafUtil.dp2px(mContext, chartData.getLabelRadius());
198 | for (int i = 0; i < size; i++) {
199 | PointValue point = values.get(i);
200 | if (!point.isShowLabel()) continue;
201 | String label = point.getLabel();
202 | Rect bounds = new Rect();
203 | int length = label.length();
204 | labelPaint.getTextBounds(label, 0, length, bounds);
205 |
206 | float textW = bounds.width();
207 | float textH = bounds.height();
208 | float left, top, right, bottom, triangleStartX, triangleEndX;
209 | if (length == 1) {
210 | left = point.getOriginX() - textW * 2.2f;
211 | right = point.getOriginX() + textW * 2.2f;
212 | } else if (length == 2) {
213 | left = point.getOriginX() - textW * 1.0f;
214 | right = point.getOriginX() + textW * 1.0f;
215 | } else {
216 | left = point.getOriginX() - textW * 0.6f;
217 | right = point.getOriginX() + textW * 0.6f;
218 | }
219 | top = point.getOriginY() - 2.5f * textH;
220 | bottom = point.getOriginY() - 0.5f * textH;
221 |
222 | //控制位置
223 | if (left < axisY.getStartX()) {
224 | left = axisY.getStartX() + 8;
225 | right += left;
226 | if (point.getOriginX() - left <= LeafUtil.dp2px(mContext, 4)) {
227 | triangleStartX = left + labelRadius;
228 | if (right - left > LeafUtil.dp2px(mContext, 8)) {
229 | triangleEndX = triangleStartX + LeafUtil.dp2px(mContext, 8);
230 | } else {
231 | triangleEndX = (right - left) / 2;
232 | }
233 | } else if (((right - labelRadius) - (left + labelRadius)) >= LeafUtil.dp2px(mContext, 8)) {
234 | triangleStartX = point.getOriginX() - LeafUtil.dp2px(mContext, 4);
235 | triangleEndX = point.getOriginX() + LeafUtil.dp2px(mContext, 4);
236 | } else {
237 | triangleStartX = left + labelRadius;
238 | triangleEndX = right - labelRadius;
239 | }
240 | } else {
241 | if (((right - labelRadius) - (left + labelRadius)) >= LeafUtil.dp2px(mContext, 8)) {
242 | triangleStartX = left + labelRadius + ((right - labelRadius) - (left + labelRadius)) / 2 - LeafUtil.dp2px(mContext, 4);
243 | triangleEndX = triangleStartX + LeafUtil.dp2px(mContext, 8);
244 | } else {
245 | triangleStartX = left + labelRadius;
246 | triangleEndX = right - labelRadius;
247 | }
248 | }
249 |
250 | if (top < 0) {
251 | top = topPadding;
252 | bottom += topPadding;
253 | }
254 |
255 | top -= LeafUtil.dp2px(mContext, 7);
256 | bottom -= LeafUtil.dp2px(mContext, 7);
257 |
258 | if (right > mWidth) {
259 | right -= rightPadding;
260 | left -= rightPadding;
261 | }
262 |
263 | RectF rectF = new RectF(left, top, right, bottom);
264 | labelPaint.setColor(chartData.getLabelColor());
265 | labelPaint.setStyle(Paint.Style.FILL);
266 | canvas.drawRoundRect(rectF, labelRadius, labelRadius, labelPaint);
267 |
268 | trianglePaint.setStrokeWidth(3.0f);
269 | trianglePaint.setStyle(Paint.Style.FILL_AND_STROKE);
270 | trianglePaint.setColor(chartData.getLabelColor());
271 | Path triangle = new Path();
272 | triangle.moveTo(triangleStartX, bottom);
273 | triangle.lineTo(triangleEndX, bottom);
274 | triangle.lineTo(point.getOriginX(), point.getOriginY() - LeafUtil.dp2px(mContext, 5));
275 | triangle.lineTo(triangleStartX, bottom);
276 | triangle.close();
277 | canvas.drawPath(triangle, trianglePaint);
278 |
279 | //drawText
280 | labelPaint.setColor(Color.WHITE);
281 | float xCoordinate = left + (right - left - textW) / 2;
282 | float yCoordinate = bottom - (bottom - top - textH) / 2;
283 | canvas.drawText(point.getLabel(), xCoordinate, yCoordinate, labelPaint);
284 | }
285 | }
286 | }
287 | }
288 |
289 | private boolean isInArea(float x, float y, float touchX, float touchY, float radius) {
290 | float diffX = touchX - x;
291 | float diffY = touchY - y;
292 | return Math.pow(diffX, 2) + Math.pow(diffY, 2) <= 2 * Math.pow(radius, 2);
293 | }
294 |
295 |
296 | }
297 |
--------------------------------------------------------------------------------
/leafchart/src/main/java/com/beiing/leafchart/renderer/OutsideLineRenderer.java:
--------------------------------------------------------------------------------
1 | package com.beiing.leafchart.renderer;
2 |
3 | import android.animation.Animator;
4 | import android.animation.ObjectAnimator;
5 | import android.animation.ValueAnimator;
6 | import android.content.Context;
7 | import android.graphics.Canvas;
8 | import android.graphics.Color;
9 | import android.graphics.DashPathEffect;
10 | import android.graphics.LinearGradient;
11 | import android.graphics.Paint;
12 | import android.graphics.Path;
13 | import android.graphics.PathEffect;
14 | import android.graphics.PathMeasure;
15 | import android.graphics.Rect;
16 | import android.graphics.RectF;
17 | import android.graphics.Shader;
18 | import android.view.View;
19 |
20 | import com.beiing.leafchart.bean.Axis;
21 | import com.beiing.leafchart.bean.AxisValue;
22 | import com.beiing.leafchart.bean.ChartData;
23 | import com.beiing.leafchart.bean.Line;
24 | import com.beiing.leafchart.bean.PointValue;
25 | import com.beiing.leafchart.support.LeafUtil;
26 |
27 | import java.util.List;
28 |
29 | /**
30 | * Created by chenliu on 2017/1/12.
31 | * 描述:
32 | *
33 | */
34 |
35 | public class OutsideLineRenderer extends AbsRenderer {
36 |
37 | /**填充画笔**/
38 | private Paint fillPaint;
39 |
40 | private PathMeasure measure;
41 |
42 | /**
43 | * 动画结束标志
44 | */
45 | private boolean isAnimateEnd;
46 |
47 | /**
48 | * 是否开始绘制,防止动画绘制之前绘制一次
49 | */
50 | private boolean isShow;
51 |
52 | private float phase;
53 |
54 | private LinearGradient fillShader;
55 |
56 | public OutsideLineRenderer(Context context, View view) {
57 | super(context, view);
58 | }
59 |
60 | @Override
61 | protected void initPaint() {
62 | super.initPaint();
63 | fillPaint = new Paint();
64 | fillPaint.setStyle(Paint.Style.FILL);
65 | }
66 |
67 | /**
68 | * 画坐标轴 刻度值
69 | * @param canvas
70 | */
71 | public void drawCoordinateText(Canvas canvas, Axis axisX, Axis axisY, int moveX) {
72 | if(axisX != null && axisY != null){
73 | //////// X 轴
74 | // 1.刻度
75 | coordPaint.setColor(axisX.getTextColor());
76 | coordPaint.setTextSize(LeafUtil.sp2px(mContext, axisX.getTextSize()));
77 |
78 | Paint.FontMetrics fontMetrics = coordPaint.getFontMetrics(); // 获取标题文字的高度(fontMetrics.descent - fontMetrics.ascent)
79 | float textH = fontMetrics.descent - fontMetrics.ascent;
80 |
81 | List valuesX = axisX.getValues();
82 | if(axisX.isShowText()){
83 | for (int i = 0; i < valuesX.size(); i++) {
84 | AxisValue value = valuesX.get(i);
85 | if(value.isShowLabel()){
86 | float textW = coordPaint.measureText(value.getLabel());
87 | canvas.drawText(value.getLabel(), value.getPointX() - textW / 2 + moveX, value.getPointY() - textH / 2, coordPaint);
88 | }
89 | }
90 | }
91 |
92 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
93 | /////// Y 轴
94 | coordPaint.setColor(axisY.getTextColor());
95 | coordPaint.setTextSize(LeafUtil.sp2px(mContext, axisY.getTextSize()));
96 |
97 | List valuesY = axisY.getValues();
98 | if(axisY.isShowText()){
99 | for (AxisValue value : valuesY){
100 | float textW = coordPaint.measureText(value.getLabel());
101 | float pointx = value.getPointX() - 1.1f * textW;
102 | canvas.drawText(value.getLabel(), pointx , value.getPointY(), coordPaint);
103 | }
104 | }
105 | }
106 | }
107 |
108 | /**
109 | * 画折线
110 | *
111 | * @param canvas
112 | */
113 | public void drawLines(Canvas canvas, Line line, Axis axisY, int moveX) {
114 | if(line != null && isShow){
115 | linePaint.setColor(line.getLineColor());
116 | linePaint.setStrokeWidth(LeafUtil.dp2px(mContext, line.getLineWidth()));
117 | linePaint.setStyle(Paint.Style.STROKE);
118 | List values = line.getValues();
119 | Path path = line.getPath();
120 | int size = values.size();
121 | for (int i = 0; i < size; i++) {
122 | PointValue point = values.get(i);
123 | if(i == 0) path.moveTo(point.getOriginX() + moveX, point.getOriginY());
124 | else path.lineTo(point.getOriginX() + moveX, point.getOriginY());
125 | }
126 |
127 | measure = new PathMeasure(path, false);
128 | linePaint.setPathEffect(createPathEffect(measure.getLength(), phase, 0.0f));
129 | canvas.save(Canvas.CLIP_SAVE_FLAG);
130 | canvas.clipRect(axisY.getStartX(), 0, mWidth, mHeight);
131 | canvas.drawPath(path, linePaint);
132 | canvas.restore();
133 | }
134 | }
135 |
136 |
137 | /**
138 | * 填充
139 | * @param canvas
140 | */
141 | public void drawFillArea(Canvas canvas, Line line, Axis axisX, int moveX) {
142 | //继续使用前面的 path
143 | if(line != null && line.getValues().size() > 1 && isShow){
144 | List values = line.getValues();
145 | PointValue firstPoint = values.get(0);
146 | float firstX = firstPoint.getOriginX();
147 |
148 | Path path = line.getPath();
149 | PointValue lastPoint = values.get(values.size() - 1);
150 | float lastX = lastPoint.getOriginX();
151 | path.lineTo(lastX + moveX, axisX.getStartY());
152 | path.lineTo(firstX + moveX, axisX.getStartY());
153 | path.close();
154 |
155 | if(fillShader == null){
156 | fillShader = new LinearGradient(0, 0, 0, mHeight, line.getFillColor(), Color.TRANSPARENT, Shader.TileMode.CLAMP);
157 | fillPaint.setShader(fillShader);
158 | }
159 |
160 | if(line.getFillColor() == 0)
161 | fillPaint.setAlpha(100);
162 | else
163 | fillPaint.setColor(line.getFillColor());
164 |
165 | canvas.save(Canvas.CLIP_SAVE_FLAG);
166 | canvas.clipRect(firstX, 0, phase * (lastX - firstX) + firstX + moveX, mHeight);
167 | canvas.drawPath(path, fillPaint);
168 | canvas.restore();
169 | path.reset();
170 | }
171 | }
172 |
173 | /**
174 | * 画圆点
175 | * @param canvas
176 | */
177 | public void drawPoints(Canvas canvas, Line line, Axis axisY, int moveX) {
178 | if (line != null && line.isHasPoints() && isShow) {
179 | List values = line.getValues();
180 | float radius = LeafUtil.dp2px(mContext, line.getPointRadius());
181 | float strokeWidth = LeafUtil.dp2px(mContext, 1);
182 | PointValue point;
183 | canvas.save(Canvas.CLIP_SAVE_FLAG);
184 | canvas.clipRect(axisY.getStartX(), 0, mWidth, mHeight);
185 | for (int i = 0, size = values.size(); i < size; i++) {
186 | point = values.get(i);
187 | labelPaint.setStyle(Paint.Style.FILL);
188 | labelPaint.setColor(line.getPointColor());
189 | canvas.drawCircle(point.getOriginX() + moveX, point.getOriginY(),
190 | radius , labelPaint);
191 | labelPaint.setStyle(Paint.Style.STROKE);
192 | labelPaint.setColor(Color.WHITE);
193 | labelPaint.setStrokeWidth(strokeWidth);
194 | canvas.drawCircle(point.getOriginX() + moveX, point.getOriginY(),
195 | radius , labelPaint);
196 | }
197 | canvas.restore();
198 | }
199 | }
200 |
201 | public void drawLabels(Canvas canvas, ChartData chartData, Axis axisY, int moveX) {
202 | if(isAnimateEnd){
203 | if (chartData != null) {
204 | if(chartData.isHasLabels()){
205 | labelPaint.setTextSize(LeafUtil.sp2px(mContext, 12));
206 | List values = chartData.getValues();
207 | int size = values.size();
208 | canvas.save(Canvas.CLIP_SAVE_FLAG);
209 | canvas.clipRect(axisY.getStartX(), 0, mWidth, mHeight);
210 | for (int i = 0; i < size; i++) {
211 | PointValue point = values.get(i);
212 | String label = point.getLabel();
213 | Rect bounds = new Rect();
214 | int length = label.length();
215 | labelPaint.getTextBounds(label, 0, length, bounds);
216 |
217 | float textW = bounds.width();
218 | float textH = bounds.height();
219 | float left, top, right, bottom;
220 | if(length == 1){
221 | left = point.getOriginX() - textW * 2.2f;
222 | right = point.getOriginX() + textW * 2.2f;
223 | } else if(length == 2){
224 | left = point.getOriginX() - textW * 1.0f;
225 | right = point.getOriginX() + textW * 1.0f;
226 | } else {
227 | left = point.getOriginX() - textW * 0.6f;
228 | right = point.getOriginX() + textW * 0.6f;
229 | }
230 | top = point.getOriginY() - 2.5f*textH;
231 | bottom = point.getOriginY() - 0.5f*textH;
232 |
233 | //控制位置
234 | if(left < axisY.getStartX()){
235 | left = axisY.getStartX();
236 | right += left;
237 | }
238 | if(top < 0){
239 | top = topPadding;
240 | bottom += topPadding;
241 | }
242 | if(right > mWidth){
243 | right -= rightPadding;
244 | left -= rightPadding;
245 | }
246 |
247 |
248 | RectF rectF = new RectF(left + moveX, top, right + moveX, bottom);
249 | float labelRadius = LeafUtil.dp2px(mContext,chartData.getLabelRadius());
250 | labelPaint.setColor(chartData.getLabelColor());
251 | labelPaint.setStyle(Paint.Style.FILL);
252 | canvas.drawRoundRect(rectF, labelRadius, labelRadius, labelPaint);
253 |
254 | //drawText
255 | labelPaint.setColor(Color.WHITE);
256 | float xCoordinate = left + (right - left - textW) / 2 + moveX;
257 | float yCoordinate = bottom - (bottom - top - textH) / 2 ;
258 | canvas.drawText(point.getLabel(), xCoordinate, yCoordinate, labelPaint);
259 | }
260 | canvas.restore();
261 | }
262 | }
263 | }
264 | }
265 |
266 | /**
267 | * 带动画的绘制
268 | * @param duration
269 | */
270 | public void showWithAnimation(int duration){
271 | isAnimateEnd = false;
272 | ObjectAnimator animator = ObjectAnimator.ofFloat(this, "phase", 0.0f, 1.0f);
273 | animator.setDuration(duration);
274 | animator.start();
275 | isShow = true;
276 |
277 | animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
278 | @Override
279 | public void onAnimationUpdate(ValueAnimator animation) {
280 | phase = (float) animation.getAnimatedValue();
281 | }
282 | });
283 |
284 | animator.addListener(new Animator.AnimatorListener() {
285 | @Override
286 | public void onAnimationStart(Animator animation) {
287 | }
288 |
289 | @Override
290 | public void onAnimationEnd(Animator animation) {
291 | isAnimateEnd = true;
292 | }
293 |
294 | @Override
295 | public void onAnimationCancel(Animator animation) {
296 |
297 | }
298 |
299 | @Override
300 | public void onAnimationRepeat(Animator animation) {
301 |
302 | }
303 | });
304 | }
305 |
306 |
307 | //showWithAnimation动画开启后会调用该方法
308 | public void setPhase(float phase) {
309 | chartView.invalidate();
310 | }
311 |
312 | private PathEffect createPathEffect(float pathLength, float phase, float offset) {
313 | return new DashPathEffect(new float[] { phase * pathLength, pathLength }, 0);
314 | }
315 |
316 |
317 | }
318 |
--------------------------------------------------------------------------------