2013-07-16 14:03:40 +00:00
|
|
|
/*\
|
|
|
|
title: $:/plugins/tiddlywiki/d3/barwidget.js
|
|
|
|
type: application/javascript
|
2013-11-08 08:47:00 +00:00
|
|
|
module-type: widget
|
2013-07-16 14:03:40 +00:00
|
|
|
|
|
|
|
A widget for displaying stacked or grouped bar charts. Derived from http://bl.ocks.org/mbostock/3943967
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
2013-11-08 08:47:00 +00:00
|
|
|
var Widget = require("$:/core/modules/widgets/widget.js").widget,
|
2013-10-24 19:45:53 +00:00
|
|
|
d3 = require("$:/plugins/tiddlywiki/d3/d3.js").d3;
|
2013-07-16 14:03:40 +00:00
|
|
|
|
2013-10-24 19:45:53 +00:00
|
|
|
var BarWidget = function(parseTreeNode,options) {
|
|
|
|
this.initialise(parseTreeNode,options);
|
2013-07-16 14:03:40 +00:00
|
|
|
};
|
|
|
|
|
2013-10-24 19:45:53 +00:00
|
|
|
/*
|
|
|
|
Inherit from the base widget class
|
|
|
|
*/
|
|
|
|
BarWidget.prototype = new Widget();
|
|
|
|
|
|
|
|
/*
|
|
|
|
Render this widget into the DOM
|
|
|
|
*/
|
|
|
|
BarWidget.prototype.render = function(parent,nextSibling) {
|
|
|
|
// Save the parent dom node
|
|
|
|
this.parentDomNode = parent;
|
|
|
|
// Compute our attributes
|
|
|
|
this.computeAttributes();
|
|
|
|
// Execute our logic
|
|
|
|
this.execute();
|
|
|
|
// Create the chart
|
|
|
|
var chart = this.createChart(parent,nextSibling);
|
|
|
|
this.updateChart = chart.updateChart;
|
|
|
|
if(this.updateChart) {
|
|
|
|
this.updateChart();
|
2013-07-16 14:03:40 +00:00
|
|
|
}
|
2013-10-24 19:45:53 +00:00
|
|
|
// Insert the chart into the DOM and render any children
|
|
|
|
parent.insertBefore(chart.domNode,nextSibling);
|
|
|
|
this.domNodes.push(chart.domNode);
|
2013-07-16 14:03:40 +00:00
|
|
|
};
|
|
|
|
|
2013-10-24 19:45:53 +00:00
|
|
|
BarWidget.prototype.createChart = function(parent,nextSibling) {
|
2013-07-16 14:03:40 +00:00
|
|
|
// Get the data we're plotting
|
2013-10-24 19:45:53 +00:00
|
|
|
var data = this.wiki.getTiddlerData(this.barData),
|
|
|
|
n,m,stack,layers;
|
2013-07-16 14:03:40 +00:00
|
|
|
if(data) {
|
|
|
|
n = data.layers;
|
|
|
|
m = data.samples;
|
|
|
|
layers = data.data;
|
2013-10-24 19:45:53 +00:00
|
|
|
} else { // Use randomly generated data if we don't have any
|
2013-07-16 14:03:40 +00:00
|
|
|
n = 4; // number of layers
|
|
|
|
m = 58; // number of samples per layer
|
|
|
|
stack = d3.layout.stack();
|
2014-08-30 20:32:55 +00:00
|
|
|
layers = stack(d3.range(n).map(function() { return bumpLayer(m, 0.1); }));
|
2013-07-16 14:03:40 +00:00
|
|
|
}
|
2013-10-24 19:45:53 +00:00
|
|
|
// Calculate the maximum data values
|
2013-07-16 14:03:40 +00:00
|
|
|
var yGroupMax = d3.max(layers, function(layer) { return d3.max(layer, function(d) { return d.y; }); }),
|
|
|
|
yStackMax = d3.max(layers, function(layer) { return d3.max(layer, function(d) { return d.y0 + d.y; }); });
|
2013-10-24 19:45:53 +00:00
|
|
|
// Calculate margins and width and height
|
2013-07-16 14:03:40 +00:00
|
|
|
var margin = {top: 40, right: 10, bottom: 20, left: 10},
|
|
|
|
width = 960 - margin.left - margin.right,
|
|
|
|
height = 500 - margin.top - margin.bottom;
|
2013-10-24 19:45:53 +00:00
|
|
|
// x-scale
|
2013-07-16 14:03:40 +00:00
|
|
|
var x = d3.scale.ordinal()
|
|
|
|
.domain(d3.range(m))
|
2014-08-30 20:32:55 +00:00
|
|
|
.rangeRoundBands([0, width], 0.08);
|
2013-10-24 19:45:53 +00:00
|
|
|
// y-scale
|
2013-07-16 14:03:40 +00:00
|
|
|
var y = d3.scale.linear()
|
|
|
|
.domain([0, yStackMax])
|
|
|
|
.range([height, 0]);
|
2013-10-24 19:45:53 +00:00
|
|
|
// Array of colour values
|
2013-07-16 14:03:40 +00:00
|
|
|
var color = d3.scale.linear()
|
|
|
|
.domain([0, n - 1])
|
|
|
|
.range(["#aad", "#556"]);
|
2013-10-24 19:45:53 +00:00
|
|
|
// x-axis
|
2013-07-16 14:03:40 +00:00
|
|
|
var xAxis = d3.svg.axis()
|
|
|
|
.scale(x)
|
|
|
|
.tickSize(0)
|
|
|
|
.tickPadding(6)
|
|
|
|
.orient("bottom");
|
2013-10-24 19:45:53 +00:00
|
|
|
// Create SVG element
|
2014-03-11 11:12:56 +00:00
|
|
|
var svgElement = d3.select(parent).insert("svg",function() {return nextSibling;})
|
2013-07-16 14:03:40 +00:00
|
|
|
.attr("viewBox", "0 0 960 500")
|
|
|
|
.attr("preserveAspectRatio", "xMinYMin meet")
|
|
|
|
.attr("width", width + margin.left + margin.right)
|
2013-10-24 19:45:53 +00:00
|
|
|
.attr("height", height + margin.top + margin.bottom);
|
|
|
|
// Create main group
|
|
|
|
var mainGroup = svgElement.append("g")
|
2013-07-16 14:03:40 +00:00
|
|
|
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
|
2013-10-24 19:45:53 +00:00
|
|
|
// Create the layers
|
|
|
|
var layer = mainGroup.selectAll(".layer")
|
2013-07-16 14:03:40 +00:00
|
|
|
.data(layers)
|
2014-08-30 20:32:55 +00:00
|
|
|
.enter().append("g")
|
2013-07-16 14:03:40 +00:00
|
|
|
.attr("class", "layer")
|
|
|
|
.style("fill", function(d, i) { return color(i); });
|
2013-10-24 19:45:53 +00:00
|
|
|
// Create the rectangles in each layer
|
2013-07-16 14:03:40 +00:00
|
|
|
var rect = layer.selectAll("rect")
|
|
|
|
.data(function(d) { return d; })
|
2014-08-30 20:32:55 +00:00
|
|
|
.enter().append("rect")
|
2013-07-16 14:03:40 +00:00
|
|
|
.attr("x", function(d) { return x(d.x); })
|
|
|
|
.attr("y", height)
|
|
|
|
.attr("width", x.rangeBand())
|
|
|
|
.attr("height", 0);
|
2013-10-24 19:45:53 +00:00
|
|
|
// Transition the rectangles to their final height
|
2013-07-16 14:03:40 +00:00
|
|
|
rect.transition()
|
|
|
|
.delay(function(d, i) { return i * 10; })
|
|
|
|
.attr("y", function(d) { return y(d.y0 + d.y); })
|
|
|
|
.attr("height", function(d) { return y(d.y0) - y(d.y0 + d.y); });
|
2013-10-24 19:45:53 +00:00
|
|
|
// Add to the DOM
|
|
|
|
mainGroup.append("g")
|
2013-07-16 14:03:40 +00:00
|
|
|
.attr("class", "x axis")
|
|
|
|
.attr("transform", "translate(0," + height + ")")
|
|
|
|
.call(xAxis);
|
2013-10-24 19:45:53 +00:00
|
|
|
var self = this;
|
|
|
|
// Return the svg node
|
|
|
|
return {
|
|
|
|
domNode: svgElement[0][0],
|
|
|
|
updateChart: function() {
|
2024-08-20 15:33:07 +00:00
|
|
|
if(self.barGrouped !== "no") {
|
2013-10-24 19:45:53 +00:00
|
|
|
transitionGrouped();
|
|
|
|
} else {
|
|
|
|
transitionStacked();
|
|
|
|
}
|
|
|
|
}
|
2013-07-16 14:03:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function transitionGrouped() {
|
2014-08-30 20:32:55 +00:00
|
|
|
y.domain([0, yGroupMax]);
|
|
|
|
rect.transition()
|
|
|
|
.duration(500)
|
|
|
|
.delay(function(d, i) { return i * 10; })
|
|
|
|
.attr("x", function(d, i, j) { return x(d.x) + x.rangeBand() / n * j; })
|
|
|
|
.attr("width", x.rangeBand() / n)
|
|
|
|
.transition()
|
|
|
|
.attr("y", function(d) { return y(d.y); })
|
|
|
|
.attr("height", function(d) { return height - y(d.y); });
|
2013-07-16 14:03:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function transitionStacked() {
|
2014-08-30 20:32:55 +00:00
|
|
|
y.domain([0, yStackMax]);
|
|
|
|
rect.transition()
|
|
|
|
.duration(500)
|
|
|
|
.delay(function(d, i) { return i * 10; })
|
|
|
|
.attr("y", function(d) { return y(d.y0 + d.y); })
|
|
|
|
.attr("height", function(d) { return y(d.y0) - y(d.y0 + d.y); })
|
|
|
|
.transition()
|
|
|
|
.attr("x", function(d) { return x(d.x); })
|
|
|
|
.attr("width", x.rangeBand());
|
2013-07-16 14:03:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Inspired by Lee Byron's test data generator.
|
|
|
|
function bumpLayer(n, o) {
|
2014-08-30 20:32:55 +00:00
|
|
|
function bump(a) {
|
|
|
|
var x = 1 / (0.1 + Math.random()),
|
|
|
|
y = 2 * Math.random() - 0.5,
|
|
|
|
z = 10 / (0.1 + Math.random());
|
2024-08-20 15:33:07 +00:00
|
|
|
for(var i = 0; i < n; i++) {
|
2014-08-30 20:32:55 +00:00
|
|
|
var w = (i / n - y) * z;
|
|
|
|
a[i] += x * Math.exp(-w * w);
|
|
|
|
}
|
2013-07-16 14:03:40 +00:00
|
|
|
}
|
2014-08-30 20:32:55 +00:00
|
|
|
var a = [], i;
|
2024-08-20 15:33:07 +00:00
|
|
|
for(i = 0; i < n; ++i) a[i] = o + o * Math.random();
|
|
|
|
for(i = 0; i < 5; ++i) bump(a);
|
2014-08-30 20:32:55 +00:00
|
|
|
return a.map(function(d, i) { return {x: i, y: Math.max(0, d)}; });
|
2013-07-16 14:03:40 +00:00
|
|
|
}
|
2013-10-24 19:45:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Compute the internal state of the widget
|
|
|
|
*/
|
|
|
|
BarWidget.prototype.execute = function() {
|
|
|
|
// Get the parameters from the attributes
|
|
|
|
this.barData = this.getAttribute("data");
|
|
|
|
this.barGrouped = this.getAttribute("grouped","no");
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
|
|
|
|
*/
|
|
|
|
BarWidget.prototype.refresh = function(changedTiddlers) {
|
|
|
|
var changedAttributes = this.computeAttributes();
|
|
|
|
if(changedAttributes.data || changedTiddlers[this.barData]) {
|
|
|
|
this.refreshSelf();
|
|
|
|
return true;
|
|
|
|
} else if(changedAttributes.grouped) {
|
|
|
|
this.execute();
|
|
|
|
if(this.updateChart) {
|
|
|
|
this.updateChart();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2013-07-16 14:03:40 +00:00
|
|
|
|
|
|
|
exports.d3bar = BarWidget;
|
|
|
|
|
|
|
|
})();
|