Huge Project Refresh

Before anyone freaks out, all these changes are under-the-hood, meaning that you probably won't even notice them at all.

The biggest difference is raising the minSdkVersion from 9 to 14. Recently android bumped this number interally as there are basically no devices running lower than 14 anymore. Sorry but you are just wasting your time if you are trying to support anything lower than 14 now.

The next biggest change is updating the project to the new AndroidX libraries, which changes some imports and nothing else really.

The third biggest change is fixing a few bugs in the code that cause values to be drawn even if there are none, which results in your app crashing. Surprisingly, these checks already existed in a few of the newer chart types, but most lacked this simple check.

Other than those three changes, nothing else is functionally different. Well, saving to gallery works on all charts in the example app now, and you can quickly see the code for each example in the menus.

The only real potential "breaking" change is that charts are now saved as PNGs by default instead of JPGs if you go the route of not specifying as a JPG. You may want to double check your file sizes as PNGs can be larger than low quality JPGs.

I still have more plans for simplifying the API and fixing other issues with the project, for the small few that closely pay attention to individual commits: there's going to be more soon.
This commit is contained in:
almic 2018-10-26 10:30:22 -06:00
parent 20bca4a8ca
commit d67ea481af
No known key found for this signature in database
GPG key ID: 432BBCA8E2FFF871
140 changed files with 4915 additions and 4289 deletions

View file

@ -1,71 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxmassdeveloper.mpchartexample"
android:versionCode="55"
android:versionName="3.0.2" >
package="com.xxmassdeveloper.mpchartexample">
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="25" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:allowBackup="false"
android:hardwareAccelerated="true"
android:icon="@drawable/ic_launcher"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:theme="@style/AppTheme">
<activity
android:name="com.xxmassdeveloper.mpchartexample.notimportant.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="LineChartActivity1"></activity> <!-- android:configChanges="keyboardHidden|orientation|screenSize" -->
<activity android:name="LineChartActivity2"></activity>
<activity android:name=".LineChartTime"></activity>
<activity android:name="BarChartActivity"></activity>
<activity android:name="HorizontalBarChartActivity"></activity>
<activity android:name="PieChartActivity"></activity>
<activity android:name="PiePolylineChartActivity"></activity>
<activity android:name="MultiLineChartActivity"></activity>
<activity android:name="BarChartActivityMultiDataset"></activity>
<activity android:name="DrawChartActivity"></activity>
<activity android:name="ScatterChartActivity"></activity>
<activity android:name="BubbleChartActivity"></activity>
<activity android:name="com.xxmassdeveloper.mpchartexample.fragments.SimpleChartDemo"></activity>
<activity android:name="ListViewBarChartActivity"></activity>
<activity android:name="ListViewMultiChartActivity"></activity>
<activity android:name="StackedBarActivity"></activity>
<activity android:name="AnotherBarActivity"></activity>
<activity android:name="InvertedLineChartActivity"></activity>
<activity android:name="CandleStickChartActivity"></activity>
<activity android:name="CubicLineChartActivity"></activity>
<activity android:name="RadarChartActivity"></activity>
<activity android:name="LineChartActivityColored"></activity>
<activity android:name="DynamicalAddingActivity"></activity>
<activity android:name=".realm.RealmDatabaseActivityLine"></activity>
<activity android:name=".realm.RealmDatabaseActivityBar"></activity>
<activity android:name=".realm.RealmDatabaseActivityHorizontalBar"></activity>
<activity android:name=".realm.RealmDatabaseActivityScatter"></activity>
<activity android:name=".realm.RealmDatabaseActivityCandle"></activity>
<activity android:name=".realm.RealmDatabaseActivityBubble"></activity>
<activity android:name=".realm.RealmDatabaseActivityPie"></activity>
<activity android:name=".realm.RealmDatabaseActivityRadar"></activity>
<activity android:name=".realm.RealmMainActivity"></activity>
<activity android:name="RealtimeLineChartActivity"></activity>
<activity android:name="CombinedChartActivity"></activity>
<activity android:name="PerformanceLineChart"></activity>
<activity android:name="BarChartActivitySinus"></activity>
<activity android:name="ScrollViewActivity"></activity>
<activity android:name="StackedBarActivityNegative"></activity>
<activity android:name=".realm.RealmWikiExample"></activity>
<activity android:name=".BarChartPositiveNegative"></activity>
<activity android:name=".FilledLineActivity"></activity>
<activity android:name=".HalfPieChartActivity"></activity>
<activity android:name="LineChartActivity1" /> <!-- android:configChanges="keyboardHidden|orientation|screenSize" -->
<activity android:name="LineChartActivity2" />
<activity android:name=".LineChartTime" />
<activity android:name="BarChartActivity" />
<activity android:name="HorizontalBarChartActivity" />
<activity android:name="PieChartActivity" />
<activity android:name="PiePolylineChartActivity" />
<activity android:name="MultiLineChartActivity" />
<activity android:name="BarChartActivityMultiDataset" />
<activity android:name="DrawChartActivity" />
<activity android:name="ScatterChartActivity" />
<activity android:name="BubbleChartActivity" />
<activity android:name="com.xxmassdeveloper.mpchartexample.fragments.SimpleChartDemo" />
<activity android:name="ListViewBarChartActivity" />
<activity android:name="ListViewMultiChartActivity" />
<activity android:name="StackedBarActivity" />
<activity android:name="AnotherBarActivity" />
<activity android:name="InvertedLineChartActivity" />
<activity android:name="CandleStickChartActivity" />
<activity android:name="CubicLineChartActivity" />
<activity android:name="RadarChartActivity" />
<activity android:name="LineChartActivityColored" />
<activity android:name="DynamicalAddingActivity" />
<activity android:name=".realm.RealmDatabaseActivityLine" />
<activity android:name=".realm.RealmDatabaseActivityBar" />
<activity android:name=".realm.RealmDatabaseActivityHorizontalBar" />
<activity android:name=".realm.RealmDatabaseActivityScatter" />
<activity android:name=".realm.RealmDatabaseActivityCandle" />
<activity android:name=".realm.RealmDatabaseActivityBubble" />
<activity android:name=".realm.RealmDatabaseActivityPie" />
<activity android:name=".realm.RealmDatabaseActivityRadar" />
<activity android:name=".realm.RealmMainActivity" />
<activity android:name="RealtimeLineChartActivity" />
<activity android:name="CombinedChartActivity" />
<activity android:name="PerformanceLineChart" />
<activity android:name="BarChartActivitySinus" />
<activity android:name="ScrollViewActivity" />
<activity android:name="StackedBarActivityNegative" />
<activity android:name=".realm.RealmWikiExample" />
<activity android:name=".BarChartPositiveNegative" />
<activity android:name=".FilledLineActivity" />
<activity android:name=".HalfPieChartActivity" />
</application>
</manifest>

View file

@ -2,14 +2,14 @@ apply plugin: 'com.android.application'
apply plugin: 'realm-android'
android {
compileSdkVersion 27
buildToolsVersion '27.0.3'
compileSdkVersion 28
defaultConfig {
applicationId "com.xxmassdeveloper.mpchartexample"
minSdkVersion 16
targetSdkVersion 27
targetSdkVersion 28
versionCode 56
versionName '3.0.3'
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
sourceSets {
main {
@ -33,17 +33,14 @@ android {
}
}
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
//classpath 'io.realm:realm-gradle-plugin:0.88.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
dependencies {
implementation "androidx.appcompat:appcompat:1.0.0"
implementation 'com.google.android.material:material:1.0.0'
//implementation "androidx.legacy:legacy-support-v4:1.0.0"
//implementation "androidx.legacy:legacy-support-v13:1.0.0"
//compile project(':MPChartLib-Realm') // clone "https://github.com/PhilJay/MPAndroidChart-Realm" to get this or uncomment the gradle dependency below:
implementation 'com.github.PhilJay:MPAndroidChart-Realm:v2.0.2@aar'
implementation project(':MPChartLib')
}
repositories {
@ -52,14 +49,3 @@ repositories {
url 'http://oss.jfrog.org/artifactory/oss-snapshot-local'
}
}
dependencies {
//compile fileTree(dir: 'libs', include: ['*.jar'])
//compile project(':MPChartLib-Realm') // clone "https://github.com/PhilJay/MPAndroidChart-Realm" to get this or uncomment the gradle dependency below:
implementation 'com.github.PhilJay:MPAndroidChart-Realm:v2.0.2@aar'
implementation project(':MPChartLib')
implementation 'com.android.support:appcompat-v7:27.1.1'
//compile 'io.realm:realm-android:0.87.5' // dependency for realm-database API (http://realm.io)
//compile 'com.github.PhilJay:MPAndroidChart:v2.2.5'
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_height="match_parent">
<com.github.mikephil.charting.charts.HorizontalBarChart
android:id="@+id/chart1"

View file

@ -2,9 +2,9 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:orientation="vertical">
<androidx.viewpager.widget.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"

View file

@ -1,14 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_height="match_parent">
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/chart1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/seekBar1" />
<SeekBar
android:id="@+id/seekBar2"
android:layout_width="match_parent"
@ -39,7 +39,7 @@
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/seekBar1"
android:layout_alignParentRight="true"
android:text="500"
android:text="@string/dash"
android:layout_marginBottom="15dp"
android:layout_marginRight="10dp"
android:gravity="right"
@ -51,7 +51,7 @@
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/seekBar2"
android:layout_alignParentRight="true"
android:text="500"
android:text="@string/dash"
android:layout_marginBottom="15dp"
android:layout_marginRight="10dp"
android:gravity="right"

View file

@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_height="match_parent">
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/chart1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</RelativeLayout>

View file

@ -28,7 +28,7 @@
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/seekbarValues"
android:layout_alignParentRight="true"
android:text="0"
android:text="@string/dash"
android:layout_marginBottom="15dp"
android:layout_marginRight="10dp"
android:gravity="right"

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_height="match_parent">
<com.github.mikephil.charting.charts.BubbleChart
android:id="@+id/chart1"
@ -9,7 +9,7 @@
android:layout_height="match_parent"
android:background="@android:color/white"
android:layout_above="@+id/seekBar1" />
<SeekBar
android:id="@+id/seekBar2"
android:layout_width="match_parent"
@ -40,7 +40,7 @@
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/seekBar1"
android:layout_alignParentRight="true"
android:text="500"
android:text="@string/dash"
android:layout_marginBottom="15dp"
android:layout_marginRight="10dp"
android:gravity="right"
@ -52,7 +52,7 @@
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/seekBar2"
android:layout_alignParentRight="true"
android:text="500"
android:text="@string/dash"
android:layout_marginBottom="15dp"
android:layout_marginRight="10dp"
android:gravity="right"

View file

@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_height="match_parent">
<com.github.mikephil.charting.charts.BubbleChart
android:id="@+id/chart1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</RelativeLayout>

View file

@ -1,14 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_height="match_parent">
<com.github.mikephil.charting.charts.CandleStickChart
android:id="@+id/chart1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/seekBar1" />
<SeekBar
android:id="@+id/seekBar2"
android:layout_width="match_parent"
@ -39,7 +39,7 @@
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/seekBar1"
android:layout_alignParentRight="true"
android:text="500"
android:text="@string/dash"
android:layout_marginBottom="15dp"
android:layout_marginRight="10dp"
android:gravity="right"
@ -51,7 +51,7 @@
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/seekBar2"
android:layout_alignParentRight="true"
android:text="500"
android:text="@string/dash"
android:layout_marginBottom="15dp"
android:layout_marginRight="10dp"
android:gravity="right"

View file

@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_height="match_parent">
<com.github.mikephil.charting.charts.CandleStickChart
android:id="@+id/chart1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</RelativeLayout>

View file

@ -2,7 +2,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:orientation="vertical">
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart1"

View file

@ -7,5 +7,5 @@
android:id="@+id/chart1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_height="match_parent">
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart1"

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_height="match_parent">
<com.github.mikephil.charting.charts.HorizontalBarChart
android:id="@+id/chart1"
@ -9,7 +9,7 @@
android:layout_height="match_parent"
android:background="@android:color/white"
android:layout_above="@+id/seekBar1" />
<SeekBar
android:id="@+id/seekBar2"
android:layout_width="match_parent"
@ -40,7 +40,7 @@
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/seekBar1"
android:layout_alignParentRight="true"
android:text="500"
android:text="@string/dash"
android:layout_marginBottom="15dp"
android:layout_marginRight="10dp"
android:gravity="right"
@ -52,7 +52,7 @@
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/seekBar2"
android:layout_alignParentRight="true"
android:text="500"
android:text="@string/dash"
android:layout_marginBottom="15dp"
android:layout_marginRight="10dp"
android:gravity="right"

View file

@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_height="match_parent">
<com.github.mikephil.charting.charts.HorizontalBarChart
android:id="@+id/chart1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</RelativeLayout>

View file

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
@ -8,7 +9,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/seekBar1" />
<SeekBar
android:id="@+id/seekBar2"
android:layout_width="match_parent"
@ -30,7 +31,7 @@
android:layout_marginBottom="35dp"
android:layout_toLeftOf="@+id/tvXMax"
android:layout_marginRight="5dp"
android:max="1500"
android:max="500"
android:paddingBottom="12dp" />
<TextView
@ -39,7 +40,7 @@
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/seekBar1"
android:layout_alignParentRight="true"
android:text="500"
android:text="@string/dash"
android:layout_marginBottom="15dp"
android:layout_marginRight="10dp"
android:gravity="right"
@ -51,7 +52,7 @@
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/seekBar2"
android:layout_alignParentRight="true"
android:text="500"
android:text="@string/dash"
android:layout_marginBottom="15dp"
android:layout_marginRight="10dp"
android:gravity="right"

View file

@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_height="match_parent">
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</RelativeLayout>

View file

@ -25,7 +25,7 @@
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="500"
android:text="@string/dash"
android:layout_marginBottom="15dp"
android:layout_marginRight="10dp"
android:gravity="right"

View file

@ -2,7 +2,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:orientation="vertical">
<ListView
android:id="@+id/listView1"

View file

@ -2,6 +2,7 @@
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
android:layout_height="fill_parent"
android:scrollbarFadeDuration="0">
</ListView>
</ListView>

View file

@ -1,8 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
android:layout_height="match_parent">
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart1"
@ -29,7 +28,7 @@
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/seekbarValues"
android:layout_alignParentRight="true"
android:text="0"
android:text="@string/dash"
android:layout_marginBottom="15dp"
android:layout_marginRight="10dp"
android:gravity="right"

View file

@ -1,14 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_height="match_parent">
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/chart1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/seekBar1" />
<SeekBar
android:id="@+id/seekBar2"
android:layout_width="match_parent"
@ -39,7 +39,7 @@
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/seekBar1"
android:layout_alignParentRight="true"
android:text="500"
android:text="@string/dash"
android:layout_marginBottom="15dp"
android:layout_marginRight="10dp"
android:gravity="right"
@ -51,7 +51,7 @@
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/seekBar2"
android:layout_alignParentRight="true"
android:text="500"
android:text="@string/dash"
android:layout_marginBottom="15dp"
android:layout_marginRight="10dp"
android:gravity="right"

View file

@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_height="match_parent">
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/chart1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</RelativeLayout>

View file

@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_height="match_parent">
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/chart1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</RelativeLayout>

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_height="match_parent">
<com.github.mikephil.charting.charts.RadarChart
android:id="@+id/chart1"

View file

@ -1,22 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.6"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="YOUR PREFERENCES"
android:id="@+id/textView"/>
<com.github.mikephil.charting.charts.RadarChart
android:id="@+id/chart1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2" />
</LinearLayout>

View file

@ -2,7 +2,6 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical">
<com.github.mikephil.charting.charts.LineChart
@ -17,4 +16,4 @@
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>

View file

@ -7,5 +7,5 @@
android:id="@+id/chart1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>

View file

@ -1,14 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_height="match_parent">
<com.github.mikephil.charting.charts.ScatterChart
android:id="@+id/chart1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/seekBar1" />
<SeekBar
android:id="@+id/seekBar2"
android:layout_width="match_parent"
@ -39,7 +39,7 @@
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/seekBar1"
android:layout_alignParentRight="true"
android:text="500"
android:text="@string/dash"
android:layout_marginBottom="15dp"
android:layout_marginRight="10dp"
android:gravity="right"
@ -51,7 +51,7 @@
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/seekBar2"
android:layout_alignParentRight="true"
android:text="500"
android:text="@string/dash"
android:layout_marginBottom="15dp"
android:layout_marginRight="10dp"
android:gravity="right"

View file

@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_height="match_parent">
<com.github.mikephil.charting.charts.ScatterChart
android:id="@+id/chart1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</RelativeLayout>

View file

@ -1,18 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="center"
android:text="START OF SCROLLVIEW" />
android:text="@string/scrollViewStart" />
<!-- THIS IS JUST A PLACEHOLDER -->
@ -30,13 +30,13 @@
<View
android:layout_width="match_parent"
android:layout_height="700dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="center"
android:text="END OF SCROLLVIEW" />
android:text="@string/scrollViewEnd" />
</LinearLayout>
</ScrollView>
</ScrollView>

View file

@ -1,8 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:background="@drawable/marker2" >
android:background="@drawable/marker2"
tools:ignore="Overdraw">
<TextView
android:id="@+id/tvContent"
@ -13,7 +15,7 @@
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text=""
android:textSize="12dp"
android:textSize="12sp"
android:textColor="@android:color/white"
android:ellipsize="end"
android:singleLine="true"

View file

@ -5,10 +5,6 @@
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- <com.github.mikephil.charting.charts.BarChart
android:id="@+id/barChart1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> -->
<!-- We add the chart programmatically here. See BarChartFrag.java -->
</FrameLayout>

View file

@ -12,7 +12,7 @@
android:layout_alignParentTop="true"
android:layout_marginLeft="4dp"
android:text="Medium Text"
android:textSize="16dp"/>
android:textSize="16sp"/>
<TextView
android:id="@+id/tvDesc"
@ -22,7 +22,7 @@
android:layout_below="@+id/tvName"
android:layout_marginTop="3dp"
android:text="Small Text"
android:textSize="12dp"
android:textSize="12sp"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@+id/tvNew"
android:layout_toStartOf="@+id/tvNew" />
@ -32,11 +32,11 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="NEW"
android:text="@string/textNew"
android:background="@drawable/new_background"
android:textColor="@android:color/white"
android:id="@+id/tvNew"
android:textSize="11dp"
android:textSize="12sp"
android:layout_marginRight="5dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"

View file

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Large Text"
android:textSize="21sp" />
<TextView
android:id="@+id/tvDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvName"
android:layout_centerHorizontal="true"
android:text="Small Text"
android:textSize="12sp"
android:visibility="gone" />
<TextView
android:padding="7dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/textNew"
android:background="@drawable/new_background"
android:textColor="@android:color/white"
android:id="@+id/tvNew"
android:textSize="12sp"
android:layout_marginRight="5dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:visibility="gone" />
</RelativeLayout>

View file

@ -1,8 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="55dp"
android:layout_height="40dp"
android:background="@drawable/radar_marker" >
android:background="@drawable/radar_marker"
tools:ignore="Overdraw">
<TextView
android:id="@+id/tvContent"
@ -14,10 +16,11 @@
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text=""
android:textSize="10dp"
android:textSize="10sp"
android:textColor="@android:color/white"
android:ellipsize="end"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceSmall" />
android:textAppearance="?android:attr/textAppearanceSmall"
tools:ignore="SmallSp" />
</RelativeLayout>

View file

@ -2,48 +2,48 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/actionToggleValues"
android:title="Toggle Values">
</item>
<item
android:id="@+id/actionToggleIcons"
android:title="Toggle Icons">
</item>
<item
android:id="@+id/actionToggleHighlight"
android:title="Toggle Highlight">
</item>
<item
android:id="@+id/animateX"
android:title="Animate X">
</item>
<item
android:id="@+id/animateY"
android:title="Animate Y">
</item>
<item
android:id="@+id/animateXY"
android:title="Animate XY">
</item>
<item
android:id="@+id/actionToggleAdjustXLegend"
android:title="Toggle AdjustXLegend">
</item>
<item
android:id="@+id/actionSave"
android:title="Save to Gallery">
</item>
<item
android:id="@+id/actionTogglePinch"
android:title="Toggle PinchZoom">
</item>
<item
android:id="@+id/actionToggleAutoScaleMinMax"
android:title="Toggle auto scale min/max">
android:id="@+id/viewGithub"
android:title="@string/viewGithub">
</item>
<item
android:id="@+id/actionToggleBarBorders"
android:title="Show Bar Borders">
android:title="@string/actionToggleBarBorders">
</item>
<item
android:id="@+id/actionToggleValues"
android:title="@string/actionToggleValues">
</item>
<item
android:id="@+id/actionToggleIcons"
android:title="@string/actionToggleIcons">
</item>
<item
android:id="@+id/actionToggleHighlight"
android:title="@string/actionToggleHighlight">
</item>
<item
android:id="@+id/actionTogglePinch"
android:title="@string/actionTogglePinch">
</item>
<item
android:id="@+id/actionToggleAutoScaleMinMax"
android:title="@string/actionToggleAutoScale">
</item>
<item
android:id="@+id/animateX"
android:title="@string/animateX">
</item>
<item
android:id="@+id/animateY"
android:title="@string/animateY">
</item>
<item
android:id="@+id/animateXY"
android:title="@string/animateXY">
</item>
<item
android:id="@+id/actionSave"
android:title="@string/actionSave">
</item>
</menu>
</menu>

View file

@ -1,45 +1,45 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/viewGithub"
android:title="@string/viewGithub">
</item>
<item
android:id="@+id/actionToggleValues"
android:title="Toggle Values">
android:title="@string/actionToggleValues">
</item>
<item
android:id="@+id/actionToggleIcons"
android:title="Toggle Icons">
android:title="@string/actionToggleIcons">
</item>
<item
android:id="@+id/actionToggleHighlight"
android:title="Toggle Highlight">
</item>
<item
android:id="@+id/actionToggleAdjustXLegend"
android:title="Toggle AdjustXLegend">
</item>
<item
android:id="@+id/animateX"
android:title="Animate X">
</item>
<item
android:id="@+id/animateY"
android:title="Animate Y">
</item>
<item
android:id="@+id/animateXY"
android:title="Animate XY">
</item>
<item
android:id="@+id/actionSave"
android:title="Save to Gallery">
android:title="@string/actionToggleHighlight">
</item>
<item
android:id="@+id/actionTogglePinch"
android:title="Toggle PinchZoom">
android:title="@string/actionTogglePinch">
</item>
<item
android:id="@+id/actionToggleAutoScaleMinMax"
android:title="Toggle auto scale min/max">
android:title="@string/actionToggleAutoScale">
</item>
<item
android:id="@+id/animateX"
android:title="@string/animateX">
</item>
<item
android:id="@+id/animateY"
android:title="@string/animateY">
</item>
<item
android:id="@+id/animateXY"
android:title="@string/animateXY">
</item>
<item
android:id="@+id/actionSave"
android:title="@string/actionSave">
</item>
</menu>
</menu>

View file

@ -1,41 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/viewGithub"
android:title="@string/viewGithub">
</item>
<item
android:id="@+id/actionToggleValues"
android:title="@string/actionToggleValues">
</item>
<item
android:id="@+id/actionToggleIcons"
android:title="@string/actionToggleIcons">
</item>
<item
android:id="@+id/actionToggleHighlight"
android:title="Toggle Highlight">
</item>
<item
android:id="@+id/animateX"
android:title="Animate X">
</item>
<item
android:id="@+id/animateY"
android:title="Animate Y">
</item>
<item
android:id="@+id/animateXY"
android:title="Animate XY">
</item>
<item
android:id="@+id/actionToggleAdjustXLegend"
android:title="Toggle AdjustXLegend">
</item>
<item
android:id="@+id/actionSave"
android:title="Save to SD-Card">
</item>
<item
android:id="@+id/actionTogglePinch"
android:title="Toggle PinchZoom">
</item>
<item
android:id="@+id/actionToggleAutoScaleMinMax"
android:title="Toggle auto scale min/max">
android:title="@string/actionToggleHighlight">
</item>
<item
android:id="@+id/actionToggleMakeShadowSameColorAsCandle"
android:title="Toggle shadow same color">
android:title="@string/actionToggleCandleShadow">
</item>
<item
android:id="@+id/actionTogglePinch"
android:title="@string/actionTogglePinch">
</item>
<item
android:id="@+id/actionToggleAutoScaleMinMax"
android:title="@string/actionToggleAutoScale">
</item>
<item
android:id="@+id/animateX"
android:title="@string/animateX">
</item>
<item
android:id="@+id/animateY"
android:title="@string/animateY">
</item>
<item
android:id="@+id/animateXY"
android:title="@string/animateXY">
</item>
<item
android:id="@+id/actionSave"
android:title="@string/actionSave">
</item>
</menu>
</menu>

View file

@ -1,16 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/viewGithub"
android:title="@string/viewGithub">
</item>
<item
android:id="@+id/actionToggleLineValues"
android:title="Toggle line Values">
android:title="@string/actionToggleLineValues">
</item>
<item
android:id="@+id/actionToggleBarValues"
android:title="Toggle bar Values">
android:title="@string/actionToggleBarValues">
</item>
<item
android:id="@+id/actionRemoveDataSet"
android:title="Remove random set">
android:title="@string/actionRemoveDataSet">
</item>
</menu>
</menu>

View file

@ -3,34 +3,30 @@
<item
android:id="@+id/actionToggleValues"
android:title="Toggle Values">
android:title="@string/actionToggleValues">
</item>
<item
android:id="@+id/actionToggleFilled"
android:title="Toggle Filled">
android:title="@string/actionToggleFilled">
</item>
<item
android:id="@+id/actionToggleCircles"
android:title="Toggle Circles">
android:title="@string/actionToggleCircles">
</item>
<item
android:id="@+id/actionToggleHighlight"
android:title="Toggle Highlight">
</item>
<item
android:id="@+id/actionToggleAdjustXLegend"
android:title="Toggle AdjustXLegend">
</item>
<item
android:id="@+id/actionSave"
android:title="Save to Gallery">
android:title="@string/actionToggleHighlight">
</item>
<item
android:id="@+id/actionTogglePinch"
android:title="Toggle PinchZoom">
android:title="@string/actionTogglePinch">
</item>
<item
android:id="@+id/actionToggleAutoScaleMinMax"
android:title="Toggle auto scale min/max">
android:title="@string/actionToggleAutoScale">
</item>
</menu>
<item
android:id="@+id/actionSave"
android:title="@string/actionSave">
</item>
</menu>

View file

@ -2,32 +2,32 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/actionAddEntry"
android:title="Add Entry">
android:id="@+id/viewGithub"
android:title="@string/viewGithub">
</item>
<item
android:id="@+id/actionAddEntry"
android:title="@string/actionAddEntry">
</item>
<item
android:id="@+id/actionRemoveEntry"
android:title="Remove Entry">
android:title="@string/actionRemoveEntry">
</item>
<item
<item
android:id="@+id/actionAddDataSet"
android:title="Add DataSet">
android:title="@string/actionAddDataSet">
</item>
<item
android:id="@+id/actionRemoveDataSet"
android:title="Remove DataSet">
android:title="@string/actionRemoveDataSet">
</item>
<item
android:id="@+id/actionAddEmptyLineData"
android:title="Add empty LineData">
</item>
<item
android:id="@+id/actionClear"
android:title="Clear chart">
android:title="@string/actionClearChart">
</item>
</menu>
<item
android:id="@+id/actionSave"
android:title="@string/actionSave">
</item>
</menu>

View file

@ -1,64 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/viewGithub"
android:title="@string/viewGithub">
</item>
<item
android:id="@+id/actionToggleValues"
android:title="Toggle Values">
android:title="@string/actionToggleValues">
</item>
<item
android:id="@+id/actionToggleIcons"
android:title="Toggle Icons">
android:title="@string/actionToggleIcons">
</item>
<item
android:id="@+id/actionToggleFilled"
android:title="Toggle Filled">
android:title="@string/actionToggleFilled">
</item>
<item
android:id="@+id/actionToggleCircles"
android:title="Toggle Circles">
android:title="@string/actionToggleCircles">
</item>
<item
android:id="@+id/actionToggleCubic"
android:title="Toggle Cubic">
android:title="@string/actionToggleCubic">
</item>
<item
android:id="@+id/actionToggleStepped"
android:title="Toggle Stepped">
android:title="@string/actionToggleStepped">
</item>
<item
android:id="@+id/actionToggleHorizontalCubic"
android:title="Toggle Horizontal Cubic">
</item>
<item
android:id="@+id/actionToggleHighlight"
android:title="Toggle Highlight">
</item>
<item
android:id="@+id/animateX"
android:title="Animate X">
</item>
<item
android:id="@+id/animateY"
android:title="Animate Y">
</item>
<item
android:id="@+id/animateXY"
android:title="Animate XY">
</item>
<item
android:id="@+id/actionToggleAdjustXLegend"
android:title="Toggle AdjustXLegend">
</item>
<item
android:id="@+id/actionSave"
android:title="Save to SD-Card">
android:title="@string/actionToggleHorizontalCubic">
</item>
<item
android:id="@+id/actionTogglePinch"
android:title="Toggle PinchZoom">
android:title="@string/actionTogglePinch">
</item>
<item
android:id="@+id/actionToggleAutoScaleMinMax"
android:title="Toggle auto scale min/max">
android:title="@string/actionToggleAutoScale">
</item>
</menu>
<item
android:id="@+id/actionToggleHighlight"
android:title="@string/actionToggleHighlight">
</item>
<item
android:id="@+id/animateX"
android:title="@string/animateX">
</item>
<item
android:id="@+id/animateY"
android:title="@string/animateY">
</item>
<item
android:id="@+id/animateXY"
android:title="@string/animateXY">
</item>
<item
android:id="@+id/actionSave"
android:title="@string/actionSave">
</item>
</menu>

View file

@ -3,19 +3,15 @@
<item
android:id="@+id/viewGithub"
android:title="View on GitHub">
android:title="@string/viewGithub">
</item>
<item
android:id="@+id/report"
android:title="Problem Report">
</item>
<item
android:id="@+id/blog"
android:title="Developer Blog">
android:title="@string/reportProblem">
</item>
<item
android:id="@+id/website"
android:title="Developer Website">
android:title="@string/viewWebsite">
</item>
</menu>
</menu>

View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/viewGithub"
android:title="@string/viewGithub">
</item>
</menu>

View file

@ -1,49 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/viewGithub"
android:title="@string/viewGithub">
</item>
<item
android:id="@+id/actionToggleValues"
android:title="Toggle Y-Values">
android:title="@string/actionToggleYValues">
</item>
<item
android:id="@+id/actionToggleXValues"
android:title="@string/actionToggleXValues">
</item>
<item
android:id="@+id/actionToggleIcons"
android:title="Toggle Icons">
</item>
<item
android:id="@+id/actionToggleXVals"
android:title="Toggle X-Values">
android:title="@string/actionToggleIcons">
</item>
<item
android:id="@+id/actionTogglePercent"
android:title="Toggle Percent">
android:title="@string/actionTogglePercent">
</item>
<item
android:id="@+id/actionToggleHole"
android:title="Toggle Hole">
</item>
<item
android:id="@+id/animateX"
android:title="Animate X">
</item>
<item
android:id="@+id/animateY"
android:title="Animate Y">
</item>
<item
android:id="@+id/animateXY"
android:title="Animate XY">
android:title="@string/actionToggleHole">
</item>
<item
android:id="@+id/actionDrawCenter"
android:title="Draw CenterText">
</item>
<item
android:id="@+id/actionSave"
android:title="Save to Gallery">
android:title="@string/actionDrawCenterText">
</item>
<item
android:id="@+id/actionToggleSpin"
android:title="Spin Animation">
android:title="@string/actionToggleSpin">
</item>
<item
android:id="@+id/animateX"
android:title="@string/animateX">
</item>
<item
android:id="@+id/animateY"
android:title="@string/animateY">
</item>
<item
android:id="@+id/animateXY"
android:title="@string/animateXY">
</item>
<item
android:id="@+id/actionSave"
android:title="@string/actionSave">
</item>
</menu>
</menu>

View file

@ -1,57 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/viewGithub"
android:title="@string/viewGithub">
</item>
<item
android:id="@+id/actionToggleValues"
android:title="Toggle Values">
android:title="@string/actionToggleValues">
</item>
<item
android:id="@+id/actionToggleIcons"
android:title="Toggle Icons">
android:title="@string/actionToggleIcons">
</item>
<item
android:id="@+id/actionToggleFilled"
android:title="Toggle Filled">
android:title="@string/actionToggleFilled">
</item>
<item
android:id="@+id/actionToggleHighlight"
android:title="Toggle Highlight">
android:title="@string/actionToggleHighlight">
</item>
<item
android:id="@+id/actionToggleHighlightCircle"
android:title="Toggle highlight circle">
</item>
<item
android:id="@+id/animateX"
android:title="Animate X">
</item>
<item
android:id="@+id/animateY"
android:title="Animate Y">
</item>
<item
android:id="@+id/animateXY"
android:title="Animate XY">
</item>
<item
android:id="@+id/actionSave"
android:title="Save to SD">
android:title="@string/actionToggleHighlightCircle">
</item>
<item
android:id="@+id/actionToggleRotate"
android:title="Toggle Rotation">
android:title="@string/actionToggleRotation">
</item>
<item
android:id="@+id/actionToggleYLabels"
android:title="Toggle Y-Labels">
android:title="@string/actionToggleYValues">
</item>
<item
android:id="@+id/actionToggleXLabels"
android:title="Toggle X-Labels">
android:title="@string/actionToggleXValues">
</item>
<item
android:id="@+id/actionToggleSpin"
android:title="Spin Animation">
android:title="@string/actionToggleSpin">
</item>
<item
android:id="@+id/animateX"
android:title="@string/animateX">
</item>
<item
android:id="@+id/animateY"
android:title="@string/animateY">
</item>
<item
android:id="@+id/animateXY"
android:title="@string/animateXY">
</item>
<item
android:id="@+id/actionSave"
android:title="@string/actionSave">
</item>
</menu>
</menu>

View file

@ -2,7 +2,12 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/viewRealm"
android:title="realm.io website">
android:id="@+id/viewGithub"
android:title="@string/viewGithub">
</item>
</menu>
<item
android:id="@+id/viewRealm"
android:title="@string/realmIOWebsite">
</item>
</menu>

View file

@ -1,16 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/viewGithub"
android:title="@string/viewGithub">
</item>
<item
android:id="@+id/actionAdd"
android:title="Feed new Entry">
android:title="@string/actionAddEntry">
</item>
<item
android:id="@+id/actionClear"
android:title="Clear Values">
android:title="@string/actionClearChart">
</item>
<item
android:id="@+id/actionFeedMultiple"
android:title="Fast feed multiple">
android:title="@string/actionFeedMultiple">
</item>
</menu>
<item
android:id="@+id/actionSave"
android:title="@string/actionSave">
</item>
</menu>

View file

@ -1,45 +1,45 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/viewGithub"
android:title="@string/viewGithub">
</item>
<item
android:id="@+id/actionToggleValues"
android:title="Toggle Values">
android:title="@string/actionToggleValues">
</item>
<item
android:id="@+id/actionToggleIcons"
android:title="Toggle Icons">
android:title="@string/actionToggleIcons">
</item>
<item
android:id="@+id/actionToggleHighlight"
android:title="Toggle Highlight">
</item>
<item
android:id="@+id/actionToggleAdjustXLegend"
android:title="Toggle AdjustXLegend">
android:title="@string/actionToggleHighlight">
</item>
<item
android:id="@+id/animateX"
android:title="Animate X">
android:title="@string/animateX">
</item>
<item
android:id="@+id/animateY"
android:title="Animate Y">
android:title="@string/animateY">
</item>
<item
android:id="@+id/animateXY"
android:title="Animate XY">
</item>
<item
android:id="@+id/actionSave"
android:title="Save to Gallery">
android:title="@string/animateXY">
</item>
<item
android:id="@+id/actionTogglePinch"
android:title="Toggle PinchZoom">
android:title="@string/actionTogglePinch">
</item>
<item
android:id="@+id/actionToggleAutoScaleMinMax"
android:title="Toggle auto scale min/max">
android:title="@string/actionToggleAutoScale">
</item>
<item
android:id="@+id/actionSave"
android:title="@string/actionSave">
</item>
</menu>
</menu>

View file

@ -1,8 +0,0 @@
<resources>
<!--
Customize dimensions originally defined in res/values/dimens.xml (such as
screen margins) for sw600dp devices (e.g. 7" tablets) here.
-->
</resources>

View file

@ -1,9 +0,0 @@
<resources>
<!--
Customize dimensions originally defined in res/values/dimens.xml (such as
screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here.
-->
<dimen name="activity_horizontal_margin">128dp</dimen>
</resources>

View file

@ -1,11 +0,0 @@
<resources>
<!--
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
<!-- API 11 theme customizations can go here. -->
</style>
</resources>

View file

@ -1,12 +0,0 @@
<resources>
<!--
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- API 14 theme customizations can go here. -->
</style>
</resources>

View file

@ -1,7 +0,0 @@
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>

View file

@ -2,7 +2,54 @@
<resources>
<string name="app_name">MPAndroidChart Example</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="viewGithub">View on GitHub</string>
<string name="reportProblem">Problem Report</string>
<string name="viewWebsite">Developer Website</string>
<string name="actionSave">Save to Gallery</string>
<string name="realmIOWebsite">realm.io website</string>
<string name="animateX">Animate X</string>
<string name="animateY">Animate Y</string>
<string name="animateXY">Animate XY</string>
<string name="actionToggleValues">Toggle Values</string>
<string name="actionToggleYValues">Toggle Y-Values</string>
<string name="actionToggleXValues">Toggle X-Values</string>
<string name="actionToggleIcons">Toggle Icons</string>
<string name="actionToggleHighlight">Toggle Highlight</string>
<string name="actionTogglePinch">Toggle PinchZoom</string>
<string name="actionToggleAutoScale">Toggle Auto Scale</string>
<string name="actionToggleLineValues">Toggle Line Values</string>
<string name="actionToggleBarValues">Toggle Bar Values</string>
<string name="actionToggleBarBorders">Toggle Bar Borders</string>
<string name="actionToggleFilled">Toggle Filled</string>
<string name="actionToggleCircles">Toggle Circles</string>
<string name="actionToggleCandleShadow">Toggle Shadow Color</string>
<string name="actionToggleCubic">Toggle Cubic</string>
<string name="actionToggleStepped">Toggle Stepped</string>
<string name="actionToggleHorizontalCubic">Toggle Horizontal Cubic</string>
<string name="actionAddEntry">Add Entry</string>
<string name="actionFeedMultiple">Add Multiple</string>
<string name="actionRemoveEntry">Remove Entry</string>
<string name="actionAddDataSet">Add Data Set</string>
<string name="actionRemoveDataSet">Remove Data Set</string>
<string name="actionClearChart">Clear chart</string>
<string name="actionTogglePercent">Toggle Percent</string>
<string name="actionToggleHole">Toggle Hole</string>
<string name="actionDrawCenterText">Draw Center Text</string>
<string name="actionToggleHighlightCircle">Toggle Highlight Circle</string>
<string name="actionToggleRotation">Toggle Rotation</string>
<string name="actionToggleSpin">Spin Animation</string>
<string name="dash">-</string>
<string name="scrollViewStart">START OF SCROLLVIEW</string>
<string name="scrollViewEnd">END OF SCROLLVIEW</string>
<string name="textNew">NEW</string>
</resources>

View file

@ -1,19 +1,7 @@
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

View file

@ -1,14 +1,18 @@
package com.xxmassdeveloper.mpchartexample;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.XAxis;
@ -25,8 +29,8 @@ import java.util.ArrayList;
public class AnotherBarActivity extends DemoBase implements OnSeekBarChangeListener {
private BarChart mChart;
private SeekBar mSeekBarX, mSeekBarY;
private BarChart chart;
private SeekBar seekBarX, seekBarY;
private TextView tvX, tvY;
@Override
@ -36,48 +40,89 @@ public class AnotherBarActivity extends DemoBase implements OnSeekBarChangeListe
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_barchart);
setTitle("AnotherBarActivity");
tvX = findViewById(R.id.tvXMax);
tvY = findViewById(R.id.tvYMax);
mSeekBarX = findViewById(R.id.seekBar1);
mSeekBarX.setOnSeekBarChangeListener(this);
seekBarX = findViewById(R.id.seekBar1);
seekBarX.setOnSeekBarChangeListener(this);
mSeekBarY = findViewById(R.id.seekBar2);
mSeekBarY.setOnSeekBarChangeListener(this);
seekBarY = findViewById(R.id.seekBar2);
seekBarY.setOnSeekBarChangeListener(this);
mChart = findViewById(R.id.chart1);
chart = findViewById(R.id.chart1);
mChart.getDescription().setEnabled(false);
chart.getDescription().setEnabled(false);
// if more than 60 entries are displayed in the chart, no values will be
// drawn
mChart.setMaxVisibleValueCount(60);
chart.setMaxVisibleValueCount(60);
// scaling can now only be done on x- and y-axis separately
mChart.setPinchZoom(false);
chart.setPinchZoom(false);
mChart.setDrawBarShadow(false);
mChart.setDrawGridBackground(false);
chart.setDrawBarShadow(false);
chart.setDrawGridBackground(false);
XAxis xAxis = mChart.getXAxis();
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(false);
mChart.getAxisLeft().setDrawGridLines(false);
chart.getAxisLeft().setDrawGridLines(false);
// setting data
mSeekBarX.setProgress(10);
mSeekBarY.setProgress(100);
seekBarX.setProgress(10);
seekBarY.setProgress(100);
// add a nice and smooth animation
mChart.animateY(2500);
mChart.getLegend().setEnabled(false);
chart.animateY(1500);
chart.getLegend().setEnabled(false);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvX.setText(String.valueOf(seekBarX.getProgress()));
tvY.setText(String.valueOf(seekBarY.getProgress()));
ArrayList<BarEntry> values = new ArrayList<>();
for (int i = 0; i < seekBarX.getProgress(); i++) {
float multi = (seekBarY.getProgress() + 1);
float val = (float) (Math.random() * multi) + multi / 3;
values.add(new BarEntry(i, val));
}
BarDataSet set1;
if (chart.getData() != null &&
chart.getData().getDataSetCount() > 0) {
set1 = (BarDataSet) chart.getData().getDataSetByIndex(0);
set1.setValues(values);
chart.getData().notifyDataChanged();
chart.notifyDataSetChanged();
} else {
set1 = new BarDataSet(values, "Data Set");
set1.setColors(ColorTemplate.VORDIPLOM_COLORS);
set1.setDrawValues(false);
ArrayList<IBarDataSet> dataSets = new ArrayList<>();
dataSets.add(set1);
BarData data = new BarData(dataSets);
chart.setData(data);
chart.setFitBars(true);
}
chart.invalidate();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.bar, menu);
menu.removeItem(R.id.actionToggleIcons);
return true;
}
@ -85,63 +130,71 @@ public class AnotherBarActivity extends DemoBase implements OnSeekBarChangeListe
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.actionToggleValues: {
for (IDataSet set : mChart.getData().getDataSets())
set.setDrawValues(!set.isDrawValuesEnabled());
mChart.invalidate();
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/AnotherBarActivity.java"));
startActivity(i);
break;
}
case R.id.actionToggleValues: {
for (IDataSet set : chart.getData().getDataSets())
set.setDrawValues(!set.isDrawValuesEnabled());
chart.invalidate();
break;
}
/*
case R.id.actionToggleIcons: { break; }
*/
case R.id.actionToggleHighlight: {
if(mChart.getData() != null) {
mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled());
mChart.invalidate();
if(chart.getData() != null) {
chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled());
chart.invalidate();
}
break;
}
case R.id.actionTogglePinch: {
if (mChart.isPinchZoomEnabled())
mChart.setPinchZoom(false);
if (chart.isPinchZoomEnabled())
chart.setPinchZoom(false);
else
mChart.setPinchZoom(true);
chart.setPinchZoom(true);
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled());
chart.notifyDataSetChanged();
break;
}
case R.id.actionToggleBarBorders: {
for (IBarDataSet set : mChart.getData().getDataSets())
for (IBarDataSet set : chart.getData().getDataSets())
((BarDataSet)set).setBarBorderWidth(set.getBarBorderWidth() == 1.f ? 0.f : 1.f);
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.animateX: {
mChart.animateX(3000);
chart.animateX(2000);
break;
}
case R.id.animateY: {
mChart.animateY(3000);
chart.animateY(2000);
break;
}
case R.id.animateXY: {
mChart.animateXY(3000, 3000);
chart.animateXY(2000, 2000);
break;
}
case R.id.actionSave: {
if (mChart.saveToGallery("title" + System.currentTimeMillis(), 50)) {
Toast.makeText(getApplicationContext(), "Saving SUCCESSFUL!",
Toast.LENGTH_SHORT).show();
} else
Toast.makeText(getApplicationContext(), "Saving FAILED!", Toast.LENGTH_SHORT)
.show();
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveToGallery();
} else {
requestStoragePermission(chart);
}
break;
}
}
@ -149,52 +202,13 @@ public class AnotherBarActivity extends DemoBase implements OnSeekBarChangeListe
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvX.setText("" + (mSeekBarX.getProgress() + 1));
tvY.setText("" + (mSeekBarY.getProgress()));
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
for (int i = 0; i < mSeekBarX.getProgress() + 1; i++) {
float mult = (mSeekBarY.getProgress() + 1);
float val = (float) (Math.random() * mult) + mult / 3;
yVals1.add(new BarEntry(i, val));
}
BarDataSet set1;
if (mChart.getData() != null &&
mChart.getData().getDataSetCount() > 0) {
set1 = (BarDataSet)mChart.getData().getDataSetByIndex(0);
set1.setValues(yVals1);
mChart.getData().notifyDataChanged();
mChart.notifyDataSetChanged();
} else {
set1 = new BarDataSet(yVals1, "Data Set");
set1.setColors(ColorTemplate.VORDIPLOM_COLORS);
set1.setDrawValues(false);
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
dataSets.add(set1);
BarData data = new BarData(dataSets);
mChart.setData(data);
mChart.setFitBars(true);
}
mChart.invalidate();
public void saveToGallery() {
saveToGallery(chart, "AnotherBarActivity");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {}
}

View file

@ -1,10 +1,13 @@
package com.xxmassdeveloper.mpchartexample;
import android.annotation.SuppressLint;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.RectF;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import androidx.core.content.ContextCompat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
@ -12,7 +15,6 @@ import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
@ -32,7 +34,6 @@ import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.model.GradientColor;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.github.mikephil.charting.utils.MPPointF;
import com.xxmassdeveloper.mpchartexample.custom.DayAxisValueFormatter;
import com.xxmassdeveloper.mpchartexample.custom.MyAxisValueFormatter;
@ -45,8 +46,8 @@ import java.util.List;
public class BarChartActivity extends DemoBase implements OnSeekBarChangeListener,
OnChartValueSelectedListener {
protected BarChart mChart;
private SeekBar mSeekBarX, mSeekBarY;
private BarChart chart;
private SeekBar seekBarX, seekBarY;
private TextView tvX, tvY;
@Override
@ -56,35 +57,40 @@ public class BarChartActivity extends DemoBase implements OnSeekBarChangeListene
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_barchart);
setTitle("BarChartActivity");
tvX = findViewById(R.id.tvXMax);
tvY = findViewById(R.id.tvYMax);
mSeekBarX = findViewById(R.id.seekBar1);
mSeekBarY = findViewById(R.id.seekBar2);
seekBarX = findViewById(R.id.seekBar1);
seekBarY = findViewById(R.id.seekBar2);
mChart = findViewById(R.id.chart1);
mChart.setOnChartValueSelectedListener(this);
seekBarY.setOnSeekBarChangeListener(this);
seekBarX.setOnSeekBarChangeListener(this);
mChart.setDrawBarShadow(false);
mChart.setDrawValueAboveBar(true);
chart = findViewById(R.id.chart1);
chart.setOnChartValueSelectedListener(this);
mChart.getDescription().setEnabled(false);
chart.setDrawBarShadow(false);
chart.setDrawValueAboveBar(true);
chart.getDescription().setEnabled(false);
// if more than 60 entries are displayed in the chart, no values will be
// drawn
mChart.setMaxVisibleValueCount(60);
chart.setMaxVisibleValueCount(60);
// scaling can now only be done on x- and y-axis separately
mChart.setPinchZoom(false);
chart.setPinchZoom(false);
mChart.setDrawGridBackground(false);
// mChart.setDrawYLabels(false);
chart.setDrawGridBackground(false);
// chart.setDrawYLabels(false);
IAxisValueFormatter xAxisFormatter = new DayAxisValueFormatter(mChart);
IAxisValueFormatter xAxisFormatter = new DayAxisValueFormatter(chart);
XAxis xAxis = mChart.getXAxis();
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxisPosition.BOTTOM);
xAxis.setTypeface(mTfLight);
xAxis.setTypeface(tfLight);
xAxis.setDrawGridLines(false);
xAxis.setGranularity(1f); // only intervals of 1 day
xAxis.setLabelCount(7);
@ -92,23 +98,23 @@ public class BarChartActivity extends DemoBase implements OnSeekBarChangeListene
IAxisValueFormatter custom = new MyAxisValueFormatter();
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setTypeface(mTfLight);
YAxis leftAxis = chart.getAxisLeft();
leftAxis.setTypeface(tfLight);
leftAxis.setLabelCount(8, false);
leftAxis.setValueFormatter(custom);
leftAxis.setPosition(YAxisLabelPosition.OUTSIDE_CHART);
leftAxis.setSpaceTop(15f);
leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
YAxis rightAxis = mChart.getAxisRight();
YAxis rightAxis = chart.getAxisRight();
rightAxis.setDrawGridLines(false);
rightAxis.setTypeface(mTfLight);
rightAxis.setTypeface(tfLight);
rightAxis.setLabelCount(8, false);
rightAxis.setValueFormatter(custom);
rightAxis.setSpaceTop(15f);
rightAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
Legend l = mChart.getLegend();
Legend l = chart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
@ -117,152 +123,46 @@ public class BarChartActivity extends DemoBase implements OnSeekBarChangeListene
l.setFormSize(9f);
l.setTextSize(11f);
l.setXEntrySpace(4f);
// l.setExtra(ColorTemplate.VORDIPLOM_COLORS, new String[] { "abc",
// "def", "ghj", "ikl", "mno" });
// l.setCustom(ColorTemplate.VORDIPLOM_COLORS, new String[] { "abc",
// "def", "ghj", "ikl", "mno" });
XYMarkerView mv = new XYMarkerView(this, xAxisFormatter);
mv.setChartView(mChart); // For bounds control
mChart.setMarker(mv); // Set the marker to the chart
setData(12, 50);
mv.setChartView(chart); // For bounds control
chart.setMarker(mv); // Set the marker to the chart
// setting data
mSeekBarY.setProgress(50);
mSeekBarX.setProgress(12);
seekBarY.setProgress(50);
seekBarX.setProgress(12);
setData(12, 50);
mSeekBarY.setOnSeekBarChangeListener(this);
mSeekBarX.setOnSeekBarChangeListener(this);
// mChart.setDrawLegend(false);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.bar, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.actionToggleValues: {
for (IDataSet set : mChart.getData().getDataSets())
set.setDrawValues(!set.isDrawValuesEnabled());
mChart.invalidate();
break;
}
case R.id.actionToggleIcons: {
for (IDataSet set : mChart.getData().getDataSets())
set.setDrawIcons(!set.isDrawIconsEnabled());
mChart.invalidate();
break;
}
case R.id.actionToggleHighlight: {
if (mChart.getData() != null) {
mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled());
mChart.invalidate();
}
break;
}
case R.id.actionTogglePinch: {
if (mChart.isPinchZoomEnabled())
mChart.setPinchZoom(false);
else
mChart.setPinchZoom(true);
mChart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
break;
}
case R.id.actionToggleBarBorders: {
for (IBarDataSet set : mChart.getData().getDataSets())
((BarDataSet) set).setBarBorderWidth(set.getBarBorderWidth() == 1.f ? 0.f : 1.f);
mChart.invalidate();
break;
}
case R.id.animateX: {
mChart.animateX(3000);
break;
}
case R.id.animateY: {
mChart.animateY(3000);
break;
}
case R.id.animateXY: {
mChart.animateXY(3000, 3000);
break;
}
case R.id.actionSave: {
if (mChart.saveToGallery("title" + System.currentTimeMillis(), 50)) {
Toast.makeText(getApplicationContext(), "Saving SUCCESSFUL!",
Toast.LENGTH_SHORT).show();
} else
Toast.makeText(getApplicationContext(), "Saving FAILED!", Toast.LENGTH_SHORT)
.show();
break;
}
}
return true;
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvX.setText("" + (mSeekBarX.getProgress() + 2));
tvY.setText("" + (mSeekBarY.getProgress()));
setData(mSeekBarX.getProgress() + 1 , mSeekBarY.getProgress());
mChart.invalidate();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
// chart.setDrawLegend(false);
}
private void setData(int count, float range) {
float start = 1f;
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
ArrayList<BarEntry> values = new ArrayList<>();
for (int i = (int) start; i < start + count + 1; i++) {
float mult = (range + 1);
float val = (float) (Math.random() * mult);
for (int i = (int) start; i < start + count; i++) {
float val = (float) (Math.random() * (range + 1));
if (Math.random() * 100 < 25) {
yVals1.add(new BarEntry(i, val, getResources().getDrawable(R.drawable.star)));
values.add(new BarEntry(i, val, getResources().getDrawable(R.drawable.star)));
} else {
yVals1.add(new BarEntry(i, val));
values.add(new BarEntry(i, val));
}
}
BarDataSet set1;
if (mChart.getData() != null &&
mChart.getData().getDataSetCount() > 0) {
set1 = (BarDataSet) mChart.getData().getDataSetByIndex(0);
set1.setValues(yVals1);
mChart.getData().notifyDataChanged();
mChart.notifyDataSetChanged();
if (chart.getData() != null &&
chart.getData().getDataSetCount() > 0) {
set1 = (BarDataSet) chart.getData().getDataSetByIndex(0);
set1.setValues(values);
chart.getData().notifyDataChanged();
chart.notifyDataSetChanged();
} else {
set1 = new BarDataSet(yVals1, "The year 2017");
set1 = new BarDataSet(values, "The year 2017");
set1.setDrawIcons(false);
@ -292,37 +192,139 @@ public class BarChartActivity extends DemoBase implements OnSeekBarChangeListene
set1.setGradientColors(gradientColors);
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
ArrayList<IBarDataSet> dataSets = new ArrayList<>();
dataSets.add(set1);
BarData data = new BarData(dataSets);
data.setValueTextSize(10f);
data.setValueTypeface(mTfLight);
data.setValueTypeface(tfLight);
data.setBarWidth(0.9f);
mChart.setData(data);
chart.setData(data);
}
}
protected RectF mOnValueSelectedRectF = new RectF();
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.bar, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivity.java"));
startActivity(i);
break;
}
case R.id.actionToggleValues: {
for (IDataSet set : chart.getData().getDataSets())
set.setDrawValues(!set.isDrawValuesEnabled());
chart.invalidate();
break;
}
case R.id.actionToggleIcons: {
for (IDataSet set : chart.getData().getDataSets())
set.setDrawIcons(!set.isDrawIconsEnabled());
chart.invalidate();
break;
}
case R.id.actionToggleHighlight: {
if (chart.getData() != null) {
chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled());
chart.invalidate();
}
break;
}
case R.id.actionTogglePinch: {
if (chart.isPinchZoomEnabled())
chart.setPinchZoom(false);
else
chart.setPinchZoom(true);
chart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled());
chart.notifyDataSetChanged();
break;
}
case R.id.actionToggleBarBorders: {
for (IBarDataSet set : chart.getData().getDataSets())
((BarDataSet) set).setBarBorderWidth(set.getBarBorderWidth() == 1.f ? 0.f : 1.f);
chart.invalidate();
break;
}
case R.id.animateX: {
chart.animateX(2000);
break;
}
case R.id.animateY: {
chart.animateY(2000);
break;
}
case R.id.animateXY: {
chart.animateXY(2000, 2000);
break;
}
case R.id.actionSave: {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveToGallery();
} else {
requestStoragePermission(chart);
}
break;
}
}
return true;
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvX.setText(String.valueOf(seekBarX.getProgress()));
tvY.setText(String.valueOf(seekBarY.getProgress()));
setData(seekBarX.getProgress(), seekBarY.getProgress());
chart.invalidate();
}
@Override
public void saveToGallery() {
saveToGallery(chart, "BarChartActivity");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
private RectF onValueSelectedRectF = new RectF();
@SuppressLint("NewApi")
@Override
public void onValueSelected(Entry e, Highlight h) {
if (e == null)
return;
RectF bounds = mOnValueSelectedRectF;
mChart.getBarBounds((BarEntry) e, bounds);
MPPointF position = mChart.getPosition(e, AxisDependency.LEFT);
RectF bounds = onValueSelectedRectF;
chart.getBarBounds((BarEntry) e, bounds);
MPPointF position = chart.getPosition(e, AxisDependency.LEFT);
Log.i("bounds", bounds.toString());
Log.i("position", position.toString());
Log.i("x-index",
"low: " + mChart.getLowestVisibleX() + ", high: "
+ mChart.getHighestVisibleX());
"low: " + chart.getLowestVisibleX() + ", high: "
+ chart.getHighestVisibleX());
MPPointF.recycleInstance(position);
}

