(id, options = {}, tooltip_message = '', color = null)
| 247 | * Ignored if options and no tooltip_message is passed. |
| 248 | */ |
| 249 | export 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. |
no test coverage detected