Commit 1dffc346557ed29454d9376312a3f529b67a94f4
1 parent
0cbfbb68
Exists in
yxb_dev
and in
2 other branches
no message
Showing
10 changed files
with
596 additions
and
5 deletions
Show diff stats
.idea/misc.xml
| @@ -24,7 +24,7 @@ | @@ -24,7 +24,7 @@ | ||
| 24 | </value> | 24 | </value> |
| 25 | </option> | 25 | </option> |
| 26 | </component> | 26 | </component> |
| 27 | - <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK"> | 27 | + <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK"> |
| 28 | <output url="file://$PROJECT_DIR$/build/classes" /> | 28 | <output url="file://$PROJECT_DIR$/build/classes" /> |
| 29 | </component> | 29 | </component> |
| 30 | <component name="ProjectType"> | 30 | <component name="ProjectType"> |
app/src/main/java/com/shunzhi/parent/ui/fragment/report/ReportDetialFragment.java
| @@ -9,11 +9,15 @@ import android.view.View; | @@ -9,11 +9,15 @@ import android.view.View; | ||
| 9 | 9 | ||
| 10 | import com.share.mvpsdk.base.BasePresenter; | 10 | import com.share.mvpsdk.base.BasePresenter; |
| 11 | import com.share.mvpsdk.base.fragment.BaseMVPCompatFragment; | 11 | import com.share.mvpsdk.base.fragment.BaseMVPCompatFragment; |
| 12 | +import com.share.mvpsdk.utils.DateUtils; | ||
| 13 | +import com.share.mvpsdk.view.chartview.ChartView; | ||
| 14 | +import com.share.mvpsdk.view.chartview.data.InputData; | ||
| 12 | import com.shunzhi.parent.R; | 15 | import com.shunzhi.parent.R; |
| 13 | import com.shunzhi.parent.adapter.DeyuDetialAdapter; | 16 | import com.shunzhi.parent.adapter.DeyuDetialAdapter; |
| 14 | import com.shunzhi.parent.bean.report.DeyuDetialBean; | 17 | import com.shunzhi.parent.bean.report.DeyuDetialBean; |
| 15 | 18 | ||
| 16 | import java.util.ArrayList; | 19 | import java.util.ArrayList; |
| 20 | +import java.util.Date; | ||
| 17 | import java.util.List; | 21 | import java.util.List; |
| 18 | 22 | ||
| 19 | /** | 23 | /** |
| @@ -25,6 +29,8 @@ public class ReportDetialFragment extends BaseMVPCompatFragment { | @@ -25,6 +29,8 @@ public class ReportDetialFragment extends BaseMVPCompatFragment { | ||
| 25 | private RecyclerView recycle_deyu; | 29 | private RecyclerView recycle_deyu; |
| 26 | private DeyuDetialAdapter deyuDetialAdapter; | 30 | private DeyuDetialAdapter deyuDetialAdapter; |
| 27 | List<DeyuDetialBean>list=new ArrayList<>(); | 31 | List<DeyuDetialBean>list=new ArrayList<>(); |
| 32 | + | ||
| 33 | + private ChartView chartView; | ||
| 28 | @NonNull | 34 | @NonNull |
| 29 | @Override | 35 | @Override |
| 30 | public BasePresenter initPresenter() { | 36 | public BasePresenter initPresenter() { |
| @@ -39,6 +45,7 @@ public class ReportDetialFragment extends BaseMVPCompatFragment { | @@ -39,6 +45,7 @@ public class ReportDetialFragment extends BaseMVPCompatFragment { | ||
| 39 | @Override | 45 | @Override |
| 40 | public void initUI(View view, @Nullable Bundle savedInstanceState) { | 46 | public void initUI(View view, @Nullable Bundle savedInstanceState) { |
| 41 | recycle_deyu=view.findViewById(R.id.recycle_deyu); | 47 | recycle_deyu=view.findViewById(R.id.recycle_deyu); |
| 48 | + chartView=view.findViewById(R.id.chartView); | ||
| 42 | recycle_deyu.setLayoutManager(new LinearLayoutManager(getActivity())); | 49 | recycle_deyu.setLayoutManager(new LinearLayoutManager(getActivity())); |
| 43 | deyuDetialAdapter=new DeyuDetialAdapter(getActivity()); | 50 | deyuDetialAdapter=new DeyuDetialAdapter(getActivity()); |
| 44 | DeyuDetialBean deyuDetialBean=new DeyuDetialBean(); | 51 | DeyuDetialBean deyuDetialBean=new DeyuDetialBean(); |
| @@ -48,6 +55,20 @@ public class ReportDetialFragment extends BaseMVPCompatFragment { | @@ -48,6 +55,20 @@ public class ReportDetialFragment extends BaseMVPCompatFragment { | ||
| 48 | deyuDetialAdapter.addAll(list); | 55 | deyuDetialAdapter.addAll(list); |
| 49 | recycle_deyu.setAdapter(deyuDetialAdapter); | 56 | recycle_deyu.setAdapter(deyuDetialAdapter); |
| 50 | 57 | ||
| 58 | + initChartViews(); | ||
| 59 | + | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + private void initChartViews() { | ||
| 63 | + | ||
| 64 | + chartView.setColors(R.color.textColor,R.color.huodong_blue,R.color.gray); | ||
| 65 | + List<InputData> inputDataList=new ArrayList<>(); | ||
| 66 | + for (int i = 0; i < 10; i++) { | ||
| 67 | + InputData inputData=new InputData((new Date().getTime()-(10-i)*24*60*60*1000),10*i); | ||
| 68 | + inputDataList.add(inputData); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + chartView.setChartPaths(inputDataList); | ||
| 51 | 72 | ||
| 52 | } | 73 | } |
| 53 | } | 74 | } |
app/src/main/res/layout/fragment_report_detial.xml
| @@ -160,12 +160,19 @@ | @@ -160,12 +160,19 @@ | ||
| 160 | 160 | ||
| 161 | </LinearLayout> | 161 | </LinearLayout> |
| 162 | 162 | ||
| 163 | - <LinearLayout | 163 | + <FrameLayout |
| 164 | android:layout_width="match_parent" | 164 | android:layout_width="match_parent" |
| 165 | - android:layout_height="wrap_content" | 165 | + android:layout_height="150dp" |
| 166 | android:layout_marginTop="@dimen/size_dp_10"> | 166 | android:layout_marginTop="@dimen/size_dp_10"> |
| 167 | 167 | ||
| 168 | + <com.share.mvpsdk.view.chartview.ChartView | ||
| 169 | + android:layout_width="match_parent" | ||
| 170 | + android:layout_height="match_parent" | ||
| 171 | + android:id="@+id/chartView" | ||
| 172 | + /> | ||
| 173 | + | ||
| 168 | <TextView | 174 | <TextView |
| 175 | + android:visibility="gone" | ||
| 169 | android:layout_width="match_parent" | 176 | android:layout_width="match_parent" |
| 170 | android:layout_height="200dp" | 177 | android:layout_height="200dp" |
| 171 | android:text="图表控件" | 178 | android:text="图表控件" |
| @@ -174,7 +181,7 @@ | @@ -174,7 +181,7 @@ | ||
| 174 | android:textColor="@color/md_red_500" | 181 | android:textColor="@color/md_red_500" |
| 175 | android:background="@color/huodong_blue" /> | 182 | android:background="@color/huodong_blue" /> |
| 176 | 183 | ||
| 177 | - </LinearLayout> | 184 | + </FrameLayout> |
| 178 | 185 | ||
| 179 | 186 | ||
| 180 | <android.support.v7.widget.RecyclerView | 187 | <android.support.v7.widget.RecyclerView |
mvpsdk/src/main/java/com/share/mvpsdk/utils/DateUtils.java
| @@ -54,7 +54,10 @@ public class DateUtils { | @@ -54,7 +54,10 @@ public class DateUtils { | ||
| 54 | return str; | 54 | return str; |
| 55 | } | 55 | } |
| 56 | 56 | ||
| 57 | - | 57 | + public static String format(long millis){ |
| 58 | + SimpleDateFormat format = new SimpleDateFormat("dd", Locale.getDefault()); | ||
| 59 | + return format.format(millis); | ||
| 60 | + } | ||
| 58 | 61 | ||
| 59 | /** | 62 | /** |
| 60 | * 获取日期 PATTERN_DATE 部分 | 63 | * 获取日期 PATTERN_DATE 部分 |
| @@ -247,4 +250,5 @@ public class DateUtils { | @@ -247,4 +250,5 @@ public class DateUtils { | ||
| 247 | public static String calendar2str(Calendar calendar, String format) { | 250 | public static String calendar2str(Calendar calendar, String format) { |
| 248 | return date2str(calendar.getTime(), format); | 251 | return date2str(calendar.getTime(), format); |
| 249 | } | 252 | } |
| 253 | + | ||
| 250 | } | 254 | } |
mvpsdk/src/main/java/com/share/mvpsdk/utils/ValueUtils.java
0 → 100644
| @@ -0,0 +1,181 @@ | @@ -0,0 +1,181 @@ | ||
| 1 | +package com.share.mvpsdk.utils; | ||
| 2 | + | ||
| 3 | +import android.support.annotation.NonNull; | ||
| 4 | +import android.support.annotation.Nullable; | ||
| 5 | +import android.support.v4.app.NotificationCompat; | ||
| 6 | +import android.util.Log; | ||
| 7 | + | ||
| 8 | + | ||
| 9 | +import com.share.mvpsdk.view.chartview.data.Chart; | ||
| 10 | +import com.share.mvpsdk.view.chartview.data.DrawData; | ||
| 11 | +import com.share.mvpsdk.view.chartview.data.InputData; | ||
| 12 | + | ||
| 13 | +import java.util.ArrayList; | ||
| 14 | +import java.util.List; | ||
| 15 | +import java.util.ListIterator; | ||
| 16 | +import java.util.concurrent.TimeUnit; | ||
| 17 | + | ||
| 18 | +public class ValueUtils { | ||
| 19 | + | ||
| 20 | + public static long getCorrectedMaxValue(long maxValue) { | ||
| 21 | + for (long value = maxValue; value >= Chart.CHART_PART_VALUE; value--) { | ||
| 22 | + if (isRightValue(value)) { | ||
| 23 | + return value; | ||
| 24 | + } | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + return maxValue; | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + public static long max(@Nullable List<InputData> dataList) { | ||
| 31 | + long maxValue = 0; | ||
| 32 | + | ||
| 33 | + if (dataList == null || dataList.isEmpty()) { | ||
| 34 | + return maxValue; | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + for (InputData data : dataList) { | ||
| 38 | + if (data.valueY > maxValue) { | ||
| 39 | + maxValue = data.valueY; | ||
| 40 | + } | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + return maxValue; | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + private static boolean isRightValue(long value) { | ||
| 47 | + long valueResidual = value % Chart.CHART_PART_VALUE; | ||
| 48 | + return valueResidual == 0; | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + @NotificationCompat.NotificationVisibility | ||
| 52 | + public static List<DrawData> getDrawData(@Nullable Chart chart) { | ||
| 53 | + if (chart == null || chart.getInputDataList().isEmpty()) { | ||
| 54 | + return new ArrayList<>(); | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + List<InputData> dataList = chart.getInputDataList(); | ||
| 58 | + correctDataListSize(dataList);//移除多余的数据 | ||
| 59 | + return createDrawDataList(chart, createValueList(dataList)); | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + private static void correctDataListSize(@NonNull List<InputData> dataList) { | ||
| 63 | + if (dataList.size() < Chart.MAX_ITEMS_COUNT) { | ||
| 64 | + addLackingItems(dataList); | ||
| 65 | + | ||
| 66 | + } else if (dataList.size() > Chart.MAX_ITEMS_COUNT) { | ||
| 67 | + removeExcessItems(dataList); | ||
| 68 | + } | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + private static void addLackingItems(@NonNull List<InputData> dataList) { | ||
| 72 | + for (int i = dataList.size(); i < Chart.MAX_ITEMS_COUNT; i++) { | ||
| 73 | + long millis = dataList.get(0).valueX - TimeUnit.DAYS.toMillis(1); | ||
| 74 | + if (millis < 0) { | ||
| 75 | + millis = 0; | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + dataList.add(0, new InputData(0, millis)); | ||
| 79 | + } | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + private static void removeExcessItems(@NonNull List<InputData> dataList) { | ||
| 83 | + for (ListIterator<InputData> iterator = dataList.listIterator(); iterator.hasNext(); ) { | ||
| 84 | + if (iterator.nextIndex() > Chart.MAX_ITEMS_COUNT) { | ||
| 85 | + iterator.remove(); | ||
| 86 | + return; | ||
| 87 | + } | ||
| 88 | + iterator.next(); | ||
| 89 | + } | ||
| 90 | + } | ||
| 91 | + | ||
| 92 | + private static List<Float> createValueList(@NonNull List<InputData> dataList) { | ||
| 93 | + List<Float> valueList = new ArrayList<>(); | ||
| 94 | + long topValue = ValueUtils.max(dataList);//取最大值 | ||
| 95 | + | ||
| 96 | + for (InputData data : dataList) { | ||
| 97 | + float value = (float) data.valueY / topValue; | ||
| 98 | + valueList.add(value); | ||
| 99 | + } | ||
| 100 | + | ||
| 101 | + return valueList; | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + @NonNull | ||
| 105 | + private static List<DrawData> createDrawDataList(@NonNull Chart chart, @NonNull List<Float> valueList) { | ||
| 106 | + List<DrawData> drawDataList = new ArrayList<>(); | ||
| 107 | + for (int i = 0; i < valueList.size() - 1; i++) { | ||
| 108 | + DrawData drawData = createDrawData(chart, valueList, i); | ||
| 109 | + drawDataList.add(drawData); | ||
| 110 | + } | ||
| 111 | + return drawDataList; | ||
| 112 | + } | ||
| 113 | + | ||
| 114 | + @NonNull | ||
| 115 | + private static DrawData createDrawData(@NonNull Chart chart, @NonNull List<Float> valueList, int position) { | ||
| 116 | + DrawData drawData = new DrawData(); | ||
| 117 | + if (position > valueList.size() - 1) { | ||
| 118 | + return drawData; | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + float value = valueList.get(position); | ||
| 122 | + int startX = getCoordinateX(chart, position); | ||
| 123 | + int startY = getCoordinateY(chart, value); | ||
| 124 | + drawData.startX = startX; | ||
| 125 | + drawData.startY = startY; | ||
| 126 | +// drawData.setStartX(startX); | ||
| 127 | +// drawData.setStartY(startY); | ||
| 128 | + int nextPosition = position + 1; | ||
| 129 | + if (nextPosition < valueList.size()) { | ||
| 130 | + float nextValue = valueList.get(nextPosition); | ||
| 131 | + int stopX = getCoordinateX(chart, nextPosition); | ||
| 132 | + int stopY = getCoordinateY(chart, nextValue); | ||
| 133 | + | ||
| 134 | +// drawData.setStopX(stopX); | ||
| 135 | +// drawData.setStopY(stopY); | ||
| 136 | + | ||
| 137 | + drawData.stopX = stopX; | ||
| 138 | + drawData.stopY = stopY; | ||
| 139 | + } | ||
| 140 | + | ||
| 141 | + return drawData; | ||
| 142 | + } | ||
| 143 | + | ||
| 144 | + @SuppressWarnings("UnnecessaryLocalVariable") | ||
| 145 | + private static int getCoordinateX(@NonNull Chart chart, int index) { | ||
| 146 | + int width = chart.width; | ||
| 147 | + int titleWidth = chart.titleWidth; | ||
| 148 | + | ||
| 149 | + int widthCorrected = width - titleWidth; | ||
| 150 | + int partWidth = widthCorrected / (Chart.MAX_ITEMS_COUNT - 1); | ||
| 151 | + int coordinate = titleWidth + (partWidth * index); | ||
| 152 | + | ||
| 153 | + if (coordinate < 0) { | ||
| 154 | + coordinate = 0; | ||
| 155 | + | ||
| 156 | + } else if (coordinate > width) { | ||
| 157 | + coordinate = width; | ||
| 158 | + } | ||
| 159 | + | ||
| 160 | + return coordinate; | ||
| 161 | + } | ||
| 162 | + | ||
| 163 | + @SuppressWarnings("UnnecessaryLocalVariable") | ||
| 164 | + private static int getCoordinateY(@NonNull Chart chart, float value) { | ||
| 165 | + int height = chart.height - chart.padding- chart.textSize; | ||
| 166 | + int heightOffset = chart.heightOffset; | ||
| 167 | + | ||
| 168 | + int heightCorrected = height - heightOffset; | ||
| 169 | + int coordinate = (int) (heightCorrected - (heightCorrected * value)); | ||
| 170 | + | ||
| 171 | + if (coordinate < 0) { | ||
| 172 | + coordinate = 0; | ||
| 173 | + | ||
| 174 | + } else if (coordinate > heightCorrected) { | ||
| 175 | + coordinate = heightCorrected; | ||
| 176 | + } | ||
| 177 | + | ||
| 178 | + coordinate += heightOffset; | ||
| 179 | + return coordinate; | ||
| 180 | + } | ||
| 181 | +} |
mvpsdk/src/main/java/com/share/mvpsdk/view/chartview/ChartView.java
0 → 100644
| @@ -0,0 +1,242 @@ | @@ -0,0 +1,242 @@ | ||
| 1 | +package com.share.mvpsdk.view.chartview; | ||
| 2 | + | ||
| 3 | +import android.content.Context; | ||
| 4 | +import android.graphics.Canvas; | ||
| 5 | +import android.graphics.Paint; | ||
| 6 | +import android.support.annotation.NonNull; | ||
| 7 | +import android.support.annotation.Nullable; | ||
| 8 | +import android.util.AttributeSet; | ||
| 9 | +import android.util.Log; | ||
| 10 | +import android.view.View; | ||
| 11 | + | ||
| 12 | +import com.share.mvpsdk.R; | ||
| 13 | +import com.share.mvpsdk.utils.DateUtils; | ||
| 14 | +import com.share.mvpsdk.utils.ValueUtils; | ||
| 15 | +import com.share.mvpsdk.view.chartview.data.Chart; | ||
| 16 | +import com.share.mvpsdk.view.chartview.data.ChartPath; | ||
| 17 | +import com.share.mvpsdk.view.chartview.data.DrawData; | ||
| 18 | +import com.share.mvpsdk.view.chartview.data.InputData; | ||
| 19 | + | ||
| 20 | +import java.util.ArrayList; | ||
| 21 | +import java.util.List; | ||
| 22 | + | ||
| 23 | +/** | ||
| 24 | + * Created by ToaHanDong on 2018/3/28. | ||
| 25 | + */ | ||
| 26 | + | ||
| 27 | +public class ChartView extends View { | ||
| 28 | + | ||
| 29 | + Paint axisPaint;//轴的画笔 | ||
| 30 | + | ||
| 31 | + Paint valuesPaint;//值的画笔 | ||
| 32 | + | ||
| 33 | + Paint textPaint;//轴的数值的画笔 | ||
| 34 | + | ||
| 35 | + Chart chart;//图表属性 | ||
| 36 | + | ||
| 37 | + List<ChartPath> chartPathList=null;//路径的集合 | ||
| 38 | + | ||
| 39 | + private int axisColor, valuesColor, textColor; | ||
| 40 | + | ||
| 41 | + | ||
| 42 | + public ChartView(Context context) { | ||
| 43 | + super(context); | ||
| 44 | + initCharView(); | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + public ChartView(Context context, @Nullable AttributeSet attrs) { | ||
| 48 | + super(context, attrs); | ||
| 49 | + initCharView(); | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + public ChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { | ||
| 53 | + super(context, attrs, defStyleAttr); | ||
| 54 | + initCharView(); | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + private void initCharView() { | ||
| 58 | + | ||
| 59 | + initPaint(); | ||
| 60 | + | ||
| 61 | + | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + //添加路径 | ||
| 65 | + public void setChartPaths(List<InputData> inputData){ | ||
| 66 | + if (null==chartPathList)chartPathList=new ArrayList<>(); | ||
| 67 | + ChartPath chartPath=new ChartPath(inputData); | ||
| 68 | + chartPathList.add(chartPath); | ||
| 69 | + chart.setInputDataList(inputData); | ||
| 70 | + chart.titleWidth=getTitleWidth(); | ||
| 71 | + measure(690,392); | ||
| 72 | + Log.d("66666","setChartPaths="+chart.toString()); | ||
| 73 | + chart.setDrawDataList(ValueUtils.getDrawData(chart)); | ||
| 74 | + invalidate(); | ||
| 75 | + | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + //设置颜色 | ||
| 79 | + public void setColors(int textColor,int axisColor,int valuesColor){ | ||
| 80 | + this.textColor=textColor; | ||
| 81 | + this.axisColor=axisColor; | ||
| 82 | + this.valuesColor=valuesColor; | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | + private int getTitleWidth() { | ||
| 86 | + List<InputData> valueList = chart.inputDataList; | ||
| 87 | + if (valueList == null || valueList.isEmpty()) { | ||
| 88 | + return 0; | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + String maxValue = String.valueOf(ValueUtils.max(valueList)); | ||
| 92 | + int titleWidth = (int) textPaint.measureText(maxValue); | ||
| 93 | + int padding = chart.padding; | ||
| 94 | + | ||
| 95 | + return padding + titleWidth + padding; | ||
| 96 | + } | ||
| 97 | + /** | ||
| 98 | + * 初始化 画笔 | ||
| 99 | + */ | ||
| 100 | + private void initPaint() { | ||
| 101 | + | ||
| 102 | + chart = new Chart(); | ||
| 103 | +// chart.height = getMeasuredHeight(); | ||
| 104 | +// chart.width = getWidth(); | ||
| 105 | + chart.heightOffset = (int) (getResources().getDimension(R.dimen.dp_4) + getResources().getDimension(R.dimen.dp_4)); | ||
| 106 | + chart.padding = (int) (getResources().getDimension(R.dimen.dp_10)); | ||
| 107 | + chart.textSize = (int) getResources().getDimension(R.dimen.dp_14); | ||
| 108 | + chart.radius = (int) getResources().getDimension(R.dimen.dp_4); | ||
| 109 | + chart.inerRadius = (int) getResources().getDimension(R.dimen.dp_4); | ||
| 110 | + | ||
| 111 | + axisPaint = new Paint(); | ||
| 112 | + axisPaint.setColor(getResources().getColor(R.color.theme_day_blue)); | ||
| 113 | + axisPaint.setStyle(Paint.Style.FILL); | ||
| 114 | + axisPaint.setAntiAlias(true); | ||
| 115 | + axisPaint.setTextSize(chart.textSize); | ||
| 116 | + | ||
| 117 | + valuesPaint = new Paint(); | ||
| 118 | + valuesPaint.setAntiAlias(true); | ||
| 119 | + valuesPaint.setTextSize(chart.textSize); | ||
| 120 | + valuesPaint.setColor(getResources().getColor(R.color.theme_day_blue)); | ||
| 121 | + | ||
| 122 | + textPaint = new Paint(); | ||
| 123 | + textPaint.setColor(getResources().getColor(R.color.text_color_dark)); | ||
| 124 | + textPaint.setTextSize(chart.textSize); | ||
| 125 | + textPaint.setAntiAlias(true); | ||
| 126 | + textPaint.setStyle(Paint.Style.FILL); | ||
| 127 | + | ||
| 128 | + } | ||
| 129 | + | ||
| 130 | + @Override | ||
| 131 | + protected void onDraw(Canvas canvas) { | ||
| 132 | + super.onDraw(canvas); | ||
| 133 | + | ||
| 134 | + drawChartVertical(canvas); | ||
| 135 | + drawChartHorizontal(canvas); | ||
| 136 | + drawFrameLines(canvas); | ||
| 137 | + | ||
| 138 | + } | ||
| 139 | + | ||
| 140 | + | ||
| 141 | + private void drawChartVertical(@NonNull Canvas canvas) { | ||
| 142 | + List<InputData> inputDataList = chart.getInputDataList(); | ||
| 143 | + if (inputDataList == null || inputDataList.isEmpty()) { | ||
| 144 | + return; | ||
| 145 | + } | ||
| 146 | + | ||
| 147 | + long maxValue = ValueUtils.max(inputDataList); | ||
| 148 | + long correctedMaxValue = ValueUtils.getCorrectedMaxValue(maxValue); | ||
| 149 | + float value = (float) correctedMaxValue / maxValue; | ||
| 150 | + | ||
| 151 | + long heightOffset = chart.heightOffset; | ||
| 152 | + int padding = chart.padding; | ||
| 153 | + int textSize = chart.textSize; | ||
| 154 | + int titleWidth = chart.titleWidth; | ||
| 155 | + | ||
| 156 | + float width = chart.width - chart.textSize - chart.padding; | ||
| 157 | + float height = chart.height - textSize - padding; | ||
| 158 | + float chartPartHeight = ((height - heightOffset) * value) / Chart.CHART_PARTS; | ||
| 159 | + | ||
| 160 | + float currHeight = height; | ||
| 161 | + int currTitle = 0; | ||
| 162 | + | ||
| 163 | + for (int i = 0; i <= Chart.CHART_PARTS; i++) { | ||
| 164 | + float titleY = currHeight; | ||
| 165 | + | ||
| 166 | + if (i <= 0) { | ||
| 167 | + titleY = height; | ||
| 168 | + | ||
| 169 | + } else if (textSize + chart.heightOffset > currHeight) { | ||
| 170 | + titleY = currHeight + textSize - Chart.TEXT_SIZE_OFFSET; | ||
| 171 | + } | ||
| 172 | + | ||
| 173 | + if (i > 0) { | ||
| 174 | +// canvas.drawLine(titleWidth, currHeight, width, currHeight, frameInternalPaint); | ||
| 175 | + } | ||
| 176 | + | ||
| 177 | + String title = String.valueOf(currTitle); | ||
| 178 | + canvas.drawText(title, padding, titleY, textPaint); | ||
| 179 | + | ||
| 180 | + currHeight -= chartPartHeight; | ||
| 181 | + currTitle += correctedMaxValue / Chart.CHART_PARTS; | ||
| 182 | + } | ||
| 183 | + } | ||
| 184 | + | ||
| 185 | + private void drawChartHorizontal(@NonNull Canvas canvas) { | ||
| 186 | + List<InputData> inputDataList = chart.getInputDataList(); | ||
| 187 | + List<DrawData> drawDataList = chart.getDrawDataList(); | ||
| 188 | + Log.d("66666","drawChartHorizontal="+drawDataList); | ||
| 189 | + if (inputDataList == null || inputDataList.isEmpty() || drawDataList == null || drawDataList.isEmpty()) { | ||
| 190 | + return; | ||
| 191 | + } | ||
| 192 | +// drawPath(drawDataList,canvas); | ||
| 193 | + for (int i = 0; i < inputDataList.size(); i++) { | ||
| 194 | + | ||
| 195 | + InputData inputData = inputDataList.get(i); | ||
| 196 | + String date = DateUtils.format(inputData.valueX); | ||
| 197 | + int dateWidth = (int) axisPaint.measureText(date); | ||
| 198 | + int x; | ||
| 199 | + if (drawDataList.size() > i) { | ||
| 200 | + DrawData drawData = drawDataList.get(i); | ||
| 201 | + x = drawData.startX; | ||
| 202 | + if (i > 0) { | ||
| 203 | +// x -= (dateWidth / 2); | ||
| 204 | + x = drawDataList.get(i).startX - (dateWidth / 2) - chart.padding - chart.textSize; | ||
| 205 | + } | ||
| 206 | + } else { | ||
| 207 | + x = drawDataList.get(drawDataList.size() - 1).stopX - dateWidth - chart.padding - chart.textSize; | ||
| 208 | + } | ||
| 209 | + if (i > 0) | ||
| 210 | + canvas.drawLine(i == drawDataList.size() ? x + chart.textSize : x + dateWidth / 2, | ||
| 211 | + drawDataList.get(i - 1).stopY, i == drawDataList.size() ? x + chart.textSize : x + dateWidth / 2, chart.height | ||
| 212 | + - chart.textSize - chart.padding, textPaint); | ||
| 213 | + canvas.drawText(date, x, chart.height, textPaint); | ||
| 214 | + } | ||
| 215 | + } | ||
| 216 | + | ||
| 217 | + private void drawFrameLines(@NonNull Canvas canvas) { | ||
| 218 | + int textSize = chart.textSize; | ||
| 219 | + int padding = chart.padding; | ||
| 220 | + | ||
| 221 | + int height = chart.height - textSize - padding; | ||
| 222 | + int width = chart.width - textSize - padding; | ||
| 223 | + int titleWidth = chart.titleWidth; | ||
| 224 | + int heightOffset = chart.heightOffset; | ||
| 225 | + | ||
| 226 | + canvas.drawLine(titleWidth, heightOffset, titleWidth, height, axisPaint); | ||
| 227 | + canvas.drawLine(titleWidth, height, width, height, axisPaint); | ||
| 228 | + } | ||
| 229 | + | ||
| 230 | + @Override | ||
| 231 | + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | ||
| 232 | + super.onMeasure(widthMeasureSpec, heightMeasureSpec); | ||
| 233 | + | ||
| 234 | + int width = MeasureSpec.getSize(widthMeasureSpec); | ||
| 235 | + int height = MeasureSpec.getSize(heightMeasureSpec) / 2; | ||
| 236 | + chart.width=width; | ||
| 237 | + chart.height=height; | ||
| 238 | + Log.d("66666","onMeasure="+chart.toString()); | ||
| 239 | + setMeasuredDimension(width, height); | ||
| 240 | + | ||
| 241 | + } | ||
| 242 | +} |
mvpsdk/src/main/java/com/share/mvpsdk/view/chartview/data/Chart.java
0 → 100644
| @@ -0,0 +1,62 @@ | @@ -0,0 +1,62 @@ | ||
| 1 | +package com.share.mvpsdk.view.chartview.data; | ||
| 2 | + | ||
| 3 | +import java.util.List; | ||
| 4 | + | ||
| 5 | +/** | ||
| 6 | + * Created by ToaHanDong on 2018/3/28. | ||
| 7 | + * 定义chart表单需要的一些数据 | ||
| 8 | + */ | ||
| 9 | + | ||
| 10 | +public class Chart { | ||
| 11 | + | ||
| 12 | + public static final int CHART_PARTS = 5; | ||
| 13 | + public static final int MAX_ITEMS_COUNT = 7; | ||
| 14 | + public static final int CHART_PART_VALUE = 10; | ||
| 15 | + public static final int TEXT_SIZE_OFFSET = 10; | ||
| 16 | + | ||
| 17 | + public int width;//表单的宽 | ||
| 18 | + public int height;//表单的高 | ||
| 19 | + | ||
| 20 | + public int padding;//内边距 | ||
| 21 | + public int titleWidth;// | ||
| 22 | + public int textSize;//字体大小 | ||
| 23 | + public int heightOffset; | ||
| 24 | + | ||
| 25 | + public int radius; | ||
| 26 | + public int inerRadius; | ||
| 27 | + | ||
| 28 | + public List<InputData> inputDataList; | ||
| 29 | + | ||
| 30 | + public List<DrawData> drawDataList; | ||
| 31 | + | ||
| 32 | + public void setInputDataList(List<InputData> inputDataList){ | ||
| 33 | + this.inputDataList=inputDataList; | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + public List<InputData> getInputDataList(){ | ||
| 37 | + return inputDataList; | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + public void setDrawDataList(List<DrawData> drawDataList){ | ||
| 41 | + this.drawDataList=drawDataList; | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + public List<DrawData> getDrawDataList(){ | ||
| 45 | + return drawDataList; | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + | ||
| 49 | + @Override | ||
| 50 | + public String toString() { | ||
| 51 | + return "Chart{" + | ||
| 52 | + "width=" + width + | ||
| 53 | + ", height=" + height + | ||
| 54 | + ", padding=" + padding + | ||
| 55 | + ", titleWidth=" + titleWidth + | ||
| 56 | + ", textSize=" + textSize + | ||
| 57 | + ", heightOffset=" + heightOffset + | ||
| 58 | + ", radius=" + radius + | ||
| 59 | + ", inerRadius=" + inerRadius + | ||
| 60 | + '}'; | ||
| 61 | + } | ||
| 62 | +} |
mvpsdk/src/main/java/com/share/mvpsdk/view/chartview/data/ChartPath.java
0 → 100644
| @@ -0,0 +1,24 @@ | @@ -0,0 +1,24 @@ | ||
| 1 | +package com.share.mvpsdk.view.chartview.data; | ||
| 2 | + | ||
| 3 | +import java.util.List; | ||
| 4 | + | ||
| 5 | +/** | ||
| 6 | + * Created by ToaHanDong on 2018/3/28. | ||
| 7 | + */ | ||
| 8 | + | ||
| 9 | +//路径 | ||
| 10 | +public class ChartPath { | ||
| 11 | + | ||
| 12 | + public List<InputData> inputDataList; | ||
| 13 | + | ||
| 14 | + public ChartPath(List<InputData> inputData) { | ||
| 15 | + this.inputDataList=inputData; | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + @Override | ||
| 19 | + public String toString() { | ||
| 20 | + return "ChartPath{" + | ||
| 21 | + "inputDataList=" + inputDataList + | ||
| 22 | + '}'; | ||
| 23 | + } | ||
| 24 | +} |
mvpsdk/src/main/java/com/share/mvpsdk/view/chartview/data/DrawData.java
0 → 100644
| @@ -0,0 +1,24 @@ | @@ -0,0 +1,24 @@ | ||
| 1 | +package com.share.mvpsdk.view.chartview.data; | ||
| 2 | + | ||
| 3 | +/** | ||
| 4 | + * Created by ToaHanDong on 2018/3/28. | ||
| 5 | + */ | ||
| 6 | + | ||
| 7 | +public class DrawData { | ||
| 8 | + | ||
| 9 | + public int startX; | ||
| 10 | + public int startY; | ||
| 11 | + | ||
| 12 | + public int stopX; | ||
| 13 | + public int stopY; | ||
| 14 | + | ||
| 15 | + @Override | ||
| 16 | + public String toString() { | ||
| 17 | + return "DrawData{" + | ||
| 18 | + "startX=" + startX + | ||
| 19 | + ", startY=" + startY + | ||
| 20 | + ", stopX=" + stopX + | ||
| 21 | + ", stopY=" + stopY + | ||
| 22 | + '}'; | ||
| 23 | + } | ||
| 24 | +} |
mvpsdk/src/main/java/com/share/mvpsdk/view/chartview/data/InputData.java
0 → 100644
| @@ -0,0 +1,26 @@ | @@ -0,0 +1,26 @@ | ||
| 1 | +package com.share.mvpsdk.view.chartview.data; | ||
| 2 | + | ||
| 3 | +/** | ||
| 4 | + * Created by ToaHanDong on 2018/3/28. | ||
| 5 | + * 输入的x、y参数 | ||
| 6 | + */ | ||
| 7 | + | ||
| 8 | +public class InputData { | ||
| 9 | + | ||
| 10 | + public long valueX; | ||
| 11 | + | ||
| 12 | + public long valueY; | ||
| 13 | + | ||
| 14 | + public InputData(long valueX,long valueY){ | ||
| 15 | + this.valueX=valueX; | ||
| 16 | + this.valueY=valueY; | ||
| 17 | + } | ||
| 18 | + | ||
| 19 | + @Override | ||
| 20 | + public String toString() { | ||
| 21 | + return "InputData{" + | ||
| 22 | + "valueX=" + valueX + | ||
| 23 | + ", valueY=" + valueY + | ||
| 24 | + '}'; | ||
| 25 | + } | ||
| 26 | +} |