View file

@ -1,8 +1,13 @@
package com.xxmassdeveloper.mpchartexample;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
@ -29,12 +34,13 @@ import com.xxmassdeveloper.mpchartexample.custom.MyMarkerView;
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
import java.util.ArrayList;
import java.util.Locale;
public class BarChartActivityMultiDataset extends DemoBase implements OnSeekBarChangeListener,
OnChartValueSelectedListener {
private BarChart mChart;
private SeekBar mSeekBarX, mSeekBarY;
private BarChart chart;
private SeekBar seekBarX, seekBarY;
private TextView tvX, tvY;
@Override
@ -44,51 +50,53 @@ public class BarChartActivityMultiDataset extends DemoBase implements OnSeekBarC
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_barchart);
setTitle("BarChartActivityMultiDataset");
tvX = findViewById(R.id.tvXMax);
tvX.setTextSize(10);
tvY = findViewById(R.id.tvYMax);
mSeekBarX = findViewById(R.id.seekBar1);
mSeekBarX.setOnSeekBarChangeListener(this);
seekBarX = findViewById(R.id.seekBar1);
seekBarX.setOnSeekBarChangeListener(this);
mSeekBarY = findViewById(R.id.seekBar2);
mSeekBarY.setOnSeekBarChangeListener(this);
seekBarY = findViewById(R.id.seekBar2);
seekBarY.setOnSeekBarChangeListener(this);
mChart = findViewById(R.id.chart1);
mChart.setOnChartValueSelectedListener(this);
mChart.getDescription().setEnabled(false);
chart = findViewById(R.id.chart1);
chart.setOnChartValueSelectedListener(this);
chart.getDescription().setEnabled(false);
// mChart.setDrawBorders(true);
// chart.setDrawBorders(true);
// scaling can now only be done on x- and y-axis separately
mChart.setPinchZoom(false);
chart.setPinchZoom(false);
mChart.setDrawBarShadow(false);
chart.setDrawBarShadow(false);
mChart.setDrawGridBackground(false);
chart.setDrawGridBackground(false);
// create a custom MarkerView (extend MarkerView) and specify the layout
// to use for it
MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker_view);
mv.setChartView(mChart); // For bounds control
mChart.setMarker(mv); // Set the marker to the chart
mv.setChartView(chart); // For bounds control
chart.setMarker(mv); // Set the marker to the chart
mSeekBarX.setProgress(10);
mSeekBarY.setProgress(100);
seekBarX.setProgress(10);
seekBarY.setProgress(100);
Legend l = mChart.getLegend();
Legend l = chart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
l.setOrientation(Legend.LegendOrientation.VERTICAL);
l.setDrawInside(true);
l.setTypeface(mTfLight);
l.setTypeface(tfLight);
l.setYOffset(0f);
l.setXOffset(10f);
l.setYEntrySpace(0f);
l.setTextSize(8f);
XAxis xAxis = mChart.getXAxis();
xAxis.setTypeface(mTfLight);
XAxis xAxis = chart.getXAxis();
xAxis.setTypeface(tfLight);
xAxis.setGranularity(1f);
xAxis.setCenterAxisLabels(true);
xAxis.setValueFormatter(new IAxisValueFormatter() {
@ -98,14 +106,88 @@ public class BarChartActivityMultiDataset extends DemoBase implements OnSeekBarC
}
});
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setTypeface(mTfLight);
YAxis leftAxis = chart.getAxisLeft();
leftAxis.setTypeface(tfLight);
leftAxis.setValueFormatter(new LargeValueFormatter());
leftAxis.setDrawGridLines(false);
leftAxis.setSpaceTop(35f);
leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
mChart.getAxisRight().setEnabled(false);
chart.getAxisRight().setEnabled(false);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
float groupSpace = 0.08f;
float barSpace = 0.03f; // x4 DataSet
float barWidth = 0.2f; // x4 DataSet
// (0.2 + 0.03) * 4 + 0.08 = 1.00 -> interval per "group"
int groupCount = seekBarX.getProgress() + 1;
int startYear = 1980;
int endYear = startYear + groupCount;
tvX.setText(String.format(Locale.ENGLISH, "%d-%d", startYear, endYear));
tvY.setText(String.valueOf(seekBarY.getProgress()));
ArrayList<BarEntry> values1 = new ArrayList<>();
ArrayList<BarEntry> values2 = new ArrayList<>();
ArrayList<BarEntry> values3 = new ArrayList<>();
ArrayList<BarEntry> values4 = new ArrayList<>();
float randomMultiplier = seekBarY.getProgress() * 100000f;
for (int i = startYear; i < endYear; i++) {
values1.add(new BarEntry(i, (float) (Math.random() * randomMultiplier)));
values2.add(new BarEntry(i, (float) (Math.random() * randomMultiplier)));
values3.add(new BarEntry(i, (float) (Math.random() * randomMultiplier)));
values4.add(new BarEntry(i, (float) (Math.random() * randomMultiplier)));
}
BarDataSet set1, set2, set3, set4;
if (chart.getData() != null && chart.getData().getDataSetCount() > 0) {
set1 = (BarDataSet) chart.getData().getDataSetByIndex(0);
set2 = (BarDataSet) chart.getData().getDataSetByIndex(1);
set3 = (BarDataSet) chart.getData().getDataSetByIndex(2);
set4 = (BarDataSet) chart.getData().getDataSetByIndex(3);
set1.setValues(values1);
set2.setValues(values2);
set3.setValues(values3);
set4.setValues(values4);
chart.getData().notifyDataChanged();
chart.notifyDataSetChanged();
} else {
// create 4 DataSets
set1 = new BarDataSet(values1, "Company A");
set1.setColor(Color.rgb(104, 241, 175));
set2 = new BarDataSet(values2, "Company B");
set2.setColor(Color.rgb(164, 228, 251));
set3 = new BarDataSet(values3, "Company C");
set3.setColor(Color.rgb(242, 247, 158));
set4 = new BarDataSet(values4, "Company D");
set4.setColor(Color.rgb(255, 102, 0));
BarData data = new BarData(set1, set2, set3, set4);
data.setValueFormatter(new LargeValueFormatter());
data.setValueTypeface(tfLight);
chart.setData(data);
}
// specify the width each bar should have
chart.getBarData().setBarWidth(barWidth);
// restrict the x-axis range
chart.getXAxis().setAxisMinimum(startYear);
// barData.getGroupWith(...) is a helper that calculates the width each group needs based on the provided parameters
chart.getXAxis().setAxisMaximum(startYear + chart.getBarData().getGroupWidth(groupSpace, barSpace) * groupCount);
chart.groupBars(startYear, groupSpace, barSpace);
chart.invalidate();
}
@Override
@ -118,56 +200,65 @@ public class BarChartActivityMultiDataset extends DemoBase implements OnSeekBarC
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivityMultiDataset.java"));
startActivity(i);
break;
}
case R.id.actionToggleValues: {
for (IBarDataSet set : mChart.getData().getDataSets())
for (IBarDataSet set : chart.getData().getDataSets())
set.setDrawValues(!set.isDrawValuesEnabled());
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionTogglePinch: {
if (mChart.isPinchZoomEnabled())
mChart.setPinchZoom(false);
if (chart.isPinchZoomEnabled())
chart.setPinchZoom(false);
else
mChart.setPinchZoom(true);
chart.setPinchZoom(true);
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled());
chart.notifyDataSetChanged();
break;
}
case R.id.actionToggleBarBorders: {
for (IBarDataSet set : mChart.getData().getDataSets())
for (IBarDataSet set : chart.getData().getDataSets())
((BarDataSet) set).setBarBorderWidth(set.getBarBorderWidth() == 1.f ? 0.f : 1.f);
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionToggleHighlight: {
if (mChart.getData() != null) {
mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled());
mChart.invalidate();
if (chart.getData() != null) {
chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled());
chart.invalidate();
}
break;
}
case R.id.actionSave: {
// mChart.saveToGallery("title"+System.currentTimeMillis());
mChart.saveToPath("title" + System.currentTimeMillis(), "");
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveToGallery();
} else {
requestStoragePermission(chart);
}
break;
}
case R.id.animateX: {
mChart.animateX(3000);
chart.animateX(2000);
break;
}
case R.id.animateY: {
mChart.animateY(3000);
chart.animateY(2000);
break;
}
case R.id.animateXY: {
mChart.animateXY(3000, 3000);
chart.animateXY(2000, 2000);
break;
}
}
@ -175,88 +266,15 @@ public class BarChartActivityMultiDataset extends DemoBase implements OnSeekBarC
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
float groupSpace = 0.08f;
float barSpace = 0.03f; // x4 DataSet
float barWidth = 0.2f; // x4 DataSet
// (0.2 + 0.03) * 4 + 0.08 = 1.00 -> interval per "group"
int groupCount = mSeekBarX.getProgress() + 1;
int startYear = 1980;
int endYear = startYear + groupCount;
tvX.setText(startYear + "-" + endYear);
tvY.setText("" + (mSeekBarY.getProgress()));
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
ArrayList<BarEntry> yVals2 = new ArrayList<BarEntry>();
ArrayList<BarEntry> yVals3 = new ArrayList<BarEntry>();
ArrayList<BarEntry> yVals4 = new ArrayList<BarEntry>();
float randomMultiplier = mSeekBarY.getProgress() * 100000f;
for (int i = startYear; i < endYear; i++) {
yVals1.add(new BarEntry(i, (float) (Math.random() * randomMultiplier)));
yVals2.add(new BarEntry(i, (float) (Math.random() * randomMultiplier)));
yVals3.add(new BarEntry(i, (float) (Math.random() * randomMultiplier)));
yVals4.add(new BarEntry(i, (float) (Math.random() * randomMultiplier)));
}
BarDataSet set1, set2, set3, set4;
if (mChart.getData() != null && mChart.getData().getDataSetCount() > 0) {
set1 = (BarDataSet) mChart.getData().getDataSetByIndex(0);
set2 = (BarDataSet) mChart.getData().getDataSetByIndex(1);
set3 = (BarDataSet) mChart.getData().getDataSetByIndex(2);
set4 = (BarDataSet) mChart.getData().getDataSetByIndex(3);
set1.setValues(yVals1);
set2.setValues(yVals2);
set3.setValues(yVals3);
set4.setValues(yVals4);
mChart.getData().notifyDataChanged();
mChart.notifyDataSetChanged();
} else {
// create 4 DataSets
set1 = new BarDataSet(yVals1, "Company A");
set1.setColor(Color.rgb(104, 241, 175));
set2 = new BarDataSet(yVals2, "Company B");
set2.setColor(Color.rgb(164, 228, 251));
set3 = new BarDataSet(yVals3, "Company C");
set3.setColor(Color.rgb(242, 247, 158));
set4 = new BarDataSet(yVals4, "Company D");
set4.setColor(Color.rgb(255, 102, 0));
BarData data = new BarData(set1, set2, set3, set4);
data.setValueFormatter(new LargeValueFormatter());
data.setValueTypeface(mTfLight);
mChart.setData(data);
}
// specify the width each bar should have
mChart.getBarData().setBarWidth(barWidth);
// restrict the x-axis range
mChart.getXAxis().setAxisMinimum(startYear);
// barData.getGroupWith(...) is a helper that calculates the width each group needs based on the provided parameters
mChart.getXAxis().setAxisMaximum(startYear + mChart.getBarData().getGroupWidth(groupSpace, barSpace) * groupCount);
mChart.groupBars(startYear, groupSpace, barSpace);
mChart.invalidate();
public void saveToGallery() {
saveToGallery(chart, "BarChartActivityMultiDataset");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {}
@Override
public void onValueSelected(Entry e, Highlight h) {

View file

@ -1,20 +1,23 @@
package com.xxmassdeveloper.mpchartexample;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.Legend.LegendForm;
import com.github.mikephil.charting.components.Legend.LegendPosition;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
@ -29,11 +32,11 @@ import java.util.List;
public class BarChartActivitySinus extends DemoBase implements OnSeekBarChangeListener {
protected BarChart mChart;
private SeekBar mSeekBarX;
private BarChart chart;
private SeekBar seekBarX;
private TextView tvX;
private List<BarEntry> mSinusData;
private List<BarEntry> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -42,57 +45,59 @@ public class BarChartActivitySinus extends DemoBase implements OnSeekBarChangeLi
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_barchart_sinus);
mSinusData = FileUtils.loadBarEntriesFromAssets(getAssets(), "othersine.txt");
setTitle("BarChartActivitySinus");
data = FileUtils.loadBarEntriesFromAssets(getAssets(), "othersine.txt");
tvX = findViewById(R.id.tvValueCount);
mSeekBarX = findViewById(R.id.seekbarValues);
seekBarX = findViewById(R.id.seekbarValues);
mChart = findViewById(R.id.chart1);
chart = findViewById(R.id.chart1);
mChart.setDrawBarShadow(false);
mChart.setDrawValueAboveBar(true);
chart.setDrawBarShadow(false);
chart.setDrawValueAboveBar(true);
mChart.getDescription().setEnabled(false);
chart.getDescription().setEnabled(false);
// if more than 60 entries are displayed in the chart, no values will be
// drawn
mChart.setMaxVisibleValueCount(60);
chart.setMaxVisibleValueCount(60);
// scaling can now only be done on x- and y-axis separately
mChart.setPinchZoom(false);
chart.setPinchZoom(false);
// draw shadows for each bar that show the maximum value
// mChart.setDrawBarShadow(true);
// chart.setDrawBarShadow(true);
// mChart.setDrawXLabels(false);
// chart.setDrawXLabels(false);
mChart.setDrawGridBackground(false);
// mChart.setDrawYLabels(false);
chart.setDrawGridBackground(false);
// chart.setDrawYLabels(false);
XAxis xAxis = mChart.getXAxis();
XAxis xAxis = chart.getXAxis();
xAxis.setEnabled(false);
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setTypeface(mTfLight);
YAxis leftAxis = chart.getAxisLeft();
leftAxis.setTypeface(tfLight);
leftAxis.setLabelCount(6, false);
leftAxis.setAxisMinimum(-2.5f);
leftAxis.setAxisMaximum(2.5f);
leftAxis.setGranularityEnabled(true);
leftAxis.setGranularity(0.1f);
YAxis rightAxis = mChart.getAxisRight();
YAxis rightAxis = chart.getAxisRight();
rightAxis.setDrawGridLines(false);
rightAxis.setTypeface(mTfLight);
rightAxis.setTypeface(tfLight);
rightAxis.setLabelCount(6, false);
rightAxis.setAxisMinimum(-2.5f);
rightAxis.setAxisMaximum(2.5f);
rightAxis.setGranularity(0.1f);
mSeekBarX.setOnSeekBarChangeListener(this);
mSeekBarX.setProgress(150); // set data
seekBarX.setOnSeekBarChangeListener(this);
seekBarX.setProgress(150); // set data
Legend l = mChart.getLegend();
Legend l = chart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
@ -102,7 +107,37 @@ public class BarChartActivitySinus extends DemoBase implements OnSeekBarChangeLi
l.setTextSize(11f);
l.setXEntrySpace(4f);
mChart.animateXY(2000, 2000);
chart.animateXY(1500, 1500);
}
private void setData(int count) {
ArrayList<BarEntry> entries = new ArrayList<>();
for (int i = 0; i < count; i++) {
entries.add(data.get(i));
}
BarDataSet set;
if (chart.getData() != null &&
chart.getData().getDataSetCount() > 0) {
set = (BarDataSet) chart.getData().getDataSetByIndex(0);
set.setValues(entries);
chart.getData().notifyDataChanged();
chart.notifyDataSetChanged();
} else {
set = new BarDataSet(entries, "Sinus Function");
set.setColor(Color.rgb(240, 120, 124));
}
BarData data = new BarData(set);
data.setValueTextSize(10f);
data.setValueTypeface(tfLight);
data.setDrawValues(false);
data.setBarWidth(0.8f);
chart.setData(data);
}
@Override
@ -115,61 +150,66 @@ public class BarChartActivitySinus extends DemoBase implements OnSeekBarChangeLi
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivitySinus.java"));
startActivity(i);
break;
}
case R.id.actionToggleValues: {
for (IBarDataSet set : mChart.getData().getDataSets())
for (IBarDataSet set : chart.getData().getDataSets())
set.setDrawValues(!set.isDrawValuesEnabled());
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionToggleHighlight: {
if (mChart.getData() != null) {
mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled());
mChart.invalidate();
if (chart.getData() != null) {
chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled());
chart.invalidate();
}
break;
}
case R.id.actionTogglePinch: {
if (mChart.isPinchZoomEnabled())
mChart.setPinchZoom(false);
if (chart.isPinchZoomEnabled())
chart.setPinchZoom(false);
else
mChart.setPinchZoom(true);
chart.setPinchZoom(true);
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled());
chart.notifyDataSetChanged();
break;
}
case R.id.actionToggleBarBorders: {
for (IBarDataSet set : mChart.getData().getDataSets())
for (IBarDataSet set : chart.getData().getDataSets())
((BarDataSet) set).setBarBorderWidth(set.getBarBorderWidth() == 1.f ? 0.f : 1.f);
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.animateX: {
mChart.animateX(1500);
chart.animateX(2000);
break;
}
case R.id.animateY: {
mChart.animateY(1500);
chart.animateY(2000);
break;
}
case R.id.animateXY: {
mChart.animateXY(2000, 2000);
chart.animateXY(2000, 2000);
break;
}
case R.id.actionSave: {
if (mChart.saveToGallery("title" + System.currentTimeMillis(), 50)) {
Toast.makeText(getApplicationContext(), "Saving SUCCESSFUL!",
Toast.LENGTH_SHORT).show();
} else
Toast.makeText(getApplicationContext(), "Saving FAILED!", Toast.LENGTH_SHORT)
.show();
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveToGallery();
} else {
requestStoragePermission(chart);
}
break;
}
}
@ -179,51 +219,21 @@ public class BarChartActivitySinus extends DemoBase implements OnSeekBarChangeLi
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvX.setText("" + (mSeekBarX.getProgress()));
tvX.setText(String.valueOf(seekBarX.getProgress()));
setData(mSeekBarX.getProgress());
mChart.invalidate();
setData(seekBarX.getProgress());
chart.invalidate();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
public void saveToGallery() {
saveToGallery(chart, "BarChartActivitySinus");
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
public void onStartTrackingTouch(SeekBar seekBar) {}
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
private void setData(int count) {
ArrayList<BarEntry> entries = new ArrayList<BarEntry>();
for (int i = 0; i < count; i++) {
entries.add(mSinusData.get(i));
}
BarDataSet set;
if (mChart.getData() != null &&
mChart.getData().getDataSetCount() > 0) {
set = (BarDataSet) mChart.getData().getDataSetByIndex(0);
set.setValues(entries);
mChart.getData().notifyDataChanged();
mChart.notifyDataSetChanged();
} else {
set = new BarDataSet(entries, "Sinus Function");
set.setColor(Color.rgb(240, 120, 124));
}
BarData data = new BarData(set);
data.setValueTextSize(10f);
data.setValueTypeface(mTfLight);
data.setDrawValues(false);
data.setBarWidth(0.8f);
mChart.setData(data);
}
}

