Skip to content

Commit 8f43076

Browse files
committed
Much better performance, especially on resize.
1 parent fa7f436 commit 8f43076

File tree

7 files changed

+70
-17
lines changed

7 files changed

+70
-17
lines changed

.eleventy.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ module.exports = function(eleventyConfig) {
55
// Support yaml data files
66
eleventyConfig.addDataExtension("yaml", contents => yaml.safeLoad(contents))
77

8+
eleventyConfig.addWatchTarget("src/site/survey/**/*.js");
9+
810
// pass images directly through to the output
911
eleventyConfig.addPassthroughCopy("src/site/img");
1012
eleventyConfig.addPassthroughCopy({

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"@zachleat/filter-container": "^1.0.3",
2828
"autoprefixer": "^10.2.5",
2929
"cssnano": "^4.1.10",
30+
"d3": "^7.1.1",
3031
"dotenv": "^8.2.0",
3132
"fast-glob": "^3.2.5",
3233
"gray-matter": "^4.0.2",

src/site/_includes/layouts/base.njk

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ ogimage: "/img/og/default-og-image.png"
3535
<meta name="google-site-verification" content="i5IheizQ6X_M4jNqb_b7vbJrT34i_gvEg8Gxkq9IY_w" />
3636
<meta name="google-site-verification" content="WDIQ_1X8K7XIpPJ5G1Z8KnOqdSeYetPQB4EgoTLfIsc" />
3737
<script type="module">document.documentElement.classList.add("js");</script>
38+
{%- for url in javascripts %}
39+
<script defer src="{{ url }}"></script>
40+
{%- endfor %}
3841
</head>
3942
<body class="bg-blue-900 text-blue-100 leading-relaxed antialiased">
4043
<svg width="0" height="0" aria-hidden="true" style="position: absolute;">
@@ -93,8 +96,5 @@ ogimage: "/img/og/default-og-image.png"
9396
{%- for path in javascriptIncludes %}
9497
{% include path %}
9598
{%- endfor %}
96-
{%- for url in javascripts %}
97-
<script defer src="{{ url }}"></script>
98-
{%- endfor %}
9999
</body>
100100
</html>

src/site/survey/2021.njk

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ layout: layouts/base.njk
44
stylesheets:
55
- /css/d3chart.css
66
javascripts:
7-
- https://d3js.org/d3.v7.min.js
8-
- /js/d3chart.js
9-
- /js/d3chart-survey.js
7+
- /survey/2021/bundle.js
108
gradientColors:
119
sunrise: [ "#F0047F", "#FC814A" ]
1210
blue: [ "#0090c9", "#00c0ad" ]
Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,45 @@ class D3Chart {
6060
this.options.labelColors = this.normalizeColors(this.options.labelColors, this.options.colorMod);
6161
}
6262

63+
onResize(callback) {
64+
if (!("ResizeObserver" in window)) {
65+
window.addEventListener("resize", () => {
66+
callback.call(this);
67+
});
68+
return;
69+
}
70+
71+
let resizeObserver = new ResizeObserver(entries => {
72+
for (let entry of entries) {
73+
// console.log( "resizing", this.target );
74+
callback.call(this);
75+
}
76+
});
77+
78+
resizeObserver.observe(this.target);
79+
}
80+
81+
onDeferInit(callback) {
82+
if (!('IntersectionObserver' in window)) {
83+
callback.call(this);
84+
return;
85+
}
86+
87+
let observer = new IntersectionObserver((entries, observer) => {
88+
entries.forEach(entry => {
89+
if (entry.isIntersecting) {
90+
// console.log( "initing", this.target );
91+
callback.call(this);
92+
observer.unobserve(entry.target);
93+
}
94+
});
95+
}, {
96+
threshold: .1
97+
});
98+
99+
observer.observe(this.target);
100+
}
101+
63102
normalizeColors(colors = [], mod = 0) {
64103
if(mod) {
65104
let c = [];
@@ -349,11 +388,14 @@ class D3VerticalBarChart extends D3Chart {
349388

350389
let data = Object.assign(d3.csvParse(csvData, d3.autoType));
351390

352-
this.render(chart, data);
353-
this.renderLegend(data);
354391

355-
window.addEventListener("resize", () => {
392+
this.onDeferInit(function() {
356393
this.render(chart, data);
394+
this.renderLegend(data);
395+
396+
this.onResize(function() {
397+
this.render(chart, data);
398+
})
357399
});
358400
}
359401

@@ -500,11 +542,13 @@ class D3HorizontalBarChart extends D3Chart {
500542
let csvData = chart.parseDataToCsv(tableId, true);
501543
let data = Object.assign(d3.csvParse(csvData, d3.autoType));
502544

503-
this.render(chart, data);
504-
this.renderLegend(data);
505-
506-
window.addEventListener("resize", () => {
545+
this.onDeferInit(function() {
507546
this.render(chart, data);
547+
this.renderLegend(data);
548+
549+
this.onResize(function() {
550+
this.render(chart, data);
551+
});
508552
});
509553
}
510554

@@ -692,11 +736,13 @@ class D3BubbleChart extends D3Chart {
692736
return b.r - a.r;
693737
});
694738

695-
this.render(chart, data);
696-
this.renderLegend(data);
697-
698-
window.addEventListener("resize", () => {
739+
this.onDeferInit(function() {
699740
this.render(chart, data);
741+
this.renderLegend(data);
742+
743+
this.onResize(function() {
744+
this.render(chart, data);
745+
});
700746
});
701747
}
702748

src/site/survey/2021/js.njk

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
permalink: /survey/2021/bundle.js
3+
---
4+
{% include "../../../../node_modules/d3/dist/d3.min.js" %}
5+
{% include "./d3chart.js" %}
6+
{% include "./d3chart-survey.js" %}

0 commit comments

Comments
 (0)