├── .gitignore ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── github │ │ │ └── hotstu │ │ │ └── chartbindingdemo │ │ │ ├── BindingViewHolder.java │ │ │ ├── ChartMapping.java │ │ │ ├── ChartViewModel.java │ │ │ ├── MainActivity.java │ │ │ └── TypedRecyclerAdapter.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ ├── activity_main.xml │ │ └── item_chart.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── github │ └── hotstu │ └── chartbindingdemo │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── lib ├── .gitignore ├── build.gradle ├── gradle-mavenizer.gradle ├── proguard-rules.pro ├── project.properties └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── github │ │ └── hotstu │ │ └── chartbinding │ │ ├── AxisLineOptions.java │ │ ├── BaseChartView.java │ │ ├── BaseLineOptions.java │ │ ├── BindingAdpater.java │ │ ├── CustomOptions.java │ │ ├── FontOptions.java │ │ ├── GlobalOptions.java │ │ ├── LegendOptions.java │ │ ├── LegendPosition.java │ │ ├── SeriesDescription.java │ │ ├── XAxisOptions.java │ │ └── YAxisOptions.java │ └── res │ ├── layout │ ├── binding_bar_chart.xml │ ├── binding_line_chart.xml │ ├── binding_none_chart.xml │ └── binding_pie_chart.xml │ └── values │ ├── attrs.xml │ ├── colors.xml │ ├── strings.xml │ └── styles.xml ├── preview └── 1.gif ├── project.properties └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![author](https://img.shields.io/badge/author-hglf-blue.svg)](https://github.com/hotstu) 2 | [![Download](https://api.bintray.com/packages/hglf/maven/hof/images/download.svg) ](https://bintray.com/hglf/maven/chartbinding/_latestVersion) 3 | [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) 4 | [![](https://jitpack.io/v/hotstu/chartbinding.svg)](https://jitpack.io/#hotstu/chartbinding) 5 | 6 | chartbinding 7 | ============= 8 | 使用DataBing对MPAndroidChart进行封装, 使用MVVM方式开发图表(饼状图、折线图、饼状图) 9 | MPAndroidChart默认的样式太难看,设置起来也太复杂,这里提供了一套默认的样式(配色来自Echart),并且可以通过style指定配色方案, 10 | 使用的时候只用关注数据、绑定数据大大提升了开发体验 11 | 12 | Android DataBinding wrapper for MPAndroidChart, using chart in a MVVM way! 13 | the goal of this lib is to making a more simple way to make chart for light charting usage, 14 | avoiding the complex property settings. 15 | 16 | 17 | 18 | ## How to use 19 | 20 | just Add the dependency 21 | ``` 22 | dependencies { 23 | implementation 'com.github.hotstu:chartbinding:1.0.0' 24 | } 25 | 26 | ``` 27 | then checkout the demo project for more 28 | 29 |
30 | 31 | ## limits 32 | 33 | only limited type of charts are supported(bar/line/pie) 34 | 35 | ### more 36 | 37 | |Github|简书| 掘金|JCenter | dockerHub| 38 | | -------------| ------------- |------------- |------------- |------------- | 39 | | [Github](https://github.com/hotstu)| [简书](https://www.jianshu.com/u/ca2207af2001) | [掘金](https://juejin.im/user/5bee320651882516be2ebbfe) |[JCenter ](https://bintray.com/hglf/maven) | [dockerHub](https://hub.docker.com/u/hglf)| 40 | # License 41 | 42 | Copyright 2018 hglf 43 | 44 | Licensed under the Apache License, Version 2.0 (the "License"); 45 | you may not use this file except in compliance with the License. 46 | You may obtain a copy of the License at 47 | 48 | http://www.apache.org/licenses/LICENSE-2.0 49 | 50 | Unless required by applicable law or agreed to in writing, software 51 | distributed under the License is distributed on an "AS IS" BASIS, 52 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 53 | See the License for the specific language governing permissions and 54 | limitations under the License. 55 | 56 |
57 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 28 5 | defaultConfig { 6 | applicationId "github.hotstu.chartbinding" 7 | minSdkVersion 19 8 | targetSdkVersion 28 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | dataBinding { 14 | enabled = true 15 | } 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | compileOptions { 23 | sourceCompatibility = '1.8' 24 | targetCompatibility = '1.8' 25 | } 26 | } 27 | 28 | dependencies { 29 | implementation fileTree(include: ['*.jar'], dir: 'libs') 30 | implementation 'androidx.appcompat:appcompat:1.0.2' 31 | implementation "androidx.recyclerview:recyclerview:1.0.0" 32 | implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0' 33 | implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3' 34 | implementation "com.google.code.gson:gson:${GSON_VERSION}" 35 | implementation 'com.jakewharton.timber:timber:4.7.1' 36 | testImplementation 'junit:junit:4.12' 37 | implementation project(':lib') 38 | } 39 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/main/java/github/hotstu/chartbindingdemo/BindingViewHolder.java: -------------------------------------------------------------------------------- 1 | package github.hotstu.chartbindingdemo; 2 | 3 | import androidx.databinding.ViewDataBinding; 4 | import androidx.recyclerview.widget.RecyclerView; 5 | 6 | 7 | public class BindingViewHolder extends RecyclerView.ViewHolder { 8 | private T binding; 9 | public BindingViewHolder(T binding) { 10 | super(binding.getRoot()); 11 | this.binding = binding; 12 | } 13 | 14 | 15 | public T getBinding() { 16 | return binding; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /app/src/main/java/github/hotstu/chartbindingdemo/ChartMapping.java: -------------------------------------------------------------------------------- 1 | package github.hotstu.chartbindingdemo; 2 | 3 | import com.google.gson.JsonDeserializationContext; 4 | import com.google.gson.JsonDeserializer; 5 | import com.google.gson.JsonElement; 6 | import com.google.gson.JsonObject; 7 | import com.google.gson.JsonParseException; 8 | 9 | import java.lang.reflect.Type; 10 | 11 | import timber.log.Timber; 12 | 13 | /** 14 | * @author hglf 15 | * @since 2018/10/25 16 | */ 17 | public class ChartMapping { 18 | public static class PieData { 19 | public String[] legend; 20 | public float[] value; 21 | } 22 | public static class BarData { 23 | public String[] legend; 24 | public float[] value; 25 | } 26 | public static class LineData { 27 | public String[] legend; 28 | public float[] value; 29 | } 30 | 31 | public String label; 32 | public String type; 33 | public T data; 34 | 35 | public static class Deserializer implements JsonDeserializer { 36 | @Override 37 | public ChartMapping deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { 38 | JsonObject jo = json.getAsJsonObject(); 39 | String type = jo.get("type").getAsString(); 40 | String label = jo.get("label").getAsString(); 41 | ChartMapping mapping = new ChartMapping(); 42 | mapping.label = label; 43 | mapping.type = type; 44 | if ("pie".equals(type)) { 45 | mapping.data = context.deserialize(jo.get("series"), PieData.class); 46 | } else if("bar".equals(type)) { 47 | mapping.data = context.deserialize(jo.get("series"), BarData.class); 48 | } else if("line".equals(type)){ 49 | mapping.data = context.deserialize(jo.get("series"), LineData.class); 50 | } 51 | else { 52 | Timber.w("不支持的类型:" + type); 53 | } 54 | return mapping; 55 | } 56 | } 57 | 58 | 59 | 60 | } 61 | -------------------------------------------------------------------------------- /app/src/main/java/github/hotstu/chartbindingdemo/ChartViewModel.java: -------------------------------------------------------------------------------- 1 | package github.hotstu.chartbindingdemo; 2 | 3 | import android.app.Application; 4 | import android.content.res.TypedArray; 5 | import android.graphics.Color; 6 | import android.os.SystemClock; 7 | 8 | import com.github.mikephil.charting.charts.PieChart; 9 | import com.github.mikephil.charting.data.BarDataSet; 10 | import com.github.mikephil.charting.data.BarEntry; 11 | import com.github.mikephil.charting.data.Entry; 12 | import com.github.mikephil.charting.data.LineDataSet; 13 | import com.github.mikephil.charting.data.PieDataSet; 14 | import com.github.mikephil.charting.data.PieEntry; 15 | import com.github.mikephil.charting.formatter.IAxisValueFormatter; 16 | import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; 17 | import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; 18 | import com.github.mikephil.charting.interfaces.datasets.IPieDataSet; 19 | 20 | import java.util.ArrayList; 21 | import java.util.List; 22 | import java.util.Random; 23 | 24 | import androidx.lifecycle.AndroidViewModel; 25 | import androidx.lifecycle.LiveData; 26 | import androidx.lifecycle.MediatorLiveData; 27 | import androidx.lifecycle.MutableLiveData; 28 | import androidx.lifecycle.Transformations; 29 | import github.hotstu.chartbinding.FontOptions; 30 | import github.hotstu.chartbinding.GlobalOptions; 31 | import github.hotstu.chartbinding.LegendOptions; 32 | import github.hotstu.chartbinding.SeriesDescription; 33 | import github.hotstu.chartbinding.XAxisOptions; 34 | import github.hotstu.chartbinding.YAxisOptions; 35 | 36 | /** 37 | * @author hglf 38 | * @since 2018/10/24 39 | */ 40 | public class ChartViewModel extends AndroidViewModel { 41 | private final MutableLiveData mQuery; 42 | private final int[] colors; 43 | private final XAxisOptions xAxisOptions; 44 | private final YAxisOptions disabled; 45 | private final GlobalOptions globalOptions; 46 | private final YAxisOptions leftAxisOptions; 47 | private final LegendOptions legendOptions; 48 | public final LiveData> lists; 49 | private final MediatorLiveData> raw; 50 | 51 | public ChartViewModel(Application application) { 52 | super(application); 53 | mQuery = new MutableLiveData<>(); 54 | 55 | raw = new MediatorLiveData<>(); 56 | 57 | TypedArray a = application.getTheme().obtainStyledAttributes(R.style.defaultChartBindingStyle, R.styleable.ChartBinding); 58 | 59 | colors = new int[]{ 60 | a.getColor(R.styleable.ChartBinding_chart_item_a, Color.BLACK), 61 | a.getColor(R.styleable.ChartBinding_chart_item_b, Color.BLACK), 62 | a.getColor(R.styleable.ChartBinding_chart_item_c, Color.BLACK), 63 | a.getColor(R.styleable.ChartBinding_chart_item_d, Color.BLACK), 64 | a.getColor(R.styleable.ChartBinding_chart_item_e, Color.BLACK), 65 | a.getColor(R.styleable.ChartBinding_chart_item_f, Color.BLACK), 66 | a.getColor(R.styleable.ChartBinding_chart_item_g, Color.BLACK), 67 | a.getColor(R.styleable.ChartBinding_chart_item_h, Color.BLACK), 68 | a.getColor(R.styleable.ChartBinding_chart_item_i, Color.BLACK), 69 | a.getColor(R.styleable.ChartBinding_chart_item_j, Color.BLACK) 70 | }; 71 | 72 | int colorText = a.getColor(R.styleable.ChartBinding_chart_text, Color.BLACK); 73 | int colorSubText = a.getColor(R.styleable.ChartBinding_chart_subtext, Color.BLACK); 74 | int colorBackGround = a.getColor(R.styleable.ChartBinding_chart_background, Color.WHITE); 75 | a.recycle(); 76 | 77 | 78 | FontOptions globalTextStyle = new FontOptions(); 79 | globalTextStyle.color = colorText; 80 | xAxisOptions = new XAxisOptions(); 81 | xAxisOptions.font = globalTextStyle; 82 | disabled = new YAxisOptions(); 83 | disabled.enabled = false; 84 | globalOptions = new GlobalOptions(); 85 | leftAxisOptions = new YAxisOptions(); 86 | legendOptions = new LegendOptions(); 87 | 88 | lists = Transformations.map(raw, input -> { 89 | List ret = new ArrayList<>(); 90 | for (int i = 0; i < input.size(); i++) { 91 | ChartMapping mapping = input.get(i); 92 | if (mapping.data instanceof ChartMapping.PieData) { 93 | ret.add(buildPieData(mapping.label, (ChartMapping.PieData) mapping.data)); 94 | } 95 | if (mapping.data instanceof ChartMapping.BarData) { 96 | ret.add(buildBarData(mapping.label, (ChartMapping.BarData) mapping.data)); 97 | } 98 | if (mapping.data instanceof ChartMapping.LineData) { 99 | ret.add(buildLineData(mapping.label, (ChartMapping.LineData) mapping.data)); 100 | } 101 | //TODO other type 102 | } 103 | return ret; 104 | }); 105 | initData(); 106 | } 107 | 108 | 109 | private void initData() { 110 | Random r = new Random(SystemClock.elapsedRealtimeNanos()); 111 | List ret = new ArrayList<>(); 112 | for (int i = 0; i < 10; i++) { 113 | ChartMapping c = new ChartMapping(); 114 | int typeInt = r.nextInt(3); 115 | String type = "line"; 116 | switch (typeInt) { 117 | case 0: 118 | type = "pie"; 119 | c.data = new ChartMapping.PieData(); 120 | ((ChartMapping.PieData) c.data).legend = new String[10]; 121 | ((ChartMapping.PieData) c.data).value = new float[10]; 122 | for (int j = 0; j < 10; j++) { 123 | int value = r.nextInt(100); 124 | ((ChartMapping.PieData) c.data).legend[j] = j+ ""; 125 | ((ChartMapping.PieData) c.data).value[j] = value; 126 | } 127 | break; 128 | case 1: 129 | type = "bar"; 130 | c.data = new ChartMapping.BarData(); 131 | ((ChartMapping.BarData) c.data).legend = new String[10]; 132 | ((ChartMapping.BarData) c.data).value = new float[10]; 133 | for (int j = 0; j < 10; j++) { 134 | int value = r.nextInt(100); 135 | ((ChartMapping.BarData) c.data).legend[j] = j+ ""; 136 | ((ChartMapping.BarData) c.data).value[j] = value; 137 | } 138 | break; 139 | case 2: 140 | type = "line"; 141 | c.data = new ChartMapping.LineData(); 142 | ((ChartMapping.LineData) c.data).legend = new String[10]; 143 | ((ChartMapping.LineData) c.data).value = new float[10]; 144 | for (int j = 0; j < 10; j++) { 145 | int value = r.nextInt(100); 146 | ((ChartMapping.LineData) c.data).legend[j] = j+ ""; 147 | ((ChartMapping.LineData) c.data).value[j] = value; 148 | } 149 | break; 150 | } 151 | String label = type + "" + i; 152 | c.type = type; 153 | c.label = label; 154 | ret.add(c); 155 | } 156 | 157 | raw.postValue(ret); 158 | } 159 | 160 | 161 | private SeriesDescription buildPieData(String label, ChartMapping.PieData data) { 162 | List entries = new ArrayList<>(); 163 | for (int i = 0; i < data.legend.length && i < data.value.length; i++) { 164 | entries.add(new PieEntry(data.value[i], data.legend[i] + "")); 165 | } 166 | PieDataSet dataSet = new PieDataSet(entries, label); 167 | stylePieDataSet(dataSet); 168 | SeriesDescription ds = new SeriesDescription<>(SeriesDescription.TYPE_PIE, dataSet); 169 | ds.globalOptions = globalOptions; 170 | ds.leftAxisOptions = leftAxisOptions; 171 | ds.xAxisOptions = xAxisOptions; 172 | ds.rightAxisOptions = disabled; 173 | LegendOptions legendOptions = this.legendOptions.copy(); 174 | legendOptions.enabled = false; 175 | ds.legendOptions = legendOptions; 176 | ds.customOptions = chart -> { 177 | if (chart instanceof PieChart) { 178 | PieChart mChart = (PieChart) chart; 179 | mChart.setEntryLabelColor(Color.BLACK); 180 | mChart.setDrawCenterText(true); 181 | mChart.setExtraOffsets(20.f, 0.f, 20.f, 0.f); 182 | mChart.setCenterText(label); 183 | } 184 | }; 185 | return ds; 186 | } 187 | 188 | private SeriesDescription buildBarData(String label, ChartMapping.BarData data) { 189 | List entries = new ArrayList<>(); 190 | for (int i = 0; i < data.legend.length && i < data.value.length; i++) { 191 | entries.add(new BarEntry(i, data.value[i])); 192 | } 193 | IAxisValueFormatter formatter = (value, axis) -> data.legend[((int) value)] + ""; 194 | 195 | BarDataSet dataSet = new BarDataSet(entries, label); 196 | styleBarDataSet(dataSet); 197 | SeriesDescription ds = new SeriesDescription<>(SeriesDescription.TYPE_BAR, dataSet); 198 | ds.globalOptions = globalOptions; 199 | ds.leftAxisOptions = leftAxisOptions; 200 | XAxisOptions copy = xAxisOptions.copy(); 201 | copy.valueFormatter = formatter; 202 | ds.xAxisOptions = copy; 203 | ds.rightAxisOptions = disabled; 204 | LegendOptions copy1 = legendOptions.copy(); 205 | copy1.position.drawInside = true; 206 | ds.legendOptions = copy1; 207 | return ds; 208 | } 209 | 210 | private SeriesDescription buildLineData(String label, ChartMapping.LineData data) { 211 | List entries = new ArrayList<>(); 212 | for (int i = 0; i < data.legend.length && i < data.value.length; i++) { 213 | entries.add(new Entry(i, data.value[i])); 214 | } 215 | IAxisValueFormatter formatter = (value, axis) -> data.legend[((int) value)] + ""; 216 | LineDataSet dataSet = new LineDataSet(entries, label); 217 | styleLineDataSet(dataSet); 218 | SeriesDescription ds = new SeriesDescription<>(SeriesDescription.TYPE_LINE, dataSet); 219 | ds.globalOptions = globalOptions; 220 | ds.leftAxisOptions = leftAxisOptions; 221 | XAxisOptions copy = xAxisOptions.copy(); 222 | copy.valueFormatter = formatter; 223 | ds.xAxisOptions = copy; 224 | ds.rightAxisOptions = disabled; 225 | LegendOptions copy1 = legendOptions.copy(); 226 | copy1.position.drawInside = true; 227 | ds.legendOptions = copy1; 228 | return ds; 229 | } 230 | 231 | private void styleLineDataSet(LineDataSet dataSet) { 232 | int color = colors[0]; 233 | dataSet.setColors(colors); 234 | dataSet.setValueTextSize(12); 235 | dataSet.setCircleRadius(5); 236 | dataSet.setCircleHoleRadius(4); 237 | dataSet.setCircleColor(color); 238 | dataSet.disableDashedLine(); 239 | dataSet.disableDashedHighlightLine(); 240 | dataSet.setColor(color); 241 | dataSet.setDrawValues(false); 242 | dataSet.setLineWidth(2); 243 | dataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER); 244 | } 245 | 246 | private void styleBarDataSet(BarDataSet dataSet) { 247 | dataSet.setColors(colors); 248 | dataSet.setValueTextSize(12); 249 | dataSet.setColor(colors[0]); 250 | dataSet.setDrawValues(false); 251 | } 252 | 253 | private void stylePieDataSet(PieDataSet dataSet) { 254 | dataSet.setColors(colors); 255 | dataSet.setSelectionShift(15); 256 | dataSet.setDrawValues(true); 257 | dataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE); 258 | dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE); 259 | dataSet.setValueTextSize(12f); 260 | dataSet.setValueTextColor(Color.BLACK); 261 | dataSet.setValueLineColor(Color.BLACK); 262 | } 263 | } 264 | -------------------------------------------------------------------------------- /app/src/main/java/github/hotstu/chartbindingdemo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package github.hotstu.chartbindingdemo; 2 | 3 | import android.os.Bundle; 4 | 5 | import com.github.mikephil.charting.utils.Utils; 6 | 7 | import androidx.annotation.NonNull; 8 | import androidx.appcompat.app.AppCompatActivity; 9 | import androidx.databinding.DataBindingUtil; 10 | import androidx.lifecycle.ViewModel; 11 | import androidx.lifecycle.ViewModelProvider; 12 | import androidx.lifecycle.ViewModelProviders; 13 | import androidx.recyclerview.widget.LinearLayoutManager; 14 | import github.hotstu.chartbindingdemo.databinding.ActivityMainBinding; 15 | 16 | public class MainActivity extends AppCompatActivity { 17 | 18 | @Override 19 | protected void onCreate(Bundle savedInstanceState) { 20 | super.onCreate(savedInstanceState); 21 | Utils.init(this);//MPAndroidChart ; 22 | ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); 23 | binding.recyclerView.setLayoutManager(new LinearLayoutManager(this)); 24 | TypedRecyclerAdapter adapter = new TypedRecyclerAdapter(); 25 | binding.recyclerView.setAdapter(adapter); 26 | ChartViewModel chartViewModel = ViewModelProviders.of(this, new ViewModelProvider.Factory() { 27 | @NonNull 28 | @Override 29 | public T create(@NonNull Class modelClass) { 30 | return (T) new ChartViewModel(getApplication()); 31 | } 32 | }).get(ChartViewModel.class); 33 | chartViewModel.lists.observe(this, seriesDescriptions -> { 34 | adapter.setDataSet(seriesDescriptions); 35 | }); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /app/src/main/java/github/hotstu/chartbindingdemo/TypedRecyclerAdapter.java: -------------------------------------------------------------------------------- 1 | package github.hotstu.chartbindingdemo; 2 | 3 | 4 | import android.view.LayoutInflater; 5 | import android.view.ViewGroup; 6 | 7 | import java.util.ArrayList; 8 | import java.util.Collections; 9 | import java.util.List; 10 | 11 | import androidx.databinding.DataBindingUtil; 12 | import androidx.databinding.ViewDataBinding; 13 | import androidx.recyclerview.widget.RecyclerView; 14 | 15 | 16 | public class TypedRecyclerAdapter extends RecyclerView.Adapter { 17 | final List mList; 18 | 19 | public TypedRecyclerAdapter() { 20 | mList = new ArrayList(); 21 | } 22 | 23 | 24 | @Override 25 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 26 | ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_chart, parent, false); 27 | return new BindingViewHolder<>(binding); 28 | } 29 | 30 | @Override 31 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 32 | ((BindingViewHolder) holder).getBinding().setVariable(BR.data, getItem(position)); 33 | ((BindingViewHolder) holder).getBinding().executePendingBindings(); 34 | } 35 | 36 | 37 | @Override 38 | public int getItemCount() { 39 | return mList.size(); 40 | } 41 | 42 | public final void setDataSet(List list) { 43 | mList.clear(); 44 | mList.addAll(list); 45 | notifyDataSetChanged(); 46 | } 47 | 48 | public final void clearDataSet() { 49 | notifyItemRangeRemoved(0, mList.size()); 50 | mList.clear(); 51 | } 52 | 53 | public final Object getItem(int position) { 54 | return mList.get(position); 55 | } 56 | 57 | public final void addItem(Object object) { 58 | mList.add(object); 59 | notifyItemInserted(mList.size() - 1); 60 | } 61 | 62 | public final void addItem(Object object, int position) { 63 | mList.add(position, object); 64 | notifyItemInserted(position); 65 | } 66 | 67 | public final void addItems(List list) { 68 | int start = mList.size(); 69 | mList.addAll(list); 70 | notifyItemRangeInserted(start, list.size()); 71 | } 72 | 73 | public final void removeItem(int position) { 74 | if (position == RecyclerView.NO_POSITION) { 75 | return; 76 | } 77 | mList.remove(position); 78 | notifyItemRemoved(position); 79 | } 80 | 81 | public final int findItem(Object item) { 82 | int position = RecyclerView.NO_POSITION; 83 | for (int i = 0; i < mList.size(); i++) { 84 | if (item.equals(mList.get(i))) { 85 | position = i; 86 | break; 87 | } 88 | } 89 | return position; 90 | } 91 | 92 | public final void removeItem(Object item) { 93 | int position = findItem(item); 94 | if (position != RecyclerView.NO_POSITION) { 95 | mList.remove(position); 96 | notifyItemRemoved(position); 97 | } 98 | } 99 | 100 | 101 | public final void moveItem(int fromPostion, int toPositon) { 102 | //Log.d(TAG, "moveItem() called with: " + "fromPostion = [" + fromPostion + "], toPositon = [" + toPositon + "]"); 103 | Collections.swap(mList, fromPostion, toPositon); 104 | notifyItemMoved(fromPostion, toPositon); 105 | } 106 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 10 | 11 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /app/src/main/res/layout/item_chart.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 11 | 12 | 13 | 14 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hotstu/chartbinding/2d722daabe0ee479c6c1ba465e211ffda94cd63f/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hotstu/chartbinding/2d722daabe0ee479c6c1ba465e211ffda94cd63f/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hotstu/chartbinding/2d722daabe0ee479c6c1ba465e211ffda94cd63f/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hotstu/chartbinding/2d722daabe0ee479c6c1ba465e211ffda94cd63f/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hotstu/chartbinding/2d722daabe0ee479c6c1ba465e211ffda94cd63f/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hotstu/chartbinding/2d722daabe0ee479c6c1ba465e211ffda94cd63f/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hotstu/chartbinding/2d722daabe0ee479c6c1ba465e211ffda94cd63f/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hotstu/chartbinding/2d722daabe0ee479c6c1ba465e211ffda94cd63f/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hotstu/chartbinding/2d722daabe0ee479c6c1ba465e211ffda94cd63f/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hotstu/chartbinding/2d722daabe0ee479c6c1ba465e211ffda94cd63f/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #008577 4 | #00574B 5 | #D81B60 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | chartbinding 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/github/hotstu/chartbindingdemo/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package github.hotstu.chartbindingdemo; 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() { 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 | google() 6 | mavenCentral() 7 | 8 | } 9 | dependencies { 10 | classpath 'com.android.tools.build:gradle:4.0.1' 11 | 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | google() 18 | mavenCentral() 19 | maven { url 'https://jitpack.io' } 20 | } 21 | } 22 | 23 | subprojects { 24 | configurations.all { 25 | resolutionStrategy { 26 | force "com.android.support:recyclerview-v7:$SUPPORT_VERSION" 27 | force "com.android.support:appcompat-v7:$SUPPORT_VERSION" 28 | force "com.android.support:design:$SUPPORT_VERSION" 29 | force "com.android.support:support-vector-drawable:$SUPPORT_VERSION" 30 | force "com.android.support:animated-vector-drawable:$SUPPORT_VERSION" 31 | } 32 | } 33 | if ("lib" == it.name) { 34 | group = 'com.github.PhilJay.chartbinding'//发布到 bintray 的groupId号 35 | version = "1.0.0" // 发布到 bintray 的版本号 36 | } 37 | } 38 | 39 | task clean(type: Delete) { 40 | delete rootProject.buildDir 41 | } 42 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | SUPPORT_VERSION=28.0.0 15 | SDK_VERSION=27 16 | GSON_VERSION=2.8.0 17 | RXJAVA_VERSION=2.1.8 18 | RXANDROID_VERSION=2.0.1 19 | android.useAndroidX=true 20 | android.enableJetifier=true 21 | android.injected.testOnly=false 22 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hotstu/chartbinding/2d722daabe0ee479c6c1ba465e211ffda94cd63f/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://mirrors.cloud.tencent.com/gradle/gradle-6.1.1-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /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 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 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 Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /lib/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /lib/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 28 5 | compileOptions { 6 | sourceCompatibility "1.8" 7 | targetCompatibility "1.8" 8 | } 9 | defaultConfig { 10 | minSdkVersion 19 11 | targetSdkVersion 28 12 | versionCode 1 13 | versionName "1.0" 14 | } 15 | 16 | dataBinding { 17 | enabled = true 18 | } 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | 26 | } 27 | 28 | dependencies { 29 | implementation fileTree(dir: 'libs', include: ['*.jar']) 30 | 31 | implementation 'androidx.appcompat:appcompat:1.0.2' 32 | implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3' 33 | testImplementation 'junit:junit:4.12' 34 | } 35 | 36 | 37 | apply from: "./gradle-mavenizer.gradle" 38 | -------------------------------------------------------------------------------- /lib/gradle-mavenizer.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'maven-publish' 2 | 3 | //utils 4 | def isAndroidProject() { 5 | def plugins = project.getPlugins() 6 | return plugins.hasPlugin('com.android.application') || plugins.hasPlugin('com.android.library') 7 | } 8 | 9 | def getStringProperty(String propertyName) { 10 | return project.hasProperty(propertyName) ? project.getProperty(propertyName) : "" 11 | } 12 | 13 | def getBooleanProperty(String propertyName) { 14 | return project.hasProperty(propertyName) ? project.getProperty(propertyName) : false 15 | } 16 | 17 | def getArrayProperty(String propertyName) { 18 | return project.hasProperty(propertyName) ? project.getProperty(propertyName) : [] 19 | } 20 | 21 | //pom manager 22 | def decoratePom(pom) { 23 | pom.name = getStringProperty("mavProjectName") 24 | pom.description = getStringProperty("mavLibraryDescription") 25 | pom.url = getStringProperty("mavSiteUrl") 26 | 27 | pom.licenses { 28 | getArrayProperty("mavLibraryLicenses").each { licenseName, licenseUrl -> 29 | license { 30 | name = licenseName 31 | url = licenseUrl 32 | } 33 | } 34 | } 35 | pom.developers { 36 | getArrayProperty("mavDevelopers").each { developerId, developerName -> 37 | developer { 38 | id = developerId 39 | name = developerName 40 | } 41 | } 42 | } 43 | pom.scm { 44 | connection = getStringProperty("mavGitUrl") 45 | developerConnection = getStringProperty("mavGitUrl") 46 | url = getStringProperty("mavSiteUrl") 47 | } 48 | 49 | addDependencies(pom) 50 | } 51 | 52 | def addDependencies(pom) { 53 | pom.withXml { 54 | final dependenciesNode = asNode().appendNode('dependencies') 55 | ext.addDependency = { Dependency dep, String scope -> 56 | if (dep.group == null || dep.version == null || dep.name == null || dep.name == "unspecified") 57 | return // ignore invalid dependencies 58 | 59 | if (dep.artifacts.size() > 0) { 60 | dep.artifacts.each { art -> 61 | addDependencyNode(dependenciesNode, dep, scope, art.classifier, art.extension) 62 | } 63 | } else { 64 | addDependencyNode(dependenciesNode, dep, scope, null, null) 65 | } 66 | } 67 | manageConfigurations(configurations) 68 | } 69 | } 70 | 71 | def addDependencyNode(dependenciesNode, dep, scope, classifier, extension) { 72 | final dependencyNode = dependenciesNode.appendNode('dependency') 73 | dependencyNode.appendNode('groupId', dep.group) 74 | dependencyNode.appendNode('artifactId', dep.name) 75 | dependencyNode.appendNode('version', dep.version) 76 | 77 | if (classifier != null) { 78 | dependencyNode.appendNode('classifier', classifier) 79 | } 80 | 81 | if (extension != null) { 82 | dependencyNode.appendNode('type', extension) 83 | } 84 | 85 | dependencyNode.appendNode('scope', scope) 86 | 87 | if (!dep.transitive) { 88 | // If this dependency is transitive, we should force exclude all its dependencies them from the POM 89 | final exclusionNode = dependencyNode.appendNode('exclusions').appendNode('exclusion') 90 | exclusionNode.appendNode('artifactId', '*') 91 | exclusionNode.appendNode('groupId', '*') 92 | } else if (!dep.properties.excludeRules.empty) { 93 | // Otherwise add specified exclude rules 94 | final exclusionsNode = dependencyNode.appendNode('exclusions') 95 | dep.properties.excludeRules.each { ExcludeRule rule -> 96 | final exclusionNode = exclusionsNode.appendNode('exclusion') 97 | exclusionNode.appendNode('artifactId', rule.module ?: '*') 98 | exclusionNode.appendNode('groupId', rule.group ?: '*') 99 | } 100 | } 101 | } 102 | 103 | def manageConfigurations(configurations) { 104 | configurations.compile.getDependencies().each { dep -> addDependency(dep, "compile") } 105 | configurations.api.getDependencies().each { dep -> addDependency(dep, "compile") } 106 | configurations.implementation.getDependencies().each { dep -> addDependency(dep, "runtime") } 107 | configurations.testImplementation.getDependencies().each { dep -> addDependency(dep, "test") } 108 | configurations.testCompile.getDependencies().each { dep -> addDependency(dep, "test") } 109 | 110 | if (!isAndroidProject()) { 111 | configurations.runtime.getDependencies().each { dep -> addDependency(dep, "runtime") } 112 | configurations.testRuntime.getDependencies().each { dep -> addDependency(dep, "test") } 113 | } 114 | } 115 | 116 | //javadoc handler 117 | if (isAndroidProject()) { 118 | task androidJavadocs(type: Javadoc) { 119 | title = "${getStringProperty("mavProjectName")} ${project.version} API" 120 | description "Generates Javadoc" 121 | source = android.sourceSets.main.java.srcDirs 122 | classpath += files(android.bootClasspath) 123 | 124 | android.libraryVariants.all { variant -> 125 | if (variant.name == 'release') { 126 | owner.classpath += variant.javaCompileProvider.get().classpath 127 | } 128 | } 129 | 130 | exclude '**/R.html', '**/R.*.html', '**/index.html', '**/*.kt' 131 | 132 | options { 133 | windowTitle("${getStringProperty("mavProjectName")} ${project.version} Reference") 134 | locale = 'en_US' 135 | encoding = 'UTF-8' 136 | charSet = 'UTF-8' 137 | setMemberLevel(JavadocMemberLevel.PUBLIC) 138 | } 139 | } 140 | 141 | task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) { 142 | classifier = 'javadoc' 143 | from androidJavadocs.destinationDir 144 | } 145 | 146 | task androidSourcesJar(type: Jar) { 147 | classifier = 'sources' 148 | from android.sourceSets.main.java.srcDirs 149 | } 150 | 151 | if (JavaVersion.current().isJava8Compatible()) { 152 | allprojects { 153 | tasks.withType(Javadoc) { 154 | options.addStringOption('Xdoclint:none', '-quiet') 155 | } 156 | } 157 | } 158 | if (JavaVersion.current().isJava9Compatible()) { 159 | allprojects { 160 | tasks.withType(Javadoc) { 161 | options.addBooleanOption('html5', true) 162 | } 163 | } 164 | } 165 | 166 | 167 | android.libraryVariants.all { variant -> 168 | tasks.androidJavadocs.doFirst { 169 | classpath += files(variant.javaCompileProvider.get().classpath.files.join(File.pathSeparator)) 170 | } 171 | } 172 | } else { 173 | task sourcesJar(type: Jar) { 174 | from sourceSets.main.allJava 175 | archiveClassifier = 'sources' 176 | } 177 | 178 | task javadocJar(type: Jar) { 179 | from javadoc 180 | archiveClassifier = 'javadoc' 181 | } 182 | } 183 | 184 | 185 | //publish 186 | project.afterEvaluate { 187 | publishing { 188 | publications { 189 | mavenPublish(MavenPublication) { 190 | if (isAndroidProject()) { 191 | artifact bundleReleaseAar 192 | artifact androidJavadocsJar 193 | artifact androidSourcesJar 194 | } else { 195 | artifact jar 196 | artifact sourcesJar 197 | artifact javadocJar 198 | } 199 | 200 | decoratePom(pom) 201 | } 202 | } 203 | 204 | repositories { 205 | def isToRemoteRepo = getBooleanProperty("mavPublishToRemoteRepo") 206 | def isToInternalRepo = getBooleanProperty("mavPublishToInternalRepo") 207 | def isToMavenLocal = getBooleanProperty("mavPublishToMavenLocal") 208 | 209 | if (isToRemoteRepo) { 210 | maven { 211 | credentials { 212 | username getStringProperty("mavRemoteRepoUser") 213 | password getStringProperty("mavRemoteRepoPassword") 214 | } 215 | url = getStringProperty("mavRepoRemoteUrl") 216 | } 217 | } 218 | 219 | if (isToInternalRepo) { 220 | maven { 221 | url = getStringProperty("mavRepoInternalUrl") 222 | } 223 | } 224 | 225 | if (isToMavenLocal || (!isToInternalRepo && !isToRemoteRepo)) { 226 | mavenLocal() 227 | } 228 | } 229 | } 230 | } 231 | -------------------------------------------------------------------------------- /lib/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /lib/project.properties: -------------------------------------------------------------------------------- 1 | # suppress inspection "UnusedProperty" for whole file 2 | #project 3 | project.repo=maven 4 | project.name=chartbinding 5 | project.desc=chartbinding 6 | 7 | project.groupId=com.github.PhilJay.chartbinding 8 | project.packaging=aar 9 | project.siteUrl= 10 | project.gitUrl= 11 | 12 | license.name= 13 | license.url= 14 | developer.id= 15 | developer.name= 16 | developer.email= -------------------------------------------------------------------------------- /lib/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /lib/src/main/java/github/hotstu/chartbinding/AxisLineOptions.java: -------------------------------------------------------------------------------- 1 | package github.hotstu.chartbinding; 2 | 3 | import com.github.mikephil.charting.components.AxisBase; 4 | 5 | /** 6 | * @author hglf 7 | * @since 2018/10/25 8 | */ 9 | public class AxisLineOptions extends BaseLineOptions { 10 | public void apply(AxisBase axisBase) { 11 | if (axisBase == null) { 12 | return; 13 | } 14 | axisBase.setAxisLineColor(color); 15 | axisBase.setAxisLineWidth(width); 16 | axisBase.setAxisMinimum(axisMinimum); 17 | if (axisMaximum != -0.01f) { 18 | axisBase.setAxisMaximum(axisMaximum); 19 | } 20 | if (dashPathEffect != null) { 21 | axisBase.setAxisLineDashedLine(dashPathEffect); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /lib/src/main/java/github/hotstu/chartbinding/BaseChartView.java: -------------------------------------------------------------------------------- 1 | package github.hotstu.chartbinding; 2 | 3 | import android.content.Context; 4 | import android.util.AttributeSet; 5 | import android.view.LayoutInflater; 6 | import android.widget.FrameLayout; 7 | 8 | import com.github.mikephil.charting.data.BarData; 9 | import com.github.mikephil.charting.data.ChartData; 10 | import com.github.mikephil.charting.data.LineData; 11 | import com.github.mikephil.charting.data.PieData; 12 | import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; 13 | import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; 14 | import com.github.mikephil.charting.interfaces.datasets.IPieDataSet; 15 | import com.github.mikephil.charting.utils.Utils; 16 | import androidx.annotation.LayoutRes; 17 | import androidx.annotation.NonNull; 18 | import androidx.annotation.Nullable; 19 | import androidx.databinding.DataBindingUtil; 20 | import androidx.databinding.ViewDataBinding; 21 | 22 | /** 23 | * @author hglf 24 | * @since 2018/10/15 25 | */ 26 | public class BaseChartView extends FrameLayout { 27 | 28 | private ViewDataBinding mBinding; 29 | private SeriesDescription mSeriesDescrption; 30 | 31 | public BaseChartView(@NonNull Context context) { 32 | super(context); 33 | init(); 34 | } 35 | 36 | public BaseChartView(@NonNull Context context, @Nullable AttributeSet attrs) { 37 | super(context, attrs); 38 | init(); 39 | } 40 | 41 | public BaseChartView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 42 | super(context, attrs, defStyleAttr); 43 | init(); 44 | } 45 | 46 | private void init() { 47 | Utils.init(getContext()); 48 | } 49 | 50 | private ChartData buildData(SeriesDescription series) { 51 | if (series == null || series.type == SeriesDescription.TYPE_NONE) { 52 | return null; 53 | } else if (series.type == SeriesDescription.TYPE_LINE) { 54 | SeriesDescription realSeries = series; 55 | return new LineData(realSeries.getDataSet()); 56 | } else if (series.type == SeriesDescription.TYPE_BAR) { 57 | SeriesDescription realSeries = series; 58 | return new BarData(realSeries.getDataSet()); 59 | } else if (series.type == SeriesDescription.TYPE_PIE) { 60 | SeriesDescription realSeries = series; 61 | return new PieData(realSeries.getDataSet()[0]); 62 | } else { 63 | return null; 64 | } 65 | } 66 | 67 | private ViewDataBinding BuildChart(SeriesDescription series) { 68 | LayoutInflater inflater = LayoutInflater.from(getContext()); 69 | @LayoutRes int layoutId; 70 | if (series == null || series.type == SeriesDescription.TYPE_NONE) { 71 | layoutId = R.layout.binding_none_chart; 72 | } else if (series.type == SeriesDescription.TYPE_LINE) { 73 | layoutId = R.layout.binding_line_chart; 74 | } else if (series.type == SeriesDescription.TYPE_BAR) { 75 | layoutId = R.layout.binding_bar_chart; 76 | } else if (series.type == SeriesDescription.TYPE_PIE) { 77 | layoutId = R.layout.binding_pie_chart; 78 | } else { 79 | layoutId = R.layout.binding_none_chart; 80 | } 81 | return DataBindingUtil.inflate(inflater, layoutId, this, true); 82 | } 83 | 84 | public void setSeries(SeriesDescription series) { 85 | if (mSeriesDescrption != null && mSeriesDescrption.type == series.type && mBinding != null) { 86 | mSeriesDescrption = series; 87 | mBinding.setVariable(BR.globalOptions, mSeriesDescrption.globalOptions); 88 | mBinding.setVariable(BR.legendOptions, mSeriesDescrption.legendOptions); 89 | mBinding.setVariable(BR.xAxisOptions, mSeriesDescrption.xAxisOptions); 90 | mBinding.setVariable(BR.leftAxisOptions, mSeriesDescrption.leftAxisOptions); 91 | mBinding.setVariable(BR.rightAxisOptions, mSeriesDescrption.rightAxisOptions); 92 | mBinding.setVariable(BR.customOptions, mSeriesDescrption.customOptions); 93 | mBinding.setVariable(BR.animateX, mSeriesDescrption.animateX); 94 | mBinding.setVariable(BR.animateY, mSeriesDescrption.animateY); 95 | mBinding.setVariable(BR.data, buildData(mSeriesDescrption)); 96 | } else { 97 | if (mBinding != null) { 98 | mBinding.unbind(); 99 | } 100 | mSeriesDescrption = series; 101 | removeAllViews(); 102 | if (mSeriesDescrption != null) { 103 | mBinding = BuildChart(mSeriesDescrption); 104 | mBinding.setVariable(BR.globalOptions, mSeriesDescrption.globalOptions); 105 | mBinding.setVariable(BR.legendOptions, mSeriesDescrption.legendOptions); 106 | mBinding.setVariable(BR.xAxisOptions, mSeriesDescrption.xAxisOptions); 107 | mBinding.setVariable(BR.leftAxisOptions, mSeriesDescrption.leftAxisOptions); 108 | mBinding.setVariable(BR.rightAxisOptions, mSeriesDescrption.rightAxisOptions); 109 | mBinding.setVariable(BR.customOptions, mSeriesDescrption.customOptions); 110 | mBinding.setVariable(BR.animateX, mSeriesDescrption.animateX); 111 | mBinding.setVariable(BR.animateY, mSeriesDescrption.animateY); 112 | mBinding.setVariable(BR.data, buildData(mSeriesDescrption)); 113 | } 114 | 115 | } 116 | invalidate(); 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /lib/src/main/java/github/hotstu/chartbinding/BaseLineOptions.java: -------------------------------------------------------------------------------- 1 | package github.hotstu.chartbinding; 2 | 3 | import android.graphics.Color; 4 | import android.graphics.DashPathEffect; 5 | 6 | /** 7 | * @author hglf 8 | * @since 2018/10/25 9 | */ 10 | public abstract class BaseLineOptions { 11 | public float width = 1; 12 | public int color = Color.BLACK; 13 | public float axisMinimum = 0f; 14 | public float axisMaximum = -0.01f; 15 | public DashPathEffect dashPathEffect; 16 | 17 | public abstract void apply(T taget); 18 | } 19 | -------------------------------------------------------------------------------- /lib/src/main/java/github/hotstu/chartbinding/BindingAdpater.java: -------------------------------------------------------------------------------- 1 | package github.hotstu.chartbinding; 2 | 3 | import com.github.mikephil.charting.charts.BarChart; 4 | import com.github.mikephil.charting.charts.Chart; 5 | import com.github.mikephil.charting.charts.LineChart; 6 | import com.github.mikephil.charting.charts.PieChart; 7 | import com.github.mikephil.charting.components.YAxis; 8 | 9 | import androidx.databinding.BindingAdapter; 10 | 11 | /** 12 | * @author hglf 13 | * @since 2018/10/15 14 | */ 15 | public class BindingAdpater { 16 | 17 | @BindingAdapter("globalOptions") 18 | public static void bindGlobalOptions(Chart chart, GlobalOptions options) { 19 | if (options == null) { 20 | return; 21 | } 22 | options.apply(chart); 23 | } 24 | @BindingAdapter("customOptions") 25 | public static void bindCustomOptions(Chart chart, CustomOptions options) { 26 | if (options == null) { 27 | return; 28 | } 29 | options.apply(chart); 30 | } 31 | 32 | @BindingAdapter("legendOptions") 33 | public static void bindLegendOptions(Chart chart, LegendOptions options) { 34 | if (options != null) { 35 | options.apply(chart.getLegend()); 36 | } 37 | } 38 | 39 | @BindingAdapter("xAxisOptions") 40 | public static void bindXAxisOptions(Chart chart, XAxisOptions options) { 41 | if (options == null || chart instanceof PieChart) { 42 | //pieChart dont have XAxis 43 | return; 44 | } 45 | options.apply(chart.getXAxis()); 46 | } 47 | 48 | @BindingAdapter("leftAxisOptions") 49 | public static void bindLeftAxisOptions(Chart chart, YAxisOptions options) { 50 | if (options == null) { 51 | return; 52 | } 53 | YAxis yAxis = null; 54 | if (chart instanceof LineChart) { 55 | yAxis = ((LineChart) chart).getAxisLeft(); 56 | } else if (chart instanceof BarChart) { 57 | yAxis = ((BarChart) chart).getAxisLeft(); 58 | } else { 59 | //没有y轴 60 | } 61 | options.apply(yAxis); 62 | } 63 | 64 | @BindingAdapter("rightAxisOptions") 65 | public static void bindRightAxisOptions(Chart chart, YAxisOptions options) { 66 | if (options == null) { 67 | return; 68 | } 69 | YAxis yAxis = null; 70 | if (chart instanceof LineChart) { 71 | yAxis = ((LineChart) chart).getAxisRight(); 72 | } else if (chart instanceof BarChart) { 73 | yAxis = ((BarChart) chart).getAxisRight(); 74 | } else { 75 | //没有y轴 76 | } 77 | options.apply(yAxis); 78 | } 79 | 80 | @BindingAdapter("animateX") 81 | public static void bindAnimateX(Chart chart, boolean aniamte) { 82 | if (aniamte) { 83 | chart.animateX(1000); 84 | } 85 | } 86 | 87 | @BindingAdapter("animateY") 88 | public static void bindAnimateY(Chart chart, boolean aniamte) { 89 | if (aniamte) { 90 | chart.animateY(1000); 91 | } 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /lib/src/main/java/github/hotstu/chartbinding/CustomOptions.java: -------------------------------------------------------------------------------- 1 | package github.hotstu.chartbinding; 2 | 3 | import com.github.mikephil.charting.charts.Chart; 4 | 5 | /** 6 | * @author hglf 7 | * @since 2018/11/12 8 | */ 9 | public interface CustomOptions { 10 | void apply(Chart chart); 11 | } 12 | -------------------------------------------------------------------------------- /lib/src/main/java/github/hotstu/chartbinding/FontOptions.java: -------------------------------------------------------------------------------- 1 | package github.hotstu.chartbinding; 2 | 3 | import android.graphics.Color; 4 | import android.graphics.Typeface; 5 | 6 | import com.github.mikephil.charting.components.ComponentBase; 7 | 8 | /** 9 | * @author hglf 10 | * @since 2018/10/25 11 | */ 12 | public class FontOptions { 13 | public int color = Color.BLACK; 14 | public float size = 14f; 15 | public Typeface typeface = null; 16 | 17 | public void apply(ComponentBase component) { 18 | if (component == null) { 19 | return; 20 | } 21 | component.setTextColor(color); 22 | component.setTextSize(size); 23 | if (typeface != null) { 24 | component.setTypeface(typeface); 25 | } 26 | } 27 | 28 | public FontOptions copy() { 29 | FontOptions ret = new FontOptions(); 30 | ret.typeface = typeface; 31 | ret.color = color; 32 | ret.size = size; 33 | return ret; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /lib/src/main/java/github/hotstu/chartbinding/GlobalOptions.java: -------------------------------------------------------------------------------- 1 | package github.hotstu.chartbinding; 2 | 3 | import com.github.mikephil.charting.charts.Chart; 4 | import com.github.mikephil.charting.components.Description; 5 | 6 | /** 7 | * @author hglf 8 | * @since 2018/10/15 9 | */ 10 | public class GlobalOptions { 11 | 12 | public Description description; 13 | public String noDataText = "没有数据"; 14 | 15 | 16 | public void apply(Chart chart) { 17 | if (chart == null) { 18 | return; 19 | } 20 | if (description != null) { 21 | chart.setDescription(description); 22 | } else { 23 | Description disabled = new Description(); 24 | disabled.setEnabled(false); 25 | chart.setDescription(disabled); 26 | } 27 | chart.setNoDataText(noDataText); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /lib/src/main/java/github/hotstu/chartbinding/LegendOptions.java: -------------------------------------------------------------------------------- 1 | package github.hotstu.chartbinding; 2 | 3 | import com.github.mikephil.charting.components.Legend; 4 | 5 | /** 6 | * @author hglf 7 | * @since 2018/10/15 8 | */ 9 | public class LegendOptions { 10 | public boolean enabled = true; 11 | public FontOptions font = new FontOptions(); 12 | public Legend.LegendForm form = Legend.LegendForm.CIRCLE; 13 | public LegendPosition position = new LegendPosition(); 14 | 15 | public void apply(Legend legend) { 16 | if (font != null) { 17 | font.apply(legend); 18 | } 19 | if (form != null) { 20 | legend.setForm(form); 21 | } 22 | if (position != null) { 23 | position.apply(legend); 24 | } 25 | legend.setEnabled(enabled); 26 | } 27 | 28 | public LegendOptions copy() { 29 | LegendOptions ret = new LegendOptions(); 30 | ret.enabled = enabled; 31 | ret.form = form; 32 | ret.font = font; 33 | ret.position = position; 34 | return ret; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /lib/src/main/java/github/hotstu/chartbinding/LegendPosition.java: -------------------------------------------------------------------------------- 1 | package github.hotstu.chartbinding; 2 | 3 | import com.github.mikephil.charting.components.Legend; 4 | 5 | /** 6 | * @author hglf 7 | * @since 2018/10/26 8 | */ 9 | public class LegendPosition { 10 | public Legend.LegendHorizontalAlignment horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT; 11 | public Legend.LegendVerticalAlignment verticalAlignment = Legend.LegendVerticalAlignment.TOP; 12 | public Legend.LegendOrientation orientation = Legend.LegendOrientation.VERTICAL; 13 | public Legend.LegendDirection direction = Legend.LegendDirection.LEFT_TO_RIGHT; 14 | public boolean drawInside = false; 15 | 16 | public static LegendPosition topRight() { 17 | return new LegendPosition(); 18 | } 19 | public static LegendPosition bottomLeft() { 20 | LegendPosition ret = new LegendPosition(); 21 | ret.horizontalAlignment = Legend.LegendHorizontalAlignment.LEFT; 22 | ret.verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM; 23 | ret.orientation = Legend.LegendOrientation.HORIZONTAL; 24 | return ret; 25 | } 26 | public void apply(Legend legend) { 27 | if (horizontalAlignment != null) { 28 | legend.setHorizontalAlignment(horizontalAlignment); 29 | } 30 | if (verticalAlignment != null) { 31 | legend.setVerticalAlignment(verticalAlignment); 32 | } 33 | if (orientation != null) { 34 | legend.setOrientation(orientation); 35 | } 36 | if (direction != null) { 37 | legend.setDirection(direction); 38 | } 39 | legend.setDrawInside(drawInside); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /lib/src/main/java/github/hotstu/chartbinding/SeriesDescription.java: -------------------------------------------------------------------------------- 1 | package github.hotstu.chartbinding; 2 | 3 | import com.github.mikephil.charting.interfaces.datasets.IDataSet; 4 | 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | 8 | import androidx.annotation.IntDef; 9 | 10 | /** 11 | * @author hglf 12 | * @since 2018/10/15 13 | */ 14 | public class SeriesDescription { 15 | public static final int TYPE_NONE = 0; 16 | public static final int TYPE_LINE = 1; 17 | public static final int TYPE_BAR = 2; 18 | public static final int TYPE_PIE = 3; 19 | 20 | @Retention(RetentionPolicy.SOURCE) 21 | @IntDef({TYPE_NONE, TYPE_LINE, TYPE_BAR, TYPE_PIE}) 22 | public @interface chartType { } 23 | 24 | final @chartType int type; 25 | 26 | final T[] dataSet; 27 | 28 | public GlobalOptions globalOptions; 29 | public XAxisOptions xAxisOptions; 30 | public YAxisOptions leftAxisOptions; 31 | public YAxisOptions rightAxisOptions; 32 | public LegendOptions legendOptions; 33 | public CustomOptions customOptions; 34 | public boolean animateX = true; 35 | public boolean animateY = true; 36 | 37 | public SeriesDescription(int type, T... dataSet) { 38 | this.type = type; 39 | this.dataSet = dataSet; 40 | } 41 | 42 | public T[] getDataSet() { 43 | return dataSet; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /lib/src/main/java/github/hotstu/chartbinding/XAxisOptions.java: -------------------------------------------------------------------------------- 1 | package github.hotstu.chartbinding; 2 | 3 | import com.github.mikephil.charting.components.XAxis; 4 | import com.github.mikephil.charting.formatter.IAxisValueFormatter; 5 | 6 | /** 7 | * @author hglf 8 | * @since 2018/10/24 9 | */ 10 | public class XAxisOptions { 11 | public XAxis.XAxisPosition position = XAxis.XAxisPosition.BOTTOM; 12 | public boolean enabled = true; 13 | public boolean drawAxisLine = true; 14 | public boolean drawGridLine = false; 15 | public float granularity = 1; 16 | public float labelRotationAngle = 0; 17 | public IAxisValueFormatter valueFormatter = null; 18 | public FontOptions font = new FontOptions(); 19 | public AxisLineOptions lineOptions = new AxisLineOptions(); 20 | 21 | 22 | public void apply(XAxis xAxis) { 23 | if (xAxis == null) { 24 | return; 25 | } 26 | if (valueFormatter != null) { 27 | xAxis.setValueFormatter(valueFormatter); 28 | } 29 | xAxis.setEnabled(enabled); 30 | xAxis.setPosition(position); 31 | xAxis.setDrawAxisLine(drawAxisLine); 32 | xAxis.setDrawGridLines(drawGridLine); 33 | xAxis.setGranularity(granularity); 34 | xAxis.setLabelRotationAngle(labelRotationAngle); 35 | if (font != null) { 36 | font.apply(xAxis); 37 | } 38 | if (lineOptions != null) { 39 | lineOptions.apply(xAxis); 40 | } 41 | } 42 | 43 | public XAxisOptions copy() { 44 | XAxisOptions ret = new XAxisOptions(); 45 | ret.position = position; 46 | ret.font = font; 47 | ret.drawAxisLine = drawAxisLine; 48 | ret.drawGridLine = drawGridLine; 49 | ret.valueFormatter = valueFormatter; 50 | ret.granularity = granularity; 51 | return ret; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /lib/src/main/java/github/hotstu/chartbinding/YAxisOptions.java: -------------------------------------------------------------------------------- 1 | package github.hotstu.chartbinding; 2 | 3 | import com.github.mikephil.charting.components.YAxis; 4 | import com.github.mikephil.charting.formatter.IAxisValueFormatter; 5 | 6 | /** 7 | * 注意,YAxis 分为left和right,可以单独赋值 8 | * @author hglf 9 | * @since 2018/10/24 10 | */ 11 | public class YAxisOptions { 12 | public boolean drawLabels = true; 13 | public boolean drawAxisLine = true; 14 | public boolean drawGridLines = false; // no grid lines 15 | public boolean drawZeroLine = false; // draw a zero line 16 | public boolean enabled = true; 17 | public IAxisValueFormatter valueFormatter; 18 | public FontOptions font = new FontOptions(); 19 | public AxisLineOptions lineOptions = new AxisLineOptions(); 20 | 21 | public void apply(YAxis yAxis) { 22 | if (yAxis == null) { 23 | return; 24 | } 25 | yAxis.setDrawLabels(drawLabels); 26 | yAxis.setDrawAxisLine(drawAxisLine); 27 | yAxis.setDrawGridLines(drawGridLines); 28 | yAxis.setDrawZeroLine(drawZeroLine); 29 | if (valueFormatter != null) { 30 | yAxis.setValueFormatter(valueFormatter); 31 | } 32 | if (font != null) { 33 | font.apply(yAxis); 34 | } 35 | if (lineOptions != null) { 36 | lineOptions.apply(yAxis); 37 | } 38 | yAxis.setZeroLineWidth(1); 39 | yAxis.setEnabled(enabled); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /lib/src/main/res/layout/binding_bar_chart.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 12 | 13 | 16 | 19 | 20 | 23 | 26 | 29 | 32 | 33 | 36 | 39 | 40 | 41 | 42 | 43 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /lib/src/main/res/layout/binding_line_chart.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 12 | 13 | 16 | 17 | 20 | 21 | 24 | 25 | 28 | 29 | 32 | 33 | 36 | 37 | 40 | 41 | 44 | 45 | 46 | 47 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /lib/src/main/res/layout/binding_none_chart.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 10 | 11 | 14 | 15 | 18 | 19 | 22 | 23 | 26 | 27 | 30 | 31 | 34 | 35 | 38 | 39 | 42 | 43 | 44 | 47 | 48 | -------------------------------------------------------------------------------- /lib/src/main/res/layout/binding_pie_chart.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 12 | 13 | 16 | 17 | 20 | 21 | 24 | 25 | 28 | 29 | 32 | 33 | 36 | 37 | 40 | 41 | 44 | 45 | 46 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /lib/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /lib/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #fef8ef 5 | #333333 6 | #aaa 7 | #d87c7c 8 | #919e8b 9 | #d7ab82 10 | #6e7074 11 | #61a0a8 12 | #efa18d 13 | #787464 14 | #cc7e63 15 | #724e58 16 | #4b565b 17 | -------------------------------------------------------------------------------- /lib/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | chartbinding 3 | 4 | -------------------------------------------------------------------------------- /lib/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 19 | -------------------------------------------------------------------------------- /preview/1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hotstu/chartbinding/2d722daabe0ee479c6c1ba465e211ffda94cd63f/preview/1.gif -------------------------------------------------------------------------------- /project.properties: -------------------------------------------------------------------------------- 1 | # suppress inspection "UnusedProperty" for whole file 2 | #project 3 | project.name=chartbinding 4 | project.repo=maven 5 | 6 | project.groupId=github.hotstu.chartbinding 7 | project.packaging=aar 8 | project.siteUrl=https\://github.com/hotstu/chartbinding 9 | project.gitUrl=https\://github.com/hotstu/chartbinding.git -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':lib' 2 | --------------------------------------------------------------------------------