View file

@ -1,9 +1,12 @@
package com.xxmassdeveloper.mpchartexample;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import com.github.mikephil.charting.charts.BarChart;
@ -26,8 +29,7 @@ import java.util.List;
public class BarChartPositiveNegative extends DemoBase {
protected BarChart mChart;
private Typeface mTf;
private BarChart chart;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -36,27 +38,28 @@ public class BarChartPositiveNegative extends DemoBase {
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_barchart_noseekbar);
mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");
mChart = findViewById(R.id.chart1);
mChart.setBackgroundColor(Color.WHITE);
mChart.setExtraTopOffset(-30f);
mChart.setExtraBottomOffset(10f);
mChart.setExtraLeftOffset(70f);
mChart.setExtraRightOffset(70f);
setTitle("BarChartPositiveNegative");
mChart.setDrawBarShadow(false);
mChart.setDrawValueAboveBar(true);
chart = findViewById(R.id.chart1);
chart.setBackgroundColor(Color.WHITE);
chart.setExtraTopOffset(-30f);
chart.setExtraBottomOffset(10f);
chart.setExtraLeftOffset(70f);
chart.setExtraRightOffset(70f);
mChart.getDescription().setEnabled(false);
chart.setDrawBarShadow(false);
chart.setDrawValueAboveBar(true);
chart.getDescription().setEnabled(false);
// scaling can now only be done on x- and y-axis separately
mChart.setPinchZoom(false);
chart.setPinchZoom(false);
mChart.setDrawGridBackground(false);
chart.setDrawGridBackground(false);
XAxis xAxis = mChart.getXAxis();
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxisPosition.BOTTOM);
xAxis.setTypeface(mTf);
xAxis.setTypeface(tfRegular);
xAxis.setDrawGridLines(false);
xAxis.setDrawAxisLine(false);
xAxis.setTextColor(Color.LTGRAY);
@ -65,7 +68,7 @@ public class BarChartPositiveNegative extends DemoBase {
xAxis.setCenterAxisLabels(true);
xAxis.setGranularity(1f);
YAxis left = mChart.getAxisLeft();
YAxis left = chart.getAxisLeft();
left.setDrawLabels(false);
left.setSpaceTop(25f);
left.setSpaceBottom(25f);
@ -74,8 +77,8 @@ public class BarChartPositiveNegative extends DemoBase {
left.setDrawZeroLine(true); // draw a zero line
left.setZeroLineColor(Color.GRAY);
left.setZeroLineWidth(0.7f);
mChart.getAxisRight().setEnabled(false);
mChart.getLegend().setEnabled(false);
chart.getAxisRight().setEnabled(false);
chart.getLegend().setEnabled(false);
// THIS IS THE ORIGINAL DATA YOU WANT TO PLOT
final List<Data> data = new ArrayList<>();
@ -97,8 +100,8 @@ public class BarChartPositiveNegative extends DemoBase {
private void setData(List<Data> dataList) {
ArrayList<BarEntry> values = new ArrayList<BarEntry>();
List<Integer> colors = new ArrayList<Integer>();
ArrayList<BarEntry> values = new ArrayList<>();
List<Integer> colors = new ArrayList<>();
int green = Color.rgb(110, 190, 102);
int red = Color.rgb(211, 74, 88);
@ -118,12 +121,12 @@ public class BarChartPositiveNegative extends DemoBase {
BarDataSet set;
if (mChart.getData() != null &&
mChart.getData().getDataSetCount() > 0) {
set = (BarDataSet)mChart.getData().getDataSetByIndex(0);
if (chart.getData() != null &&
chart.getData().getDataSetCount() > 0) {
set = (BarDataSet) chart.getData().getDataSetByIndex(0);
set.setValues(values);
mChart.getData().notifyDataChanged();
mChart.notifyDataSetChanged();
chart.getData().notifyDataChanged();
chart.notifyDataSetChanged();
} else {
set = new BarDataSet(values, "Values");
set.setColors(colors);
@ -131,12 +134,12 @@ public class BarChartPositiveNegative extends DemoBase {
BarData data = new BarData(set);
data.setValueTextSize(13f);
data.setValueTypeface(mTf);
data.setValueTypeface(tfRegular);
data.setValueFormatter(new ValueFormatter());
data.setBarWidth(0.8f);
mChart.setData(data);
mChart.invalidate();
chart.setData(data);
chart.invalidate();
}
}
@ -145,11 +148,11 @@ public class BarChartPositiveNegative extends DemoBase {
*/
private class Data {
public String xAxisValue;
public float yValue;
public float xValue;
String xAxisValue;
float yValue;
float xValue;
public Data(float xValue, float yValue, String xAxisValue) {
Data(float xValue, float yValue, String xAxisValue) {
this.xAxisValue = xAxisValue;
this.yValue = yValue;
this.xValue = xValue;
@ -161,7 +164,7 @@ public class BarChartPositiveNegative extends DemoBase {
private DecimalFormat mFormat;
public ValueFormatter() {
ValueFormatter() {
mFormat = new DecimalFormat("######.0");
}
@ -170,4 +173,28 @@ public class BarChartPositiveNegative extends DemoBase {
return mFormat.format(value);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.only_github, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartPositiveNegative.java"));
startActivity(i);
break;
}
}
return true;
}
@Override
public void saveToGallery() { /* Intentionally left empty */ }
}

View file

@ -1,8 +1,13 @@
package com.xxmassdeveloper.mpchartexample;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
@ -13,7 +18,6 @@ import android.widget.TextView;
import com.github.mikephil.charting.charts.BubbleChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.Legend.LegendPosition;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BubbleData;
@ -33,10 +37,10 @@ import java.util.ArrayList;
public class BubbleChartActivity extends DemoBase implements OnSeekBarChangeListener,
OnChartValueSelectedListener {
private BubbleChart mChart;
private SeekBar mSeekBarX, mSeekBarY;
private BubbleChart chart;
private SeekBar seekBarX, seekBarY;
private TextView tvX, tvY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -44,52 +48,106 @@ public class BubbleChartActivity extends DemoBase implements OnSeekBarChangeList
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_bubblechart);
setTitle("BubbleChartActivity");
tvX = findViewById(R.id.tvXMax);
tvY = findViewById(R.id.tvYMax);
mSeekBarX = findViewById(R.id.seekBar1);
mSeekBarX.setOnSeekBarChangeListener(this);
seekBarX = findViewById(R.id.seekBar1);
seekBarX.setOnSeekBarChangeListener(this);
mSeekBarY = findViewById(R.id.seekBar2);
mSeekBarY.setOnSeekBarChangeListener(this);
seekBarY = findViewById(R.id.seekBar2);
seekBarY.setOnSeekBarChangeListener(this);
mChart = findViewById(R.id.chart1);
mChart.getDescription().setEnabled(false);
chart = findViewById(R.id.chart1);
chart.getDescription().setEnabled(false);
mChart.setOnChartValueSelectedListener(this);
chart.setOnChartValueSelectedListener(this);
mChart.setDrawGridBackground(false);
chart.setDrawGridBackground(false);
mChart.setTouchEnabled(true);
chart.setTouchEnabled(true);
// enable scaling and dragging
mChart.setDragEnabled(true);
mChart.setScaleEnabled(true);
chart.setDragEnabled(true);
chart.setScaleEnabled(true);
mChart.setMaxVisibleValueCount(200);
mChart.setPinchZoom(true);
chart.setMaxVisibleValueCount(200);
chart.setPinchZoom(true);
mSeekBarX.setProgress(10);
mSeekBarY.setProgress(50);
seekBarX.setProgress(10);
seekBarY.setProgress(50);
Legend l = mChart.getLegend();
Legend l = chart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
l.setOrientation(Legend.LegendOrientation.VERTICAL);
l.setDrawInside(false);
l.setTypeface(mTfLight);
l.setTypeface(tfLight);
YAxis yl = mChart.getAxisLeft();
yl.setTypeface(mTfLight);
YAxis yl = chart.getAxisLeft();
yl.setTypeface(tfLight);
yl.setSpaceTop(30f);
yl.setSpaceBottom(30f);
yl.setDrawZeroLine(false);
mChart.getAxisRight().setEnabled(false);
XAxis xl = mChart.getXAxis();
chart.getAxisRight().setEnabled(false);
XAxis xl = chart.getXAxis();
xl.setPosition(XAxis.XAxisPosition.BOTTOM);
xl.setTypeface(mTfLight);
xl.setTypeface(tfLight);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int count = seekBarX.getProgress();
int range = seekBarY.getProgress();
tvX.setText(String.valueOf(count));
tvY.setText(String.valueOf(range));
ArrayList<BubbleEntry> values1 = new ArrayList<>();
ArrayList<BubbleEntry> values2 = new ArrayList<>();
ArrayList<BubbleEntry> values3 = new ArrayList<>();
for (int i = 0; i < count; i++) {
values1.add(new BubbleEntry(i, (float) (Math.random() * range), (float) (Math.random() * range), getResources().getDrawable(R.drawable.star)));
values2.add(new BubbleEntry(i, (float) (Math.random() * range), (float) (Math.random() * range), getResources().getDrawable(R.drawable.star)));
values3.add(new BubbleEntry(i, (float) (Math.random() * range), (float) (Math.random() * range)));
}
// create a dataset and give it a type
BubbleDataSet set1 = new BubbleDataSet(values1, "DS 1");
set1.setDrawIcons(false);
set1.setColor(ColorTemplate.COLORFUL_COLORS[0], 130);
set1.setDrawValues(true);
BubbleDataSet set2 = new BubbleDataSet(values2, "DS 2");
set2.setDrawIcons(false);
set2.setIconsOffset(new MPPointF(0, 15));
set2.setColor(ColorTemplate.COLORFUL_COLORS[1], 130);
set2.setDrawValues(true);
BubbleDataSet set3 = new BubbleDataSet(values3, "DS 3");
set3.setColor(ColorTemplate.COLORFUL_COLORS[2], 130);
set3.setDrawValues(true);
ArrayList<IBubbleDataSet> dataSets = new ArrayList<>();
dataSets.add(set1); // add the data sets
dataSets.add(set2);
dataSets.add(set3);
// create a data object with the data sets
BubbleData data = new BubbleData(dataSets);
data.setDrawValues(false);
data.setValueTypeface(tfLight);
data.setValueTextSize(8f);
data.setValueTextColor(Color.WHITE);
data.setHighlightCircleWidth(1.5f);
chart.setData(data);
chart.invalidate();
}
@Override
@ -102,56 +160,65 @@ public class BubbleChartActivity extends DemoBase implements OnSeekBarChangeList
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BubbleChartActivity.java"));
startActivity(i);
break;
}
case R.id.actionToggleValues: {
for (IDataSet set : mChart.getData().getDataSets())
for (IDataSet set : chart.getData().getDataSets())
set.setDrawValues(!set.isDrawValuesEnabled());
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionToggleIcons: {
for (IDataSet set : mChart.getData().getDataSets())
for (IDataSet set : chart.getData().getDataSets())
set.setDrawIcons(!set.isDrawIconsEnabled());
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionToggleHighlight: {
if(mChart.getData() != null) {
mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled());
mChart.invalidate();
if(chart.getData() != null) {
chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled());
chart.invalidate();
}
break;
}
case R.id.actionTogglePinch: {
if (mChart.isPinchZoomEnabled())
mChart.setPinchZoom(false);
if (chart.isPinchZoomEnabled())
chart.setPinchZoom(false);
else
mChart.setPinchZoom(true);
chart.setPinchZoom(true);
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled());
chart.notifyDataSetChanged();
break;
}
case R.id.actionSave: {
mChart.saveToPath("title" + System.currentTimeMillis(), "");
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveToGallery();
} else {
requestStoragePermission(chart);
}
break;
}
case R.id.animateX: {
mChart.animateX(3000);
chart.animateX(2000);
break;
}
case R.id.animateY: {
mChart.animateY(3000);
chart.animateY(2000);
break;
}
case R.id.animateXY: {
mChart.animateXY(3000, 3000);
chart.animateXY(2000, 2000);
break;
}
}
@ -159,70 +226,8 @@ public class BubbleChartActivity extends DemoBase implements OnSeekBarChangeList
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int count = mSeekBarX.getProgress();
int range = mSeekBarY.getProgress();
tvX.setText("" + count);
tvY.setText("" + range);
ArrayList<BubbleEntry> yVals1 = new ArrayList<BubbleEntry>();
ArrayList<BubbleEntry> yVals2 = new ArrayList<BubbleEntry>();
ArrayList<BubbleEntry> yVals3 = new ArrayList<BubbleEntry>();
for (int i = 0; i < count; i++) {
float val = (float) (Math.random() * range);
float size = (float) (Math.random() * range);
yVals1.add(new BubbleEntry(i, val, size, getResources().getDrawable(R.drawable.star)));
}
for (int i = 0; i < count; i++) {
float val = (float) (Math.random() * range);
float size = (float) (Math.random() * range);
yVals2.add(new BubbleEntry(i, val, size, getResources().getDrawable(R.drawable.star)));
}
for (int i = 0; i < count; i++) {
float val = (float) (Math.random() * range);
float size = (float) (Math.random() * range);
yVals3.add(new BubbleEntry(i, val, size));
}
// create a dataset and give it a type
BubbleDataSet set1 = new BubbleDataSet(yVals1, "DS 1");
set1.setDrawIcons(false);
set1.setColor(ColorTemplate.COLORFUL_COLORS[0], 130);
set1.setDrawValues(true);
BubbleDataSet set2 = new BubbleDataSet(yVals2, "DS 2");
set2.setDrawIcons(false);
set2.setIconsOffset(new MPPointF(0, 15));
set2.setColor(ColorTemplate.COLORFUL_COLORS[1], 130);
set2.setDrawValues(true);
BubbleDataSet set3 = new BubbleDataSet(yVals3, "DS 3");
set3.setColor(ColorTemplate.COLORFUL_COLORS[2], 130);
set3.setDrawValues(true);
ArrayList<IBubbleDataSet> dataSets = new ArrayList<IBubbleDataSet>();
dataSets.add(set1); // add the datasets
dataSets.add(set2);
dataSets.add(set3);
// create a data object with the datasets
BubbleData data = new BubbleData(dataSets);
data.setDrawValues(false);
data.setValueTypeface(mTfLight);
data.setValueTextSize(8f);
data.setValueTextColor(Color.WHITE);
data.setHighlightCircleWidth(1.5f);
mChart.setData(data);
mChart.invalidate();
public void saveToGallery() {
saveToGallery(chart, "BubbleChartActivity");
}
@Override
@ -233,20 +238,11 @@ public class BubbleChartActivity extends DemoBase implements OnSeekBarChangeList
}
@Override
public void onNothingSelected() {
// TODO Auto-generated method stub
}
public void onNothingSelected() {}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {}
}

View file

@ -1,16 +1,20 @@
package com.xxmassdeveloper.mpchartexample;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import com.github.mikephil.charting.charts.CandleStickChart;
import com.github.mikephil.charting.components.XAxis;
@ -28,8 +32,8 @@ import java.util.ArrayList;
public class CandleStickChartActivity extends DemoBase implements OnSeekBarChangeListener {
private CandleStickChart mChart;
private SeekBar mSeekBarX, mSeekBarY;
private CandleStickChart chart;
private SeekBar seekBarX, seekBarY;
private TextView tvX, tvY;
@Override
@ -39,154 +43,77 @@ public class CandleStickChartActivity extends DemoBase implements OnSeekBarChang
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_candlechart);
setTitle("CandleStickChartActivity");
tvX = findViewById(R.id.tvXMax);
tvY = findViewById(R.id.tvYMax);
mSeekBarX = findViewById(R.id.seekBar1);
mSeekBarX.setOnSeekBarChangeListener(this);
seekBarX = findViewById(R.id.seekBar1);
seekBarX.setOnSeekBarChangeListener(this);
mSeekBarY = findViewById(R.id.seekBar2);
mSeekBarY.setOnSeekBarChangeListener(this);
seekBarY = findViewById(R.id.seekBar2);
seekBarY.setOnSeekBarChangeListener(this);
mChart = findViewById(R.id.chart1);
mChart.setBackgroundColor(Color.WHITE);
chart = findViewById(R.id.chart1);
chart.setBackgroundColor(Color.WHITE);
mChart.getDescription().setEnabled(false);
chart.getDescription().setEnabled(false);
// if more than 60 entries are displayed in the chart, no values will be
// drawn
mChart.setMaxVisibleValueCount(60);
chart.setMaxVisibleValueCount(60);
// scaling can now only be done on x- and y-axis separately
mChart.setPinchZoom(false);
chart.setPinchZoom(false);
mChart.setDrawGridBackground(false);
chart.setDrawGridBackground(false);
XAxis xAxis = mChart.getXAxis();
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(false);
YAxis leftAxis = mChart.getAxisLeft();
YAxis leftAxis = chart.getAxisLeft();
// leftAxis.setEnabled(false);
leftAxis.setLabelCount(7, false);
leftAxis.setDrawGridLines(false);
leftAxis.setDrawAxisLine(false);
YAxis rightAxis = mChart.getAxisRight();
YAxis rightAxis = chart.getAxisRight();
rightAxis.setEnabled(false);
// rightAxis.setStartAtZero(false);
// setting data
mSeekBarX.setProgress(40);
mSeekBarY.setProgress(100);
mChart.getLegend().setEnabled(false);
}
seekBarX.setProgress(40);
seekBarY.setProgress(100);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.candle, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.actionToggleValues: {
for (IDataSet set : mChart.getData().getDataSets())
set.setDrawValues(!set.isDrawValuesEnabled());
mChart.invalidate();
break;
}
case R.id.actionToggleIcons: {
for (IDataSet set : mChart.getData().getDataSets())
set.setDrawIcons(!set.isDrawIconsEnabled());
mChart.invalidate();
break;
}
case R.id.actionToggleHighlight: {
if(mChart.getData() != null) {
mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled());
mChart.invalidate();
}
break;
}
case R.id.actionTogglePinch: {
if (mChart.isPinchZoomEnabled())
mChart.setPinchZoom(false);
else
mChart.setPinchZoom(true);
mChart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
break;
}
case R.id.actionToggleMakeShadowSameColorAsCandle: {
for (ICandleDataSet set : mChart.getData().getDataSets()) {
//TODO: set.setShadowColorSameAsCandle(!set.getShadowColorSameAsCandle());
}
mChart.invalidate();
break;
}
case R.id.animateX: {
mChart.animateX(3000);
break;
}
case R.id.animateY: {
mChart.animateY(3000);
break;
}
case R.id.animateXY: {
mChart.animateXY(3000, 3000);
break;
}
case R.id.actionSave: {
if (mChart.saveToGallery("title" + System.currentTimeMillis(), 50)) {
Toast.makeText(getApplicationContext(), "Saving SUCCESSFUL!",
Toast.LENGTH_SHORT).show();
} else
Toast.makeText(getApplicationContext(), "Saving FAILED!", Toast.LENGTH_SHORT)
.show();
break;
}
}
return true;
chart.getLegend().setEnabled(false);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int prog = (mSeekBarX.getProgress() + 1);
tvX.setText("" + prog);
tvY.setText("" + (mSeekBarY.getProgress()));
mChart.resetTracking();
progress = (seekBarX.getProgress());
ArrayList<CandleEntry> yVals1 = new ArrayList<CandleEntry>();
tvX.setText(String.valueOf(progress));
tvY.setText(String.valueOf(seekBarY.getProgress()));
chart.resetTracking();
ArrayList<CandleEntry> values = new ArrayList<>();
for (int i = 0; i < progress; i++) {
float multi = (seekBarY.getProgress() + 1);
float val = (float) (Math.random() * 40) + multi;
for (int i = 0; i < prog; i++) {
float mult = (mSeekBarY.getProgress() + 1);
float val = (float) (Math.random() * 40) + mult;
float high = (float) (Math.random() * 9) + 8f;
float low = (float) (Math.random() * 9) + 8f;
float open = (float) (Math.random() * 6) + 1f;
float close = (float) (Math.random() * 6) + 1f;
boolean even = i % 2 == 0;
yVals1.add(new CandleEntry(
values.add(new CandleEntry(
i, val + high,
val - low,
even ? val + open : val - open,
@ -195,7 +122,7 @@ public class CandleStickChartActivity extends DemoBase implements OnSeekBarChang
));
}
CandleDataSet set1 = new CandleDataSet(yVals1, "Data Set");
CandleDataSet set1 = new CandleDataSet(values, "Data Set");
set1.setDrawIcons(false);
set1.setAxisDependency(AxisDependency.LEFT);
@ -210,20 +137,102 @@ public class CandleStickChartActivity extends DemoBase implements OnSeekBarChang
//set1.setHighlightLineWidth(1f);
CandleData data = new CandleData(set1);
mChart.setData(data);
mChart.invalidate();
chart.setData(data);
chart.invalidate();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.candle, menu);
return true;
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/CandleStickChartActivity.java"));
startActivity(i);
break;
}
case R.id.actionToggleValues: {
for (IDataSet set : chart.getData().getDataSets())
set.setDrawValues(!set.isDrawValuesEnabled());
chart.invalidate();
break;
}
case R.id.actionToggleIcons: {
for (IDataSet set : chart.getData().getDataSets())
set.setDrawIcons(!set.isDrawIconsEnabled());
chart.invalidate();
break;
}
case R.id.actionToggleHighlight: {
if(chart.getData() != null) {
chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled());
chart.invalidate();
}
break;
}
case R.id.actionTogglePinch: {
if (chart.isPinchZoomEnabled())
chart.setPinchZoom(false);
else
chart.setPinchZoom(true);
chart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled());
chart.notifyDataSetChanged();
break;
}
case R.id.actionToggleMakeShadowSameColorAsCandle: {
for (ICandleDataSet set : chart.getData().getDataSets()) {
((CandleDataSet) set).setShadowColorSameAsCandle(!set.getShadowColorSameAsCandle());
}
chart.invalidate();
break;
}
case R.id.animateX: {
chart.animateX(2000);
break;
}
case R.id.animateY: {
chart.animateY(2000);
break;
}
case R.id.animateXY: {
chart.animateXY(2000, 2000);
break;
}
case R.id.actionSave: {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveToGallery();
} else {
requestStoragePermission(chart);
}
break;
}
}
return true;
}
@Override
public void saveToGallery() {
saveToGallery(chart, "CandleStickChartActivity");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
}

View file

@ -1,7 +1,9 @@
package com.xxmassdeveloper.mpchartexample;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
@ -38,8 +40,8 @@ import java.util.ArrayList;
public class CombinedChartActivity extends DemoBase {
private CombinedChart mChart;
private final int itemcount = 12;
private CombinedChart chart;
private final int count = 12;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -48,41 +50,43 @@ public class CombinedChartActivity extends DemoBase {
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_combined);
mChart = findViewById(R.id.chart1);
mChart.getDescription().setEnabled(false);
mChart.setBackgroundColor(Color.WHITE);
mChart.setDrawGridBackground(false);
mChart.setDrawBarShadow(false);
mChart.setHighlightFullBarEnabled(false);
setTitle("CombinedChartActivity");
chart = findViewById(R.id.chart1);
chart.getDescription().setEnabled(false);
chart.setBackgroundColor(Color.WHITE);
chart.setDrawGridBackground(false);
chart.setDrawBarShadow(false);
chart.setHighlightFullBarEnabled(false);
// draw bars behind lines
mChart.setDrawOrder(new DrawOrder[]{
chart.setDrawOrder(new DrawOrder[]{
DrawOrder.BAR, DrawOrder.BUBBLE, DrawOrder.CANDLE, DrawOrder.LINE, DrawOrder.SCATTER
});
Legend l = mChart.getLegend();
Legend l = chart.getLegend();
l.setWordWrapEnabled(true);
l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
l.setDrawInside(false);
YAxis rightAxis = mChart.getAxisRight();
YAxis rightAxis = chart.getAxisRight();
rightAxis.setDrawGridLines(false);
rightAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
YAxis leftAxis = mChart.getAxisLeft();
YAxis leftAxis = chart.getAxisLeft();
leftAxis.setDrawGridLines(false);
leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
XAxis xAxis = mChart.getXAxis();
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxisPosition.BOTH_SIDED);
xAxis.setAxisMinimum(0f);
xAxis.setGranularity(1f);
xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return mMonths[(int) value % mMonths.length];
return months[(int) value % months.length];
}
});
@ -93,21 +97,21 @@ public class CombinedChartActivity extends DemoBase {
data.setData(generateBubbleData());
data.setData(generateScatterData());
data.setData(generateCandleData());
data.setValueTypeface(mTfLight);
data.setValueTypeface(tfLight);
xAxis.setAxisMaximum(data.getXMax() + 0.25f);
mChart.setData(data);
mChart.invalidate();
chart.setData(data);
chart.invalidate();
}
private LineData generateLineData() {
LineData d = new LineData();
ArrayList<Entry> entries = new ArrayList<Entry>();
ArrayList<Entry> entries = new ArrayList<>();
for (int index = 0; index < itemcount; index++)
for (int index = 0; index < count; index++)
entries.add(new Entry(index + 0.5f, getRandom(15, 5)));
LineDataSet set = new LineDataSet(entries, "Line DataSet");
@ -129,10 +133,10 @@ public class CombinedChartActivity extends DemoBase {
private BarData generateBarData() {
ArrayList<BarEntry> entries1 = new ArrayList<BarEntry>();
ArrayList<BarEntry> entries2 = new ArrayList<BarEntry>();
ArrayList<BarEntry> entries1 = new ArrayList<>();
ArrayList<BarEntry> entries2 = new ArrayList<>();
for (int index = 0; index < itemcount; index++) {
for (int index = 0; index < count; index++) {
entries1.add(new BarEntry(0, getRandom(25, 25)));
// stacked
@ -147,7 +151,7 @@ public class CombinedChartActivity extends DemoBase {
BarDataSet set2 = new BarDataSet(entries2, "");
set2.setStackLabels(new String[]{"Stack 1", "Stack 2"});
set2.setColors(new int[]{Color.rgb(61, 165, 255), Color.rgb(23, 197, 255)});
set2.setColors(Color.rgb(61, 165, 255), Color.rgb(23, 197, 255));
set2.setValueTextColor(Color.rgb(61, 165, 255));
set2.setValueTextSize(10f);
set2.setAxisDependency(YAxis.AxisDependency.LEFT);
@ -166,13 +170,13 @@ public class CombinedChartActivity extends DemoBase {
return d;
}
protected ScatterData generateScatterData() {
ScatterData generateScatterData() {
ScatterData d = new ScatterData();
ArrayList<Entry> entries = new ArrayList<Entry>();
ArrayList<Entry> entries = new ArrayList<>();
for (float index = 0; index < itemcount; index += 0.5f)
for (float index = 0; index < count; index += 0.5f)
entries.add(new Entry(index + 0.25f, getRandom(10, 55)));
ScatterDataSet set = new ScatterDataSet(entries, "Scatter DataSet");
@ -185,13 +189,13 @@ public class CombinedChartActivity extends DemoBase {
return d;
}
protected CandleData generateCandleData() {
CandleData generateCandleData() {
CandleData d = new CandleData();
ArrayList<CandleEntry> entries = new ArrayList<CandleEntry>();
ArrayList<CandleEntry> entries = new ArrayList<>();
for (int index = 0; index < itemcount; index += 2)
for (int index = 0; index < count; index += 2)
entries.add(new CandleEntry(index + 1f, 90, 70, 85, 75f));
CandleDataSet set = new CandleDataSet(entries, "Candle DataSet");
@ -205,13 +209,13 @@ public class CombinedChartActivity extends DemoBase {
return d;
}
protected BubbleData generateBubbleData() {
BubbleData generateBubbleData() {
BubbleData bd = new BubbleData();
ArrayList<BubbleEntry> entries = new ArrayList<BubbleEntry>();
ArrayList<BubbleEntry> entries = new ArrayList<>();
for (int index = 0; index < itemcount; index++) {
for (int index = 0; index < count; index++) {
float y = getRandom(10, 105);
float size = getRandom(100, 105);
entries.add(new BubbleEntry(index + 0.5f, y, size));
@ -237,34 +241,42 @@ public class CombinedChartActivity extends DemoBase {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/CombinedChartActivity.java"));
startActivity(i);
break;
}
case R.id.actionToggleLineValues: {
for (IDataSet set : mChart.getData().getDataSets()) {
for (IDataSet set : chart.getData().getDataSets()) {
if (set instanceof LineDataSet)
set.setDrawValues(!set.isDrawValuesEnabled());
}
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionToggleBarValues: {
for (IDataSet set : mChart.getData().getDataSets()) {
for (IDataSet set : chart.getData().getDataSets()) {
if (set instanceof BarDataSet)
set.setDrawValues(!set.isDrawValuesEnabled());
}
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionRemoveDataSet: {
int rnd = (int) getRandom(mChart.getData().getDataSetCount(), 0);
mChart.getData().removeDataSet(mChart.getData().getDataSetByIndex(rnd));
mChart.getData().notifyDataChanged();
mChart.notifyDataSetChanged();
mChart.invalidate();
int rnd = (int) getRandom(chart.getData().getDataSetCount(), 0);
chart.getData().removeDataSet(chart.getData().getDataSetByIndex(rnd));
chart.getData().notifyDataChanged();
chart.notifyDataSetChanged();
chart.invalidate();
break;
}
}
return true;
}
@Override
public void saveToGallery() { /* Intentionally left empty */ }
}

View file

@ -1,15 +1,19 @@
package com.xxmassdeveloper.mpchartexample;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
@ -28,8 +32,8 @@ import java.util.List;
public class CubicLineChartActivity extends DemoBase implements OnSeekBarChangeListener {
private LineChart mChart;
private SeekBar mSeekBarX, mSeekBarY;
private LineChart chart;
private SeekBar seekBarX, seekBarY;
private TextView tvX, tvY;
@Override
@ -39,252 +43,90 @@ public class CubicLineChartActivity extends DemoBase implements OnSeekBarChangeL
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_linechart);
setTitle("CubicLineChartActivity");
tvX = findViewById(R.id.tvXMax);
tvY = findViewById(R.id.tvYMax);
mSeekBarX = findViewById(R.id.seekBar1);
mSeekBarY = findViewById(R.id.seekBar2);
seekBarX = findViewById(R.id.seekBar1);
seekBarY = findViewById(R.id.seekBar2);
mSeekBarX.setProgress(45);
mSeekBarY.setProgress(100);
mSeekBarY.setOnSeekBarChangeListener(this);
mSeekBarX.setOnSeekBarChangeListener(this);
mChart = findViewById(R.id.chart1);
mChart.setViewPortOffsets(0, 0, 0, 0);
mChart.setBackgroundColor(Color.rgb(104, 241, 175));
chart = findViewById(R.id.chart1);
chart.setViewPortOffsets(0, 0, 0, 0);
chart.setBackgroundColor(Color.rgb(104, 241, 175));
// no description text
mChart.getDescription().setEnabled(false);
chart.getDescription().setEnabled(false);
// enable touch gestures
mChart.setTouchEnabled(true);
chart.setTouchEnabled(true);
// enable scaling and dragging
mChart.setDragEnabled(true);
mChart.setScaleEnabled(true);
chart.setDragEnabled(true);
chart.setScaleEnabled(true);
// if disabled, scaling can be done on x- and y-axis separately
mChart.setPinchZoom(false);
chart.setPinchZoom(false);
mChart.setDrawGridBackground(false);
mChart.setMaxHighlightDistance(300);
XAxis x = mChart.getXAxis();
chart.setDrawGridBackground(false);
chart.setMaxHighlightDistance(300);
XAxis x = chart.getXAxis();
x.setEnabled(false);
YAxis y = mChart.getAxisLeft();
y.setTypeface(mTfLight);
YAxis y = chart.getAxisLeft();
y.setTypeface(tfLight);
y.setLabelCount(6, false);
y.setTextColor(Color.WHITE);
y.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
y.setDrawGridLines(false);
y.setAxisLineColor(Color.WHITE);
mChart.getAxisRight().setEnabled(false);
chart.getAxisRight().setEnabled(false);
// add data
seekBarY.setOnSeekBarChangeListener(this);
seekBarX.setOnSeekBarChangeListener(this);
// lower max, as cubic runs significantly slower than linear
seekBarX.setMax(700);
seekBarX.setProgress(45);
seekBarY.setProgress(100);
setData(45, 100);
mChart.getLegend().setEnabled(false);
mChart.animateXY(2000, 2000);
// dont forget to refresh the drawing
mChart.invalidate();
}
chart.getLegend().setEnabled(false);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.line, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.actionToggleValues: {
for (IDataSet set : mChart.getData().getDataSets())
set.setDrawValues(!set.isDrawValuesEnabled());
mChart.invalidate();
break;
}
case R.id.actionToggleHighlight: {
if(mChart.getData() != null) {
mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled());
mChart.invalidate();
}
break;
}
case R.id.actionToggleFilled: {
List<ILineDataSet> sets = mChart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
if (set.isDrawFilledEnabled())
set.setDrawFilled(false);
else
set.setDrawFilled(true);
}
mChart.invalidate();
break;
}
case R.id.actionToggleCircles: {
List<ILineDataSet> sets = mChart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
if (set.isDrawCirclesEnabled())
set.setDrawCircles(false);
else
set.setDrawCircles(true);
}
mChart.invalidate();
break;
}
case R.id.actionToggleCubic: {
List<ILineDataSet> sets = mChart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
set.setMode(set.getMode() == LineDataSet.Mode.CUBIC_BEZIER
? LineDataSet.Mode.LINEAR
: LineDataSet.Mode.CUBIC_BEZIER);
}
mChart.invalidate();
break;
}
case R.id.actionToggleStepped: {
List<ILineDataSet> sets = mChart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
set.setMode(set.getMode() == LineDataSet.Mode.STEPPED
? LineDataSet.Mode.LINEAR
: LineDataSet.Mode.STEPPED);
}
mChart.invalidate();
break;
}
case R.id.actionToggleHorizontalCubic: {
List<ILineDataSet> sets = mChart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
set.setMode(set.getMode() == LineDataSet.Mode.HORIZONTAL_BEZIER
? LineDataSet.Mode.LINEAR
: LineDataSet.Mode.HORIZONTAL_BEZIER);
}
mChart.invalidate();
break;
}
case R.id.actionTogglePinch: {
if (mChart.isPinchZoomEnabled())
mChart.setPinchZoom(false);
else
mChart.setPinchZoom(true);
mChart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
break;
}
case R.id.animateX: {
mChart.animateX(3000);
break;
}
case R.id.animateY: {
mChart.animateY(3000);
break;
}
case R.id.animateXY: {
mChart.animateXY(3000, 3000);
break;
}
case R.id.actionSave: {
if (mChart.saveToPath("title" + System.currentTimeMillis(), "")) {
Toast.makeText(getApplicationContext(), "Saving SUCCESSFUL!",
Toast.LENGTH_SHORT).show();
} else
Toast.makeText(getApplicationContext(), "Saving FAILED!", Toast.LENGTH_SHORT)
.show();
// mChart.saveToGallery("title"+System.currentTimeMillis())
break;
}
}
return true;
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvX.setText("" + (mSeekBarX.getProgress() + 1));
tvY.setText("" + (mSeekBarY.getProgress()));
setData(mSeekBarX.getProgress() + 1, mSeekBarY.getProgress());
// redraw
mChart.invalidate();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
chart.animateXY(2000, 2000);
// don't forget to refresh the drawing
chart.invalidate();
}
private void setData(int count, float range) {
ArrayList<Entry> yVals = new ArrayList<Entry>();
ArrayList<Entry> values = new ArrayList<>();
for (int i = 0; i < count; i++) {
float mult = (range + 1);
float val = (float) (Math.random() * mult) + 20;// + (float)
// ((mult *
// 0.1) / 10);
yVals.add(new Entry(i, val));
float val = (float) (Math.random() * (range + 1)) + 20;
values.add(new Entry(i, val));
}
LineDataSet set1;
if (mChart.getData() != null &&
mChart.getData().getDataSetCount() > 0) {
set1 = (LineDataSet)mChart.getData().getDataSetByIndex(0);
set1.setValues(yVals);
mChart.getData().notifyDataChanged();
mChart.notifyDataSetChanged();
if (chart.getData() != null &&
chart.getData().getDataSetCount() > 0) {
set1 = (LineDataSet) chart.getData().getDataSetByIndex(0);
set1.setValues(values);
chart.getData().notifyDataChanged();
chart.notifyDataSetChanged();
} else {
// create a dataset and give it a type
set1 = new LineDataSet(yVals, "DataSet 1");
set1 = new LineDataSet(values, "DataSet 1");
set1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
set1.setCubicIntensity(0.2f);
//set1.setDrawFilled(true);
set1.setDrawFilled(true);
set1.setDrawCircles(false);
set1.setLineWidth(1.8f);
set1.setCircleRadius(4f);
@ -297,18 +139,183 @@ public class CubicLineChartActivity extends DemoBase implements OnSeekBarChangeL
set1.setFillFormatter(new IFillFormatter() {
@Override
public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
return -10;
return chart.getAxisLeft().getAxisMinimum();
}
});
// create a data object with the datasets
// create a data object with the data sets
LineData data = new LineData(set1);
data.setValueTypeface(mTfLight);
data.setValueTypeface(tfLight);
data.setValueTextSize(9f);
data.setDrawValues(false);
// set data
mChart.setData(data);
chart.setData(data);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.line, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/CubicLineChartActivity.java"));
startActivity(i);
break;
}
case R.id.actionToggleValues: {
for (IDataSet set : chart.getData().getDataSets())
set.setDrawValues(!set.isDrawValuesEnabled());
chart.invalidate();
break;
}
case R.id.actionToggleHighlight: {
if(chart.getData() != null) {
chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled());
chart.invalidate();
}
break;
}
case R.id.actionToggleFilled: {
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
if (set.isDrawFilledEnabled())
set.setDrawFilled(false);
else
set.setDrawFilled(true);
}
chart.invalidate();
break;
}
case R.id.actionToggleCircles: {
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
if (set.isDrawCirclesEnabled())
set.setDrawCircles(false);
else
set.setDrawCircles(true);
}
chart.invalidate();
break;
}
case R.id.actionToggleCubic: {
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
set.setMode(set.getMode() == LineDataSet.Mode.CUBIC_BEZIER
? LineDataSet.Mode.LINEAR
: LineDataSet.Mode.CUBIC_BEZIER);
}
chart.invalidate();
break;
}
case R.id.actionToggleStepped: {
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
set.setMode(set.getMode() == LineDataSet.Mode.STEPPED
? LineDataSet.Mode.LINEAR
: LineDataSet.Mode.STEPPED);
}
chart.invalidate();
break;
}
case R.id.actionToggleHorizontalCubic: {
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
set.setMode(set.getMode() == LineDataSet.Mode.HORIZONTAL_BEZIER
? LineDataSet.Mode.LINEAR
: LineDataSet.Mode.HORIZONTAL_BEZIER);
}
chart.invalidate();
break;
}
case R.id.actionTogglePinch: {
if (chart.isPinchZoomEnabled())
chart.setPinchZoom(false);
else
chart.setPinchZoom(true);
chart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled());
chart.notifyDataSetChanged();
break;
}
case R.id.animateX: {
chart.animateX(2000);
break;
}
case R.id.animateY: {
chart.animateY(2000);
break;
}
case R.id.animateXY: {
chart.animateXY(2000, 2000);
break;
}
case R.id.actionSave: {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveToGallery();
} else {
requestStoragePermission(chart);
}
break;
}
}
return true;
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvX.setText(String.valueOf(seekBarX.getProgress()));
tvY.setText(String.valueOf(seekBarY.getProgress()));
setData(seekBarX.getProgress(), seekBarY.getProgress());
// redraw
chart.invalidate();
}
@Override
public void saveToGallery() {
saveToGallery(chart, "CubicLineChartActivity");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
}

