Skip to content

Commit ca4218a

Browse files
mbostockpstuffa
andauthored
google-analytics edits (#759)
* checkpoint google-analytics edits * paired down version * remove unused horizon chart component * fix height, note * more polish * more polish * more polish * fix options --------- Co-authored-by: Paul Buffa <[email protected]>
1 parent 4f654b4 commit ca4218a

11 files changed

+188
-355
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as Plot from "npm:@observablehq/plot";
2+
3+
export function lineChart(data, {width, height = 94, x = "date", y, percent} = {}) {
4+
return Plot.plot({
5+
width,
6+
height,
7+
axis: null,
8+
margin: 0,
9+
insetTop: 10,
10+
insetLeft: -17,
11+
insetRight: -17,
12+
y: {zero: true, percent, domain: percent ? [0, 100] : undefined},
13+
marks: [Plot.areaY(data, {x, y, fillOpacity: 0.2}), Plot.lineY(data, {x, y, tip: true})]
14+
});
15+
}

examples/google-analytics/docs/components/marimekko.js renamed to examples/google-analytics/docs/components/marimekkoChart.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,41 @@
11
import * as Plot from "npm:@observablehq/plot";
22
import * as d3 from "npm:d3";
33

4-
export function Marimekko({x, y, z, value = z, anchor = "middle", inset = 0.5, ...options} = {}) {
4+
export function marimekkoChart(data, {x, y, value, color, ...options} = {}) {
5+
const xy = (options) => Marimekko({...options, x, y, value});
6+
return Plot.plot({
7+
...options,
8+
label: null,
9+
x: {percent: true, tickFormat: (d) => d + (d === 100 ? "%" : "")},
10+
y: {percent: true, tickFormat: (d) => d + (d === 100 ? "%" : "")},
11+
color,
12+
marks: [
13+
Plot.rect(data, xy({fill: y})),
14+
Plot.text(data, xy({text: y, dy: -16, fill: "black"})),
15+
Plot.text(data, xy({text: (d) => d[value].toLocaleString("en-US"), fontSize: 14, fontWeight: 600, fill: "black"})),
16+
Plot.text(
17+
data,
18+
Plot.selectMaxY(
19+
xy({
20+
z: x,
21+
text: (d) => capitalize(d[x]),
22+
anchor: "top",
23+
lineAnchor: "bottom",
24+
fontSize: 12,
25+
dy: -6
26+
})
27+
)
28+
)
29+
]
30+
});
31+
}
32+
33+
function capitalize([t, ...text]) {
34+
return t.toUpperCase() + text.join("");
35+
}
36+
37+
// https://observablehq.com/@observablehq/plot-marimekko
38+
function Marimekko({x, y, z, value = z, anchor = "middle", inset = 0.5, ...options} = {}) {
539
const stackX = /\bleft$/i.test(anchor) ? Plot.stackX1 : /\bright$/i.test(anchor) ? Plot.stackX2 : Plot.stackX;
640
const stackY = /^top\b/i.test(anchor) ? Plot.stackY2 : /^bottom\b/i.test(anchor) ? Plot.stackY1 : Plot.stackY;
741
const [X, setX] = Plot.column(x);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import * as Plot from "npm:@observablehq/plot";
2+
import * as d3 from "npm:d3";
3+
4+
export function punchcardChart(data, {label, value, ...options} = {}) {
5+
const aggregatedValues = d3
6+
.rollups(
7+
data,
8+
(v) => d3.median(v, (d) => d[value]),
9+
(d) => d.hour,
10+
(d) => d.dayOfWeek
11+
)
12+
.flatMap((d) => d[1].map((d) => d[1]));
13+
return Plot.plot({
14+
...options,
15+
inset: 12,
16+
padding: 0,
17+
marginBottom: 10,
18+
grid: true,
19+
round: false,
20+
label: null,
21+
x: {
22+
axis: "top",
23+
domain: d3.range(24),
24+
interval: 1,
25+
tickFormat: (d) => (d % 12 || 12) + (d === 0 ? " AM" : d === 12 ? " PM" : "")
26+
},
27+
y: {
28+
domain: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
29+
tickFormat: (d) => d.substr(0, 3)
30+
},
31+
r: {label, range: [1, 15], domain: d3.extent(aggregatedValues)},
32+
marks: [
33+
Plot.dot(
34+
data,
35+
Plot.group(
36+
{r: "median"},
37+
{
38+
y: "dayOfWeek",
39+
x: "hour",
40+
r: value,
41+
fill: "currentColor",
42+
stroke: "var(--theme-background)",
43+
sort: null,
44+
tip: true
45+
}
46+
)
47+
)
48+
]
49+
});
50+
}

examples/google-analytics/docs/components/trend.js

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {html} from "npm:htl";
2+
3+
export function trendNumber(data, {focus, value, ...options} = {}) {
4+
const focusIndex = data.findIndex((d) => d === focus);
5+
return formatTrend(focus[value] - data[focusIndex - 1]?.[value], options);
6+
}
7+
8+
export function formatTrend(
9+
value,
10+
{
11+
locale,
12+
format = {},
13+
positive = "green",
14+
negative = "red",
15+
base = "muted",
16+
positiveSuffix = " ↗︎",
17+
negativeSuffix = " ↘︎",
18+
baseSuffix = ""
19+
} = {}
20+
) {
21+
if (format.signDisplay === undefined) format = {...format, signDisplay: "always"};
22+
return html`<span class="small ${value > 0 ? positive : value < 0 ? negative : base}">${value.toLocaleString(
23+
locale,
24+
format
25+
)}${value > 0 ? positiveSuffix : value < 0 ? negativeSuffix : baseSuffix}`;
26+
}

examples/google-analytics/docs/data/countries-110m.json.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

examples/google-analytics/docs/data/google-analytics-channels.csv.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

examples/google-analytics/docs/data/google-analytics-country.csv.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

examples/google-analytics/docs/data/google-analytics-time-of-day.csv.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import {runReport} from "./google-analytics.js";
44
const response = await runReport({
55
dateRanges: [{startDate: "2023-04-01", endDate: "2023-12-31"}],
66
dimensions: [{name: "hour"}, {name: "dayOfWeekName"}],
7-
metrics: [{name: "activeUsers"}]
7+
metrics: [{name: "activeUsers"}, {name: "newUsers"}]
88
});
99

1010
process.stdout.write(
1111
csvFormat(
1212
response.rows.map((d) => ({
1313
hour: d.dimensionValues[0].value,
1414
dayOfWeek: d.dimensionValues[1].value,
15-
activeUsers: d.metricValues[0].value
15+
activeUsers: d.metricValues[0].value,
16+
newUsers: d.metricValues[1].value
1617
}))
1718
)
1819
);

examples/google-analytics/docs/data/google-analytics.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ const defaultDimensionFilter = {
1616
filter: {
1717
fieldName: "fullPageUrl",
1818
stringFilter: {
19-
value: "observablehq.com/plot/"
19+
value: "observablehq.com/plot",
20+
matchType: "BEGINS_WITH",
21+
caseSensitive: false
2022
}
2123
}
2224
};

0 commit comments

Comments
 (0)