/****************************************************
* Random Data Generation
*****************************************************/
// Create a Random Date
function randomDate(start, end) {
var d = new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
d.setHours(0,0,0,0);
return d;
}
var start = new Date(2013, 10, 1);
var end = new Date(2013, 11, 1);
var stringLength = 2;
// Generate Random Data [Date, VowelString, Random Number, Random Measure]
var data = [];
for(var i = 0; i < 200; i++) {
data[i] = [randomDate(start, end), Math.floor(Math.random() * 20), Math.floor(Math.random() * 30000)];
}
/****************************************************
* Charts
*****************************************************/
// Default chart sizes
var width = 400;
var height = 200;
var margins = {top: 15, right: 10, bottom: 20, left: 40};
// Crossfilter the data
var ndx = crossfilter(data);
// dimension on day of week
var dim1 = ndx.dimension(function(d) {
return d[0].getDay();
});
function average_map(m) {
var sum = 0;
m.forEach(function(k, v) {
sum += v;
});
return m.size() ? sum / m.size() : 0;
}
// group on day of week
var grp1 = dim1.group().reduce(
function(p, v) { // add
var day = d3.time.day(v[0]).getTime();
p.map.set(day, p.map.has(day) ? p.map.get(day) + 1 : 1);
p.avg = average_map(p.map);
return p;
},
function(p, v) { // remove
var day = d3.time.day(v[0]).getTime();
p.map.set(day, p.map.has(day) ? p.map.get(day) - 1 : 0);
p.avg = average_map(p.map);
return p;
},
function() { // init
return {map: d3.map(), avg: 0};
}
);
var chart1 = dc.barChart("#chart1")
.width(width)
.height(height)
.margins(margins)
.dimension(dim1)
.group(grp1)
.valueAccessor(function(kv) { return kv.value.avg; })
.gap(1)
.round(dc.round.floor)
.renderHorizontalGridLines(true)
.x(d3.scale.linear().domain([0,...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.