View file

@ -1,8 +1,10 @@
// TODO: Finish and add to main activity list
package com.xxmassdeveloper.mpchartexample;
import android.graphics.Typeface;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
@ -28,13 +30,13 @@ import java.util.List;
/**
* This Activity demonstrates drawing into the Chart with the finger. Both line,
* bar and scatter charts can be used for drawing.
*
*
* @author Philipp Jahoda
*/
public class DrawChartActivity extends DemoBase implements OnChartValueSelectedListener,
OnDrawListener {
private LineChart mChart;
private LineChart chart;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -43,49 +45,49 @@ public class DrawChartActivity extends DemoBase implements OnChartValueSelectedL
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_draw_chart);
mChart = findViewById(R.id.chart1);
setTitle("DrawChartActivity");
chart = findViewById(R.id.chart1);
// listener for selecting and drawing
mChart.setOnChartValueSelectedListener(this);
mChart.setOnDrawListener(this);
chart.setOnChartValueSelectedListener(this);
chart.setOnDrawListener(this);
// if disabled, drawn datasets with the finger will not be automatically
// if disabled, drawn data sets with the finger will not be automatically
// finished
// mChart.setAutoFinish(true);
mChart.setDrawGridBackground(false);
// chart.setAutoFinish(true);
chart.setDrawGridBackground(false);
// add dummy-data to the chart
initWithDummyData();
Typeface tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");
XAxis xl = mChart.getXAxis();
xl.setTypeface(tf);
XAxis xl = chart.getXAxis();
xl.setTypeface(tfRegular);
xl.setAvoidFirstLastClipping(true);
YAxis yl = mChart.getAxisLeft();
yl.setTypeface(tf);
YAxis yl = chart.getAxisLeft();
yl.setTypeface(tfRegular);
mChart.getLegend().setEnabled(false);
chart.getLegend().setEnabled(false);
// mChart.setYRange(-40f, 40f, true);
// chart.setYRange(-40f, 40f, true);
// call this to reset the changed y-range
// mChart.resetYRange(true);
// chart.resetYRange(true);
}
private void initWithDummyData() {
ArrayList<Entry> yVals = new ArrayList<Entry>();
ArrayList<Entry> values = new ArrayList<>();
// create a dataset and give it a type (0)
LineDataSet set1 = new LineDataSet(yVals, "DataSet");
LineDataSet set1 = new LineDataSet(values, "DataSet");
set1.setLineWidth(3f);
set1.setCircleRadius(5f);
// create a data object with the datasets
// create a data object with the data sets
LineData data = new LineData(set1);
mChart.setData(data);
chart.setData(data);
}
@Override
@ -99,7 +101,7 @@ public class DrawChartActivity extends DemoBase implements OnChartValueSelectedL
switch (item.getItemId()) {
case R.id.actionToggleValues: {
List<ILineDataSet> sets = mChart.getData()
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
@ -108,39 +110,47 @@ public class DrawChartActivity extends DemoBase implements OnChartValueSelectedL
set.setDrawValues(!set.isDrawValuesEnabled());
}
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionToggleHighlight: {
if(mChart.getData() != null) {
mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled());
mChart.invalidate();
if(chart.getData() != null) {
chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled());
chart.invalidate();
}
break;
}
case R.id.actionTogglePinch: {
if (mChart.isPinchZoomEnabled())
mChart.setPinchZoom(false);
if (chart.isPinchZoomEnabled())
chart.setPinchZoom(false);
else
mChart.setPinchZoom(true);
chart.setPinchZoom(true);
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled());
chart.notifyDataSetChanged();
break;
}
case R.id.actionSave: {
// mChart.saveToGallery("title"+System.currentTimeMillis());
mChart.saveToPath("title" + System.currentTimeMillis(), "");
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveToGallery();
} else {
requestStoragePermission(chart);
}
break;
}
}
return true;
}
@Override
public void saveToGallery() {
saveToGallery(chart, "DrawChartActivity");
}
@Override
public void onValueSelected(Entry e, Highlight h) {
Log.i("VAL SELECTED",
@ -164,7 +174,7 @@ public class DrawChartActivity extends DemoBase implements OnChartValueSelectedL
Log.i(Chart.LOG_TAG, "DataSet drawn. " + dataSet.toSimpleString());
// prepare the legend again
mChart.getLegendRenderer().computeLegend(mChart.getData());
chart.getLegendRenderer().computeLegend(chart.getData());
}
@Override

View file

@ -1,8 +1,13 @@
package com.xxmassdeveloper.mpchartexample;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
@ -23,7 +28,7 @@ import java.util.ArrayList;
public class DynamicalAddingActivity extends DemoBase implements OnChartValueSelectedListener {
private LineChart mChart;
private LineChart chart;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -32,24 +37,30 @@ public class DynamicalAddingActivity extends DemoBase implements OnChartValueSel
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_linechart_noseekbar);
mChart = findViewById(R.id.chart1);
mChart.setOnChartValueSelectedListener(this);
mChart.setDrawGridBackground(false);
mChart.getDescription().setEnabled(false);
setTitle("DynamicalAddingActivity");
// add an empty data object
mChart.setData(new LineData());
// mChart.getXAxis().setDrawLabels(false);
// mChart.getXAxis().setDrawGridLines(false);
chart = findViewById(R.id.chart1);
chart.setOnChartValueSelectedListener(this);
chart.setDrawGridBackground(false);
chart.getDescription().setEnabled(false);
chart.setNoDataText("No chart data available. Use the menu to add entries and data sets!");
mChart.invalidate();
// chart.getXAxis().setDrawLabels(false);
// chart.getXAxis().setDrawGridLines(false);
chart.invalidate();
}
int[] mColors = ColorTemplate.VORDIPLOM_COLORS;
private final int[] colors = ColorTemplate.VORDIPLOM_COLORS;
private void addEntry() {
LineData data = mChart.getData();
LineData data = chart.getData();
if (data == null) {
data = new LineData();
chart.setData(data);
}
ILineDataSet set = data.getDataSetByIndex(0);
// set.addEntry(...); // can be called as well
@ -61,25 +72,26 @@ public class DynamicalAddingActivity extends DemoBase implements OnChartValueSel
// choose a random dataSet
int randomDataSetIndex = (int) (Math.random() * data.getDataSetCount());
float yValue = (float) (Math.random() * 10) + 50f;
ILineDataSet randomSet = data.getDataSetByIndex(randomDataSetIndex);
float value = (float) (Math.random() * 50) + 50f * (randomDataSetIndex + 1);
data.addEntry(new Entry(data.getDataSetByIndex(randomDataSetIndex).getEntryCount(), yValue), randomDataSetIndex);
data.addEntry(new Entry(randomSet.getEntryCount(), value), randomDataSetIndex);
data.notifyDataChanged();
// let the chart know it's data has changed
mChart.notifyDataSetChanged();
chart.notifyDataSetChanged();
mChart.setVisibleXRangeMaximum(6);
//mChart.setVisibleYRangeMaximum(15, AxisDependency.LEFT);
//
chart.setVisibleXRangeMaximum(6);
//chart.setVisibleYRangeMaximum(15, AxisDependency.LEFT);
//
// // this automatically refreshes the chart (calls invalidate())
mChart.moveViewTo(data.getEntryCount() - 7, 50f, AxisDependency.LEFT);
chart.moveViewTo(data.getEntryCount() - 7, 50f, AxisDependency.LEFT);
}
private void removeLastEntry() {
LineData data = mChart.getData();
LineData data = chart.getData();
if (data != null) {
@ -93,31 +105,33 @@ public class DynamicalAddingActivity extends DemoBase implements OnChartValueSel
// or remove by index
// mData.removeEntryByXValue(xIndex, dataSetIndex);
data.notifyDataChanged();
mChart.notifyDataSetChanged();
mChart.invalidate();
chart.notifyDataSetChanged();
chart.invalidate();
}
}
}
private void addDataSet() {
LineData data = mChart.getData();
if (data != null) {
LineData data = chart.getData();
if (data == null) {
chart.setData(new LineData());
} else {
int count = (data.getDataSetCount() + 1);
int amount = data.getDataSetByIndex(0).getEntryCount();
ArrayList<Entry> yVals = new ArrayList<Entry>();
ArrayList<Entry> values = new ArrayList<>();
for (int i = 0; i < data.getEntryCount(); i++) {
yVals.add(new Entry(i, (float) (Math.random() * 50f) + 50f * count));
for (int i = 0; i < amount; i++) {
values.add(new Entry(i, (float) (Math.random() * 50f) + 50f * count));
}
LineDataSet set = new LineDataSet(yVals, "DataSet " + count);
LineDataSet set = new LineDataSet(values, "DataSet " + count);
set.setLineWidth(2.5f);
set.setCircleRadius(4.5f);
int color = mColors[count % mColors.length];
int color = colors[count % colors.length];
set.setColor(color);
set.setCircleColor(color);
@ -127,74 +141,24 @@ public class DynamicalAddingActivity extends DemoBase implements OnChartValueSel
data.addDataSet(set);
data.notifyDataChanged();
mChart.notifyDataSetChanged();
mChart.invalidate();
chart.notifyDataSetChanged();
chart.invalidate();
}
}
private void removeDataSet() {
LineData data = mChart.getData();
LineData data = chart.getData();
if (data != null) {
data.removeDataSet(data.getDataSetByIndex(data.getDataSetCount() - 1));
mChart.notifyDataSetChanged();
mChart.invalidate();
chart.notifyDataSetChanged();
chart.invalidate();
}
}
@Override
public void onValueSelected(Entry e, Highlight h) {
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected() {
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.dynamical, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.actionAddEntry:
addEntry();
Toast.makeText(this, "Entry added!", Toast.LENGTH_SHORT).show();
break;
case R.id.actionRemoveEntry:
removeLastEntry();
Toast.makeText(this, "Entry removed!", Toast.LENGTH_SHORT).show();
break;
case R.id.actionAddDataSet:
addDataSet();
Toast.makeText(this, "DataSet added!", Toast.LENGTH_SHORT).show();
break;
case R.id.actionRemoveDataSet:
removeDataSet();
Toast.makeText(this, "DataSet removed!", Toast.LENGTH_SHORT).show();
break;
case R.id.actionAddEmptyLineData:
mChart.setData(new LineData());
mChart.invalidate();
Toast.makeText(this, "Empty data added!", Toast.LENGTH_SHORT).show();
break;
case R.id.actionClear:
mChart.clear();
Toast.makeText(this, "Chart cleared!", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
private LineDataSet createSet() {
LineDataSet set = new LineDataSet(null, "DataSet 1");
@ -208,4 +172,71 @@ public class DynamicalAddingActivity extends DemoBase implements OnChartValueSel
return set;
}
@Override
public void onValueSelected(Entry e, Highlight h) {
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected() {}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.dynamical, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/DynamicalAddingActivity.java"));
startActivity(i);
break;
}
case R.id.actionAddEntry: {
addEntry();
Toast.makeText(this, "Entry added!", Toast.LENGTH_SHORT).show();
break;
}
case R.id.actionRemoveEntry: {
removeLastEntry();
Toast.makeText(this, "Entry removed!", Toast.LENGTH_SHORT).show();
break;
}
case R.id.actionAddDataSet: {
addDataSet();
Toast.makeText(this, "DataSet added!", Toast.LENGTH_SHORT).show();
break;
}
case R.id.actionRemoveDataSet: {
removeDataSet();
Toast.makeText(this, "DataSet removed!", Toast.LENGTH_SHORT).show();
break;
}
case R.id.actionClear: {
chart.clear();
Toast.makeText(this, "Chart cleared!", Toast.LENGTH_SHORT).show();
break;
}
case R.id.actionSave: {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveToGallery();
} else {
requestStoragePermission(chart);
}
break;
}
}
return true;
}
@Override
public void saveToGallery() {
saveToGallery(chart, "DynamicalAddingActivity");
}
}

View file

@ -1,8 +1,12 @@
package com.xxmassdeveloper.mpchartexample;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import com.github.mikephil.charting.charts.LineChart;
@ -19,10 +23,19 @@ import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
import java.util.ArrayList;
/**
* This works by inverting the background and desired "fill" color. First, we draw the fill color
* that we want between the lines as the actual background of the chart. Then, we fill the area
* above the highest line and the area under the lowest line with the desired background color.
*
* This method makes it look like we filled the area between the lines, but really we are filling
* the area OUTSIDE the lines!
*/
@SuppressWarnings("SameParameterValue")
public class FilledLineActivity extends DemoBase {
private LineChart mChart;
private int mFillColor = Color.argb(150, 51, 181, 229);
private LineChart chart;
private final int fillColor = Color.argb(150, 51, 181, 229);
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -31,73 +44,71 @@ public class FilledLineActivity extends DemoBase {
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_linechart_noseekbar);
mChart = findViewById(R.id.chart1);
mChart.setBackgroundColor(Color.WHITE);
mChart.setGridBackgroundColor(mFillColor);
mChart.setDrawGridBackground(true);
setTitle("FilledLineActivity");
mChart.setDrawBorders(true);
chart = findViewById(R.id.chart1);
chart.setBackgroundColor(Color.WHITE);
chart.setGridBackgroundColor(fillColor);
chart.setDrawGridBackground(true);
chart.setDrawBorders(true);
// no description text
mChart.getDescription().setEnabled(false);
chart.getDescription().setEnabled(false);
// if disabled, scaling can be done on x- and y-axis separately
mChart.setPinchZoom(false);
chart.setPinchZoom(false);
Legend l = mChart.getLegend();
Legend l = chart.getLegend();
l.setEnabled(false);
XAxis xAxis = mChart.getXAxis();
XAxis xAxis = chart.getXAxis();
xAxis.setEnabled(false);
YAxis leftAxis = mChart.getAxisLeft();
YAxis leftAxis = chart.getAxisLeft();
leftAxis.setAxisMaximum(900f);
leftAxis.setAxisMinimum(-250f);
leftAxis.setDrawAxisLine(false);
leftAxis.setDrawZeroLine(false);
leftAxis.setDrawGridLines(false);
mChart.getAxisRight().setEnabled(false);
chart.getAxisRight().setEnabled(false);
// add data
setData(100, 60);
mChart.invalidate();
chart.invalidate();
}
private void setData(int count, float range) {
ArrayList<Entry> yVals1 = new ArrayList<Entry>();
ArrayList<Entry> values1 = new ArrayList<>();
for (int i = 0; i < count; i++) {
float val = (float) (Math.random() * range) + 50;// + (float)
// ((mult *
// 0.1) / 10);
yVals1.add(new Entry(i, val));
float val = (float) (Math.random() * range) + 50;
values1.add(new Entry(i, val));
}
ArrayList<Entry> yVals2 = new ArrayList<Entry>();
ArrayList<Entry> values2 = new ArrayList<>();
for (int i = 0; i < count; i++) {
float val = (float) (Math.random() * range) + 450;// + (float)
// ((mult *
// 0.1) / 10);
yVals2.add(new Entry(i, val));
float val = (float) (Math.random() * range) + 450;
values2.add(new Entry(i, val));
}
LineDataSet set1, set2;
if (mChart.getData() != null &&
mChart.getData().getDataSetCount() > 0) {
set1 = (LineDataSet)mChart.getData().getDataSetByIndex(0);
set2 = (LineDataSet)mChart.getData().getDataSetByIndex(1);
set1.setValues(yVals1);
set2.setValues(yVals2);
mChart.getData().notifyDataChanged();
mChart.notifyDataSetChanged();
if (chart.getData() != null &&
chart.getData().getDataSetCount() > 0) {
set1 = (LineDataSet) chart.getData().getDataSetByIndex(0);
set2 = (LineDataSet) chart.getData().getDataSetByIndex(1);
set1.setValues(values1);
set2.setValues(values2);
chart.getData().notifyDataChanged();
chart.notifyDataSetChanged();
} else {
// create a dataset and give it a type
set1 = new LineDataSet(yVals1, "DataSet 1");
set1 = new LineDataSet(values1, "DataSet 1");
set1.setAxisDependency(YAxis.AxisDependency.LEFT);
set1.setColor(Color.rgb(255, 241, 46));
@ -112,12 +123,14 @@ public class FilledLineActivity extends DemoBase {
set1.setFillFormatter(new IFillFormatter() {
@Override
public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
return mChart.getAxisLeft().getAxisMinimum();
// change the return value here to better understand the effect
// return 0;
return chart.getAxisLeft().getAxisMinimum();
}
});
// create a dataset and give it a type
set2 = new LineDataSet(yVals2, "DataSet 2");
set2 = new LineDataSet(values2, "DataSet 2");
set2.setAxisDependency(YAxis.AxisDependency.LEFT);
set2.setColor(Color.rgb(255, 241, 46));
set2.setDrawCircles(false);
@ -131,20 +144,46 @@ public class FilledLineActivity extends DemoBase {
set2.setFillFormatter(new IFillFormatter() {
@Override
public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
return mChart.getAxisLeft().getAxisMaximum();
// change the return value here to better understand the effect
// return 600;
return chart.getAxisLeft().getAxisMaximum();
}
});
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
dataSets.add(set1); // add the datasets
ArrayList<ILineDataSet> dataSets = new ArrayList<>();
dataSets.add(set1); // add the data sets
dataSets.add(set2);
// create a data object with the datasets
// create a data object with the data sets
LineData data = new LineData(dataSets);
data.setDrawValues(false);
// set data
mChart.setData(data);
chart.setData(data);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.only_github, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/FilledLineActivity.java"));
startActivity(i);
break;
}
}
return true;
}
@Override
public void saveToGallery() { /* Intentionally left empty */ }
}

View file

@ -1,22 +1,24 @@
package com.xxmassdeveloper.mpchartexample;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.view.Display;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.Legend.LegendPosition;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
@ -26,9 +28,10 @@ import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
import java.util.ArrayList;
@SuppressWarnings("SameParameterValue")
public class HalfPieChartActivity extends DemoBase {
private PieChart mChart;
private PieChart chart;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -37,40 +40,42 @@ public class HalfPieChartActivity extends DemoBase {
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_piechart_half);
mChart = findViewById(R.id.chart1);
mChart.setBackgroundColor(Color.WHITE);
setTitle("HalfPieChartActivity");
chart = findViewById(R.id.chart1);
chart.setBackgroundColor(Color.WHITE);
moveOffScreen();
mChart.setUsePercentValues(true);
mChart.getDescription().setEnabled(false);
chart.setUsePercentValues(true);
chart.getDescription().setEnabled(false);
mChart.setCenterTextTypeface(mTfLight);
mChart.setCenterText(generateCenterSpannableText());
chart.setCenterTextTypeface(tfLight);
chart.setCenterText(generateCenterSpannableText());
mChart.setDrawHoleEnabled(true);
mChart.setHoleColor(Color.WHITE);
chart.setDrawHoleEnabled(true);
chart.setHoleColor(Color.WHITE);
mChart.setTransparentCircleColor(Color.WHITE);
mChart.setTransparentCircleAlpha(110);
chart.setTransparentCircleColor(Color.WHITE);
chart.setTransparentCircleAlpha(110);
mChart.setHoleRadius(58f);
mChart.setTransparentCircleRadius(61f);
chart.setHoleRadius(58f);
chart.setTransparentCircleRadius(61f);
mChart.setDrawCenterText(true);
chart.setDrawCenterText(true);
mChart.setRotationEnabled(false);
mChart.setHighlightPerTapEnabled(true);
chart.setRotationEnabled(false);
chart.setHighlightPerTapEnabled(true);
mChart.setMaxAngle(180f); // HALF CHART
mChart.setRotationAngle(180f);
mChart.setCenterTextOffset(0, -20);
chart.setMaxAngle(180f); // HALF CHART
chart.setRotationAngle(180f);
chart.setCenterTextOffset(0, -20);
setData(4, 100);
mChart.animateY(1400, Easing.EaseInOutQuad);
chart.animateY(1400, Easing.EaseInOutQuad);
Legend l = mChart.getLegend();
Legend l = chart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
@ -80,17 +85,17 @@ public class HalfPieChartActivity extends DemoBase {
l.setYOffset(0f);
// entry label styling
mChart.setEntryLabelColor(Color.WHITE);
mChart.setEntryLabelTypeface(mTfRegular);
mChart.setEntryLabelTextSize(12f);
chart.setEntryLabelColor(Color.WHITE);
chart.setEntryLabelTypeface(tfRegular);
chart.setEntryLabelTextSize(12f);
}
private void setData(int count, float range) {
ArrayList<PieEntry> values = new ArrayList<PieEntry>();
ArrayList<PieEntry> values = new ArrayList<>();
for (int i = 0; i < count; i++) {
values.add(new PieEntry((float) ((Math.random() * range) + range / 5), mParties[i % mParties.length]));
values.add(new PieEntry((float) ((Math.random() * range) + range / 5), parties[i % parties.length]));
}
PieDataSet dataSet = new PieDataSet(values, "Election Results");
@ -104,10 +109,10 @@ public class HalfPieChartActivity extends DemoBase {
data.setValueFormatter(new PercentFormatter());
data.setValueTextSize(11f);
data.setValueTextColor(Color.WHITE);
data.setValueTypeface(mTfLight);
mChart.setData(data);
data.setValueTypeface(tfLight);
chart.setData(data);
mChart.invalidate();
chart.invalidate();
}
private SpannableString generateCenterSpannableText() {
@ -124,14 +129,40 @@ public class HalfPieChartActivity extends DemoBase {
private void moveOffScreen() {
Display display = getWindowManager().getDefaultDisplay();
int height = display.getHeight(); // deprecated
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int offset = (int)(height * 0.65); /* percent to move */
RelativeLayout.LayoutParams rlParams =
(RelativeLayout.LayoutParams)mChart.getLayoutParams();
(RelativeLayout.LayoutParams) chart.getLayoutParams();
rlParams.setMargins(0, 0, 0, -offset);
mChart.setLayoutParams(rlParams);
chart.setLayoutParams(rlParams);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.only_github, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/HalfPieChartActivity.java"));
startActivity(i);
break;
}
}
return true;
}
@Override
public void saveToGallery() { /* Intentionally left empty */ }
}

View file

@ -1,9 +1,13 @@
package com.xxmassdeveloper.mpchartexample;
import android.annotation.SuppressLint;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.RectF;
import android.net.Uri;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
@ -11,11 +15,9 @@ import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.Legend.LegendPosition;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
import com.github.mikephil.charting.components.YAxis;
@ -35,8 +37,8 @@ import java.util.List;
public class HorizontalBarChartActivity extends DemoBase implements OnSeekBarChangeListener,
OnChartValueSelectedListener {
protected HorizontalBarChart mChart;
private SeekBar mSeekBarX, mSeekBarY;
private HorizontalBarChart chart;
private SeekBar seekBarX, seekBarY;
private TextView tvX, tvY;
@Override
@ -46,67 +48,69 @@ public class HorizontalBarChartActivity extends DemoBase implements OnSeekBarCha
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_horizontalbarchart);
setTitle("HorizontalBarChartActivity");
tvX = findViewById(R.id.tvXMax);
tvY = findViewById(R.id.tvYMax);
mSeekBarX = findViewById(R.id.seekBar1);
mSeekBarY = findViewById(R.id.seekBar2);
seekBarX = findViewById(R.id.seekBar1);
seekBarY = findViewById(R.id.seekBar2);
mChart = findViewById(R.id.chart1);
mChart.setOnChartValueSelectedListener(this);
// mChart.setHighlightEnabled(false);
seekBarY.setOnSeekBarChangeListener(this);
seekBarX.setOnSeekBarChangeListener(this);
mChart.setDrawBarShadow(false);
chart = findViewById(R.id.chart1);
chart.setOnChartValueSelectedListener(this);
// chart.setHighlightEnabled(false);
mChart.setDrawValueAboveBar(true);
chart.setDrawBarShadow(false);
mChart.getDescription().setEnabled(false);
chart.setDrawValueAboveBar(true);
chart.getDescription().setEnabled(false);
// if more than 60 entries are displayed in the chart, no values will be
// drawn
mChart.setMaxVisibleValueCount(60);
chart.setMaxVisibleValueCount(60);
// scaling can now only be done on x- and y-axis separately
mChart.setPinchZoom(false);
chart.setPinchZoom(false);
// draw shadows for each bar that show the maximum value
// mChart.setDrawBarShadow(true);
// chart.setDrawBarShadow(true);
mChart.setDrawGridBackground(false);
chart.setDrawGridBackground(false);
XAxis xl = mChart.getXAxis();
XAxis xl = chart.getXAxis();
xl.setPosition(XAxisPosition.BOTTOM);
xl.setTypeface(mTfLight);
xl.setTypeface(tfLight);
xl.setDrawAxisLine(true);
xl.setDrawGridLines(false);
xl.setGranularity(10f);
YAxis yl = mChart.getAxisLeft();
yl.setTypeface(mTfLight);
YAxis yl = chart.getAxisLeft();
yl.setTypeface(tfLight);
yl.setDrawAxisLine(true);
yl.setDrawGridLines(true);
yl.setAxisMinimum(0f); // this replaces setStartAtZero(true)
// yl.setInverted(true);
YAxis yr = mChart.getAxisRight();
yr.setTypeface(mTfLight);
YAxis yr = chart.getAxisRight();
yr.setTypeface(tfLight);
yr.setDrawAxisLine(true);
yr.setDrawGridLines(false);
yr.setAxisMinimum(0f); // this replaces setStartAtZero(true)
// yr.setInverted(true);
setData(12, 50);
mChart.setFitBars(true);
mChart.animateY(2500);
chart.setFitBars(true);
chart.animateY(2500);
// setting data
mSeekBarY.setProgress(50);
mSeekBarX.setProgress(12);
seekBarY.setProgress(50);
seekBarX.setProgress(12);
mSeekBarY.setOnSeekBarChangeListener(this);
mSeekBarX.setOnSeekBarChangeListener(this);
Legend l = mChart.getLegend();
Legend l = chart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
@ -115,6 +119,42 @@ public class HorizontalBarChartActivity extends DemoBase implements OnSeekBarCha
l.setXEntrySpace(4f);
}
private void setData(int count, float range) {
float barWidth = 9f;
float spaceForBar = 10f;
ArrayList<BarEntry> values = new ArrayList<>();
for (int i = 0; i < count; i++) {
float val = (float) (Math.random() * range);
values.add(new BarEntry(i * spaceForBar, val,
getResources().getDrawable(R.drawable.star)));
}
BarDataSet set1;
if (chart.getData() != null &&
chart.getData().getDataSetCount() > 0) {
set1 = (BarDataSet) chart.getData().getDataSetByIndex(0);
set1.setValues(values);
chart.getData().notifyDataChanged();
chart.notifyDataSetChanged();
} else {
set1 = new BarDataSet(values, "DataSet 1");
set1.setDrawIcons(false);
ArrayList<IBarDataSet> dataSets = new ArrayList<>();
dataSets.add(set1);
BarData data = new BarData(dataSets);
data.setValueTextSize(10f);
data.setValueTypeface(tfLight);
data.setBarWidth(barWidth);
chart.setData(data);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.bar, menu);
@ -125,80 +165,80 @@ public class HorizontalBarChartActivity extends DemoBase implements OnSeekBarCha
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/HorizontalBarChartActivity.java"));
startActivity(i);
break;
}
case R.id.actionToggleValues: {
List<IBarDataSet> sets = mChart.getData()
List<IBarDataSet> sets = chart.getData()
.getDataSets();
for (IBarDataSet iSet : sets) {
IBarDataSet set = (BarDataSet) iSet;
set.setDrawValues(!set.isDrawValuesEnabled());
iSet.setDrawValues(!iSet.isDrawValuesEnabled());
}
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionToggleIcons: {
List<IBarDataSet> sets = mChart.getData()
List<IBarDataSet> sets = chart.getData()
.getDataSets();
for (IBarDataSet iSet : sets) {
IBarDataSet set = (BarDataSet) iSet;
set.setDrawIcons(!set.isDrawIconsEnabled());
iSet.setDrawIcons(!iSet.isDrawIconsEnabled());
}
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionToggleHighlight: {
if(mChart.getData() != null) {
mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled());
mChart.invalidate();
if(chart.getData() != null) {
chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled());
chart.invalidate();
}
break;
}
case R.id.actionTogglePinch: {
if (mChart.isPinchZoomEnabled())
mChart.setPinchZoom(false);
if (chart.isPinchZoomEnabled())
chart.setPinchZoom(false);
else
mChart.setPinchZoom(true);
chart.setPinchZoom(true);
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled());
chart.notifyDataSetChanged();
break;
}
case R.id.actionToggleBarBorders: {
for (IBarDataSet set : mChart.getData().getDataSets())
for (IBarDataSet set : chart.getData().getDataSets())
((BarDataSet)set).setBarBorderWidth(set.getBarBorderWidth() == 1.f ? 0.f : 1.f);
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.animateX: {
mChart.animateX(3000);
chart.animateX(2000);
break;
}
case R.id.animateY: {
mChart.animateY(3000);
chart.animateY(2000);
break;
}
case R.id.animateXY: {
mChart.animateXY(3000, 3000);
chart.animateXY(2000, 2000);
break;
}
case R.id.actionSave: {
if (mChart.saveToGallery("title" + System.currentTimeMillis(), 50)) {
Toast.makeText(getApplicationContext(), "Saving SUCCESSFUL!",
Toast.LENGTH_SHORT).show();
} else
Toast.makeText(getApplicationContext(), "Saving FAILED!", Toast.LENGTH_SHORT)
.show();
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveToGallery();
} else {
requestStoragePermission(chart);
}
break;
}
}
@ -208,64 +248,27 @@ public class HorizontalBarChartActivity extends DemoBase implements OnSeekBarCha
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvX.setText("" + (mSeekBarX.getProgress() + 1));
tvY.setText("" + (mSeekBarY.getProgress()));
tvX.setText(String.valueOf(seekBarX.getProgress()));
tvY.setText(String.valueOf(seekBarY.getProgress()));
setData(mSeekBarX.getProgress() + 1, mSeekBarY.getProgress());
mChart.setFitBars(true);
mChart.invalidate();
setData(seekBarX.getProgress(), seekBarY.getProgress());
chart.setFitBars(true);
chart.invalidate();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
public void saveToGallery() {
saveToGallery(chart, "HorizontalBarChartActivity");
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
public void onStartTrackingTouch(SeekBar seekBar) {}
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
private void setData(int count, float range) {
private RectF mOnValueSelectedRectF = new RectF();
float barWidth = 9f;
float spaceForBar = 10f;
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
for (int i = 0; i < count; i++) {
float val = (float) (Math.random() * range);
yVals1.add(new BarEntry(i * spaceForBar, val,
getResources().getDrawable(R.drawable.star)));
}
BarDataSet set1;
if (mChart.getData() != null &&
mChart.getData().getDataSetCount() > 0) {
set1 = (BarDataSet)mChart.getData().getDataSetByIndex(0);
set1.setValues(yVals1);
mChart.getData().notifyDataChanged();
mChart.notifyDataSetChanged();
} else {
set1 = new BarDataSet(yVals1, "DataSet 1");
set1.setDrawIcons(false);
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
dataSets.add(set1);
BarData data = new BarData(dataSets);
data.setValueTextSize(10f);
data.setValueTypeface(mTfLight);
data.setBarWidth(barWidth);
mChart.setData(data);
}
}
protected RectF mOnValueSelectedRectF = new RectF();
@SuppressLint("NewApi")
@Override
public void onValueSelected(Entry e, Highlight h) {
@ -273,9 +276,9 @@ public class HorizontalBarChartActivity extends DemoBase implements OnSeekBarCha
return;
RectF bounds = mOnValueSelectedRectF;
mChart.getBarBounds((BarEntry) e, bounds);
chart.getBarBounds((BarEntry) e, bounds);
MPPointF position = mChart.getPosition(e, mChart.getData().getDataSetByIndex(h.getDataSetIndex())
MPPointF position = chart.getPosition(e, chart.getData().getDataSetByIndex(h.getDataSetIndex())
.getAxisDependency());
Log.i("bounds", bounds.toString());
@ -285,6 +288,5 @@ public class HorizontalBarChartActivity extends DemoBase implements OnSeekBarCha
}
@Override
public void onNothingSelected() {
};
public void onNothingSelected() {}
}

View file

@ -1,7 +1,12 @@
package com.xxmassdeveloper.mpchartexample;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
@ -9,7 +14,6 @@ import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
@ -33,8 +37,8 @@ import java.util.List;
public class InvertedLineChartActivity extends DemoBase implements OnSeekBarChangeListener,
OnChartValueSelectedListener {
private LineChart mChart;
private SeekBar mSeekBarX, mSeekBarY;
private LineChart chart;
private SeekBar seekBarX, seekBarY;
private TextView tvX, tvY;
@Override
@ -44,217 +48,79 @@ public class InvertedLineChartActivity extends DemoBase implements OnSeekBarChan
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_linechart);
setTitle("InvertedLineChartActivity");
tvX = findViewById(R.id.tvXMax);
tvY = findViewById(R.id.tvYMax);
mSeekBarX = findViewById(R.id.seekBar1);
mSeekBarY = findViewById(R.id.seekBar2);
seekBarX = findViewById(R.id.seekBar1);
seekBarY = findViewById(R.id.seekBar2);
mSeekBarX.setProgress(45);
mSeekBarY.setProgress(100);
seekBarX.setProgress(45);
seekBarY.setProgress(100);
mSeekBarY.setOnSeekBarChangeListener(this);
mSeekBarX.setOnSeekBarChangeListener(this);
seekBarY.setOnSeekBarChangeListener(this);
seekBarX.setOnSeekBarChangeListener(this);
chart = findViewById(R.id.chart1);
chart.setOnChartValueSelectedListener(this);
chart.setDrawGridBackground(false);
mChart = findViewById(R.id.chart1);
mChart.setOnChartValueSelectedListener(this);
mChart.setDrawGridBackground(false);
// no description text
mChart.getDescription().setEnabled(false);
chart.getDescription().setEnabled(false);
// enable touch gestures
mChart.setTouchEnabled(true);
chart.setTouchEnabled(true);
// enable scaling and dragging
mChart.setDragEnabled(true);
mChart.setScaleEnabled(true);
chart.setDragEnabled(true);
chart.setScaleEnabled(true);
// if disabled, scaling can be done on x- and y-axis separately
mChart.setPinchZoom(true);
chart.setPinchZoom(true);
// set an alternative background color
// mChart.setBackgroundColor(Color.GRAY);
// chart.setBackgroundColor(Color.GRAY);
// create a custom MarkerView (extend MarkerView) and specify the layout
// to use for it
MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker_view);
mv.setChartView(mChart); // For bounds control
mChart.setMarker(mv); // Set the marker to the chart
XAxis xl = mChart.getXAxis();
mv.setChartView(chart); // For bounds control
chart.setMarker(mv); // Set the marker to the chart
XAxis xl = chart.getXAxis();
xl.setAvoidFirstLastClipping(true);
xl.setAxisMinimum(0f);
YAxis leftAxis = mChart.getAxisLeft();
YAxis leftAxis = chart.getAxisLeft();
leftAxis.setInverted(true);
leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
YAxis rightAxis = mChart.getAxisRight();
YAxis rightAxis = chart.getAxisRight();
rightAxis.setEnabled(false);
// add data
setData(25, 50);
// // restrain the maximum scale-out factor
// mChart.setScaleMinima(3f, 3f);
// chart.setScaleMinima(3f, 3f);
//
// // center the view to a specific position inside the chart
// mChart.centerViewPort(10, 50);
// chart.centerViewPort(10, 50);
// get the legend (only possible after setting data)
Legend l = mChart.getLegend();
Legend l = chart.getLegend();
// modify the legend ...
l.setForm(LegendForm.LINE);
// dont forget to refresh the drawing
mChart.invalidate();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.line, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.actionToggleValues: {
List<ILineDataSet> sets = mChart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
set.setDrawValues(!set.isDrawValuesEnabled());
}
mChart.invalidate();
break;
}
case R.id.actionToggleHighlight: {
if(mChart.getData() != null) {
mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled());
mChart.invalidate();
}
break;
}
case R.id.actionToggleFilled: {
List<ILineDataSet> sets = mChart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
if (set.isDrawFilledEnabled())
set.setDrawFilled(false);
else
set.setDrawFilled(true);
}
mChart.invalidate();
break;
}
case R.id.actionToggleCircles: {
List<ILineDataSet> sets = mChart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
if (set.isDrawCirclesEnabled())
set.setDrawCircles(false);
else
set.setDrawCircles(true);
}
mChart.invalidate();
break;
}
case R.id.animateX: {
mChart.animateX(3000);
break;
}
case R.id.animateY: {
mChart.animateY(3000);
break;
}
case R.id.animateXY: {
mChart.animateXY(3000, 3000);
break;
}
case R.id.actionTogglePinch: {
if (mChart.isPinchZoomEnabled())
mChart.setPinchZoom(false);
else
mChart.setPinchZoom(true);
mChart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
break;
}
case R.id.actionSave: {
if (mChart.saveToPath("title" + System.currentTimeMillis(), "")) {
Toast.makeText(getApplicationContext(), "Saving SUCCESSFUL!",
Toast.LENGTH_SHORT).show();
} else
Toast.makeText(getApplicationContext(), "Saving FAILED!", Toast.LENGTH_SHORT)
.show();
// mChart.saveToGallery("title"+System.currentTimeMillis())
break;
}
}
return true;
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvX.setText("" + (mSeekBarX.getProgress() + 1));
tvY.setText("" + (mSeekBarY.getProgress()));
setData(mSeekBarX.getProgress() + 1, mSeekBarY.getProgress());
// redraw
mChart.invalidate();
}
@Override
public void onValueSelected(Entry e, Highlight h) {
Log.i("VAL SELECTED",
"Value: " + e.getY() + ", xIndex: " + e.getX()
+ ", DataSet index: " + h.getDataSetIndex());
}
@Override
public void onNothingSelected() {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
// don't forget to refresh the drawing
chart.invalidate();
}
private void setData(int count, float range) {
ArrayList<Entry> entries = new ArrayList<Entry>();
ArrayList<Entry> entries = new ArrayList<>();
for (int i = 0; i < count; i++) {
float xVal = (float) (Math.random() * range);
@ -271,10 +137,149 @@ public class InvertedLineChartActivity extends DemoBase implements OnSeekBarChan
set1.setLineWidth(1.5f);
set1.setCircleRadius(4f);
// create a data object with the datasets
// create a data object with the data sets
LineData data = new LineData(set1);
// set data
mChart.setData(data);
chart.setData(data);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.line, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/InvertedLineChartActivity.java"));
startActivity(i);
break;
}
case R.id.actionToggleValues: {
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
set.setDrawValues(!set.isDrawValuesEnabled());
}
chart.invalidate();
break;
}
case R.id.actionToggleHighlight: {
if(chart.getData() != null) {
chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled());
chart.invalidate();
}
break;
}
case R.id.actionToggleFilled: {
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
if (set.isDrawFilledEnabled())
set.setDrawFilled(false);
else
set.setDrawFilled(true);
}
chart.invalidate();
break;
}
case R.id.actionToggleCircles: {
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
if (set.isDrawCirclesEnabled())
set.setDrawCircles(false);
else
set.setDrawCircles(true);
}
chart.invalidate();
break;
}
case R.id.animateX: {
chart.animateX(2000);
break;
}
case R.id.animateY: {
chart.animateY(2000);
break;
}
case R.id.animateXY: {
chart.animateXY(2000, 2000);
break;
}
case R.id.actionTogglePinch: {
if (chart.isPinchZoomEnabled())
chart.setPinchZoom(false);
else
chart.setPinchZoom(true);
chart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled());
chart.notifyDataSetChanged();
break;
}
case R.id.actionSave: {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveToGallery();
} else {
requestStoragePermission(chart);
}
break;
}
}
return true;
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvX.setText(String.valueOf(seekBarX.getProgress()));
tvY.setText(String.valueOf(seekBarY.getProgress()));
setData(seekBarX.getProgress(), seekBarY.getProgress());
// redraw
chart.invalidate();
}
@Override
public void saveToGallery() {
saveToGallery(chart, "InvertedLineChartActivity");
}
@Override
public void onValueSelected(Entry e, Highlight h) {
Log.i("VAL SELECTED",
"Value: " + e.getY() + ", xIndex: " + e.getX()
+ ", DataSet index: " + h.getDataSetIndex());
}
@Override
public void onNothingSelected() {}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
}

