Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions app/lib/frontend/dom/material.dart
Original file line number Diff line number Diff line change
Expand Up @@ -449,18 +449,17 @@ d.Node radioButtons({
nodes.add(d.strong(text: leadingText));
}
radios.forEach((e) {
nodes.add(d.input(
final button = (d.input(
id: e.id,
type: 'radio',
name: name,
value: e.value,
classes: [
...?classes,
],
attributes: {if (e.value == initialValue) 'checked': ''},
));
nodes.add(d.label(attributes: {'for': e.id}, child: d.text(e.label)));
final label = d.label(attributes: {'for': e.id}, child: d.text(e.label));
final div = d.div(children: [button, label]);
nodes.add(div);
});

return d.div(classes: ['mdc-form-field'], children: nodes);
return d.div(classes: ['mdc-form-field', ...?classes], children: nodes);
}
40 changes: 26 additions & 14 deletions app/test/frontend/golden/pkg_score_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -308,25 +308,37 @@ <h3>
</p>
<h1>Weekly downloads</h1>
<div class="downloads-chart-display-modes">
<div class="mdc-form-field">
<div class="mdc-form-field downloads-chart-radio-button">
<strong>Display as:</strong>
<input id="display-modes-unstacked" class="downloads-chart-radio-button" type="radio" name="display-modes" value="unstacked" checked=""/>
<label for="display-modes-unstacked">Unstacked</label>
<input id="version-modes-stacked" class="downloads-chart-radio-button" type="radio" name="display-modes" value="stacked"/>
<label for="version-modes-stacked">Stacked</label>
<input id="version-modes-percentage" class="downloads-chart-radio-button" type="radio" name="display-modes" value="percentage"/>
<label for="version-modes-percentage">Percentage</label>
<div>
<input id="display-modes-unstacked" type="radio" name="display-modes" value="unstacked" checked=""/>
<label for="display-modes-unstacked">Unstacked</label>
</div>
<div>
<input id="version-modes-stacked" type="radio" name="display-modes" value="stacked"/>
<label for="version-modes-stacked">Stacked</label>
</div>
<div>
<input id="version-modes-percentage" type="radio" name="display-modes" value="percentage"/>
<label for="version-modes-percentage">Percentage</label>
</div>
</div>
</div>
<div class="downloads-chart-version-modes">
<div class="mdc-form-field">
<div class="mdc-form-field downloads-chart-radio-button">
<strong>By versions:</strong>
<input id="version-modes-major" class="downloads-chart-radio-button" type="radio" name="version-modes" value="major" checked=""/>
<label for="version-modes-major">Major</label>
<input id="version-modes-minor" class="downloads-chart-radio-button" type="radio" name="version-modes" value="minor"/>
<label for="version-modes-minor">Minor</label>
<input id="version-modes-patch" class="downloads-chart-radio-button" type="radio" name="version-modes" value="patch"/>
<label for="version-modes-patch">Patch</label>
<div>
<input id="version-modes-major" type="radio" name="version-modes" value="major" checked=""/>
<label for="version-modes-major">Major</label>
</div>
<div>
<input id="version-modes-minor" type="radio" name="version-modes" value="minor"/>
<label for="version-modes-minor">Minor</label>
</div>
<div>
<input id="version-modes-patch" type="radio" name="version-modes" value="patch"/>
<label for="version-modes-patch">Patch</label>
</div>
</div>
</div>
<div id="-downloads-chart" class="downloads-chart" data-widget="downloads-chart" data-downloads-chart-points="%%downloads-chart-points%%" data-downloads-chart-versions-radio="version-modes" data-downloads-chart-display-radio="display-modes"></div>
Expand Down
40 changes: 26 additions & 14 deletions app/test/frontend/golden/pkg_score_page_with_downloads_chart.html
Original file line number Diff line number Diff line change
Expand Up @@ -308,25 +308,37 @@ <h3>
</p>
<h1>Weekly downloads</h1>
<div class="downloads-chart-display-modes">
<div class="mdc-form-field">
<div class="mdc-form-field downloads-chart-radio-button">
<strong>Display as:</strong>
<input id="display-modes-unstacked" class="downloads-chart-radio-button" type="radio" name="display-modes" value="unstacked" checked=""/>
<label for="display-modes-unstacked">Unstacked</label>
<input id="version-modes-stacked" class="downloads-chart-radio-button" type="radio" name="display-modes" value="stacked"/>
<label for="version-modes-stacked">Stacked</label>
<input id="version-modes-percentage" class="downloads-chart-radio-button" type="radio" name="display-modes" value="percentage"/>
<label for="version-modes-percentage">Percentage</label>
<div>
<input id="display-modes-unstacked" type="radio" name="display-modes" value="unstacked" checked=""/>
<label for="display-modes-unstacked">Unstacked</label>
</div>
<div>
<input id="version-modes-stacked" type="radio" name="display-modes" value="stacked"/>
<label for="version-modes-stacked">Stacked</label>
</div>
<div>
<input id="version-modes-percentage" type="radio" name="display-modes" value="percentage"/>
<label for="version-modes-percentage">Percentage</label>
</div>
</div>
</div>
<div class="downloads-chart-version-modes">
<div class="mdc-form-field">
<div class="mdc-form-field downloads-chart-radio-button">
<strong>By versions:</strong>
<input id="version-modes-major" class="downloads-chart-radio-button" type="radio" name="version-modes" value="major" checked=""/>
<label for="version-modes-major">Major</label>
<input id="version-modes-minor" class="downloads-chart-radio-button" type="radio" name="version-modes" value="minor"/>
<label for="version-modes-minor">Minor</label>
<input id="version-modes-patch" class="downloads-chart-radio-button" type="radio" name="version-modes" value="patch"/>
<label for="version-modes-patch">Patch</label>
<div>
<input id="version-modes-major" type="radio" name="version-modes" value="major" checked=""/>
<label for="version-modes-major">Major</label>
</div>
<div>
<input id="version-modes-minor" type="radio" name="version-modes" value="minor"/>
<label for="version-modes-minor">Minor</label>
</div>
<div>
<input id="version-modes-patch" type="radio" name="version-modes" value="patch"/>
<label for="version-modes-patch">Patch</label>
</div>
</div>
</div>
<div id="-downloads-chart" class="downloads-chart" data-widget="downloads-chart" data-downloads-chart-points="%%downloads-chart-points%%" data-downloads-chart-versions-radio="version-modes" data-downloads-chart-display-radio="display-modes"></div>
Expand Down
54 changes: 45 additions & 9 deletions pkg/web_app/lib/src/widget/downloads_chart/widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:convert';
import 'dart:js_interop';
import 'dart:math' as math;

import 'package:_pub_shared/data/download_counts_data.dart';
Expand Down Expand Up @@ -111,6 +112,7 @@ void create(HTMLElement element, Map<String, String> options) {
currentDisplayList = displayList;
drawChart(
svg,
svg.getBoundingClientRect().width,
toolTip,
displayList,
data.newestDate,
Expand Down Expand Up @@ -143,6 +145,7 @@ void create(HTMLElement element, Map<String, String> options) {
currentDisplayMode = displayMode;
drawChart(
svg,
svg.getBoundingClientRect().width,
toolTip,
currentDisplayList,
data.newestDate,
Expand All @@ -152,17 +155,41 @@ void create(HTMLElement element, Map<String, String> options) {
});
});

void resize(double width) {
element.removeChild(svg);
svg = createNewSvg();
element.append(svg);

drawChart(
svg,
width,
toolTip,
currentDisplayList,
data.newestDate,
totals,
);
}

final resizeObserver = ResizeObserver(
(JSArray<ResizeObserverEntry> a, ResizeObserverBoxOptions b) {
resize(a.toDart[0].contentRect.width);
}.toJS);

drawChart(
svg,
svg.getBoundingClientRect().width,
toolTip,
majorDisplayLists,
currentDisplayList,
data.newestDate,
totals,
);

resizeObserver.observe(element);
}

void drawChart(
Element svg,
double width,
HTMLDivElement toolTip,
({List<String> ranges, List<List<int>> weekLists}) displayLists,
DateTime newestDate,
Expand All @@ -173,8 +200,7 @@ void drawChart(

if (values.isEmpty) return;

final frameWidth =
775; // TODO(zarah): Investigate if this width can be dynamic
final frameWidth = width;
final topPadding = 30;
final leftPadding = 30;
final rightPadding = 70; // Make extra room for labels on y-axis
Expand Down Expand Up @@ -266,8 +292,15 @@ void drawChart(

final tickLabel = SVGTextElement();
chart.append(tickLabel);
tickLabel.setAttribute(
'class', 'downloads-chart-tick-label downloads-chart-tick-label-x');

if (week % 8 == 0) {
// We skip every other label on small screens.
tickLabel.setAttribute('class',
'downloads-chart-tick-label downloads-chart-anchored-tick-label-x');
} else {
tickLabel.setAttribute(
'class', 'downloads-chart-tick-label downloads-chart-tick-label-x');
}
tickLabel.text = formatAbbrMonthDay(date);
tickLabel.setAttribute('y', '$tickLabelYCoordinate');
tickLabel.setAttribute('x', '$x');
Expand Down Expand Up @@ -460,12 +493,15 @@ void drawChart(
}

cursor.setAttribute('style', 'opacity:1');
toolTip.setAttribute(
'style',
'top:${e.y + toolTipOffsetFromMouse + document.scrollingElement!.scrollTop}px;'
'left:${e.x}px;');

final pointPercentage = (e.x - boundingRect.x - xZero) / chartWidth;
final horizontalPosition =
e.x + toolTip.getBoundingClientRect().width > width
? 'left:${e.x - toolTip.getBoundingClientRect().width}px;'
: 'left:${e.x}px;';
toolTip.setAttribute('style',
'top:${e.y + toolTipOffsetFromMouse + document.scrollingElement!.scrollTop}px;$horizontalPosition');

final nearestIndex = ((values.length - 1) * pointPercentage).round();
final selectedDay =
computeDateForWeekNumber(newestDate, values.length, nearestIndex);
Expand Down
21 changes: 20 additions & 1 deletion pkg/web_css/lib/src/_pkg.scss
Original file line number Diff line number Diff line change
Expand Up @@ -279,21 +279,32 @@
.downloads-chart {
display: flex;
height: 700px;
width: 775px;
width: 100%;
flex-direction: column;
margin-top: 16px;
}

.downloads-chart-version-modes {
float: right;

@media (max-width: variables.$downloads-chart-display-max-width) {
float: left;
}
}

.downloads-chart-display-modes {
float: left;
}

.downloads-chart-radio-button {
display: flex;
flex-direction: row;
margin-left: 10px;

@media (max-width: variables.$downloads-chart-radio-max-width) {
display: flex;
flex-direction: column;
}
}

.downloads-chart-axis-line {
Expand Down Expand Up @@ -358,6 +369,14 @@

.downloads-chart-tick-label-x {
text-anchor: middle;
@media (max-width: variables.$downloads-chart-label-max-width) {
display: none;
}
}

// These labels are not removed even if the screen gets smaller.
.downloads-chart-anchored-tick-label-x {
text-anchor: middle;
}

.downloads-chart-tick-label-y {
Expand Down
4 changes: 4 additions & 0 deletions pkg/web_css/lib/src/_variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ $device-desktop-min-width: 641px;
$device-mobile-max-width: 640px;
$device-tablet-max-width: 979px;

$downloads-chart-label-max-width: 825px;
$downloads-chart-display-max-width: 1025px;
$downloads-chart-radio-max-width: 650px;

$z-index-nav-mask: 1000;

$site-max-width: 1136px;
Expand Down
Loading