├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml └── runConfigurations.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── wzh │ │ └── linechart │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── wzh │ │ │ └── linechart │ │ │ ├── MainActivity.java │ │ │ └── view │ │ │ └── LineView.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-xhdpi │ │ └── dimens.xml │ │ ├── values-xxhdpi │ │ └── dimens.xml │ │ └── values │ │ ├── attrs.xml │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── wzh │ └── linechart │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.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/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | 47 | 48 | 49 | 50 | 1.7 51 | 52 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Android-LineChartView-master 2 | 最近看到有人在做带有折线图的项目,看着挺牛叉的,于是动手写了一把,记录下来绘制的过程,希望不会遗忘,主要就是计算问题,其他就是canvas绘制的过程了。 3 | 4 | ![](https://upload-images.jianshu.io/upload_images/2018489-e604cab833d2dc07.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/540) 5 | 6 | 简单封装了一下几个关键属性,使用的姿势: 7 | ``` 8 | 22 | 23 | ``` 24 | - isShowColumnLine :是否显示竖直方向的垂直线(如图虚线所示) 25 | - isShowRowLine:是否显示水平方向的垂直线(图上是没显示出来) 26 | - dashColor:这个是垂直线的颜色 27 | - dataFactor:这个是y轴数据的因子,比如都是2位数的数据该值就为10,三位数就为100... 28 | - dotRadius:折线图上数据点的半径大小 29 | - isLinearGradient:是否折线图下面显示渐变 30 | - lineColor:折线的颜色 31 | - lineWidth:折现的粗细 32 | 33 | 在代码中去设置数据集合及横左边的lable,以及x,y轴的名称等 34 | ``` 35 | private String yTitle = "销售额(单位:万元)"; 36 | private String xTitle = "(年份)"; 37 | private int[] data = {70, 80, 90, 60, 80, 70, 40}; 38 | private String[] lables = {"2010", "2011", "2012", "2013", "2014", "2015", "2016"}; 39 | 40 | lineView.setData(data); 41 | lineView.setLables(lables); 42 | lineView.setxTitle(xTitle); 43 | lineView.setyTitle(yTitle); 44 | lineView.setDataFactor(10); 45 | 46 | ``` 47 | 48 | 这样设置完之后就可以运行出来效果了。 49 | 50 | 总结下:通过此自定义折线图熟练了绘制点、线、path、文本、以及根据最大和最小的数据值及高度得到的比例,去计算每个数据应在的y坐标,还学会如何使用LinearGradient去绘制渐变。 51 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.2" 6 | defaultConfig { 7 | applicationId "com.wzh.linechart" 8 | minSdkVersion 15 9 | targetSdkVersion 23 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | compile 'com.android.support:appcompat-v7:24.2.0' 28 | testCompile 'junit:junit:4.12' 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 D:\Android\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/wzh/linechart/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.wzh.linechart; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.wzh.linechart", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /app/src/main/java/com/wzh/linechart/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.wzh.linechart; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | 6 | import com.wzh.linechart.view.LineView; 7 | 8 | public class MainActivity extends AppCompatActivity { 9 | private LineView lineView ; 10 | private String yTitle = "销售额(单位:万元)"; 11 | private String xTitle = "(年份)"; 12 | private int[] data = {70, 80, 90, 60, 80, 70, 40}; 13 | private String[] lables = {"2010", "2011", "2012", "2013", "2014", "2015", "2016"}; 14 | @Override 15 | protected void onCreate(Bundle savedInstanceState) { 16 | super.onCreate(savedInstanceState); 17 | setContentView(R.layout.activity_main); 18 | lineView = (LineView) findViewById(R.id.lineView); 19 | lineView.setData(data); 20 | lineView.setLables(lables); 21 | lineView.setxTitle(xTitle); 22 | lineView.setyTitle(yTitle); 23 | lineView.setDataFactor(10); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/com/wzh/linechart/view/LineView.java: -------------------------------------------------------------------------------- 1 | package com.wzh.linechart.view; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.graphics.Canvas; 6 | import android.graphics.Color; 7 | import android.graphics.DashPathEffect; 8 | import android.graphics.LinearGradient; 9 | import android.graphics.Paint; 10 | import android.graphics.Path; 11 | import android.graphics.Shader; 12 | import android.support.v4.content.ContextCompat; 13 | import android.util.AttributeSet; 14 | import android.util.DisplayMetrics; 15 | import android.view.View; 16 | import android.view.WindowManager; 17 | 18 | import com.wzh.linechart.R; 19 | 20 | 21 | /** 22 | * Created by zhihao.wen on 2016/11/18. 23 | */ 24 | public class LineView extends View { 25 | private int width = 200, height = 200; 26 | /** 27 | * 绘制坐标轴的画笔 28 | */ 29 | private Paint axesPaint; 30 | /** 31 | * 绘制图的时候画笔 32 | */ 33 | private Paint linePaint; 34 | 35 | private Path path; 36 | private Path p; 37 | 38 | private final int marginBorder = 80; 39 | private int[] data ; 40 | private String[] lables; 41 | /** 42 | * 存放横坐标的值 43 | */ 44 | private float x[]; 45 | /** 46 | * 存放纵坐标的值 47 | */ 48 | private float y[]; 49 | private int yMaxNum = 0; 50 | private float h1, h2; 51 | private int valueMax, valueMin; 52 | 53 | private DisplayMetrics dm; 54 | private Shader mShader; 55 | /** 56 | * shape的是底部渐变效果所用的 57 | */ 58 | private Paint shapePaint; 59 | private Path shapePath; 60 | /** 61 | * 文字的颜色 62 | */ 63 | private int textColor; 64 | /** 65 | * 折线的颜色 66 | */ 67 | private int lineColor; 68 | /** 69 | * 折线的粗细 70 | */ 71 | private float lineWidth; 72 | /** 73 | * 折线点的半径大小 74 | */ 75 | private float dotRadius; 76 | /** 77 | * 数据的放大因子,比如数据是2位数的就是10,2位数的就是100。根据数据来定 78 | */ 79 | private int dataFactor = 100; 80 | /** 81 | * 是否显示纵向的垂直线 82 | */ 83 | private boolean isShowColumnLine; 84 | /** 85 | * 是否显示横向的垂直线 86 | */ 87 | private boolean isShowRowLine; 88 | /** 89 | * 是否显示下面的渐变颜色 90 | */ 91 | private boolean isLinearGradient; 92 | /** 93 | * 垂直的虚线的颜色 94 | */ 95 | private int dashColor; 96 | private String yTitle = "y坐标" ; 97 | private String xTitle ="x坐标"; 98 | 99 | public LineView(Context context) { 100 | this(context, null); 101 | } 102 | 103 | public LineView(Context context, AttributeSet attrs) { 104 | this(context, attrs, 0); 105 | } 106 | 107 | public LineView(Context context, AttributeSet attrs, int defStyleAttr) { 108 | super(context, attrs, defStyleAttr); 109 | TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.LineView); 110 | textColor = ta.getColor(R.styleable.LineView_textColor, ContextCompat.getColor(getContext(), R.color.black)); 111 | lineColor = ta.getColor(R.styleable.LineView_lineColor, ContextCompat.getColor(getContext(), R.color.colorAccent)); 112 | lineWidth = ta.getDimension(R.styleable.LineView_lineWidth, 3); 113 | dotRadius = ta.getDimension(R.styleable.LineView_dotRadius, 10); 114 | dataFactor = ta.getInteger(R.styleable.LineView_dataFactor, 100); 115 | isShowColumnLine = ta.getBoolean(R.styleable.LineView_isShowColumnLine, false); 116 | isShowRowLine = ta.getBoolean(R.styleable.LineView_isShowRowLine, false); 117 | isLinearGradient = ta.getBoolean(R.styleable.LineView_isLinearGradient, true); 118 | dashColor = ta.getColor(R.styleable.LineView_dashColor, ContextCompat.getColor(getContext(), R.color.colorPrimary)); 119 | 120 | ta.recycle(); 121 | init(); 122 | } 123 | 124 | private void init() { 125 | dm = new DisplayMetrics(); 126 | WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); 127 | wm.getDefaultDisplay().getMetrics(dm); 128 | 129 | axesPaint = new Paint(); 130 | axesPaint.setColor(textColor); 131 | axesPaint.setAntiAlias(true); 132 | axesPaint.setTextSize(getResources().getDimension(R.dimen.font_smalll)); 133 | 134 | linePaint = new Paint(); 135 | linePaint.setColor(lineColor); 136 | linePaint.setAntiAlias(true); 137 | path = new Path(); 138 | p = new Path(); 139 | shapePath = new Path(); 140 | mShader = new LinearGradient(10, 0, 0, dm.heightPixels / 2, 141 | new int[]{Color.parseColor("#00ffffff"), Color.parseColor("#BF00e5ff")}, null, Shader.TileMode.MIRROR); 142 | shapePaint = new Paint(); 143 | shapePaint.setAntiAlias(true); 144 | } 145 | 146 | @Override 147 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 148 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 149 | int sizeWidth = MeasureSpec.getSize(widthMeasureSpec); 150 | int sizeHeight = MeasureSpec.getSize(heightMeasureSpec); 151 | int modeWidth = MeasureSpec.getMode(widthMeasureSpec); 152 | int modeHeight = MeasureSpec.getMode(heightMeasureSpec); 153 | if (modeWidth == MeasureSpec.EXACTLY) { 154 | width = sizeWidth; 155 | } 156 | if (modeHeight == MeasureSpec.EXACTLY) { 157 | height = Math.min(sizeHeight, sizeWidth); 158 | } 159 | setMeasuredDimension(width, height); 160 | } 161 | 162 | @Override 163 | protected void onDraw(Canvas canvas) { 164 | super.onDraw(canvas); 165 | if (data==null || lables==null){ 166 | return; 167 | } 168 | if (data.length==0 || lables.length==0){ 169 | return; 170 | } 171 | yMaxNum = Math.round(getMax() / dataFactor) + 1; 172 | x = new float[lables.length]; 173 | y = new float[data.length]; 174 | 175 | drawYAxes(canvas); 176 | drawXAxes(canvas); 177 | drawLine(canvas); 178 | } 179 | 180 | @Override 181 | protected void onAttachedToWindow() { 182 | super.onAttachedToWindow(); 183 | // postInvalidate(); 184 | 185 | } 186 | 187 | @Override 188 | protected void onDetachedFromWindow() { 189 | super.onDetachedFromWindow(); 190 | 191 | } 192 | 193 | /** 194 | * 绘制折现 195 | * 196 | * @param canvas 197 | */ 198 | private void drawLine(Canvas canvas) { 199 | for (int i = 0; i < y.length; i++) { 200 | /** 201 | * 重置画笔,绘制折线 202 | */ 203 | linePaint.setColor(lineColor); 204 | linePaint.setStyle(Paint.Style.FILL);//设置空 205 | linePaint.setShader(null); 206 | linePaint.setPathEffect(null); 207 | canvas.drawCircle(x[i], y[i], dotRadius, linePaint); 208 | /** 209 | * 绘制点上的值,如果是V字型的点就在底下显示,默认在下面 210 | */ 211 | if (i < y.length - 1 && i > 1) { 212 | if (y[i] > y[i - 1] && y[i] > y[i + 1]) { 213 | canvas.drawText(data[i] + "", x[i] - marginBorder / 3, y[i] + (marginBorder / 3) * 2, axesPaint); 214 | } else { 215 | canvas.drawText(data[i] + "", x[i] - marginBorder / 4, y[i] - (marginBorder / 3), axesPaint); 216 | } 217 | } else { 218 | canvas.drawText(data[i] + "", x[i] - marginBorder / 3, y[i] + (marginBorder / 3) * 2, axesPaint); 219 | } 220 | /** 221 | * 绘制折线 222 | */ 223 | if (i == 0) { 224 | shapePath.moveTo(x[i], y[i]); 225 | path.moveTo(x[i], y[i]); 226 | } else { 227 | shapePath.lineTo(x[i], y[i]); 228 | path.lineTo(x[i], y[i]); 229 | } 230 | linePaint.setStrokeWidth(lineWidth); 231 | linePaint.setStyle(Paint.Style.STROKE);//设置空 232 | canvas.drawPath(path, linePaint); 233 | 234 | /** 235 | * 绘制竖直方向的虚线 236 | */ 237 | linePaint.setStrokeWidth(dip2px(1)); 238 | if (isShowColumnLine) { 239 | DashPathEffect pathEffect = new DashPathEffect(new float[]{15, 15, 15, 15}, 1); 240 | linePaint.setPathEffect(pathEffect); 241 | linePaint.setColor(dashColor); 242 | p.reset(); 243 | p.moveTo(x[i], y[i]); 244 | p.lineTo((width / (lables.length + 1)) * (i + 1) + getPaddingLeft(), height - marginBorder - 20); 245 | canvas.drawPath(p, linePaint); 246 | } 247 | if (isShowRowLine) { 248 | /** 249 | * 横向的虚线 250 | */ 251 | DashPathEffect pathEffect = new DashPathEffect(new float[]{15, 15, 15, 15}, 1); 252 | linePaint.setPathEffect(pathEffect); 253 | linePaint.setColor(dashColor); 254 | p.reset(); 255 | p.moveTo(x[i], y[i]); 256 | p.lineTo(getPaddingLeft() + marginBorder + 20, y[i]); 257 | canvas.drawPath(p, linePaint); 258 | } 259 | } 260 | /** 261 | * 绘制下部分渐变颜色 262 | */ 263 | shapePath.lineTo((width / (lables.length + 1)) * (y.length) + getPaddingLeft(), height - marginBorder); 264 | shapePath.lineTo((width / (lables.length + 1)) + getPaddingLeft(), height - marginBorder); 265 | shapePath.close(); // 使这些点构成封闭的多边形 266 | if (isLinearGradient){ 267 | shapePaint.setShader(mShader); 268 | canvas.drawPath(shapePath, shapePaint); 269 | } 270 | } 271 | 272 | /** 273 | * 绘制y相关坐标 274 | * 275 | * @param canvas 276 | */ 277 | private void drawYAxes(Canvas canvas) { 278 | /** 279 | * 绘制Y纵坐标 280 | */ 281 | canvas.drawLine(getPaddingLeft() + marginBorder, height - marginBorder, getPaddingLeft() + marginBorder, marginBorder, axesPaint); 282 | for (int i = 0; i < yMaxNum; i++) { 283 | if (i == 0) { 284 | valueMax = (yMaxNum - i) * dataFactor; 285 | h2 = ((height - marginBorder) / (yMaxNum + 1)) * (i + 1); 286 | } else { 287 | canvas.drawLine(getPaddingLeft() + marginBorder, ((height - marginBorder) / (yMaxNum + 1)) * (i + 1), getPaddingLeft() + marginBorder + 20, ((height - marginBorder) / (yMaxNum + 1)) * (i + 1), axesPaint); 288 | canvas.drawText((yMaxNum - i) * dataFactor + "", getPaddingLeft() / 2, ((height - marginBorder * 4 / 5) / (yMaxNum + 1)) * (i + 1), axesPaint); 289 | } 290 | } 291 | for (int i = 0; i < data.length; i++) { 292 | y[i] = height - marginBorder - ((data[i] * 1.0f / (valueMax * 1.0f))) * (height - marginBorder - h2); 293 | } 294 | 295 | /** 296 | * 绘制小箭头 297 | */ 298 | canvas.drawLine(getPaddingLeft() + marginBorder - 20, marginBorder + 20, getPaddingLeft() + marginBorder, marginBorder, axesPaint); 299 | canvas.drawLine(getPaddingLeft() + marginBorder + 20, marginBorder + 20, getPaddingLeft() + marginBorder, marginBorder, axesPaint); 300 | /** 301 | * 绘制数据单位 302 | */ 303 | axesPaint.setTextSize(getResources().getDimension(R.dimen.font_smalll)); 304 | 305 | canvas.drawText(yTitle, getPaddingLeft() / 2, marginBorder / 2, axesPaint); 306 | } 307 | 308 | /** 309 | * 绘制x相关坐标 310 | * 311 | * @param canvas 312 | */ 313 | private void drawXAxes(Canvas canvas) { 314 | /** 315 | * 绘制X横坐标 316 | */ 317 | canvas.drawLine(getPaddingLeft() + marginBorder, height - marginBorder, width - getPaddingRight() / 2, height - marginBorder, axesPaint); 318 | 319 | for (int i = 0; i < lables.length; i++) { 320 | canvas.drawLine((width / (lables.length + 1)) * (i + 1) + getPaddingLeft(), height - marginBorder, (width / (lables.length + 1)) * (i + 1) + getPaddingLeft(), height - marginBorder - 20, axesPaint); 321 | canvas.drawText(lables[i], (width / (lables.length + 1)) * (i + 1) - marginBorder * 2 / 3 + getPaddingLeft(), height - marginBorder / 3, axesPaint); 322 | x[i] = (width / (lables.length + 1)) * (i + 1) + getPaddingLeft(); 323 | } 324 | canvas.drawText(xTitle, getPaddingLeft() / 2, height - marginBorder / 3, axesPaint); 325 | /** 326 | * 绘制小箭头 327 | */ 328 | canvas.drawLine(width - getPaddingRight() / 2 - 20, height - marginBorder - 20, width - getPaddingRight() / 2, height - marginBorder, axesPaint); 329 | canvas.drawLine(width - getPaddingRight() / 2 - 20, height - marginBorder + 20, width - getPaddingRight() / 2, height - marginBorder, axesPaint); 330 | } 331 | 332 | /** 333 | * 求最大值 334 | * 335 | * @return 336 | */ 337 | public int getMax() { 338 | int max = data[0]; 339 | for (int i = 1; i < data.length; i++) { 340 | if (max < data[i]) { 341 | max = data[i]; 342 | } 343 | } 344 | return max; 345 | } 346 | 347 | public void setLables(String[] lables) { 348 | this.lables = lables; 349 | postInvalidate(); 350 | } 351 | 352 | public void setData(int[] data) { 353 | this.data = data; 354 | postInvalidate(); 355 | } 356 | 357 | public void setyTitle(String yTitle) { 358 | this.yTitle = yTitle; 359 | postInvalidate(); 360 | } 361 | 362 | public void setxTitle(String xTitle) { 363 | this.xTitle = xTitle; 364 | postInvalidate(); 365 | } 366 | 367 | public void setDataFactor(int dataFactor) { 368 | this.dataFactor = dataFactor; 369 | postInvalidate(); 370 | } 371 | 372 | /** 373 | * 根据手机的分辨率从 dp 的单位 转成为 px(像素) 374 | */ 375 | private int dip2px(float dpValue) { 376 | return (int) (dpValue * dm.density + 0.5f); 377 | } 378 | } 379 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 18 | 32 | 33 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wenzhihao123/Android-LineChartView-master/0a74b3dab239d8021d2ea13012d4b683a8bc5338/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wenzhihao123/Android-LineChartView-master/0a74b3dab239d8021d2ea13012d4b683a8bc5338/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wenzhihao123/Android-LineChartView-master/0a74b3dab239d8021d2ea13012d4b683a8bc5338/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wenzhihao123/Android-LineChartView-master/0a74b3dab239d8021d2ea13012d4b683a8bc5338/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wenzhihao123/Android-LineChartView-master/0a74b3dab239d8021d2ea13012d4b683a8bc5338/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-xhdpi/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | 50dp 7 | 25dp 8 | 13sp 9 | 15sp 10 | 17sp 11 | 20sp 12 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/values-xxhdpi/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 50dp 6 | 25dp 7 | 8 | -------------------------------------------------------------------------------- /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 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | #999999 7 | #e6e6e6 8 | #666666 9 | 10 | #e9e9e9 11 | #ffffff 12 | #000000 13 | 14 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | 45dp 7 | 20dp 8 | 11sp 9 | 13sp 10 | 15sp 11 | 18sp 12 | 22sp 13 | 14 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Android-LineChartView-master 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/wzh/linechart/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.wzh.linechart; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:2.2.2' 9 | 10 | // NOTE: Do not place your application dependencies here; they belong 11 | // in the individual module build.gradle files 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | jcenter() 18 | } 19 | } 20 | 21 | task clean(type: Delete) { 22 | delete rootProject.buildDir 23 | } 24 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wenzhihao123/Android-LineChartView-master/0a74b3dab239d8021d2ea13012d4b683a8bc5338/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 28 10:00:20 PST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | --------------------------------------------------------------------------------