├── .gitignore ├── .idea ├── caches │ └── build_file_checksums.ser ├── codeStyles │ └── Project.xml ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── markdown-navigator.xml ├── markdown-navigator │ └── profiles_settings.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── io │ │ └── github │ │ └── kntryer │ │ └── linechart │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── io │ │ │ └── github │ │ │ └── kntryer │ │ │ └── linechart │ │ │ ├── LineChart.java │ │ │ ├── LineChartData.java │ │ │ └── MainActivity.java │ └── res │ │ ├── layout │ │ └── activity_main.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 │ └── io │ └── github │ └── kntryer │ └── linechart │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── linechart.gif ├── linechart.png ├── linechart_1_2.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 | -------------------------------------------------------------------------------- /.idea/caches/build_file_checksums.ser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kntryer/LineChart/185753a935700902c801e124fab1af3043a19cad/.idea/caches/build_file_checksums.ser -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | xmlns:android 14 | 15 | ^$ 16 | 17 | 18 | 19 |
20 |
21 | 22 | 23 | 24 | xmlns:.* 25 | 26 | ^$ 27 | 28 | 29 | BY_NAME 30 | 31 |
32 |
33 | 34 | 35 | 36 | .*:id 37 | 38 | http://schemas.android.com/apk/res/android 39 | 40 | 41 | 42 |
43 |
44 | 45 | 46 | 47 | .*:name 48 | 49 | http://schemas.android.com/apk/res/android 50 | 51 | 52 | 53 |
54 |
55 | 56 | 57 | 58 | name 59 | 60 | ^$ 61 | 62 | 63 | 64 |
65 |
66 | 67 | 68 | 69 | style 70 | 71 | ^$ 72 | 73 | 74 | 75 |
76 |
77 | 78 | 79 | 80 | .* 81 | 82 | ^$ 83 | 84 | 85 | BY_NAME 86 | 87 |
88 |
89 | 90 | 91 | 92 | .* 93 | 94 | http://schemas.android.com/apk/res/android 95 | 96 | 97 | ANDROID_ATTRIBUTE_ORDER 98 | 99 |
100 |
101 | 102 | 103 | 104 | .* 105 | 106 | .* 107 | 108 | 109 | BY_NAME 110 | 111 |
112 |
113 |
114 |
115 |
116 |
-------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/markdown-navigator.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 36 | 37 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /.idea/markdown-navigator/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 24 | 41 | 42 | 43 | 44 | 45 | 46 | 48 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LineChart 2 | a simple line chart for android 3 | 4 | 简易折线图,仿滴答清单UI。 5 | 6 | ### v 1.2 (2020-04-02) 7 | 8 | > 添加了空心点,渐变色 9 | 10 | 🤣 有两年没写安卓,看回以前写的代码觉得太粗糙了,哈哈。 11 | 12 | #### Preview 13 | 14 | 15 | 16 | ### v 1.1 (2017-11-07) 17 | 18 | > 加入属性动画 19 | 20 | #### Preview 21 | 22 | 23 | 24 | ### v 1.0 (2016-11-05) 25 | 26 | > 完成基本的折线图 27 | 28 | #### Preview 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 26 5 | 6 | defaultConfig { 7 | applicationId "io.github.kntryer.linechart" 8 | minSdkVersion 15 9 | targetSdkVersion 26 10 | versionCode 1 11 | versionName "1.2" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(dir: 'libs', include: ['*.jar']) 23 | implementation 'com.android.support:appcompat-v7:26.1.0' 24 | implementation 'com.android.support.constraint:constraint-layout:1.0.2' 25 | implementation 'com.android.support:design:26.1.0' 26 | testImplementation 'junit:junit:4.12' 27 | androidTestImplementation 'com.android.support.test:runner:1.0.1' 28 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' 29 | } 30 | -------------------------------------------------------------------------------- /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 F:\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/io/github/kntryer/linechart/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package io.github.kntryer.linechart; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /app/src/main/java/io/github/kntryer/linechart/LineChart.java: -------------------------------------------------------------------------------- 1 | package io.github.kntryer.linechart; 2 | 3 | import android.animation.Animator; 4 | import android.animation.AnimatorListenerAdapter; 5 | import android.animation.ValueAnimator; 6 | import android.content.Context; 7 | import android.content.res.TypedArray; 8 | import android.graphics.Canvas; 9 | import android.graphics.Color; 10 | import android.graphics.LinearGradient; 11 | import android.graphics.Paint; 12 | import android.graphics.Path; 13 | import android.graphics.Shader; 14 | import android.util.AttributeSet; 15 | import android.util.Log; 16 | import android.util.TypedValue; 17 | import android.view.View; 18 | import android.view.ViewGroup; 19 | import android.view.animation.LinearInterpolator; 20 | 21 | import java.math.BigDecimal; 22 | import java.util.List; 23 | 24 | /** 25 | * Created by kn on 2016/11/1. 26 | *

