Skip to content

Instantly share code, notes, and snippets.

@lucguillemot
Last active May 10, 2019 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucguillemot/d45805e52b22f2c7ca60bbacb358b2ea to your computer and use it in GitHub Desktop.
Save lucguillemot/d45805e52b22f2c7ca60bbacb358b2ea to your computer and use it in GitHub Desktop.
import * as React from "react";
import * as vega from "vega";
// Note the added prop "handleClick"
export const Chart = ({ spec, handleClick }) => {
const chartContainer = React.useRef(null);
React.useEffect(
() => {
const createView = async () => {
try {
const view = new vega.View(vega.parse(spec), {
logLevel: vega.Warn,
renderer: "svg",
container: chartContainer.current,
hover: true
});
// Wait for Vega's scenegraph to finish rendering
const awaitedView = await view.runAsync();
// Attach the handler to the signal defined
// in Vega's chart spec.
awaitedView.addSignalListener("clickOnCategory", handleClick);
} catch (error) {
console.log(error);
}
};
createView();
},
// Notice the added dependency "handleClick"
[spec, handleClick]
);
return <div ref={chartContainer} />;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment