Commit e280dc617640122faff8fa5183efd4e82ea43d3f
1 parent
1dffc346
Exists in
yxb_dev
and in
2 other branches
no message
Showing
6 changed files
with
174 additions
and
84 deletions
Show diff stats
app/src/main/java/com/shunzhi/parent/ui/fragment/report/ReportDetialFragment.java
... | ... | @@ -63,12 +63,20 @@ public class ReportDetialFragment extends BaseMVPCompatFragment { |
63 | 63 | |
64 | 64 | chartView.setColors(R.color.textColor,R.color.huodong_blue,R.color.gray); |
65 | 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); | |
66 | + List<InputData> inputDataList1=new ArrayList<>(); | |
67 | + for (int i = 0; i < 7; i++) { | |
68 | + InputData inputData=new InputData("文明",10*i); | |
68 | 69 | inputDataList.add(inputData); |
69 | 70 | } |
70 | 71 | |
71 | 72 | chartView.setChartPaths(inputDataList); |
72 | 73 | |
74 | + for (int i = 0; i < 7; i++) { | |
75 | + InputData inputData=new InputData("文明1",20*i); | |
76 | + inputDataList1.add(inputData); | |
77 | + } | |
78 | + | |
79 | + chartView.setChartPaths(inputDataList1); | |
80 | + | |
73 | 81 | } |
74 | 82 | } | ... | ... |
app/src/main/res/layout/fragment_report_detial.xml
... | ... | @@ -162,7 +162,7 @@ |
162 | 162 | |
163 | 163 | <FrameLayout |
164 | 164 | android:layout_width="match_parent" |
165 | - android:layout_height="150dp" | |
165 | + android:layout_height="wrap_content" | |
166 | 166 | android:layout_marginTop="@dimen/size_dp_10"> |
167 | 167 | |
168 | 168 | <com.share.mvpsdk.view.chartview.ChartView | ... | ... |
mvpsdk/src/main/java/com/share/mvpsdk/utils/DateUtils.java
... | ... | @@ -55,7 +55,7 @@ public class DateUtils { |
55 | 55 | } |
56 | 56 | |
57 | 57 | public static String format(long millis){ |
58 | - SimpleDateFormat format = new SimpleDateFormat("dd", Locale.getDefault()); | |
58 | + SimpleDateFormat format = new SimpleDateFormat("MM/dd", Locale.getDefault()); | |
59 | 59 | return format.format(millis); |
60 | 60 | } |
61 | 61 | ... | ... |
mvpsdk/src/main/java/com/share/mvpsdk/utils/ValueUtils.java
... | ... | @@ -7,6 +7,7 @@ import android.util.Log; |
7 | 7 | |
8 | 8 | |
9 | 9 | import com.share.mvpsdk.view.chartview.data.Chart; |
10 | +import com.share.mvpsdk.view.chartview.data.ChartPath; | |
10 | 11 | import com.share.mvpsdk.view.chartview.data.DrawData; |
11 | 12 | import com.share.mvpsdk.view.chartview.data.InputData; |
12 | 13 | |
... | ... | @@ -27,22 +28,39 @@ public class ValueUtils { |
27 | 28 | return maxValue; |
28 | 29 | } |
29 | 30 | |
30 | - public static long max(@Nullable List<InputData> dataList) { | |
31 | + public static long max(@Nullable List<ChartPath> dataList) { | |
31 | 32 | long maxValue = 0; |
32 | 33 | |
33 | 34 | if (dataList == null || dataList.isEmpty()) { |
34 | 35 | return maxValue; |
35 | 36 | } |
36 | - | |
37 | - for (InputData data : dataList) { | |
38 | - if (data.valueY > maxValue) { | |
39 | - maxValue = data.valueY; | |
37 | + for (ChartPath charpath : | |
38 | + dataList) { | |
39 | + for (InputData data : charpath.inputDataList) { | |
40 | + if (data.valueY > maxValue) { | |
41 | + maxValue = data.valueY; | |
42 | + } | |
40 | 43 | } |
41 | 44 | } |
42 | 45 | |
43 | 46 | return maxValue; |
44 | 47 | } |
45 | 48 | |
49 | + private static long maxOne(List<InputData> dataList) { | |
50 | + long maxValue = 0; | |
51 | + | |
52 | + if (dataList == null || dataList.isEmpty()) { | |
53 | + return maxValue; | |
54 | + } | |
55 | + for (InputData data : dataList) { | |
56 | + if (data.valueY > maxValue) { | |
57 | + maxValue = data.valueY; | |
58 | + } | |
59 | + } | |
60 | + | |
61 | + return maxValue; | |
62 | + } | |
63 | + | |
46 | 64 | private static boolean isRightValue(long value) { |
47 | 65 | long valueResidual = value % Chart.CHART_PART_VALUE; |
48 | 66 | return valueResidual == 0; |
... | ... | @@ -55,29 +73,34 @@ public class ValueUtils { |
55 | 73 | } |
56 | 74 | |
57 | 75 | List<InputData> dataList = chart.getInputDataList(); |
58 | - correctDataListSize(dataList);//移除多余的数据 | |
76 | +// correctDataListSize(dataList);//移除多余的数据 | |
59 | 77 | return createDrawDataList(chart, createValueList(dataList)); |
60 | 78 | } |
61 | 79 | |
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 | - } | |
80 | + @NotificationCompat.NotificationVisibility | |
81 | + public static List<DrawData> getDrawDatas(@Nullable List<InputData> inputDataList,@NonNull Chart chart) { | |
82 | + return createDrawDataList(chart, createValueList(inputDataList)); | |
69 | 83 | } |
70 | 84 | |
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 | - } | |
85 | +// private static void correctDataListSize(@NonNull List<InputData> dataList) { | |
86 | +// if (dataList.size() < Chart.MAX_ITEMS_COUNT) { | |
87 | +// addLackingItems(dataList); | |
88 | +// | |
89 | +// } else if (dataList.size() > Chart.MAX_ITEMS_COUNT) { | |
90 | +// removeExcessItems(dataList); | |
91 | +// } | |
92 | +// } | |
93 | +// | |
94 | +// private static void addLackingItems(@NonNull List<InputData> dataList) { | |
95 | +// for (int i = dataList.size(); i < Chart.MAX_ITEMS_COUNT; i++) { | |
96 | +// long millis = dataList.get(0).valueX - TimeUnit.DAYS.toMillis(1); | |
97 | +// if (millis < 0) { | |
98 | +// millis = 0; | |
99 | +// } | |
100 | +// | |
101 | +// dataList.add(0, new InputData(0, millis)); | |
102 | +// } | |
103 | +// } | |
81 | 104 | |
82 | 105 | private static void removeExcessItems(@NonNull List<InputData> dataList) { |
83 | 106 | for (ListIterator<InputData> iterator = dataList.listIterator(); iterator.hasNext(); ) { |
... | ... | @@ -88,10 +111,10 @@ public class ValueUtils { |
88 | 111 | iterator.next(); |
89 | 112 | } |
90 | 113 | } |
91 | - | |
114 | + static long topValue=0; | |
92 | 115 | private static List<Float> createValueList(@NonNull List<InputData> dataList) { |
93 | 116 | List<Float> valueList = new ArrayList<>(); |
94 | - long topValue = ValueUtils.max(dataList);//取最大值 | |
117 | + topValue=topValue > ValueUtils.maxOne(dataList)?topValue:ValueUtils.maxOne(dataList);//取最大值 | |
95 | 118 | |
96 | 119 | for (InputData data : dataList) { |
97 | 120 | float value = (float) data.valueY / topValue; |
... | ... | @@ -162,7 +185,7 @@ public class ValueUtils { |
162 | 185 | |
163 | 186 | @SuppressWarnings("UnnecessaryLocalVariable") |
164 | 187 | private static int getCoordinateY(@NonNull Chart chart, float value) { |
165 | - int height = chart.height - chart.padding- chart.textSize; | |
188 | + int height = chart.height - chart.padding - chart.textSize; | |
166 | 189 | int heightOffset = chart.heightOffset; |
167 | 190 | |
168 | 191 | int heightCorrected = height - heightOffset; | ... | ... |
mvpsdk/src/main/java/com/share/mvpsdk/view/chartview/ChartView.java
... | ... | @@ -2,7 +2,13 @@ package com.share.mvpsdk.view.chartview; |
2 | 2 | |
3 | 3 | import android.content.Context; |
4 | 4 | import android.graphics.Canvas; |
5 | +import android.graphics.Color; | |
6 | +import android.graphics.DashPathEffect; | |
5 | 7 | import android.graphics.Paint; |
8 | +import android.graphics.Path; | |
9 | +import android.graphics.PathDashPathEffect; | |
10 | +import android.graphics.RectF; | |
11 | +import android.graphics.Shader; | |
6 | 12 | import android.support.annotation.NonNull; |
7 | 13 | import android.support.annotation.Nullable; |
8 | 14 | import android.util.AttributeSet; |
... | ... | @@ -32,9 +38,11 @@ public class ChartView extends View { |
32 | 38 | |
33 | 39 | Paint textPaint;//轴的数值的画笔 |
34 | 40 | |
41 | + Paint effectPaint;//有间隔的画笔 | |
42 | + | |
35 | 43 | Chart chart;//图表属性 |
36 | 44 | |
37 | - List<ChartPath> chartPathList=null;//路径的集合 | |
45 | + List<ChartPath> chartPathList = null;//路径的集合 | |
38 | 46 | |
39 | 47 | private int axisColor, valuesColor, textColor; |
40 | 48 | |
... | ... | @@ -61,25 +69,11 @@ public class ChartView extends View { |
61 | 69 | |
62 | 70 | } |
63 | 71 | |
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 | 72 | //设置颜色 |
79 | - public void setColors(int textColor,int axisColor,int valuesColor){ | |
80 | - this.textColor=textColor; | |
81 | - this.axisColor=axisColor; | |
82 | - this.valuesColor=valuesColor; | |
73 | + public void setColors(int textColor, int axisColor, int valuesColor) { | |
74 | + this.textColor = textColor; | |
75 | + this.axisColor = axisColor; | |
76 | + this.valuesColor = valuesColor; | |
83 | 77 | } |
84 | 78 | |
85 | 79 | private int getTitleWidth() { |
... | ... | @@ -88,15 +82,29 @@ public class ChartView extends View { |
88 | 82 | return 0; |
89 | 83 | } |
90 | 84 | |
91 | - String maxValue = String.valueOf(ValueUtils.max(valueList)); | |
85 | + String maxValue = String.valueOf(ValueUtils.max(chartPathList)); | |
92 | 86 | int titleWidth = (int) textPaint.measureText(maxValue); |
93 | 87 | int padding = chart.padding; |
94 | 88 | |
95 | 89 | return padding + titleWidth + padding; |
96 | 90 | } |
91 | + | |
92 | + //添加路径 | |
93 | + public void setChartPaths(List<InputData> inputData) { | |
94 | + if (null == chartPathList) chartPathList = new ArrayList<>(); | |
95 | + ChartPath chartPath = new ChartPath(inputData); | |
96 | + chartPathList.add(chartPath); | |
97 | + chart.setInputDataList(inputData); | |
98 | + chart.titleWidth = getTitleWidth(); | |
99 | + invalidate(); | |
100 | + | |
101 | + } | |
102 | + | |
97 | 103 | /** |
98 | 104 | * 初始化 画笔 |
99 | 105 | */ |
106 | + Path path = new Path(); | |
107 | + | |
100 | 108 | private void initPaint() { |
101 | 109 | |
102 | 110 | chart = new Chart(); |
... | ... | @@ -117,6 +125,8 @@ public class ChartView extends View { |
117 | 125 | valuesPaint = new Paint(); |
118 | 126 | valuesPaint.setAntiAlias(true); |
119 | 127 | valuesPaint.setTextSize(chart.textSize); |
128 | + valuesPaint.setStrokeWidth(2); | |
129 | + valuesPaint.setStyle(Paint.Style.FILL); | |
120 | 130 | valuesPaint.setColor(getResources().getColor(R.color.theme_day_blue)); |
121 | 131 | |
122 | 132 | textPaint = new Paint(); |
... | ... | @@ -125,6 +135,14 @@ public class ChartView extends View { |
125 | 135 | textPaint.setAntiAlias(true); |
126 | 136 | textPaint.setStyle(Paint.Style.FILL); |
127 | 137 | |
138 | + effectPaint = new Paint(); | |
139 | + effectPaint.setStyle(Paint.Style.STROKE); | |
140 | + effectPaint.setAntiAlias(true); | |
141 | + effectPaint.setStrokeWidth(2); | |
142 | + effectPaint.setColor(getResources().getColor(R.color.theme_day_blue)); | |
143 | +// path.lineTo(); | |
144 | + effectPaint.setPathEffect(new PathDashPathEffect(path, 15, 0, PathDashPathEffect.Style.ROTATE)); | |
145 | + | |
128 | 146 | } |
129 | 147 | |
130 | 148 | @Override |
... | ... | @@ -132,8 +150,9 @@ public class ChartView extends View { |
132 | 150 | super.onDraw(canvas); |
133 | 151 | |
134 | 152 | drawChartVertical(canvas); |
135 | - drawChartHorizontal(canvas); | |
136 | 153 | drawFrameLines(canvas); |
154 | + drawChartHorizontal(canvas); | |
155 | + | |
137 | 156 | |
138 | 157 | } |
139 | 158 | |
... | ... | @@ -144,7 +163,7 @@ public class ChartView extends View { |
144 | 163 | return; |
145 | 164 | } |
146 | 165 | |
147 | - long maxValue = ValueUtils.max(inputDataList); | |
166 | + long maxValue = ValueUtils.max(chartPathList); | |
148 | 167 | long correctedMaxValue = ValueUtils.getCorrectedMaxValue(maxValue); |
149 | 168 | float value = (float) correctedMaxValue / maxValue; |
150 | 169 | |
... | ... | @@ -172,10 +191,13 @@ public class ChartView extends View { |
172 | 191 | |
173 | 192 | if (i > 0) { |
174 | 193 | // canvas.drawLine(titleWidth, currHeight, width, currHeight, frameInternalPaint); |
194 | + //横向间隔线 | |
195 | + canvas.drawLine(titleWidth-5,titleY-textSize/2,width,titleY-textSize/2,textPaint); | |
175 | 196 | } |
176 | 197 | |
177 | 198 | String title = String.valueOf(currTitle); |
178 | 199 | canvas.drawText(title, padding, titleY, textPaint); |
200 | + textPaint.setColor(getResources().getColor(R.color.gray)); | |
179 | 201 | |
180 | 202 | currHeight -= chartPartHeight; |
181 | 203 | currTitle += correctedMaxValue / Chart.CHART_PARTS; |
... | ... | @@ -183,44 +205,82 @@ public class ChartView extends View { |
183 | 205 | } |
184 | 206 | |
185 | 207 | 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) { | |
208 | + for (int j = 0; j < chartPathList.size(); j++) { | |
209 | + chagerColor(j); | |
210 | + List<InputData> inputDataList = chart.getInputDataList(); | |
211 | +// List<DrawData> drawDataList = chart.getDrawDataList(); | |
212 | + List<DrawData> drawDataList = ValueUtils.getDrawDatas(chartPathList.get(j).inputDataList, chart); | |
213 | + if (inputDataList == null || inputDataList.isEmpty() || drawDataList == null || drawDataList.isEmpty()) { | |
214 | + return; | |
215 | + } | |
216 | + for (int i = 0; i < inputDataList.size(); i++) { | |
217 | + | |
218 | + InputData inputData = inputDataList.get(i); | |
219 | +// String date = DateUtils.format(inputData.valueX); | |
220 | + String date = inputData.valueX; | |
221 | + int dateWidth = (int) axisPaint.measureText(date); | |
222 | + int x; | |
223 | + if (drawDataList.size() > i) { | |
224 | + DrawData drawData = drawDataList.get(i); | |
225 | + x = drawData.startX; | |
226 | + if (i > 0) { | |
203 | 227 | // x -= (dateWidth / 2); |
204 | - x = drawDataList.get(i).startX - (dateWidth / 2) - chart.padding - chart.textSize; | |
228 | + x = drawDataList.get(i).startX - (dateWidth / 2) - chart.padding - chart.textSize; | |
229 | + } | |
230 | + } else { | |
231 | + x = drawDataList.get(drawDataList.size() - 1).stopX - dateWidth - chart.padding - chart.textSize; | |
232 | + } | |
233 | + if (i > 0) { | |
234 | + float startX = i == drawDataList.size() ? x + chart.textSize : x + dateWidth / 2; | |
235 | + float startY = drawDataList.get(i - 1).stopY; | |
236 | + float stopX = i == drawDataList.size() ? x + chart.textSize : x + dateWidth / 2; | |
237 | + float stopY = chart.height - chart.textSize - chart.padding; | |
238 | + | |
239 | +// canvas.drawLine(startX, | |
240 | +// startY, | |
241 | +// stopX, | |
242 | +// stopY, effectPaint); | |
243 | + | |
244 | + //画文字 | |
245 | + canvas.drawText(date, x, chart.height - chart.padding / 2, textPaint); | |
246 | + | |
247 | + //画圆点 | |
248 | + canvas.drawCircle(startX, startY, 10, valuesPaint); | |
249 | + | |
250 | + //点之间的连线 | |
251 | + if (i < drawDataList.size() - 1) { | |
252 | + canvas.drawLine(startX, startY, drawDataList.get(i + 1).startX - chart.padding - chart.textSize, drawDataList.get(i + 1).startY, valuesPaint); | |
253 | + } else { | |
254 | + canvas.drawLine(drawDataList.get(drawDataList.size() - 1).startX - chart.padding - chart.textSize, | |
255 | + drawDataList.get(drawDataList.size() - 1).startY, startX, startY, valuesPaint); | |
256 | + } | |
205 | 257 | } |
206 | - } else { | |
207 | - x = drawDataList.get(drawDataList.size() - 1).stopX - dateWidth - chart.padding - chart.textSize; | |
208 | 258 | } |
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 | 259 | } |
215 | 260 | } |
216 | 261 | |
262 | + private void chagerColor(int j) { | |
263 | + switch (j){ | |
264 | + case 0: | |
265 | + valuesPaint.setColor(getResources().getColor(R.color.theme_day_blue)); | |
266 | + break; | |
267 | + case 1: | |
268 | + valuesPaint.setColor(getResources().getColor(R.color.md_light_green_500)); | |
269 | + break; | |
270 | + case 2: | |
271 | + valuesPaint.setColor(getResources().getColor(R.color.light_yellow)); | |
272 | + break; | |
273 | + } | |
274 | + } | |
275 | + | |
276 | + //画横纵坐标轴 | |
217 | 277 | private void drawFrameLines(@NonNull Canvas canvas) { |
218 | 278 | int textSize = chart.textSize; |
219 | 279 | int padding = chart.padding; |
220 | 280 | |
221 | 281 | int height = chart.height - textSize - padding; |
222 | 282 | int width = chart.width - textSize - padding; |
223 | - int titleWidth = chart.titleWidth; | |
283 | + int titleWidth = chart.titleWidth-5; | |
224 | 284 | int heightOffset = chart.heightOffset; |
225 | 285 | |
226 | 286 | canvas.drawLine(titleWidth, heightOffset, titleWidth, height, axisPaint); |
... | ... | @@ -233,10 +293,9 @@ public class ChartView extends View { |
233 | 293 | |
234 | 294 | int width = MeasureSpec.getSize(widthMeasureSpec); |
235 | 295 | int height = MeasureSpec.getSize(heightMeasureSpec) / 2; |
236 | - chart.width=width; | |
237 | - chart.height=height; | |
238 | - Log.d("66666","onMeasure="+chart.toString()); | |
296 | + chart.width = width; | |
297 | + chart.height = height; | |
239 | 298 | setMeasuredDimension(width, height); |
240 | - | |
299 | + chart.setDrawDataList(ValueUtils.getDrawData(chart)); | |
241 | 300 | } |
242 | 301 | } | ... | ... |
mvpsdk/src/main/java/com/share/mvpsdk/view/chartview/data/InputData.java
... | ... | @@ -7,11 +7,11 @@ package com.share.mvpsdk.view.chartview.data; |
7 | 7 | |
8 | 8 | public class InputData { |
9 | 9 | |
10 | - public long valueX; | |
10 | + public String valueX; | |
11 | 11 | |
12 | 12 | public long valueY; |
13 | 13 | |
14 | - public InputData(long valueX,long valueY){ | |
14 | + public InputData(String valueX,long valueY){ | |
15 | 15 | this.valueX=valueX; |
16 | 16 | this.valueY=valueY; |
17 | 17 | } | ... | ... |