├── line-chart-view
├── .gitignore
├── src
│ ├── main
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── org
│ │ │ └── hogel
│ │ │ └── android
│ │ │ └── linechartview
│ │ │ ├── DateLineChartView.java
│ │ │ ├── LineChartStyle.java
│ │ │ └── LineChartView.java
│ └── test
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── org
│ │ └── hogel
│ │ └── android
│ │ └── linechartview
│ │ ├── ViewTestBase.java
│ │ └── LineChartViewTest.java
├── proguard-rules.pro
└── build.gradle
├── line-chart-view-demo
├── .gitignore
├── src
│ └── main
│ │ ├── res
│ │ ├── drawable-hdpi
│ │ │ └── ic_launcher.png
│ │ ├── drawable-mdpi
│ │ │ └── ic_launcher.png
│ │ ├── drawable-xhdpi
│ │ │ └── ic_launcher.png
│ │ ├── drawable-xxhdpi
│ │ │ └── ic_launcher.png
│ │ ├── values
│ │ │ ├── strings.xml
│ │ │ ├── styles.xml
│ │ │ └── dimens.xml
│ │ ├── values-w820dp
│ │ │ └── dimens.xml
│ │ └── layout
│ │ │ ├── activity_date_chart.xml
│ │ │ ├── activity_main.xml
│ │ │ └── activity_chart.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── org
│ │ └── hogel
│ │ └── android
│ │ └── linechartviewdemo
│ │ ├── MainActivity.java
│ │ ├── LineChartActivity.java
│ │ └── DateLineChartActivity.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── line-chart-view.png
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .travis.yml
├── .gitignore
├── gradle.properties
├── LICENSE
├── release.rb
├── README.md
├── gradlew.bat
├── gradle-mvn-push.gradle
└── gradlew
/line-chart-view/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/line-chart-view-demo/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':line-chart-view', ':line-chart-view-demo'
2 |
--------------------------------------------------------------------------------
/line-chart-view.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hogelog/line-chart-view/HEAD/line-chart-view.png
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hogelog/line-chart-view/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/line-chart-view-demo/src/main/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hogelog/line-chart-view/HEAD/line-chart-view-demo/src/main/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/line-chart-view-demo/src/main/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hogelog/line-chart-view/HEAD/line-chart-view-demo/src/main/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/line-chart-view-demo/src/main/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hogelog/line-chart-view/HEAD/line-chart-view-demo/src/main/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/line-chart-view-demo/src/main/res/drawable-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hogelog/line-chart-view/HEAD/line-chart-view-demo/src/main/res/drawable-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/line-chart-view-demo/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | LineChartDemo
5 | Settings
6 |
7 |
8 |
--------------------------------------------------------------------------------
/line-chart-view/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
7 |
8 |
--------------------------------------------------------------------------------
/line-chart-view-demo/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/line-chart-view-demo/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Thu Jan 07 10:46:18 JST 2016
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-2.2.1-bin.zip
7 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: android
2 | jdk: oraclejdk8
3 | android:
4 | components:
5 | - tools
6 | - build-tools-23.0.2
7 | - extra-android-support
8 | - extra-android-m2repository
9 | - android-19
10 | licenses:
11 | - '.+'
12 | script:
13 | - ./gradlew clean assemble check
14 |
--------------------------------------------------------------------------------
/line-chart-view/src/test/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
7 |
8 |
12 |
13 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 |
15 | # Gradle files
16 | .gradle/
17 | build/
18 |
19 | # Local configuration file (sdk path, etc)
20 | local.properties
21 |
22 | # Proguard folder generated by Eclipse
23 | proguard/
24 |
25 | .DS_Store
26 | .idea
27 | *.iml
28 |
--------------------------------------------------------------------------------
/line-chart-view-demo/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | VERSION_NAME=0.2.1-SNAPSHOT
2 | VERSION_CODE=9
3 | GROUP=org.hogel
4 |
5 |
6 | POM_NAME=LineChartView
7 | POM_ARTIFACT_ID=line-chart-view
8 | POM_PACKAGING=jar
9 |
10 | POM_DESCRIPTION=Android Library to line chart view
11 | POM_URL=https://github.com/hogelog/line-chart-view
12 | POM_SCM_URL=https://github.com/hogelog/line-chart-view
13 | POM_SCM_CONNECTION=scm:git@github.com:hogelog/line-chart-view.git
14 | POM_SCM_DEV_CONNECTION=scm:git@github.com:hogelog/line-chart-view.git
15 | POM_LICENCE_NAME=The MIT License (MIT)
16 | POM_LICENCE_URL=http://opensource.org/licenses/MIT
17 | POM_LICENCE_DIST=repo
18 | POM_DEVELOPER_ID=hogelog
19 | POM_DEVELOPER_NAME=Sunao Komuro
20 |
--------------------------------------------------------------------------------
/line-chart-view/src/test/java/org/hogel/android/linechartview/ViewTestBase.java:
--------------------------------------------------------------------------------
1 | package org.hogel.android.linechartview;
2 |
3 | import android.app.Activity;
4 | import org.junit.After;
5 | import org.junit.Before;
6 | import org.junit.runner.RunWith;
7 | import org.robolectric.Robolectric;
8 | import org.robolectric.RobolectricGradleTestRunner;
9 |
10 | @RunWith(RobolectricGradleTestRunner.class)
11 | public abstract class ViewTestBase {
12 | Activity activity;
13 |
14 | @Before
15 | public void setUp() throws Exception {
16 | activity = Robolectric.setupActivity(Activity.class);
17 | }
18 |
19 | @After
20 | public void tearDown() throws Exception {
21 |
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/line-chart-view/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 /opt/android-sdk/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 |
--------------------------------------------------------------------------------
/line-chart-view-demo/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 /opt/android-sdk/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 |
--------------------------------------------------------------------------------
/line-chart-view-demo/src/main/res/layout/activity_date_chart.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
13 |
14 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/line-chart-view-demo/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
10 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/line-chart-view-demo/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 19
5 | buildToolsVersion '23.0.2'
6 |
7 | defaultConfig {
8 | applicationId "org.hogel.android.linechartviewdemo"
9 | minSdkVersion 9
10 | targetSdkVersion 19
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 |
15 | compileOptions {
16 | sourceCompatibility JavaVersion.VERSION_1_7
17 | targetCompatibility JavaVersion.VERSION_1_7
18 | }
19 | buildTypes {
20 | release {
21 | minifyEnabled false
22 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
23 | }
24 | }
25 | }
26 |
27 | dependencies {
28 | compile fileTree(dir: 'libs', include: ['*.jar'])
29 | compile 'com.android.support:appcompat-v7:19.1.0'
30 | compile 'com.google.guava:guava:17.0'
31 | compile project(':line-chart-view')
32 | }
33 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2014 hogelog
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/line-chart-view-demo/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
11 |
12 |
19 |
20 |
27 |
28 |
--------------------------------------------------------------------------------
/line-chart-view-demo/src/main/java/org/hogel/android/linechartviewdemo/MainActivity.java:
--------------------------------------------------------------------------------
1 | package org.hogel.android.linechartviewdemo;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.os.Bundle;
6 | import android.view.View;
7 |
8 |
9 | public class MainActivity extends Activity {
10 | @Override
11 | protected void onCreate(Bundle savedInstanceState) {
12 | super.onCreate(savedInstanceState);
13 | setContentView(R.layout.activity_main);
14 |
15 | View lineChartButton = findViewById(R.id.line_chart_button);
16 | lineChartButton.setOnClickListener(new View.OnClickListener() {
17 | @Override
18 | public void onClick(View v) {
19 | startActivity(new Intent(MainActivity.this, LineChartActivity.class));
20 | }
21 | });
22 | View dateLineChartButton = findViewById(R.id.date_line_chart_button);
23 | dateLineChartButton.setOnClickListener(new View.OnClickListener() {
24 | @Override
25 | public void onClick(View v) {
26 | startActivity(new Intent(MainActivity.this, DateLineChartActivity.class));
27 | }
28 | });
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/line-chart-view/src/test/java/org/hogel/android/linechartview/LineChartViewTest.java:
--------------------------------------------------------------------------------
1 | package org.hogel.android.linechartview;
2 |
3 | import org.junit.Test;
4 | import org.robolectric.annotation.Config;
5 |
6 | import static org.assertj.core.api.Assertions.assertThat;
7 | import static org.assertj.core.api.Assertions.fail;
8 |
9 | @Config(constants = BuildConfig.class)
10 | public class LineChartViewTest extends ViewTestBase {
11 | @Override
12 | public void setUp() throws Exception {
13 | super.setUp();
14 | }
15 |
16 | @Test
17 | public void createSuccess() {
18 | LineChartView lineChartView = new LineChartView(activity);
19 |
20 | lineChartView.setManualMinX(0);
21 | lineChartView.setManualMaxX(1000);
22 | assertThat(lineChartView.getXGridUnit()).isGreaterThan(0);
23 |
24 | lineChartView.setManualMinY(0);
25 | lineChartView.setManualMaxY(1000);
26 | assertThat(lineChartView.getYGridUnit()).isGreaterThan(0);
27 | }
28 |
29 | @Test
30 | public void createFailure() {
31 | try {
32 | new LineChartView(null);
33 | fail("Invalid context value null");
34 | } catch (Exception e) {
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/line-chart-view/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'org.robolectric'
3 |
4 | android {
5 | compileSdkVersion 19
6 | buildToolsVersion '23.0.2'
7 |
8 | defaultConfig {
9 | minSdkVersion 9
10 | targetSdkVersion 19
11 | versionName project.VERSION_NAME
12 | versionCode Integer.parseInt(project.VERSION_CODE)
13 | }
14 |
15 | compileOptions {
16 | sourceCompatibility JavaVersion.VERSION_1_7
17 | targetCompatibility JavaVersion.VERSION_1_7
18 | }
19 | }
20 |
21 | dependencies {
22 | compile fileTree(dir: 'libs', include: ['*.jar'])
23 |
24 | testCompile 'junit:junit:4.12'
25 | testCompile 'org.robolectric:robolectric:3.0'
26 | testCompile 'com.squareup.assertj:assertj-android:1.1.1'
27 | }
28 |
29 | android.testOptions.unitTests.all {
30 | include '**/*Test.class'
31 |
32 | maxHeapSize = "2048m"
33 | }
34 |
35 | android.libraryVariants.all { variant ->
36 | def name = variant.buildType.name
37 | def task = project.tasks.create "jar${name.capitalize()}", Jar
38 | task.dependsOn variant.javaCompile
39 | task.from variant.javaCompile.destinationDir
40 | artifacts.add('archives', task);
41 | }
42 |
43 | apply from: '../gradle-mvn-push.gradle'
44 |
--------------------------------------------------------------------------------
/line-chart-view-demo/src/main/res/layout/activity_chart.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
13 |
14 |
19 |
24 |
25 |
26 |
33 |
34 |
--------------------------------------------------------------------------------
/line-chart-view-demo/src/main/java/org/hogel/android/linechartviewdemo/LineChartActivity.java:
--------------------------------------------------------------------------------
1 | package org.hogel.android.linechartviewdemo;
2 |
3 | import android.app.Activity;
4 | import android.os.Bundle;
5 | import android.view.View;
6 | import org.hogel.android.linechartview.LineChartView;
7 |
8 | import java.util.ArrayList;
9 | import java.util.List;
10 |
11 | public class LineChartActivity extends Activity {
12 | private static final double MAX_Y = 100;
13 | private LineChartView chartView;
14 |
15 | @Override
16 | protected void onCreate(Bundle savedInstanceState) {
17 | super.onCreate(savedInstanceState);
18 |
19 | setContentView(R.layout.activity_chart);
20 |
21 | chartView = (LineChartView) findViewById(R.id.chart_view);
22 | chartView.setManualMinY(0);
23 |
24 | View nextButton = findViewById(R.id.next_data);
25 | nextButton.setOnClickListener(new View.OnClickListener() {
26 | @Override
27 | public void onClick(View v) {
28 | nextChartData();
29 | }
30 | });
31 | }
32 |
33 | private void nextChartData() {
34 | List points = new ArrayList<>();
35 | for (int i = 0; i < 5; i++) {
36 | int y = (int) (Math.random() * MAX_Y);
37 | points.add(new LineChartView.Point(i, y));
38 | }
39 | chartView.setPoints(points);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/release.rb:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env ruby
2 | gradle_properties = File.read("gradle.properties")
3 |
4 | unless gradle_properties =~ /^VERSION_NAME=(.+)-SNAPSHOT$/
5 | STDERR.puts "Invalid VERSION_NAME"
6 | exit 1
7 | end
8 |
9 | version_name = $1
10 | version_snapshot = "#{version_name}-SNAPSHOT"
11 |
12 | unless gradle_properties =~ /^VERSION_CODE=(\d+)$/
13 | STDERR.puts "Invalid VERSION_CODE"
14 | exit 1
15 | end
16 |
17 | version_code = $1.to_i
18 |
19 | gradle_properties.gsub!("#{version_name}-SNAPSHOT", version_name)
20 |
21 | File.write("gradle.properties", gradle_properties)
22 |
23 | system("git commit -a -m 'Release #{version_name}'") || exit(1)
24 | system("git tag #{version_name}") || exit(1)
25 | system("cd line-chart-view && ../gradlew clean uploadArchives") || exit(1)
26 |
27 | print "Type next version: "
28 | next_version = STDIN.readline.chomp
29 | next_version_snapshot = "#{next_version}-SNAPSHOT"
30 | gradle_properties.gsub!("VERSION_NAME=#{version_name}", "VERSION_NAME=#{next_version_snapshot}")
31 | gradle_properties.gsub!("VERSION_CODE=#{version_code}", "VERSION_CODE=#{version_code + 1}")
32 | File.write("gradle.properties", gradle_properties)
33 |
34 | readme = File.read("README.md")
35 | readme.gsub!(/org.hogel:line-chart-view:\d+\.\d+\.\d+/, "org.hogel:line-chart-view:#{version_name}")
36 | readme.gsub!(/\d+\.\d+\.\d+<\/version>/, "#{version_name}")
37 | readme.gsub!(/org.hogel:line-chart-view:\d+\.\d+\.\d+-SNAPSHOT/, "org.hogel:line-chart-view:#{next_version_snapshot}")
38 | File.write("README.md", readme)
39 |
40 | system("git commit -a -m 'Next version #{next_version}'") || exit(1)
41 | system("cd line-chart-view && ../gradlew clean uploadArchives") || exit(1)
42 |
43 | system("git push --tags") || exit(1)
44 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # line-chart-view [](https://travis-ci.org/hogelog/line-chart-view) [](https://maven-badges.herokuapp.com/maven-central/org.hogel/line-chart-view)
2 |
3 |
4 | Android line chart view library.
5 |
6 | ## Usage
7 | ### Add dependency
8 | #### Gradle
9 |
10 | ```groovy
11 | dependencies {
12 | compile 'org.hogel:line-chart-view:0.2.0'
13 | }
14 | ```
15 |
16 | #### Maven
17 |
18 | ```xml
19 |
20 | org.hogel
21 | line-chart-view
22 | 0.2.0
23 |
24 | ```
25 |
26 | ### LineChartView
27 |
28 | ```java
29 | List points = new ArrayList();
30 | points.add(new LineChartView.Point(-17, -100));
31 | points.add(new LineChartView.Point(4, 200));
32 | points.add(new LineChartView.Point(5, 400));
33 | points.add(new LineChartView.Point(6, 1100));
34 | points.add(new LineChartView.Point(7, 700));
35 |
36 | LineChartView lineChartView = new LineChartView(this, points);
37 |
38 | chartContainer.addView(lineChartView);
39 | ```
40 |
41 | 
42 |
43 | ## Example
44 | See [line-chart-view-demo](https://github.com/hogelog/line-chart-view/tree/master/line-chart-view-demo)
45 |
46 | ## API
47 | See [Javadoc](http://hogelog.github.io/line-chart-view/javadoc/)
48 |
49 | ## Development version
50 | Use sonatype snapshot repository.
51 |
52 | ```groovy
53 | repositories {
54 | maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
55 | }
56 |
57 | dependencies {
58 | compile 'org.hogel:line-chart-view:0.2.1-SNAPSHOT'
59 | }
60 | ```
61 |
--------------------------------------------------------------------------------
/line-chart-view/src/main/java/org/hogel/android/linechartview/DateLineChartView.java:
--------------------------------------------------------------------------------
1 | package org.hogel.android.linechartview;
2 |
3 | import android.content.Context;
4 | import android.text.format.DateFormat;
5 |
6 | import java.util.ArrayList;
7 | import java.util.List;
8 |
9 | public class DateLineChartView extends LineChartView {
10 |
11 | private static final long HALF_DAY = 12 * 60 * 60 * 1000;
12 |
13 | private static final long A_DAY = 24 * 60 * 60 * 1000;
14 |
15 | public DateLineChartView(Context context) {
16 | this(context, new ArrayList());
17 | }
18 |
19 | public DateLineChartView(Context context, List points) {
20 | this(context, points, new LineChartStyle());
21 | }
22 |
23 | public DateLineChartView(Context context, List points, LineChartStyle lineChartStyle) {
24 | super(context, points, lineChartStyle);
25 | }
26 |
27 | public DateLineChartView(Context context, LineChartStyle lineChartStyle) {
28 | super(context, new ArrayList(), lineChartStyle);
29 | }
30 |
31 | @Override
32 | protected String formatXLabel(long x) {
33 | if (lineChartStyle.getXLabelFormatter() != null) {
34 | return lineChartStyle.getXLabelFormatter().format(x);
35 | }
36 | return DateFormat.format("yyyy/M/d", x).toString();
37 | }
38 |
39 | @Override
40 | public long getMaxX() {
41 | if (manualMaxX != null) {
42 | return manualMaxX;
43 | }
44 | return getRawMaxX() + HALF_DAY;
45 | }
46 |
47 | @Override
48 | public long getRawMinX() {
49 | if (points.isEmpty()) {
50 | return (System.currentTimeMillis() / A_DAY - 7) * A_DAY;
51 | }
52 | return super.getRawMinX();
53 | }
54 |
55 | @Override
56 | public long getRawMaxX() {
57 | if (points.isEmpty()) {
58 | return (System.currentTimeMillis() / A_DAY) * A_DAY;
59 | }
60 | return super.getRawMaxX();
61 | }
62 |
63 | @Override
64 | public long getMinY() {
65 | if (manualMinY != null) {
66 | return manualMinY;
67 | }
68 | return 0;
69 | }
70 |
71 | @Override
72 | public long getXGridUnit() {
73 | if (manualXGridUnit != null) {
74 | return manualXGridUnit;
75 | }
76 | return A_DAY;
77 | }
78 |
79 | @Override
80 | protected long calcMinGridValue(long min, long gridUnit) {
81 | return min;
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/line-chart-view-demo/src/main/java/org/hogel/android/linechartviewdemo/DateLineChartActivity.java:
--------------------------------------------------------------------------------
1 | package org.hogel.android.linechartviewdemo;
2 |
3 | import org.hogel.android.linechartview.DateLineChartView;
4 | import org.hogel.android.linechartview.LineChartStyle;
5 | import org.hogel.android.linechartview.LineChartView;
6 |
7 | import android.app.Activity;
8 | import android.os.Bundle;
9 | import android.text.format.DateFormat;
10 | import android.view.ViewGroup;
11 |
12 | import java.text.ParseException;
13 | import java.text.SimpleDateFormat;
14 | import java.util.ArrayList;
15 | import java.util.List;
16 |
17 | public class DateLineChartActivity extends Activity {
18 | @Override
19 | protected void onCreate(Bundle savedInstanceState) {
20 | super.onCreate(savedInstanceState);
21 |
22 | setContentView(R.layout.activity_date_chart);
23 |
24 | ViewGroup chartContainer = (ViewGroup) findViewById(R.id.chart_container);
25 |
26 | LineChartStyle lineChartStyle = new LineChartStyle();
27 | lineChartStyle.setDrawPointCenter(false);
28 | LineChartStyle.Border leftBottomBorder = new LineChartStyle.Border(
29 | LineChartStyle.Border.LEFT, LineChartStyle.Border.BOTTOM
30 | );
31 | leftBottomBorder.setWidth(8.0f);
32 | lineChartStyle.addBorder(leftBottomBorder);
33 | lineChartStyle.setXLabelFormatter(new LineChartStyle.LabelFormatter() {
34 | @Override
35 | public String format(long value) {
36 | return DateFormat.format("M/d", value).toString();
37 | }
38 | });
39 | lineChartStyle.setYLabelWidth(80.0f);
40 | DateLineChartView chartView = new DateLineChartView(this, lineChartStyle);
41 | chartView.setStyle(lineChartStyle);
42 | chartView.setManualXGridUnit(2 * 24 * 60 * 60 * 1000);
43 | chartContainer.addView(chartView);
44 |
45 | chartView.setPoints(generatePoints());
46 | List yLabels = chartView.getYLabels();
47 | yLabels.remove(0);
48 | yLabels.remove(yLabels.size() - 1);
49 | chartView.setManualYLabels(yLabels);
50 | }
51 |
52 | private List generatePoints() {
53 | List points = new ArrayList<>();
54 | try {
55 | points.add(new LineChartView.Point(date("2014/07/01"), 100));
56 | points.add(new LineChartView.Point(date("2014/07/02"), 200));
57 | points.add(new LineChartView.Point(date("2014/07/03"), 400));
58 | points.add(new LineChartView.Point(date("2014/07/05"), 1100));
59 | points.add(new LineChartView.Point(date("2014/07/06"), 700));
60 | points.add(new LineChartView.Point(date("2014/07/08"), 1700));
61 | points.add(new LineChartView.Point(date("2014/07/09"), 2700));
62 | points.add(new LineChartView.Point(date("2014/07/10"), 100));
63 | points.add(new LineChartView.Point(date("2014/07/11"), 1200));
64 | points.add(new LineChartView.Point(date("2014/07/12"), 1100));
65 | } catch (ParseException e) {
66 | throw new RuntimeException(e);
67 | }
68 | return points;
69 | }
70 |
71 | private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
72 |
73 | private long date(String date) throws ParseException {
74 | return dateFormat.parse(date).getTime();
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/gradle-mvn-push.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2013 Chris Banes
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | apply plugin: 'maven'
18 | apply plugin: 'signing'
19 |
20 | def isReleaseBuild() {
21 | return VERSION_NAME.contains("SNAPSHOT") == false
22 | }
23 |
24 | def getReleaseRepositoryUrl() {
25 | return hasProperty('RELEASE_REPOSITORY_URL') ? RELEASE_REPOSITORY_URL
26 | : "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
27 | }
28 |
29 | def getSnapshotRepositoryUrl() {
30 | return hasProperty('SNAPSHOT_REPOSITORY_URL') ? SNAPSHOT_REPOSITORY_URL
31 | : "https://oss.sonatype.org/content/repositories/snapshots/"
32 | }
33 |
34 | def getRepositoryUsername() {
35 | return hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : ""
36 | }
37 |
38 | def getRepositoryPassword() {
39 | return hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : ""
40 | }
41 |
42 | afterEvaluate { project ->
43 | uploadArchives {
44 | repositories {
45 | mavenDeployer {
46 | beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
47 |
48 | pom.groupId = GROUP
49 | pom.artifactId = POM_ARTIFACT_ID
50 | pom.version = VERSION_NAME
51 |
52 | repository(url: getReleaseRepositoryUrl()) {
53 | authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
54 | }
55 | snapshotRepository(url: getSnapshotRepositoryUrl()) {
56 | authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
57 | }
58 |
59 | pom.project {
60 | name POM_NAME
61 | packaging POM_PACKAGING
62 | description POM_DESCRIPTION
63 | url POM_URL
64 |
65 | scm {
66 | url POM_SCM_URL
67 | connection POM_SCM_CONNECTION
68 | developerConnection POM_SCM_DEV_CONNECTION
69 | }
70 |
71 | licenses {
72 | license {
73 | name POM_LICENCE_NAME
74 | url POM_LICENCE_URL
75 | distribution POM_LICENCE_DIST
76 | }
77 | }
78 |
79 | developers {
80 | developer {
81 | id POM_DEVELOPER_ID
82 | name POM_DEVELOPER_NAME
83 | }
84 | }
85 | }
86 | }
87 | }
88 | }
89 |
90 | signing {
91 | required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") }
92 | sign configurations.archives
93 | }
94 |
95 | task androidJavadocs(type: Javadoc) {
96 | options.locale = 'en_US'
97 | source = android.sourceSets.main.java.getSrcDirs()
98 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
99 | }
100 |
101 | task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
102 | classifier = 'javadoc'
103 | from androidJavadocs.destinationDir
104 | }
105 |
106 | task androidSourcesJar(type: Jar) {
107 | classifier = 'sources'
108 | from android.sourceSets.main.java.getSrcDirs()
109 | }
110 |
111 | artifacts {
112 | archives androidSourcesJar
113 | archives androidJavadocsJar
114 | }
115 | }
116 |
--------------------------------------------------------------------------------
/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 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/line-chart-view/src/main/java/org/hogel/android/linechartview/LineChartStyle.java:
--------------------------------------------------------------------------------
1 | package org.hogel.android.linechartview;
2 |
3 | import android.graphics.Color;
4 |
5 | import java.util.ArrayList;
6 | import java.util.List;
7 |
8 | public class LineChartStyle {
9 |
10 | public static final float AUTO_WIDTH = -1;
11 |
12 | public static final float AUTO_HEIGHT = -1;
13 |
14 | public static class Border {
15 | public static final int LEFT = 1;
16 |
17 | public static final int TOP = 2;
18 |
19 | public static final int RIGHT = 4;
20 |
21 | public static final int BOTTOM = 8;
22 |
23 | public static final int ALL = LEFT | TOP | RIGHT | BOTTOM;
24 |
25 | private int style;
26 |
27 | private int color = Color.GRAY;
28 |
29 | private float width = 1.0f;
30 |
31 | public Border(int... values) {
32 | style = 0;
33 | for (int value : values) {
34 | style |= value;
35 | }
36 | }
37 |
38 | public boolean contains(int value) {
39 | return (style & value) > 0;
40 | }
41 |
42 | public boolean left() {
43 | return contains(LEFT);
44 | }
45 |
46 | public boolean top() {
47 | return contains(TOP);
48 | }
49 |
50 | public boolean right() {
51 | return contains(RIGHT);
52 | }
53 |
54 | public boolean bottom() {
55 | return contains(BOTTOM);
56 | }
57 |
58 | public int getColor() {
59 | return color;
60 | }
61 |
62 | public void setColor(int color) {
63 | this.color = color;
64 | }
65 |
66 | public float getWidth() {
67 | return width;
68 | }
69 |
70 | public void setWidth(float width) {
71 | this.width = width;
72 | }
73 | }
74 |
75 |
76 | public interface LabelFormatter {
77 | String format(long value);
78 | }
79 |
80 | private int lineColor = Color.RED;
81 |
82 | private float lineWidth = 8.0f;
83 |
84 | private boolean drawPoint = true;
85 |
86 | private int pointColor = Color.RED;
87 |
88 | private float pointSize = 10.0f;
89 |
90 | private boolean drawPointCenter = true;
91 |
92 | private float pointCenterSize = 5.0f;
93 |
94 | private int gridColor = Color.GRAY;
95 |
96 | private float gridWidth = 2.0f;
97 |
98 | private int backgroundColor = Color.WHITE;
99 |
100 | private float labelTextSize = 20f;
101 |
102 | private int labelTextColor = Color.BLACK;
103 |
104 | private float yLabelMargin = 10f;
105 |
106 | private float yLabelWidth = AUTO_WIDTH;
107 |
108 | private float xLabelHeight = AUTO_HEIGHT;
109 |
110 | private float xLabelMargin = 10f;
111 |
112 | private final List borders = new ArrayList<>();
113 |
114 | private LabelFormatter xLabelFormatter = null;
115 |
116 | private LabelFormatter yLabelFormatter = null;
117 |
118 | public LineChartStyle() {
119 | borders.add(new Border(Border.ALL));
120 | }
121 |
122 | public int getLineColor() {
123 | return lineColor;
124 | }
125 |
126 | public void setLineColor(int lineColor) {
127 | this.lineColor = lineColor;
128 | }
129 |
130 | public float getLineWidth() {
131 | return lineWidth;
132 | }
133 |
134 | public void setLineWidth(float lineWidth) {
135 | this.lineWidth = lineWidth;
136 | }
137 |
138 | public int getPointColor() {
139 | return pointColor;
140 | }
141 |
142 | public void setPointColor(int pointColor) {
143 | this.pointColor = pointColor;
144 | }
145 |
146 | public float getPointSize() {
147 | return pointSize;
148 | }
149 |
150 | public void setPointSize(float pointSize) {
151 | this.pointSize = pointSize;
152 | }
153 |
154 | public float getPointCenterSize() {
155 | return pointCenterSize;
156 | }
157 |
158 | public void setPointCenterSize(float pointCenterSize) {
159 | this.pointCenterSize = pointCenterSize;
160 | }
161 |
162 | public int getGridColor() {
163 | return gridColor;
164 | }
165 |
166 | public void setGridColor(int gridColor) {
167 | this.gridColor = gridColor;
168 | }
169 |
170 | public float getGridWidth() {
171 | return gridWidth;
172 | }
173 |
174 | public void setGridWidth(float gridWidth) {
175 | this.gridWidth = gridWidth;
176 | }
177 |
178 | public boolean isDrawPoint() {
179 | return drawPoint;
180 | }
181 |
182 | public void setDrawPoint(boolean drawPoint) {
183 | this.drawPoint = drawPoint;
184 | }
185 |
186 | public boolean isDrawPointCenter() {
187 | return drawPointCenter;
188 | }
189 |
190 | public void setDrawPointCenter(boolean drawPointCenter) {
191 | this.drawPointCenter = drawPointCenter;
192 | }
193 |
194 | public int getBackgroundColor() {
195 | return backgroundColor;
196 | }
197 |
198 | public void setBackgroundColor(int backgroundColor) {
199 | this.backgroundColor = backgroundColor;
200 | }
201 |
202 | public float getLabelTextSize() {
203 | return labelTextSize;
204 | }
205 |
206 | public void setLabelTextSize(float labelTextSize) {
207 | this.labelTextSize = labelTextSize;
208 | }
209 |
210 | public int getLabelTextColor() {
211 | return labelTextColor;
212 | }
213 |
214 | public void setLabelTextColor(int labelTextColor) {
215 | this.labelTextColor = labelTextColor;
216 | }
217 |
218 | public float getYLabelMargin() {
219 | return yLabelMargin;
220 | }
221 |
222 | public void setYLabelMargin(float yLabelMargin) {
223 | this.yLabelMargin = yLabelMargin;
224 | }
225 |
226 | public float getXLabelMargin() {
227 | return xLabelMargin;
228 | }
229 |
230 | public void setXLabelMargin(float xLabelMargin) {
231 | this.xLabelMargin = xLabelMargin;
232 | }
233 |
234 | public List getBorders() {
235 | return borders;
236 | }
237 |
238 | public void addBorder(Border border) {
239 | borders.add(border);
240 | }
241 |
242 | public void clearBorders() {
243 | borders.clear();
244 | }
245 |
246 | public LabelFormatter getXLabelFormatter() {
247 | return xLabelFormatter;
248 | }
249 |
250 | public void setXLabelFormatter(LabelFormatter xLabelFormatter) {
251 | this.xLabelFormatter = xLabelFormatter;
252 | }
253 |
254 | public LabelFormatter getYLabelFormatter() {
255 | return yLabelFormatter;
256 | }
257 |
258 | public void setYLabelFormatter(LabelFormatter yLabelFormatter) {
259 | this.yLabelFormatter = yLabelFormatter;
260 | }
261 |
262 | public float getYLabelWidth() {
263 | return yLabelWidth;
264 | }
265 |
266 | public void setYLabelWidth(float yLabelWidth) {
267 | this.yLabelWidth = yLabelWidth;
268 | }
269 |
270 | public float getXLabelHeight() {
271 | return xLabelHeight;
272 | }
273 |
274 | public void setXLabelHeight(float xLabelHeight) {
275 | this.xLabelHeight = xLabelHeight;
276 | }
277 | }
278 |
--------------------------------------------------------------------------------
/line-chart-view/src/main/java/org/hogel/android/linechartview/LineChartView.java:
--------------------------------------------------------------------------------
1 | package org.hogel.android.linechartview;
2 |
3 | import android.content.Context;
4 | import android.graphics.Canvas;
5 | import android.graphics.Paint;
6 | import android.graphics.Rect;
7 | import android.graphics.drawable.ShapeDrawable;
8 | import android.graphics.drawable.shapes.Shape;
9 | import android.util.AttributeSet;
10 | import android.view.View;
11 |
12 | import java.util.ArrayList;
13 | import java.util.List;
14 |
15 | public class LineChartView extends View {
16 |
17 | private static final long DEFAULT_MAX_X = 1000;
18 |
19 | private static final long DEFAULT_MAX_Y = 1000;
20 |
21 | public static class Point {
22 | private long x;
23 |
24 | private long y;
25 |
26 | public Point() {
27 | }
28 |
29 | public Point(long x, long y) {
30 | this.x = x;
31 | this.y = y;
32 | }
33 |
34 | public long getX() {
35 | return x;
36 | }
37 |
38 | public void setX(long x) {
39 | this.x = x;
40 | }
41 |
42 | public long getY() {
43 | return y;
44 | }
45 |
46 | public void setY(long y) {
47 | this.y = y;
48 | }
49 | }
50 |
51 | protected final List points;
52 |
53 | protected final Paint paint = new Paint();
54 |
55 | protected final Paint labelPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
56 |
57 | protected final Paint borderPaint = new Paint();
58 |
59 | protected final ShapeDrawable chartDrawable;
60 |
61 | protected final ShapeDrawable yLabelDrawable;
62 |
63 | protected final ShapeDrawable xLabelDrawable;
64 |
65 | protected LineChartStyle lineChartStyle;
66 |
67 | protected Long manualXGridUnit = null;
68 |
69 | protected Long manualYGridUnit = null;
70 |
71 | protected long yLabelWidth = 0;
72 |
73 | protected long xLabelHeight = 0;
74 |
75 | protected long chartTopMargin = 0;
76 |
77 | protected long chartRightMargin = 0;
78 |
79 | protected List manualXLabels = null;
80 |
81 | protected List manualYLabels = null;
82 |
83 | protected Long manualMinX = null;
84 |
85 | protected Long manualMaxX = null;
86 |
87 | protected Long manualMinY = null;
88 |
89 | protected Long manualMaxY = null;
90 |
91 | public LineChartView(Context context) {
92 | this(context, new ArrayList());
93 | }
94 |
95 | public LineChartView(Context context, AttributeSet attrs) {
96 | super(context, attrs);
97 |
98 | points = new ArrayList<>();
99 | lineChartStyle = new LineChartStyle();
100 |
101 | paint.setAntiAlias(true);
102 |
103 | yLabelDrawable = new ShapeDrawable();
104 | xLabelDrawable = new ShapeDrawable();
105 |
106 | chartDrawable = new ShapeDrawable();
107 |
108 | updateIfEditMode();
109 | updateDrawables();
110 | }
111 |
112 | public LineChartView(Context context, List points) {
113 | this(context, points, new LineChartStyle());
114 | }
115 |
116 | public LineChartView(Context context, LineChartStyle lineChartStyle) {
117 | this(context, new ArrayList(), lineChartStyle);
118 | }
119 |
120 | public LineChartView(Context context, List points, LineChartStyle lineChartStyle) {
121 | super(context);
122 | this.points = points;
123 | this.lineChartStyle = lineChartStyle;
124 | paint.setAntiAlias(true);
125 |
126 | yLabelDrawable = new ShapeDrawable();
127 | xLabelDrawable = new ShapeDrawable();
128 |
129 | chartDrawable = new ShapeDrawable();
130 | updateDrawables();
131 | }
132 |
133 | public void updateDrawables() {
134 | drawXLabels(xLabelDrawable);
135 | drawYLabels(yLabelDrawable);
136 | drawLineChart(chartDrawable);
137 | invalidate();
138 | }
139 |
140 | @Override
141 | protected void onDraw(Canvas canvas) {
142 | yLabelDrawable.draw(canvas);
143 | xLabelDrawable.draw(canvas);
144 | chartDrawable.draw(canvas);
145 | }
146 |
147 | protected void drawYLabels(ShapeDrawable labelDrawable) {
148 | Shape labelsShape = new Shape() {
149 | @Override
150 | public void draw(Canvas canvas, Paint paint) {
151 | labelPaint.setTextAlign(Paint.Align.RIGHT);
152 | labelPaint.setTextSize(lineChartStyle.getLabelTextSize());
153 | labelPaint.setColor(lineChartStyle.getLabelTextColor());
154 |
155 | long minY = getMinY();
156 | long maxY = getMaxY();
157 | long yrange = maxY - minY;
158 |
159 | float height = getHeight();
160 |
161 | float left = getYLabelWidth();
162 | List yLabels = getYLabels();
163 | for (long y : yLabels) {
164 | String label = formatYLabel(y);
165 | float yCoordinate = getYCoordinate(height, y, minY, yrange);
166 | canvas.drawText(label, left, yCoordinate, labelPaint);
167 | }
168 | }
169 | };
170 | measureYLabel();
171 | labelDrawable.setBounds(0, 0, getWidth(), getHeight());
172 | labelDrawable.setShape(labelsShape);
173 | }
174 |
175 | protected void measureYLabel() {
176 | labelPaint.setTextAlign(Paint.Align.RIGHT);
177 | labelPaint.setTextSize(lineChartStyle.getLabelTextSize());
178 |
179 | long minY = getMinY();
180 | long maxY = getMaxY();
181 | long yGridUnit = getYGridUnit();
182 |
183 | yLabelWidth = 0;
184 | chartTopMargin = 0;
185 |
186 | long y = minY;
187 | Rect textBounds = new Rect();
188 |
189 | while (y <= maxY) {
190 | String label = formatYLabel(y);
191 | labelPaint.getTextBounds(label, 0, label.length(), textBounds);
192 | if (textBounds.width() > yLabelWidth) {
193 | yLabelWidth = textBounds.width();
194 | }
195 | chartTopMargin = textBounds.height();
196 | y += yGridUnit;
197 | }
198 | }
199 |
200 | protected String formatYLabel(long y) {
201 | if (lineChartStyle.getYLabelFormatter() != null) {
202 | return lineChartStyle.getYLabelFormatter().format(y);
203 | }
204 | return String.format("%,d", y);
205 | }
206 |
207 | protected void drawXLabels(ShapeDrawable labelDrawable) {
208 | Shape labelsShape = new Shape() {
209 | @Override
210 | public void draw(Canvas canvas, Paint paint) {
211 | labelPaint.setTextAlign(Paint.Align.CENTER);
212 | labelPaint.setTextSize(lineChartStyle.getLabelTextSize());
213 | labelPaint.setColor(lineChartStyle.getLabelTextColor());
214 |
215 | long minX = getMinX();
216 | long maxX = getMaxX();
217 | long xrange = maxX - minX;
218 |
219 | float width = getWidth();
220 | float height = getHeight();
221 |
222 | float labelHeight = height - lineChartStyle.getXLabelMargin();
223 | Rect textBounds = new Rect();
224 | List xLabels = getXLabels();
225 | for (long x : xLabels) {
226 | String label = formatXLabel(x);
227 | labelPaint.getTextBounds(label, 0, label.length(), textBounds);
228 | float xCoordinate = getXCoordinate(width, x, minX, xrange);
229 | canvas.drawText(label, xCoordinate, labelHeight, labelPaint);
230 | }
231 | }
232 | };
233 | measureXLabel();
234 | labelDrawable.setBounds(0, 0, getWidth(), getHeight());
235 | labelDrawable.setShape(labelsShape);
236 | }
237 |
238 | protected void measureXLabel() {
239 | labelPaint.setTextAlign(Paint.Align.CENTER);
240 | labelPaint.setTextSize(lineChartStyle.getLabelTextSize());
241 |
242 | long minX = getMinX();
243 | long maxX = getMaxX();
244 | long xGridUnit = getXGridUnit();
245 |
246 | xLabelHeight = 0;
247 | chartRightMargin = 0;
248 |
249 | long x = minX;
250 | Rect textBounds = new Rect();
251 |
252 | while (x <= maxX) {
253 | String label = formatXLabel(x);
254 | labelPaint.getTextBounds(label, 0, label.length(), textBounds);
255 | int height = (int) (textBounds.height() + lineChartStyle.getXLabelMargin() * 2);
256 | if (height > xLabelHeight) {
257 | xLabelHeight = height;
258 | }
259 | chartRightMargin = textBounds.width() / 2;
260 | x += xGridUnit;
261 | }
262 | }
263 |
264 | protected String formatXLabel(long x) {
265 | if (lineChartStyle.getXLabelFormatter() != null) {
266 | return lineChartStyle.getXLabelFormatter().format(x);
267 | }
268 | return String.format("%,d", x);
269 | }
270 |
271 | protected void drawLineChart(ShapeDrawable chartDrawable) {
272 | Shape chartShape = new Shape() {
273 | @Override
274 | public void draw(Canvas canvas, Paint paint) {
275 | long minX = getMinX();
276 | long maxX = getMaxX();
277 | long xrange = maxX - minX;
278 |
279 | long minY = getMinY();
280 | long maxY = getMaxY();
281 | long yrange = maxY - minY;
282 |
283 | float width = getWidth();
284 | float height = getHeight();
285 | float left = getChartLeftMargin();
286 | float top = getChartTopMargin();
287 | float right = width - getChartRightMargin();
288 | float bottom = height - getChartBottomMargin();
289 |
290 | drawChartFrame(canvas, left, top, right, bottom);
291 |
292 | drawXGrid(canvas, minX, xrange);
293 | drawYGrid(canvas, minY, yrange);
294 |
295 | List borders = lineChartStyle.getBorders();
296 | for (LineChartStyle.Border border : borders) {
297 | drawChartBorder(canvas, border, left, top, right, bottom);
298 | }
299 |
300 | drawLines(canvas, minX, xrange, minY, yrange);
301 |
302 | if (lineChartStyle.isDrawPoint()) {
303 | drawPoints(canvas, minX, xrange, minY, yrange);
304 | }
305 | }
306 | };
307 | chartDrawable.setBounds(0, 0, getWidth(), getHeight());
308 | chartDrawable.setShape(chartShape);
309 | }
310 |
311 | protected void drawChartFrame(Canvas canvas, float left, float top, float right, float bottom) {
312 | canvas.save();
313 | canvas.clipRect(left, top, right, bottom);
314 | canvas.drawColor(lineChartStyle.getBackgroundColor());
315 | canvas.restore();
316 | }
317 |
318 | protected void drawChartBorder(Canvas canvas, LineChartStyle.Border border, float left, float top, float right, float bottom) {
319 | borderPaint.setColor(border.getColor());
320 | borderPaint.setStrokeWidth(border.getWidth());
321 |
322 | float fixWidth = border.getWidth() / 2;
323 | float leftFix = border.left() ? fixWidth : 0;
324 | float topFix = border.top() ? fixWidth : 0;
325 | float bottomFix = border.bottom() ? fixWidth : 0;
326 | float rightFix = border.right() ? fixWidth : 0;
327 | if (border.left()) {
328 | canvas.drawLine(left, top - topFix, left, bottom + bottomFix, borderPaint);
329 | }
330 | if (border.top()) {
331 | canvas.drawLine(left - leftFix, top, right + rightFix, top, borderPaint);
332 | }
333 | if (border.right()) {
334 | canvas.drawLine(right, top - topFix, right, bottom + bottomFix, borderPaint);
335 | }
336 | if (border.bottom()) {
337 | canvas.drawLine(left - leftFix, bottom, right + rightFix, bottom, borderPaint);
338 | }
339 | }
340 |
341 | public float getChartLeftMargin() {
342 | return getYLabelWidth() + lineChartStyle.getYLabelMargin();
343 | }
344 |
345 | public float getChartTopMargin() {
346 | return chartTopMargin;
347 | }
348 |
349 | public float getChartRightMargin() {
350 | return chartRightMargin;
351 | }
352 |
353 | public float getChartBottomMargin() {
354 | return getXLabelHeight() + lineChartStyle.getXLabelMargin();
355 | }
356 |
357 | public void clearManualMinX() {
358 | manualMinX = null;
359 | updateDrawables();
360 | }
361 |
362 | public void setManualMinX(long minX) {
363 | manualMinX = minX;
364 | updateDrawables();
365 | }
366 |
367 | public long getMinX() {
368 | if (manualMinX != null) {
369 | return manualMinX;
370 | }
371 | return getRawMinX();
372 | }
373 |
374 | public long getRawMinX() {
375 | if (points.isEmpty()) {
376 | return 0;
377 | }
378 | return points.get(0).getX();
379 | }
380 |
381 | public void clearManualMaxX(){
382 | manualMaxX = null;
383 | updateDrawables();
384 | }
385 |
386 | public void setManualMaxX(long maxX) {
387 | manualMaxX = maxX;
388 | updateDrawables();
389 | }
390 |
391 | public long getMaxX() {
392 | if (manualMaxX != null) {
393 | return manualMaxX;
394 | }
395 | long rawMaxX = getRawMaxX();
396 | long step = getUnit(getAbsMaxX());
397 | return (long) ((Math.floor(1.0 * rawMaxX / step) + 1) * step);
398 | }
399 |
400 | public long getRawMaxX() {
401 | if (points.isEmpty()) {
402 | return DEFAULT_MAX_X;
403 | }
404 | return points.get(points.size() - 1).getX();
405 | }
406 |
407 | protected long getAbsMaxX() {
408 | if (points.isEmpty()) {
409 | return DEFAULT_MAX_X;
410 | }
411 | long absMaxX = Long.MIN_VALUE;
412 | for (Point point : points) {
413 | long x = Math.abs(point.getX());
414 | if (x > absMaxX) {
415 | absMaxX = x;
416 | }
417 | }
418 | return absMaxX;
419 | }
420 |
421 | protected float getXCoordinate(float width, Point point, long minX, long xrange) {
422 | return getXCoordinate(width, point.getX(), minX, xrange);
423 | }
424 |
425 | protected float getXCoordinate(float width, long x, long minX, long xrange) {
426 | return getXCoordinate(width, x, minX, xrange, true);
427 | }
428 |
429 | protected float getXCoordinate(float width, long x, long minX, long xrange, boolean inChartArea) {
430 | if (inChartArea) {
431 | float left = getChartLeftMargin();
432 | float right = getChartRightMargin();
433 | float margin = left + right;
434 | return (width - margin) * (x - minX) * 1.0f / (xrange) + left;
435 | } else {
436 | return width * (x - minX) * 1.0f / xrange;
437 | }
438 | }
439 |
440 | protected long getAbsMaxY() {
441 | if (points.isEmpty()) {
442 | return DEFAULT_MAX_Y;
443 | }
444 | long absMaxY = Long.MIN_VALUE;
445 | for (Point point : points) {
446 | long y = Math.abs(point.getY());
447 | if (y > absMaxY) {
448 | absMaxY = y;
449 | }
450 | }
451 | return absMaxY;
452 | }
453 |
454 | public void clearManualMinY() {
455 | manualMinY = null;
456 | updateDrawables();
457 | }
458 |
459 | public void setManualMinY(long minY) {
460 | manualMinY = minY;
461 | updateDrawables();
462 | }
463 |
464 | public long getMinY() {
465 | if (manualMinY != null) {
466 | return manualMinY;
467 | }
468 | long rawMinY = getRawMinY();
469 | long step = getUnit(getAbsMaxY());
470 | return (long) ((Math.ceil(1.0 * rawMinY / step) - 1) * step);
471 | }
472 |
473 | public long getRawMinY() {
474 | if (points.isEmpty()) {
475 | return 0;
476 | }
477 | long minY = Long.MAX_VALUE;
478 | for (Point point : points) {
479 | long y = point.getY();
480 | if (y < minY) {
481 | minY = y;
482 | }
483 | }
484 | return minY;
485 | }
486 |
487 | public void clearManualMaxY() {
488 | manualMaxY = null;
489 | updateDrawables();
490 | }
491 |
492 | public void setManualMaxY(long maxY) {
493 | manualMaxY = maxY;
494 | updateDrawables();
495 | }
496 |
497 | public long getMaxY() {
498 | if (manualMaxY != null) {
499 | return manualMaxY;
500 | }
501 | long rawMaxY = getRawMaxY();
502 | long step = getUnit(getAbsMaxY());
503 | return (long) ((Math.floor(1.0 * rawMaxY / step) + 1) * step);
504 | }
505 |
506 | public long getRawMaxY() {
507 | if (points.isEmpty()) {
508 | return DEFAULT_MAX_Y;
509 | }
510 | long maxY = Long.MIN_VALUE;
511 | for (Point point : points) {
512 | long y = point.getY();
513 | if (y > maxY) {
514 | maxY = y;
515 | }
516 | }
517 | return maxY;
518 | }
519 |
520 | protected long getUnit(long maxValue) {
521 | int digits = (int) Math.log10(maxValue);
522 | long unit = (long) Math.pow(10, digits);
523 | return unit;
524 | }
525 |
526 | protected float getYCoordinate(float height, Point point, long minY, long yrange) {
527 | return getYCoordinate(height, point.getY(), minY, yrange);
528 | }
529 |
530 | protected float getYCoordinate(float height, long y, long minY, long yrange) {
531 | return getYCoordinate(height, y, minY, yrange, true);
532 | }
533 |
534 | protected float getYCoordinate(float height, long y, long minY, long yrange, boolean inChartArea) {
535 | if (inChartArea) {
536 | float top = getChartTopMargin();
537 | float bottom = getChartBottomMargin();
538 | float margin = top + bottom;
539 | return (height - margin) * (1.0f - (y - minY) * 1.0f / (yrange)) + top;
540 | } else {
541 | return height * (1.0f - (y - minY) * 1.0f / yrange);
542 | }
543 | }
544 |
545 | protected void drawXGrid(Canvas canvas, long minX, long xrange) {
546 | long maxX = getMaxX();
547 | long xGridUnit = getXGridUnit();
548 |
549 | float width = getWidth();
550 | float height = getHeight();
551 |
552 | float top = getChartTopMargin();
553 | float bottom = height - getChartBottomMargin();
554 |
555 | paint.setColor(lineChartStyle.getGridColor());
556 | paint.setStrokeWidth(lineChartStyle.getGridWidth());
557 |
558 | long x = calcMinGridValue(minX, xGridUnit);
559 |
560 | while (x <= maxX) {
561 | float xCoordinate = getXCoordinate(width, x, minX, xrange);
562 | canvas.drawLine(xCoordinate, bottom, xCoordinate, top, paint);
563 | x += xGridUnit;
564 | }
565 | }
566 |
567 | protected void drawYGrid(Canvas canvas, long minY, long yrange) {
568 | long yGridUnit = getYGridUnit();
569 | long maxY = getMaxY();
570 |
571 | float width = getWidth();
572 | float height = getHeight();
573 |
574 | float left = getChartLeftMargin();
575 | float right = width - getChartRightMargin();
576 |
577 | paint.setColor(lineChartStyle.getGridColor());
578 | paint.setStrokeWidth(lineChartStyle.getGridWidth());
579 |
580 | long y = calcMinGridValue(minY, yGridUnit);
581 | while (y <= maxY) {
582 | float yCoordinate = getYCoordinate(height, y, minY, yrange);
583 | canvas.drawLine(left, yCoordinate, right, yCoordinate, paint);
584 | y += yGridUnit;
585 | }
586 | }
587 |
588 | protected void drawLines(Canvas canvas, long minX, long xrange, long minY, long yrange) {
589 | Point prevPoint = null;
590 | float px = 0.0f, py = 0.0f;
591 |
592 | float width = getWidth();
593 | float height = getHeight();
594 |
595 | paint.setColor(lineChartStyle.getLineColor());
596 | paint.setStrokeWidth(lineChartStyle.getLineWidth());
597 | for (Point point : points) {
598 | float x = getXCoordinate(width, point, minX, xrange);
599 | float y = getYCoordinate(height, point, minY, yrange);
600 | if (prevPoint != null) {
601 | canvas.drawLine(px, py, x, y, paint);
602 | }
603 | prevPoint = point;
604 | px = x;
605 | py = y;
606 | }
607 | }
608 |
609 | protected void drawPoints(Canvas canvas, long minX, long xrange, long minY, long yrange) {
610 | float width = getWidth();
611 | float height = getHeight();
612 |
613 | for (Point point : points) {
614 | float x = getXCoordinate(width, point, minX, xrange);
615 | float y = getYCoordinate(height, point, minY, yrange);
616 |
617 | paint.setColor(lineChartStyle.getLineColor());
618 | canvas.drawCircle(x, y, lineChartStyle.getPointSize(), paint);
619 |
620 | if (lineChartStyle.isDrawPointCenter()) {
621 | paint.setColor(lineChartStyle.getBackgroundColor());
622 | canvas.drawCircle(x, y, lineChartStyle.getPointCenterSize(), paint);
623 | }
624 | }
625 | }
626 |
627 | public void clearManualXGridUnit() {
628 | manualXGridUnit = null;
629 | updateDrawables();
630 | }
631 |
632 | public void setManualXGridUnit(long xGridUnit) {
633 | manualXGridUnit = xGridUnit;
634 | updateDrawables();
635 | }
636 |
637 | public long getXGridUnit() {
638 | if (manualXGridUnit != null) {
639 | return manualXGridUnit;
640 | }
641 | return getUnit(getAbsMaxX());
642 | }
643 |
644 | public void clearManualYGridUnit() {
645 | manualYGridUnit = null;
646 | updateDrawables();
647 | }
648 |
649 | public void setManualYGridUnit(long yGridUnit) {
650 | manualYGridUnit = yGridUnit;
651 | updateDrawables();
652 | }
653 |
654 | public long getYGridUnit() {
655 | if (manualYGridUnit != null) {
656 | return manualYGridUnit;
657 | }
658 | return getUnit(getAbsMaxY());
659 | }
660 |
661 | public List getXLabels() {
662 | if (manualXLabels != null) {
663 | return manualXLabels;
664 | }
665 |
666 | long minX = getMinX();
667 | long maxX = getMaxX();
668 | long xGridUnit = getXGridUnit();
669 | long x = calcMinGridValue(minX, xGridUnit);
670 | List xLabels = new ArrayList<>();
671 | while (x <= maxX) {
672 | xLabels.add(x);
673 | x += xGridUnit;
674 | }
675 | return xLabels;
676 | }
677 |
678 | public void clearManualXLabels() {
679 | manualXLabels = null;
680 | updateDrawables();
681 | }
682 |
683 | public void setManualXLabels(List labels) {
684 | manualXLabels = labels;
685 | updateDrawables();
686 | }
687 |
688 | public List getYLabels() {
689 | if (manualYLabels != null) {
690 | return manualYLabels;
691 | }
692 |
693 | long minY = getMinY();
694 | long maxY = getMaxY();
695 | long yGridUnit = getYGridUnit();
696 | long y = calcMinGridValue(minY, yGridUnit);
697 | List yLabels = new ArrayList<>();
698 | while (y <= maxY) {
699 | yLabels.add(y);
700 | y += yGridUnit;
701 | }
702 | return yLabels;
703 | }
704 |
705 | public void clearManualYLabels() {
706 | manualYLabels = null;
707 | updateDrawables();
708 | }
709 |
710 | public void setManualYLabels(List labels) {
711 | manualYLabels = labels;
712 | updateDrawables();
713 | }
714 |
715 | protected long calcMinGridValue(long min, long gridUnit) {
716 | return (long) (Math.ceil(1.0 * min / gridUnit) * gridUnit);
717 | }
718 |
719 | public float getYLabelWidth() {
720 | if (lineChartStyle.getYLabelWidth() != LineChartStyle.AUTO_WIDTH) {
721 | return lineChartStyle.getYLabelWidth();
722 | }
723 | return yLabelWidth;
724 | }
725 |
726 | public float getXLabelHeight() {
727 | if (lineChartStyle.getXLabelHeight() != LineChartStyle.AUTO_HEIGHT) {
728 | return lineChartStyle.getXLabelHeight();
729 | }
730 | return xLabelHeight;
731 | }
732 |
733 | public LineChartStyle getStyle() {
734 | return lineChartStyle;
735 | }
736 |
737 | public void setStyle(LineChartStyle lineChartStyle) {
738 | this.lineChartStyle = lineChartStyle;
739 | updateDrawables();
740 | }
741 |
742 | public List getPoints() {
743 | return points;
744 | }
745 |
746 | public void setPoints(List points) {
747 | this.points.clear();
748 | this.points.addAll(points);
749 | updateDrawables();
750 | }
751 |
752 | private void updateIfEditMode() {
753 | if (!isInEditMode()) {
754 | return;
755 | }
756 | List points = new ArrayList<>();
757 | points.add(new LineChartView.Point(-17, -100));
758 | points.add(new LineChartView.Point(4, 200));
759 | points.add(new LineChartView.Point(5, 400));
760 | points.add(new LineChartView.Point(6, 1100));
761 | points.add(new LineChartView.Point(7, 700));
762 | setPoints(points);
763 | }
764 |
765 | @Override
766 | protected void onSizeChanged(int w, int h, int oldw, int oldh) {
767 | super.onSizeChanged(w, h, oldw, oldh);
768 | updateDrawables();
769 | }
770 | }
771 |
--------------------------------------------------------------------------------