) chartData;
+ this.mHalfWidthCallback = callback;
+ count = mDataSet.size();
+ if (callback == null) {
+ mTopMaxLineHalf = mLastLineOffset + getDefaultHalfWidthOffset() * count;
} else {
- mCustomLabel.drawText(canvas, mPaintLabel, labelX, labelY, i);
+ halfArrays = new float[count];
+ float max = 0;
+ float halfWidth = 0;
+ //将所有的宽度数据组装进数组中
+ 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;
+ }
+ }
+ mTopMaxLineHalf = max + mLastLineOffset;
+ }
+ invalidate();
+ }
+
+ /*
+ * 默认漏斗宽度变化策略
+ * */
+ private float getDefaultHalfWidthOffset() {
+ if (count <= 4) {
+ return dip2px(mContext, 17);
+ } else if (count <= 6) {
+ return dip2px(mContext, 13);
+ } else if (count <= 8) {
+ return dip2px(mContext, 10);
+ } else if (count <= 10) {
+ return dip2px(mContext, 7);
+ } else {
+ return dip2px(mContext, 5);
}
}
+
}
diff --git a/funnellib/src/main/java/jie/com/funnellib/HalfWidthCallback.java b/funnellib/src/main/java/jie/com/funnellib/HalfWidthCallback.java
new file mode 100644
index 0000000..e41e47f
--- /dev/null
+++ b/funnellib/src/main/java/jie/com/funnellib/HalfWidthCallback.java
@@ -0,0 +1,16 @@
+package jie.com.funnellib;
+
+/**
+ * Created by hj on 2019/2/25.
+ * 说明:自定义漏斗宽度变化策略
+ */
+public interface HalfWidthCallback {
+ /**
+ * 自定义漏斗宽度变化策略
+ * @param halfWidth 上一个梯形的宽度,第一个为0
+ * @param count 漏斗梯形个数
+ * @param i 梯形下标
+ * @return 当前梯形的宽度,单位:PX
+ */
+ float getHalfStrategy(float halfWidth,int count,int i);
+}
diff --git a/funnellib/src/main/java/jie/com/funnellib/IFunnelData.java b/funnellib/src/main/java/jie/com/funnellib/IFunnelData.java
index a346891..758d347 100644
--- a/funnellib/src/main/java/jie/com/funnellib/IFunnelData.java
+++ b/funnellib/src/main/java/jie/com/funnellib/IFunnelData.java
@@ -2,10 +2,19 @@
/**
* Created by hj on 2019/2/22.
- * 说明:
+ * 说明:获取漏斗数据源接口
*/
public interface IFunnelData {
- String getValue();
+
+ /**
+ * 颜色
+ * @return color
+ */
int getColor();
+
+ /**
+ * 描述
+ * @return string
+ */
String getLabel();
}
diff --git a/funnellib/src/main/java/jie/com/funnellib/Util.java b/funnellib/src/main/java/jie/com/funnellib/Util.java
index 5e3e9bd..77c289f 100644
--- a/funnellib/src/main/java/jie/com/funnellib/Util.java
+++ b/funnellib/src/main/java/jie/com/funnellib/Util.java
@@ -11,18 +11,18 @@
* Created by hj on 2019/2/22.
* 说明:
*/
-class Util {
+public class Util {
/**
* convert sp to its equivalent px
*
* 将sp转换为px
*/
- static float sp2px(Context context, float spValue) {
+ public static float sp2px(Context context, float spValue) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spValue,context.getResources().getDisplayMetrics());
}
/** dip转换px */
- static float dip2px(Context context,int dip) {
+ public static float dip2px(Context context,int dip) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip,context.getResources().getDisplayMetrics());
}
@@ -30,7 +30,7 @@ static float dip2px(Context context,int dip) {
/*
* 禁止硬件加速
* */
- static void disableHardwareAccelerated(View view) {
+ public static void disableHardwareAccelerated(View view) {
if (view == null) {
return;
}
@@ -46,12 +46,24 @@ static void disableHardwareAccelerated(View view) {
* @param paint 画笔
* @return 高度
*/
- static float getPaintFontHeight(Paint paint)
+ public static float getPaintFontHeight(Paint paint)
{
Paint.FontMetrics fm = paint.getFontMetrics();
return (float) Math.ceil(fm.descent - fm.ascent);
}
+ /**
+ * 得到一段文本的宽度
+ * @param paint 画笔
+ * @param str 文本
+ * @return 文本宽度
+ */
+ public static float getTextWidth(Paint paint, String str)
+ {
+ if(str.length() == 0) return 0.0f;
+ return paint.measureText(str, 0, str.length());
+ }
+
/**
* 减法运算
*
@@ -59,7 +71,7 @@ static float getPaintFontHeight(Paint paint)
* @param v2 参数2
* @return 运算结果
*/
- static float sub(float v1, float v2) {
+ public static float sub(float v1, float v2) {
BigDecimal bgNum1 = new BigDecimal(Float.toString(v1));
BigDecimal bgNum2 = new BigDecimal(Float.toString(v2));
return bgNum1.subtract(bgNum2).floatValue();