Last time out we created the Hello World extension for SAP Lumira. If you have not created an SAP Lumira extension before I'd suggest reading that post first.
This time I will walk you through a more real world example showing how to bring in a D3 chart as extension for SAP Lumira, I will be using the D3 Bullet Chart as an example but the steps should be similar for other charts.
Updated for SAP Lumira 1.20 as there were improvements made for creating SAP Lumira extensions since this was first written.
You will need to be comfortable with JavaScript and D3 to follow this guide as this is a more advanced example.
You need to install:
To save time I have already made these changes to a reusable local copy of the D3 Bullet Chart code (look for the // MDL comments), see the resources section (below), you can find the modified files on GitHub.
The Bullet Chart uses a simple JSON data structure, think of it being a JSON version of a CSV file.
[ {"title":"Revenue","subtitle":"US$, in thousands","ranges":[150,225,300],"measures":[220,270],"markers":[250]}, {"title":"Profit","subtitle":"%","ranges":[20,25,30],"measures":[21,23],"markers":[26]}, {"title":"Order Size","subtitle":"US$, average","ranges":[350,500,600],"measures":[100,320],"markers":[550]}, {"title":"New Customers","subtitle":"count","ranges":[1400,2000,2500],"measures":[1000,1650],"markers":[2100]}, {"title":"Satisfaction","subtitle":"out of 5","ranges":[3.5,4.25,5],"measures":[3.2,4.7],"markers":[4.4]} ] |
Originally vizPacker used this JSON format which suited the D3 Bullet Chart but not many other D3 charts. The majority of D3 charts use D3.csv data format, so vizPacker now uses the D3.csv data format as well to make things easier.
We will see later on in this blog how to convert/map from the D3.csv format to the JSON format for use with the Bullet Chart.
At a high level here are the changes I made:
<svg id="vis"></svg> <script src="http://d3js.org/d3.v3.min.js"></script> |
<script> var vis = d3.select("#vis"); |
var vis = d3.select("#vis"); // MDL: embed JSON so we can test with Google Chrome locally. // MDL: renamed "data" to "fdata". // MDL: Combined "title" and "subtitle" as "Titles" array. // MDL: Renamed ranges to "Ranges". // MDL: Renamed measures to "Actuals". // MDL: Renamed markers to "Target". var fdata = [ {"Titles":["Revenue","US$, in thousands"],"Ranges":[150,225,300],"Actuals":[220,270],"Target":[250]}, {"Titles":["Profit","%"],"Ranges":[20,25,30],"Actuals":[21,23],"Target":[26]}, {"Titles":["Order Size","US$, average"],"Ranges":[350,500,600],"Actuals":[100,320],"Target":[550]}, {"Titles":["New Customers","count"],"Ranges":[1400,2000,2500],"Actuals":[1000,1650],"Target":[2100]}, {"Titles":["Satisfaction","out of 5"],"Ranges":[3.5,4.25,5],"Actuals":[3.2,4.7],"Target":[4.4]} ]; // MDL: end |
Follow these steps to create and test your D3 Bullet Chart extension inside the vizPacker:
var render = function(data, container, width, height, colorPalette, properties, dispatch) { //prepare canvas with width and height of container container.selectAll('svg').remove(); var vis = container.append('svg').attr('width', width).attr('height', height) .append('g').attr('class', 'vis').attr('width', width).attr('height', height); }; |
var fdata = []; var meta = data.meta; var titleFields = meta.dimensions('Titles'); var actualFields = meta.measures('Actuals'); var targetFields = meta.measures('Target'); var rangeFields = meta.measures('Ranges'); |
.bullet { font: 10px sans-serif; } .bullet .marker { stroke: #000; stroke-width: 2px; } .bullet .tick line { stroke: #666; stroke-width: .5px; } .bullet .range.s0 { fill: #eee; } .bullet .range.s1 { fill: #ddd; } .bullet .range.s2 { fill: #ccc; } .bullet .measure.s0 { fill: lightsteelblue; } .bullet .measure.s1 { fill: steelblue; } .bullet .title { font-size: 14px; font-weight: bold; } .bullet .subtitle { fill: #999; } |
Titles | Title, Subtitle |
Actuals | Actual, Pace |
Target | Target |
Ranges | Range 1, Range 2, Range 3 |
Have a look at the:
You can find all of the Bullet Chart files on GitHub.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
13 | |
13 | |
10 | |
9 | |
9 | |
8 | |
7 | |
7 | |
7 | |
6 |