27 | * 折线图 28 | */ 29 | public class LineChart extends View { 30 | 31 | private int mWidth, mHeight;//View 的宽和高 32 | 33 | private float mFontSize = 12;//字体的大小 34 | private float mStrokeWidth = 1.5f;//线条的宽度 35 | private float mPointRadius = 2;//点的半径 36 | private int mDateTextColor = Color.parseColor("#cfcfcf");//日期字体颜色 37 | private int mDarkColor = Color.parseColor("#5b7fdf");//点、线的颜色(深色) 38 | private int mLightColor = Color.parseColor("#d5d8f7");//点、线的颜色(浅色) 39 | private int mShapeColor = Color.parseColor("#f3f6fd");//阴影的颜色 40 | 41 | private int mGradientLightColor = Color.parseColor("#d5d8f7");//渐变浅色 42 | private int mGradientDarkColor = Color.parseColor("#5b7fdf");//渐变深色 43 | private boolean mIsShowGradient = false;//是否显示渐变 44 | 45 | private String[] mXItems;//X轴的文字 46 | private int[] mPoints;//点的数组,-1表示该日还没到 47 | private int mLength = 7;//最大比例 48 | 49 | private Paint mDatePaint = new Paint();//日期画笔 50 | private Paint mPointPaint = new Paint();//点画笔 51 | private Paint mLinePaint = new Paint();//线条画笔 52 | private Paint mShapePaint = new Paint();//阴影部分画笔 53 | 54 | private int max = 7; 55 | private Context mContext; 56 | 57 | public LineChart(Context context) { 58 | this(context, null); 59 | } 60 | 61 | public LineChart(Context context, AttributeSet attrs) { 62 | this(context, attrs, 0); 63 | } 64 | 65 | public LineChart(Context context, AttributeSet attrs, int defStyleAttr) { 66 | super(context, attrs, defStyleAttr); 67 | mContext = context; 68 | TypedArray typedArray = mContext.getTheme().obtainStyledAttributes(attrs, R.styleable.LineChart, 0, 0); 69 | try { 70 | mDateTextColor = typedArray.getColor(R.styleable.LineChart_DateTextColor, mDateTextColor); 71 | mDarkColor = typedArray.getColor(R.styleable.LineChart_DarkColor, mDarkColor); 72 | mLightColor = typedArray.getColor(R.styleable.LineChart_LightColor, mLightColor); 73 | mShapeColor = typedArray.getColor(R.styleable.LineChart_ShapeColor, mShapeColor); 74 | mFontSize = typedArray.getDimensionPixelSize(R.styleable.LineChart_FontSize, 75 | (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, mFontSize, mContext.getResources().getDisplayMetrics())); 76 | mStrokeWidth = typedArray.getDimensionPixelSize(R.styleable.LineChart_StrokeWidth, 77 | (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, mStrokeWidth, mContext.getResources().getDisplayMetrics())); 78 | mPointRadius = typedArray.getDimensionPixelSize(R.styleable.LineChart_PointRadius, 79 | (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mPointRadius, mContext.getResources().getDisplayMetrics())); 80 | mGradientLightColor = typedArray.getColor(R.styleable.LineChart_GradientLightColor, mGradientLightColor); 81 | mGradientDarkColor = typedArray.getColor(R.styleable.LineChart_GradientDarkColor, mGradientDarkColor); 82 | mIsShowGradient = typedArray.getBoolean(R.styleable.LineChart_IsShowGradient, mIsShowGradient); 83 | } finally { 84 | typedArray.recycle(); 85 | } 86 | initPaint(); 87 | } 88 | 89 | private void initPaint() { 90 | //日期画笔 91 | mDatePaint.setTextSize(mFontSize); 92 | mDatePaint.setColor(mDateTextColor); 93 | //点画笔 94 | mPointPaint.setTextSize(mFontSize); 95 | mPointPaint.setColor(mDarkColor); 96 | //先画笔 97 | mLinePaint.setAntiAlias(true); 98 | mLinePaint.setStrokeWidth(mStrokeWidth);//设置线条宽度 99 | mLinePaint.setStyle(Paint.Style.FILL); 100 | mLinePaint.setColor(mDarkColor); 101 | //阴影部分画笔 102 | mShapePaint.setAntiAlias(true); 103 | mShapePaint.setStyle(Paint.Style.FILL); 104 | mShapePaint.setColor(mShapeColor); 105 | } 106 | 107 | @Override 108 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 109 | int widthMode = MeasureSpec.getMode(widthMeasureSpec); 110 | int heightMode = MeasureSpec.getMode(heightMeasureSpec); 111 | int widthSize = MeasureSpec.getSize(widthMeasureSpec); 112 | int heightSize = MeasureSpec.getSize(heightMeasureSpec); 113 | mWidth = widthSize; 114 | mHeight = heightSize; 115 | // if (widthMode == MeasureSpec.EXACTLY) { 116 | // mWidth = widthSize; 117 | // } else if (widthMode == MeasureSpec.AT_MOST) { 118 | // mWidth = widthSize; 119 | // } 120 | // 121 | // if (heightMode == MeasureSpec.EXACTLY) { 122 | // mHeight = heightSize; 123 | // } else if (heightMode == MeasureSpec.AT_MOST) { 124 | // mHeight = mWidth / 7 * 3; 125 | // } 126 | 127 | if (heightMode == MeasureSpec.AT_MOST) { 128 | mHeight = mWidth / 7 * 3; 129 | } 130 | setMeasuredDimension(mWidth, mHeight); 131 | } 132 | 133 | private float mAnimatedValue = 0f; 134 | 135 | protected void OnAnimationUpdate(ValueAnimator valueAnimator) { 136 | mAnimatedValue = (float) valueAnimator.getAnimatedValue(); 137 | invalidate(); 138 | } 139 | 140 | public ValueAnimator valueAnimator; 141 | 142 | private ValueAnimator startViewAnim(float startF, final float endF, long time) { 143 | valueAnimator = ValueAnimator.ofFloat(startF, endF); 144 | valueAnimator.setDuration(time); 145 | valueAnimator.setInterpolator(new LinearInterpolator()); 146 | 147 | // valueAnimator.setRepeatCount(ValueAnimator.INFINITE); 148 | 149 | // valueAnimator.setRepeatMode(ValueAnimator.RESTART); 150 | 151 | valueAnimator.setRepeatMode(ValueAnimator.REVERSE); 152 | 153 | valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 154 | @Override 155 | public void onAnimationUpdate(ValueAnimator valueAnimator) { 156 | OnAnimationUpdate(valueAnimator); 157 | 158 | } 159 | }); 160 | valueAnimator.addListener(new AnimatorListenerAdapter() { 161 | @Override 162 | public void onAnimationEnd(Animator animation) { 163 | super.onAnimationEnd(animation); 164 | 165 | } 166 | 167 | @Override 168 | public void onAnimationStart(Animator animation) { 169 | super.onAnimationStart(animation); 170 | } 171 | 172 | @Override 173 | public void onAnimationRepeat(Animator animation) { 174 | super.onAnimationRepeat(animation); 175 | } 176 | }); 177 | if (!valueAnimator.isRunning()) { 178 | valueAnimator.start(); 179 | } 180 | 181 | return valueAnimator; 182 | } 183 | 184 | // 阴影部分梯形路径 185 | Path path = new Path(); 186 | // 渐变 187 | Shader mShader = new LinearGradient(0, 0, 0, mHeight, 188 | new int[]{mGradientLightColor, mGradientDarkColor}, null, Shader.TileMode.CLAMP); 189 | 190 | int[] xPoints = new int[mLength];//x轴的刻度集合 191 | int[] yPoints = new int[mLength];//y轴的刻度集合 192 | 193 | @Override 194 | protected void onDraw(Canvas canvas) { 195 | 196 | if (mXItems == null) { 197 | mXItems = new String[]{"日", "一", "二", "三", "四", "五", "六"}; 198 | mPoints = new int[]{0, -1, -1, -1, -1, -1, -1}; 199 | mLength = mXItems.length; 200 | xPoints = new int[mLength]; 201 | yPoints = new int[mLength]; 202 | } 203 | 204 | //最大比例 205 | for (int i = 0; i < mLength; i++) { 206 | if (mPoints[i] > max) { 207 | max = mPoints[i]; 208 | } 209 | } 210 | 211 | //原点坐标 212 | int xOrigin = (int) (0.5 * (mWidth / mLength) - mFontSize / 2); 213 | int yOrigin = (int) (max * ((mHeight - mLength * mFontSize) / max) + 4 * mFontSize); 214 | 215 | canvas.save(); 216 | 217 | for (int i = 0; i < mLength; i++) { 218 | 219 | //获取点的坐标 220 | xPoints[i] = (int) ((i + 0.5) * (mWidth / mLength)); 221 | yPoints[i] = (int) ((max - (mPoints[i] == -1 ? 0 : mPoints[i]) * mAnimatedValue) * ((mHeight - mLength * mFontSize) / max) + 4 * mFontSize); 222 | 223 | if (i > 0) { 224 | //画一个实心梯形,阴影部分 225 | path.moveTo(xPoints[i - 1], yOrigin + mPointRadius / 2); 226 | path.lineTo(xPoints[i - 1], yPoints[i - 1]); 227 | path.lineTo(xPoints[i], yPoints[i]); 228 | path.lineTo(xPoints[i], yOrigin + mPointRadius / 2); 229 | path.close(); 230 | 231 | if (mIsShowGradient) { 232 | //添加线性渐变 233 | mShapePaint.setShader(mShader); 234 | } 235 | canvas.drawPath(path, mShapePaint); 236 | path.reset(); 237 | } 238 | //画出日期 239 | canvas.drawText(mXItems[i], (int) ((i + 0.5) * mWidth / mLength) - mFontSize / 2, mHeight - mFontSize, mDatePaint); 240 | } 241 | 242 | for (int i = 0; i < mLength; i++) { 243 | mLinePaint.setColor(mPoints[i] == -1 ? mLightColor : mDarkColor); 244 | mPointPaint.setColor(mPoints[i] == -1 ? mLightColor : mDarkColor); 245 | if (i > 0) { 246 | //画连线 247 | canvas.drawLine(xPoints[i - 1], yPoints[i - 1], xPoints[i], yPoints[i], mLinePaint); 248 | } 249 | //画点的数值 250 | canvas.drawText(mPoints[i] == -1 ? " " : String.valueOf((int) Math.floor(mPoints[i] * mAnimatedValue)) 251 | , xPoints[i] - mFontSize / 4, yPoints[i] - mFontSize, mPointPaint); 252 | } 253 | 254 | // 顶点 255 | for (int i = 0; i < mLength; i++) { 256 | mPointPaint.setColor(mPoints[i] == -1 ? mLightColor : mDarkColor); 257 | mLinePaint.setColor(Color.parseColor("#FFFFFF")); 258 | //画大点 259 | canvas.drawCircle(xPoints[i], yPoints[i], mPointRadius + 2, mPointPaint); 260 | //画小点 261 | canvas.drawCircle(xPoints[i], yPoints[i], mPointRadius, mLinePaint); 262 | } 263 | 264 | canvas.restore(); 265 | } 266 | 267 | public void setData(List dataList) { 268 | mLength = dataList.size(); 269 | if (mLength > 0) { 270 | mXItems = new String[mLength]; 271 | mPoints = new int[mLength]; 272 | for (int i = 0; i < mLength; i++) { 273 | mPoints[i] = dataList.get(i).getPoint(); 274 | mXItems[i] = dataList.get(i).getItem(); 275 | } 276 | } 277 | startViewAnim(0f, 1f, 1000); 278 | } 279 | 280 | //设置是否显示渐变 281 | public LineChart setIsShowGradient(boolean mIsShowGradient) { 282 | this.mIsShowGradient = mIsShowGradient; 283 | return this; 284 | } 285 | 286 | //设置渐变颜色 287 | public LineChart setGradientColor(String mLightColor, String mDarkColor) { 288 | this.mGradientLightColor = Color.parseColor(mLightColor); 289 | this.mGradientDarkColor = Color.parseColor(mDarkColor); 290 | mShader = new LinearGradient(0, 0, 0, mHeight, 291 | new int[]{mGradientLightColor, mGradientDarkColor}, null, Shader.TileMode.CLAMP); 292 | return this; 293 | } 294 | 295 | //设置 view 高度 296 | public LineChart setHigh(int mHeight) { 297 | mHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mHeight, mContext.getResources().getDisplayMetrics()); 298 | ViewGroup.LayoutParams layoutParams = this.getLayoutParams(); 299 | if (layoutParams.height != mHeight) { 300 | layoutParams.height = mHeight; 301 | this.setLayoutParams(layoutParams); 302 | } 303 | this.mHeight = mHeight; 304 | mShader = new LinearGradient(0, 0, 0, mHeight, 305 | new int[]{mGradientLightColor, mGradientDarkColor}, null, Shader.TileMode.CLAMP); 306 | return this; 307 | } 308 | 309 | //刷新view 310 | public void refreshView() { 311 | startViewAnim(0f, 1f, 1000); 312 | } 313 | 314 | } 315 | -------------------------------------------------------------------------------- /app/src/main/java/io/github/kntryer/linechart/LineChartData.java: -------------------------------------------------------------------------------- 1 | package io.github.kntryer.linechart; 2 | 3 | /** 4 | * Created by kn on 2016/11/5. 5 | */ 6 | public class LineChartData { 7 | 8 | private int point; 9 | private String item; 10 | 11 | public int getPoint() { 12 | return point; 13 | } 14 | 15 | public void setPoint(int point) { 16 | this.point = point; 17 | } 18 | 19 | public String getItem() { 20 | return item; 21 | } 22 | 23 | public void setItem(String item) { 24 | this.item = item; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/java/io/github/kntryer/linechart/MainActivity.java: -------------------------------------------------------------------------------- 1 | package io.github.kntryer.linechart; 2 | 3 | import android.graphics.Color; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | import android.widget.TextView; 8 | 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | public class MainActivity extends AppCompatActivity { 13 | 14 | private LineChart mLineChart; 15 | private TextView tvDay; 16 | private TextView tvWeek; 17 | private TextView tvMonth; 18 | private TextView tvGradient; 19 | 20 | private String[] mDayItems = new String[]{"31日", "1日", "2日", "3日", "4日", "5日", "6日"}; 21 | private int[] mDayPoints = new int[]{0, 2, 7, 4, 0, 1, -1}; 22 | private String[] mWeekItems = new String[]{"日", "一", "二", "三", "四", "五", "六"}; 23 | private int[] mWeekPoints = new int[]{7, 2, 1, 4, 0, 1, -1}; 24 | private String[] mMonthItems = new String[]{"5月", "6月", "7月", "8月", "9月", "10月", "11月"}; 25 | private int[] mMonthPoints = new int[]{0, 2, 1, 0, 0, 0, 8}; 26 | private List dataList = new ArrayList<>(); 27 | 28 | @Override 29 | protected void onCreate(Bundle savedInstanceState) { 30 | super.onCreate(savedInstanceState); 31 | setContentView(R.layout.activity_main); 32 | 33 | mLineChart = findViewById(R.id.line_chart); 34 | tvDay = findViewById(R.id.tv_day); 35 | tvWeek = findViewById(R.id.tv_week); 36 | tvMonth = findViewById(R.id.tv_month); 37 | tvGradient = findViewById(R.id.tv_gradient); 38 | 39 | resetTextColor(); 40 | tvDay.setTextColor(getResources().getColor(R.color.colorPrimary)); 41 | for (int i = 0; i < mDayItems.length; i++) { 42 | LineChartData data = new LineChartData(); 43 | data.setItem(mDayItems[i]); 44 | data.setPoint(mDayPoints[i]); 45 | dataList.add(data); 46 | } 47 | mLineChart.setData(dataList); 48 | } 49 | 50 | public void dayClick(View view){ 51 | resetTextColor(); 52 | tvDay.setTextColor(getResources().getColor(R.color.colorPrimary)); 53 | dataList.clear(); 54 | for (int i = 0; i < mDayItems.length; i++) { 55 | LineChartData data = new LineChartData(); 56 | data.setItem(mDayItems[i]); 57 | data.setPoint(mDayPoints[i]); 58 | dataList.add(data); 59 | } 60 | mLineChart.setData(dataList); 61 | } 62 | 63 | public void weekClick(View view){ 64 | resetTextColor(); 65 | tvWeek.setTextColor(getResources().getColor(R.color.colorPrimary)); 66 | dataList.clear(); 67 | for (int i = 0; i < mWeekItems.length; i++) { 68 | LineChartData data = new LineChartData(); 69 | data.setItem(mWeekItems[i]); 70 | data.setPoint(mWeekPoints[i]); 71 | dataList.add(data); 72 | } 73 | mLineChart.setData(dataList); 74 | } 75 | 76 | public void monthClick(View view){ 77 | resetTextColor(); 78 | tvMonth.setTextColor(getResources().getColor(R.color.colorPrimary)); 79 | dataList.clear(); 80 | for (int i = 0; i < mMonthItems.length; i++) { 81 | LineChartData data = new LineChartData(); 82 | data.setItem(mMonthItems[i]); 83 | data.setPoint(mMonthPoints[i]); 84 | dataList.add(data); 85 | } 86 | mLineChart.setData(dataList); 87 | } 88 | 89 | public void gradientClick(View view){ 90 | resetTextColor(); 91 | tvGradient.setTextColor(getResources().getColor(R.color.colorPrimary)); 92 | mLineChart.setIsShowGradient(true) 93 | .setGradientColor("#f3f6fd", "#fa267b") 94 | .setHigh(300) 95 | .refreshView(); 96 | } 97 | 98 | public void resetTextColor(){ 99 | tvDay.setTextColor(getResources().getColor(R.color.text_title)); 100 | tvWeek.setTextColor(getResources().getColor(R.color.text_title)); 101 | tvMonth.setTextColor(getResources().getColor(R.color.text_title)); 102 | tvGradient.setTextColor(getResources().getColor(R.color.text_title)); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 15 | 16 | 24 | 25 | 31 | 32 | 38 | 39 | 48 | 49 | 58 | 59 | 68 | 69 | 78 | 79 | 80 | 81 | 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kntryer/LineChart/185753a935700902c801e124fab1af3043a19cad/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kntryer/LineChart/185753a935700902c801e124fab1af3043a19cad/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kntryer/LineChart/185753a935700902c801e124fab1af3043a19cad/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kntryer/LineChart/185753a935700902c801e124fab1af3043a19cad/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kntryer/LineChart/185753a935700902c801e124fab1af3043a19cad/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 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #617fdf 4 | #616edf 5 | #FF4081 6 | 7 | #e4e5e9 8 | #FFFFFF 9 | #a8a9ad 10 | #cfcfcf 11 | #5b7fdf 12 | #d5d8f7 13 | #f3f6fd 14 | #fa267b 15 | #fce4ec 16 | #fff0f5 17 | 18 | 19 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | LineChart 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/io/github/kntryer/linechart/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package io.github.kntryer.linechart; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * To work on unit tests, switch the Test Artifact in the Build Variants view. 9 | */ 10 | public class ExampleUnitTest { 11 | @Test 12 | public void addition_isCorrect() throws Exception { 13 | assertEquals(4, 2 + 2); 14 | } 15 | } -------------------------------------------------------------------------------- /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 | jcenter() 7 | } 8 | dependencies { 9 | classpath 'com.android.tools.build:gradle:3.0.0' 10 | 11 | // NOTE: Do not place your application dependencies here; they belong 12 | // in the individual module build.gradle files 13 | } 14 | } 15 | 16 | allprojects { 17 | repositories { 18 | google() 19 | jcenter() 20 | } 21 | } 22 | 23 | task clean(type: Delete) { 24 | delete rootProject.buildDir 25 | } 26 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kntryer/LineChart/185753a935700902c801e124fab1af3043a19cad/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Nov 06 17:24:47 CST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.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 | -------------------------------------------------------------------------------- /linechart.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kntryer/LineChart/185753a935700902c801e124fab1af3043a19cad/linechart.gif -------------------------------------------------------------------------------- /linechart.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kntryer/LineChart/185753a935700902c801e124fab1af3043a19cad/linechart.png -------------------------------------------------------------------------------- /linechart_1_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kntryer/LineChart/185753a935700902c801e124fab1af3043a19cad/linechart_1_2.png -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | --------------------------------------------------------------------------------