MCPcopy Create free account
hub / github.com/internetarchive/openlibrary / loadGraph

Function loadGraph

openlibrary/plugins/openlibrary/js/graphs/plot.js:249–275  ·  view source on GitHub ↗
(id, options = {}, tooltip_message = '', color = null)

Source from the content-addressed store, hash-verified

247 * Ignored if options and no tooltip_message is passed.
248 */
249export function loadGraph(id, options = {}, tooltip_message = '', color = null) {
250 let data;
251 const node = document.getElementById(id);
252 const graphSelector = `graph-json-${id}`;
253 const dataSource = document.getElementById(graphSelector);
254 if (!node) {
255 throw new Error(
256 `No graph associated with ${id} on the page.`
257 );
258 }
259 if (!dataSource) {
260 throw new Error(
261 `No data associated with ${id} - make sure a script tag with type text/json and id "${graphSelector}" is present on the page.`
262 );
263 } else {
264 try {
265 data = JSON.parse(dataSource.textContent);
266 } catch (e) {
267 throw new Error(`Unable to parse JSON in ${graphSelector}`);
268 }
269 if (tooltip_message) {
270 return plot_tooltip_graph($(node), data, tooltip_message, color);
271 } else {
272 return $.plot($(node), [{data: data}], options);
273 }
274 }
275}
276
277/**
278 * Render a graph inside an element which has the id attribute.

Callers 1

loadGraphIfExistsFunction · 0.85

Calls 2

plot_tooltip_graphFunction · 0.85
parseMethod · 0.80

Tested by

no test coverage detected