@andyreagan/hedotools 7.0.0 → 7.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -4,4 +4,171 @@ hedotools
4
4
  [![CI](https://github.com/andyreagan/hedotools/actions/workflows/ci.yml/badge.svg)](https://github.com/andyreagan/hedotools/actions/workflows/ci.yml)
5
5
  [![npm](https://img.shields.io/npm/v/@andyreagan/hedotools)](https://www.npmjs.com/package/@andyreagan/hedotools)
6
6
 
7
- a collection of js tools in use at hedonometer.org
7
+ A collection of [D3](https://d3js.org/) tools in use at [hedonometer.org](https://hedonometer.org):
8
+
9
+ - **shifter** — the interactive wordshift graph, provided by [`@andyreagan/d3-shifterator`](https://www.npmjs.com/package/@andyreagan/d3-shifterator) and exposed here as `hedotools.shifter`.
10
+ - **map** — a choropleth of the US states, coloured by happiness.
11
+ - **sankey** — a rank-flow diagram between two time periods.
12
+ - **lens** — a brushable word-score histogram that filters which words count.
13
+ - **barchart** — a ranked, diverging bar chart of per-state happiness.
14
+
15
+ ## Versioning
16
+
17
+ The major version tracks the major version of D3 it supports:
18
+
19
+ | hedotools | D3 | shifter |
20
+ | --------- | --- | ------- |
21
+ | 3.x | v3 | bundled (frozen) |
22
+ | 4.x | v4 | bundled, then 4.5+ via `@andyreagan/d3-shifterator` |
23
+ | 5.x | v5 | `@andyreagan/d3-shifterator` ^5 |
24
+ | 6.x | v6 | `@andyreagan/d3-shifterator` ^6 |
25
+ | 7.x | v7 | `@andyreagan/d3-shifterator` ^7 |
26
+
27
+ The current line (7.x) targets **D3 v7**.
28
+
29
+ ## Installation
30
+
31
+ ```
32
+ npm install @andyreagan/hedotools
33
+ ```
34
+
35
+ The modules are browser scripts that attach to a global `hedotools` namespace.
36
+
37
+ ### Single-file bundle (recommended)
38
+
39
+ `dist/hedotools-bundle.js` inlines `@andyreagan/d3-shifterator`, so you only
40
+ need global `d3` (plus jQuery, and `topojson` for the map). One script tag
41
+ gives you the whole `hedotools.*` namespace:
42
+
43
+ ```html
44
+ <script src="https://d3js.org/d3.v7.min.js"></script>
45
+ <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
46
+ <script src="https://unpkg.com/topojson-client@3"></script>
47
+ <script src="node_modules/@andyreagan/hedotools/dist/hedotools-bundle.js"></script>
48
+ ```
49
+
50
+ `dist/hedotools.js` is the same but *without* d3-shifterator inlined — use it
51
+ if you already load the `shifterator` global yourself (load it before this
52
+ file). Both are produced by `bundle.sh` / `npm run build`.
53
+
54
+ ### Individual sources
55
+
56
+ Or load the raw sources in order — D3, then the d3-shifterator UMD (it defines
57
+ the global `shifterator`), then the hedotools sources:
58
+
59
+ ```html
60
+ <script src="https://d3js.org/d3.v7.min.js"></script>
61
+ <script src="node_modules/@andyreagan/d3-shifterator/dist/shifterator.js"></script>
62
+ <script src="node_modules/@andyreagan/hedotools/js/hedotools.init.v4.js"></script>
63
+ <script src="node_modules/@andyreagan/hedotools/js/hedotools.shifter.js"></script>
64
+ <!-- plus whichever dashboard modules you need: -->
65
+ <script src="node_modules/@andyreagan/hedotools/js/hedotools.barchart.js"></script>
66
+ <script src="node_modules/@andyreagan/hedotools/js/hedotools.map.js"></script>
67
+ <script src="node_modules/@andyreagan/hedotools/js/hedotools.lens.js"></script>
68
+ <script src="node_modules/@andyreagan/hedotools/js/hedotools.sankey.js"></script>
69
+ ```
70
+
71
+ `hedotools.map` and `hedotools.sankey` also use
72
+ [`topojson-client`](https://www.npmjs.com/package/topojson-client) (global
73
+ `topojson`); some helpers use jQuery.
74
+
75
+ ## Usage
76
+
77
+ ### shifter
78
+
79
+ `hedotools.shifter` is a singleton instance of `@andyreagan/d3-shifterator`.
80
+ Drive it with the frequency vectors, word scores (`lens`), and word list:
81
+
82
+ ```js
83
+ hedotools.shifter.shift(refF, compF, lens, words);
84
+ hedotools.shifter
85
+ .setfigure(d3.select("#shift01"))
86
+ .setText(["Why comparison is happier than reference:"])
87
+ .plot();
88
+ ```
89
+
90
+ See the [d3-shifterator README](https://github.com/andyreagan/d3-shifterator)
91
+ for the full shifter API and a [live Observable example](https://observablehq.com/@andyreagan/d3-shifterator-v4).
92
+
93
+ Each module is an **invoked singleton** on the `hedotools` namespace (like
94
+ `hedotools.shifter`) — use it directly, don't call it as a factory:
95
+
96
+ ### barchart
97
+
98
+ ```js
99
+ hedotools.barchart.setfigure(d3.select("#barchart"));
100
+ hedotools.barchart.setdata(data, geodata); // data: per-state values; geodata: geojson w/ properties.name
101
+ hedotools.barchart._figheight(400);
102
+ hedotools.barchart.plot();
103
+ ```
104
+
105
+ ### map
106
+
107
+ ```js
108
+ hedotools.map.setfigure(d3.select("#map"));
109
+ hedotools.map.setdata(geoJson); // us-states topojson
110
+ hedotools.map.plot();
111
+ ```
112
+
113
+ ### lens
114
+
115
+ ```js
116
+ hedotools.lens.setfigure(d3.select("#lens"));
117
+ hedotools.lens.setdata(lens); // array of word-happiness scores
118
+ hedotools.lens.plot();
119
+ ```
120
+
121
+ The lens uses `hedotools.urllib` (bundled) to persist its range in the URL, and
122
+ its brush-move callback recomputes state happiness via `hedotools.computeHapps`
123
+ (also bundled) before redrawing the map/shift. Load the full bundle, or the
124
+ individual sources with `urllib` and `computeHapps` before `lens`.
125
+
126
+ ### sankey
127
+
128
+ ```js
129
+ hedotools.sankey.setfigure(d3.select("#sankey"));
130
+ hedotools.sankey.setdata(oldlist, newlist, stateNames); // two happiness vectors + names
131
+ hedotools.sankey.plot();
132
+ ```
133
+
134
+ `hedotools.nonsankey.js` is an alternate implementation (the cities-page
135
+ variant) that **also** defines `hedotools.sankey` — load it *instead of*
136
+ `hedotools.sankey.js`, never alongside. It ships as a loose file and is not in
137
+ the default bundle.
138
+
139
+ > **Note:** unlike the shifter, the dashboard modules were written for the
140
+ > hedonometer.org pages and read page-level globals (e.g. `allData`, `lens`,
141
+ > `words`, `shiftRef`/`shiftComp`, and the url encoders) in their interaction
142
+ > handlers, where hovering/clicking drives a linked shift. They render
143
+ > standalone from `setdata(...).plot()`, but the hover→shift wiring (and
144
+ > barchart's mousedown click-to-open) expects those globals to be present. See
145
+ > `tests/browser/` for working fixtures.
146
+
147
+ ## Developing
148
+
149
+ ```
150
+ npm install
151
+ npm test # lint + unit + Playwright browser tests
152
+ npm run lint
153
+ npm run test:browser
154
+ npm run build # bundle.sh -> dist/hedotools.js + dist/hedotools-bundle.js
155
+ ```
156
+
157
+ Browser tests live in `tests/browser/`: each module loads as a `<script>` with
158
+ real D3 and is asserted against the SVG it renders (including the v6
159
+ `(event, d)` interaction handlers).
160
+
161
+ Releases publish to npm automatically via GitHub Actions
162
+ ([Trusted Publishing / OIDC](.github/workflows/publish.yml)) when a GitHub
163
+ Release is created:
164
+
165
+ ```
166
+ # bump "version" in package.json, commit
167
+ git tag vX.Y.Z
168
+ git push origin master --tags
169
+ gh release create vX.Y.Z # -> CI runs npm test, then npm publish
170
+ ```
171
+
172
+ ## License
173
+
174
+ [BSD-2-Clause](LICENSE)
package/bundle.sh CHANGED
@@ -1,22 +1,46 @@
1
1
  #!/usr/bin/env bash
2
- # Concatenate the D3 v4 sources into a single browser bundle for hedonometer.org.
3
- # Order matters: init.v4 defines the `hedotools` namespace + helpers first, and
4
- # the dashboard modules (barchart/lens/map/sankey) depend on hedotools.shifter.
2
+ # Build the distributable browser bundles into dist/.
5
3
  #
6
- # NOTE: hedotools.shifter.js only adapts the @andyreagan/d3-shifterator package
7
- # into hedotools.shifter — the page must load D3 v4 and the d3-shifterator UMD
8
- # bundle (global `shifterator`) BEFORE this bundle.
4
+ # Two artifacts, mirroring @andyreagan/d3-shifterator's shifterator.js /
5
+ # shifterator-bundle.js split:
6
+ #
7
+ # dist/hedotools.js the concatenated hedotools sources. Attaches
8
+ # everything to a global `hedotools`. Expects global
9
+ # `d3` (v7) AND `shifterator` (from
10
+ # @andyreagan/d3-shifterator) to be loaded first.
11
+ #
12
+ # dist/hedotools-bundle.js the above with the d3-shifterator UMD inlined, so
13
+ # a consumer needs only global `d3` (plus jQuery, and
14
+ # `topojson` for the map). This is the single-file
15
+ # drop-in for a page that just wants `hedotools.*`.
16
+ #
17
+ # Source order matters: init.v4 defines the `hedotools` namespace + helpers,
18
+ # the dashboard modules depend on it, and hedotools.shifter.js (the adapter)
19
+ # needs the global `shifterator` at load time.
9
20
  set -euo pipefail
10
- cd "$(dirname "$0")/js"
21
+ here="$(cd "$(dirname "$0")" && pwd)"
22
+ cd "$here"
23
+ mkdir -p dist
11
24
 
25
+ # hedotools.nonsankey.js is deliberately NOT bundled: it is an alternate that
26
+ # also defines `hedotools.sankey` (the cities-page variant), so it is loaded
27
+ # INSTEAD of hedotools.sankey.js, never alongside it. It ships as a loose file.
12
28
  cat \
13
- hedotools.init.v4.js \
14
- hedotools.urllib.js \
15
- hedotools.barchart.js \
16
- hedotools.lens.js \
17
- hedotools.map.js \
18
- hedotools.sankey.js \
19
- hedotools.shifter.js \
20
- > hedotools.v4.js
29
+ js/hedotools.init.v4.js \
30
+ js/hedotools.urllib.js \
31
+ js/hedotools.computeHapps.js \
32
+ js/hedotools.barchart.js \
33
+ js/hedotools.lens.js \
34
+ js/hedotools.map.js \
35
+ js/hedotools.sankey.js \
36
+ js/hedotools.shifter.js \
37
+ > dist/hedotools.js
38
+ echo "built dist/hedotools.js"
21
39
 
22
- echo "built js/hedotools.v4.js"
40
+ shifter="node_modules/@andyreagan/d3-shifterator/dist/shifterator.js"
41
+ if [ -f "$shifter" ]; then
42
+ cat "$shifter" dist/hedotools.js > dist/hedotools-bundle.js
43
+ echo "built dist/hedotools-bundle.js"
44
+ else
45
+ echo "WARNING: $shifter not found (run npm install) — skipped dist/hedotools-bundle.js" >&2
46
+ fi