View file

@ -1,21 +1,22 @@
package com.xxmassdeveloper.mpchartexample;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import androidx.core.content.ContextCompat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.LineChart;
@ -28,10 +29,10 @@ import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IFillFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.dataprovider.LineDataProvider;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.listener.ChartTouchListener;
import com.github.mikephil.charting.listener.OnChartGestureListener;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.Utils;
import com.xxmassdeveloper.mpchartexample.custom.MyMarkerView;
@ -40,11 +41,17 @@ import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
import java.util.ArrayList;
import java.util.List;
/**
* Example of a heavily customized {@link LineChart} with limit lines, custom line shapes, etc.
*
* @since 1.7.4
* @version 3.0.3
*/
public class LineChartActivity1 extends DemoBase implements OnSeekBarChangeListener,
OnChartGestureListener, OnChartValueSelectedListener {
OnChartValueSelectedListener {
private LineChart mChart;
private SeekBar mSeekBarX, mSeekBarY;
private LineChart chart;
private SeekBar seekBarX, seekBarY;
private TextView tvX, tvY;
@Override
@ -54,117 +61,200 @@ public class LineChartActivity1 extends DemoBase implements OnSeekBarChangeListe
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_linechart);
setTitle("LineChartActivity1");
tvX = findViewById(R.id.tvXMax);
tvY = findViewById(R.id.tvYMax);
mSeekBarX = findViewById(R.id.seekBar1);
mSeekBarY = findViewById(R.id.seekBar2);
seekBarX = findViewById(R.id.seekBar1);
seekBarX.setOnSeekBarChangeListener(this);
mSeekBarX.setProgress(45);
mSeekBarY.setProgress(100);
mSeekBarY.setOnSeekBarChangeListener(this);
mSeekBarX.setOnSeekBarChangeListener(this);
mChart = findViewById(R.id.chart1);
mChart.setOnChartGestureListener(this);
mChart.setOnChartValueSelectedListener(this);
mChart.setDrawGridBackground(false);
// no description text
mChart.getDescription().setEnabled(false);
// enable touch gestures
mChart.setTouchEnabled(true);
// enable scaling and dragging
mChart.setDragEnabled(true);
mChart.setScaleEnabled(true);
// mChart.setScaleXEnabled(true);
// mChart.setScaleYEnabled(true);
// if disabled, scaling can be done on x- and y-axis separately
mChart.setPinchZoom(true);
// set an alternative background color
// mChart.setBackgroundColor(Color.GRAY);
// create a custom MarkerView (extend MarkerView) and specify the layout
// to use for it
MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker_view);
mv.setChartView(mChart); // For bounds control
mChart.setMarker(mv); // Set the marker to the chart
// x-axis limit line
LimitLine llXAxis = new LimitLine(10f, "Index 10");
llXAxis.setLineWidth(4f);
llXAxis.enableDashedLine(10f, 10f, 0f);
llXAxis.setLabelPosition(LimitLabelPosition.RIGHT_BOTTOM);
llXAxis.setTextSize(10f);
XAxis xAxis = mChart.getXAxis();
xAxis.enableGridDashedLine(10f, 10f, 0f);
//xAxis.setValueFormatter(new MyCustomXAxisValueFormatter());
//xAxis.addLimitLine(llXAxis); // add x-axis limit line
seekBarY = findViewById(R.id.seekBar2);
seekBarY.setMax(180);
seekBarY.setOnSeekBarChangeListener(this);
Typeface tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");
{ // // Chart Style // //
chart = findViewById(R.id.chart1);
LimitLine ll1 = new LimitLine(150f, "Upper Limit");
ll1.setLineWidth(4f);
ll1.enableDashedLine(10f, 10f, 0f);
ll1.setLabelPosition(LimitLabelPosition.RIGHT_TOP);
ll1.setTextSize(10f);
ll1.setTypeface(tf);
// background color
chart.setBackgroundColor(Color.WHITE);
LimitLine ll2 = new LimitLine(-30f, "Lower Limit");
ll2.setLineWidth(4f);
ll2.enableDashedLine(10f, 10f, 0f);
ll2.setLabelPosition(LimitLabelPosition.RIGHT_BOTTOM);
ll2.setTextSize(10f);
ll2.setTypeface(tf);
// disable description text
chart.getDescription().setEnabled(false);
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.removeAllLimitLines(); // reset all limit lines to avoid overlapping lines
leftAxis.addLimitLine(ll1);
leftAxis.addLimitLine(ll2);
leftAxis.setAxisMaximum(200f);
leftAxis.setAxisMinimum(-50f);
//leftAxis.setYOffset(20f);
leftAxis.enableGridDashedLine(10f, 10f, 0f);
leftAxis.setDrawZeroLine(false);
// enable touch gestures
chart.setTouchEnabled(true);
// limit lines are drawn behind data (and not on top)
leftAxis.setDrawLimitLinesBehindData(true);
// set listeners
chart.setOnChartValueSelectedListener(this);
chart.setDrawGridBackground(false);
mChart.getAxisRight().setEnabled(false);
// create marker to display box when values are selected
MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker_view);
//mChart.getViewPortHandler().setMaximumScaleY(2f);
//mChart.getViewPortHandler().setMaximumScaleX(2f);
// Set the marker to the chart
mv.setChartView(chart);
chart.setMarker(mv);
// enable scaling and dragging
chart.setDragEnabled(true);
chart.setScaleEnabled(true);
// chart.setScaleXEnabled(true);
// chart.setScaleYEnabled(true);
// force pinch zoom along both axis
chart.setPinchZoom(true);
}
XAxis xAxis;
{ // // X-Axis Style // //
xAxis = chart.getXAxis();
// vertical grid lines
xAxis.enableGridDashedLine(10f, 10f, 0f);
}
YAxis yAxis;
{ // // Y-Axis Style // //
yAxis = chart.getAxisLeft();
// disable dual axis (only use LEFT axis)
chart.getAxisRight().setEnabled(false);
// horizontal grid lines
yAxis.enableGridDashedLine(10f, 10f, 0f);
// axis range
yAxis.setAxisMaximum(200f);
yAxis.setAxisMinimum(-50f);
}
{ // // Create Limit Lines // //
LimitLine llXAxis = new LimitLine(9f, "Index 10");
llXAxis.setLineWidth(4f);
llXAxis.enableDashedLine(10f, 10f, 0f);
llXAxis.setLabelPosition(LimitLabelPosition.RIGHT_BOTTOM);
llXAxis.setTextSize(10f);
llXAxis.setTypeface(tfRegular);
LimitLine ll1 = new LimitLine(150f, "Upper Limit");
ll1.setLineWidth(4f);
ll1.enableDashedLine(10f, 10f, 0f);
ll1.setLabelPosition(LimitLabelPosition.RIGHT_TOP);
ll1.setTextSize(10f);
ll1.setTypeface(tfRegular);
LimitLine ll2 = new LimitLine(-30f, "Lower Limit");
ll2.setLineWidth(4f);
ll2.enableDashedLine(10f, 10f, 0f);
ll2.setLabelPosition(LimitLabelPosition.RIGHT_BOTTOM);
ll2.setTextSize(10f);
ll2.setTypeface(tfRegular);
// draw limit lines behind data instead of on top
yAxis.setDrawLimitLinesBehindData(true);
xAxis.setDrawLimitLinesBehindData(true);
// add limit lines
yAxis.addLimitLine(ll1);
yAxis.addLimitLine(ll2);
//xAxis.addLimitLine(llXAxis);
}
// add data
setData(45, 100);
seekBarX.setProgress(45);
seekBarY.setProgress(180);
setData(45, 180);
// mChart.setVisibleXRange(20);
// mChart.setVisibleYRange(20f, AxisDependency.LEFT);
// mChart.centerViewTo(20, 50, AxisDependency.LEFT);
mChart.animateX(2500);
//mChart.invalidate();
// draw points over time
chart.animateX(1500);
// get the legend (only possible after setting data)
Legend l = mChart.getLegend();
Legend l = chart.getLegend();
// modify the legend ...
// draw legend entries as lines
l.setForm(LegendForm.LINE);
// // dont forget to refresh the drawing
// mChart.invalidate();
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
private void setData(int count, float range) {
ArrayList<Entry> values = new ArrayList<>();
for (int i = 0; i < count; i++) {
float val = (float) (Math.random() * range) - 30;
values.add(new Entry(i, val, getResources().getDrawable(R.drawable.star)));
}
LineDataSet set1;
if (chart.getData() != null &&
chart.getData().getDataSetCount() > 0) {
set1 = (LineDataSet) chart.getData().getDataSetByIndex(0);
set1.setValues(values);
set1.notifyDataSetChanged();
chart.getData().notifyDataChanged();
chart.notifyDataSetChanged();
} else {
// create a dataset and give it a type
set1 = new LineDataSet(values, "DataSet 1");
set1.setDrawIcons(false);
// draw dashed line
set1.enableDashedLine(10f, 5f, 0f);
// black lines and points
set1.setColor(Color.BLACK);
set1.setCircleColor(Color.BLACK);
// line thickness and point size
set1.setLineWidth(1f);
set1.setCircleRadius(3f);
// draw points as solid circles
set1.setDrawCircleHole(false);
// customize legend entry
set1.setFormLineWidth(1f);
set1.setFormLineDashEffect(new DashPathEffect(new float[]{10f, 5f}, 0f));
set1.setFormSize(15.f);
// text size of values
set1.setValueTextSize(9f);
// draw selection line as dashed
set1.enableDashedHighlightLine(10f, 5f, 0f);
// set the filled area
set1.setDrawFilled(true);
set1.setFillFormatter(new IFillFormatter() {
@Override
public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
return chart.getAxisLeft().getAxisMinimum();
}
});
// set color of filled area
if (Utils.getSDKInt() >= 18) {
// drawables only supported on api level 18 and above
Drawable drawable = ContextCompat.getDrawable(this, R.drawable.fade_red);
set1.setFillDrawable(drawable);
} else {
set1.setFillColor(Color.BLACK);
}
ArrayList<ILineDataSet> dataSets = new ArrayList<>();
dataSets.add(set1); // add the data sets
// create a data object with the data sets
LineData data = new LineData(dataSets);
// set data
chart.setData(data);
}
}
@Override
@ -177,8 +267,14 @@ public class LineChartActivity1 extends DemoBase implements OnSeekBarChangeListe
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity1.java"));
startActivity(i);
break;
}
case R.id.actionToggleValues: {
List<ILineDataSet> sets = mChart.getData()
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
@ -187,11 +283,11 @@ public class LineChartActivity1 extends DemoBase implements OnSeekBarChangeListe
set.setDrawValues(!set.isDrawValuesEnabled());
}
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionToggleIcons: {
List<ILineDataSet> sets = mChart.getData()
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
@ -200,19 +296,19 @@ public class LineChartActivity1 extends DemoBase implements OnSeekBarChangeListe
set.setDrawIcons(!set.isDrawIconsEnabled());
}
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionToggleHighlight: {
if(mChart.getData() != null) {
mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled());
mChart.invalidate();
if(chart.getData() != null) {
chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled());
chart.invalidate();
}
break;
}
case R.id.actionToggleFilled: {
List<ILineDataSet> sets = mChart.getData()
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
@ -223,11 +319,11 @@ public class LineChartActivity1 extends DemoBase implements OnSeekBarChangeListe
else
set.setDrawFilled(true);
}
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionToggleCircles: {
List<ILineDataSet> sets = mChart.getData()
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
@ -238,11 +334,11 @@ public class LineChartActivity1 extends DemoBase implements OnSeekBarChangeListe
else
set.setDrawCircles(true);
}
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionToggleCubic: {
List<ILineDataSet> sets = mChart.getData()
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
@ -252,11 +348,11 @@ public class LineChartActivity1 extends DemoBase implements OnSeekBarChangeListe
? LineDataSet.Mode.LINEAR
: LineDataSet.Mode.CUBIC_BEZIER);
}
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionToggleStepped: {
List<ILineDataSet> sets = mChart.getData()
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
@ -266,11 +362,11 @@ public class LineChartActivity1 extends DemoBase implements OnSeekBarChangeListe
? LineDataSet.Mode.LINEAR
: LineDataSet.Mode.STEPPED);
}
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionToggleHorizontalCubic: {
List<ILineDataSet> sets = mChart.getData()
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
@ -280,44 +376,41 @@ public class LineChartActivity1 extends DemoBase implements OnSeekBarChangeListe
? LineDataSet.Mode.LINEAR
: LineDataSet.Mode.HORIZONTAL_BEZIER);
}
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionTogglePinch: {
if (mChart.isPinchZoomEnabled())
mChart.setPinchZoom(false);
if (chart.isPinchZoomEnabled())
chart.setPinchZoom(false);
else
mChart.setPinchZoom(true);
chart.setPinchZoom(true);
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled());
chart.notifyDataSetChanged();
break;
}
case R.id.animateX: {
mChart.animateX(3000);
chart.animateX(2000);
break;
}
case R.id.animateY: {
mChart.animateY(3000, Easing.EaseInCubic);
chart.animateY(2000, Easing.EaseInCubic);
break;
}
case R.id.animateXY: {
mChart.animateXY(3000, 3000);
chart.animateXY(2000, 2000);
break;
}
case R.id.actionSave: {
if (mChart.saveToPath("title" + System.currentTimeMillis(), "")) {
Toast.makeText(getApplicationContext(), "Saving SUCCESSFUL!",
Toast.LENGTH_SHORT).show();
} else
Toast.makeText(getApplicationContext(), "Saving FAILED!", Toast.LENGTH_SHORT)
.show();
// mChart.saveToGallery("title"+System.currentTimeMillis())
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveToGallery();
} else {
requestStoragePermission(chart);
}
break;
}
}
@ -327,134 +420,31 @@ public class LineChartActivity1 extends DemoBase implements OnSeekBarChangeListe
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvX.setText("" + (mSeekBarX.getProgress() + 1));
tvY.setText("" + (mSeekBarY.getProgress()));
tvX.setText(String.valueOf(seekBarX.getProgress()));
tvY.setText(String.valueOf(seekBarY.getProgress()));
setData(mSeekBarX.getProgress() + 1, mSeekBarY.getProgress());
setData(seekBarX.getProgress(), seekBarY.getProgress());
// redraw
mChart.invalidate();
chart.invalidate();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
public void saveToGallery() {
saveToGallery(chart, "LineChartActivity1");
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
private void setData(int count, float range) {
ArrayList<Entry> values = new ArrayList<Entry>();
for (int i = 0; i < count; i++) {
float val = (float) (Math.random() * range) + 3;
values.add(new Entry(i, val, getResources().getDrawable(R.drawable.star)));
}
LineDataSet set1;
if (mChart.getData() != null &&
mChart.getData().getDataSetCount() > 0) {
set1 = (LineDataSet)mChart.getData().getDataSetByIndex(0);
set1.setValues(values);
mChart.getData().notifyDataChanged();
mChart.notifyDataSetChanged();
} else {
// create a dataset and give it a type
set1 = new LineDataSet(values, "DataSet 1");
set1.setDrawIcons(false);
// set the line to be drawn like this "- - - - - -"
set1.enableDashedLine(10f, 5f, 0f);
set1.enableDashedHighlightLine(10f, 5f, 0f);
set1.setColor(Color.BLACK);
set1.setCircleColor(Color.BLACK);
set1.setLineWidth(1f);
set1.setCircleRadius(3f);
set1.setDrawCircleHole(false);
set1.setValueTextSize(9f);
set1.setDrawFilled(true);
set1.setFormLineWidth(1f);
set1.setFormLineDashEffect(new DashPathEffect(new float[]{10f, 5f}, 0f));
set1.setFormSize(15.f);
if (Utils.getSDKInt() >= 18) {
// fill drawable only supported on api level 18 and above
Drawable drawable = ContextCompat.getDrawable(this, R.drawable.fade_red);
set1.setFillDrawable(drawable);
}
else {
set1.setFillColor(Color.BLACK);
}
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
dataSets.add(set1); // add the datasets
// create a data object with the datasets
LineData data = new LineData(dataSets);
// set data
mChart.setData(data);
}
}
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
Log.i("Gesture", "START, x: " + me.getX() + ", y: " + me.getY());
}
@Override
public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
Log.i("Gesture", "END, lastGesture: " + lastPerformedGesture);
// un-highlight values after the gesture is finished and no single-tap
if(lastPerformedGesture != ChartTouchListener.ChartGesture.SINGLE_TAP)
mChart.highlightValues(null); // or highlightTouch(null) for callback to onNothingSelected(...)
}
@Override
public void onChartLongPressed(MotionEvent me) {
Log.i("LongPress", "Chart longpressed.");
}
@Override
public void onChartDoubleTapped(MotionEvent me) {
Log.i("DoubleTap", "Chart double-tapped.");
}
@Override
public void onChartSingleTapped(MotionEvent me) {
Log.i("SingleTap", "Chart single-tapped.");
}
@Override
public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) {
Log.i("Fling", "Chart flinged. VeloX: " + velocityX + ", VeloY: " + velocityY);
}
@Override
public void onChartScale(MotionEvent me, float scaleX, float scaleY) {
Log.i("Scale / Zoom", "ScaleX: " + scaleX + ", ScaleY: " + scaleY);
}
@Override
public void onChartTranslate(MotionEvent me, float dX, float dY) {
Log.i("Translate / Move", "dX: " + dX + ", dY: " + dY);
}
public void onStopTrackingTouch(SeekBar seekBar) {}
@Override
public void onValueSelected(Entry e, Highlight h) {
Log.i("Entry selected", e.toString());
Log.i("LOWHIGH", "low: " + mChart.getLowestVisibleX() + ", high: " + mChart.getHighestVisibleX());
Log.i("MIN MAX", "xmin: " + mChart.getXChartMin() + ", xmax: " + mChart.getXChartMax() + ", ymin: " + mChart.getYChartMin() + ", ymax: " + mChart.getYChartMax());
Log.i("LOW HIGH", "low: " + chart.getLowestVisibleX() + ", high: " + chart.getHighestVisibleX());
Log.i("MIN MAX", "xMin: " + chart.getXChartMin() + ", xMax: " + chart.getXChartMax() + ", yMin: " + chart.getYChartMin() + ", yMax: " + chart.getYChartMax());
}
@Override

View file

@ -1,8 +1,13 @@
package com.xxmassdeveloper.mpchartexample;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
@ -10,7 +15,6 @@ import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
@ -30,11 +34,17 @@ import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
import java.util.ArrayList;
import java.util.List;
/**
* Example of a dual axis {@link LineChart} with multiple data sets.
*
* @since 1.7.4
* @version 3.0.3
*/
public class LineChartActivity2 extends DemoBase implements OnSeekBarChangeListener,
OnChartValueSelectedListener {
private LineChart mChart;
private SeekBar mSeekBarX, mSeekBarY;
private LineChart chart;
private SeekBar seekBarX, seekBarY;
private TextView tvX, tvY;
@Override
@ -44,51 +54,53 @@ public class LineChartActivity2 extends DemoBase implements OnSeekBarChangeListe
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_linechart);
setTitle("LineChartActivity2");
tvX = findViewById(R.id.tvXMax);
tvY = findViewById(R.id.tvYMax);
mSeekBarX = findViewById(R.id.seekBar1);
mSeekBarY = findViewById(R.id.seekBar2);
mSeekBarX.setProgress(45);
mSeekBarY.setProgress(100);
seekBarX = findViewById(R.id.seekBar1);
seekBarX.setOnSeekBarChangeListener(this);
mSeekBarY.setOnSeekBarChangeListener(this);
mSeekBarX.setOnSeekBarChangeListener(this);
seekBarY = findViewById(R.id.seekBar2);
seekBarY.setOnSeekBarChangeListener(this);
mChart = findViewById(R.id.chart1);
mChart.setOnChartValueSelectedListener(this);
chart = findViewById(R.id.chart1);
chart.setOnChartValueSelectedListener(this);
// no description text
mChart.getDescription().setEnabled(false);
chart.getDescription().setEnabled(false);
// enable touch gestures
mChart.setTouchEnabled(true);
chart.setTouchEnabled(true);
mChart.setDragDecelerationFrictionCoef(0.9f);
chart.setDragDecelerationFrictionCoef(0.9f);
// enable scaling and dragging
mChart.setDragEnabled(true);
mChart.setScaleEnabled(true);
mChart.setDrawGridBackground(false);
mChart.setHighlightPerDragEnabled(true);
chart.setDragEnabled(true);
chart.setScaleEnabled(true);
chart.setDrawGridBackground(false);
chart.setHighlightPerDragEnabled(true);
// if disabled, scaling can be done on x- and y-axis separately
mChart.setPinchZoom(true);
chart.setPinchZoom(true);
// set an alternative background color
mChart.setBackgroundColor(Color.LTGRAY);
chart.setBackgroundColor(Color.LTGRAY);
// add data
seekBarX.setProgress(20);
seekBarY.setProgress(30);
setData(20, 30);
mChart.animateX(2500);
chart.animateX(1500);
// get the legend (only possible after setting data)
Legend l = mChart.getLegend();
Legend l = chart.getLegend();
// modify the legend ...
l.setForm(LegendForm.LINE);
l.setTypeface(mTfLight);
l.setTypeface(tfLight);
l.setTextSize(11f);
l.setTextColor(Color.WHITE);
l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
@ -97,23 +109,23 @@ public class LineChartActivity2 extends DemoBase implements OnSeekBarChangeListe
l.setDrawInside(false);
// l.setYOffset(11f);
XAxis xAxis = mChart.getXAxis();
xAxis.setTypeface(mTfLight);
XAxis xAxis = chart.getXAxis();
xAxis.setTypeface(tfLight);
xAxis.setTextSize(11f);
xAxis.setTextColor(Color.WHITE);
xAxis.setDrawGridLines(false);
xAxis.setDrawAxisLine(false);
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setTypeface(mTfLight);
YAxis leftAxis = chart.getAxisLeft();
leftAxis.setTypeface(tfLight);
leftAxis.setTextColor(ColorTemplate.getHoloBlue());
leftAxis.setAxisMaximum(200f);
leftAxis.setAxisMinimum(0f);
leftAxis.setDrawGridLines(true);
leftAxis.setGranularityEnabled(true);
YAxis rightAxis = mChart.getAxisRight();
rightAxis.setTypeface(mTfLight);
YAxis rightAxis = chart.getAxisRight();
rightAxis.setTypeface(tfLight);
rightAxis.setTextColor(Color.RED);
rightAxis.setAxisMaximum(900);
rightAxis.setAxisMinimum(-200);
@ -122,208 +134,44 @@ public class LineChartActivity2 extends DemoBase implements OnSeekBarChangeListe
rightAxis.setGranularityEnabled(false);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.line, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.actionToggleValues: {
List<ILineDataSet> sets = mChart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
set.setDrawValues(!set.isDrawValuesEnabled());
}
mChart.invalidate();
break;
}
case R.id.actionToggleHighlight: {
if (mChart.getData() != null) {
mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled());
mChart.invalidate();
}
break;
}
case R.id.actionToggleFilled: {
List<ILineDataSet> sets = mChart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
if (set.isDrawFilledEnabled())
set.setDrawFilled(false);
else
set.setDrawFilled(true);
}
mChart.invalidate();
break;
}
case R.id.actionToggleCircles: {
List<ILineDataSet> sets = mChart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
if (set.isDrawCirclesEnabled())
set.setDrawCircles(false);
else
set.setDrawCircles(true);
}
mChart.invalidate();
break;
}
case R.id.actionToggleCubic: {
List<ILineDataSet> sets = mChart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
set.setMode(set.getMode() == LineDataSet.Mode.CUBIC_BEZIER
? LineDataSet.Mode.LINEAR
: LineDataSet.Mode.CUBIC_BEZIER);
}
mChart.invalidate();
break;
}
case R.id.actionToggleStepped: {
List<ILineDataSet> sets = mChart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
set.setMode(set.getMode() == LineDataSet.Mode.STEPPED
? LineDataSet.Mode.LINEAR
: LineDataSet.Mode.STEPPED);
}
mChart.invalidate();
break;
}
case R.id.actionToggleHorizontalCubic: {
List<ILineDataSet> sets = mChart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
set.setMode(set.getMode() == LineDataSet.Mode.HORIZONTAL_BEZIER
? LineDataSet.Mode.LINEAR
: LineDataSet.Mode.HORIZONTAL_BEZIER);
}
mChart.invalidate();
break;
}
case R.id.actionTogglePinch: {
if (mChart.isPinchZoomEnabled())
mChart.setPinchZoom(false);
else
mChart.setPinchZoom(true);
mChart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
break;
}
case R.id.animateX: {
mChart.animateX(3000);
//mChart.highlightValue(9.7f, 1, false);
break;
}
case R.id.animateY: {
mChart.animateY(3000);
break;
}
case R.id.animateXY: {
mChart.animateXY(3000, 3000);
break;
}
case R.id.actionSave: {
if (mChart.saveToPath("title" + System.currentTimeMillis(), "")) {
Toast.makeText(getApplicationContext(), "Saving SUCCESSFUL!",
Toast.LENGTH_SHORT).show();
} else
Toast.makeText(getApplicationContext(), "Saving FAILED!", Toast.LENGTH_SHORT)
.show();
// mChart.saveToGallery("title"+System.currentTimeMillis())
break;
}
}
return true;
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvX.setText("" + (mSeekBarX.getProgress() + 1));
tvY.setText("" + (mSeekBarY.getProgress()));
setData(mSeekBarX.getProgress() + 1, mSeekBarY.getProgress());
// redraw
mChart.invalidate();
}
private void setData(int count, float range) {
ArrayList<Entry> yVals1 = new ArrayList<Entry>();
ArrayList<Entry> values1 = new ArrayList<>();
for (int i = 0; i < count; i++) {
float mult = range / 2f;
float val = (float) (Math.random() * mult) + 50;
yVals1.add(new Entry(i, val));
float val = (float) (Math.random() * (range / 2f)) + 50;
values1.add(new Entry(i, val));
}
ArrayList<Entry> yVals2 = new ArrayList<Entry>();
for (int i = 0; i < count-1; i++) {
float mult = range;
float val = (float) (Math.random() * mult) + 450;
yVals2.add(new Entry(i, val));
// if(i == 10) {
// yVals2.add(new Entry(i, val + 50));
// }
}
ArrayList<Entry> yVals3 = new ArrayList<Entry>();
ArrayList<Entry> values2 = new ArrayList<>();
for (int i = 0; i < count; i++) {
float mult = range;
float val = (float) (Math.random() * mult) + 500;
yVals3.add(new Entry(i, val));
float val = (float) (Math.random() * range) + 450;
values2.add(new Entry(i, val));
}
ArrayList<Entry> values3 = new ArrayList<>();
for (int i = 0; i < count; i++) {
float val = (float) (Math.random() * range) + 500;
values3.add(new Entry(i, val));
}
LineDataSet set1, set2, set3;
if (mChart.getData() != null &&
mChart.getData().getDataSetCount() > 0) {
set1 = (LineDataSet) mChart.getData().getDataSetByIndex(0);
set2 = (LineDataSet) mChart.getData().getDataSetByIndex(1);
set3 = (LineDataSet) mChart.getData().getDataSetByIndex(2);
set1.setValues(yVals1);
set2.setValues(yVals2);
set3.setValues(yVals3);
mChart.getData().notifyDataChanged();
mChart.notifyDataSetChanged();
if (chart.getData() != null &&
chart.getData().getDataSetCount() > 0) {
set1 = (LineDataSet) chart.getData().getDataSetByIndex(0);
set2 = (LineDataSet) chart.getData().getDataSetByIndex(1);
set3 = (LineDataSet) chart.getData().getDataSetByIndex(2);
set1.setValues(values1);
set2.setValues(values2);
set3.setValues(values3);
chart.getData().notifyDataChanged();
chart.notifyDataSetChanged();
} else {
// create a dataset and give it a type
set1 = new LineDataSet(yVals1, "DataSet 1");
set1 = new LineDataSet(values1, "DataSet 1");
set1.setAxisDependency(AxisDependency.LEFT);
set1.setColor(ColorTemplate.getHoloBlue());
@ -340,7 +188,7 @@ public class LineChartActivity2 extends DemoBase implements OnSeekBarChangeListe
//set1.setCircleHoleColor(Color.WHITE);
// create a dataset and give it a type
set2 = new LineDataSet(yVals2, "DataSet 2");
set2 = new LineDataSet(values2, "DataSet 2");
set2.setAxisDependency(AxisDependency.RIGHT);
set2.setColor(Color.RED);
set2.setCircleColor(Color.WHITE);
@ -352,7 +200,7 @@ public class LineChartActivity2 extends DemoBase implements OnSeekBarChangeListe
set2.setHighLightColor(Color.rgb(244, 117, 117));
//set2.setFillFormatter(new MyFillFormatter(900f));
set3 = new LineDataSet(yVals3, "DataSet 3");
set3 = new LineDataSet(values3, "DataSet 3");
set3.setAxisDependency(AxisDependency.RIGHT);
set3.setColor(Color.YELLOW);
set3.setCircleColor(Color.WHITE);
@ -363,25 +211,189 @@ public class LineChartActivity2 extends DemoBase implements OnSeekBarChangeListe
set3.setDrawCircleHole(false);
set3.setHighLightColor(Color.rgb(244, 117, 117));
// create a data object with the datasets
// create a data object with the data sets
LineData data = new LineData(set1, set2, set3);
data.setValueTextColor(Color.WHITE);
data.setValueTextSize(9f);
// set data
mChart.setData(data);
chart.setData(data);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.line, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity2.java"));
startActivity(i);
break;
}
case R.id.actionToggleValues: {
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
set.setDrawValues(!set.isDrawValuesEnabled());
}
chart.invalidate();
break;
}
case R.id.actionToggleHighlight: {
if (chart.getData() != null) {
chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled());
chart.invalidate();
}
break;
}
case R.id.actionToggleFilled: {
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
if (set.isDrawFilledEnabled())
set.setDrawFilled(false);
else
set.setDrawFilled(true);
}
chart.invalidate();
break;
}
case R.id.actionToggleCircles: {
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
if (set.isDrawCirclesEnabled())
set.setDrawCircles(false);
else
set.setDrawCircles(true);
}
chart.invalidate();
break;
}
case R.id.actionToggleCubic: {
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
set.setMode(set.getMode() == LineDataSet.Mode.CUBIC_BEZIER
? LineDataSet.Mode.LINEAR
: LineDataSet.Mode.CUBIC_BEZIER);
}
chart.invalidate();
break;
}
case R.id.actionToggleStepped: {
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
set.setMode(set.getMode() == LineDataSet.Mode.STEPPED
? LineDataSet.Mode.LINEAR
: LineDataSet.Mode.STEPPED);
}
chart.invalidate();
break;
}
case R.id.actionToggleHorizontalCubic: {
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
set.setMode(set.getMode() == LineDataSet.Mode.HORIZONTAL_BEZIER
? LineDataSet.Mode.LINEAR
: LineDataSet.Mode.HORIZONTAL_BEZIER);
}
chart.invalidate();
break;
}
case R.id.actionTogglePinch: {
if (chart.isPinchZoomEnabled())
chart.setPinchZoom(false);
else
chart.setPinchZoom(true);
chart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled());
chart.notifyDataSetChanged();
break;
}
case R.id.animateX: {
chart.animateX(2000);
break;
}
case R.id.animateY: {
chart.animateY(2000);
break;
}
case R.id.animateXY: {
chart.animateXY(2000, 2000);
break;
}
case R.id.actionSave: {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveToGallery();
} else {
requestStoragePermission(chart);
}
break;
}
}
return true;
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvX.setText(String.valueOf(seekBarX.getProgress()));
tvY.setText(String.valueOf(seekBarY.getProgress()));
setData(seekBarX.getProgress(), seekBarY.getProgress());
// redraw
chart.invalidate();
}
@Override
public void saveToGallery() {
saveToGallery(chart, "LineChartActivity2");
}
@Override
public void onValueSelected(Entry e, Highlight h) {
Log.i("Entry selected", e.toString());
mChart.centerViewToAnimated(e.getX(), e.getY(), mChart.getData().getDataSetByIndex(h.getDataSetIndex())
chart.centerViewToAnimated(e.getX(), e.getY(), chart.getData().getDataSetByIndex(h.getDataSetIndex())
.getAxisDependency(), 500);
//mChart.zoomAndCenterAnimated(2.5f, 2.5f, e.getX(), e.getY(), mChart.getData().getDataSetByIndex(dataSetIndex)
//chart.zoomAndCenterAnimated(2.5f, 2.5f, e.getX(), e.getY(), chart.getData().getDataSetByIndex(dataSetIndex)
// .getAxisDependency(), 1000);
//mChart.zoomAndCenterAnimated(1.8f, 1.8f, e.getX(), e.getY(), mChart.getData().getDataSetByIndex(dataSetIndex)
//chart.zoomAndCenterAnimated(1.8f, 1.8f, e.getX(), e.getY(), chart.getData().getDataSetByIndex(dataSetIndex)
// .getAxisDependency(), 1000);
}
@ -391,14 +403,8 @@ public class LineChartActivity2 extends DemoBase implements OnSeekBarChangeListe
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {}
}

