OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SampleCode.h" | 8 #include "SampleCode.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkPaint.h" | 10 #include "SkPaint.h" |
11 #include "SkPath.h" | 11 #include "SkPath.h" |
12 #include "SkRandom.h" | 12 #include "SkRandom.h" |
13 #include "SkView.h" | 13 #include "SkView.h" |
14 | 14 |
15 // Generates y values for the chart plots. | 15 // Generates y values for the chart plots. |
16 static void gen_data(SkScalar yAvg, SkScalar ySpread, int count, SkTDArray<SkSca
lar>* dataPts) { | 16 static void gen_data(SkScalar yAvg, SkScalar ySpread, int count, SkTDArray<SkSca
lar>* dataPts) { |
17 dataPts->setCount(count); | 17 dataPts->setCount(count); |
18 static SkRandom gRandom; | 18 static SkRandom gRandom; |
19 for (int i = 0; i < count; ++i) { | 19 for (int i = 0; i < count; ++i) { |
20 (*dataPts)[i] = gRandom.nextRangeScalar(yAvg - SkScalarHalf(ySpread), | 20 (*dataPts)[i] = gRandom.nextRangeScalar(yAvg - SkScalarHalf(ySpread), |
21 yAvg + SkScalarHalf(ySpread)); | 21 yAvg + SkScalarHalf(ySpread)); |
22 } | 22 } |
23 } | 23 } |
24 | 24 |
25 // Generates a path to stroke along the top of each plot and a fill path for the
area below each | 25 // Generates a path to stroke along the top of each plot and a fill path for the
area below each |
26 // plot. The fill path is bounded below by the bottomData plot points or a horiz
ontal line at | 26 // plot. The fill path is bounded below by the bottomData plot points or a horiz
ontal line at |
27 // yBase if bottomData == NULL. | 27 // yBase if bottomData == nullptr. |
28 // The plots are animated by rotating the data points by leftShift. | 28 // The plots are animated by rotating the data points by leftShift. |
29 static void gen_paths(const SkTDArray<SkScalar>& topData, | 29 static void gen_paths(const SkTDArray<SkScalar>& topData, |
30 const SkTDArray<SkScalar>* bottomData, | 30 const SkTDArray<SkScalar>* bottomData, |
31 SkScalar yBase, | 31 SkScalar yBase, |
32 SkScalar xLeft, SkScalar xDelta, | 32 SkScalar xLeft, SkScalar xDelta, |
33 int leftShift, | 33 int leftShift, |
34 SkPath* plot, SkPath* fill) { | 34 SkPath* plot, SkPath* fill) { |
35 plot->rewind(); | 35 plot->rewind(); |
36 fill->rewind(); | 36 fill->rewind(); |
37 plot->incReserve(topData.count()); | 37 plot->incReserve(topData.count()); |
38 if (NULL == bottomData) { | 38 if (nullptr == bottomData) { |
39 fill->incReserve(topData.count() + 2); | 39 fill->incReserve(topData.count() + 2); |
40 } else { | 40 } else { |
41 fill->incReserve(2 * topData.count()); | 41 fill->incReserve(2 * topData.count()); |
42 } | 42 } |
43 | 43 |
44 leftShift %= topData.count(); | 44 leftShift %= topData.count(); |
45 SkScalar x = xLeft; | 45 SkScalar x = xLeft; |
46 | 46 |
47 // Account for the leftShift using two loops | 47 // Account for the leftShift using two loops |
48 int shiftToEndCount = topData.count() - leftShift; | 48 int shiftToEndCount = topData.count() - leftShift; |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 SkPaint plotPaint; | 135 SkPaint plotPaint; |
136 SkPaint fillPaint; | 136 SkPaint fillPaint; |
137 plotPaint.setAntiAlias(true); | 137 plotPaint.setAntiAlias(true); |
138 plotPaint.setStyle(SkPaint::kStroke_Style); | 138 plotPaint.setStyle(SkPaint::kStroke_Style); |
139 plotPaint.setStrokeWidth(kStrokeWidth); | 139 plotPaint.setStrokeWidth(kStrokeWidth); |
140 plotPaint.setStrokeCap(SkPaint::kRound_Cap); | 140 plotPaint.setStrokeCap(SkPaint::kRound_Cap); |
141 plotPaint.setStrokeJoin(SkPaint::kRound_Join); | 141 plotPaint.setStrokeJoin(SkPaint::kRound_Join); |
142 fillPaint.setAntiAlias(true); | 142 fillPaint.setAntiAlias(true); |
143 fillPaint.setStyle(SkPaint::kFill_Style); | 143 fillPaint.setStyle(SkPaint::kFill_Style); |
144 | 144 |
145 SkTDArray<SkScalar>* prevData = NULL; | 145 SkTDArray<SkScalar>* prevData = nullptr; |
146 for (int i = 0; i < kNumGraphs; ++i) { | 146 for (int i = 0; i < kNumGraphs; ++i) { |
147 gen_paths(fData[i], | 147 gen_paths(fData[i], |
148 prevData, | 148 prevData, |
149 height, | 149 height, |
150 0, | 150 0, |
151 SkIntToScalar(kPixelsPerTick), | 151 SkIntToScalar(kPixelsPerTick), |
152 fShift, | 152 fShift, |
153 &plotPath, | 153 &plotPath, |
154 &fillPath); | 154 &fillPath); |
155 | 155 |
156 // Make the fills partially transparent | 156 // Make the fills partially transparent |
157 fillPaint.setColor((gColors[i] & 0x00ffffff) | 0x80000000); | 157 fillPaint.setColor((gColors[i] & 0x00ffffff) | 0x80000000); |
158 canvas->drawPath(fillPath, fillPaint); | 158 canvas->drawPath(fillPath, fillPaint); |
159 | 159 |
160 plotPaint.setColor(gColors[i]); | 160 plotPaint.setColor(gColors[i]); |
161 canvas->drawPath(plotPath, plotPaint); | 161 canvas->drawPath(plotPath, plotPaint); |
162 | 162 |
163 prevData = fData + i; | 163 prevData = fData + i; |
164 } | 164 } |
165 | 165 |
166 fShift += kShiftPerFrame; | 166 fShift += kShiftPerFrame; |
167 this->inval(NULL); | 167 this->inval(nullptr); |
168 } | 168 } |
169 | 169 |
170 private: | 170 private: |
171 enum { | 171 enum { |
172 kNumGraphs = 5, | 172 kNumGraphs = 5, |
173 kPixelsPerTick = 3, | 173 kPixelsPerTick = 3, |
174 kShiftPerFrame = 1, | 174 kShiftPerFrame = 1, |
175 }; | 175 }; |
176 int fShift; | 176 int fShift; |
177 SkISize fSize; | 177 SkISize fSize; |
178 SkTDArray<SkScalar> fData[kNumGraphs]; | 178 SkTDArray<SkScalar> fData[kNumGraphs]; |
179 typedef SampleView INHERITED; | 179 typedef SampleView INHERITED; |
180 }; | 180 }; |
181 | 181 |
182 ////////////////////////////////////////////////////////////////////////////// | 182 ////////////////////////////////////////////////////////////////////////////// |
183 | 183 |
184 static SkView* MyFactory() { return new ChartView; } | 184 static SkView* MyFactory() { return new ChartView; } |
185 static SkViewRegister reg(MyFactory); | 185 static SkViewRegister reg(MyFactory); |
OLD | NEW |