Skip to content

Commit 4b216bc

Browse files
khangusleeMuthukrishnan Krishnamurthy
andauthored
feat: CHARTS-6392 Add Dashboard Embedding examples (#59)
* feat: Render unauthenticated embedded dashboard in example * feat: Add theme toggle for unauthenticated dashboard example * feat: Add height/width mode select * feat: Add charts background colour select * style: Prettify buttons and dropdowns * feat: Add refresh and max data age to unauthenticated dashboard example * feat: Add embedded authenticated custom JWT dashboard example * feat: Add embedded authenticated google dashboard example * feat: Add embedded authenticated realm dashboard example * chore: Update dashboard examples with latest charts-embed-dom package * chore: Replace Embed Chart comment with Embed Dashboard * style: Remove unused font from unauthenticated dashboard example * feat: Create basic unauthenticated embedded dashboard to use getAllCharts method * feat: Refresh a single chart that you request for * feat: Display clicked element information * chore: Replace charts link with prod example links for authenticated examples * chore: Replace charts link with prod example links for unauthenticated examples * refactor: Replace mentions of refresh interval with max data age * doc: Add README for unauthenticated embedded dashboard * doc: Add README for unauthenticated embedded dashboard using getAllCharts and getChart methods * doc: Add README files for all authenticated dashboard examples * doc: Remove links to code sandbox for authenticated examples that do not work * doc: Add links to dashboard embedding documentation * feat: Flip theme text for unauthenticated embedded dashboard * doc: Clear up max data age documentation in README * Updated sdk version 2.2.0 * doc: Add links to embedded dashboard examples on main README Co-authored-by: Muthukrishnan Krishnamurthy <muthukrish.k@M-C02DQ489MD6M.modem>
1 parent b18f5c4 commit 4b216bc

File tree

35 files changed

+39201
-20
lines changed

35 files changed

+39201
-20
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,11 @@ yarn add @mongodb-js/charts-embed-dom
3838
- [Getting started with click events](https://github.com/mongodb-js/charts-embed-sdk/tree/master/examples/charts/click-events-basic)
3939
- [Interactive filtering using click events](https://github.com/mongodb-js/charts-embed-sdk/tree/master/examples/charts/click-events-filtering)
4040
- [Programmatic highlighting](https://github.com/mongodb-js/charts-embed-sdk/tree/master/examples/charts/programmatic-highlighting)
41+
42+
### Embedded Dashboard Guides
43+
44+
- [Embedding an unauthenticated dashboard](https://github.com/mongodb-js/charts-embed-sdk/tree/master/examples/dashboard/unauthenticated)
45+
- [Dashboard embedding with custom JWT authentication](https://github.com/mongodb-js/charts-embed-sdk/tree/master/examples/dashboard/authenticated-custom-jwt)
46+
- [Dashboard embedding with Realm authentication](https://github.com/mongodb-js/charts-embed-sdk/tree/master/examples/dashboard/authenticated-realm)
47+
- [Dashboard embedding with Google authentication](https://github.com/mongodb-js/charts-embed-sdk/tree/master/examples/dashboard/authenticated-google)
48+
- [Accessing a Chart instance via Dashboard class](https://github.com/mongodb-js/charts-embed-sdk/tree/master/examples/dashboard/unauthenticated-get-chart)

examples/README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ This directory contains example applications making use of both unauthenticated
1111

1212
in the example directory of your choosing.
1313

14-
---
14+
## Embedded Charts
1515

1616
The [Unauthenticated](https://github.com/mongodb-js/charts-embed-sdk/tree/master/examples/charts/unauthenticated) example is the best place to see all the current SDK features being used. Since it doesn't require authentication, it's the easiest example to set up and follow along with.
1717

@@ -28,6 +28,24 @@ If you'd like to build **interactive** charts, we have two examples showing this
2828

2929
There is also an [example](https://github.com/mongodb-js/charts-embed-sdk/tree/master/examples/charts/timeline-charts-example) of how to build an animated timeline chart.
3030

31+
## Embedded Dashboard
32+
33+
The embedded dashboards are very similar to the embedded charts examples, but with a few differences that are highlighted in each example.
34+
35+
The easiest method of embedding a dashboard is by following the [unauthenticated example](https://github.com/mongodb-js/charts-embed-sdk/tree/master/examples/dashboard/unauthenticated).
36+
37+
For interactive purposes, you can also look at the [unauthenticated get chart example](https://github.com/mongodb-js/charts-embed-sdk/tree/master/examples/dashboard/unauthenticated-get-chart) where you can attach a click event listener to individual charts on an embedded dashboard.
38+
39+
If your dashboard needs some privacy, you can look at our authenticated examples which uses the same authentication providers as the embedded chart examples too.
40+
41+
42+
- [Custom JWT](https://github.com/mongodb-js/charts-embed-sdk/tree/master/examples/dashboard/authenticated-custom-jwt)
43+
- [MongoDB Realm](https://github.com/mongodb-js/charts-embed-sdk/tree/master/examples/dashboard/authenticated-realm)
44+
- [Google](https://github.com/mongodb-js/charts-embed-sdk/tree/master/examples/dashboard/authenticated-google)
45+
46+
47+
---
48+
3149
We hope these resources help, and as always,
3250

3351
Happy Charting! 🚀 📈

examples/charts/authenticated-google/README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
📄 _[See the MongoDB Charts Embedding Docs for more details](https://docs.mongodb.com/charts/saas/embedding-charts/)_
66

7-
🎮 _[Play with a live demo of this sample here](https://codesandbox.io/s/github/mongodb-js/charts-embed-sdk/tree/master/examples/charts/authenticated-google)_
8-
97
MongoDB Charts allows you to create visualizations of your MongoDB data using a simple web interface. You can view the visualizations within the Charts UI, or you can use the Embedding feature to render the charts in an external web application.
108

119
Charts can be embedded either using a simple IFRAME snippet, or by using the Charts Embedding SDK from your JavaScript code. When using the SDK, embedded charts can be either unauthenticated (meaning anyone who has the embed code can view the chart), or authenticated (whereby the user can only view the chart if they have an active authentication session linked to a Charts authentication provider).
@@ -85,7 +83,6 @@ Once you gain an understanding of the API, consider the following
8583
- Consider how you want to secure your data with the Google Embedding Provider. Anybody can create a Google account. Consider using Charts Injected Filters to make your application more secure.
8684

8785
- For example, consider the following injected filter
88-
-
8986

9087
```javascript
9188
// Reject tokens that aren't from example.com domain

examples/charts/unauthenticated/README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ This sample shows how to use the JavaScript Embedding SDK to render unauthentica
1717
- Render an embedded chart on a web page
1818
- Set the charts theme to either `'light'` or `'dark'`
1919
- Get the charts current theme
20-
- Set the charts refresh interval
21-
- Note, the default interval is 0, implying that after rendering, the chart will never refresh.
22-
- The minimum refresh interval is 10 seconds.
23-
- Get the charts current refresh interval
20+
- Set the charts max data age to various values
21+
- Get the charts current max data age
2422
- Manually refresh the chart
2523
- Set the charts current filter
2624
- Note, filtering on a chart requires setting up white listed fields in MongoDB Charts. We have done this for our sample data.

examples/charts/unauthenticated/index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
<button id="refresh"></button>
2525
</label>
2626
<label
27-
><span>REFRESH INTERVAL </span>
28-
<select id="refresh-interval">
27+
><span>MAX DATA AGE </span>
28+
<select id="maxDataAge">
2929
<option value="-1">None</option>
3030
<option value="10">10 Seconds</option>
3131
<option value="60"><span>1 Minute</span></option>
@@ -61,8 +61,8 @@
6161
<h3 class="chart-title">CHART DATA</h3>
6262
<table>
6363
<tr>
64-
<th>Current Refresh Interval (in secs)</th>
65-
<td id="currentRefreshInterval">3600</td>
64+
<th>Current Max Data Age (in secs)</th>
65+
<td id="currentMaxDataAge">3600</td>
6666
</tr>
6767
<tr>
6868
<th>Current Theme</th>

examples/charts/unauthenticated/src/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ async function renderChart() {
2727
2828
chart.getMaxDataAge()
2929
Returns a number pertaining to the charts current
30-
refresh interval.
30+
max data age.
3131
*/
3232
document
33-
.getElementById("refresh-interval")
33+
.getElementById("maxDataAge")
3434
.addEventListener("change", async (e) => {
35-
var refreshInterval = e.target.value;
36-
refreshInterval
37-
? chart.setMaxDataAge(Number(refreshInterval))
35+
var maxDataAge = e.target.value;
36+
maxDataAge
37+
? chart.setMaxDataAge(Number(maxDataAge))
3838
: chart.setMaxDataAge(3600);
3939

40-
var currentRefreshInterval = await chart.getMaxDataAge();
41-
document.getElementById("currentRefreshInterval").innerText =
42-
currentRefreshInterval;
40+
var currentMaxDataAge = await chart.getMaxDataAge();
41+
document.getElementById("currentMaxDataAge").innerText =
42+
currentMaxDataAge;
4343
});
4444

4545
/*
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# MongoDB Charts Embedding Example - Authenticated Embedded Dashboard (Custom JWT)
2+
3+
## Background
4+
5+
📄 _[See the MongoDB Charts Embedding Docs for more details](https://dochub.mongodb.org/core/charts-embedding-dashboards)_
6+
7+
The MongoDB Charts Embedding SDK allows you to embed a Chart or a Dashboard directly into your application. Just like embedded charts, you can embed a dashboard via unauthenticated or an authenticated method.
8+
9+
This example, demonstrates how you might render an authenticated dashboard embedded onto your application using Custom JWT as the authentication provider.
10+
11+
_Note that this is not an exhaustive list of all the features for the Dashboard Embedding SDK._
12+
13+
## Highlighted Features
14+
15+
In this example, you should be able to:
16+
17+
- Render an authenticated embedded dashboard
18+
- Render a dashboard to valid users only
19+
- Custom JWT authentication via the `jsonwebtoken` package
20+
21+
## Quick Start
22+
23+
_The following steps presume the use of npm, though yarn works as well._
24+
25+
1. Ensure you have Node installed. You can confirm with `node --version`. On some operating systems, Node is available as the `nodejs` binary instead.
26+
27+
2. Clone the Git repository or download the code to your computer.
28+
29+
3. Run `npm install` to install the package dependencies.
30+
31+
4. Run `npm start` to start the application. This will utilise parcel.js
32+
- Optional Parcel.js documentation https://parceljs.org/ for more information on what this is
33+
34+
This should create a local server running the Charts demo. Open a web browser and navigate to `http://localhost:1234` in the url bar to see the sample.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const express = require("express");
2+
const bodyParser = require("body-parser");
3+
const cors = require("cors");
4+
const jwt = require("jsonwebtoken");
5+
const config = require("./config.js");
6+
7+
const app = express();
8+
const port = 8000;
9+
10+
// Configuring body parser middleware
11+
app.use(bodyParser.urlencoded({ extended: false }));
12+
app.use(bodyParser.json());
13+
app.use(cors());
14+
15+
app.post("/login", (req, res) => {
16+
const loginDetails = req.body;
17+
// mock a check against the database
18+
let mockedUsername = "admin";
19+
let mockedPassword = "password";
20+
21+
if (
22+
loginDetails &&
23+
loginDetails.username === mockedUsername &&
24+
loginDetails.password === mockedPassword
25+
) {
26+
let token = jwt.sign({ username: loginDetails.username }, config.secret, {
27+
expiresIn: "12h" // expires in 12 hours
28+
});
29+
res.json({ bearerToken: token });
30+
} else {
31+
res.status(401).send(false);
32+
}
33+
});
34+
35+
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#dashboard {
2+
border-top: none;
3+
background-color: #f9fbfa;
4+
flex-grow: 1;
5+
height: 100%;
6+
box-shadow: grey 0px 0px 10px 1px;
7+
visibility: hidden;
8+
}
9+
10+
.dashboard-container {
11+
height: 80vh;
12+
width: 80vw;
13+
margin: auto;
14+
text-align: center;
15+
}
16+
17+
body.logged-in #login-page {
18+
visibility: hidden;
19+
height: 0;
20+
width: 0;
21+
}
22+
23+
body.logged-in #lock::before {
24+
content: "🔐";
25+
}
26+
27+
body #lock::before {
28+
content: "🔒";
29+
}
30+
31+
body.logged-in #dashboard {
32+
visibility: visible;
33+
}
34+
35+
.hideLogin {
36+
visibility: hidden;
37+
height: 0;
38+
width: 0;
39+
}
40+
41+
.main {
42+
box-sizing: border-box;
43+
padding: 1rem;
44+
height: 100%;
45+
display: flex;
46+
flex-direction: column;
47+
}
48+
49+
.login-page {
50+
width: 360px;
51+
margin: auto;
52+
}
53+
.form {
54+
position: relative;
55+
z-index: 1;
56+
background: #ffffff;
57+
max-width: 360px;
58+
margin: 0 auto 100px;
59+
padding: 45px;
60+
text-align: center;
61+
box-shadow: 0 0 20px 0 rgba(0, 0, 0, 0.2), 0 5px 5px 0 rgba(0, 0, 0, 0.24);
62+
}
63+
.form input {
64+
font-family: "Heebo", sans-serif;
65+
outline: 0;
66+
background: #f2f2f2;
67+
width: 100%;
68+
border: 0;
69+
margin: 0 0 15px;
70+
padding: 15px;
71+
box-sizing: border-box;
72+
font-size: 14px;
73+
}
74+
.form button {
75+
font-family: "Heebo", sans-serif;
76+
text-transform: uppercase;
77+
outline: 0;
78+
background: #13aa52;
79+
width: 100%;
80+
border: 0;
81+
padding: 15px;
82+
color: #ffffff;
83+
font-size: 14px;
84+
-webkit-transition: all 0.3 ease;
85+
transition: all 0.3 ease;
86+
cursor: pointer;
87+
}
88+
.form button:hover,
89+
.form button:active,
90+
.form button:focus {
91+
background: #109246;
92+
}
93+
.form .message {
94+
margin: 15px 0 0;
95+
color: #b3b3b3;
96+
font-size: 12px;
97+
}
98+
.form .message a {
99+
color: #13aa52;
100+
text-decoration: none;
101+
}
102+
103+
body {
104+
background: #e7eeec; /* fallback for old browsers */
105+
font-family: "Heebo", sans-serif;
106+
-webkit-font-smoothing: antialiased;
107+
-moz-osx-font-smoothing: grayscale;
108+
text-align: center;
109+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
secret: "topsecret"
3+
};

0 commit comments

Comments
 (0)