Skip to content

Commit

Permalink
new feature to v1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
linjiang committed May 21, 2017
1 parent b4f09f5 commit b454725
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 19 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ suitline是一个小巧且高效的线性图表组件。

![image](http://note.youdao.com/yws/api/personal/file/C24FFB56C0F944E9AF4332357F33F09C?method=download&shareKey=156bb9a08b3f4f4d6ae8f5ddac4607fb) ![image](http://note.youdao.com/yws/api/personal/file/CC73337B96A94DA493D6B8E27F9103D7?method=download&shareKey=d0115a84957acf8bba265058460a63d9)

## 更新历史

- 2017/5/21(v1.1.0)

1. 修复BUG:#1#7
2. 新增FILL形态时显示图表上边框线的属性coverLine,具体效果已添加至demo,通过方法`setCoverLine(boolean enable)`开启或关闭;


## 功能特性
suitline基于实用性目的而打造,相较于其它图表库,suitlines在多线段、性能体验以及视觉反馈等几个方面进行了支持和优化,使其更适合用于实际项目中。suitLines的所有特性如下:

Expand All @@ -33,7 +41,7 @@ suitline基于实用性目的而打造,相较于其它图表库,suitlines在
### 1.集成
- 第一种:通过build.gradle方式集成
```groovy
compile 'tech.linjiang:suitlines:1.0.0'
compile 'tech.linjiang:suitlines:1.1.0'
```

- 第二种:直接下载源文件到项目。(**推荐**
Expand Down Expand Up @@ -74,6 +82,7 @@ maxOfVisible | / | 一组数据在可见区域中的最大可见点数,至少>
countOfY | / | y轴刻度数,至少>=1
/ | setLineSize | 设置line在非填充形态时的大小
/ | setLineForm | 设置line的形态:是否填充,默认为false
/ | setCoverLine | 设置当line是FILL形态时,是否现在上边框线,默认false

### 3.填充数据

Expand Down
20 changes: 10 additions & 10 deletions app/src/main/java/tech/linjiang/suitlines/simple/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public void onBtnClick(View view) {
suitLines.anim();
}

private boolean enable;
public void onBtnClick1(View view) {
suitLines.setCoverLine(enable = !enable);
}

public void onBtnClick13(View view) {
int size = new SecureRandom().nextInt(4);
int[] colors = new int[size == 0 ? (size = 1) : size];
for (int i = 0; i < size; i++) {
colors[i] = color[new SecureRandom().nextInt(4)];
}
int[] colors = new int[2];
colors[0] = color[new SecureRandom().nextInt(7)];
colors[1] = Color.WHITE;
suitLines.setDefaultOneLineColor(colors);
}

Expand Down Expand Up @@ -75,11 +75,11 @@ public void onBtnClick8(View view) {
}

public void onBtnClick9(View view) {
suitLines.setEdgeEffectColor(color[new SecureRandom().nextInt(4)]);
suitLines.setEdgeEffectColor(color[new SecureRandom().nextInt(7)]);
}

public void onBtnClick10(View view) {
suitLines.setXyColor(color[new SecureRandom().nextInt(4)]);
suitLines.setXyColor(color[new SecureRandom().nextInt(7)]);
}

private float textSize = 8;
Expand All @@ -98,10 +98,10 @@ public void onBtnClick14(View view) {
suitLines.disableClickHint();
}
public void onBtnClick15(View view) {
suitLines.setHintColor(color[new SecureRandom().nextInt(4)]);
suitLines.setHintColor(color[new SecureRandom().nextInt(7)]);
}

private int[] color = {Color.RED, Color.GRAY, 0xFFF76055, 0xFF9B3655, 0xFFF7A055};
private int[] color = {Color.RED, Color.GRAY, Color.BLACK, Color.BLUE, 0xFFF76055, 0xFF9B3655, 0xFFF7A055};

public void init(int count) {
if (count <= 0) {
Expand All @@ -122,7 +122,7 @@ public void init(int count) {
for (int i = 0; i < 50; i++) {
lines.add(new Unit(new SecureRandom().nextInt(128), "" + i));
}
builder.add(lines, new int[]{color[new SecureRandom().nextInt(4)], color[new SecureRandom().nextInt(4)], color[new SecureRandom().nextInt(4)]});
builder.add(lines, new int[]{color[new SecureRandom().nextInt(7)], Color.WHITE});
}
builder.build(suitLines, true);

Expand Down
16 changes: 12 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
android:orientation="vertical">

<tech.linjiang.suitlines.SuitLines
xmlns:line="http://schemas.android.com/apk/res-auto"
Expand Down Expand Up @@ -66,12 +65,13 @@
</LinearLayout>

<TextView
android:text="|| 设置Line的形态/风格/类型/颜色:"
android:text="|| 设置Line的形态/fill形态下的边缘线/风格/类型/颜色:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="60dp"
android:orientation="horizontal">

<Button
Expand All @@ -81,6 +81,14 @@
android:onClick="onBtnClick5"
android:text="Form" />

<Button
android:textSize="13sp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onBtnClick1"
android:text="cover-line" />

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
Expand Down
Empty file modified gradlew
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ publish {
userOrg = 'yanglssc'
groupId = 'tech.linjiang'
artifactId = 'suitlines'
publishVersion = '1.0.0'
publishVersion = '1.1.0'
desc = 'a tiny and effecient line-chart for android library.'
website = 'https://github.com/whataa/SuitLines'
}
Expand Down
62 changes: 59 additions & 3 deletions lib/src/main/java/tech/linjiang/suitlines/SuitLines.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public SuitLines(Context context, AttributeSet attrs, int defStyleAttr) {
basePaint.setColor(defaultLineColor[0]);
basePaint.setStyle(Paint.Style.STROKE);
basePaint.setStrokeWidth(4);
coverLinePaint.setStyle(Paint.Style.STROKE);
coverLinePaint.setStrokeWidth(Util.dip2px(5));
setLineStyle(SOLID);
xyPaint.setTextSize(Util.size2sp(defaultXySize, getContext()));
xyPaint.setColor(defaultXyColor);
Expand Down Expand Up @@ -130,7 +132,7 @@ private void initOptionalState(Context ctx, AttributeSet attrs) {
/**
* 默认画笔的颜色,索引0位置为画笔颜色,整个数组为shader颜色
*/
private int[] defaultLineColor = {Color.RED, 0xFFF35E54, Color.YELLOW};
private int[] defaultLineColor = {Color.RED, Color.YELLOW, Color.WHITE};
private int hintColor = Color.RED;
/**
* xy轴文字颜色和大小
Expand All @@ -142,6 +144,11 @@ private void initOptionalState(Context ctx, AttributeSet attrs) {
*/
private List<Paint> paints = new ArrayList<>();
private List<Path> paths = new ArrayList<>();
private Path tmpPath = new Path();
/**
* fill形态下时,边缘线画笔
*/
Paint coverLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
/**
* 约定:如果需要实现多组数据,那么每组数据的长度必须相同!
* 多组数据的数据池;
Expand Down Expand Up @@ -242,6 +249,11 @@ private void initOptionalState(Context ctx, AttributeSet attrs) {
*/
private boolean needEdgeEffect = true;
private int edgeEffectColor = Color.GRAY;
/**
* fill形态下,是否绘制边缘线
* 若开启该特性,闭合路径的操作将延迟到绘制时
*/
private boolean needCoverLine;
/**
* 点击是否弹出额外信息
*/
Expand Down Expand Up @@ -661,7 +673,7 @@ private void drawLines(Canvas canvas, int startIndex, int endIndex) {
(previous.getXY().x + current.getXY().x) / 2, curY,
current.getXY().x, curY);
}
if (isLineFill() && i == endIndex) {
if (!needCoverLine && isLineFill() && i == endIndex) {
paths.get(j).lineTo(current.getXY().x, linesArea.bottom);
paths.get(j).lineTo(datas.get(j).get(startIndex).getXY().x, linesArea.bottom);
paths.get(j).close();
Expand All @@ -678,11 +690,34 @@ private void drawLines(Canvas canvas, int startIndex, int endIndex) {
private void drawExsitDirectly(Canvas canvas) {
// TODO 需要优化
for (int j = 0; j < datas.size(); j++) {
canvas.drawPath(paths.get(j), paints.get(j));
if (!isLineFill() || !needCoverLine) {
canvas.drawPath(paths.get(j), paints.get(j));
} else {
if (needCoverLine) {
coverLinePaint.setColor(Util.tryGetStartColorOfLinearGradient((LinearGradient) paints.get(j).getShader()));
canvas.save();
canvas.clipRect(linesArea.left - offset, linesArea.top, linesArea.right - offset, linesArea.bottom);
// 由于paint的stroke是双边,所以下一个draw不会覆盖当前已经的draw
canvas.drawPath(paths.get(j), coverLinePaint);
canvas.restore();
tmpPath.set(paths.get(j));
tmpPath.lineTo(datas.get(j).get(suitEdge[1]).getXY().x, linesArea.bottom);
tmpPath.lineTo(datas.get(j).get(suitEdge[0]).getXY().x, linesArea.bottom);
tmpPath.close();
canvas.drawPath(tmpPath, paints.get(j));
tmpPath.reset();
}
}
}
// TODO 画点
}

private float calcReferenceLengthOf(int j) {
return linesArea.height() * 2 - datas.get(j).get(suitEdge[0]).getXY().y
- datas.get(j).get(suitEdge[1]).getXY().y
+ datas.get(j).get(suitEdge[1]).getXY().x - datas.get(j).get(suitEdge[0]).getXY().x;
}

/**
* 画提示文本和辅助线
* @param canvas
Expand Down Expand Up @@ -1050,6 +1085,27 @@ protected void onDetachedFromWindow() {

///APIs/////////////////////////////////////////////////////////////////////////////////////////

/**
* 在fill形态时,是否在图表上边缘绘制line
* @param enable true表示需要,反正不需要
*/
public void setCoverLine(boolean enable) {
needCoverLine = enable;
forceToDraw = true;
postInvalidate();
}

/**
* 在fill形态时,指定在图表上边缘绘制line的宽度,该方法会开启needCoverLine特性
* @param withdp 宽度
*/
public void setCoverLine(float withdp) {
needCoverLine = true;
coverLinePaint.setStrokeWidth(Util.dip2px(withdp) * 2);
forceToDraw = true;
postInvalidate();
}

/**
* 设置默认一条line时的颜色
* @param colors 默认为defaultLineColor
Expand Down
21 changes: 21 additions & 0 deletions lib/src/main/java/tech/linjiang/suitlines/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.RectF;
Expand Down Expand Up @@ -75,4 +77,23 @@ static void trySetColorForEdgeEffect(EdgeEffect edgeEffect, int color) {
} catch (Exception ignored) {
}
}

static int tryGetStartColorOfLinearGradient(LinearGradient gradient) {
try {
Field field = LinearGradient.class.getDeclaredField("mColors");
field.setAccessible(true);
int[] colors = (int[]) field.get(gradient);
return colors[0];
} catch (Exception e) {
e.printStackTrace();
try {
Field field = LinearGradient.class.getDeclaredField("mColor0");
field.setAccessible(true);
return (int) field.get(gradient);
} catch (Exception e1) {
e1.printStackTrace();
}
}
return Color.BLACK;
}
}

0 comments on commit b454725

Please sign in to comment.