33// BSD-style license that can be found in the LICENSE file.
44
55import 'dart:convert' ;
6+ import 'dart:js_interop' ;
67import 'dart:math' as math;
78
89import 'package:_pub_shared/data/download_counts_data.dart' ;
@@ -111,6 +112,7 @@ void create(HTMLElement element, Map<String, String> options) {
111112 currentDisplayList = displayList;
112113 drawChart (
113114 svg,
115+ svg.getBoundingClientRect ().width,
114116 toolTip,
115117 displayList,
116118 data.newestDate,
@@ -143,6 +145,7 @@ void create(HTMLElement element, Map<String, String> options) {
143145 currentDisplayMode = displayMode;
144146 drawChart (
145147 svg,
148+ svg.getBoundingClientRect ().width,
146149 toolTip,
147150 currentDisplayList,
148151 data.newestDate,
@@ -152,17 +155,41 @@ void create(HTMLElement element, Map<String, String> options) {
152155 });
153156 });
154157
158+ void resize (double width) {
159+ element.removeChild (svg);
160+ svg = createNewSvg ();
161+ element.append (svg);
162+
163+ drawChart (
164+ svg,
165+ width,
166+ toolTip,
167+ currentDisplayList,
168+ data.newestDate,
169+ totals,
170+ );
171+ }
172+
173+ final resizeObserver = ResizeObserver (
174+ (JSArray <ResizeObserverEntry > a, ResizeObserverBoxOptions b) {
175+ resize (a.toDart[0 ].contentRect.width);
176+ }.toJS);
177+
155178 drawChart (
156179 svg,
180+ svg.getBoundingClientRect ().width,
157181 toolTip,
158- majorDisplayLists ,
182+ currentDisplayList ,
159183 data.newestDate,
160184 totals,
161185 );
186+
187+ resizeObserver.observe (element);
162188}
163189
164190void drawChart (
165191 Element svg,
192+ double width,
166193 HTMLDivElement toolTip,
167194 ({List <String > ranges, List <List <int >> weekLists}) displayLists,
168195 DateTime newestDate,
@@ -173,8 +200,7 @@ void drawChart(
173200
174201 if (values.isEmpty) return ;
175202
176- final frameWidth =
177- 775 ; // TODO(zarah): Investigate if this width can be dynamic
203+ final frameWidth = width;
178204 final topPadding = 30 ;
179205 final leftPadding = 30 ;
180206 final rightPadding = 70 ; // Make extra room for labels on y-axis
@@ -266,8 +292,15 @@ void drawChart(
266292
267293 final tickLabel = SVGTextElement ();
268294 chart.append (tickLabel);
269- tickLabel.setAttribute (
270- 'class' , 'downloads-chart-tick-label downloads-chart-tick-label-x' );
295+
296+ if (week % 8 == 0 ) {
297+ // We skip every other label on small screens.
298+ tickLabel.setAttribute ('class' ,
299+ 'downloads-chart-tick-label downloads-chart-anchored-tick-label-x' );
300+ } else {
301+ tickLabel.setAttribute (
302+ 'class' , 'downloads-chart-tick-label downloads-chart-tick-label-x' );
303+ }
271304 tickLabel.text = formatAbbrMonthDay (date);
272305 tickLabel.setAttribute ('y' , '$tickLabelYCoordinate ' );
273306 tickLabel.setAttribute ('x' , '$x ' );
@@ -460,12 +493,15 @@ void drawChart(
460493 }
461494
462495 cursor.setAttribute ('style' , 'opacity:1' );
463- toolTip.setAttribute (
464- 'style' ,
465- 'top:${e .y + toolTipOffsetFromMouse + document .scrollingElement !.scrollTop }px;'
466- 'left:${e .x }px;' );
467496
468497 final pointPercentage = (e.x - boundingRect.x - xZero) / chartWidth;
498+ final horizontalPosition =
499+ e.x + toolTip.getBoundingClientRect ().width > width
500+ ? 'left:${e .x - toolTip .getBoundingClientRect ().width }px;'
501+ : 'left:${e .x }px;' ;
502+ toolTip.setAttribute ('style' ,
503+ 'top:${e .y + toolTipOffsetFromMouse + document .scrollingElement !.scrollTop }px;$horizontalPosition ' );
504+
469505 final nearestIndex = ((values.length - 1 ) * pointPercentage).round ();
470506 final selectedDay =
471507 computeDateForWeekNumber (newestDate, values.length, nearestIndex);
0 commit comments