Skip to content

Instantly share code, notes, and snippets.

@GordonSmith
Last active August 8, 2018 09:17
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 GordonSmith/62169f8017a585fc142bec9d2c72b9dc to your computer and use it in GitHub Desktop.
Save GordonSmith/62169f8017a585fc142bec9d2c72b9dc to your computer and use it in GitHub Desktop.
Tutorial 4: Multi Chart
license: Apache-2.0

Tutorial 4: Multi Chart.

Building on the Pie Chart and Column Chart tutorials, lets step it up some more and let the user decide which chart to use.

1 - Import the entire chart package:

import * as hpccChart from "@hpcc-js/chart";

2 - Instantiate and render the visualisation base on a variable chartName:

new hpccChart[chartName]()          //  Create new instance of Widget
    .target("placeholder")          //  Nominate target on web page 
    .columns(["Subject", "Result"]) //  Set "Columns"
    .data([                         //  Set "Data"
        ["English", 45],
        ["Irish", 28],
        ["Math", 98],
        ["Geography", 48],
        ["Science", 82]
    ])
    .render()                       //  Render
    ;
body {
padding:0px;
margin:0px;
overflow:hidden;
}
#placeholder {
width:100%;
height:100vh;
}
.topcorner {
position: absolute;
top: 8px;
right: 8px;
}
<!DOCTYPE html>
<html>
<head>
<title>Multi Chart</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="./index.css">
<!-- GetLibs: An in-browser module loader for quick demos -->
<script src="https://unpkg.com/getlibs"></script>
</head>
<body>
<div id="placeholder">
<!-- Placeholder for Visualization -->
</div>
<select id="popType" class="topcorner" onchange="doRender(this.value);">
<option value="Area">Area</option>
<option value="Bar">Bar</option>
<option value="Bubble">Bubble</option>
<option value="Contour">Contour</option>
<option value="Column" selected="selected">Column</option>
<option value="HexBin">HexBin</option>
<option value="Line">Line</option>
<option value="Pie">Pie</option>
<option value="Scatter">Scatter</option>
<option value="Radar">Radar</option>
<option value="Step">Step</option>
<option value="WordCloud">Word Cloud</option>
</select>
<script>
// Load Example JavaScript (via GetLibs)---
System.import('./index.js');
</script>
</body>
</html>
import { select as d3Select } from "d3-selection";
import * as hpccChart from "@hpcc-js/chart";
function doRender(chartName) {
d3Select("#placeholder").html(""); // Clear existing DOM Node
new hpccChart[chartName]() // Create new instance of Widget
.target("placeholder") // Nominate target on web page
.columns(["Subject", "Result"]) // Set "Columns"
.data([ // Set "Data"
["English", 45],
["Irish", 28],
["Math", 98],
["Geography", 48],
["Science", 82]
])
.render() // Render
;
}
doRender("Column"); // Render Column by default
window.doRender = doRender; // Expose to global namespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment