Using Your Own Data
This section covers the methods and specifications to:
- Initialise the widget in your site
- Use the widget with your own data fed in
Create Widget Props and Parameters
- Widget Props
- Widget Parameters
const widgetProps = {
key: CLIENT_KEY,
parentElementSelector: ELEMENT_SELECTOR,
};
Replace CLIENT_KEY
with the unique alphanumeric string that will be provided to you after successfully onboarding with us.
Replace ELEMENT_SELECTOR
with the class or id of the element that will contain the widget.
(eg. ".hs-widget-class" or "#hs-widget-id")
const widgetParametersCustomData = {
graphType: GRAPH_TYPE,
};
Replace GRAPH_TYPE
with one of the following: "radial" | "interconnect" | "lbo"
.
Methods
Once initialised, there are methods exposed by the Widget that can be used to render the graph visualisation.
We'll be focusing on these two methods below.
Method | Parameters to supply | Description |
---|---|---|
drawGraphWithCustomData | JSON graph data in this structure, Widget Parameters | Renders the graph visualisation with the JSON data supplied |
updateGraphWithCustomData | JSON graph data in this structure, Widget Parameters , Widget Props (optional) | Similar to drawGraphWithCustomData , it updates the graph visualisation with the JSON data supplied. Can only be called after either drawGraphWithCustomData or drawGraphWithEndpointData |
Initialise the Widget
The following code snippets is used to initialise the widget.
- JavaScript
- React
// script to load the widget file
async function initWidget() {
try {
// Fetch both the widget script and JSON data
const [scriptResponse, dataResponse] = await Promise.all([
fetch(WIDGET_SCRIPT_URL),
fetch(YOUR_DATA_URL),
]);
const scriptContent = await scriptResponse.text();
const data = await dataResponse.json();
const script = document.createElement("script");
script.text = scriptContent;
document.body.appendChild(script);
// initialise a widget instance from the script
const widget = new hs_widget(widgetProps);
// use the drawGraphWithEndpointData method to generate graph using your own data
widget.drawGraphWithCustomData(data, widgetParametersCustomData);
} catch (error) {
console.log(error);
}
}
Use widget.drawGraphWithCustomData
instead of widget.drawGraphWithEndpointData
Widget Parameters are not needed when using your own data.
To use the Handshakes Graph widget on your web page, insert the above code snippets within a <script>
tag located just before the ending <body>
tag of the web page. For example:
// example of inserting the snippet onto your web page
<html>
<head></head>
<body>
<script type="module">
import { widgetProps } from "./src/data/widget-props.js";
import { widgetParametersCustomData } from "./src/data/widget-parameters.js";
async function initWidget() {
try {
// Fetch both the widget script and JSON data
const [scriptResponse, dataResponse] = await Promise.all([
fetch(WIDGET_SCRIPT_URL),
fetch(YOUR_DATA_URL),
]);
const scriptContent = await scriptResponse.text();
const data = await dataResponse.json();
const script = document.createElement("script");
script.text = scriptContent;
document.body.appendChild(script);
const widget = new hs_widget(widgetProps);
widget.drawGraphWithCustomData(data, widgetParametersCustomData);
} catch (error) {
console.log(error);
}
}
initWidget();
</script>
</body>
</html>
To use the Handshakes Graph Widget in your react app on component mount, you can follow the structure below:
import { useEffect, useState, useRef } from "react";
import { HandshakesWidget } from "./types";
import { widgetProps, widgetParametersCustomData } from "./data";
export const HSWidget = () => {
const [widget, setWidget] = useState<InstanceType<HandshakesWidget> | null>(
null
);
const containerRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const scriptSrc = WIDGET_SCRIPT_URL;
const script = document.createElement("script");
script.src = scriptSrc;
script.async = true;
script.onload = () => {
const WidgetConstructor = (window as any).hs_widget as HandshakesWidget;
// initialise a widget instance from the script
const newWidget = new WidgetConstructor(widgetProps);
setWidget(newWidget);
};
script.onerror = (error) => {
console.error("Error loading widget script:", error);
};
document.body.appendChild(script);
return () => {
document.body.removeChild(script);
};
}, []);
useEffect(() => {
if (widget && containerRef.current) {
const fetchData = async () => {
try {
const response = await fetch(YOUR_DATA_URL);
const apiData = await response.json();
// use the drawGraphWithEndpointData method to generate graph using your own data
widget.drawGraphWithCustomData(apiData, widgetParametersCustomData);
} catch (error) {
console.error("Error fetching data:", error);
}
};
fetchData();
}
}, [widget]);
return (
<div
style={{
width: "100vw",
height: "100vh",
boxSizing: "border-box",
position: "relative",
}}
>
<div
id="hs-graph"
ref={containerRef}
style={{
/* WARNING: due to the nature of our map technology,*/
/* do not use display: flex; on the parent element, only use display: block;*/
display: "block";
/* do not use min-height or min-width values */
width: "100%",
height: "100%",
}}
/>
</div>
);
};
Use widget.drawGraphWithCustomData
instead of widget.drawGraphWithEndpointData
Widget Parameters are not needed when using your own data.
Data Structure
To be able to have your data displayed in the Handshakes Graph Widget, it will have to be structured in a way that the widget can consume.
The JSON data fetched from YOUR_DATA_URL
should be in this format.