├── .gitignore ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── charlie │ │ └── mpandroidcharttest │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── charlie │ │ │ └── mpandroidcharttest │ │ │ ├── MainActivity.java │ │ │ ├── chartactivity │ │ │ ├── BarChartActivity.java │ │ │ ├── CombineChartActivity.java │ │ │ ├── LineChartActivity.java │ │ │ ├── MultiLineChartActivity.java │ │ │ ├── PieChartActivity.java │ │ │ ├── PositiveNegativeBarChartActivity.java │ │ │ ├── ThreeBarChartActivity.java │ │ │ └── TwoBarChartActivity.java │ │ │ ├── common │ │ │ ├── ArrowTextView.java │ │ │ ├── MPChartMarkerView.java │ │ │ ├── MyValueFormatter.java │ │ │ └── StringAxisValueFormatter.java │ │ │ └── util │ │ │ ├── MPChartHelper.java │ │ │ └── StringUtils.java │ └── res │ │ ├── layout │ │ ├── activity_bar_chart.xml │ │ ├── activity_combine_chart.xml │ │ ├── activity_line_chart.xml │ │ ├── activity_main.xml │ │ ├── activity_pie_chart.xml │ │ └── custom_marker_view.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── attrs.xml │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── charlie │ └── mpandroidcharttest │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── screenshots ├── 1.png ├── 2.png ├── 3.png ├── 4.png ├── 5.png ├── 6.png ├── 7.png └── 8.png └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MPAndroidChartTest 2 | MPAndroidChart库测试,并封装LineChart、BarChart、PieChart和CombineChart设置代码。 3 | 4 | # 项目介绍 5 | 引用的库是MPAndroidChart:v3.0.1;自定义了MPChartMarkerView、MyValueFormatter和StringAxisValueFormatter。
6 | # MPAndroidChart使用流程大致如下 7 | ### 1、创建Chart 8 |    使用LineChart, BarChart, PieChart或CombineChart时需要先在布局文件中进行定义,然后在后台代码中绑定;或者直接在代码中声明也行(不常用)。 9 | ### 2、设置Chart样式 10 |    当创建好一个chart后,就可以为该chart设置样式,包括chart的放缩,chart视图窗口的边距和加载动画,X/Y轴标签的样式、显示的位置、坐标轴的宽度、是否可用等,图例的位置、文字大小等等。
11 | ```Java 12 | lineChart.getDescription().setEnabled(false);//设置描述 13 | lineChart.setPinchZoom(true);//设置按比例放缩柱状图 14 | 15 | //x坐标轴设置 16 | XAxis xAxis = lineChart.getXAxis(); 17 | xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//设置X轴标签显示位置 18 | xAxis.setDrawGridLines(false);//不绘制格网线 19 | xAxis.setGranularity(1f);//设置最小间隔,防止当放大时,出现重复标签。 20 | xAxis.setLabelCount(12);//设置x轴显示的标签个数 21 | xAxis.setAxisLineWidth(2f);//设置x轴宽度, ...其他样式 22 | 23 | //y轴设置 24 | YAxis leftAxis = lineChart.getAxisLeft();//取得左侧y轴 25 | leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);//y轴标签绘制的位置 26 | leftAxis.setDrawGridLines(false);//不绘制y轴格网线 27 | leftAxis.setDrawLabels(false);//不显示坐标轴上的值, ...其他样式 28 | 29 | lineChart.getAxisRight().setEnabled(false); 30 | 31 | //图例设置 32 | Legend legend = lineChart.getLegend(); 33 | legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER); 34 | legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); 35 | legend.setOrientation(Legend.LegendOrientation.HORIZONTAL); 36 | legend.setDrawInside(false); 37 | legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT); 38 | legend.setForm(Legend.LegendForm.LINE); 39 | legend.setTextSize(12f);//设置图例字体大小, ...其他样式 40 | 41 | lineChart.setExtraOffsets(10, 30, 20, 10);//设置视图窗口大小 42 | lineChart.animateX(1500);//数据显示动画,从左往右依次显示 43 | ``` 44 | ### 3、设置Chart数据 45 | * 当设置好一个chart的样式后,就可以为该chart添加数据。例如LineChart,一个Entry类代表图上的一个(x,y)坐标对。但在其他的chart类型中,例如BarChart,则是BarEntry类代表图上的一个(x,y)坐标对。
46 | ```Java 47 | YourData[] dataObjects = ...; 48 | List entries = new ArrayList(); 49 | for (YourData data : dataObjects) { 50 | // turn your data into Entry objects 51 | entries.add(new Entry(data.getValueX(), data.getValueY())); 52 | } 53 | ``` 54 | * 用List\初始化一个LineDataSet对象以代表该数据集。如果一个LineChart有多个LineDataSet,每个LineDataSet可以设置自己的样式。
55 | ```Java 56 | LineDataSet dataSet = new LineDataSet(entries, "Label"); // add entries to dataset 57 | dataSet.setColor(...); 58 | dataSet.setValueTextColor(...); // styling, ... 59 | ``` 60 | * 然后,将LineDataSet添加到LineData对象中。LineData对象持有LineChart的所有数据,并允许设置额外的样式。最后,将LineData对象设置到LineChart并刷新该LineChart即可。
61 | ```Java 62 | ArrayList dataSets = new ArrayList(); 63 | dataSets.add(dataSet); // add the datasets ...other add 64 | 65 | LineData lineData = new LineData(dataSets); 66 | chart.setData(lineData); 67 | chart.setValueTextSize(10f);//设置数值字体大小, ...other styling 68 | chart.invalidate(); // refresh 69 | ``` 70 | # 截图如下 71 | ![github](https://github.com/WJKCharlie/MPAndroidChartTest/raw/master/screenshots/1.png) 72 | ![github](https://github.com/WJKCharlie/MPAndroidChartTest/raw/master/screenshots/2.png) 73 | ![github](https://github.com/WJKCharlie/MPAndroidChartTest/raw/master/screenshots/3.png) 74 | ![github](https://github.com/WJKCharlie/MPAndroidChartTest/raw/master/screenshots/4.png) 75 | ![github](https://github.com/WJKCharlie/MPAndroidChartTest/raw/master/screenshots/5.png) 76 | ![github](https://github.com/WJKCharlie/MPAndroidChartTest/raw/master/screenshots/6.png) 77 | ![github](https://github.com/WJKCharlie/MPAndroidChartTest/raw/master/screenshots/7.png) 78 | ![github](https://github.com/WJKCharlie/MPAndroidChartTest/raw/master/screenshots/8.png) 79 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "24.0.3" 6 | defaultConfig { 7 | applicationId "com.charlie.mpandroidcharttest" 8 | minSdkVersion 21 9 | targetSdkVersion 23 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | compile 'com.android.support:appcompat-v7:23.4.0' 28 | testCompile 'junit:junit:4.12' 29 | 30 | compile 'com.github.PhilJay:MPAndroidChart:v3.0.1' 31 | } 32 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\Develop\android-sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/charlie/mpandroidcharttest/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.charlie.mpandroidcharttest; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.charlie.mpandroidcharttest", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /app/src/main/java/com/charlie/mpandroidcharttest/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.charlie.mpandroidcharttest; 2 | 3 | import android.content.Intent; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.os.Bundle; 6 | import android.view.View; 7 | import android.widget.AdapterView; 8 | import android.widget.ArrayAdapter; 9 | import android.widget.ListView; 10 | 11 | import com.charlie.mpandroidcharttest.chartactivity.BarChartActivity; 12 | import com.charlie.mpandroidcharttest.chartactivity.CombineChartActivity; 13 | import com.charlie.mpandroidcharttest.chartactivity.LineChartActivity; 14 | import com.charlie.mpandroidcharttest.chartactivity.MultiLineChartActivity; 15 | import com.charlie.mpandroidcharttest.chartactivity.PieChartActivity; 16 | import com.charlie.mpandroidcharttest.chartactivity.PositiveNegativeBarChartActivity; 17 | import com.charlie.mpandroidcharttest.chartactivity.ThreeBarChartActivity; 18 | import com.charlie.mpandroidcharttest.chartactivity.TwoBarChartActivity; 19 | 20 | import java.util.ArrayList; 21 | import java.util.List; 22 | 23 | public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener { 24 | 25 | private ListView listView; 26 | @Override 27 | protected void onCreate(Bundle savedInstanceState) { 28 | super.onCreate(savedInstanceState); 29 | setContentView(R.layout.activity_main); 30 | 31 | listView=(ListView)findViewById(R.id.listview); 32 | listView.setAdapter(new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,initListData())); 33 | listView.setOnItemClickListener(this); 34 | } 35 | 36 | private List initListData(){ 37 | List data = new ArrayList<>(); 38 | data.add("柱状图(单)"); 39 | data.add("柱状图(双)"); 40 | data.add("柱状图(三)"); 41 | data.add("正负柱状图"); 42 | data.add("折线图(单)"); 43 | data.add("折线图(复)"); 44 | data.add("饼图"); 45 | data.add("组合图"); 46 | return data; 47 | } 48 | 49 | 50 | @Override 51 | public void onItemClick(AdapterView parent, View view, int position, long id) { 52 | Intent i; 53 | switch (position){ 54 | case 0: 55 | i = new Intent(MainActivity.this, BarChartActivity.class); 56 | startActivity(i); 57 | break; 58 | case 1: 59 | i = new Intent(MainActivity.this, TwoBarChartActivity.class); 60 | startActivity(i); 61 | break; 62 | case 2: 63 | i = new Intent(MainActivity.this, ThreeBarChartActivity.class); 64 | startActivity(i); 65 | break; 66 | case 3: 67 | i = new Intent(MainActivity.this, PositiveNegativeBarChartActivity.class); 68 | startActivity(i); 69 | break; 70 | case 4: 71 | i = new Intent(MainActivity.this, LineChartActivity.class); 72 | startActivity(i); 73 | break; 74 | case 5: 75 | i = new Intent(MainActivity.this, MultiLineChartActivity.class); 76 | startActivity(i); 77 | break; 78 | case 6: 79 | i = new Intent(MainActivity.this, PieChartActivity.class); 80 | startActivity(i); 81 | break; 82 | case 7: 83 | i = new Intent(MainActivity.this, CombineChartActivity.class); 84 | startActivity(i); 85 | break; 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /app/src/main/java/com/charlie/mpandroidcharttest/chartactivity/BarChartActivity.java: -------------------------------------------------------------------------------- 1 | package com.charlie.mpandroidcharttest.chartactivity; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | 6 | import com.charlie.mpandroidcharttest.R; 7 | import com.charlie.mpandroidcharttest.util.MPChartHelper; 8 | import com.github.mikephil.charting.charts.BarChart; 9 | 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | 13 | public class BarChartActivity extends AppCompatActivity { 14 | 15 | private BarChart barChart1; 16 | 17 | private List xAxisValues; 18 | private List yAxisValues; 19 | 20 | @Override 21 | protected void onCreate(Bundle savedInstanceState) { 22 | super.onCreate(savedInstanceState); 23 | setContentView(R.layout.activity_bar_chart); 24 | 25 | initView(); 26 | initData(); 27 | MPChartHelper.setBarChart(barChart1,xAxisValues,yAxisValues,"柱状图(单)",15,null); 28 | } 29 | 30 | private void initView(){ 31 | barChart1=(BarChart)findViewById(R.id.barChart1); 32 | } 33 | 34 | private void initData(){ 35 | xAxisValues = new ArrayList<>(); 36 | yAxisValues = new ArrayList<>(); 37 | for(int i=0;i<10;++i){ 38 | xAxisValues.add(String.valueOf(i)); 39 | yAxisValues.add((float)(Math.random()*1000+20)); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /app/src/main/java/com/charlie/mpandroidcharttest/chartactivity/CombineChartActivity.java: -------------------------------------------------------------------------------- 1 | package com.charlie.mpandroidcharttest.chartactivity; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | 6 | import com.charlie.mpandroidcharttest.R; 7 | import com.charlie.mpandroidcharttest.util.MPChartHelper; 8 | import com.github.mikephil.charting.charts.CombinedChart; 9 | 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | 13 | public class CombineChartActivity extends AppCompatActivity { 14 | 15 | private CombinedChart combineChart; 16 | private List xAxisValues; 17 | private List lineValues; 18 | private List barValues; 19 | 20 | @Override 21 | protected void onCreate(Bundle savedInstanceState) { 22 | super.onCreate(savedInstanceState); 23 | setContentView(R.layout.activity_combine_chart); 24 | 25 | initView(); 26 | initData(); 27 | MPChartHelper.setCombineChart(combineChart,xAxisValues,lineValues,barValues,"折线","柱子"); 28 | } 29 | 30 | private void initView(){ 31 | combineChart=(CombinedChart)findViewById(R.id.combineChart); 32 | } 33 | 34 | private void initData(){ 35 | xAxisValues = new ArrayList<>(); 36 | lineValues = new ArrayList<>(); 37 | barValues = new ArrayList<>(); 38 | for(int i=0;i<10;++i){ 39 | xAxisValues.add(String.valueOf(i)); 40 | lineValues.add((float)(Math.random()*1000+20)); 41 | barValues.add((float)(Math.random()*1000+20)); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /app/src/main/java/com/charlie/mpandroidcharttest/chartactivity/LineChartActivity.java: -------------------------------------------------------------------------------- 1 | package com.charlie.mpandroidcharttest.chartactivity; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | 6 | import com.charlie.mpandroidcharttest.R; 7 | import com.charlie.mpandroidcharttest.util.MPChartHelper; 8 | import com.github.mikephil.charting.charts.LineChart; 9 | 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | 13 | public class LineChartActivity extends AppCompatActivity { 14 | 15 | private LineChart lineChart; 16 | private List xAxisValues; 17 | private List yAxisValues; 18 | 19 | @Override 20 | protected void onCreate(Bundle savedInstanceState) { 21 | super.onCreate(savedInstanceState); 22 | setContentView(R.layout.activity_line_chart); 23 | 24 | initView(); 25 | initData(); 26 | MPChartHelper.setLineChart(lineChart,xAxisValues,yAxisValues,"折线图(单)",true); 27 | } 28 | 29 | private void initView(){ 30 | lineChart=(LineChart)findViewById(R.id.lineChart); 31 | } 32 | 33 | private void initData(){ 34 | xAxisValues = new ArrayList<>(); 35 | yAxisValues = new ArrayList<>(); 36 | for(int i=0;i<10;++i){ 37 | xAxisValues.add(String.valueOf(i)); 38 | yAxisValues.add((float)(Math.random()*1000+20)); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /app/src/main/java/com/charlie/mpandroidcharttest/chartactivity/MultiLineChartActivity.java: -------------------------------------------------------------------------------- 1 | package com.charlie.mpandroidcharttest.chartactivity; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | 6 | import com.charlie.mpandroidcharttest.R; 7 | import com.charlie.mpandroidcharttest.util.MPChartHelper; 8 | import com.github.mikephil.charting.charts.LineChart; 9 | 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | 13 | public class MultiLineChartActivity extends AppCompatActivity { 14 | 15 | private LineChart lineChart; 16 | private List xAxisValues; 17 | private List titles; 18 | private List> yAxisValues; 19 | 20 | @Override 21 | protected void onCreate(Bundle savedInstanceState) { 22 | super.onCreate(savedInstanceState); 23 | setContentView(R.layout.activity_line_chart); 24 | 25 | initView(); 26 | initData(); 27 | MPChartHelper.setLinesChart(lineChart,xAxisValues,yAxisValues,titles,false,null); 28 | } 29 | 30 | private void initView(){ 31 | lineChart=(LineChart)findViewById(R.id.lineChart); 32 | } 33 | 34 | private void initData(){ 35 | xAxisValues = new ArrayList<>(); 36 | List yAxisValues1 = new ArrayList<>(); 37 | List yAxisValues2 = new ArrayList<>(); 38 | List yAxisValues3 = new ArrayList<>(); 39 | for(int i=0;i<10;++i){ 40 | xAxisValues.add(String.valueOf(i)); 41 | yAxisValues1.add((float)(Math.random()*20+20)); 42 | yAxisValues2.add((float)(Math.random()*20+40)); 43 | yAxisValues3.add((float)(Math.random()*20+60)); 44 | } 45 | 46 | yAxisValues = new ArrayList<>(); 47 | yAxisValues.add(yAxisValues1); 48 | yAxisValues.add(yAxisValues2); 49 | yAxisValues.add(yAxisValues3); 50 | 51 | titles = new ArrayList<>(); 52 | titles.add("折线1"); 53 | titles.add("折线2"); 54 | titles.add("折线3"); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /app/src/main/java/com/charlie/mpandroidcharttest/chartactivity/PieChartActivity.java: -------------------------------------------------------------------------------- 1 | package com.charlie.mpandroidcharttest.chartactivity; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | 6 | import com.charlie.mpandroidcharttest.R; 7 | import com.charlie.mpandroidcharttest.util.MPChartHelper; 8 | import com.github.mikephil.charting.charts.PieChart; 9 | 10 | import java.util.ArrayList; 11 | import java.util.LinkedHashMap; 12 | import java.util.Map; 13 | 14 | public class PieChartActivity extends AppCompatActivity { 15 | 16 | private PieChart pieChart; 17 | private Map pieValues; 18 | 19 | @Override 20 | protected void onCreate(Bundle savedInstanceState) { 21 | super.onCreate(savedInstanceState); 22 | setContentView(R.layout.activity_pie_chart); 23 | 24 | initView(); 25 | initData(); 26 | MPChartHelper.setPieChart(pieChart,pieValues,"饼图",true); 27 | } 28 | 29 | private void initView(){ 30 | pieChart=(PieChart)findViewById(R.id.pieChart); 31 | } 32 | 33 | private void initData(){ 34 | pieValues=new LinkedHashMap<>(); 35 | pieValues.put("A",100f); 36 | pieValues.put("B",150f); 37 | pieValues.put("C",30f); 38 | pieValues.put("D",70f); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /app/src/main/java/com/charlie/mpandroidcharttest/chartactivity/PositiveNegativeBarChartActivity.java: -------------------------------------------------------------------------------- 1 | package com.charlie.mpandroidcharttest.chartactivity; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | 6 | import com.charlie.mpandroidcharttest.R; 7 | import com.charlie.mpandroidcharttest.util.MPChartHelper; 8 | import com.github.mikephil.charting.charts.BarChart; 9 | 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | 13 | public class PositiveNegativeBarChartActivity extends AppCompatActivity { 14 | 15 | private BarChart barChart1; 16 | 17 | private List xAxisValues; 18 | private List yAxisValues; 19 | 20 | @Override 21 | protected void onCreate(Bundle savedInstanceState) { 22 | super.onCreate(savedInstanceState); 23 | setContentView(R.layout.activity_bar_chart); 24 | 25 | initView(); 26 | initData(); 27 | MPChartHelper.setPositiveNegativeBarChart(barChart1,xAxisValues,yAxisValues,"正负柱状图"); 28 | } 29 | 30 | private void initView(){ 31 | barChart1=(BarChart)findViewById(R.id.barChart1); 32 | } 33 | 34 | private void initData(){ 35 | xAxisValues = new ArrayList<>(); 36 | yAxisValues = new ArrayList<>(); 37 | for(int i=0;i<10;++i){ 38 | xAxisValues.add(String.valueOf(i)); 39 | yAxisValues.add((float)(Math.random()*20-10)); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /app/src/main/java/com/charlie/mpandroidcharttest/chartactivity/ThreeBarChartActivity.java: -------------------------------------------------------------------------------- 1 | package com.charlie.mpandroidcharttest.chartactivity; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | 6 | import com.charlie.mpandroidcharttest.R; 7 | import com.charlie.mpandroidcharttest.util.MPChartHelper; 8 | import com.github.mikephil.charting.charts.BarChart; 9 | 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | 13 | public class ThreeBarChartActivity extends AppCompatActivity { 14 | 15 | private BarChart barChart1; 16 | 17 | private List xAxisValues; 18 | private List yAxisValues1; 19 | private List yAxisValues2; 20 | private List yAxisValues3; 21 | 22 | @Override 23 | protected void onCreate(Bundle savedInstanceState) { 24 | super.onCreate(savedInstanceState); 25 | setContentView(R.layout.activity_bar_chart); 26 | 27 | initView(); 28 | initData(); 29 | MPChartHelper.setThreeBarChart(barChart1,xAxisValues,yAxisValues1,yAxisValues2,yAxisValues3,"柱状图1","柱状图2","柱状图3"); 30 | } 31 | 32 | private void initView(){ 33 | barChart1=(BarChart)findViewById(R.id.barChart1); 34 | } 35 | 36 | private void initData(){ 37 | xAxisValues = new ArrayList<>(); 38 | yAxisValues1 = new ArrayList<>(); 39 | yAxisValues2 = new ArrayList<>(); 40 | yAxisValues3 = new ArrayList<>(); 41 | for(int i=0;i<10;++i){ 42 | xAxisValues.add(i); 43 | yAxisValues1.add((float)(Math.random()*800+20)); 44 | yAxisValues2.add((float)(Math.random()*1000+20)); 45 | yAxisValues3.add((float)(Math.random()*900+20)); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /app/src/main/java/com/charlie/mpandroidcharttest/chartactivity/TwoBarChartActivity.java: -------------------------------------------------------------------------------- 1 | package com.charlie.mpandroidcharttest.chartactivity; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | 6 | import com.charlie.mpandroidcharttest.R; 7 | import com.charlie.mpandroidcharttest.util.MPChartHelper; 8 | import com.github.mikephil.charting.charts.BarChart; 9 | 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | 13 | public class TwoBarChartActivity extends AppCompatActivity { 14 | 15 | private BarChart barChart1; 16 | 17 | private List xAxisValues; 18 | private List yAxisValues1; 19 | private List yAxisValues2; 20 | 21 | @Override 22 | protected void onCreate(Bundle savedInstanceState) { 23 | super.onCreate(savedInstanceState); 24 | setContentView(R.layout.activity_bar_chart); 25 | 26 | initView(); 27 | initData(); 28 | MPChartHelper.setTwoBarChart(barChart1,xAxisValues,yAxisValues1,yAxisValues2,"柱状图1","柱状图2"); 29 | } 30 | 31 | private void initView(){ 32 | barChart1=(BarChart)findViewById(R.id.barChart1); 33 | } 34 | 35 | private void initData(){ 36 | xAxisValues = new ArrayList<>(); 37 | yAxisValues1 = new ArrayList<>(); 38 | yAxisValues2 = new ArrayList<>(); 39 | for(int i=0;i<10;++i){ 40 | xAxisValues.add(i); 41 | yAxisValues1.add((float)(Math.random()*800+20)); 42 | yAxisValues2.add((float)(Math.random()*1000+20)); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /app/src/main/java/com/charlie/mpandroidcharttest/common/ArrowTextView.java: -------------------------------------------------------------------------------- 1 | package com.charlie.mpandroidcharttest.common; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.graphics.Canvas; 6 | import android.graphics.Color; 7 | import android.graphics.Paint; 8 | import android.graphics.Path; 9 | import android.graphics.RectF; 10 | import android.util.AttributeSet; 11 | import android.util.TypedValue; 12 | import android.widget.TextView; 13 | 14 | import com.charlie.mpandroidcharttest.R; 15 | 16 | 17 | /** 18 | * Created by JKWANG-PC on 2016/11/1. 19 | * 带下箭头的文本框。 20 | */ 21 | 22 | public class ArrowTextView extends TextView { 23 | 24 | private float radius; 25 | private float arrowWidth; 26 | private float arrowHeight; 27 | private int color; 28 | 29 | public ArrowTextView(Context context, AttributeSet attrs) { 30 | super(context, attrs); 31 | ini(context, attrs); 32 | } 33 | 34 | private void ini(Context context, AttributeSet attrs) { 35 | TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ArrowTextView); 36 | radius = typedArray.getDimension(R.styleable.ArrowTextView_radius, 0); 37 | arrowWidth = typedArray.getDimension(R.styleable.ArrowTextView_arrowWidth, 0); 38 | arrowHeight = typedArray.getDimension(R.styleable.ArrowTextView_arrowHeight, 0); 39 | color = typedArray.getColor(R.styleable.ArrowTextView_bg, Color.RED); 40 | } 41 | 42 | public ArrowTextView(Context context, AttributeSet attrs, int defStyle) { 43 | super(context, attrs, defStyle); 44 | ini(context, attrs); 45 | } 46 | 47 | 48 | public ArrowTextView(Context context) { 49 | super(context); 50 | } 51 | 52 | 53 | /** 54 | * @param arrowWidth 三角形箭头的宽度....... 55 | */ 56 | public void setArrowWidth(float arrowWidth) { 57 | this.arrowWidth = arrowWidth; 58 | invalidate(); 59 | 60 | } 61 | 62 | /** 63 | * @param arrowHeight 三角形箭头的高度...... 64 | */ 65 | public void setArrowHeight(float arrowHeight) { 66 | this.arrowHeight = arrowHeight; 67 | invalidate(); 68 | } 69 | 70 | /** 71 | * @param radius 矩形四角圆角的半径.......... 72 | */ 73 | public void setRadius(float radius) { 74 | this.radius = radius; 75 | invalidate(); 76 | 77 | } 78 | 79 | /** 80 | * @param color 箭头矩形的背景色......... 81 | */ 82 | public void setBgColor(int color) { 83 | this.color = color; 84 | invalidate(); 85 | 86 | } 87 | 88 | @Override 89 | protected void onDraw(Canvas canvas) { 90 | Paint paint = new Paint(); 91 | paint.setColor(color == 0 ? Color.RED : color); 92 | paint.setAntiAlias(true); 93 | if (radius == 0) { 94 | radius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 5, getResources().getDisplayMetrics()); 95 | } 96 | if (arrowWidth == 0) { 97 | arrowWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics()); 98 | } 99 | if (arrowHeight == 0) { 100 | arrowHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 15, getResources().getDisplayMetrics()); 101 | } 102 | //带圆角的矩形(下边减去三角形的高度...........) 103 | int width = getWidth(); 104 | Float height = getHeight() - arrowHeight; 105 | canvas.drawRoundRect(new RectF(0, 0, getWidth(), height), radius, radius, paint); 106 | 107 | 108 | //画三角形 109 | Path path = new Path(); 110 | path.setFillType(Path.FillType.EVEN_ODD); 111 | float xMiddle = width / 2; 112 | float xLeft = xMiddle - arrowWidth / 2; 113 | float xRight = xMiddle + arrowWidth / 2; 114 | float yBottom = height + arrowHeight; 115 | path.moveTo(xMiddle, yBottom); 116 | path.lineTo(xLeft, height-1); 117 | path.lineTo(xRight, height-1); 118 | path.lineTo(xMiddle, yBottom); 119 | path.close(); 120 | canvas.drawPath(path, paint); 121 | // canvas.restore(); 122 | // canvas.translate(left, 0); 123 | super.onDraw(canvas); 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /app/src/main/java/com/charlie/mpandroidcharttest/common/MPChartMarkerView.java: -------------------------------------------------------------------------------- 1 | package com.charlie.mpandroidcharttest.common; 2 | 3 | import android.content.Context; 4 | 5 | import com.charlie.mpandroidcharttest.R; 6 | import com.charlie.mpandroidcharttest.util.StringUtils; 7 | import com.github.mikephil.charting.components.MarkerView; 8 | import com.github.mikephil.charting.data.CandleEntry; 9 | import com.github.mikephil.charting.data.Entry; 10 | import com.github.mikephil.charting.highlight.Highlight; 11 | import com.github.mikephil.charting.utils.MPPointF; 12 | 13 | /** 14 | * Created by JKWANG-PC on 2016/11/1. 15 | * 16 | * Since release v3.0.0, markers (popup views) in the chart are represented by the IMarker interface. 17 | */ 18 | 19 | public class MPChartMarkerView extends MarkerView { 20 | 21 | private ArrowTextView tvContent; 22 | 23 | /** 24 | * Constructor. Sets up the MarkerView with a custom layout resource. 25 | * 26 | * @param context 27 | * @param layoutResource the layout resource to use for the MarkerView 28 | */ 29 | public MPChartMarkerView(Context context, int layoutResource) { 30 | super(context, layoutResource); 31 | 32 | tvContent = (ArrowTextView) findViewById(R.id.tvContent); 33 | } 34 | 35 | @Override 36 | public void refreshContent(Entry e, Highlight highlight) { 37 | if (e instanceof CandleEntry) { 38 | 39 | CandleEntry ce = (CandleEntry) e; 40 | 41 | tvContent.setText(StringUtils.double2String(ce.getHigh(), 2)); 42 | } else { 43 | 44 | tvContent.setText(StringUtils.double2String(e.getY(), 2)); 45 | } 46 | 47 | super.refreshContent(e, highlight);//必须加上该句话;This sentence must be added. 48 | } 49 | 50 | private MPPointF mOffset; 51 | 52 | @Override 53 | public MPPointF getOffset() { 54 | if(mOffset == null) { 55 | // center the marker horizontally and vertically 56 | mOffset = new MPPointF(-(getWidth() / 2), -getHeight()); 57 | } 58 | 59 | return mOffset; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /app/src/main/java/com/charlie/mpandroidcharttest/common/MyValueFormatter.java: -------------------------------------------------------------------------------- 1 | package com.charlie.mpandroidcharttest.common; 2 | 3 | import com.charlie.mpandroidcharttest.util.StringUtils; 4 | import com.github.mikephil.charting.data.Entry; 5 | import com.github.mikephil.charting.formatter.IValueFormatter; 6 | import com.github.mikephil.charting.utils.ViewPortHandler; 7 | 8 | /** 9 | * Created by JKWANG-PC on 2016/11/21. 10 | */ 11 | 12 | public class MyValueFormatter implements IValueFormatter { 13 | 14 | @Override 15 | public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) { 16 | return StringUtils.double2String(value, 2); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /app/src/main/java/com/charlie/mpandroidcharttest/common/StringAxisValueFormatter.java: -------------------------------------------------------------------------------- 1 | package com.charlie.mpandroidcharttest.common; 2 | 3 | import com.github.mikephil.charting.components.AxisBase; 4 | import com.github.mikephil.charting.formatter.IAxisValueFormatter; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | * Created by Charlie on 2016/9/23. 10 | * 对字符串类型的坐标轴标记进行格式化 11 | */ 12 | public class StringAxisValueFormatter implements IAxisValueFormatter { 13 | 14 | //区域值 15 | private List mStrs; 16 | 17 | /** 18 | * 对字符串类型的坐标轴标记进行格式化 19 | * @param strs 20 | */ 21 | public StringAxisValueFormatter(List strs){ 22 | this.mStrs =strs; 23 | } 24 | 25 | @Override 26 | public String getFormattedValue(float v, AxisBase axisBase) { 27 | return mStrs.get((int)v); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /app/src/main/java/com/charlie/mpandroidcharttest/util/MPChartHelper.java: -------------------------------------------------------------------------------- 1 | package com.charlie.mpandroidcharttest.util; 2 | 3 | import android.graphics.Color; 4 | import android.support.v4.content.ContextCompat; 5 | 6 | import com.charlie.mpandroidcharttest.R; 7 | import com.charlie.mpandroidcharttest.common.MPChartMarkerView; 8 | import com.charlie.mpandroidcharttest.common.MyValueFormatter; 9 | import com.charlie.mpandroidcharttest.common.StringAxisValueFormatter; 10 | import com.github.mikephil.charting.animation.Easing; 11 | import com.github.mikephil.charting.charts.BarChart; 12 | import com.github.mikephil.charting.charts.CombinedChart; 13 | import com.github.mikephil.charting.charts.LineChart; 14 | import com.github.mikephil.charting.charts.PieChart; 15 | import com.github.mikephil.charting.components.AxisBase; 16 | import com.github.mikephil.charting.components.Legend; 17 | import com.github.mikephil.charting.components.XAxis; 18 | import com.github.mikephil.charting.components.YAxis; 19 | import com.github.mikephil.charting.data.BarData; 20 | import com.github.mikephil.charting.data.BarDataSet; 21 | import com.github.mikephil.charting.data.BarEntry; 22 | import com.github.mikephil.charting.data.CombinedData; 23 | import com.github.mikephil.charting.data.Entry; 24 | import com.github.mikephil.charting.data.LineData; 25 | import com.github.mikephil.charting.data.LineDataSet; 26 | import com.github.mikephil.charting.data.PieData; 27 | import com.github.mikephil.charting.data.PieDataSet; 28 | import com.github.mikephil.charting.data.PieEntry; 29 | import com.github.mikephil.charting.formatter.IAxisValueFormatter; 30 | import com.github.mikephil.charting.formatter.IValueFormatter; 31 | import com.github.mikephil.charting.formatter.PercentFormatter; 32 | import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; 33 | import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; 34 | import com.github.mikephil.charting.utils.ViewPortHandler; 35 | 36 | import java.text.DecimalFormat; 37 | import java.util.ArrayList; 38 | import java.util.Collections; 39 | import java.util.Iterator; 40 | import java.util.List; 41 | import java.util.Map; 42 | import java.util.Set; 43 | 44 | /** 45 | * Created by JKWANG-PC on 2016/10/20. 46 | */ 47 | 48 | public class MPChartHelper { 49 | 50 | public static final int[] PIE_COLORS = { 51 | Color.rgb(181, 194, 202), Color.rgb(129, 216, 200), Color.rgb(241, 214, 145), 52 | Color.rgb(108, 176, 223), Color.rgb(195, 221, 155), Color.rgb(251, 215, 191), 53 | Color.rgb(237, 189, 189), Color.rgb(172, 217, 243) 54 | }; 55 | 56 | public static final int[] LINE_COLORS = { 57 | Color.rgb(140, 210, 118), Color.rgb(159, 143, 186), Color.rgb(233, 197, 23) 58 | };//绿色,紫色,黄色 59 | 60 | public static final int[] LINE_FILL_COLORS = { 61 | Color.rgb(222, 239, 228), Color.rgb(246, 234, 208), Color.rgb(235, 228, 248) 62 | }; 63 | 64 | 65 | /** 66 | * 单数据集。设置柱状图样式,X轴为字符串,Y轴为数值 67 | * 68 | * @param barChart 69 | * @param xAxisValue 70 | * @param yAxisValue 71 | * @param title 图例文字 72 | * @param xAxisTextSize x轴标签字体大小 73 | * @param barColor 74 | */ 75 | public static void setBarChart(BarChart barChart, List xAxisValue, List yAxisValue, String title, float xAxisTextSize, Integer barColor) { 76 | barChart.getDescription().setEnabled(false);//设置描述 77 | barChart.setPinchZoom(true);//设置按比例放缩柱状图 78 | 79 | //设置自定义的markerView 80 | MPChartMarkerView markerView = new MPChartMarkerView(barChart.getContext(), R.layout.custom_marker_view); 81 | barChart.setMarker(markerView); 82 | 83 | //x坐标轴设置 84 | IAxisValueFormatter xAxisFormatter = new StringAxisValueFormatter(xAxisValue);//设置自定义的x轴值格式化器 85 | XAxis xAxis = barChart.getXAxis();//获取x轴 86 | xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//设置X轴标签显示位置 87 | xAxis.setDrawGridLines(false);//不绘制格网线 88 | xAxis.setGranularity(1f);//设置最小间隔,防止当放大时,出现重复标签。 89 | xAxis.setValueFormatter(xAxisFormatter); 90 | xAxis.setTextSize(xAxisTextSize);//设置标签字体大小 91 | xAxis.setLabelCount(xAxisValue.size());//设置标签显示的个数 92 | 93 | //y轴设置 94 | YAxis leftAxis = barChart.getAxisLeft();//获取左侧y轴 95 | leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);//设置y轴标签显示在外侧 96 | leftAxis.setAxisMinimum(0f);//设置Y轴最小值 97 | leftAxis.setDrawGridLines(false); 98 | leftAxis.setDrawLabels(false);//禁止绘制y轴标签 99 | leftAxis.setDrawAxisLine(false);//禁止绘制y轴 100 | 101 | barChart.getAxisRight().setEnabled(false);//禁用右侧y轴 102 | 103 | //图例设置 104 | Legend legend = barChart.getLegend(); 105 | legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);//图例水平居中 106 | legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);//图例在图表上方 107 | legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);//图例的方向为水平 108 | legend.setDrawInside(false);//绘制在chart的外侧 109 | legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);//图例中的文字方向 110 | 111 | legend.setForm(Legend.LegendForm.SQUARE);//图例窗体的形状 112 | legend.setFormSize(0f);//图例窗体的大小 113 | legend.setTextSize(16f);//图例文字的大小 114 | //legend.setYOffset(-2f); 115 | 116 | //设置柱状图数据 117 | setBarChartData(barChart, yAxisValue, title, barColor); 118 | 119 | barChart.setExtraBottomOffset(10);//距视图窗口底部的偏移,类似与paddingbottom 120 | barChart.setExtraTopOffset(30);//距视图窗口顶部的偏移,类似与paddingtop 121 | barChart.setFitBars(true);//使两侧的柱图完全显示 122 | barChart.animateX(1500);//数据显示动画,从左往右依次显示 123 | } 124 | 125 | /** 126 | * 设置柱图 127 | * 128 | * @param barChart 129 | * @param yAxisValue 130 | * @param title 131 | * @param barColor 132 | */ 133 | private static void setBarChartData(BarChart barChart, List yAxisValue, String title, Integer barColor) { 134 | 135 | ArrayList entries = new ArrayList<>(); 136 | 137 | for (int i = 0, n = yAxisValue.size(); i < n; ++i) { 138 | entries.add(new BarEntry(i, yAxisValue.get(i))); 139 | } 140 | 141 | BarDataSet set1; 142 | 143 | if (barChart.getData() != null && barChart.getData().getDataSetCount() > 0) { 144 | set1 = (BarDataSet) barChart.getData().getDataSetByIndex(0); 145 | set1.setValues(entries); 146 | barChart.getData().notifyDataChanged(); 147 | barChart.notifyDataSetChanged(); 148 | } else { 149 | set1 = new BarDataSet(entries, title); 150 | if (barColor == null) { 151 | set1.setColor(ContextCompat.getColor(barChart.getContext(), R.color.bar));//设置set1的柱的颜色 152 | } else { 153 | set1.setColor(barColor); 154 | } 155 | 156 | ArrayList dataSets = new ArrayList<>(); 157 | dataSets.add(set1); 158 | 159 | BarData data = new BarData(dataSets); 160 | data.setValueTextSize(10f); 161 | data.setBarWidth(0.9f); 162 | data.setValueFormatter(new MyValueFormatter()); 163 | 164 | barChart.setData(data); 165 | } 166 | } 167 | 168 | 169 | /** 170 | * 设置正负值在0轴上下方的柱图 171 | * 172 | * @param barChart 173 | * @param xAxisValue x轴的值。必须与yAxisValue的值个数相同 174 | * @param yAxisValue y轴的值。必须与xAxisValue的值个数相同 175 | * @param title 176 | */ 177 | public static void setPositiveNegativeBarChart(BarChart barChart, List xAxisValue, List yAxisValue, String title) { 178 | barChart.setDrawBarShadow(false); 179 | barChart.setDrawValueAboveBar(true); 180 | barChart.getDescription().setEnabled(false); 181 | // scaling can now only be done on x- and y-axis separately 182 | barChart.setPinchZoom(false); 183 | barChart.setDrawGridBackground(false); 184 | barChart.setExtraBottomOffset(10); 185 | barChart.setExtraTopOffset(30); 186 | 187 | MPChartMarkerView markerView = new MPChartMarkerView(barChart.getContext(), R.layout.custom_marker_view); 188 | barChart.setMarker(markerView); 189 | 190 | XAxis xAxis = barChart.getXAxis(); 191 | xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); 192 | xAxis.setDrawGridLines(false); 193 | xAxis.setDrawAxisLine(false); 194 | xAxis.setTextColor(Color.DKGRAY); 195 | xAxis.setTextSize(13f); 196 | xAxis.setAxisMinimum(0f); 197 | xAxis.setAxisMaximum(xAxisValue.size()); 198 | xAxis.setLabelCount(xAxisValue.size()); 199 | xAxis.setCenterAxisLabels(true); 200 | xAxis.setGranularity(1f); 201 | 202 | YAxis left = barChart.getAxisLeft(); 203 | left.setDrawLabels(false); 204 | left.setSpaceTop(25f); 205 | left.setSpaceBottom(25f); 206 | left.setDrawAxisLine(false); 207 | left.setDrawGridLines(false); 208 | left.setDrawZeroLine(true); // draw a zero line 209 | left.setZeroLineColor(Color.DKGRAY); 210 | left.setZeroLineWidth(1f); 211 | barChart.getAxisRight().setEnabled(false); 212 | 213 | Legend legend = barChart.getLegend(); 214 | legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER); 215 | legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); 216 | legend.setOrientation(Legend.LegendOrientation.HORIZONTAL); 217 | legend.setDrawInside(false); 218 | legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT); 219 | 220 | legend.setForm(Legend.LegendForm.SQUARE); 221 | legend.setFormSize(0f); 222 | legend.setTextSize(16f); 223 | legend.setYOffset(-2f); 224 | 225 | 226 | // THIS IS THE ORIGINAL DATA YOU WANT TO PLOT 227 | final List data = new ArrayList<>(); 228 | for (int i = 0, n = xAxisValue.size(); i < n; ++i) { 229 | data.add(new Data(0.5f + i, yAxisValue.get(i), xAxisValue.get(i))); 230 | } 231 | 232 | xAxis.setValueFormatter(new IAxisValueFormatter() { 233 | @Override 234 | public String getFormattedValue(float value, AxisBase axis) { 235 | return data.get(Math.min(Math.max((int) value, 0), data.size() - 1)).xAxisValue; 236 | } 237 | }); 238 | 239 | setData(barChart, data, title); 240 | } 241 | 242 | private static void setData(BarChart barChart, List dataList, String title) { 243 | 244 | ArrayList values = new ArrayList(); 245 | List colors = new ArrayList(); 246 | 247 | int green = Color.rgb(195, 221, 155); 248 | int red = Color.rgb(237, 189, 189); 249 | 250 | for (int i = 0; i < dataList.size(); i++) { 251 | 252 | Data d = dataList.get(i); 253 | BarEntry entry = new BarEntry(d.xValue, d.yValue); 254 | values.add(entry); 255 | 256 | // specific colors 257 | if (d.yValue >= 0) 258 | colors.add(red); 259 | else 260 | colors.add(green); 261 | } 262 | 263 | BarDataSet set; 264 | 265 | if (barChart.getData() != null && 266 | barChart.getData().getDataSetCount() > 0) { 267 | set = (BarDataSet) barChart.getData().getDataSetByIndex(0); 268 | set.setValues(values); 269 | barChart.getData().notifyDataChanged(); 270 | barChart.notifyDataSetChanged(); 271 | } else { 272 | set = new BarDataSet(values, title); 273 | set.setColors(colors); 274 | set.setValueTextColors(colors); 275 | 276 | BarData data = new BarData(set); 277 | data.setValueTextSize(13f); 278 | data.setValueFormatter(new PositiveNegativeBarChartValueFormatter()); 279 | data.setBarWidth(0.8f); 280 | 281 | barChart.setData(data); 282 | barChart.invalidate(); 283 | } 284 | } 285 | 286 | /** 287 | * positive-negative data model representing data. 288 | */ 289 | private static class Data { 290 | 291 | public String xAxisValue; 292 | public float yValue; 293 | public float xValue; 294 | 295 | public Data(float xValue, float yValue, String xAxisValue) { 296 | this.xAxisValue = xAxisValue; 297 | this.yValue = yValue; 298 | this.xValue = xValue; 299 | } 300 | } 301 | 302 | private static class PositiveNegativeBarChartValueFormatter implements IValueFormatter { 303 | 304 | private DecimalFormat mFormattedStringCache; 305 | 306 | public PositiveNegativeBarChartValueFormatter() { 307 | mFormattedStringCache = new DecimalFormat("######.00"); 308 | } 309 | 310 | @Override 311 | public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) { 312 | return mFormattedStringCache.format(value); 313 | } 314 | } 315 | 316 | 317 | /** 318 | * 设置双柱状图样式 319 | * 320 | * @param barChart 321 | * @param xAxisValue 322 | * @param yAxisValue1 323 | * @param yAxisValue2 324 | * @param bartilte1 325 | * @param bartitle2 326 | */ 327 | public static void setTwoBarChart(BarChart barChart, List xAxisValue, List yAxisValue1, List yAxisValue2, String bartilte1, String bartitle2) { 328 | barChart.getDescription().setEnabled(false);//设置描述 329 | barChart.setPinchZoom(true);//设置按比例放缩柱状图 330 | barChart.setExtraBottomOffset(10); 331 | barChart.setExtraTopOffset(30); 332 | 333 | MPChartMarkerView markerView = new MPChartMarkerView(barChart.getContext(), R.layout.custom_marker_view); 334 | barChart.setMarker(markerView); 335 | 336 | //x坐标轴设置 337 | XAxis xAxis = barChart.getXAxis(); 338 | xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); 339 | xAxis.setDrawGridLines(false); 340 | xAxis.setGranularity(1f); 341 | xAxis.setLabelCount(xAxisValue.size()); 342 | xAxis.setCenterAxisLabels(true);//设置标签居中 343 | xAxis.setValueFormatter(new IAxisValueFormatter() { 344 | @Override 345 | public String getFormattedValue(float v, AxisBase axisBase) { 346 | return String.valueOf((int) v); 347 | } 348 | }); 349 | 350 | //y轴设置 351 | YAxis leftAxis = barChart.getAxisLeft(); 352 | leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART); 353 | leftAxis.setDrawGridLines(false); 354 | leftAxis.setDrawLabels(false); 355 | leftAxis.setDrawAxisLine(false); 356 | 357 | //设置坐标轴最大最小值 358 | Float yMin1 = Collections.min(yAxisValue1); 359 | Float yMin2 = Collections.min(yAxisValue2); 360 | Float yMax1 = Collections.max(yAxisValue1); 361 | Float yMax2 = Collections.max(yAxisValue2); 362 | Float yMin = Double.valueOf((yMin1 < yMin2 ? yMin1 : yMin2) * 0.1).floatValue(); 363 | Float yMax = Double.valueOf((yMax1 > yMax2 ? yMax1 : yMax2) * 1.1).floatValue(); 364 | leftAxis.setAxisMaximum(yMax); 365 | leftAxis.setAxisMinimum(yMin); 366 | 367 | barChart.getAxisRight().setEnabled(false); 368 | 369 | 370 | //图例设置 371 | Legend legend = barChart.getLegend(); 372 | legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER); 373 | legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); 374 | legend.setOrientation(Legend.LegendOrientation.HORIZONTAL); 375 | legend.setDrawInside(false); 376 | legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT); 377 | legend.setForm(Legend.LegendForm.SQUARE); 378 | legend.setTextSize(12f); 379 | 380 | //设置柱状图数据 381 | setTwoBarChartData(barChart, xAxisValue, yAxisValue1, yAxisValue2, bartilte1, bartitle2); 382 | 383 | barChart.animateX(1500);//数据显示动画,从左往右依次显示 384 | barChart.invalidate(); 385 | } 386 | 387 | /** 388 | * 设置柱状图数据源 389 | */ 390 | private static void setTwoBarChartData(BarChart barChart, List xAxisValue, List yAxisValue1, List yAxisValue2, String bartilte1, String bartitle2) { 391 | float groupSpace = 0.04f; 392 | float barSpace = 0.03f; 393 | float barWidth = 0.45f; 394 | // (0.45 + 0.03) * 2 + 0.04 = 1,即一个间隔为一组,包含两个柱图 -> interval per "group" 395 | 396 | ArrayList entries1 = new ArrayList<>(); 397 | ArrayList entries2 = new ArrayList<>(); 398 | 399 | for (int i = 0, n = yAxisValue1.size(); i < n; ++i) { 400 | entries1.add(new BarEntry(xAxisValue.get(i), yAxisValue1.get(i))); 401 | entries2.add(new BarEntry(xAxisValue.get(i), yAxisValue2.get(i))); 402 | } 403 | 404 | BarDataSet dataset1, dataset2; 405 | 406 | if (barChart.getData() != null && barChart.getData().getDataSetCount() > 0) { 407 | dataset1 = (BarDataSet) barChart.getData().getDataSetByIndex(0); 408 | dataset2 = (BarDataSet) barChart.getData().getDataSetByIndex(1); 409 | dataset1.setValues(entries1); 410 | dataset2.setValues(entries2); 411 | barChart.getData().notifyDataChanged(); 412 | barChart.notifyDataSetChanged(); 413 | } else { 414 | dataset1 = new BarDataSet(entries1, bartilte1); 415 | dataset2 = new BarDataSet(entries2, bartitle2); 416 | 417 | dataset1.setColor(Color.rgb(129, 216, 200)); 418 | dataset2.setColor(Color.rgb(181, 194, 202)); 419 | 420 | ArrayList dataSets = new ArrayList<>(); 421 | dataSets.add(dataset1); 422 | dataSets.add(dataset2); 423 | 424 | BarData data = new BarData(dataSets); 425 | data.setValueTextSize(10f); 426 | data.setBarWidth(0.9f); 427 | data.setValueFormatter(new IValueFormatter() { 428 | @Override 429 | public String getFormattedValue(float value, Entry entry, int i, ViewPortHandler viewPortHandler) { 430 | return StringUtils.double2String(value, 2); 431 | } 432 | }); 433 | 434 | barChart.setData(data); 435 | } 436 | 437 | barChart.getBarData().setBarWidth(barWidth); 438 | barChart.getXAxis().setAxisMinimum(xAxisValue.get(0)); 439 | // barData.getGroupWith(...) is a helper that calculates the width each group needs based on the provided parameters 440 | barChart.getXAxis().setAxisMaximum(barChart.getBarData().getGroupWidth(groupSpace, barSpace) * xAxisValue.size() + xAxisValue.get(0)); 441 | barChart.groupBars(xAxisValue.get(0), groupSpace, barSpace); 442 | } 443 | 444 | 445 | /** 446 | * 设置三柱状图样式 447 | * 448 | * @param barChart 449 | * @param xAxisValue 450 | * @param yAxisValue1 451 | * @param yAxisValue2 452 | * @param yAxisValue3 453 | * @param bartilte1 454 | * @param bartitle2 455 | * @param bartitle3 456 | */ 457 | public static void setThreeBarChart(BarChart barChart, List xAxisValue, List yAxisValue1, List yAxisValue2, List yAxisValue3, String bartilte1, String bartitle2, String bartitle3) { 458 | barChart.getDescription().setEnabled(false);//设置描述 459 | barChart.setPinchZoom(false);//设置不按比例放缩柱状图 460 | barChart.setExtraBottomOffset(10); 461 | barChart.setExtraTopOffset(30); 462 | 463 | MPChartMarkerView markerView = new MPChartMarkerView(barChart.getContext(), R.layout.custom_marker_view); 464 | barChart.setMarker(markerView); 465 | 466 | //x坐标轴设置 467 | XAxis xAxis = barChart.getXAxis(); 468 | xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); 469 | xAxis.setGranularity(1f); 470 | xAxis.setLabelCount(xAxisValue.size()); 471 | xAxis.setCenterAxisLabels(true); 472 | xAxis.setDrawGridLines(false); 473 | xAxis.setValueFormatter(new IAxisValueFormatter() { 474 | @Override 475 | public String getFormattedValue(float v, AxisBase axisBase) { 476 | return String.valueOf((int) v); 477 | } 478 | }); 479 | 480 | //y轴设置 481 | YAxis leftAxis = barChart.getAxisLeft(); 482 | leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART); 483 | leftAxis.setDrawGridLines(false); 484 | leftAxis.setDrawLabels(false); 485 | leftAxis.setDrawAxisLine(false); 486 | 487 | Float yMin1 = Collections.min(yAxisValue1); 488 | Float yMin2 = Collections.min(yAxisValue2); 489 | Float yMin3 = Collections.min(yAxisValue3); 490 | Float yMax1 = Collections.max(yAxisValue1); 491 | Float yMax2 = Collections.max(yAxisValue2); 492 | Float yMax3 = Collections.max(yAxisValue3); 493 | Float yMinTemp = yMin1 < yMin2 ? yMin1 : yMin2; 494 | Float yMin = yMinTemp < yMin3 ? yMinTemp : yMin3; 495 | Float yMaxTemp = yMax1 > yMax2 ? yMax1 : yMax2; 496 | Float yMax = yMaxTemp > yMax3 ? yMaxTemp : yMax3; 497 | leftAxis.setAxisMinimum(Double.valueOf(yMin * 0.9).floatValue()); 498 | leftAxis.setAxisMaximum(Double.valueOf(yMax * 1.1).floatValue()); 499 | 500 | 501 | barChart.getAxisRight().setEnabled(false); 502 | 503 | //图例设置 504 | Legend legend = barChart.getLegend(); 505 | legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER); 506 | legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); 507 | legend.setOrientation(Legend.LegendOrientation.HORIZONTAL); 508 | legend.setDrawInside(false); 509 | legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT); 510 | legend.setForm(Legend.LegendForm.SQUARE); 511 | legend.setTextSize(12f); 512 | 513 | //设置柱状图数据 514 | setThreeBarChartData(barChart, xAxisValue, yAxisValue1, yAxisValue2, yAxisValue3, bartilte1, bartitle2, bartitle3); 515 | 516 | barChart.animateX(1500);//数据显示动画,从左往右依次显示 517 | barChart.invalidate(); 518 | } 519 | 520 | /** 521 | * 设置三柱图数据源 522 | * 523 | * @param barChart 524 | * @param xAxisValue 525 | * @param yAxisValue1 526 | * @param yAxisValue2 527 | * @param yAxisValue3 528 | * @param bartilte1 529 | * @param bartitle2 530 | * @param bartitle3 531 | */ 532 | private static void setThreeBarChartData(BarChart barChart, List xAxisValue, List yAxisValue1, List yAxisValue2, List yAxisValue3, String bartilte1, String bartitle2, String bartitle3) { 533 | float groupSpace = 0.04f; 534 | float barSpace = 0.02f; 535 | float barWidth = 0.3f; 536 | // (0.3 + 0.02) * 3 + 0.04 = 1,即一个间隔为一组,包含三个柱图 -> interval per "group" 537 | 538 | ArrayList first_entries = new ArrayList<>(); 539 | ArrayList second_entries = new ArrayList<>(); 540 | ArrayList third_entries = new ArrayList<>(); 541 | 542 | for (int i = 0, n = xAxisValue.size(); i < n; ++i) { 543 | first_entries.add(new BarEntry(xAxisValue.get(i), yAxisValue1.get(i))); 544 | second_entries.add(new BarEntry(xAxisValue.get(i), yAxisValue2.get(i))); 545 | third_entries.add(new BarEntry(xAxisValue.get(i), yAxisValue3.get(i))); 546 | } 547 | 548 | BarDataSet first_set, second_set, third_set; 549 | 550 | if (barChart.getData() != null && barChart.getData().getDataSetCount() > 0) { 551 | first_set = (BarDataSet) barChart.getData().getDataSetByIndex(0); 552 | second_set = (BarDataSet) barChart.getData().getDataSetByIndex(1); 553 | third_set = (BarDataSet) barChart.getData().getDataSetByIndex(2); 554 | first_set.setValues(first_entries); 555 | second_set.setValues(second_entries); 556 | third_set.setValues(third_entries); 557 | barChart.getData().notifyDataChanged(); 558 | barChart.notifyDataSetChanged(); 559 | } else { 560 | first_set = new BarDataSet(first_entries, bartilte1); 561 | second_set = new BarDataSet(second_entries, bartitle2); 562 | third_set = new BarDataSet(third_entries, bartitle3); 563 | 564 | first_set.setColor(ContextCompat.getColor(barChart.getContext(), R.color.bar1)); 565 | second_set.setColor(ContextCompat.getColor(barChart.getContext(), R.color.bar2)); 566 | third_set.setColor(ContextCompat.getColor(barChart.getContext(), R.color.bar3)); 567 | 568 | ArrayList dataSets = new ArrayList<>(); 569 | dataSets.add(first_set); 570 | dataSets.add(second_set); 571 | dataSets.add(third_set); 572 | 573 | BarData data = new BarData(dataSets); 574 | data.setValueTextSize(10f); 575 | data.setBarWidth(0.9f); 576 | data.setValueFormatter(new IValueFormatter() { 577 | @Override 578 | public String getFormattedValue(float value, Entry entry, int i, ViewPortHandler viewPortHandler) { 579 | return StringUtils.double2String(value, 2); 580 | } 581 | }); 582 | 583 | barChart.setData(data); 584 | } 585 | 586 | barChart.getBarData().setBarWidth(barWidth); 587 | barChart.getXAxis().setAxisMinimum(xAxisValue.get(0)); 588 | barChart.getXAxis().setAxisMaximum(barChart.getBarData().getGroupWidth(groupSpace, barSpace) * xAxisValue.size() + xAxisValue.get(0)); 589 | barChart.groupBars(xAxisValue.get(0), groupSpace, barSpace); 590 | } 591 | 592 | 593 | 594 | 595 | /** 596 | * 单线单y轴图。 597 | * 598 | * @param lineChart 599 | * @param xAxisValue 600 | * @param yAxisValue 601 | * @param title 602 | * @param showSetValues 是否在折线上显示数据集的值。true为显示,此时y轴上的数值不可见,否则相反。 603 | */ 604 | public static void setLineChart(LineChart lineChart, List xAxisValue, List yAxisValue, String title, boolean showSetValues) { 605 | List> entriesList = new ArrayList<>(); 606 | entriesList.add(yAxisValue); 607 | 608 | List titles = new ArrayList<>(); 609 | titles.add(title); 610 | 611 | setLinesChart(lineChart, xAxisValue, entriesList, titles, showSetValues,null); 612 | } 613 | 614 | /** 615 | * 绘制线图,默认最多绘制三种颜色。所有线均依赖左侧y轴显示。 616 | * 617 | * @param lineChart 618 | * @param xAxisValue x轴的轴 619 | * @param yXAxisValues y轴的值 620 | * @param titles 每一个数据系列的标题 621 | * @param showSetValues 是否在折线上显示数据集的值。true为显示,此时y轴上的数值不可见,否则相反。 622 | * @param lineColors 线的颜色数组。为null时取默认颜色,此时最多绘制三种颜色。 623 | */ 624 | public static void setLinesChart(LineChart lineChart, List xAxisValue, List> yXAxisValues, List titles, boolean showSetValues, int[] lineColors) { 625 | lineChart.getDescription().setEnabled(false);//设置描述 626 | lineChart.setPinchZoom(true);//设置按比例放缩柱状图 627 | 628 | MPChartMarkerView markerView = new MPChartMarkerView(lineChart.getContext(), R.layout.custom_marker_view); 629 | lineChart.setMarker(markerView); 630 | 631 | 632 | //x坐标轴设置 633 | IAxisValueFormatter xAxisFormatter = new StringAxisValueFormatter(xAxisValue); 634 | XAxis xAxis = lineChart.getXAxis(); 635 | xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); 636 | xAxis.setDrawGridLines(false); 637 | xAxis.setGranularity(1f); 638 | xAxis.setLabelCount(xAxisValue.size()); 639 | /*xAxis.setAxisLineWidth(2f);*/ 640 | xAxis.setValueFormatter(xAxisFormatter); 641 | 642 | //y轴设置 643 | YAxis leftAxis = lineChart.getAxisLeft(); 644 | leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART); 645 | leftAxis.setDrawGridLines(false); 646 | if (showSetValues) { 647 | leftAxis.setDrawLabels(false);//折线上显示值,则不显示坐标轴上的值 648 | } 649 | //leftAxis.setDrawZeroLine(true); 650 | /*leftAxis.setAxisMinimum(0f);*/ 651 | /*leftAxis.setAxisLineWidth(2f);*/ 652 | 653 | lineChart.getAxisRight().setEnabled(false); 654 | 655 | //图例设置 656 | Legend legend = lineChart.getLegend(); 657 | legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER); 658 | legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); 659 | legend.setOrientation(Legend.LegendOrientation.HORIZONTAL); 660 | legend.setDrawInside(false); 661 | legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT); 662 | legend.setForm(Legend.LegendForm.LINE); 663 | legend.setTextSize(12f); 664 | 665 | //设置柱状图数据 666 | setLinesChartData(lineChart, yXAxisValues, titles, showSetValues,lineColors); 667 | 668 | lineChart.setExtraOffsets(10, 30, 20, 10); 669 | lineChart.animateX(1500);//数据显示动画,从左往右依次显示 670 | } 671 | 672 | private static void setLinesChartData(LineChart lineChart, List> yXAxisValues, List titles, boolean showSetValues, int[] lineColors) { 673 | 674 | List> entriesList = new ArrayList<>(); 675 | for (int i = 0; i < yXAxisValues.size(); ++i) { 676 | ArrayList entries = new ArrayList<>(); 677 | for (int j = 0, n = yXAxisValues.get(i).size(); j < n; j++) { 678 | entries.add(new Entry(j, yXAxisValues.get(i).get(j))); 679 | } 680 | entriesList.add(entries); 681 | } 682 | 683 | if (lineChart.getData() != null && lineChart.getData().getDataSetCount() > 0) { 684 | 685 | for (int i = 0; i < lineChart.getData().getDataSetCount(); ++i) { 686 | LineDataSet set = (LineDataSet) lineChart.getData().getDataSetByIndex(i); 687 | set.setValues(entriesList.get(i)); 688 | set.setLabel(titles.get(i)); 689 | } 690 | 691 | lineChart.getData().notifyDataChanged(); 692 | lineChart.notifyDataSetChanged(); 693 | } else { 694 | ArrayList dataSets = new ArrayList<>(); 695 | 696 | for (int i = 0; i < entriesList.size(); ++i) { 697 | LineDataSet set = new LineDataSet(entriesList.get(i), titles.get(i)); 698 | if(lineColors!=null){ 699 | set.setColor(lineColors[i % entriesList.size()]); 700 | set.setCircleColor(lineColors[i % entriesList.size()]); 701 | set.setCircleColorHole(Color.WHITE); 702 | } else { 703 | set.setColor(LINE_COLORS[i % 3]); 704 | set.setCircleColor(LINE_COLORS[i % 3]); 705 | set.setCircleColorHole(Color.WHITE); 706 | } 707 | 708 | if (entriesList.size() == 1) { 709 | set.setDrawFilled(true); 710 | set.setFillColor(LINE_FILL_COLORS[i % 3]); 711 | } 712 | dataSets.add(set); 713 | } 714 | 715 | LineData data = new LineData(dataSets); 716 | if (showSetValues) { 717 | data.setValueTextSize(10f); 718 | data.setValueFormatter(new IValueFormatter() { 719 | @Override 720 | public String getFormattedValue(float value, Entry entry, int i, ViewPortHandler viewPortHandler) { 721 | return StringUtils.double2String(value, 1); 722 | } 723 | }); 724 | } else { 725 | data.setDrawValues(false); 726 | } 727 | 728 | lineChart.setData(data); 729 | } 730 | } 731 | 732 | 733 | /** 734 | * 设置饼图样式 735 | * 736 | * @param pieChart 737 | * @param pieValues 738 | * @param title 739 | * @param showLegend 是否显示图例 740 | */ 741 | public static void setPieChart(PieChart pieChart, Map pieValues, String title, boolean showLegend) { 742 | pieChart.setUsePercentValues(true);//设置使用百分比 743 | pieChart.getDescription().setEnabled(false);//设置描述 744 | pieChart.setExtraOffsets(20, 15, 20, 15); 745 | pieChart.setCenterText(title);//设置环中的文字 746 | pieChart.setCenterTextSize(22f);//设置环中文字的大小 747 | pieChart.setDrawCenterText(true);//设置绘制环中文字 748 | pieChart.setRotationAngle(120f);//设置旋转角度 749 | 750 | 751 | //图例设置 752 | Legend legend = pieChart.getLegend(); 753 | if (showLegend) { 754 | legend.setEnabled(true); 755 | legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER); 756 | legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); 757 | legend.setOrientation(Legend.LegendOrientation.HORIZONTAL); 758 | legend.setDrawInside(false); 759 | legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT); 760 | } else { 761 | legend.setEnabled(false); 762 | } 763 | 764 | //设置饼图数据 765 | setPieChartData(pieChart, pieValues); 766 | 767 | pieChart.animateX(1500, Easing.EasingOption.EaseInOutQuad);//数据显示动画 768 | } 769 | 770 | /** 771 | * 设置饼图数据源 772 | */ 773 | private static void setPieChartData(PieChart pieChart, Map pieValues) { 774 | ArrayList entries = new ArrayList<>(); 775 | 776 | Set set = pieValues.entrySet(); 777 | Iterator it = set.iterator(); 778 | while (it.hasNext()) { 779 | Map.Entry entry = (Map.Entry) it.next(); 780 | entries.add(new PieEntry(Float.valueOf(entry.getValue().toString()), entry.getKey().toString())); 781 | } 782 | 783 | PieDataSet dataSet = new PieDataSet(entries, ""); 784 | dataSet.setSliceSpace(3f);//设置饼块之间的间隔 785 | dataSet.setSelectionShift(5f);//设置饼块选中时偏离饼图中心的距离 786 | 787 | dataSet.setColors(PIE_COLORS);//设置饼块的颜色 788 | dataSet.setValueLinePart1OffsetPercentage(80f);//数据连接线距图形片内部边界的距离,为百分数 789 | dataSet.setValueLinePart1Length(0.3f); 790 | dataSet.setValueLinePart2Length(0.4f); 791 | dataSet.setValueLineColor(Color.BLUE);//设置连接线的颜色 792 | dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE); 793 | 794 | PieData pieData = new PieData(dataSet); 795 | pieData.setValueFormatter(new PercentFormatter()); 796 | pieData.setValueTextSize(11f); 797 | pieData.setValueTextColor(Color.DKGRAY); 798 | 799 | pieChart.setData(pieData); 800 | pieChart.highlightValues(null); 801 | pieChart.invalidate(); 802 | } 803 | 804 | 805 | /** 806 | * 设置柱线组合图样式,柱图依赖左侧y轴,线图依赖右侧y轴 807 | */ 808 | public static void setCombineChart(CombinedChart combineChart, final List xAxisValues, List lineValues, List barValues, String lineTitle, String barTitle) { 809 | combineChart.getDescription().setEnabled(false);//设置描述 810 | combineChart.setPinchZoom(true);//设置按比例放缩柱状图 811 | 812 | MPChartMarkerView markerView = new MPChartMarkerView(combineChart.getContext(), R.layout.custom_marker_view); 813 | combineChart.setMarker(markerView); 814 | 815 | //设置绘制顺序,让线在柱的上层 816 | combineChart.setDrawOrder(new CombinedChart.DrawOrder[]{ 817 | CombinedChart.DrawOrder.BAR, CombinedChart.DrawOrder.LINE 818 | }); 819 | 820 | //x坐标轴设置 821 | XAxis xAxis = combineChart.getXAxis(); 822 | xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); 823 | xAxis.setDrawGridLines(false); 824 | xAxis.setGranularity(1f); 825 | xAxis.setLabelCount(xAxisValues.size() + 2); 826 | xAxis.setValueFormatter(new IAxisValueFormatter() { 827 | @Override 828 | public String getFormattedValue(float v, AxisBase axisBase) { 829 | if (v < 0 || v > (xAxisValues.size() - 1))//使得两侧柱子完全显示 830 | return ""; 831 | return xAxisValues.get((int) v); 832 | } 833 | }); 834 | 835 | //y轴设置 836 | YAxis leftAxis = combineChart.getAxisLeft(); 837 | leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART); 838 | leftAxis.setDrawGridLines(false); 839 | /*leftAxis.setAxisMinimum(0f);*/ 840 | 841 | Float yMin = Double.valueOf(Collections.min(barValues) * 0.9).floatValue(); 842 | Float yMax = Double.valueOf(Collections.max(barValues) * 1.1).floatValue(); 843 | leftAxis.setAxisMaximum(yMax); 844 | leftAxis.setAxisMinimum(yMin); 845 | 846 | 847 | YAxis rightAxis = combineChart.getAxisRight(); 848 | rightAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART); 849 | rightAxis.setDrawGridLines(false); 850 | 851 | //图例设置 852 | Legend legend = combineChart.getLegend(); 853 | legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER); 854 | legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); 855 | legend.setOrientation(Legend.LegendOrientation.HORIZONTAL); 856 | legend.setDrawInside(false); 857 | legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT); 858 | legend.setForm(Legend.LegendForm.SQUARE); 859 | legend.setTextSize(12f); 860 | 861 | //设置组合图数据 862 | CombinedData data = new CombinedData(); 863 | data.setData(generateLineData(lineValues, lineTitle)); 864 | data.setData(generateBarData(barValues, barTitle)); 865 | 866 | combineChart.setData(data);//设置组合图数据源 867 | 868 | 869 | //使得两侧柱子完全显示 870 | xAxis.setAxisMinimum(combineChart.getCombinedData().getXMin() - 1f); 871 | xAxis.setAxisMaximum(combineChart.getCombinedData().getXMax() + 1f); 872 | 873 | combineChart.setExtraTopOffset(30); 874 | combineChart.setExtraBottomOffset(10); 875 | combineChart.animateX(1500);//数据显示动画,从左往右依次显示 876 | combineChart.invalidate(); 877 | } 878 | 879 | /** 880 | * 生成线图数据 881 | */ 882 | private static LineData generateLineData(List lineValues, String lineTitle) { 883 | ArrayList lineEntries = new ArrayList<>(); 884 | 885 | for (int i = 0, n = lineValues.size(); i < n; ++i) { 886 | lineEntries.add(new Entry(i, lineValues.get(i))); 887 | } 888 | 889 | LineDataSet lineDataSet = new LineDataSet(lineEntries, lineTitle); 890 | lineDataSet.setColor(Color.rgb(233, 196, 21)); 891 | lineDataSet.setLineWidth(2.5f);//设置线的宽度 892 | lineDataSet.setCircleColor(Color.rgb(244, 219, 100));//设置圆圈的颜色 893 | lineDataSet.setCircleColorHole(Color.WHITE);//设置圆圈内部洞的颜色 894 | //lineDataSet.setValueTextColor(Color.rgb(254,116,139)); 895 | lineDataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);//设置线数据依赖于右侧y轴 896 | lineDataSet.setDrawValues(false);//不绘制线的数据 897 | 898 | LineData lineData = new LineData(lineDataSet); 899 | lineData.setValueTextSize(10f); 900 | lineData.setValueFormatter(new IValueFormatter() { 901 | @Override 902 | public String getFormattedValue(float value, Entry entry, int i, ViewPortHandler viewPortHandler) { 903 | return StringUtils.double2String(value, 2); 904 | } 905 | }); 906 | 907 | return lineData; 908 | } 909 | 910 | /** 911 | * 生成柱图数据 912 | * 913 | * @param barValues 914 | * @return 915 | */ 916 | private static BarData generateBarData(List barValues, String barTitle) { 917 | 918 | ArrayList barEntries = new ArrayList<>(); 919 | 920 | for (int i = 0, n = barValues.size(); i < n; ++i) { 921 | barEntries.add(new BarEntry(i, barValues.get(i))); 922 | } 923 | 924 | BarDataSet barDataSet = new BarDataSet(barEntries, barTitle); 925 | barDataSet.setColor(Color.rgb(159, 143, 186)); 926 | barDataSet.setValueTextColor(Color.rgb(159, 143, 186)); 927 | barDataSet.setAxisDependency(YAxis.AxisDependency.LEFT); 928 | 929 | BarData barData = new BarData(barDataSet); 930 | barData.setValueTextSize(10f); 931 | barData.setBarWidth(0.9f); 932 | barData.setValueFormatter(new IValueFormatter() { 933 | @Override 934 | public String getFormattedValue(float value, Entry entry, int i, ViewPortHandler viewPortHandler) { 935 | return StringUtils.double2String(value, 2); 936 | } 937 | }); 938 | 939 | return barData; 940 | } 941 | } 942 | -------------------------------------------------------------------------------- /app/src/main/java/com/charlie/mpandroidcharttest/util/StringUtils.java: -------------------------------------------------------------------------------- 1 | package com.charlie.mpandroidcharttest.util; 2 | 3 | import java.text.NumberFormat; 4 | 5 | /** 6 | * Created by Charlie on 2016/10/8. 7 | * 通用字符串管理类 8 | */ 9 | public class StringUtils { 10 | 11 | /** 12 | * 将double转为数值,并最多保留num位小数。例如当num为2时,1.268为1.27,1.2仍为1.2;1仍为1,而非1.00;100.00则返回100。 13 | * 14 | * @param d 15 | * @param num 小数位数 16 | * @return 17 | */ 18 | public static String double2String(double d, int num) { 19 | NumberFormat nf = NumberFormat.getNumberInstance(); 20 | nf.setMaximumFractionDigits(num);//保留两位小数 21 | nf.setGroupingUsed(false);//去掉数值中的千位分隔符 22 | 23 | String temp = nf.format(d); 24 | if (temp.contains(".")) { 25 | String s1 = temp.split("\\.")[0]; 26 | String s2 = temp.split("\\.")[1]; 27 | for (int i = s2.length(); i > 0; --i) { 28 | if (!s2.substring(i - 1, i).equals("0")) { 29 | return s1 + "." + s2.substring(0, i); 30 | } 31 | } 32 | return s1; 33 | } 34 | return temp; 35 | } 36 | 37 | /** 38 | * 将double转为数值,并最多保留num位小数。 39 | * 40 | * @param d 41 | * @param num 小数个数 42 | * @param defValue 默认值。当d为null时,返回该值。 43 | * @return 44 | */ 45 | public static String double2String(Double d, int num, String defValue){ 46 | if(d==null){ 47 | return defValue; 48 | } 49 | 50 | return double2String(d,num); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_bar_chart.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 12 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_combine_chart.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 12 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_line_chart.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 12 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 13 | 14 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_pie_chart.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 12 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/layout/custom_marker_view.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 22 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WJKCharlie/MPAndroidChartTest/f407c9878af558acbe64a4baa0b447da737ccb9a/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WJKCharlie/MPAndroidChartTest/f407c9878af558acbe64a4baa0b447da737ccb9a/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WJKCharlie/MPAndroidChartTest/f407c9878af558acbe64a4baa0b447da737ccb9a/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WJKCharlie/MPAndroidChartTest/f407c9878af558acbe64a4baa0b447da737ccb9a/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WJKCharlie/MPAndroidChartTest/f407c9878af558acbe64a4baa0b447da737ccb9a/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | #6CB0DF 8 | #B5C2CA 9 | #F5E6BF 10 | #81D8C8 11 | 12 | #686868 13 | 14 | #8CD276 15 | #DEEFE4 16 | #E9C517 17 | #ECEAD0 18 | #9F8FBA 19 | #EBE4F8 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | MPAndroidChartTest 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/charlie/mpandroidcharttest/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.charlie.mpandroidcharttest; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:2.2.2' 9 | 10 | // NOTE: Do not place your application dependencies here; they belong 11 | // in the individual module build.gradle files 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | jcenter() 18 | maven { url "https://jitpack.io" } 19 | } 20 | } 21 | 22 | task clean(type: Delete) { 23 | delete rootProject.buildDir 24 | } 25 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WJKCharlie/MPAndroidChartTest/f407c9878af558acbe64a4baa0b447da737ccb9a/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 28 10:00:20 PST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /screenshots/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WJKCharlie/MPAndroidChartTest/f407c9878af558acbe64a4baa0b447da737ccb9a/screenshots/1.png -------------------------------------------------------------------------------- /screenshots/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WJKCharlie/MPAndroidChartTest/f407c9878af558acbe64a4baa0b447da737ccb9a/screenshots/2.png -------------------------------------------------------------------------------- /screenshots/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WJKCharlie/MPAndroidChartTest/f407c9878af558acbe64a4baa0b447da737ccb9a/screenshots/3.png -------------------------------------------------------------------------------- /screenshots/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WJKCharlie/MPAndroidChartTest/f407c9878af558acbe64a4baa0b447da737ccb9a/screenshots/4.png -------------------------------------------------------------------------------- /screenshots/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WJKCharlie/MPAndroidChartTest/f407c9878af558acbe64a4baa0b447da737ccb9a/screenshots/5.png -------------------------------------------------------------------------------- /screenshots/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WJKCharlie/MPAndroidChartTest/f407c9878af558acbe64a4baa0b447da737ccb9a/screenshots/6.png -------------------------------------------------------------------------------- /screenshots/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WJKCharlie/MPAndroidChartTest/f407c9878af558acbe64a4baa0b447da737ccb9a/screenshots/7.png -------------------------------------------------------------------------------- /screenshots/8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WJKCharlie/MPAndroidChartTest/f407c9878af558acbe64a4baa0b447da737ccb9a/screenshots/8.png -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | --------------------------------------------------------------------------------