View file

@ -1,9 +1,13 @@
package com.xxmassdeveloper.mpchartexample;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import com.github.mikephil.charting.charts.LineChart;
@ -15,10 +19,10 @@ import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
import java.util.ArrayList;
@SuppressWarnings("SameParameterValue")
public class LineChartActivityColored extends DemoBase {
private LineChart[] mCharts = new LineChart[4];
private Typeface mTf;
private LineChart[] charts = new LineChart[4];
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -27,26 +31,28 @@ public class LineChartActivityColored extends DemoBase {
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_colored_lines);
mCharts[0] = findViewById(R.id.chart1);
mCharts[1] = findViewById(R.id.chart2);
mCharts[2] = findViewById(R.id.chart3);
mCharts[3] = findViewById(R.id.chart4);
setTitle("LineChartActivityColored");
mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Bold.ttf");
charts[0] = findViewById(R.id.chart1);
charts[1] = findViewById(R.id.chart2);
charts[2] = findViewById(R.id.chart3);
charts[3] = findViewById(R.id.chart4);
for (int i = 0; i < mCharts.length; i++) {
Typeface mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Bold.ttf");
for (int i = 0; i < charts.length; i++) {
LineData data = getData(36, 100);
data.setValueTypeface(mTf);
// add some transparency to the color with "& 0x90FFFFFF"
setupChart(mCharts[i], data, mColors[i % mColors.length]);
setupChart(charts[i], data, colors[i % colors.length]);
}
}
private int[] mColors = new int[] {
Color.rgb(137, 230, 81),
Color.rgb(240, 240, 30),
private final int[] colors = new int[] {
Color.rgb(137, 230, 81),
Color.rgb(240, 240, 30),
Color.rgb(89, 199, 250),
Color.rgb(250, 104, 104)
};
@ -57,8 +63,8 @@ public class LineChartActivityColored extends DemoBase {
// no description text
chart.getDescription().setEnabled(false);
// mChart.setDrawHorizontalGrid(false);
// chart.setDrawHorizontalGrid(false);
//
// enable / disable grid background
chart.setDrawGridBackground(false);
@ -75,7 +81,7 @@ public class LineChartActivityColored extends DemoBase {
chart.setPinchZoom(false);
chart.setBackgroundColor(color);
// set custom chart offsets (automatic offset calculation is hereby disabled)
chart.setViewPortOffsets(10, 0, 10, 0);
@ -96,18 +102,18 @@ public class LineChartActivityColored extends DemoBase {
// animate calls invalidate()...
chart.animateX(2500);
}
private LineData getData(int count, float range) {
ArrayList<Entry> yVals = new ArrayList<Entry>();
ArrayList<Entry> values = new ArrayList<>();
for (int i = 0; i < count; i++) {
float val = (float) (Math.random() * range) + 3;
yVals.add(new Entry(i, val));
values.add(new Entry(i, val));
}
// create a dataset and give it a type
LineDataSet set1 = new LineDataSet(yVals, "DataSet 1");
LineDataSet set1 = new LineDataSet(values, "DataSet 1");
// set1.setFillAlpha(110);
// set1.setFillColor(Color.RED);
@ -119,9 +125,31 @@ public class LineChartActivityColored extends DemoBase {
set1.setHighLightColor(Color.WHITE);
set1.setDrawValues(false);
// create a data object with the datasets
LineData data = new LineData(set1);
return data;
// create a data object with the data sets
return new LineData(set1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.only_github, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivityColored.java"));
startActivity(i);
break;
}
}
return true;
}
@Override
public void saveToGallery() { /* Intentionally left empty */ }
}

View file

@ -1,15 +1,19 @@
package com.xxmassdeveloper.mpchartexample;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
@ -25,17 +29,17 @@ import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
public class LineChartTime extends DemoBase implements OnSeekBarChangeListener {
private LineChart mChart;
private SeekBar mSeekBarX;
private LineChart chart;
private SeekBar seekBarX;
private TextView tvX;
@Override
@ -45,44 +49,44 @@ public class LineChartTime extends DemoBase implements OnSeekBarChangeListener {
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_linechart_time);
setTitle("LineChartTime");
tvX = findViewById(R.id.tvXMax);
mSeekBarX = findViewById(R.id.seekBar1);
mSeekBarX.setProgress(100);
tvX.setText("100");
seekBarX = findViewById(R.id.seekBar1);
seekBarX.setOnSeekBarChangeListener(this);
mSeekBarX.setOnSeekBarChangeListener(this);
mChart = findViewById(R.id.chart1);
chart = findViewById(R.id.chart1);
// no description text
mChart.getDescription().setEnabled(false);
chart.getDescription().setEnabled(false);
// enable touch gestures
mChart.setTouchEnabled(true);
chart.setTouchEnabled(true);
mChart.setDragDecelerationFrictionCoef(0.9f);
chart.setDragDecelerationFrictionCoef(0.9f);
// enable scaling and dragging
mChart.setDragEnabled(true);
mChart.setScaleEnabled(true);
mChart.setDrawGridBackground(false);
mChart.setHighlightPerDragEnabled(true);
chart.setDragEnabled(true);
chart.setScaleEnabled(true);
chart.setDrawGridBackground(false);
chart.setHighlightPerDragEnabled(true);
// set an alternative background color
mChart.setBackgroundColor(Color.WHITE);
mChart.setViewPortOffsets(0f, 0f, 0f, 0f);
chart.setBackgroundColor(Color.WHITE);
chart.setViewPortOffsets(0f, 0f, 0f, 0f);
// add data
seekBarX.setProgress(100);
setData(100, 30);
mChart.invalidate();
chart.invalidate();
// get the legend (only possible after setting data)
Legend l = mChart.getLegend();
Legend l = chart.getLegend();
l.setEnabled(false);
XAxis xAxis = mChart.getXAxis();
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.TOP_INSIDE);
xAxis.setTypeface(mTfLight);
xAxis.setTypeface(tfLight);
xAxis.setTextSize(10f);
xAxis.setTextColor(Color.WHITE);
xAxis.setDrawAxisLine(false);
@ -92,7 +96,7 @@ public class LineChartTime extends DemoBase implements OnSeekBarChangeListener {
xAxis.setGranularity(1f); // one hour
xAxis.setValueFormatter(new IAxisValueFormatter() {
private SimpleDateFormat mFormat = new SimpleDateFormat("dd MMM HH:mm");
private SimpleDateFormat mFormat = new SimpleDateFormat("dd MMM HH:mm", Locale.ENGLISH);
@Override
public String getFormattedValue(float value, AxisBase axis) {
@ -102,9 +106,9 @@ public class LineChartTime extends DemoBase implements OnSeekBarChangeListener {
}
});
YAxis leftAxis = mChart.getAxisLeft();
YAxis leftAxis = chart.getAxisLeft();
leftAxis.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
leftAxis.setTypeface(mTfLight);
leftAxis.setTypeface(tfLight);
leftAxis.setTextColor(ColorTemplate.getHoloBlue());
leftAxis.setDrawGridLines(true);
leftAxis.setGranularityEnabled(true);
@ -113,168 +117,22 @@ public class LineChartTime extends DemoBase implements OnSeekBarChangeListener {
leftAxis.setYOffset(-9f);
leftAxis.setTextColor(Color.rgb(255, 192, 56));
YAxis rightAxis = mChart.getAxisRight();
YAxis rightAxis = chart.getAxisRight();
rightAxis.setEnabled(false);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.line, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.actionToggleValues: {
List<ILineDataSet> sets = mChart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
set.setDrawValues(!set.isDrawValuesEnabled());
}
mChart.invalidate();
break;
}
case R.id.actionToggleHighlight: {
if (mChart.getData() != null) {
mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled());
mChart.invalidate();
}
break;
}
case R.id.actionToggleFilled: {
List<ILineDataSet> sets = mChart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
if (set.isDrawFilledEnabled())
set.setDrawFilled(false);
else
set.setDrawFilled(true);
}
mChart.invalidate();
break;
}
case R.id.actionToggleCircles: {
List<ILineDataSet> sets = mChart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
if (set.isDrawCirclesEnabled())
set.setDrawCircles(false);
else
set.setDrawCircles(true);
}
mChart.invalidate();
break;
}
case R.id.actionToggleCubic: {
List<ILineDataSet> sets = mChart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
if (set.getMode() == LineDataSet.Mode.CUBIC_BEZIER)
set.setMode(LineDataSet.Mode.LINEAR);
else
set.setMode(LineDataSet.Mode.CUBIC_BEZIER);
}
mChart.invalidate();
break;
}
case R.id.actionToggleStepped: {
List<ILineDataSet> sets = mChart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
if (set.getMode() == LineDataSet.Mode.STEPPED)
set.setMode(LineDataSet.Mode.LINEAR);
else
set.setMode(LineDataSet.Mode.STEPPED);
}
mChart.invalidate();
break;
}
case R.id.actionTogglePinch: {
if (mChart.isPinchZoomEnabled())
mChart.setPinchZoom(false);
else
mChart.setPinchZoom(true);
mChart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
break;
}
case R.id.animateX: {
mChart.animateX(3000);
break;
}
case R.id.animateY: {
mChart.animateY(3000);
break;
}
case R.id.animateXY: {
mChart.animateXY(3000, 3000);
break;
}
case R.id.actionSave: {
if (mChart.saveToPath("title" + System.currentTimeMillis(), "")) {
Toast.makeText(getApplicationContext(), "Saving SUCCESSFUL!",
Toast.LENGTH_SHORT).show();
} else
Toast.makeText(getApplicationContext(), "Saving FAILED!", Toast.LENGTH_SHORT)
.show();
// mChart.saveToGallery("title"+System.currentTimeMillis())
break;
}
}
return true;
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvX.setText("" + (mSeekBarX.getProgress()));
setData(mSeekBarX.getProgress(), 50);
// redraw
mChart.invalidate();
}
private void setData(int count, float range) {
// now in hours
long now = TimeUnit.MILLISECONDS.toHours(System.currentTimeMillis());
ArrayList<Entry> values = new ArrayList<Entry>();
float from = now;
ArrayList<Entry> values = new ArrayList<>();
// count = hours
float to = now + count;
// increment by 1 hour
for (float x = from; x < to; x++) {
for (float x = now; x < to; x++) {
float y = getRandom(range, 50);
values.add(new Entry(x, y)); // add one entry per hour
@ -293,24 +151,170 @@ public class LineChartTime extends DemoBase implements OnSeekBarChangeListener {
set1.setHighLightColor(Color.rgb(244, 117, 117));
set1.setDrawCircleHole(false);
// create a data object with the datasets
// create a data object with the data sets
LineData data = new LineData(set1);
data.setValueTextColor(Color.WHITE);
data.setValueTextSize(9f);
// set data
mChart.setData(data);
chart.setData(data);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.line, menu);
return true;
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartTime.java"));
startActivity(i);
break;
}
case R.id.actionToggleValues: {
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
set.setDrawValues(!set.isDrawValuesEnabled());
}
chart.invalidate();
break;
}
case R.id.actionToggleHighlight: {
if (chart.getData() != null) {
chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled());
chart.invalidate();
}
break;
}
case R.id.actionToggleFilled: {
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
if (set.isDrawFilledEnabled())
set.setDrawFilled(false);
else
set.setDrawFilled(true);
}
chart.invalidate();
break;
}
case R.id.actionToggleCircles: {
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
if (set.isDrawCirclesEnabled())
set.setDrawCircles(false);
else
set.setDrawCircles(true);
}
chart.invalidate();
break;
}
case R.id.actionToggleCubic: {
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
if (set.getMode() == LineDataSet.Mode.CUBIC_BEZIER)
set.setMode(LineDataSet.Mode.LINEAR);
else
set.setMode(LineDataSet.Mode.CUBIC_BEZIER);
}
chart.invalidate();
break;
}
case R.id.actionToggleStepped: {
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
if (set.getMode() == LineDataSet.Mode.STEPPED)
set.setMode(LineDataSet.Mode.LINEAR);
else
set.setMode(LineDataSet.Mode.STEPPED);
}
chart.invalidate();
break;
}
case R.id.actionTogglePinch: {
if (chart.isPinchZoomEnabled())
chart.setPinchZoom(false);
else
chart.setPinchZoom(true);
chart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled());
chart.notifyDataSetChanged();
break;
}
case R.id.animateX: {
chart.animateX(2000);
break;
}
case R.id.animateY: {
chart.animateY(2000);
break;
}
case R.id.animateXY: {
chart.animateXY(2000, 2000);
break;
}
case R.id.actionSave: {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveToGallery();
} else {
requestStoragePermission(chart);
}
break;
}
}
return true;
}
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvX.setText(String.valueOf(seekBarX.getProgress()));
setData(seekBarX.getProgress(), 50);
// redraw
chart.invalidate();
}
@Override
public void saveToGallery() {
saveToGallery(chart, "LineChartTime");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
}

View file

@ -1,11 +1,16 @@
package com.xxmassdeveloper.mpchartexample;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
@ -28,8 +33,8 @@ import java.util.List;
/**
* Demonstrates the use of charts inside a ListView. IMPORTANT: provide a
* specific height attribute for the chart inside your listview-item
*
* specific height attribute for the chart inside your ListView item
*
* @author Philipp Jahoda
*/
public class ListViewBarChartActivity extends DemoBase {
@ -40,10 +45,12 @@ public class ListViewBarChartActivity extends DemoBase {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_listview_chart);
setTitle("ListViewBarChartActivity");
ListView lv = findViewById(R.id.listView1);
ArrayList<BarData> list = new ArrayList<BarData>();
ArrayList<BarData> list = new ArrayList<>();
// 20 items
for (int i = 0; i < 20; i++) {
@ -56,16 +63,18 @@ public class ListViewBarChartActivity extends DemoBase {
private class ChartDataAdapter extends ArrayAdapter<BarData> {
public ChartDataAdapter(Context context, List<BarData> objects) {
ChartDataAdapter(Context context, List<BarData> objects) {
super(context, 0, objects);
}
@SuppressLint("InflateParams")
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
BarData data = getItem(position);
ViewHolder holder = null;
ViewHolder holder;
if (convertView == null) {
@ -82,30 +91,32 @@ public class ListViewBarChartActivity extends DemoBase {
}
// apply styling
data.setValueTypeface(mTfLight);
data.setValueTextColor(Color.BLACK);
if (data != null) {
data.setValueTypeface(tfLight);
data.setValueTextColor(Color.BLACK);
}
holder.chart.getDescription().setEnabled(false);
holder.chart.setDrawGridBackground(false);
XAxis xAxis = holder.chart.getXAxis();
xAxis.setPosition(XAxisPosition.BOTTOM);
xAxis.setTypeface(mTfLight);
xAxis.setTypeface(tfLight);
xAxis.setDrawGridLines(false);
YAxis leftAxis = holder.chart.getAxisLeft();
leftAxis.setTypeface(mTfLight);
leftAxis.setTypeface(tfLight);
leftAxis.setLabelCount(5, false);
leftAxis.setSpaceTop(15f);
YAxis rightAxis = holder.chart.getAxisRight();
rightAxis.setTypeface(mTfLight);
rightAxis.setTypeface(tfLight);
rightAxis.setLabelCount(5, false);
rightAxis.setSpaceTop(15f);
// set data
holder.chart.setData(data);
holder.chart.setFitBars(true);
// do not forget to refresh the chart
// holder.chart.invalidate();
holder.chart.animateY(700);
@ -121,12 +132,12 @@ public class ListViewBarChartActivity extends DemoBase {
/**
* generates a random ChartData object with just one DataSet
*
* @return
*
* @return Bar data
*/
private BarData generateData(int cnt) {
ArrayList<BarEntry> entries = new ArrayList<BarEntry>();
ArrayList<BarEntry> entries = new ArrayList<>();
for (int i = 0; i < 12; i++) {
entries.add(new BarEntry(i, (float) (Math.random() * 70) + 30));
@ -135,12 +146,36 @@ public class ListViewBarChartActivity extends DemoBase {
BarDataSet d = new BarDataSet(entries, "New DataSet " + cnt);
d.setColors(ColorTemplate.VORDIPLOM_COLORS);
d.setBarShadowColor(Color.rgb(203, 203, 203));
ArrayList<IBarDataSet> sets = new ArrayList<IBarDataSet>();
ArrayList<IBarDataSet> sets = new ArrayList<>();
sets.add(d);
BarData cd = new BarData(sets);
cd.setBarWidth(0.9f);
return cd;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.only_github, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/ListViewBarChartActivity.java"));
startActivity(i);
break;
}
}
return true;
}
@Override
public void saveToGallery() { /* Intentionally left empty */ }
}

View file

@ -2,8 +2,13 @@
package com.xxmassdeveloper.mpchartexample;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.NonNull;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
@ -32,8 +37,8 @@ import java.util.List;
/**
* Demonstrates the use of charts inside a ListView. IMPORTANT: provide a
* specific height attribute for the chart inside your listview-item
*
* specific height attribute for the chart inside your ListView item
*
* @author Philipp Jahoda
*/
public class ListViewMultiChartActivity extends DemoBase {
@ -44,20 +49,22 @@ public class ListViewMultiChartActivity extends DemoBase {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_listview_chart);
setTitle("ListViewMultiChartActivity");
ListView lv = findViewById(R.id.listView1);
ArrayList<ChartItem> list = new ArrayList<ChartItem>();
ArrayList<ChartItem> list = new ArrayList<>();
// 30 items
for (int i = 0; i < 30; i++) {
if(i % 3 == 0) {
list.add(new LineChartItem(generateDataLine(i + 1), getApplicationContext()));
} else if(i % 3 == 1) {
list.add(new BarChartItem(generateDataBar(i + 1), getApplicationContext()));
} else if(i % 3 == 2) {
list.add(new PieChartItem(generateDataPie(i + 1), getApplicationContext()));
list.add(new PieChartItem(generateDataPie(), getApplicationContext()));
}
}
@ -67,77 +74,79 @@ public class ListViewMultiChartActivity extends DemoBase {
/** adapter that supports 3 different item types */
private class ChartDataAdapter extends ArrayAdapter<ChartItem> {
public ChartDataAdapter(Context context, List<ChartItem> objects) {
ChartDataAdapter(Context context, List<ChartItem> objects) {
super(context, 0, objects);
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
//noinspection ConstantConditions
return getItem(position).getView(position, convertView, getContext());
}
@Override
public int getItemViewType(int position) {
public int getItemViewType(int position) {
// return the views type
return getItem(position).getItemType();
ChartItem ci = getItem(position);
return ci != null ? ci.getItemType() : 0;
}
@Override
public int getViewTypeCount() {
return 3; // we have 3 different item-types
}
}
/**
* generates a random ChartData object with just one DataSet
*
* @return
*
* @return Line data
*/
private LineData generateDataLine(int cnt) {
ArrayList<Entry> e1 = new ArrayList<Entry>();
ArrayList<Entry> values1 = new ArrayList<>();
for (int i = 0; i < 12; i++) {
e1.add(new Entry(i, (int) (Math.random() * 65) + 40));
values1.add(new Entry(i, (int) (Math.random() * 65) + 40));
}
LineDataSet d1 = new LineDataSet(e1, "New DataSet " + cnt + ", (1)");
LineDataSet d1 = new LineDataSet(values1, "New DataSet " + cnt + ", (1)");
d1.setLineWidth(2.5f);
d1.setCircleRadius(4.5f);
d1.setHighLightColor(Color.rgb(244, 117, 117));
d1.setDrawValues(false);
ArrayList<Entry> e2 = new ArrayList<Entry>();
ArrayList<Entry> values2 = new ArrayList<>();
for (int i = 0; i < 12; i++) {
e2.add(new Entry(i, e1.get(i).getY() - 30));
values2.add(new Entry(i, values1.get(i).getY() - 30));
}
LineDataSet d2 = new LineDataSet(e2, "New DataSet " + cnt + ", (2)");
LineDataSet d2 = new LineDataSet(values2, "New DataSet " + cnt + ", (2)");
d2.setLineWidth(2.5f);
d2.setCircleRadius(4.5f);
d2.setHighLightColor(Color.rgb(244, 117, 117));
d2.setColor(ColorTemplate.VORDIPLOM_COLORS[0]);
d2.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[0]);
d2.setDrawValues(false);
ArrayList<ILineDataSet> sets = new ArrayList<ILineDataSet>();
ArrayList<ILineDataSet> sets = new ArrayList<>();
sets.add(d1);
sets.add(d2);
LineData cd = new LineData(sets);
return cd;
return new LineData(sets);
}
/**
* generates a random ChartData object with just one DataSet
*
* @return
*
* @return Bar data
*/
private BarData generateDataBar(int cnt) {
ArrayList<BarEntry> entries = new ArrayList<BarEntry>();
ArrayList<BarEntry> entries = new ArrayList<>();
for (int i = 0; i < 12; i++) {
entries.add(new BarEntry(i, (int) (Math.random() * 70) + 30));
@ -146,32 +155,55 @@ public class ListViewMultiChartActivity extends DemoBase {
BarDataSet d = new BarDataSet(entries, "New DataSet " + cnt);
d.setColors(ColorTemplate.VORDIPLOM_COLORS);
d.setHighLightAlpha(255);
BarData cd = new BarData(d);
cd.setBarWidth(0.9f);
return cd;
}
/**
* generates a random ChartData object with just one DataSet
*
* @return
*
* @return Pie data
*/
private PieData generateDataPie(int cnt) {
private PieData generateDataPie() {
ArrayList<PieEntry> entries = new ArrayList<PieEntry>();
ArrayList<PieEntry> entries = new ArrayList<>();
for (int i = 0; i < 4; i++) {
entries.add(new PieEntry((float) ((Math.random() * 70) + 30), "Quarter " + (i+1)));
}
PieDataSet d = new PieDataSet(entries, "");
// space between slices
d.setSliceSpace(2f);
d.setColors(ColorTemplate.VORDIPLOM_COLORS);
PieData cd = new PieData(d);
return cd;
return new PieData(d);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.only_github, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/ListViewMultiChartActivity.java"));
startActivity(i);
break;
}
}
return true;
}
@Override
public void saveToGallery() { /* Intentionally left empty */ }
}

View file

@ -1,10 +1,16 @@
package com.xxmassdeveloper.mpchartexample;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
@ -12,12 +18,13 @@ import android.widget.TextView;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.Legend.LegendPosition;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.listener.ChartTouchListener;
import com.github.mikephil.charting.listener.OnChartGestureListener;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
@ -26,10 +33,10 @@ import java.util.ArrayList;
import java.util.List;
public class MultiLineChartActivity extends DemoBase implements OnSeekBarChangeListener,
OnChartValueSelectedListener {
OnChartGestureListener, OnChartValueSelectedListener {
private LineChart mChart;
private SeekBar mSeekBarX, mSeekBarY;
private LineChart chart;
private SeekBar seekBarX, seekBarY;
private TextView tvX, tvY;
@Override
@ -39,166 +46,74 @@ public class MultiLineChartActivity extends DemoBase implements OnSeekBarChangeL
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_linechart);
setTitle("MultiLineChartActivity");
tvX = findViewById(R.id.tvXMax);
tvY = findViewById(R.id.tvYMax);
mSeekBarX = findViewById(R.id.seekBar1);
mSeekBarX.setOnSeekBarChangeListener(this);
seekBarX = findViewById(R.id.seekBar1);
seekBarX.setOnSeekBarChangeListener(this);
mSeekBarY = findViewById(R.id.seekBar2);
mSeekBarY.setOnSeekBarChangeListener(this);
seekBarY = findViewById(R.id.seekBar2);
seekBarY.setOnSeekBarChangeListener(this);
mChart = findViewById(R.id.chart1);
mChart.setOnChartValueSelectedListener(this);
mChart.setDrawGridBackground(false);
mChart.getDescription().setEnabled(false);
mChart.setDrawBorders(false);
chart = findViewById(R.id.chart1);
chart.setOnChartValueSelectedListener(this);
mChart.getAxisLeft().setEnabled(false);
mChart.getAxisRight().setDrawAxisLine(false);
mChart.getAxisRight().setDrawGridLines(false);
mChart.getXAxis().setDrawAxisLine(false);
mChart.getXAxis().setDrawGridLines(false);
chart.setDrawGridBackground(false);
chart.getDescription().setEnabled(false);
chart.setDrawBorders(false);
chart.getAxisLeft().setEnabled(false);
chart.getAxisRight().setDrawAxisLine(false);
chart.getAxisRight().setDrawGridLines(false);
chart.getXAxis().setDrawAxisLine(false);
chart.getXAxis().setDrawGridLines(false);
// enable touch gestures
mChart.setTouchEnabled(true);
chart.setTouchEnabled(true);
// enable scaling and dragging
mChart.setDragEnabled(true);
mChart.setScaleEnabled(true);
chart.setDragEnabled(true);
chart.setScaleEnabled(true);
// if disabled, scaling can be done on x- and y-axis separately
mChart.setPinchZoom(false);
chart.setPinchZoom(false);
mSeekBarX.setProgress(20);
mSeekBarY.setProgress(100);
seekBarX.setProgress(20);
seekBarY.setProgress(100);
Legend l = mChart.getLegend();
Legend l = chart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
l.setOrientation(Legend.LegendOrientation.VERTICAL);
l.setDrawInside(false);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.line, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.actionToggleValues: {
List<ILineDataSet> sets = mChart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
set.setDrawValues(!set.isDrawValuesEnabled());
}
mChart.invalidate();
break;
}
case R.id.actionTogglePinch: {
if (mChart.isPinchZoomEnabled())
mChart.setPinchZoom(false);
else
mChart.setPinchZoom(true);
mChart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
break;
}
case R.id.actionToggleHighlight: {
if(mChart.getData() != null) {
mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled());
mChart.invalidate();
}
break;
}
case R.id.actionToggleFilled: {
List<ILineDataSet> sets = mChart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
if (set.isDrawFilledEnabled())
set.setDrawFilled(false);
else
set.setDrawFilled(true);
}
mChart.invalidate();
break;
}
case R.id.actionToggleCircles: {
List<ILineDataSet> sets = mChart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
if (set.isDrawCirclesEnabled())
set.setDrawCircles(false);
else
set.setDrawCircles(true);
}
mChart.invalidate();
break;
}
case R.id.actionSave: {
// mChart.saveToGallery("title"+System.currentTimeMillis());
mChart.saveToPath("title" + System.currentTimeMillis(), "");
break;
}
case R.id.animateX: {
mChart.animateX(3000);
break;
}
case R.id.animateY: {
mChart.animateY(3000);
break;
}
case R.id.animateXY: {
mChart.animateXY(3000, 3000);
break;
}
}
return true;
}
private int[] mColors = new int[] {
ColorTemplate.VORDIPLOM_COLORS[0],
private final int[] colors = new int[] {
ColorTemplate.VORDIPLOM_COLORS[0],
ColorTemplate.VORDIPLOM_COLORS[1],
ColorTemplate.VORDIPLOM_COLORS[2]
};
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mChart.resetTracking();
tvX.setText("" + (mSeekBarX.getProgress()));
tvY.setText("" + (mSeekBarY.getProgress()));
chart.resetTracking();
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
progress = seekBarX.getProgress();
tvX.setText(String.valueOf(seekBarX.getProgress()));
tvY.setText(String.valueOf(seekBarY.getProgress()));
ArrayList<ILineDataSet> dataSets = new ArrayList<>();
for (int z = 0; z < 3; z++) {
ArrayList<Entry> values = new ArrayList<Entry>();
ArrayList<Entry> values = new ArrayList<>();
for (int i = 0; i < mSeekBarX.getProgress(); i++) {
double val = (Math.random() * mSeekBarY.getProgress()) + 3;
for (int i = 0; i < progress; i++) {
double val = (Math.random() * seekBarY.getProgress()) + 3;
values.add(new Entry(i, (float) val));
}
@ -206,7 +121,7 @@ public class MultiLineChartActivity extends DemoBase implements OnSeekBarChangeL
d.setLineWidth(2.5f);
d.setCircleRadius(4f);
int color = mColors[z % mColors.length];
int color = colors[z % colors.length];
d.setColor(color);
d.setCircleColor(color);
dataSets.add(d);
@ -218,8 +133,207 @@ public class MultiLineChartActivity extends DemoBase implements OnSeekBarChangeL
((LineDataSet) dataSets.get(0)).setCircleColors(ColorTemplate.VORDIPLOM_COLORS);
LineData data = new LineData(dataSets);
mChart.setData(data);
mChart.invalidate();
chart.setData(data);
chart.invalidate();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.line, menu);
menu.removeItem(R.id.actionToggleIcons);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/MultiLineChartActivity.java"));
startActivity(i);
break;
}
case R.id.actionToggleValues: {
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
set.setDrawValues(!set.isDrawValuesEnabled());
}
chart.invalidate();
break;
}
/*
case R.id.actionToggleIcons: { break; }
*/
case R.id.actionTogglePinch: {
if (chart.isPinchZoomEnabled())
chart.setPinchZoom(false);
else
chart.setPinchZoom(true);
chart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled());
chart.notifyDataSetChanged();
break;
}
case R.id.actionToggleHighlight: {
if(chart.getData() != null) {
chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled());
chart.invalidate();
}
break;
}
case R.id.actionToggleFilled: {
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
if (set.isDrawFilledEnabled())
set.setDrawFilled(false);
else
set.setDrawFilled(true);
}
chart.invalidate();
break;
}
case R.id.actionToggleCircles: {
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
if (set.isDrawCirclesEnabled())
set.setDrawCircles(false);
else
set.setDrawCircles(true);
}
chart.invalidate();
break;
}
case R.id.actionToggleCubic: {
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
set.setMode(set.getMode() == LineDataSet.Mode.CUBIC_BEZIER
? LineDataSet.Mode.LINEAR
: LineDataSet.Mode.CUBIC_BEZIER);
}
chart.invalidate();
break;
}
case R.id.actionToggleStepped: {
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
set.setMode(set.getMode() == LineDataSet.Mode.STEPPED
? LineDataSet.Mode.LINEAR
: LineDataSet.Mode.STEPPED);
}
chart.invalidate();
break;
}
case R.id.actionToggleHorizontalCubic: {
List<ILineDataSet> sets = chart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
set.setMode(set.getMode() == LineDataSet.Mode.HORIZONTAL_BEZIER
? LineDataSet.Mode.LINEAR
: LineDataSet.Mode.HORIZONTAL_BEZIER);
}
chart.invalidate();
break;
}
case R.id.actionSave: {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveToGallery();
} else {
requestStoragePermission(chart);
}
break;
}
case R.id.animateX: {
chart.animateX(2000);
break;
}
case R.id.animateY: {
chart.animateY(2000);
break;
}
case R.id.animateXY: {
chart.animateXY(2000, 2000);
break;
}
}
return true;
}
@Override
public void saveToGallery() {
saveToGallery(chart, "MultiLineChartActivity");
}
@Override
public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
Log.i("Gesture", "START, x: " + me.getX() + ", y: " + me.getY());
}
@Override
public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
Log.i("Gesture", "END, lastGesture: " + lastPerformedGesture);
// un-highlight values after the gesture is finished and no single-tap
if(lastPerformedGesture != ChartTouchListener.ChartGesture.SINGLE_TAP)
chart.highlightValues(null); // or highlightTouch(null) for callback to onNothingSelected(...)
}
@Override
public void onChartLongPressed(MotionEvent me) {
Log.i("LongPress", "Chart long pressed.");
}
@Override
public void onChartDoubleTapped(MotionEvent me) {
Log.i("DoubleTap", "Chart double-tapped.");
}
@Override
public void onChartSingleTapped(MotionEvent me) {
Log.i("SingleTap", "Chart single-tapped.");
}
@Override
public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) {
Log.i("Fling", "Chart fling. VelocityX: " + velocityX + ", VelocityY: " + velocityY);
}
@Override
public void onChartScale(MotionEvent me, float scaleX, float scaleY) {
Log.i("Scale / Zoom", "ScaleX: " + scaleX + ", ScaleY: " + scaleY);
}
@Override
public void onChartTranslate(MotionEvent me, float dX, float dY) {
Log.i("Translate / Move", "dX: " + dX + ", dY: " + dY);
}
@Override
@ -230,16 +344,11 @@ public class MultiLineChartActivity extends DemoBase implements OnSeekBarChangeL
}
@Override
public void onNothingSelected() {
// TODO Auto-generated method stub
}
public void onNothingSelected() {}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {}
}

View file

