Skip to content

Commit f70a77e

Browse files
chore: CHARTS-5035 update the click event examples (#26)
* chore: CHARTS-5035 update the click event examples * chore: CHARTS-5035 update the SDK package version to use the new beta version
1 parent c7e4062 commit f70a77e

File tree

6 files changed

+62
-66
lines changed

6 files changed

+62
-66
lines changed

examples/click-events-basic/package-lock.json

Lines changed: 20 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/click-events-basic/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"build": "parcel build index.html"
99
},
1010
"dependencies": {
11-
"@mongodb-js/charts-embed-dom": "^1.2.0-beta.1",
11+
"@mongodb-js/charts-embed-dom": "^2.1.0-beta.1",
1212
"regenerator-runtime": "^0.13.3",
1313
"parcel": "^1.12.4"
1414
},

examples/click-events-basic/src/index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ import "regenerator-runtime/runtime";
22
import ChartsEmbedSDK from "@mongodb-js/charts-embed-dom";
33

44
const sdk = new ChartsEmbedSDK({
5-
baseUrl: "https://charts.mongodb.com/charts-embedding-examples-wgffp" // Optional: ~REPLACE~ with the Base URL from your Embed Chart dialog
5+
baseUrl: "https://charts.mongodb.com/charts-embedding-examples-wgffp", // Optional: ~REPLACE~ with the Base URL from your Embed Chart dialog
66
});
77

88
const chart = sdk.createChart({
99
chartId: "90a8fe84-dd27-4d53-a3fc-0e40392685dd", // Optional: ~REPLACE~ with the Chart ID from your Embed Chart dialog
10-
height: "700px"
10+
height: "700px",
1111
});
1212

13-
chart.addEventListener("click", (payload) => {
14-
document.getElementById("payload").innerHTML = '<pre>' + JSON.stringify(payload, null, 2) + '</pre>';
13+
const clickHandler = (payload) => {
14+
document.getElementById("payload").innerHTML =
15+
"<pre>" + JSON.stringify(payload, null, 2) + "</pre>";
1516

1617
let infoText = "";
1718
if (payload.target.role) {
@@ -36,10 +37,11 @@ chart.addEventListener("click", (payload) => {
3637
}
3738

3839
document.getElementById("info").innerHTML = "<ul>" + infoText + "</ul>";
39-
});
40+
};
4041

4142
async function renderCharts() {
4243
await chart.render(document.getElementById("chart"));
44+
await chart.addEventListener("click", clickHandler);
4345
}
4446

4547
renderCharts().catch((e) => window.alert(e.message));

examples/click-events-filtering/package-lock.json

Lines changed: 20 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/click-events-filtering/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"build": "parcel build index.html"
99
},
1010
"dependencies": {
11-
"@mongodb-js/charts-embed-dom": "^1.2.0-beta.1",
11+
"@mongodb-js/charts-embed-dom": "^2.1.0-beta.1",
1212
"regenerator-runtime": "^0.13.3",
1313
"parcel": "^1.12.4"
1414
},

examples/click-events-filtering/src/index.js

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,33 @@ import "regenerator-runtime/runtime";
22
import ChartsEmbedSDK from "@mongodb-js/charts-embed-dom";
33

44
const sdk = new ChartsEmbedSDK({
5-
baseUrl: "https://charts.mongodb.com/charts-embedding-examples-wgffp" // Optional: ~REPLACE~ with the Base URL from your Embed Chart dialog
5+
baseUrl: "https://charts.mongodb.com/charts-embedding-examples-wgffp", // Optional: ~REPLACE~ with the Base URL from your Embed Chart dialog
66
});
77

88
const chart1 = sdk.createChart({
99
chartId: "90a8fe84-dd27-4d53-a3fc-0e40392685dd", // Optional: ~REPLACE~ with the Chart ID from your Embed Chart dialog
10-
height: "700px"
10+
height: "700px",
1111
});
1212

1313
const chart2 = sdk.createChart({
1414
chartId: "c6d6b83e-b096-4bb8-9d7f-b7344177cd11", // Optional: ~REPLACE~ with the Chart ID from your Embed Chart dialog
15-
height: "700px"
15+
height: "700px",
1616
});
1717

18-
chart1.addEventListener("click", (payload) => {
19-
if (payload.target.role === "mark") {
20-
// Optional: ~REPLACE~ this with a suitable filter if you're using your own chart
21-
const filter = {
22-
genres: payload.data.y.value,
23-
year: {
24-
$gte: payload.data.color.lowerBound,
25-
$lt: payload.data.color.upperBound
26-
}
27-
};
28-
chart2.setFilter(filter);
29-
document.getElementById("filterMessage").innerText = `${payload.data.y.value} movies from the ${payload.data.color.lowerBound}s`
30-
31-
} else {
32-
chart2.setFilter({});
33-
document.getElementById("filterMessage").innerText = "all movies"
34-
}
35-
});
18+
const clickHandler = (payload) => {
19+
// Optional: ~REPLACE~ this with a suitable filter if you're using your own chart
20+
chart2.setFilter(payload.selectionFilter);
21+
document.getElementById(
22+
"filterMessage"
23+
).innerText = `${payload.data.y.value} movies from the ${payload.data.color.lowerBound}s`;
24+
};
3625

3726
async function renderCharts() {
3827
await chart1.render(document.getElementById("chart1"));
3928
await chart2.render(document.getElementById("chart2"));
29+
30+
const options = { includes: [{ roles: ["mark"] }] };
31+
await chart1.addEventListener("click", clickHandler, options);
4032
}
4133

4234
renderCharts().catch((e) => window.alert(e.message));

0 commit comments

Comments
 (0)