Skip to content

Instantly share code, notes, and snippets.

@GordonSmith
Last active August 8, 2018 08:47
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/913403c91d45b11473baa82d0ba790a4 to your computer and use it in GitHub Desktop.
Save GordonSmith/913403c91d45b11473baa82d0ba790a4 to your computer and use it in GitHub Desktop.
Tutorial 2: Pie Chart
license: Apache-2.0

Tutorial 2: A simple "2D" Chart.

Similar to the Hello World tutorial, but now we introduce some data:

1 - Import the required visualization:

import { Pie } from "@hpcc-js/chart";

2 - Instantiate and render the visualisation:

var widget = new Pie()              //  Create new instance of Pie
    .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
    ;

Think of the above pattern as a "spreadsheet" of data, with the "columns" being row 0 and the "data" being the rest of the spreadsheet.

Note 1: The reason for two distinct methods "columns" + "data", is to faciliate the initial render of a chart (with known columns), while the data payload could arrive at any later time (think dynamic dashboard). Note 2: To discover all the available "published properties" for the Pie Widget see: Dermatology-Pie and enable the "Properties" view.

body {
padding:0px;
margin:0px;
overflow:hidden;
}
#placeholder {
width:100%;
height:100vh;
}
<!DOCTYPE html>
<html>
<head>
<title>Pie 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>
<script>
// Load Example JavaScript (via GetLibs)---
System.import('./index.js');
</script>
</body>
</html>
import { Pie } from "@hpcc-js/chart";
var widget = new Pie() // Create new instance of Pie
.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
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment