Skip to content

Commit

Permalink
添加一个线的颜色属性。调整绘制顺序
Browse files Browse the repository at this point in the history
  • Loading branch information
Jay-huangjie committed Feb 25, 2019
1 parent 2ef5aca commit d44eed3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
48 changes: 23 additions & 25 deletions funnellib/src/main/java/jie/com/funnellib/FunnelView.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class FunnelView extends View {
private float mBottom = 0.0f;
private Context mContext;
private float mLineWidth; //线的长度
private int mLineColor;
private float mLineTextSpace; //字与线的间距
private float mLastLineOffset; //最底部从中心点向两边的偏移量
private float mTotalHeight; //单个梯形的目标高度
Expand Down Expand Up @@ -136,6 +137,7 @@ private void initView(Context context, AttributeSet attributeSet) {
if (attributeSet != null) {
TypedArray ta = context.obtainStyledAttributes(attributeSet, R.styleable.FunnelView);
mLineWidth = ta.getDimension(R.styleable.FunnelView_lineWidth, dip2px(context, 12));
mLineColor = ta.getColor(R.styleable.FunnelView_lineColor, -1);
mLineTextSpace = ta.getDimension(R.styleable.FunnelView_lineTextSpace, dip2px(context, 7));
mLastLineOffset = ta.getDimension(R.styleable.FunnelView_lastLineOffset, dip2px(context, 20));
mTotalHeight = ta.getDimension(R.styleable.FunnelView_totalHeight, dip2px(context, 30));
Expand Down Expand Up @@ -265,7 +267,7 @@ private void renderPlotDesc(Canvas canvas, float cx, float funnelHeight) {
pStart.x = cx - mPlotWidth / 2;
pStop.x = cx + mPlotWidth / 2;
pStart.y = pStop.y = mPlotBottom;
float labelY = 0.f;
float lineY;
Path path = new Path();
for (int i = 0; i < count; i++) {
IFunnelData d = mDataSet.get(i);
Expand All @@ -285,39 +287,38 @@ private void renderPlotDesc(Canvas canvas, float cx, float funnelHeight) {
halfWidth = halfArrays[i];
}
bottomY = sub(mPlotBottom, i * funnelHeight);
labelY = bottomY - funnelHeight / 2;
lineY = bottomY - funnelHeight / 2;

pStart.x = cx - mLastLineOffset - halfWidth;
pStart.y = bottomY - funnelHeight;
pStop.x = cx + mLastLineOffset + halfWidth;
pStop.y = bottomY - funnelHeight;
path.lineTo(pStop.x, pStop.y); //画右边的线
path.lineTo(pStart.x, pStart.y); //画左边的线

//画线
mPaintLabelLine.setColor(mLineColor == -1 ? d.getColor() : mLineColor);
float lineX = pStop.x + mLineWidth;
canvas.drawLine(cx, lineY, lineX, lineY, mPaintLabelLine);

path.lineTo(pStop.x, pStop.y); //画梯形右边的线
path.lineTo(pStart.x, pStart.y); //画梯形左边的线
mPaint.setColor(d.getColor());
path.close();
canvas.drawPath(path, mPaint);
if (i != count - 1) { //绘制中间的线
canvas.drawLine(pStart.x, pStart.y, pStop.x, pStop.y, mPaintFunnelLine);
}
renderLabels(canvas, d, cx, labelY, d.getColor(), (int) (pStop.x - cx), i);
}
}

//画线和字
private void renderLabels(Canvas canvas, IFunnelData data, float cx, float y, int color, int halfWidth, int i) {
if (data == null) return;
mPaintLabelLine.setColor(color);
float lineX = cx + halfWidth + mLineWidth;
canvas.drawLine(cx, y, lineX, y, mPaintLabelLine);
float labelX = lineX + mLineTextSpace;
float labelY = y + getPaintFontHeight(mPaintLabel) / 3;
if (mCustomLabelCallback == null) {
canvas.drawText(data.getLabel(), labelX, labelY, mPaintLabel);
} else {
mCustomLabelCallback.drawText(canvas, mPaintLabel, labelX, labelY, i);
//绘制描述文字
float labelX = lineX + mLineTextSpace;
float labelY = lineY + getPaintFontHeight(mPaintLabel) / 3;
if (mCustomLabelCallback == null) {
canvas.drawText(d.getLabel(), labelX, labelY, mPaintLabel);
} else {
mCustomLabelCallback.drawText(canvas, mPaintLabel, labelX, labelY, i);
}
}
}


/*
* 设置线的宽度
* */
Expand Down Expand Up @@ -369,11 +370,8 @@ public <T extends IFunnelData> void setChartData(@NonNull List<T> chartData, Hal
for (int i = 0; i < count; i++) {
halfWidth = callback.getHalfStrategy(halfWidth, count, i);
halfArrays[i] = halfWidth;
}
//找出其中的最大值,此值也就是漏斗的最大宽度(不包括线和描述文字)
for (float value : halfArrays) {
if (max < value) {
max = value;
if (max < halfWidth) {
max = halfWidth; //找出其中的最大值,此值也就是漏斗的最大宽度(不包括线和描述文字)
}
}
mTopMaxLineHalf = max + mLastLineOffset;
Expand Down
1 change: 1 addition & 0 deletions funnellib/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<declare-styleable name="FunnelView">
<attr name="lineWidth" format="dimension"/>
<attr name="lineTextSpace" format="dimension"/>
<attr name="lineColor" format="color"/>
<attr name="lastLineOffset" format="dimension"/>
<attr name="totalHeight" format="dimension"/>
<attr name="funnelLineColor" format="color"/>
Expand Down

0 comments on commit d44eed3

Please sign in to comment.