Skip to content

Commit dedfe4a

Browse files
committed
store lines as coordinates
1 parent e6c85db commit dedfe4a

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

pkg/web_app/lib/src/widget/downloads_chart/widget.dart

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,9 @@ void drawChart(
289289
// Chart lines and legends
290290

291291
final lastestDownloads = List.filled(values.length, 0);
292-
final lines = <StringBuffer>[];
292+
final lines = <List<(double, double)>>[];
293293
for (int versionRange = 0; versionRange < values[0].length; versionRange++) {
294-
final line = StringBuffer();
295-
var c = 'M';
294+
final List<(double, double)> lineCoordinates = <(double, double)>[];
296295
for (int week = 0; week < values.length; week++) {
297296
if (displayMode == DisplayMode.stacked) {
298297
lastestDownloads[week] += values[week][versionRange];
@@ -302,10 +301,36 @@ void drawChart(
302301
final (x, y) = computeCoordinates(
303302
computeDateForWeekNumber(newestDate, values.length, week),
304303
lastestDownloads[week]);
305-
line.write(' $c$x $y');
306-
c = 'L';
304+
lineCoordinates.add((x, y));
307305
}
308-
lines.add(line);
306+
lines.add(lineCoordinates);
307+
}
308+
309+
StringBuffer computeLine(List<(double, double)> coordinates) {
310+
final path = StringBuffer();
311+
var command = 'M';
312+
coordinates.forEach((c) {
313+
path.write(' $command${c.$1} ${c.$2}');
314+
command = 'L';
315+
});
316+
return path;
317+
}
318+
319+
StringBuffer computeArea(List<(double, double)> topCoordinates,
320+
List<(double, double)> bottomCoordinates) {
321+
final path = StringBuffer();
322+
var command = 'M';
323+
topCoordinates.forEach((c) {
324+
path.write(' $command${c.$1} ${c.$2}');
325+
command = 'L';
326+
});
327+
328+
bottomCoordinates.reversed.forEach((c) {
329+
path.write(' $command${c.$1} ${c.$2}');
330+
command = 'L';
331+
});
332+
path.write('Z');
333+
return path;
309334
}
310335

311336
double legendX = xZero;
@@ -315,29 +340,24 @@ void drawChart(
315340
final legendHeight = 8;
316341

317342
for (int i = 0; i < lines.length; i++) {
318-
// We assign colors in reverse order so that main colors are chosen first for
319-
// the newest versions.
320-
final line = lines[lines.length - 1 - i];
321-
343+
// We add the lines in reverse order so that the newest versions get the
344+
// main color.
345+
final line = computeLine(lines[lines.length - 1 - i]);
322346
final path = SVGPathElement();
323347
path.setAttribute('class', '${strokeColorClass(i)} downloads-chart-line ');
324348
path.setAttribute('d', '$line');
325349
path.setAttribute('clip-path', 'url(#clipRect)');
326350
chart.append(path);
327351

328352
if (displayMode == DisplayMode.stacked) {
329-
final area = SVGPathElement();
330-
area.setAttribute('class', '${fillColorClass(i)} downloads-chart-area ');
331353
final prevLine = i == lines.length - 1
332-
? ' M $xZero $yZero L$xMax $yZero'
354+
? [(xZero, yZero), (xMax, yZero)]
333355
: lines[lines.length - 1 - i - 1];
334-
final reversed = prevLine
335-
.toString()
336-
.replaceAll(' M', '')
337-
.split('L')
338-
.reversed
339-
.join('L');
340-
area.setAttribute('d', '$line L$reversed Z');
356+
final areaPath = computeArea(lines[lines.length - 1 - i], prevLine);
357+
final area = SVGPathElement();
358+
area.setAttribute('class', '${fillColorClass(i)} downloads-chart-area ');
359+
area.setAttribute('d', '$areaPath');
360+
area.setAttribute('clip-path', 'url(#clipRect)');
341361
chart.append(area);
342362
}
343363

0 commit comments

Comments
 (0)