Hi - My ultimate goal is creating a dashboard of D3 charts using data from Google Sheets. I've got as far as pulling data from Sheets using JSONP
.
Grateful for any assistance.
<script>
/**
* Maps the spreadsheet cells into a dataframe, consisting of an array of rows (i.e. a 2d array)
* In many cases we have empty rows or incomplete rows, so you can skip those by including
* the realrowlength parameter - it will skip any rows that don't have this length.
* Alternatively, you can just choose to skip the first 'n' rows by setting the skip parameter.
*/
function mapEntries(json, realrowlength, skip){
if (!skip) skip = 0;
var dataframe = new Array();
var row = new Array();
for (var i=0; i < json.feed.entry.length; i++) {
var entry = json.feed.entry[i];
if (row != null) {
if ((!realrowlength || row.length === realrowlength) && (skip === 0)){
dataframe.push(row);
} else {
if (skip > 0) skip--;
}
}
var row = new Array();
}
row.push(entry.content.$t);
}
dataframe.push(row);
return dataframe;
}
//
// Show a nice bubble chart!
//
function drawBubbleChart(root){
//
// How big the chart is
//
var diameter = 960;
//
// Pick some colours for the categories (groups)
//
var color = d3.scale.category10();
//
// Create a bubble layout based on the tree of objects.
// This adds properties x,y,r to each of our leaf objects
// indicating where to draw them (x,y), and how big to draw them (r).
// This is worked out using the "value" property of each leaf.
//
var bubble = d3.layout.pack().sort(null).size([960,960]).padding(1.5);
//
// Make a SVG graphic
//
var svg = d3.select("body")
.append("svg")
.attr("width",960)
.attr("height", 960)
.attr("class","bubble");
//
// For each leaf, create a new "node" and place it in the correct
// location using the transform attribute.
//
var node = svg.selectAll(".node")
.data(bubble.nodes(root)
.filter(function(d){ return !d.children;}))
.enter()
.append("g")
.attr("class","node")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
//
// For each node, make a circle of the correct radius (r) and
// colour it in according to the group it belongs to
//
node.append("circle")
.attr("r", function(d) { return d.r; })
.style("fill", function(d) { return color(d.group) });
//
// For each node, add a label to the middle of the circle
//
node.append("text")
.attr("dy", ".3em")
.style("text-anchor", "middle")
.text(function(d) {
});
}
//
// Render the data
//
function render(data){
//
// Convert the JSON into a nice array
//
var dataframe = mapEntries(data,null,2);
//
// Build a "tree" from each of the data rows
//
// Each leaf in the tree has a name, value and group
//
var root = {};
root.children = new Array();
for (i=0;i<dataframe.length;i++){
var item = {};
item.value = Number(dataframe[i][1]);
item.group = dataframe[i][2];
root.children.push(item);
}
//
// Draw a bubble chart of the tree
//
drawBubbleChart(root);
}
</script>