@ -1,8 +1,12 @@
package com.xxmassdeveloper.mpchartexample;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
@ -17,11 +21,12 @@ import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
import java.util.ArrayList;
@SuppressWarnings("SameParameterValue")
public class PerformanceLineChart extends DemoBase implements OnSeekBarChangeListener {
private LineChart mChart;
private SeekBar mSeekBarValues;
private TextView mTvCount;
private LineChart chart;
private SeekBar seekBarValues;
private TextView tvCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -30,80 +35,51 @@ public class PerformanceLineChart extends DemoBase implements OnSeekBarChangeLis
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_performance_linechart);
mTvCount = findViewById(R.id.tvValueCount);
mSeekBarValues = findViewById(R.id.seekbarValues);
mTvCount.setText("500");
setTitle("PerformanceLineChart");
mSeekBarValues.setProgress(500);
mSeekBarValues.setOnSeekBarChangeListener(this);
tvCount = findViewById(R.id.tvValueCount);
seekBarValues = findViewById(R.id.seekbarValues);
seekBarValues.setOnSeekBarChangeListener(this);
mChart = findViewById(R.id.chart1);
mChart.setDrawGridBackground(false);
chart = findViewById(R.id.chart1);
chart.setDrawGridBackground(false);
// no description text
mChart.getDescription().setEnabled(false);
chart.getDescription().setEnabled(false);
// enable touch gestures
mChart.setTouchEnabled(true);
chart.setTouchEnabled(true);
// enable scaling and dragging
mChart.setDragEnabled(true);
mChart.setScaleEnabled(true);
chart.setDragEnabled(true);
chart.setScaleEnabled(true);
// if disabled, scaling can be done on x- and y-axis separately
mChart.setPinchZoom(false);
mChart.getAxisLeft().setDrawGridLines(false);
mChart.getAxisRight().setEnabled(false);
mChart.getXAxis().setDrawGridLines(true);
mChart.getXAxis().setDrawAxisLine(false);
chart.setPinchZoom(false);
// dont forget to refresh the drawing
mChart.invalidate();
}
chart.getAxisLeft().setDrawGridLines(false);
chart.getAxisRight().setEnabled(false);
chart.getXAxis().setDrawGridLines(true);
chart.getXAxis().setDrawAxisLine(false);
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int count = mSeekBarValues.getProgress() + 1000;
mTvCount.setText("" + count);
mChart.resetTracking();
setData(count, 500f);
// redraw
mChart.invalidate();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
seekBarValues.setProgress(9000);
// don't forget to refresh the drawing
chart.invalidate();
}
private void setData(int count, float range) {
ArrayList<Entry> yVals = new ArrayList<Entry>();
ArrayList<Entry> values = new ArrayList<>();
for (int i = 0; i < count; i++) {
float mult = (range + 1);
float val = (float) (Math.random() * mult) + 3;// + (float)
// ((mult *
// 0.1) / 10);
yVals.add(new Entry(i * 0.001f, val));
float val = (float) (Math.random() * (range + 1)) + 3;
values.add(new Entry(i * 0.001f, val));
}
// create a dataset and give it a type
LineDataSet set1 = new LineDataSet(yVals, "DataSet 1");
LineDataSet set1 = new LineDataSet(values, "DataSet 1");
set1.setColor(Color.BLACK);
set1.setLineWidth(0.5f);
set1.setDrawValues(false);
@ -111,14 +87,58 @@ public class PerformanceLineChart extends DemoBase implements OnSeekBarChangeLis
set1.setMode(LineDataSet.Mode.LINEAR);
set1.setDrawFilled(false);
// create a data object with the datasets
// create a data object with the data sets
LineData data = new LineData(set1);
// set data
mChart.setData(data);
chart.setData(data);
// get the legend (only possible after setting data)
Legend l = mChart.getLegend();
Legend l = chart.getLegend();
l.setEnabled(false);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.only_github, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PerformanceLineChart.java"));
startActivity(i);
break;
}
}
return true;
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int count = seekBarValues.getProgress() + 1000;
tvCount.setText(String.valueOf(count));
chart.resetTracking();
setData(count, 500f);
// redraw
chart.invalidate();
}
@Override
public void saveToGallery() { /* Intentionally left empty */ }
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
}

View file

@ -1,9 +1,14 @@
package com.xxmassdeveloper.mpchartexample;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
@ -19,7 +24,6 @@ import android.widget.TextView;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.Legend.LegendPosition;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
@ -37,8 +41,8 @@ import java.util.ArrayList;
public class PieChartActivity extends DemoBase implements OnSeekBarChangeListener,
OnChartValueSelectedListener {
private PieChart mChart;
private SeekBar mSeekBarX, mSeekBarY;
private PieChart chart;
private SeekBar seekBarX, seekBarY;
private TextView tvX, tvY;
@Override
@ -48,55 +52,57 @@ public class PieChartActivity extends DemoBase implements OnSeekBarChangeListene
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_piechart);
setTitle("PieChartActivity");
tvX = findViewById(R.id.tvXMax);
tvY = findViewById(R.id.tvYMax);
mSeekBarX = findViewById(R.id.seekBar1);
mSeekBarY = findViewById(R.id.seekBar2);
mSeekBarX.setProgress(4);
mSeekBarY.setProgress(10);
seekBarX = findViewById(R.id.seekBar1);
seekBarY = findViewById(R.id.seekBar2);
mChart = findViewById(R.id.chart1);
mChart.setUsePercentValues(true);
mChart.getDescription().setEnabled(false);
mChart.setExtraOffsets(5, 10, 5, 5);
seekBarX.setOnSeekBarChangeListener(this);
seekBarY.setOnSeekBarChangeListener(this);
mChart.setDragDecelerationFrictionCoef(0.95f);
chart = findViewById(R.id.chart1);
chart.setUsePercentValues(true);
chart.getDescription().setEnabled(false);
chart.setExtraOffsets(5, 10, 5, 5);
mChart.setCenterTextTypeface(mTfLight);
mChart.setCenterText(generateCenterSpannableText());
chart.setDragDecelerationFrictionCoef(0.95f);
mChart.setDrawHoleEnabled(true);
mChart.setHoleColor(Color.WHITE);
chart.setCenterTextTypeface(tfLight);
chart.setCenterText(generateCenterSpannableText());
mChart.setTransparentCircleColor(Color.WHITE);
mChart.setTransparentCircleAlpha(110);
chart.setDrawHoleEnabled(true);
chart.setHoleColor(Color.WHITE);
mChart.setHoleRadius(58f);
mChart.setTransparentCircleRadius(61f);
chart.setTransparentCircleColor(Color.WHITE);
chart.setTransparentCircleAlpha(110);
mChart.setDrawCenterText(true);
chart.setHoleRadius(58f);
chart.setTransparentCircleRadius(61f);
mChart.setRotationAngle(0);
chart.setDrawCenterText(true);
chart.setRotationAngle(0);
// enable rotation of the chart by touch
mChart.setRotationEnabled(true);
mChart.setHighlightPerTapEnabled(true);
chart.setRotationEnabled(true);
chart.setHighlightPerTapEnabled(true);
// mChart.setUnit("");
// mChart.setDrawUnitsInChart(true);
// chart.setUnit("");
// chart.setDrawUnitsInChart(true);
// add a selection listener
mChart.setOnChartValueSelectedListener(this);
chart.setOnChartValueSelectedListener(this);
seekBarX.setProgress(4);
seekBarY.setProgress(10);
setData(4, 100);
mChart.animateY(1400, Easing.EaseInOutQuad);
// mChart.spin(2000, 0, 360);
chart.animateY(1400, Easing.EaseInOutQuad);
// chart.spin(2000, 0, 360);
mSeekBarX.setOnSeekBarChangeListener(this);
mSeekBarY.setOnSeekBarChangeListener(this);
Legend l = mChart.getLegend();
Legend l = chart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
l.setOrientation(Legend.LegendOrientation.VERTICAL);
@ -106,106 +112,19 @@ public class PieChartActivity extends DemoBase implements OnSeekBarChangeListene
l.setYOffset(0f);
// entry label styling
mChart.setEntryLabelColor(Color.WHITE);
mChart.setEntryLabelTypeface(mTfRegular);
mChart.setEntryLabelTextSize(12f);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.pie, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.actionToggleValues: {
for (IDataSet<?> set : mChart.getData().getDataSets())
set.setDrawValues(!set.isDrawValuesEnabled());
mChart.invalidate();
break;
}
case R.id.actionToggleIcons: {
for (IDataSet<?> set : mChart.getData().getDataSets())
set.setDrawIcons(!set.isDrawIconsEnabled());
mChart.invalidate();
break;
}
case R.id.actionToggleHole: {
if (mChart.isDrawHoleEnabled())
mChart.setDrawHoleEnabled(false);
else
mChart.setDrawHoleEnabled(true);
mChart.invalidate();
break;
}
case R.id.actionDrawCenter: {
if (mChart.isDrawCenterTextEnabled())
mChart.setDrawCenterText(false);
else
mChart.setDrawCenterText(true);
mChart.invalidate();
break;
}
case R.id.actionToggleXVals: {
mChart.setDrawEntryLabels(!mChart.isDrawEntryLabelsEnabled());
mChart.invalidate();
break;
}
case R.id.actionSave: {
// mChart.saveToGallery("title"+System.currentTimeMillis());
mChart.saveToPath("title" + System.currentTimeMillis(), "");
break;
}
case R.id.actionTogglePercent:
mChart.setUsePercentValues(!mChart.isUsePercentValuesEnabled());
mChart.invalidate();
break;
case R.id.animateX: {
mChart.animateX(1400);
break;
}
case R.id.animateY: {
mChart.animateY(1400);
break;
}
case R.id.animateXY: {
mChart.animateXY(1400, 1400);
break;
}
case R.id.actionToggleSpin: {
mChart.spin(1000, mChart.getRotationAngle(), mChart.getRotationAngle() + 360, Easing.EaseInCubic);
break;
}
}
return true;
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvX.setText("" + (mSeekBarX.getProgress()));
tvY.setText("" + (mSeekBarY.getProgress()));
setData(mSeekBarX.getProgress(), mSeekBarY.getProgress());
chart.setEntryLabelColor(Color.WHITE);
chart.setEntryLabelTypeface(tfRegular);
chart.setEntryLabelTextSize(12f);
}
private void setData(int count, float range) {
float mult = range;
ArrayList<PieEntry> entries = new ArrayList<PieEntry>();
ArrayList<PieEntry> entries = new ArrayList<>();
// NOTE: The order of the entries when being added to the entries array determines their position around the center of
// the chart.
for (int i = 0; i < count ; i++) {
entries.add(new PieEntry((float) ((Math.random() * mult) + mult / 5),
mParties[i % mParties.length],
entries.add(new PieEntry((float) ((Math.random() * range) + range / 5),
parties[i % parties.length],
getResources().getDrawable(R.drawable.star)));
}
@ -219,7 +138,7 @@ public class PieChartActivity extends DemoBase implements OnSeekBarChangeListene
// add a lot of colors
ArrayList<Integer> colors = new ArrayList<Integer>();
ArrayList<Integer> colors = new ArrayList<>();
for (int c : ColorTemplate.VORDIPLOM_COLORS)
colors.add(c);
@ -245,13 +164,111 @@ public class PieChartActivity extends DemoBase implements OnSeekBarChangeListene
data.setValueFormatter(new PercentFormatter());
data.setValueTextSize(11f);
data.setValueTextColor(Color.WHITE);
data.setValueTypeface(mTfLight);
mChart.setData(data);
data.setValueTypeface(tfLight);
chart.setData(data);
// undo all highlights
mChart.highlightValues(null);
chart.highlightValues(null);
mChart.invalidate();
chart.invalidate();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.pie, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java"));
startActivity(i);
break;
}
case R.id.actionToggleValues: {
for (IDataSet<?> set : chart.getData().getDataSets())
set.setDrawValues(!set.isDrawValuesEnabled());
chart.invalidate();
break;
}
case R.id.actionToggleIcons: {
for (IDataSet<?> set : chart.getData().getDataSets())
set.setDrawIcons(!set.isDrawIconsEnabled());
chart.invalidate();
break;
}
case R.id.actionToggleHole: {
if (chart.isDrawHoleEnabled())
chart.setDrawHoleEnabled(false);
else
chart.setDrawHoleEnabled(true);
chart.invalidate();
break;
}
case R.id.actionDrawCenter: {
if (chart.isDrawCenterTextEnabled())
chart.setDrawCenterText(false);
else
chart.setDrawCenterText(true);
chart.invalidate();
break;
}
case R.id.actionToggleXValues: {
chart.setDrawEntryLabels(!chart.isDrawEntryLabelsEnabled());
chart.invalidate();
break;
}
case R.id.actionTogglePercent:
chart.setUsePercentValues(!chart.isUsePercentValuesEnabled());
chart.invalidate();
break;
case R.id.animateX: {
chart.animateX(1400);
break;
}
case R.id.animateY: {
chart.animateY(1400);
break;
}
case R.id.animateXY: {
chart.animateXY(1400, 1400);
break;
}
case R.id.actionToggleSpin: {
chart.spin(1000, chart.getRotationAngle(), chart.getRotationAngle() + 360, Easing.EaseInOutCubic);
break;
}
case R.id.actionSave: {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveToGallery();
} else {
requestStoragePermission(chart);
}
break;
}
}
return true;
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvX.setText(String.valueOf(seekBarX.getProgress()));
tvY.setText(String.valueOf(seekBarY.getProgress()));
setData(seekBarX.getProgress(), seekBarY.getProgress());
}
@Override
public void saveToGallery() {
saveToGallery(chart, "PieChartActivity");
}
private SpannableString generateCenterSpannableText() {
@ -282,14 +299,8 @@ public class PieChartActivity extends DemoBase implements OnSeekBarChangeListene
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {}
}

View file

@ -1,9 +1,14 @@
package com.xxmassdeveloper.mpchartexample;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
@ -35,8 +40,8 @@ import java.util.ArrayList;
public class PiePolylineChartActivity extends DemoBase implements OnSeekBarChangeListener,
OnChartValueSelectedListener {
private PieChart mChart;
private SeekBar mSeekBarX, mSeekBarY;
private PieChart chart;
private SeekBar seekBarX, seekBarY;
private TextView tvX, tvY;
private Typeface tf;
@ -48,59 +53,61 @@ public class PiePolylineChartActivity extends DemoBase implements OnSeekBarChang
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_piechart);
setTitle("PiePolylineChartActivity");
tvX = findViewById(R.id.tvXMax);
tvY = findViewById(R.id.tvYMax);
mSeekBarX = findViewById(R.id.seekBar1);
mSeekBarY = findViewById(R.id.seekBar2);
seekBarX = findViewById(R.id.seekBar1);
seekBarY = findViewById(R.id.seekBar2);
mSeekBarY.setProgress(10);
seekBarX.setOnSeekBarChangeListener(this);
seekBarY.setOnSeekBarChangeListener(this);
mSeekBarX.setOnSeekBarChangeListener(this);
mSeekBarY.setOnSeekBarChangeListener(this);
chart = findViewById(R.id.chart1);
chart.setUsePercentValues(true);
chart.getDescription().setEnabled(false);
chart.setExtraOffsets(5, 10, 5, 5);
mChart = findViewById(R.id.chart1);
mChart.setUsePercentValues(true);
mChart.getDescription().setEnabled(false);
mChart.setExtraOffsets(5, 10, 5, 5);
mChart.setDragDecelerationFrictionCoef(0.95f);
chart.setDragDecelerationFrictionCoef(0.95f);
tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");
mChart.setCenterTextTypeface(Typeface.createFromAsset(getAssets(), "OpenSans-Light.ttf"));
mChart.setCenterText(generateCenterSpannableText());
chart.setCenterTextTypeface(Typeface.createFromAsset(getAssets(), "OpenSans-Light.ttf"));
chart.setCenterText(generateCenterSpannableText());
mChart.setExtraOffsets(20.f, 0.f, 20.f, 0.f);
chart.setExtraOffsets(20.f, 0.f, 20.f, 0.f);
mChart.setDrawHoleEnabled(true);
mChart.setHoleColor(Color.WHITE);
chart.setDrawHoleEnabled(true);
chart.setHoleColor(Color.WHITE);
mChart.setTransparentCircleColor(Color.WHITE);
mChart.setTransparentCircleAlpha(110);
chart.setTransparentCircleColor(Color.WHITE);
chart.setTransparentCircleAlpha(110);
mChart.setHoleRadius(58f);
mChart.setTransparentCircleRadius(61f);
chart.setHoleRadius(58f);
chart.setTransparentCircleRadius(61f);
mChart.setDrawCenterText(true);
chart.setDrawCenterText(true);
mChart.setRotationAngle(0);
chart.setRotationAngle(0);
// enable rotation of the chart by touch
mChart.setRotationEnabled(true);
mChart.setHighlightPerTapEnabled(true);
chart.setRotationEnabled(true);
chart.setHighlightPerTapEnabled(true);
// mChart.setUnit("");
// mChart.setDrawUnitsInChart(true);
// chart.setUnit("");
// chart.setDrawUnitsInChart(true);
// add a selection listener
mChart.setOnChartValueSelectedListener(this);
chart.setOnChartValueSelectedListener(this);
seekBarX.setProgress(4);
seekBarY.setProgress(100);
setData(4, 100);
mChart.animateY(1400, Easing.EaseInOutQuad);
// mChart.spin(2000, 0, 360);
chart.animateY(1400, Easing.EaseInOutQuad);
// chart.spin(2000, 0, 360);
Legend l = mChart.getLegend();
Legend l = chart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
l.setOrientation(Legend.LegendOrientation.VERTICAL);
@ -108,89 +115,14 @@ public class PiePolylineChartActivity extends DemoBase implements OnSeekBarChang
l.setEnabled(false);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.pie, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.actionToggleValues: {
for (IDataSet<?> set : mChart.getData().getDataSets())
set.setDrawValues(!set.isDrawValuesEnabled());
mChart.invalidate();
break;
}
case R.id.actionToggleHole: {
if (mChart.isDrawHoleEnabled())
mChart.setDrawHoleEnabled(false);
else
mChart.setDrawHoleEnabled(true);
mChart.invalidate();
break;
}
case R.id.actionDrawCenter: {
if (mChart.isDrawCenterTextEnabled())
mChart.setDrawCenterText(false);
else
mChart.setDrawCenterText(true);
mChart.invalidate();
break;
}
case R.id.actionToggleXVals: {
mChart.setDrawEntryLabels(!mChart.isDrawEntryLabelsEnabled());
mChart.invalidate();
break;
}
case R.id.actionSave: {
// mChart.saveToGallery("title"+System.currentTimeMillis());
mChart.saveToPath("title" + System.currentTimeMillis(), "");
break;
}
case R.id.actionTogglePercent:
mChart.setUsePercentValues(!mChart.isUsePercentValuesEnabled());
mChart.invalidate();
break;
case R.id.animateX: {
mChart.animateX(1400);
break;
}
case R.id.animateY: {
mChart.animateY(1400);
break;
}
case R.id.animateXY: {
mChart.animateXY(1400, 1400);
break;
}
}
return true;
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvX.setText("" + (mSeekBarX.getProgress()));
tvY.setText("" + (mSeekBarY.getProgress()));
setData(mSeekBarX.getProgress(), mSeekBarY.getProgress());
}
private void setData(int count, float range) {
float mult = range;
ArrayList<PieEntry> entries = new ArrayList<PieEntry>();
ArrayList<PieEntry> entries = new ArrayList<>();
// NOTE: The order of the entries when being added to the entries array determines their position around the center of
// the chart.
for (int i = 0; i < count; i++) {
entries.add(new PieEntry((float) (Math.random() * mult) + mult / 5, mParties[i % mParties.length]));
entries.add(new PieEntry((float) (Math.random() * range) + range / 5, parties[i % parties.length]));
}
PieDataSet dataSet = new PieDataSet(entries, "Election Results");
@ -199,7 +131,7 @@ public class PiePolylineChartActivity extends DemoBase implements OnSeekBarChang
// add a lot of colors
ArrayList<Integer> colors = new ArrayList<Integer>();
ArrayList<Integer> colors = new ArrayList<>();
for (int c : ColorTemplate.VORDIPLOM_COLORS)
colors.add(c);
@ -235,12 +167,103 @@ public class PiePolylineChartActivity extends DemoBase implements OnSeekBarChang
data.setValueTextSize(11f);
data.setValueTextColor(Color.BLACK);
data.setValueTypeface(tf);
mChart.setData(data);
chart.setData(data);
// undo all highlights
mChart.highlightValues(null);
chart.highlightValues(null);
mChart.invalidate();
chart.invalidate();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.pie, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PiePolylineChartActivity.java"));
startActivity(i);
break;
}
case R.id.actionToggleValues: {
for (IDataSet<?> set : chart.getData().getDataSets())
set.setDrawValues(!set.isDrawValuesEnabled());
chart.invalidate();
break;
}
case R.id.actionToggleHole: {
if (chart.isDrawHoleEnabled())
chart.setDrawHoleEnabled(false);
else
chart.setDrawHoleEnabled(true);
chart.invalidate();
break;
}
case R.id.actionDrawCenter: {
if (chart.isDrawCenterTextEnabled())
chart.setDrawCenterText(false);
else
chart.setDrawCenterText(true);
chart.invalidate();
break;
}
case R.id.actionToggleXValues: {
chart.setDrawEntryLabels(!chart.isDrawEntryLabelsEnabled());
chart.invalidate();
break;
}
case R.id.actionTogglePercent:
chart.setUsePercentValues(!chart.isUsePercentValuesEnabled());
chart.invalidate();
break;
case R.id.animateX: {
chart.animateX(1400);
break;
}
case R.id.animateY: {
chart.animateY(1400);
break;
}
case R.id.animateXY: {
chart.animateXY(1400, 1400);
break;
}
case R.id.actionToggleSpin: {
chart.spin(1000, chart.getRotationAngle(), chart.getRotationAngle() + 360, Easing.EaseInOutCubic);
break;
}
case R.id.actionSave: {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveToGallery();
} else {
requestStoragePermission(chart);
}
break;
}
}
return true;
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvX.setText(String.valueOf(seekBarX.getProgress()));
tvY.setText(String.valueOf(seekBarY.getProgress()));
setData(seekBarX.getProgress(), seekBarY.getProgress());
}
@Override
public void saveToGallery() {
saveToGallery(chart, "PiePolylineChartActivity");
}
private SpannableString generateCenterSpannableText() {
@ -271,14 +294,8 @@ public class PiePolylineChartActivity extends DemoBase implements OnSeekBarChang
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {}
}

View file

@ -1,13 +1,16 @@
package com.xxmassdeveloper.mpchartexample;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.RadarChart;
@ -29,49 +32,46 @@ import java.util.ArrayList;
public class RadarChartActivity extends DemoBase {
private RadarChart mChart;
private RadarChart chart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_radarchart_noseekbar);
setContentView(R.layout.activity_radarchart);
TextView tv = findViewById(R.id.textView);
tv.setTypeface(mTfLight);
tv.setTextColor(Color.WHITE);
tv.setBackgroundColor(Color.rgb(60, 65, 82));
setTitle("RadarChartActivity");
mChart = findViewById(R.id.chart1);
mChart.setBackgroundColor(Color.rgb(60, 65, 82));
chart = findViewById(R.id.chart1);
chart.setBackgroundColor(Color.rgb(60, 65, 82));
mChart.getDescription().setEnabled(false);
chart.getDescription().setEnabled(false);
mChart.setWebLineWidth(1f);
mChart.setWebColor(Color.LTGRAY);
mChart.setWebLineWidthInner(1f);
mChart.setWebColorInner(Color.LTGRAY);
mChart.setWebAlpha(100);
chart.setWebLineWidth(1f);
chart.setWebColor(Color.LTGRAY);
chart.setWebLineWidthInner(1f);
chart.setWebColorInner(Color.LTGRAY);
chart.setWebAlpha(100);
// create a custom MarkerView (extend MarkerView) and specify the layout
// to use for it
MarkerView mv = new RadarMarkerView(this, R.layout.radar_markerview);
mv.setChartView(mChart); // For bounds control
mChart.setMarker(mv); // Set the marker to the chart
mv.setChartView(chart); // For bounds control
chart.setMarker(mv); // Set the marker to the chart
setData();
mChart.animateXY(1400, 1400, Easing.EaseInOutQuad);
chart.animateXY(1400, 1400, Easing.EaseInOutQuad);
XAxis xAxis = mChart.getXAxis();
xAxis.setTypeface(mTfLight);
XAxis xAxis = chart.getXAxis();
xAxis.setTypeface(tfLight);
xAxis.setTextSize(9f);
xAxis.setYOffset(0f);
xAxis.setXOffset(0f);
xAxis.setValueFormatter(new IAxisValueFormatter() {
private String[] mActivities = new String[]{"Burger", "Steak", "Salad", "Pasta", "Pizza"};
private final String[] mActivities = new String[]{"Burger", "Steak", "Salad", "Pasta", "Pizza"};
@Override
public String getFormattedValue(float value, AxisBase axis) {
@ -80,139 +80,41 @@ public class RadarChartActivity extends DemoBase {
});
xAxis.setTextColor(Color.WHITE);
YAxis yAxis = mChart.getYAxis();
yAxis.setTypeface(mTfLight);
YAxis yAxis = chart.getYAxis();
yAxis.setTypeface(tfLight);
yAxis.setLabelCount(5, false);
yAxis.setTextSize(9f);
yAxis.setAxisMinimum(0f);
yAxis.setAxisMaximum(80f);
yAxis.setDrawLabels(false);
Legend l = mChart.getLegend();
Legend l = chart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
l.setDrawInside(false);
l.setTypeface(mTfLight);
l.setTypeface(tfLight);
l.setXEntrySpace(7f);
l.setYEntrySpace(5f);
l.setTextColor(Color.WHITE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.radar, menu);
return true;
}
private void setData() {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.actionToggleValues: {
for (IDataSet<?> set : mChart.getData().getDataSets())
set.setDrawValues(!set.isDrawValuesEnabled());
mChart.invalidate();
break;
}
case R.id.actionToggleHighlight: {
if (mChart.getData() != null) {
mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled());
mChart.invalidate();
}
break;
}
case R.id.actionToggleRotate: {
if (mChart.isRotationEnabled())
mChart.setRotationEnabled(false);
else
mChart.setRotationEnabled(true);
mChart.invalidate();
break;
}
case R.id.actionToggleFilled: {
ArrayList<IRadarDataSet> sets = (ArrayList<IRadarDataSet>) mChart.getData()
.getDataSets();
for (IRadarDataSet set : sets) {
if (set.isDrawFilledEnabled())
set.setDrawFilled(false);
else
set.setDrawFilled(true);
}
mChart.invalidate();
break;
}
case R.id.actionToggleHighlightCircle: {
ArrayList<IRadarDataSet> sets = (ArrayList<IRadarDataSet>) mChart.getData()
.getDataSets();
for (IRadarDataSet set : sets) {
set.setDrawHighlightCircleEnabled(!set.isDrawHighlightCircleEnabled());
}
mChart.invalidate();
break;
}
case R.id.actionSave: {
if (mChart.saveToPath("title" + System.currentTimeMillis(), "")) {
Toast.makeText(getApplicationContext(), "Saving SUCCESSFUL!",
Toast.LENGTH_SHORT).show();
} else
Toast.makeText(getApplicationContext(), "Saving FAILED!", Toast.LENGTH_SHORT)
.show();
break;
}
case R.id.actionToggleXLabels: {
mChart.getXAxis().setEnabled(!mChart.getXAxis().isEnabled());
mChart.notifyDataSetChanged();
mChart.invalidate();
break;
}
case R.id.actionToggleYLabels: {
mChart.getYAxis().setEnabled(!mChart.getYAxis().isEnabled());
mChart.invalidate();
break;
}
case R.id.animateX: {
mChart.animateX(1400);
break;
}
case R.id.animateY: {
mChart.animateY(1400);
break;
}
case R.id.animateXY: {
mChart.animateXY(1400, 1400);
break;
}
case R.id.actionToggleSpin: {
mChart.spin(2000, mChart.getRotationAngle(), mChart.getRotationAngle() + 360, Easing.EaseInCubic);
break;
}
}
return true;
}
public void setData() {
float mult = 80;
float mul = 80;
float min = 20;
int cnt = 5;
ArrayList<RadarEntry> entries1 = new ArrayList<RadarEntry>();
ArrayList<RadarEntry> entries2 = new ArrayList<RadarEntry>();
ArrayList<RadarEntry> entries1 = new ArrayList<>();
ArrayList<RadarEntry> entries2 = new ArrayList<>();
// NOTE: The order of the entries when being added to the entries array determines their position around the center of
// the chart.
for (int i = 0; i < cnt; i++) {
float val1 = (float) (Math.random() * mult) + min;
float val1 = (float) (Math.random() * mul) + min;
entries1.add(new RadarEntry(val1));
float val2 = (float) (Math.random() * mult) + min;
float val2 = (float) (Math.random() * mul) + min;
entries2.add(new RadarEntry(val2));
}
@ -234,17 +136,125 @@ public class RadarChartActivity extends DemoBase {
set2.setDrawHighlightCircleEnabled(true);
set2.setDrawHighlightIndicators(false);
ArrayList<IRadarDataSet> sets = new ArrayList<IRadarDataSet>();
ArrayList<IRadarDataSet> sets = new ArrayList<>();
sets.add(set1);
sets.add(set2);
RadarData data = new RadarData(sets);
data.setValueTypeface(mTfLight);
data.setValueTypeface(tfLight);
data.setValueTextSize(8f);
data.setDrawValues(false);
data.setValueTextColor(Color.WHITE);
mChart.setData(data);
mChart.invalidate();
chart.setData(data);
chart.invalidate();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.radar, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/RadarChartActivity.java"));
startActivity(i);
break;
}
case R.id.actionToggleValues: {
for (IDataSet<?> set : chart.getData().getDataSets())
set.setDrawValues(!set.isDrawValuesEnabled());
chart.invalidate();
break;
}
case R.id.actionToggleHighlight: {
if (chart.getData() != null) {
chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled());
chart.invalidate();
}
break;
}
case R.id.actionToggleRotate: {
if (chart.isRotationEnabled())
chart.setRotationEnabled(false);
else
chart.setRotationEnabled(true);
chart.invalidate();
break;
}
case R.id.actionToggleFilled: {
ArrayList<IRadarDataSet> sets = (ArrayList<IRadarDataSet>) chart.getData()
.getDataSets();
for (IRadarDataSet set : sets) {
if (set.isDrawFilledEnabled())
set.setDrawFilled(false);
else
set.setDrawFilled(true);
}
chart.invalidate();
break;
}
case R.id.actionToggleHighlightCircle: {
ArrayList<IRadarDataSet> sets = (ArrayList<IRadarDataSet>) chart.getData()
.getDataSets();
for (IRadarDataSet set : sets) {
set.setDrawHighlightCircleEnabled(!set.isDrawHighlightCircleEnabled());
}
chart.invalidate();
break;
}
case R.id.actionToggleXLabels: {
chart.getXAxis().setEnabled(!chart.getXAxis().isEnabled());
chart.notifyDataSetChanged();
chart.invalidate();
break;
}
case R.id.actionToggleYLabels: {
chart.getYAxis().setEnabled(!chart.getYAxis().isEnabled());
chart.invalidate();
break;
}
case R.id.animateX: {
chart.animateX(1400);
break;
}
case R.id.animateY: {
chart.animateY(1400);
break;
}
case R.id.animateXY: {
chart.animateXY(1400, 1400);
break;
}
case R.id.actionToggleSpin: {
chart.spin(2000, chart.getRotationAngle(), chart.getRotationAngle() + 360, Easing.EaseInOutCubic);
break;
}
case R.id.actionSave: {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveToGallery();
} else {
requestStoragePermission(chart);
}
break;
}
}
return true;
}
@Override
public void saveToGallery() {
saveToGallery(chart, "RadarChartActivity");
}
}

View file

@ -1,8 +1,13 @@
package com.xxmassdeveloper.mpchartexample;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
@ -27,7 +32,7 @@ import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
public class RealtimeLineChartActivity extends DemoBase implements
OnChartValueSelectedListener {
private LineChart mChart;
private LineChart chart;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -36,89 +41,64 @@ public class RealtimeLineChartActivity extends DemoBase implements
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_realtime_linechart);
mChart = findViewById(R.id.chart1);
mChart.setOnChartValueSelectedListener(this);
setTitle("RealtimeLineChartActivity");
chart = findViewById(R.id.chart1);
chart.setOnChartValueSelectedListener(this);
// enable description text
mChart.getDescription().setEnabled(true);
chart.getDescription().setEnabled(true);
// enable touch gestures
mChart.setTouchEnabled(true);
chart.setTouchEnabled(true);
// enable scaling and dragging
mChart.setDragEnabled(true);
mChart.setScaleEnabled(true);
mChart.setDrawGridBackground(false);
chart.setDragEnabled(true);
chart.setScaleEnabled(true);
chart.setDrawGridBackground(false);
// if disabled, scaling can be done on x- and y-axis separately
mChart.setPinchZoom(true);
chart.setPinchZoom(true);
// set an alternative background color
mChart.setBackgroundColor(Color.LTGRAY);
chart.setBackgroundColor(Color.LTGRAY);
LineData data = new LineData();
data.setValueTextColor(Color.WHITE);
// add empty data
mChart.setData(data);
chart.setData(data);
// get the legend (only possible after setting data)
Legend l = mChart.getLegend();
Legend l = chart.getLegend();
// modify the legend ...
l.setForm(LegendForm.LINE);
l.setTypeface(mTfLight);
l.setTypeface(tfLight);
l.setTextColor(Color.WHITE);
XAxis xl = mChart.getXAxis();
xl.setTypeface(mTfLight);
XAxis xl = chart.getXAxis();
xl.setTypeface(tfLight);
xl.setTextColor(Color.WHITE);
xl.setDrawGridLines(false);
xl.setAvoidFirstLastClipping(true);
xl.setEnabled(true);
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setTypeface(mTfLight);
YAxis leftAxis = chart.getAxisLeft();
leftAxis.setTypeface(tfLight);
leftAxis.setTextColor(Color.WHITE);
leftAxis.setAxisMaximum(100f);
leftAxis.setAxisMinimum(0f);
leftAxis.setDrawGridLines(true);
YAxis rightAxis = mChart.getAxisRight();
YAxis rightAxis = chart.getAxisRight();
rightAxis.setEnabled(false);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.realtime, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.actionAdd: {
addEntry();
break;
}
case R.id.actionClear: {
mChart.clearValues();
Toast.makeText(this, "Chart cleared!", Toast.LENGTH_SHORT).show();
break;
}
case R.id.actionFeedMultiple: {
feedMultiple();
break;
}
}
return true;
}
private void addEntry() {
LineData data = mChart.getData();
LineData data = chart.getData();
if (data != null) {
@ -134,17 +114,17 @@ public class RealtimeLineChartActivity extends DemoBase implements
data.notifyDataChanged();
// let the chart know it's data has changed
mChart.notifyDataSetChanged();
chart.notifyDataSetChanged();
// limit the number of visible entries
mChart.setVisibleXRangeMaximum(120);
// mChart.setVisibleYRange(30, AxisDependency.LEFT);
chart.setVisibleXRangeMaximum(120);
// chart.setVisibleYRange(30, AxisDependency.LEFT);
// move to the latest entry
mChart.moveViewToX(data.getEntryCount());
chart.moveViewToX(data.getEntryCount());
// this automatically refreshes the chart (calls invalidate())
// mChart.moveViewTo(data.getXValCount()-7, 55f,
// chart.moveViewTo(data.getXValCount()-7, 55f,
// AxisDependency.LEFT);
}
}
@ -193,7 +173,6 @@ public class RealtimeLineChartActivity extends DemoBase implements
try {
Thread.sleep(25);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@ -203,6 +182,52 @@ public class RealtimeLineChartActivity extends DemoBase implements
thread.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.realtime, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/RealtimeLineChartActivity.java"));
startActivity(i);
break;
}
case R.id.actionAdd: {
addEntry();
break;
}
case R.id.actionClear: {
chart.clearValues();
Toast.makeText(this, "Chart cleared!", Toast.LENGTH_SHORT).show();
break;
}
case R.id.actionFeedMultiple: {
feedMultiple();
break;
}
case R.id.actionSave: {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveToGallery();
} else {
requestStoragePermission(chart);
}
break;
}
}
return true;
}
@Override
public void saveToGallery() {
saveToGallery(chart, "RealtimeLineChartActivity");
}
@Override
public void onValueSelected(Entry e, Highlight h) {
Log.i("Entry selected", e.toString());

View file

@ -1,7 +1,12 @@
package com.xxmassdeveloper.mpchartexample;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
@ -12,7 +17,6 @@ import android.widget.TextView;
import com.github.mikephil.charting.charts.ScatterChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.Legend.LegendPosition;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
@ -31,10 +35,10 @@ import java.util.List;
public class ScatterChartActivity extends DemoBase implements OnSeekBarChangeListener,
OnChartValueSelectedListener {
private ScatterChart mChart;
private SeekBar mSeekBarX, mSeekBarY;
private ScatterChart chart;
private SeekBar seekBarX, seekBarY;
private TextView tvX, tvY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -42,52 +46,109 @@ public class ScatterChartActivity extends DemoBase implements OnSeekBarChangeLis
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_scatterchart);
setTitle("ScatterChartActivity");
tvX = findViewById(R.id.tvXMax);
tvY = findViewById(R.id.tvYMax);
mSeekBarX = findViewById(R.id.seekBar1);
mSeekBarX.setOnSeekBarChangeListener(this);
seekBarX = findViewById(R.id.seekBar1);
seekBarX.setOnSeekBarChangeListener(this);
mSeekBarY = findViewById(R.id.seekBar2);
mSeekBarY.setOnSeekBarChangeListener(this);
seekBarY = findViewById(R.id.seekBar2);
seekBarY.setOnSeekBarChangeListener(this);
mChart = findViewById(R.id.chart1);
mChart.getDescription().setEnabled(false);
mChart.setOnChartValueSelectedListener(this);
chart = findViewById(R.id.chart1);
chart.getDescription().setEnabled(false);
chart.setOnChartValueSelectedListener(this);
mChart.setDrawGridBackground(false);
mChart.setTouchEnabled(true);
mChart.setMaxHighlightDistance(50f);
chart.setDrawGridBackground(false);
chart.setTouchEnabled(true);
chart.setMaxHighlightDistance(50f);
// enable scaling and dragging
mChart.setDragEnabled(true);
mChart.setScaleEnabled(true);
chart.setDragEnabled(true);
chart.setScaleEnabled(true);
mChart.setMaxVisibleValueCount(200);
mChart.setPinchZoom(true);
chart.setMaxVisibleValueCount(200);
chart.setPinchZoom(true);
mSeekBarX.setProgress(45);
mSeekBarY.setProgress(100);
seekBarX.setProgress(45);
seekBarY.setProgress(100);
Legend l = mChart.getLegend();
Legend l = chart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
l.setOrientation(Legend.LegendOrientation.VERTICAL);
l.setDrawInside(false);
l.setTypeface(mTfLight);
l.setTypeface(tfLight);
l.setXOffset(5f);
YAxis yl = mChart.getAxisLeft();
yl.setTypeface(mTfLight);
YAxis yl = chart.getAxisLeft();
yl.setTypeface(tfLight);
yl.setAxisMinimum(0f); // this replaces setStartAtZero(true)
mChart.getAxisRight().setEnabled(false);
XAxis xl = mChart.getXAxis();
xl.setTypeface(mTfLight);
chart.getAxisRight().setEnabled(false);
XAxis xl = chart.getXAxis();
xl.setTypeface(tfLight);
xl.setDrawGridLines(false);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvX.setText(String.valueOf(seekBarX.getProgress()));
tvY.setText(String.valueOf(seekBarY.getProgress()));
ArrayList<Entry> values1 = new ArrayList<>();
ArrayList<Entry> values2 = new ArrayList<>();
ArrayList<Entry> values3 = new ArrayList<>();
for (int i = 0; i < seekBarX.getProgress(); i++) {
float val = (float) (Math.random() * seekBarY.getProgress()) + 3;
values1.add(new Entry(i, val));
}
for (int i = 0; i < seekBarX.getProgress(); i++) {
float val = (float) (Math.random() * seekBarY.getProgress()) + 3;
values2.add(new Entry(i+0.33f, val));
}
for (int i = 0; i < seekBarX.getProgress(); i++) {
float val = (float) (Math.random() * seekBarY.getProgress()) + 3;
values3.add(new Entry(i+0.66f, val));
}
// create a dataset and give it a type
ScatterDataSet set1 = new ScatterDataSet(values1, "DS 1");
set1.setScatterShape(ScatterChart.ScatterShape.SQUARE);
set1.setColor(ColorTemplate.COLORFUL_COLORS[0]);
ScatterDataSet set2 = new ScatterDataSet(values2, "DS 2");
set2.setScatterShape(ScatterChart.ScatterShape.CIRCLE);
set2.setScatterShapeHoleColor(ColorTemplate.COLORFUL_COLORS[3]);
set2.setScatterShapeHoleRadius(3f);
set2.setColor(ColorTemplate.COLORFUL_COLORS[1]);
ScatterDataSet set3 = new ScatterDataSet(values3, "DS 3");
set3.setShapeRenderer(new CustomScatterShapeRenderer());
set3.setColor(ColorTemplate.COLORFUL_COLORS[2]);
set1.setScatterShapeSize(8f);
set2.setScatterShapeSize(8f);
set3.setScatterShapeSize(8f);
ArrayList<IScatterDataSet> dataSets = new ArrayList<>();
dataSets.add(set1); // add the data sets
dataSets.add(set2);
dataSets.add(set3);
// create a data object with the data sets
ScatterData data = new ScatterData(dataSets);
data.setValueTypeface(tfLight);
chart.setData(data);
chart.invalidate();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.scatter, menu);
@ -98,8 +159,14 @@ public class ScatterChartActivity extends DemoBase implements OnSeekBarChangeLis
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/ScatterChartActivity.java"));
startActivity(i);
break;
}
case R.id.actionToggleValues: {
List<IScatterDataSet> sets = mChart.getData()
List<IScatterDataSet> sets = chart.getData()
.getDataSets();
for (IScatterDataSet iSet : sets) {
@ -108,46 +175,49 @@ public class ScatterChartActivity extends DemoBase implements OnSeekBarChangeLis
set.setDrawValues(!set.isDrawValuesEnabled());
}
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionToggleHighlight: {
if(mChart.getData() != null) {
mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled());
mChart.invalidate();
if(chart.getData() != null) {
chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled());
chart.invalidate();
}
break;
}
case R.id.actionTogglePinch: {
if (mChart.isPinchZoomEnabled())
mChart.setPinchZoom(false);
if (chart.isPinchZoomEnabled())
chart.setPinchZoom(false);
else
mChart.setPinchZoom(true);
chart.setPinchZoom(true);
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
break;
}
case R.id.actionSave: {
// mChart.saveToGallery("title"+System.currentTimeMillis());
mChart.saveToPath("title" + System.currentTimeMillis(), "");
chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled());
chart.notifyDataSetChanged();
break;
}
case R.id.animateX: {
mChart.animateX(3000);
chart.animateX(3000);
break;
}
case R.id.animateY: {
mChart.animateY(3000);
chart.animateY(3000);
break;
}
case R.id.animateXY: {
mChart.animateXY(3000, 3000);
chart.animateXY(3000, 3000);
break;
}
case R.id.actionSave: {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveToGallery();
} else {
requestStoragePermission(chart);
}
break;
}
}
@ -155,58 +225,8 @@ public class ScatterChartActivity extends DemoBase implements OnSeekBarChangeLis
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvX.setText("" + (mSeekBarX.getProgress() + 1));
tvY.setText("" + (mSeekBarY.getProgress()));
ArrayList<Entry> yVals1 = new ArrayList<Entry>();
ArrayList<Entry> yVals2 = new ArrayList<Entry>();
ArrayList<Entry> yVals3 = new ArrayList<Entry>();
for (int i = 0; i < mSeekBarX.getProgress(); i++) {
float val = (float) (Math.random() * mSeekBarY.getProgress()) + 3;
yVals1.add(new Entry(i, val));
}
for (int i = 0; i < mSeekBarX.getProgress(); i++) {
float val = (float) (Math.random() * mSeekBarY.getProgress()) + 3;
yVals2.add(new Entry(i+0.33f, val));
}
for (int i = 0; i < mSeekBarX.getProgress(); i++) {
float val = (float) (Math.random() * mSeekBarY.getProgress()) + 3;
yVals3.add(new Entry(i+0.66f, val));
}
// create a dataset and give it a type
ScatterDataSet set1 = new ScatterDataSet(yVals1, "DS 1");
set1.setScatterShape(ScatterChart.ScatterShape.SQUARE);
set1.setColor(ColorTemplate.COLORFUL_COLORS[0]);
ScatterDataSet set2 = new ScatterDataSet(yVals2, "DS 2");
set2.setScatterShape(ScatterChart.ScatterShape.CIRCLE);
set2.setScatterShapeHoleColor(ColorTemplate.COLORFUL_COLORS[3]);
set2.setScatterShapeHoleRadius(3f);
set2.setColor(ColorTemplate.COLORFUL_COLORS[1]);
ScatterDataSet set3 = new ScatterDataSet(yVals3, "DS 3");
set3.setShapeRenderer(new CustomScatterShapeRenderer());
set3.setColor(ColorTemplate.COLORFUL_COLORS[2]);
set1.setScatterShapeSize(8f);
set2.setScatterShapeSize(8f);
set3.setScatterShapeSize(8f);
ArrayList<IScatterDataSet> dataSets = new ArrayList<IScatterDataSet>();
dataSets.add(set1); // add the datasets
dataSets.add(set2);
dataSets.add(set3);
// create a data object with the datasets
ScatterData data = new ScatterData(dataSets);
data.setValueTypeface(mTfLight);
mChart.setData(data);
mChart.invalidate();
public void saveToGallery() {
saveToGallery(chart, "ScatterChartActivity");
}
@Override
@ -217,20 +237,11 @@ public class ScatterChartActivity extends DemoBase implements OnSeekBarChangeLis
}
@Override
public void onNothingSelected() {
// TODO Auto-generated method stub
}
public void onNothingSelected() {}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {}
}

