Skip to content

Commit 3e5f0b4

Browse files
authored
Toggle stacked download graph (#5010)
1 parent d707a7f commit 3e5f0b4

File tree

5 files changed

+90
-1
lines changed

5 files changed

+90
-1
lines changed

app/components/download-graph.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<canvas
1111
{{did-insert this.createChart}}
1212
{{did-update this.updateChart @data}}
13+
{{did-update this.updateStacked @stacked}}
1314
{{will-destroy this.destroyChart}}
1415
/>
1516
{{else}}

app/components/download-graph.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ export default class DownloadGraph extends Component {
5959
}
6060
}
6161

62+
@action updateStacked() {
63+
let { chart, data } = this;
64+
65+
if (chart) {
66+
data.dataset = data.datasets.map(d => {
67+
d.fill = this.args.stacked ? 'origin' : false;
68+
chart.options.scales.y.stacked = this.args.stacked;
69+
return d;
70+
});
71+
chart.data = data;
72+
chart.update();
73+
}
74+
}
75+
6276
@action destroyChart() {
6377
this.chart?.destroy();
6478
}

app/controllers/crate/version.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import Controller from '@ember/controller';
2+
import { action } from '@ember/object';
23
import { inject as service } from '@ember/service';
4+
import { tracked } from '@glimmer/tracking';
35

46
import { task } from 'ember-concurrency';
57
import { alias } from 'macro-decorators';
@@ -11,6 +13,16 @@ export default class CrateVersionController extends Controller {
1113
return this.requestedVersion ? this.currentVersion : this.crate;
1214
}
1315

16+
@tracked stackedGraph = true;
17+
18+
@action setStackedGraph() {
19+
this.stackedGraph = true;
20+
}
21+
22+
@action setUnstackedGraph() {
23+
this.stackedGraph = false;
24+
}
25+
1426
@alias('downloadsContext.version_downloads.content') downloads;
1527
@alias('model.crate') crate;
1628
@alias('model.requestedVersion') requestedVersion;

app/styles/crate/version.module.css

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,37 @@
125125

126126
h4 {
127127
color: var(--main-color-light);
128+
float: left;
128129
}
129130

130131
@media only percy {
131132
display: none;
132133
}
133134
}
135+
136+
.graph-data {
137+
clear: both;
138+
}
139+
140+
.toggle-stacked {
141+
float: right;
142+
margin-top: calc(1.33em - 10px);
143+
margin-bottom: calc(1.33em - 10px);
144+
145+
.trigger {
146+
background-color: var(--main-bg-dark);
147+
font-size: 85%;
148+
padding: 10px;
149+
border: none;
150+
border-radius: 5px;
151+
152+
.trigger-label {
153+
min-width: 65px;
154+
}
155+
}
156+
157+
.dropdown-button {
158+
background: none;
159+
border: 0;
160+
}
161+
}

app/templates/crate/version.hbs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,41 @@
7979
</div>
8080
<div local-class='graph'>
8181
<h4>Downloads over the last 90 days</h4>
82-
<DownloadGraph @data={{this.downloads}} local-class="graph-data" />
82+
<div local-class="toggle-stacked">
83+
<span local-class="toggle-stacked-label">Display as </span>
84+
<Dropdown as |dd|>
85+
<dd.Trigger local-class="trigger">
86+
<span local-class="trigger-label">
87+
{{#if this.stackedGraph}}
88+
Stacked
89+
{{else}}
90+
Unstacked
91+
{{/if}}
92+
</span>
93+
</dd.Trigger>
94+
<dd.Menu as |menu|>
95+
<menu.Item>
96+
<button
97+
type="button"
98+
local-class="dropdown-button"
99+
{{on "click" this.setStackedGraph}}
100+
>
101+
Stacked
102+
</button>
103+
</menu.Item>
104+
<menu.Item>
105+
<button
106+
type="button"
107+
local-class="dropdown-button"
108+
{{on "click" this.setUnstackedGraph}}
109+
>
110+
Unstacked
111+
</button>
112+
</menu.Item>
113+
</dd.Menu>
114+
</Dropdown>
115+
</div>
116+
<DownloadGraph @data={{this.downloads}} @stacked={{this.stackedGraph}} local-class="graph-data" />
83117
</div>
84118
</div>
85119
{{/if}}

0 commit comments

Comments
 (0)