Using Handshakes' Data
This section covers the methods and specifications to:
- Initialise the widget in your site
- Use the widget with data obtained from Handshakes
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 widgetParametersEndpointData = {
graphType: GRAPH_TYPE,
datasets: [DATASET],
entities: [{ dataset: DATASET, entityId: ENTITY_ID }],
degree: DEGREE_OF_SEPERATION,
};
Replace GRAPH_TYPE
with one of the following: "radial" | "interconnect" | "lbo"
.
Replace DATASET
with one of the following: "CM" | "CRSG" | "CRMY"
.
Replace ENTITY_ID
with the ID of the entity you wish to see rendered by the widget.
Replace DEGREE_OF_SEPERATION
with an integer: eg. 1
.
Graph Type "interconnect" will require a degree of at least 2
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 |
---|---|---|
drawGraphWithEndpointData | Widget Parameters | Renders the graph visualisation with data from an API endpoint based on the widget parameters supplied |
updateGraphWithEndpointData | Widget Parameters , Widget Props (optional) | Similar to drawGraphWithEndpointData , it updates the graph visualisation with data from an API endpoint based on the updated widget parameters supplied. Can only be called after either drawGraphWithEndpointData or drawGraphWithCustomData |
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 {
const response = await fetch(WIDGET_SCRIPT_URL);
const scriptContent = await response.text();
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 API data
widget.drawGraphWithEndpointData(WidgetParametersEndpointData);
} catch (error) {
console.log(error);
}
}
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 { WidgetParametersEndpointData } from "./src/data/widget-parameters.js";
async function initWidget() {
try {
const response = await fetch(WIDGET_SCRIPT_URL);
const scriptContent = await response.text();
const script = document.createElement("script");
script.text = scriptContent;
document.body.appendChild(script);
const widget = new hs_widget(widgetProps);
widget.drawGraphWithEndpointData(WidgetParametersEndpointData);
} 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, widgetParametersEndpointData } 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) {
// use the drawGraphWithEndpointData method to generate graph using API data
widget.drawGraphWithEndpointData(widgetParametersEndpointData);
}
}, [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>
);
};