View file

@ -1,7 +1,11 @@
package com.xxmassdeveloper.mpchartexample;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import com.github.mikephil.charting.charts.BarChart;
@ -15,9 +19,10 @@ import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
import java.util.ArrayList;
@SuppressWarnings("SameParameterValue")
public class ScrollViewActivity extends DemoBase {
private BarChart mChart;
private BarChart chart;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -26,45 +31,71 @@ public class ScrollViewActivity extends DemoBase {
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_scrollview);
mChart = findViewById(R.id.chart1);
setTitle("ScrollViewActivity");
mChart.getDescription().setEnabled(false);
chart = findViewById(R.id.chart1);
chart.getDescription().setEnabled(false);
// scaling can now only be done on x- and y-axis separately
mChart.setPinchZoom(false);
chart.setPinchZoom(false);
mChart.setDrawBarShadow(false);
mChart.setDrawGridBackground(false);
chart.setDrawBarShadow(false);
chart.setDrawGridBackground(false);
XAxis xAxis = mChart.getXAxis();
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(false);
mChart.getAxisLeft().setDrawGridLines(false);
mChart.getLegend().setEnabled(false);
chart.getAxisLeft().setDrawGridLines(false);
chart.getLegend().setEnabled(false);
setData(10);
mChart.setFitBars(true);
chart.setFitBars(true);
}
private void setData(int count) {
ArrayList<BarEntry> yVals = new ArrayList<BarEntry>();
ArrayList<BarEntry> values = new ArrayList<>();
for (int i = 0; i < count; i++) {
float val = (float) (Math.random() * count) + 15;
yVals.add(new BarEntry(i, (int) val));
values.add(new BarEntry(i, (int) val));
}
BarDataSet set = new BarDataSet(yVals, "Data Set");
BarDataSet set = new BarDataSet(values, "Data Set");
set.setColors(ColorTemplate.VORDIPLOM_COLORS);
set.setDrawValues(false);
BarData data = new BarData(set);
mChart.setData(data);
mChart.invalidate();
mChart.animateY(800);
chart.setData(data);
chart.invalidate();
chart.animateY(800);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.only_github, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/ScrollViewActivity.java"));
startActivity(i);
break;
}
}
return true;
}
@Override
public void saveToGallery() { /* Intentionally left empty */ }
}

View file

@ -1,7 +1,12 @@
package com.xxmassdeveloper.mpchartexample;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
@ -9,11 +14,9 @@ import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.Legend.LegendPosition;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
import com.github.mikephil.charting.components.YAxis;
@ -34,8 +37,8 @@ import java.util.List;
public class StackedBarActivity extends DemoBase implements OnSeekBarChangeListener, OnChartValueSelectedListener {
private BarChart mChart;
private SeekBar mSeekBarX, mSeekBarY;
private BarChart chart;
private SeekBar seekBarX, seekBarY;
private TextView tvX, tvY;
@Override
@ -44,50 +47,52 @@ public class StackedBarActivity extends DemoBase implements OnSeekBarChangeListe
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_barchart);
setTitle("StackedBarActivity");
tvX = findViewById(R.id.tvXMax);
tvY = findViewById(R.id.tvYMax);
mSeekBarX = findViewById(R.id.seekBar1);
mSeekBarX.setOnSeekBarChangeListener(this);
seekBarX = findViewById(R.id.seekBar1);
seekBarX.setOnSeekBarChangeListener(this);
mSeekBarY = findViewById(R.id.seekBar2);
mSeekBarY.setOnSeekBarChangeListener(this);
seekBarY = findViewById(R.id.seekBar2);
seekBarY.setOnSeekBarChangeListener(this);
mChart = findViewById(R.id.chart1);
mChart.setOnChartValueSelectedListener(this);
chart = findViewById(R.id.chart1);
chart.setOnChartValueSelectedListener(this);
mChart.getDescription().setEnabled(false);
chart.getDescription().setEnabled(false);
// if more than 60 entries are displayed in the chart, no values will be
// drawn
mChart.setMaxVisibleValueCount(40);
chart.setMaxVisibleValueCount(40);
// scaling can now only be done on x- and y-axis separately
mChart.setPinchZoom(false);
chart.setPinchZoom(false);
mChart.setDrawGridBackground(false);
mChart.setDrawBarShadow(false);
chart.setDrawGridBackground(false);
chart.setDrawBarShadow(false);
mChart.setDrawValueAboveBar(false);
mChart.setHighlightFullBarEnabled(false);
chart.setDrawValueAboveBar(false);
chart.setHighlightFullBarEnabled(false);
// change the position of the y-labels
YAxis leftAxis = mChart.getAxisLeft();
YAxis leftAxis = chart.getAxisLeft();
leftAxis.setValueFormatter(new MyAxisValueFormatter());
leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
mChart.getAxisRight().setEnabled(false);
chart.getAxisRight().setEnabled(false);
XAxis xLabels = mChart.getXAxis();
XAxis xLabels = chart.getXAxis();
xLabels.setPosition(XAxisPosition.TOP);
// mChart.setDrawXLabels(false);
// mChart.setDrawYLabels(false);
// chart.setDrawXLabels(false);
// chart.setDrawYLabels(false);
// setting data
mSeekBarX.setProgress(12);
mSeekBarY.setProgress(100);
seekBarX.setProgress(12);
seekBarY.setProgress(100);
Legend l = mChart.getLegend();
Legend l = chart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
@ -96,7 +101,55 @@ public class StackedBarActivity extends DemoBase implements OnSeekBarChangeListe
l.setFormToTextSpace(4f);
l.setXEntrySpace(6f);
// mChart.setDrawLegend(false);
// chart.setDrawLegend(false);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvX.setText(String.valueOf(seekBarX.getProgress()));
tvY.setText(String.valueOf(seekBarY.getProgress()));
ArrayList<BarEntry> values = new ArrayList<>();
for (int i = 0; i < seekBarX.getProgress(); i++) {
float mul = (seekBarY.getProgress() + 1);
float val1 = (float) (Math.random() * mul) + mul / 3;
float val2 = (float) (Math.random() * mul) + mul / 3;
float val3 = (float) (Math.random() * mul) + mul / 3;
values.add(new BarEntry(
i,
new float[]{val1, val2, val3},
getResources().getDrawable(R.drawable.star)));
}
BarDataSet set1;
if (chart.getData() != null &&
chart.getData().getDataSetCount() > 0) {
set1 = (BarDataSet) chart.getData().getDataSetByIndex(0);
set1.setValues(values);
chart.getData().notifyDataChanged();
chart.notifyDataSetChanged();
} else {
set1 = new BarDataSet(values, "Statistics Vienna 2014");
set1.setDrawIcons(false);
set1.setColors(getColors());
set1.setStackLabels(new String[]{"Births", "Divorces", "Marriages"});
ArrayList<IBarDataSet> dataSets = new ArrayList<>();
dataSets.add(set1);
BarData data = new BarData(dataSets);
data.setValueFormatter(new MyValueFormatter());
data.setValueTextColor(Color.WHITE);
chart.setData(data);
}
chart.setFitBars(true);
chart.invalidate();
}
@Override
@ -109,8 +162,14 @@ public class StackedBarActivity extends DemoBase implements OnSeekBarChangeListe
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/StackedBarActivity.java"));
startActivity(i);
break;
}
case R.id.actionToggleValues: {
List<IBarDataSet> sets = mChart.getData()
List<IBarDataSet> sets = chart.getData()
.getDataSets();
for (IBarDataSet iSet : sets) {
@ -119,11 +178,11 @@ public class StackedBarActivity extends DemoBase implements OnSeekBarChangeListe
set.setDrawValues(!set.isDrawValuesEnabled());
}
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionToggleIcons: {
List<IBarDataSet> sets = mChart.getData()
List<IBarDataSet> sets = chart.getData()
.getDataSets();
for (IBarDataSet iSet : sets) {
@ -132,55 +191,56 @@ public class StackedBarActivity extends DemoBase implements OnSeekBarChangeListe
set.setDrawIcons(!set.isDrawIconsEnabled());
}
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionToggleHighlight: {
if (mChart.getData() != null) {
mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled());
mChart.invalidate();
if (chart.getData() != null) {
chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled());
chart.invalidate();
}
break;
}
case R.id.actionTogglePinch: {
if (mChart.isPinchZoomEnabled())
mChart.setPinchZoom(false);
if (chart.isPinchZoomEnabled())
chart.setPinchZoom(false);
else
mChart.setPinchZoom(true);
chart.setPinchZoom(true);
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled());
chart.notifyDataSetChanged();
break;
}
case R.id.actionToggleBarBorders: {
for (IBarDataSet set : mChart.getData().getDataSets())
for (IBarDataSet set : chart.getData().getDataSets())
((BarDataSet) set).setBarBorderWidth(set.getBarBorderWidth() == 1.f ? 0.f : 1.f);
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.animateX: {
mChart.animateX(3000);
chart.animateX(2000);
break;
}
case R.id.animateY: {
mChart.animateY(3000);
chart.animateY(2000);
break;
}
case R.id.animateXY: {
mChart.animateXY(3000, 3000);
chart.animateXY(2000, 2000);
break;
}
case R.id.actionSave: {
if (mChart.saveToGallery("title" + System.currentTimeMillis(), 50)) {
Toast.makeText(getApplicationContext(), "Saving SUCCESSFUL!", Toast.LENGTH_SHORT).show();
} else
Toast.makeText(getApplicationContext(), "Saving FAILED!", Toast.LENGTH_SHORT).show();
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveToGallery();
} else {
requestStoragePermission(chart);
}
break;
}
}
@ -188,64 +248,15 @@ public class StackedBarActivity extends DemoBase implements OnSeekBarChangeListe
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvX.setText("" + (mSeekBarX.getProgress() + 1));
tvY.setText("" + (mSeekBarY.getProgress()));
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
for (int i = 0; i < mSeekBarX.getProgress() + 1; i++) {
float mult = (mSeekBarY.getProgress() + 1);
float val1 = (float) (Math.random() * mult) + mult / 3;
float val2 = (float) (Math.random() * mult) + mult / 3;
float val3 = (float) (Math.random() * mult) + mult / 3;
yVals1.add(new BarEntry(
i,
new float[]{val1, val2, val3},
getResources().getDrawable(R.drawable.star)));
}
BarDataSet set1;
if (mChart.getData() != null &&
mChart.getData().getDataSetCount() > 0) {
set1 = (BarDataSet) mChart.getData().getDataSetByIndex(0);
set1.setValues(yVals1);
mChart.getData().notifyDataChanged();
mChart.notifyDataSetChanged();
} else {
set1 = new BarDataSet(yVals1, "Statistics Vienna 2014");
set1.setDrawIcons(false);
set1.setColors(getColors());
set1.setStackLabels(new String[]{"Births", "Divorces", "Marriages"});
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
dataSets.add(set1);
BarData data = new BarData(dataSets);
data.setValueFormatter(new MyValueFormatter());
data.setValueTextColor(Color.WHITE);
mChart.setData(data);
}
mChart.setFitBars(true);
mChart.invalidate();
public void saveToGallery() {
saveToGallery(chart, "StackedBarActivity");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {}
@Override
public void onValueSelected(Entry e, Highlight h) {
@ -259,21 +270,14 @@ public class StackedBarActivity extends DemoBase implements OnSeekBarChangeListe
}
@Override
public void onNothingSelected() {
// TODO Auto-generated method stub
}
public void onNothingSelected() {}
private int[] getColors() {
int stacksize = 3;
// have as many colors as stack-values per entry
int[] colors = new int[stacksize];
int[] colors = new int[3];
for (int i = 0; i < colors.length; i++) {
colors[i] = ColorTemplate.MATERIAL_COLORS[i];
}
System.arraycopy(ColorTemplate.MATERIAL_COLORS, 0, colors, 0, 3);
return colors;
}

View file

@ -1,18 +1,21 @@
package com.xxmassdeveloper.mpchartexample;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.Toast;
import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.Legend.LegendPosition;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
import com.github.mikephil.charting.components.YAxis;
@ -35,7 +38,7 @@ import java.util.List;
public class StackedBarActivityNegative extends DemoBase implements
OnChartValueSelectedListener {
private HorizontalBarChart mChart;
private HorizontalBarChart chart;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -44,30 +47,30 @@ public class StackedBarActivityNegative extends DemoBase implements
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_age_distribution);
setTitle("Age Distribution Austria");
setTitle("StackedBarActivityNegative");
mChart = findViewById(R.id.chart1);
mChart.setOnChartValueSelectedListener(this);
mChart.setDrawGridBackground(false);
mChart.getDescription().setEnabled(false);
chart = findViewById(R.id.chart1);
chart.setOnChartValueSelectedListener(this);
chart.setDrawGridBackground(false);
chart.getDescription().setEnabled(false);
// scaling can now only be done on x- and y-axis separately
mChart.setPinchZoom(false);
chart.setPinchZoom(false);
mChart.setDrawBarShadow(false);
mChart.setDrawValueAboveBar(true);
mChart.setHighlightFullBarEnabled(false);
mChart.getAxisLeft().setEnabled(false);
mChart.getAxisRight().setAxisMaximum(25f);
mChart.getAxisRight().setAxisMinimum(-25f);
mChart.getAxisRight().setDrawGridLines(false);
mChart.getAxisRight().setDrawZeroLine(true);
mChart.getAxisRight().setLabelCount(7, false);
mChart.getAxisRight().setValueFormatter(new CustomFormatter());
mChart.getAxisRight().setTextSize(9f);
chart.setDrawBarShadow(false);
chart.setDrawValueAboveBar(true);
chart.setHighlightFullBarEnabled(false);
XAxis xAxis = mChart.getXAxis();
chart.getAxisLeft().setEnabled(false);
chart.getAxisRight().setAxisMaximum(25f);
chart.getAxisRight().setAxisMinimum(-25f);
chart.getAxisRight().setDrawGridLines(false);
chart.getAxisRight().setDrawZeroLine(true);
chart.getAxisRight().setLabelCount(7, false);
chart.getAxisRight().setValueFormatter(new CustomFormatter());
chart.getAxisRight().setTextSize(9f);
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxisPosition.BOTH_SIDED);
xAxis.setDrawGridLines(false);
xAxis.setDrawAxisLine(false);
@ -87,7 +90,7 @@ public class StackedBarActivityNegative extends DemoBase implements
}
});
Legend l = mChart.getLegend();
Legend l = chart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
@ -97,36 +100,34 @@ public class StackedBarActivityNegative extends DemoBase implements
l.setXEntrySpace(6f);
// IMPORTANT: When using negative values in stacked bars, always make sure the negative values are in the array first
ArrayList<BarEntry> yValues = new ArrayList<BarEntry>();
yValues.add(new BarEntry(5, new float[]{ -10, 10 }));
yValues.add(new BarEntry(15, new float[]{ -12, 13 }));
yValues.add(new BarEntry(25, new float[]{ -15, 15 }));
yValues.add(new BarEntry(35, new float[]{ -17, 17 }));
yValues.add(new BarEntry(45, new float[]{ -19, 20 }));
yValues.add(new BarEntry(45, new float[]{ -19, 20 }, getResources().getDrawable(R.drawable.star)));
yValues.add(new BarEntry(55, new float[]{ -19, 19 }));
yValues.add(new BarEntry(65, new float[]{ -16, 16 }));
yValues.add(new BarEntry(75, new float[]{ -13, 14 }));
yValues.add(new BarEntry(85, new float[]{ -10, 11 }));
yValues.add(new BarEntry(95, new float[]{ -5, 6 }));
yValues.add(new BarEntry(105, new float[]{ -1, 2 }));
ArrayList<BarEntry> values = new ArrayList<>();
values.add(new BarEntry(5, new float[]{ -10, 10 }));
values.add(new BarEntry(15, new float[]{ -12, 13 }));
values.add(new BarEntry(25, new float[]{ -15, 15 }));
values.add(new BarEntry(35, new float[]{ -17, 17 }));
values.add(new BarEntry(45, new float[]{ -19, 20 }));
values.add(new BarEntry(45, new float[]{ -19, 20 }, getResources().getDrawable(R.drawable.star)));
values.add(new BarEntry(55, new float[]{ -19, 19 }));
values.add(new BarEntry(65, new float[]{ -16, 16 }));
values.add(new BarEntry(75, new float[]{ -13, 14 }));
values.add(new BarEntry(85, new float[]{ -10, 11 }));
values.add(new BarEntry(95, new float[]{ -5, 6 }));
values.add(new BarEntry(105, new float[]{ -1, 2 }));
BarDataSet set = new BarDataSet(yValues, "Age Distribution");
BarDataSet set = new BarDataSet(values, "Age Distribution");
set.setDrawIcons(false);
set.setValueFormatter(new CustomFormatter());
set.setValueTextSize(7f);
set.setAxisDependency(YAxis.AxisDependency.RIGHT);
set.setColors(new int[] {Color.rgb(67,67,72), Color.rgb(124,181,236)});
set.setColors(Color.rgb(67,67,72), Color.rgb(124,181,236));
set.setStackLabels(new String[]{
"Men", "Women"
});
String []xLabels = new String[]{"0-10", "10-20", "20-30", "30-40", "40-50", "50-60", "60-70", "70-80", "80-90", "90-100", "100+"};
BarData data = new BarData(set);
data.setBarWidth(8.5f);
mChart.setData(data);
mChart.invalidate();
chart.setData(data);
chart.invalidate();
}
@Override
@ -139,8 +140,14 @@ public class StackedBarActivityNegative extends DemoBase implements
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/StackedBarActivityNegative.java"));
startActivity(i);
break;
}
case R.id.actionToggleValues: {
List<IBarDataSet> sets = mChart.getData()
List<IBarDataSet> sets = chart.getData()
.getDataSets();
for (IBarDataSet iSet : sets) {
@ -149,11 +156,11 @@ public class StackedBarActivityNegative extends DemoBase implements
set.setDrawValues(!set.isDrawValuesEnabled());
}
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionToggleIcons: {
List<IBarDataSet> sets = mChart.getData()
List<IBarDataSet> sets = chart.getData()
.getDataSets();
for (IBarDataSet iSet : sets) {
@ -162,57 +169,56 @@ public class StackedBarActivityNegative extends DemoBase implements
set.setDrawIcons(!set.isDrawIconsEnabled());
}
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionToggleHighlight: {
if(mChart.getData() != null) {
mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled());
mChart.invalidate();
if(chart.getData() != null) {
chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled());
chart.invalidate();
}
break;
}
case R.id.actionTogglePinch: {
if (mChart.isPinchZoomEnabled())
mChart.setPinchZoom(false);
if (chart.isPinchZoomEnabled())
chart.setPinchZoom(false);
else
mChart.setPinchZoom(true);
chart.setPinchZoom(true);
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax: {
mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
mChart.notifyDataSetChanged();
chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled());
chart.notifyDataSetChanged();
break;
}
case R.id.actionToggleBarBorders: {
for (IBarDataSet set : mChart.getData().getDataSets())
for (IBarDataSet set : chart.getData().getDataSets())
((BarDataSet)set).setBarBorderWidth(set.getBarBorderWidth() == 1.f ? 0.f : 1.f);
mChart.invalidate();
chart.invalidate();
break;
}
case R.id.animateX: {
mChart.animateX(3000);
chart.animateX(3000);
break;
}
case R.id.animateY: {
mChart.animateY(3000);
chart.animateY(3000);
break;
}
case R.id.animateXY: {
mChart.animateXY(3000, 3000);
chart.animateXY(3000, 3000);
break;
}
case R.id.actionSave: {
if (mChart.saveToGallery("title" + System.currentTimeMillis(), 50)) {
Toast.makeText(getApplicationContext(), "Saving SUCCESSFUL!",
Toast.LENGTH_SHORT).show();
} else
Toast.makeText(getApplicationContext(), "Saving FAILED!", Toast.LENGTH_SHORT)
.show();
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveToGallery();
} else {
requestStoragePermission(chart);
}
break;
}
}
@ -220,8 +226,12 @@ public class StackedBarActivityNegative extends DemoBase implements
}
@Override
public void onValueSelected(Entry e, Highlight h) {
public void saveToGallery() {
saveToGallery(chart, "StackedBarActivityNegative");
}
@Override
public void onValueSelected(Entry e, Highlight h) {
BarEntry entry = (BarEntry) e;
Log.i("VAL SELECTED",
"Value: " + Math.abs(entry.getYVals()[h.getStackIndex()]));
@ -229,16 +239,14 @@ public class StackedBarActivityNegative extends DemoBase implements
@Override
public void onNothingSelected() {
// TODO Auto-generated method stub
Log.i("NOTING SELECTED", "");
}
private class CustomFormatter implements IValueFormatter, IAxisValueFormatter
{
private class CustomFormatter implements IValueFormatter, IAxisValueFormatter {
private DecimalFormat mFormat;
public CustomFormatter() {
CustomFormatter() {
mFormat = new DecimalFormat("###");
}

View file

@ -10,7 +10,7 @@ import com.github.mikephil.charting.formatter.IAxisValueFormatter;
public class DayAxisValueFormatter implements IAxisValueFormatter
{
protected String[] mMonths = new String[]{
private final String[] mMonths = new String[]{
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

View file

@ -8,7 +8,10 @@ import java.text.DecimalFormat;
/**
* Created by Philipp Jahoda on 14/09/15.
*
* @deprecated The {@link MyAxisValueFormatter} does exactly the same thing and is more functional.
*/
@Deprecated
public class MyCustomXAxisValueFormatter implements IAxisValueFormatter
{

View file

@ -7,18 +7,19 @@ import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
/**
* Created by Philipp Jahoda on 12/09/15.
*/
@SuppressWarnings("unused")
public class MyFillFormatter implements IFillFormatter
{
private float mFillPos = 0f;
private float fillPos;
public MyFillFormatter(float fillpos) {
this.mFillPos = fillpos;
public MyFillFormatter(float fillPos) {
this.fillPos = fillPos;
}
@Override
public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
// your logic could be here
return mFillPos;
return fillPos;
}
}

View file

@ -1,6 +1,7 @@
package com.xxmassdeveloper.mpchartexample.custom;
import android.annotation.SuppressLint;
import android.content.Context;
import android.widget.TextView;
@ -14,9 +15,10 @@ import com.xxmassdeveloper.mpchartexample.R;
/**
* Custom implementation of the MarkerView.
*
*
* @author Philipp Jahoda
*/
@SuppressLint("ViewConstructor")
public class MyMarkerView extends MarkerView {
private TextView tvContent;
@ -27,7 +29,7 @@ public class MyMarkerView extends MarkerView {
tvContent = findViewById(R.id.tvContent);
}
// callbacks everytime the MarkerView is redrawn, can be used to update the
// runs every time the MarkerView is redrawn, can be used to update the
// content (user-interface)
@Override
public void refreshContent(Entry e, Highlight highlight) {
@ -36,10 +38,10 @@ public class MyMarkerView extends MarkerView {
CandleEntry ce = (CandleEntry) e;
tvContent.setText("" + Utils.formatNumber(ce.getHigh(), 0, true));
tvContent.setText(Utils.formatNumber(ce.getHigh(), 0, true));
} else {
tvContent.setText("" + Utils.formatNumber(e.getY(), 0, true));
tvContent.setText(Utils.formatNumber(e.getY(), 0, true));
}
super.refreshContent(e, highlight);

View file

@ -1,25 +1,25 @@
package com.xxmassdeveloper.mpchartexample.custom;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Typeface;
import android.widget.TextView;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;
import com.github.mikephil.charting.utils.Utils;
import com.xxmassdeveloper.mpchartexample.R;
import java.text.DecimalFormat;
/**
* Custom implementation of the MarkerView.
*
*
* @author Philipp Jahoda
*/
@SuppressLint("ViewConstructor")
public class RadarMarkerView extends MarkerView {
private TextView tvContent;
@ -32,11 +32,11 @@ public class RadarMarkerView extends MarkerView {
tvContent.setTypeface(Typeface.createFromAsset(context.getAssets(), "OpenSans-Light.ttf"));
}
// callbacks everytime the MarkerView is redrawn, can be used to update the
// runs every time the MarkerView is redrawn, can be used to update the
// content (user-interface)
@Override
public void refreshContent(Entry e, Highlight highlight) {
tvContent.setText(format.format(e.getY()) + " %");
tvContent.setText(String.format("%s %%", format.format(e.getY())));
super.refreshContent(e, highlight);
}

View file

@ -8,6 +8,7 @@ import io.realm.RealmObject;
* Demo class that encapsulates data stored in realm.io database.
* This class represents data suitable for all chart-types.
*/
@SuppressWarnings("unused")
public class RealmDemoData extends RealmObject {
private float yValue;
@ -26,15 +27,7 @@ public class RealmDemoData extends RealmObject {
*/
private String label;
// ofc there could me more fields here...
public RealmDemoData() {
}
public RealmDemoData(float yValue) {
this.yValue = yValue;
}
public RealmDemoData() {}
public RealmDemoData(float xValue, float yValue) {
this.xValue = xValue;
@ -44,12 +37,12 @@ public class RealmDemoData extends RealmObject {
/**
* Constructor for stacked bars.
*
* @param xValue
* @param stackValues
* @param xValue x position for bars
* @param stackValues values of bars in the stack
*/
public RealmDemoData(float xValue, float[] stackValues) {
this.xValue = xValue;
this.stackValues = new RealmList<RealmFloat>();
this.stackValues = new RealmList<>();
for (float val : stackValues) {
this.stackValues.add(new RealmFloat(val));
@ -59,11 +52,11 @@ public class RealmDemoData extends RealmObject {
/**
* Constructor for candles.
*
* @param xValue
* @param high
* @param low
* @param open
* @param close
* @param xValue x position of candle
* @param high high value for candle
* @param low low value for candle
* @param open open value for candle
* @param close close value for candle
*/
public RealmDemoData(float xValue, float high, float low, float open, float close) {
this.yValue = (high + low) / 2f;
@ -77,9 +70,9 @@ public class RealmDemoData extends RealmObject {
/**
* Constructor for bubbles.
*
* @param xValue
* @param yValue
* @param bubbleSize
* @param xValue x position of bubble
* @param yValue y position of bubble
* @param bubbleSize size of bubble
*/
public RealmDemoData(float xValue, float yValue, float bubbleSize) {
this.xValue = xValue;
@ -90,27 +83,27 @@ public class RealmDemoData extends RealmObject {
/**
* Constructor for pie chart.
*
* @param yValue
* @param label
* @param yValue size of pie slice
* @param label label for pie slice
*/
public RealmDemoData(float yValue, String label) {
this.yValue = yValue;
this.label = label;
}
public float getyValue() {
public float getYValue() {
return yValue;
}
public void setyValue(float yValue) {
public void setYValue(float yValue) {
this.yValue = yValue;
}
public float getxValue() {
public float getXValue() {
return xValue;
}
public void setxValue(float xValue) {
public void setXValue(float xValue) {
this.xValue = xValue;
}
@ -177,4 +170,4 @@ public class RealmDemoData extends RealmObject {
public void setLabel(String label) {
this.label = label;
}
}
}

View file

@ -5,6 +5,7 @@ import io.realm.RealmObject;
/**
* Created by Philipp Jahoda on 09/11/15.
*/
@SuppressWarnings("unused")
public class RealmFloat extends RealmObject {
private float floatValue;

View file

@ -1,6 +1,7 @@
package com.xxmassdeveloper.mpchartexample.custom;
import android.annotation.SuppressLint;
import android.content.Context;
import android.widget.TextView;
@ -14,9 +15,11 @@ import com.xxmassdeveloper.mpchartexample.R;
/**
* Custom implementation of the MarkerView.
*
*
* @author Philipp Jahoda
*/
@SuppressWarnings("unused")
@SuppressLint("ViewConstructor")
public class StackedBarsMarkerView extends MarkerView {
private TextView tvContent;
@ -27,7 +30,7 @@ public class StackedBarsMarkerView extends MarkerView {
tvContent = findViewById(R.id.tvContent);
}
// callbacks everytime the MarkerView is redrawn, can be used to update the
// runs every time the MarkerView is redrawn, can be used to update the
// content (user-interface)
@Override
public void refreshContent(Entry e, Highlight highlight) {
@ -39,13 +42,13 @@ public class StackedBarsMarkerView extends MarkerView {
if(be.getYVals() != null) {
// draw the stack value
tvContent.setText("" + Utils.formatNumber(be.getYVals()[highlight.getStackIndex()], 0, true));
tvContent.setText(Utils.formatNumber(be.getYVals()[highlight.getStackIndex()], 0, true));
} else {
tvContent.setText("" + Utils.formatNumber(be.getY(), 0, true));
tvContent.setText(Utils.formatNumber(be.getY(), 0, true));
}
} else {
tvContent.setText("" + Utils.formatNumber(e.getY(), 0, true));
tvContent.setText(Utils.formatNumber(e.getY(), 0, true));
}
super.refreshContent(e, highlight);

View file

@ -1,6 +1,7 @@
package com.xxmassdeveloper.mpchartexample.custom;
import android.annotation.SuppressLint;
import android.content.Context;
import android.widget.TextView;
@ -18,6 +19,7 @@ import java.text.DecimalFormat;
*
* @author Philipp Jahoda
*/
@SuppressLint("ViewConstructor")
public class XYMarkerView extends MarkerView {
private TextView tvContent;
@ -33,12 +35,12 @@ public class XYMarkerView extends MarkerView {
format = new DecimalFormat("###.0");
}
// callbacks everytime the MarkerView is redrawn, can be used to update the
// runs every time the MarkerView is redrawn, can be used to update the
// content (user-interface)
@Override
public void refreshContent(Entry e, Highlight highlight) {
tvContent.setText("x: " + xAxisValueFormatter.getFormattedValue(e.getX(), null) + ", y: " + format.format(e.getY()));
tvContent.setText(String.format("x: %s, y: %s", xAxisValueFormatter.getFormattedValue(e.getX(), null), format.format(e.getY())));
super.refreshContent(e, highlight);
}

View file

@ -6,16 +6,16 @@ import com.github.mikephil.charting.formatter.IAxisValueFormatter;
/**
* Created by Philipp Jahoda on 14/09/15.
*/
@SuppressWarnings("unused")
public class YearXAxisFormatter implements IAxisValueFormatter
{
protected String[] mMonths = new String[]{
private final String[] mMonths = new String[]{
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"
};
public YearXAxisFormatter() {
// maybe do something here or provide parameters in constructor
// take parameters to change behavior of formatter
}
@Override

Some files were not shown because too many files have changed in this diff Show more