@andyreagan/hedotools 6.0.0 → 7.1.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 +158 -1
- package/bundle.sh +36 -16
- package/dist/hedotools-bundle.js +4972 -0
- package/dist/hedotools.js +2091 -0
- package/package.json +14 -5
package/README.md
CHANGED
|
@@ -4,4 +4,161 @@ hedotools
|
|
|
4
4
|
[](https://github.com/andyreagan/hedotools/actions/workflows/ci.yml)
|
|
5
5
|
[](https://www.npmjs.com/package/@andyreagan/hedotools)
|
|
6
6
|
|
|
7
|
-
|
|
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
|
+
### barchart
|
|
94
|
+
|
|
95
|
+
```js
|
|
96
|
+
var bc = hedotools.barchart();
|
|
97
|
+
bc.setfigure(d3.select("#barchart"));
|
|
98
|
+
bc.setdata(data, geodata); // data: per-state values; geodata: geojson w/ properties.name
|
|
99
|
+
bc._figheight(400);
|
|
100
|
+
bc.plot();
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### map
|
|
104
|
+
|
|
105
|
+
```js
|
|
106
|
+
var m = hedotools.map();
|
|
107
|
+
m.setfigure(d3.select("#map"));
|
|
108
|
+
m.setdata(geoJson); // us-states topojson
|
|
109
|
+
m.plot();
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### lens
|
|
113
|
+
|
|
114
|
+
```js
|
|
115
|
+
var l = hedotools.lens();
|
|
116
|
+
l.setfigure(d3.select("#lens"));
|
|
117
|
+
l.setdata(lens); // array of word-happiness scores
|
|
118
|
+
l.plot();
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### sankey
|
|
122
|
+
|
|
123
|
+
```js
|
|
124
|
+
var s = hedotools.sankey();
|
|
125
|
+
s.setfigure(d3.select("#sankey"));
|
|
126
|
+
s.setdata(oldlist, newlist, stateNames); // two happiness vectors + names
|
|
127
|
+
s.plot();
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
> **Note:** unlike the shifter, the four dashboard modules were written for the
|
|
131
|
+
> hedonometer.org pages and read page-level globals (e.g. `allData`, `lens`,
|
|
132
|
+
> `words`, `shiftRef`/`shiftComp`, and the url encoders) in their interaction
|
|
133
|
+
> handlers, where hovering/clicking drives a linked shift. They render
|
|
134
|
+
> standalone from `setdata(...).plot()`, but the hover→shift wiring expects
|
|
135
|
+
> those globals to be present. See `tests/browser/` for working fixtures.
|
|
136
|
+
|
|
137
|
+
## Developing
|
|
138
|
+
|
|
139
|
+
```
|
|
140
|
+
npm install
|
|
141
|
+
npm test # lint + unit + Playwright browser tests
|
|
142
|
+
npm run lint
|
|
143
|
+
npm run test:browser
|
|
144
|
+
npm run build # bundle.sh -> dist/hedotools.js + dist/hedotools-bundle.js
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Browser tests live in `tests/browser/`: each module loads as a `<script>` with
|
|
148
|
+
real D3 and is asserted against the SVG it renders (including the v6
|
|
149
|
+
`(event, d)` interaction handlers).
|
|
150
|
+
|
|
151
|
+
Releases publish to npm automatically via GitHub Actions
|
|
152
|
+
([Trusted Publishing / OIDC](.github/workflows/publish.yml)) when a GitHub
|
|
153
|
+
Release is created:
|
|
154
|
+
|
|
155
|
+
```
|
|
156
|
+
# bump "version" in package.json, commit
|
|
157
|
+
git tag vX.Y.Z
|
|
158
|
+
git push origin master --tags
|
|
159
|
+
gh release create vX.Y.Z # -> CI runs npm test, then npm publish
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## License
|
|
163
|
+
|
|
164
|
+
[BSD-2-Clause](LICENSE)
|
package/bundle.sh
CHANGED
|
@@ -1,22 +1,42 @@
|
|
|
1
1
|
#!/usr/bin/env bash
|
|
2
|
-
#
|
|
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
|
-
#
|
|
7
|
-
#
|
|
8
|
-
#
|
|
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")
|
|
21
|
+
here="$(cd "$(dirname "$0")" && pwd)"
|
|
22
|
+
cd "$here"
|
|
23
|
+
mkdir -p dist
|
|
11
24
|
|
|
12
25
|
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.
|
|
26
|
+
js/hedotools.init.v4.js \
|
|
27
|
+
js/hedotools.urllib.js \
|
|
28
|
+
js/hedotools.barchart.js \
|
|
29
|
+
js/hedotools.lens.js \
|
|
30
|
+
js/hedotools.map.js \
|
|
31
|
+
js/hedotools.sankey.js \
|
|
32
|
+
js/hedotools.shifter.js \
|
|
33
|
+
> dist/hedotools.js
|
|
34
|
+
echo "built dist/hedotools.js"
|
|
21
35
|
|
|
22
|
-
|
|
36
|
+
shifter="node_modules/@andyreagan/d3-shifterator/dist/shifterator.js"
|
|
37
|
+
if [ -f "$shifter" ]; then
|
|
38
|
+
cat "$shifter" dist/hedotools.js > dist/hedotools-bundle.js
|
|
39
|
+
echo "built dist/hedotools-bundle.js"
|
|
40
|
+
else
|
|
41
|
+
echo "WARNING: $shifter not found (run npm install) — skipped dist/hedotools-bundle.js" >&2
|
|
42
|
+
fi
|