first commit
This commit is contained in:
@@ -0,0 +1,333 @@
|
||||
/* ------------------------------------------------------------------------------
|
||||
*
|
||||
* # D3.js - hierarchical bar chart
|
||||
*
|
||||
* Demo d3.js hierarchical bar chart setup with .json data
|
||||
*
|
||||
* Version: 1.0
|
||||
* Latest update: August 1, 2015
|
||||
*
|
||||
* ---------------------------------------------------------------------------- */
|
||||
|
||||
$(function () {
|
||||
|
||||
// Initialize chart
|
||||
stackedMultiples('#d3-hierarchical-bars', 400);
|
||||
|
||||
// Chart setup
|
||||
function stackedMultiples(element, height) {
|
||||
|
||||
|
||||
// Basic setup
|
||||
// ------------------------------
|
||||
|
||||
// Define main variables
|
||||
var d3Container = d3.select(element),
|
||||
margin = {top: 25, right: 40, bottom: 20, left: 130},
|
||||
width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right,
|
||||
height = height - margin.top - margin.bottom - 5,
|
||||
barHeight = 30,
|
||||
duration = 750,
|
||||
delay = 25;
|
||||
|
||||
|
||||
|
||||
// Construct scales
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
var x = d3.scale.linear()
|
||||
.range([0, width]);
|
||||
|
||||
// Colors
|
||||
var color = d3.scale.ordinal()
|
||||
.range(["#26A69A", "#ccc"]);
|
||||
|
||||
|
||||
|
||||
// Create axes
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
var xAxis = d3.svg.axis()
|
||||
.scale(x)
|
||||
.orient("top");
|
||||
|
||||
|
||||
|
||||
// Create chart
|
||||
// ------------------------------
|
||||
|
||||
// Add SVG element
|
||||
var container = d3Container.append("svg");
|
||||
|
||||
// Add SVG group
|
||||
var svg = container
|
||||
.attr("width", width + margin.left + margin.right)
|
||||
.attr("height", height + margin.top + margin.bottom)
|
||||
.append("g")
|
||||
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
|
||||
|
||||
|
||||
// Construct chart layout
|
||||
// ------------------------------
|
||||
|
||||
// Partition
|
||||
var partition = d3.layout.partition()
|
||||
.value(function(d) { return d.size; });
|
||||
|
||||
|
||||
|
||||
// Load data
|
||||
// ------------------------------
|
||||
|
||||
d3.json("assets/demo_data/d3/bars/bars_hierarchical.json", function(error, root) {
|
||||
partition.nodes(root);
|
||||
x.domain([0, root.value]).nice();
|
||||
down(root, 0);
|
||||
});
|
||||
|
||||
|
||||
//
|
||||
// Append chart elements
|
||||
//
|
||||
|
||||
// Add background bars
|
||||
svg.append("rect")
|
||||
.attr("class", "d3-bars-background")
|
||||
.attr("width", width)
|
||||
.attr("height", height)
|
||||
.style("fill", "#fff")
|
||||
.on("click", up);
|
||||
|
||||
|
||||
// Append axes
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
svg.append("g")
|
||||
.attr("class", "d3-axis d3-axis-horizontal d3-axis-strong");
|
||||
|
||||
|
||||
// Append bars
|
||||
// ------------------------------
|
||||
|
||||
// Create hierarchical structure
|
||||
function down(d, i) {
|
||||
if (!d.children || this.__transition__) return;
|
||||
var end = duration + d.children.length * delay;
|
||||
|
||||
// Mark any currently-displayed bars as exiting.
|
||||
var exit = svg.selectAll(".enter")
|
||||
.attr("class", "exit");
|
||||
|
||||
// Entering nodes immediately obscure the clicked-on bar, so hide it.
|
||||
exit.selectAll("rect").filter(function(p) { return p === d; })
|
||||
.style("fill-opacity", 1e-6);
|
||||
|
||||
// Enter the new bars for the clicked-on data.
|
||||
// Per above, entering bars are immediately visible.
|
||||
var enter = bar(d)
|
||||
.attr("transform", stack(i))
|
||||
.style("opacity", 1);
|
||||
|
||||
// Have the text fade-in, even though the bars are visible.
|
||||
// Color the bars as parents; they will fade to children if appropriate.
|
||||
enter.select("text").style("fill-opacity", 1e-6);
|
||||
enter.select("rect").style("fill", color(true));
|
||||
|
||||
// Update the x-scale domain.
|
||||
x.domain([0, d3.max(d.children, function(d) { return d.value; })]).nice();
|
||||
|
||||
// Update the x-axis.
|
||||
svg.selectAll(".d3-axis-horizontal").transition()
|
||||
.duration(duration)
|
||||
.call(xAxis);
|
||||
|
||||
// Transition entering bars to their new position.
|
||||
var enterTransition = enter.transition()
|
||||
.duration(duration)
|
||||
.delay(function(d, i) { return i * delay; })
|
||||
.attr("transform", function(d, i) { return "translate(0," + barHeight * i * 1.2 + ")"; });
|
||||
|
||||
// Transition entering text.
|
||||
enterTransition.select("text")
|
||||
.style("fill-opacity", 1);
|
||||
|
||||
// Transition entering rects to the new x-scale.
|
||||
enterTransition.select("rect")
|
||||
.attr("width", function(d) { return x(d.value); })
|
||||
.style("fill", function(d) { return color(!!d.children); });
|
||||
|
||||
// Transition exiting bars to fade out.
|
||||
var exitTransition = exit.transition()
|
||||
.duration(duration)
|
||||
.style("opacity", 1e-6)
|
||||
.remove();
|
||||
|
||||
// Transition exiting bars to the new x-scale.
|
||||
exitTransition.selectAll("rect")
|
||||
.attr("width", function(d) { return x(d.value); });
|
||||
|
||||
// Rebind the current node to the background.
|
||||
svg.select(".d3-bars-background")
|
||||
.datum(d)
|
||||
.transition()
|
||||
.duration(end);
|
||||
|
||||
d.index = i;
|
||||
}
|
||||
|
||||
// Return to parent level
|
||||
function up(d) {
|
||||
if (!d.parent || this.__transition__) return;
|
||||
var end = duration + d.children.length * delay;
|
||||
|
||||
// Mark any currently-displayed bars as exiting.
|
||||
var exit = svg.selectAll(".enter")
|
||||
.attr("class", "exit");
|
||||
|
||||
// Enter the new bars for the clicked-on data's parent.
|
||||
var enter = bar(d.parent)
|
||||
.attr("transform", function(d, i) { return "translate(0," + barHeight * i * 1.2 + ")"; })
|
||||
.style("opacity", 1e-6);
|
||||
|
||||
// Color the bars as appropriate.
|
||||
// Exiting nodes will obscure the parent bar, so hide it.
|
||||
enter.select("rect")
|
||||
.style("fill", function(d) { return color(!!d.children); })
|
||||
.filter(function(p) { return p === d; })
|
||||
.style("fill-opacity", 1e-6);
|
||||
|
||||
// Update the x-scale domain.
|
||||
x.domain([0, d3.max(d.parent.children, function(d) { return d.value; })]).nice();
|
||||
|
||||
// Update the x-axis.
|
||||
svg.selectAll(".d3-axis-horizontal").transition()
|
||||
.duration(duration)
|
||||
.call(xAxis);
|
||||
|
||||
// Transition entering bars to fade in over the full duration.
|
||||
var enterTransition = enter.transition()
|
||||
.duration(end)
|
||||
.style("opacity", 1);
|
||||
|
||||
// Transition entering rects to the new x-scale.
|
||||
// When the entering parent rect is done, make it visible!
|
||||
enterTransition.select("rect")
|
||||
.attr("width", function(d) { return x(d.value); })
|
||||
.each("end", function(p) { if (p === d) d3.select(this).style("fill-opacity", null); });
|
||||
|
||||
// Transition exiting bars to the parent's position.
|
||||
var exitTransition = exit.selectAll("g").transition()
|
||||
.duration(duration)
|
||||
.delay(function(d, i) { return i * delay; })
|
||||
.attr("transform", stack(d.index));
|
||||
|
||||
// Transition exiting text to fade out.
|
||||
exitTransition.select("text")
|
||||
.style("fill-opacity", 1e-6);
|
||||
|
||||
// Transition exiting rects to the new scale and fade to parent color.
|
||||
exitTransition.select("rect")
|
||||
.attr("width", function(d) { return x(d.value); })
|
||||
.style("fill", color(true));
|
||||
|
||||
// Remove exiting nodes when the last child has finished transitioning.
|
||||
exit.transition()
|
||||
.duration(end)
|
||||
.remove();
|
||||
|
||||
// Rebind the current parent to the background.
|
||||
svg.select(".d3-bars-background")
|
||||
.datum(d.parent)
|
||||
.transition()
|
||||
.duration(end);
|
||||
}
|
||||
|
||||
// Creates a set of bars for the given data node, at the specified index.
|
||||
function bar(d) {
|
||||
var bar = svg.insert("g", ".d3-axis-vertical")
|
||||
.attr("class", "enter")
|
||||
.attr("transform", "translate(0,5)")
|
||||
.selectAll("g")
|
||||
.data(d.children)
|
||||
.enter()
|
||||
.append("g")
|
||||
.style("cursor", function(d) { return !d.children ? null : "pointer"; })
|
||||
.on("click", down);
|
||||
|
||||
bar.append("text")
|
||||
.attr("x", -6)
|
||||
.attr("y", barHeight / 2)
|
||||
.attr("dy", ".35em")
|
||||
.style("text-anchor", "end")
|
||||
.text(function(d) { return d.name; });
|
||||
|
||||
bar.append("rect")
|
||||
.attr("width", function(d) { return x(d.value); })
|
||||
.attr("height", barHeight);
|
||||
|
||||
return bar;
|
||||
}
|
||||
|
||||
// A stateful closure for stacking bars horizontally.
|
||||
function stack(i) {
|
||||
var x0 = 0;
|
||||
return function(d) {
|
||||
var tx = "translate(" + x0 + "," + barHeight * i * 1.2 + ")";
|
||||
x0 += x(d.value);
|
||||
return tx;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Resize chart
|
||||
// ------------------------------
|
||||
|
||||
// Call function on window resize
|
||||
$(window).on('resize', resize);
|
||||
|
||||
// Call function on sidebar width change
|
||||
$('.sidebar-control').on('click', resize);
|
||||
|
||||
// Resize function
|
||||
//
|
||||
// Since D3 doesn't support SVG resize by default,
|
||||
// we need to manually specify parts of the graph that need to
|
||||
// be updated on window resize
|
||||
function resize() {
|
||||
|
||||
// Layout variables
|
||||
width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right;
|
||||
|
||||
|
||||
// Layout
|
||||
// -------------------------
|
||||
|
||||
// Main svg width
|
||||
container.attr("width", width + margin.left + margin.right);
|
||||
|
||||
// Width of appended group
|
||||
svg.attr("width", width + margin.left + margin.right);
|
||||
|
||||
|
||||
// Axes
|
||||
// -------------------------
|
||||
|
||||
// Horizontal range
|
||||
x.range([0, width]);
|
||||
|
||||
// Horizontal axis
|
||||
svg.selectAll('.d3-axis-horizontal').call(xAxis);
|
||||
|
||||
|
||||
// Chart elements
|
||||
// -------------------------
|
||||
|
||||
// Bars
|
||||
svg.selectAll('.enter rect').attr("width", function(d) { return x(d.value); });
|
||||
}
|
||||
}
|
||||
});
|
||||
+198
@@ -0,0 +1,198 @@
|
||||
/* ------------------------------------------------------------------------------
|
||||
*
|
||||
* # D3.js - histogram chart
|
||||
*
|
||||
* Demo d3.js histogram chart setup with tooltip and random data
|
||||
*
|
||||
* Version: 1.0
|
||||
* Latest update: August 1, 2015
|
||||
*
|
||||
* ---------------------------------------------------------------------------- */
|
||||
|
||||
$(function () {
|
||||
|
||||
// Initialize chart
|
||||
stackedMultiples('#d3-histogram', 400);
|
||||
|
||||
// Chart setup
|
||||
function stackedMultiples(element, height) {
|
||||
|
||||
|
||||
// Basic setup
|
||||
// ------------------------------
|
||||
|
||||
// Define main variables
|
||||
var d3Container = d3.select(element),
|
||||
margin = {top: 15, right: 20, bottom: 20, left: 60},
|
||||
width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right,
|
||||
height = height - margin.top - margin.bottom - 5;
|
||||
|
||||
// Generate a Bates distribution of 10 random variables.
|
||||
var values = d3.range(1000).map(d3.random.bates(4));
|
||||
|
||||
// Data format
|
||||
var formatCount = d3.format(",.0f");
|
||||
|
||||
|
||||
|
||||
// Construct scales
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
var x = d3.scale.linear()
|
||||
.domain([0, 1])
|
||||
.range([0, width]);
|
||||
|
||||
// Generate a histogram using twenty uniformly-spaced bins.
|
||||
var data = d3.layout.histogram()
|
||||
.bins(x.ticks(20))
|
||||
(values);
|
||||
|
||||
// Vertical
|
||||
var y = d3.scale.linear()
|
||||
.domain([0, d3.max(data, function(d) { return d.y; })])
|
||||
.range([height, 0]);
|
||||
|
||||
// Colors
|
||||
var color = d3.scale.ordinal().range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);
|
||||
|
||||
|
||||
|
||||
// Create axes
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
var xAxis = d3.svg.axis()
|
||||
.scale(x)
|
||||
.orient("bottom");
|
||||
|
||||
|
||||
|
||||
// Create chart
|
||||
// ------------------------------
|
||||
|
||||
// Add SVG element
|
||||
var container = d3Container.append("svg");
|
||||
|
||||
// Add SVG group
|
||||
var svg = container
|
||||
.attr("width", width + margin.left + margin.right)
|
||||
.attr("height", height + margin.top + margin.bottom)
|
||||
.append("g")
|
||||
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
|
||||
|
||||
|
||||
|
||||
// Add tooltip
|
||||
// ------------------------------
|
||||
|
||||
// Create tooltip
|
||||
var tip = d3.tip()
|
||||
.attr('class', 'd3-tip')
|
||||
.offset([-25, 0])
|
||||
.html(function(d) {
|
||||
return "Current value: " + "<span class='text-semibold'>" + formatCount(d.y) + "</span>";
|
||||
})
|
||||
|
||||
// Initialize tooltip
|
||||
svg.call(tip);
|
||||
|
||||
|
||||
//
|
||||
// Append chart elements
|
||||
//
|
||||
|
||||
// Add bars
|
||||
// ------------------------------
|
||||
|
||||
// Group each bar
|
||||
var bar = svg.selectAll(".d3-bar")
|
||||
.data(data)
|
||||
.enter()
|
||||
.append("g")
|
||||
.attr("class", "d3-bar")
|
||||
.attr("transform", function(d) { return "translate(" + x(d.x) + "," + y(d.y) + ")"; })
|
||||
.on('mouseover', tip.show)
|
||||
.on('mouseout', tip.hide);
|
||||
|
||||
// Append bars
|
||||
bar.append("rect")
|
||||
.attr("x", 1)
|
||||
.attr("width", x(data[0].dx) - 3)
|
||||
.attr("height", function(d) { return height - y(d.y); })
|
||||
.style("fill", function(d) { return color(d); });
|
||||
|
||||
// Append text
|
||||
bar.append("text")
|
||||
.attr("dy", ".75em")
|
||||
.attr("y", -15)
|
||||
.attr("x", x(data[0].dx) / 2)
|
||||
.style("text-anchor", "middle")
|
||||
.style("fill", "#333")
|
||||
.text(function(d) { return formatCount(d.y); });
|
||||
|
||||
// Append axes
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
svg.append("g")
|
||||
.attr("class", "d3-axis d3-axis-horizontal d3-axis-strong")
|
||||
.attr("transform", "translate(0," + height + ")")
|
||||
.call(xAxis);
|
||||
|
||||
|
||||
|
||||
// Resize chart
|
||||
// ------------------------------
|
||||
|
||||
// Call function on window resize
|
||||
$(window).on('resize', resize);
|
||||
|
||||
// Call function on sidebar width change
|
||||
$('.sidebar-control').on('click', resize);
|
||||
|
||||
// Resize function
|
||||
//
|
||||
// Since D3 doesn't support SVG resize by default,
|
||||
// we need to manually specify parts of the graph that need to
|
||||
// be updated on window resize
|
||||
function resize() {
|
||||
|
||||
// Layout variables
|
||||
width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right;
|
||||
|
||||
|
||||
// Layout
|
||||
// -------------------------
|
||||
|
||||
// Main svg width
|
||||
container.attr("width", width + margin.left + margin.right);
|
||||
|
||||
// Width of appended group
|
||||
svg.attr("width", width + margin.left + margin.right);
|
||||
|
||||
|
||||
// Axes
|
||||
// -------------------------
|
||||
|
||||
// Horizontal range
|
||||
x.range([0, width]);
|
||||
|
||||
// Horizontal axis
|
||||
svg.selectAll('.d3-axis-horizontal').call(xAxis);
|
||||
|
||||
|
||||
// Chart elements
|
||||
// -------------------------
|
||||
|
||||
// Bar group
|
||||
svg.selectAll('.d3-bar').attr("transform", function(d) { return "translate(" + x(d.x) + "," + y(d.y) + ")"; });
|
||||
|
||||
// Bar rect
|
||||
svg.selectAll('.d3-bar rect').attr("x", 1).attr("width", x(data[0].dx) - 3);
|
||||
|
||||
// Bar text
|
||||
svg.selectAll('.d3-bar text').attr("x", x(data[0].dx) / 2);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,271 @@
|
||||
/* ------------------------------------------------------------------------------
|
||||
*
|
||||
* # D3.js - bars with simple interaction
|
||||
*
|
||||
* Demo d3.js bar chart setup with animated data source change
|
||||
*
|
||||
* Version: 1.0
|
||||
* Latest update: August 1, 2015
|
||||
*
|
||||
* ---------------------------------------------------------------------------- */
|
||||
|
||||
$(function () {
|
||||
|
||||
// Create Uniform checkbox
|
||||
$(".toggle-dataset").uniform();
|
||||
|
||||
|
||||
// Initialize chart
|
||||
interaction('#d3-simple-interaction', 400);
|
||||
|
||||
// Chart setup
|
||||
function interaction(element, height) {
|
||||
|
||||
|
||||
// Basic setup
|
||||
// ------------------------------
|
||||
|
||||
// Define main variables
|
||||
var d3Container = d3.select(element),
|
||||
margin = {top: 5, right: 20, bottom: 20, left: 10},
|
||||
width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right,
|
||||
height = height - margin.top - margin.bottom - 5;
|
||||
|
||||
// Demo data set
|
||||
var dataset = [40.5, 33.1, 31.6, 31.0, 29.9, 28.9, 25.2, 25.2, 24.8, 24.3, 24.0, 22.6, 20.5, 19.5, 19.0, 18.9, 18.8, 18.5, 18.4, 17.6, 17.1];
|
||||
|
||||
|
||||
|
||||
// Construct scales
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
var x = d3.scale.ordinal()
|
||||
.domain(d3.range(dataset.length))
|
||||
.rangeRoundBands([0, width], 0.05);
|
||||
|
||||
// Vertical
|
||||
var y = d3.scale.linear()
|
||||
.domain([0, d3.max(dataset)])
|
||||
.range([0, height]);
|
||||
|
||||
// Colors
|
||||
var colors = d3.scale.category20();
|
||||
|
||||
|
||||
|
||||
// Create axes
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
var xAxis = d3.svg.axis()
|
||||
.scale(x)
|
||||
.orient("bottom");
|
||||
|
||||
|
||||
|
||||
// Create chart
|
||||
// ------------------------------
|
||||
|
||||
// Add SVG element
|
||||
var container = d3Container.append("svg");
|
||||
|
||||
// Add SVG group
|
||||
var svg = container
|
||||
.attr("width", width + margin.left + margin.right)
|
||||
.attr("height", height + margin.top + margin.bottom)
|
||||
.append("g")
|
||||
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
|
||||
|
||||
|
||||
|
||||
// Add tooltip
|
||||
// ------------------------------
|
||||
|
||||
// Create tooltip
|
||||
var tip = d3.tip()
|
||||
.attr('class', 'd3-tip')
|
||||
.offset([-10, 0])
|
||||
.html(function(d) { return d })
|
||||
|
||||
// Initialize tooltip
|
||||
svg.call(tip);
|
||||
|
||||
|
||||
//
|
||||
// Append chart elements
|
||||
//
|
||||
|
||||
// Append bars
|
||||
// ------------------------------
|
||||
|
||||
// Add bars
|
||||
var drawBars = svg.selectAll(".d3-bar")
|
||||
.data(dataset)
|
||||
.enter()
|
||||
.append("rect")
|
||||
.attr("class", "d3-bar")
|
||||
.attr("x", function(d, i) { return x(i) })
|
||||
.attr("width", x.rangeBand())
|
||||
.attr("height", 0)
|
||||
.attr("y", height)
|
||||
.attr("fill", function(d, i) { return colors(i); })
|
||||
.style("cursor", "pointer")
|
||||
.on('mouseover', tip.show)
|
||||
.on('mouseout', tip.hide)
|
||||
|
||||
// Add bar transition
|
||||
drawBars.transition()
|
||||
.delay(200)
|
||||
.duration(1000)
|
||||
.attr("height", function(d) { return y(d) })
|
||||
.attr("y", function(d) { return height - y(d) })
|
||||
|
||||
|
||||
// Add text labels
|
||||
var drawLabels = svg.selectAll(".value-label")
|
||||
.data(dataset)
|
||||
.enter()
|
||||
.append("text")
|
||||
.attr("class", "value-label")
|
||||
.attr("x", function(d, i) { return x(i) + x.rangeBand() / 2 })
|
||||
.attr("y", function(d) { return height - y(d) + 25; })
|
||||
.style('opacity', 0)
|
||||
.style("text-anchor", "middle")
|
||||
.style("fill", "white")
|
||||
.text(function(d) {return d;});
|
||||
|
||||
// Add text label transition
|
||||
drawLabels.transition()
|
||||
.delay(1000)
|
||||
.duration(500)
|
||||
.style('opacity', 1);
|
||||
|
||||
|
||||
|
||||
// Create axes
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
var xAxis = d3.svg.axis()
|
||||
.scale(x)
|
||||
.orient("bottom");
|
||||
|
||||
|
||||
|
||||
// Change data sets
|
||||
// ------------------------------
|
||||
|
||||
$('.toggle-dataset').on('change', function() {
|
||||
if(this.checked) {
|
||||
|
||||
dataset = [8.4, 12.1, 25.5, 10.3, 11.7, 10.9, 13.3, 23.1, 15.4, 12.3, 17.8, 18.8, 14.7, 8.8, 11.2, 10.2, 17.1, 14.5, 11.9, 7.3, 7.4];
|
||||
|
||||
// Update all rects
|
||||
svg.selectAll("rect")
|
||||
.data(dataset)
|
||||
.transition()
|
||||
.delay(0)
|
||||
.duration(1000)
|
||||
.ease('cubic-in-out')
|
||||
.attr("y", function(d) { return height - y(d) })
|
||||
.attr("height", function(d) { return y(d) })
|
||||
.style("fill", colors)
|
||||
|
||||
// Update labels
|
||||
var drawNewlabels = svg.selectAll("text")
|
||||
.data(dataset)
|
||||
.attr("x", function(d, i) {return x(i) + x.rangeBand() / 2 })
|
||||
.attr("y", function(d) {return height - y(d) + 25 })
|
||||
.style('opacity', 0)
|
||||
.text(function(d) {return d;});
|
||||
|
||||
// Transition
|
||||
drawNewlabels.transition()
|
||||
.delay(1000)
|
||||
.duration(500)
|
||||
.style('opacity', 1)
|
||||
}
|
||||
else {
|
||||
|
||||
dataset = [40.5, 33.1, 31.6, 31.0, 29.9, 28.9, 25.2, 25.2, 24.8, 24.3, 24.0, 22.6, 20.5, 19.5, 19.0, 18.9, 18.8, 18.5, 18.4, 17.6, 17.1];
|
||||
|
||||
// Update all rects
|
||||
svg.selectAll("rect")
|
||||
.data(dataset)
|
||||
.transition()
|
||||
.delay(0)
|
||||
.duration(1000)
|
||||
.ease('cubic-in-out')
|
||||
.attr("y", function(d) { return height - y(d) })
|
||||
.attr("height", function(d) { return y(d) })
|
||||
.style("fill", function(d, i) { return colors(i); });
|
||||
|
||||
|
||||
/* Update labels */
|
||||
var drawFirstlabels = svg.selectAll("text")
|
||||
.data(dataset)
|
||||
.attr("x", function(d, i) {return x(i) + x.rangeBand() / 2 })
|
||||
.attr("y", function(d) {return height - y(d) + 25 })
|
||||
.style('opacity', 0)
|
||||
.text(function(d) {return d;});
|
||||
|
||||
drawFirstlabels.transition()
|
||||
.delay(1000)
|
||||
.duration(500)
|
||||
.style('opacity', 1);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// Resize chart
|
||||
// ------------------------------
|
||||
|
||||
// Call function on window resize
|
||||
$(window).on('resize', resize);
|
||||
|
||||
// Call function on sidebar width change
|
||||
$('.sidebar-control').on('click', resize);
|
||||
|
||||
// Resize function
|
||||
//
|
||||
// Since D3 doesn't support SVG resize by default,
|
||||
// we need to manually specify parts of the graph that need to
|
||||
// be updated on window resize
|
||||
function resize() {
|
||||
|
||||
// Layout variables
|
||||
width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right;
|
||||
|
||||
|
||||
// Layout
|
||||
// -------------------------
|
||||
|
||||
// Main svg width
|
||||
container.attr("width", width + margin.left + margin.right);
|
||||
|
||||
// Width of appended group
|
||||
svg.attr("width", width + margin.left + margin.right);
|
||||
|
||||
|
||||
// Axes
|
||||
// -------------------------
|
||||
|
||||
// Horizontal range
|
||||
x.rangeRoundBands([0, width], 0.05);
|
||||
|
||||
// Horizontal axis
|
||||
svg.selectAll('.d3-axis-horizontal').call(xAxis);
|
||||
|
||||
|
||||
// Chart elements
|
||||
// -------------------------
|
||||
|
||||
// Bars
|
||||
svg.selectAll('.d3-bar').attr("x", function(d, i) { return x(i) }).attr("width", x.rangeBand());
|
||||
|
||||
// Text label
|
||||
svg.selectAll(".value-label").attr("x", function(d, i) { return x(i) + x.rangeBand() / 2 });
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,201 @@
|
||||
/* ------------------------------------------------------------------------------
|
||||
*
|
||||
* # D3.js - horizontal sortable bars
|
||||
*
|
||||
* Demo d3.js horizontal bar chart setup with animated sorting
|
||||
*
|
||||
* Version: 1.0
|
||||
* Latest update: August 1, 2015
|
||||
*
|
||||
* ---------------------------------------------------------------------------- */
|
||||
|
||||
$(function () {
|
||||
|
||||
// Initialize chart
|
||||
sortableVertical('#d3-bar-sortable-horizontal', 400);
|
||||
|
||||
// Chart setup
|
||||
function sortableVertical(element, height) {
|
||||
|
||||
|
||||
// Basic setup
|
||||
// ------------------------------
|
||||
|
||||
// Define main variables
|
||||
var d3Container = d3.select(element),
|
||||
margin = {top: 5, right: 20, bottom: 20, left: 40},
|
||||
width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right,
|
||||
height = height - margin.top - margin.bottom - 5;
|
||||
|
||||
// Add data
|
||||
var index = d3.range(8),
|
||||
data = index.map(d3.random.normal(40, 10));
|
||||
|
||||
|
||||
|
||||
// Construct scales
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
var x = d3.scale.linear()
|
||||
.domain([0, d3.max(data)])
|
||||
.range([0, width]);
|
||||
|
||||
// Vertical
|
||||
var y = d3.scale.ordinal()
|
||||
.domain(index)
|
||||
.rangeRoundBands([height, 0], .3);
|
||||
|
||||
// Colors
|
||||
var colors = d3.scale.category20c();
|
||||
|
||||
|
||||
|
||||
// Create axes
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
var xAxis = d3.svg.axis()
|
||||
.scale(x)
|
||||
.orient("bottom");
|
||||
|
||||
// Vertical
|
||||
var yAxis = d3.svg.axis()
|
||||
.scale(y)
|
||||
.orient("left")
|
||||
.ticks(8);
|
||||
|
||||
|
||||
|
||||
// Create chart
|
||||
// ------------------------------
|
||||
|
||||
// Add SVG element
|
||||
var container = d3Container.append("svg");
|
||||
|
||||
// Add SVG group
|
||||
var svg = container
|
||||
.attr("width", width + margin.left + margin.right)
|
||||
.attr("height", height + margin.top + margin.bottom)
|
||||
.append("g")
|
||||
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
|
||||
|
||||
|
||||
//
|
||||
// Append chart elements
|
||||
//
|
||||
|
||||
// Append axes
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
svg.append("g")
|
||||
.attr("class", "d3-axis d3-axis-horizontal d3-axis-strong")
|
||||
.attr("transform", "translate(0," + height + ")")
|
||||
.call(xAxis);
|
||||
|
||||
// Vertical
|
||||
svg.append("g")
|
||||
.attr("class", "d3-axis d3-axis-vertical d3-axis-strong")
|
||||
.call(yAxis);
|
||||
|
||||
|
||||
// Append bars
|
||||
// ------------------------------
|
||||
|
||||
// Group each bar
|
||||
var bar = svg.selectAll(".d3-bar")
|
||||
.data(data)
|
||||
.enter()
|
||||
.append("g")
|
||||
.attr("class", "d3-bar")
|
||||
.attr("fill", function(d, i) { return colors(i); })
|
||||
.attr("transform", function(d, i) { return "translate(0," + y(i) + ")"; });
|
||||
|
||||
// Append bar rectangle
|
||||
bar.append("rect")
|
||||
.attr("height", y.rangeBand())
|
||||
.attr("width", x);
|
||||
|
||||
// Append text label
|
||||
bar.append("text")
|
||||
.attr("x", function(d) { return x(d) - 12 })
|
||||
.attr("y", y.rangeBand() / 2)
|
||||
.attr("dy", ".35em")
|
||||
.style("fill", "#fff")
|
||||
.style("text-anchor", "end")
|
||||
.text(function(d, i) { return i; });
|
||||
|
||||
|
||||
// Setup sort
|
||||
// ------------------------------
|
||||
|
||||
var sort = false;
|
||||
setInterval(function() {
|
||||
if (sort = !sort) {
|
||||
index.sort(function(a, b) { return data[a] - data[b]; });
|
||||
} else {
|
||||
index = d3.range(8);
|
||||
}
|
||||
|
||||
y.domain(index);
|
||||
|
||||
bar.transition()
|
||||
.duration(750)
|
||||
.delay(function(d, i) { return i * 50; })
|
||||
.attr("transform", function(d, i) { return "translate(0," + y(i) + ")"; });
|
||||
}, 4000);
|
||||
|
||||
|
||||
|
||||
// Resize chart
|
||||
// ------------------------------
|
||||
|
||||
// Call function on window resize
|
||||
$(window).on('resize', resize);
|
||||
|
||||
// Call function on sidebar width change
|
||||
$('.sidebar-control').on('click', resize);
|
||||
|
||||
// Resize function
|
||||
//
|
||||
// Since D3 doesn't support SVG resize by default,
|
||||
// we need to manually specify parts of the graph that need to
|
||||
// be updated on window resize
|
||||
function resize() {
|
||||
|
||||
// Layout variables
|
||||
width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right;
|
||||
|
||||
|
||||
// Layout
|
||||
// -------------------------
|
||||
|
||||
// Main svg width
|
||||
container.attr("width", width + margin.left + margin.right);
|
||||
|
||||
// Width of appended group
|
||||
svg.attr("width", width + margin.left + margin.right);
|
||||
|
||||
|
||||
// Axes
|
||||
// -------------------------
|
||||
|
||||
// Horizontal range
|
||||
x.range([0, width]);
|
||||
|
||||
// Horizontal axis
|
||||
svg.selectAll('.d3-axis-horizontal').call(xAxis);
|
||||
|
||||
|
||||
// Chart elements
|
||||
// -------------------------
|
||||
|
||||
// Bar rect
|
||||
svg.selectAll('.d3-bar rect').attr("width", x);
|
||||
|
||||
// Bar text
|
||||
svg.selectAll('.d3-bar text').attr("x", function(d) { return x(d) - 12; });
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,236 @@
|
||||
/* ------------------------------------------------------------------------------
|
||||
*
|
||||
* # D3.js - vertical sortable bars
|
||||
*
|
||||
* Demo d3.js vertical bar chart setup with animated sorting and .tsv data source
|
||||
*
|
||||
* Version: 1.0
|
||||
* Latest update: August 1, 2015
|
||||
*
|
||||
* ---------------------------------------------------------------------------- */
|
||||
|
||||
$(function () {
|
||||
|
||||
// Add uniform styling
|
||||
$(".toggle-sort").uniform();
|
||||
|
||||
|
||||
// Initialize chart
|
||||
sortableVertical('#d3-bar-sortable-vertical', 400);
|
||||
|
||||
// Chart setup
|
||||
function sortableVertical(element, height) {
|
||||
|
||||
|
||||
// Basic setup
|
||||
// ------------------------------
|
||||
|
||||
// Define main variables
|
||||
var d3Container = d3.select(element),
|
||||
margin = {top: 5, right: 20, bottom: 20, left: 40},
|
||||
width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right,
|
||||
height = height - margin.top - margin.bottom - 5;
|
||||
|
||||
// Format data
|
||||
var formatPercent = d3.format(".0%");
|
||||
|
||||
|
||||
|
||||
// Construct scales
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
var x = d3.scale.ordinal()
|
||||
.rangeRoundBands([0, width], .1, 1);
|
||||
|
||||
// Vertical
|
||||
var y = d3.scale.linear()
|
||||
.range([height, 0]);
|
||||
|
||||
// Colors
|
||||
var colors = d3.scale.category20c();
|
||||
|
||||
|
||||
|
||||
// Create axes
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
var xAxis = d3.svg.axis()
|
||||
.scale(x)
|
||||
.orient("bottom");
|
||||
|
||||
// Vertical
|
||||
var yAxis = d3.svg.axis()
|
||||
.scale(y)
|
||||
.orient("left")
|
||||
.tickFormat(formatPercent);
|
||||
|
||||
|
||||
|
||||
// Create chart
|
||||
// ------------------------------
|
||||
|
||||
// Add SVG element
|
||||
var container = d3Container.append("svg");
|
||||
|
||||
// Add SVG group
|
||||
var svg = container
|
||||
.attr("width", width + margin.left + margin.right)
|
||||
.attr("height", height + margin.top + margin.bottom)
|
||||
.append("g")
|
||||
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
|
||||
|
||||
|
||||
// Load data
|
||||
// ------------------------------
|
||||
|
||||
d3.tsv("assets/demo_data/d3/bars/bars_basic.tsv", function(error, data) {
|
||||
|
||||
// Pull out values
|
||||
data.forEach(function(d) {
|
||||
d.frequency = +d.frequency;
|
||||
});
|
||||
|
||||
|
||||
// Set input domains
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
x.domain(data.map(function(d) { return d.letter; }));
|
||||
|
||||
// Vertical
|
||||
y.domain([0, d3.max(data, function(d) { return d.frequency; })]);
|
||||
|
||||
|
||||
//
|
||||
// Append chart elements
|
||||
//
|
||||
|
||||
// Append axes
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
svg.append("g")
|
||||
.attr("class", "d3-axis d3-axis-horizontal d3-axis-strong")
|
||||
.attr("transform", "translate(0," + height + ")")
|
||||
.call(xAxis);
|
||||
|
||||
// Vertical
|
||||
var verticalAxis = svg.append("g")
|
||||
.attr("class", "d3-axis d3-axis-vertical d3-axis-strong")
|
||||
.call(yAxis);
|
||||
|
||||
// Add text label
|
||||
verticalAxis.append("text")
|
||||
.attr("transform", "rotate(-90)")
|
||||
.attr("y", 10)
|
||||
.attr("dy", ".71em")
|
||||
.style("text-anchor", "end")
|
||||
.style("fill", "#999")
|
||||
.style("font-size", 12)
|
||||
.text("Frequency");
|
||||
|
||||
|
||||
// Append bars
|
||||
// ------------------------------
|
||||
|
||||
svg.selectAll(".d3-bar")
|
||||
.data(data)
|
||||
.enter()
|
||||
.append("rect")
|
||||
.attr("class", "d3-bar")
|
||||
.attr("fill", function(d, i) { return colors(i); })
|
||||
.attr("x", function(d) { return x(d.letter); })
|
||||
.attr("width", x.rangeBand())
|
||||
.attr("y", function(d) { return y(d.frequency); })
|
||||
.attr("height", function(d) { return height - y(d.frequency); });
|
||||
|
||||
|
||||
// Change data sets
|
||||
// ------------------------------
|
||||
|
||||
// Attach change event
|
||||
d3.select(".toggle-sort").on("change", change);
|
||||
|
||||
// Sort values on page load with delay
|
||||
var sortTimeout = setTimeout(function() {
|
||||
d3.select(".toggle-sort").property("checked", true).each(change);
|
||||
$.uniform.update();
|
||||
}, 2000);
|
||||
|
||||
// Sorting function
|
||||
function change() {
|
||||
clearTimeout(sortTimeout);
|
||||
|
||||
// Copy-on-write since tweens are evaluated after a delay.
|
||||
var x0 = x.domain(data.sort(this.checked
|
||||
? function(a, b) { return b.frequency - a.frequency; }
|
||||
: function(a, b) { return d3.ascending(a.letter, b.letter); })
|
||||
.map(function(d) { return d.letter; }))
|
||||
.copy();
|
||||
|
||||
var transition = svg.transition().duration(750),
|
||||
delay = function(d, i) { return i * 50; };
|
||||
|
||||
transition.selectAll(".d3-bar")
|
||||
.delay(delay)
|
||||
.attr("x", function(d) { return x0(d.letter); });
|
||||
|
||||
transition.select(".d3-axis-horizontal")
|
||||
.call(xAxis)
|
||||
.selectAll("g")
|
||||
.delay(delay);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
// Resize chart
|
||||
// ------------------------------
|
||||
|
||||
// Call function on window resize
|
||||
$(window).on('resize', resize);
|
||||
|
||||
// Call function on sidebar width change
|
||||
$('.sidebar-control').on('click', resize);
|
||||
|
||||
// Resize function
|
||||
//
|
||||
// Since D3 doesn't support SVG resize by default,
|
||||
// we need to manually specify parts of the graph that need to
|
||||
// be updated on window resize
|
||||
function resize() {
|
||||
|
||||
// Layout variables
|
||||
width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right;
|
||||
|
||||
|
||||
// Layout
|
||||
// -------------------------
|
||||
|
||||
// Main svg width
|
||||
container.attr("width", width + margin.left + margin.right);
|
||||
|
||||
// Width of appended group
|
||||
svg.attr("width", width + margin.left + margin.right);
|
||||
|
||||
|
||||
// Axes
|
||||
// -------------------------
|
||||
|
||||
// Horizontal range
|
||||
x.rangeRoundBands([0, width], .1, 1);
|
||||
|
||||
// Horizontal axis
|
||||
svg.selectAll('.d3-axis-horizontal').call(xAxis);
|
||||
|
||||
|
||||
// Chart elements
|
||||
// -------------------------
|
||||
|
||||
// Line path
|
||||
svg.selectAll('.d3-bar').attr("width", x.rangeBand()).attr("x", function(d) { return x(d.letter); });
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,291 @@
|
||||
/* ------------------------------------------------------------------------------
|
||||
*
|
||||
* # D3.js - stacked and multiple bars
|
||||
*
|
||||
* Demo d3.js bar chart setup with animated transition between stacked and multiple bars
|
||||
*
|
||||
* Version: 1.0
|
||||
* Latest update: August 1, 2015
|
||||
*
|
||||
* ---------------------------------------------------------------------------- */
|
||||
|
||||
$(function () {
|
||||
|
||||
// Create Uniform checkbox
|
||||
$(".stacked-multiple").uniform({
|
||||
radioClass: 'choice'
|
||||
});
|
||||
|
||||
|
||||
// Initialize chart
|
||||
stackedMultiples('#d3-bar-stacked-multiples', 400);
|
||||
|
||||
// Chart setup
|
||||
function stackedMultiples(element, height) {
|
||||
|
||||
|
||||
// Basic setup
|
||||
// ------------------------------
|
||||
|
||||
// Define main variables
|
||||
var d3Container = d3.select(element),
|
||||
margin = {top: 5, right: 20, bottom: 20, left: 60},
|
||||
width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right,
|
||||
height = height - margin.top - margin.bottom - 5;
|
||||
|
||||
|
||||
// Format data
|
||||
var parseDate = d3.time.format("%Y-%m").parse,
|
||||
formatYear = d3.format("02d"),
|
||||
formatDate = function(d) { return "Q" + ((d.getMonth() / 3 | 0) + 1) + formatYear(d.getFullYear() % 100); };
|
||||
|
||||
|
||||
|
||||
// Construct scales
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
var x = d3.scale.ordinal()
|
||||
.rangeRoundBands([0, width], .2);
|
||||
|
||||
// Vertical
|
||||
var y = d3.scale.ordinal()
|
||||
.rangeRoundBands([height, 0]);
|
||||
|
||||
var y0 = d3.scale.ordinal()
|
||||
.rangeRoundBands([height, 0]);
|
||||
|
||||
var y1 = d3.scale.linear();
|
||||
|
||||
// Colors
|
||||
var color = d3.scale.category20();
|
||||
|
||||
|
||||
|
||||
// Create axes
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
var xAxis = d3.svg.axis()
|
||||
.scale(x)
|
||||
.orient("bottom")
|
||||
.tickFormat(formatDate);
|
||||
|
||||
// Vertical
|
||||
var yAxis = d3.svg.axis()
|
||||
.scale(y)
|
||||
.orient("left")
|
||||
.ticks(10, "%");
|
||||
|
||||
|
||||
|
||||
// Create chart
|
||||
// ------------------------------
|
||||
|
||||
// Add SVG element
|
||||
var container = d3Container.append("svg");
|
||||
|
||||
// Add SVG group
|
||||
var svg = container
|
||||
.attr("width", width + margin.left + margin.right)
|
||||
.attr("height", height + margin.top + margin.bottom)
|
||||
.append("g")
|
||||
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
|
||||
|
||||
|
||||
|
||||
// Construct chart layout
|
||||
// ------------------------------
|
||||
|
||||
// Nest
|
||||
var nest = d3.nest()
|
||||
.key(function(d) { return d.browser; });
|
||||
|
||||
// Stack
|
||||
var stack = d3.layout.stack()
|
||||
.values(function(d) { return d.values; })
|
||||
.x(function(d) { return d.date; })
|
||||
.y(function(d) { return d.value; })
|
||||
.out(function(d, y0) { d.valueOffset = y0; });
|
||||
|
||||
|
||||
|
||||
|
||||
// Load data
|
||||
// ------------------------------
|
||||
|
||||
d3.tsv("assets/demo_data/d3/bars/bars_stacked_multiple.tsv", function(error, data) {
|
||||
|
||||
// Pull out values
|
||||
data.forEach(function(d) {
|
||||
d.date = parseDate(d.date);
|
||||
d.value = +d.value;
|
||||
});
|
||||
|
||||
// Nest values
|
||||
var dataByGroup = nest.entries(data);
|
||||
|
||||
|
||||
// Set input domains
|
||||
// ------------------------------
|
||||
|
||||
// Stack
|
||||
stack(dataByGroup);
|
||||
|
||||
// Horizontal
|
||||
x.domain(dataByGroup[0].values.map(function(d) { return d.date; }));
|
||||
|
||||
// Vertical
|
||||
y0.domain(dataByGroup.map(function(d) { return d.key; }));
|
||||
y1.domain([0, d3.max(data, function(d) { return d.value; })]).range([y0.rangeBand(), 0]);
|
||||
|
||||
|
||||
//
|
||||
// Append chart elements
|
||||
//
|
||||
|
||||
// Add bars
|
||||
// ------------------------------
|
||||
|
||||
// Group bars
|
||||
var group = svg.selectAll(".d3-bar-group")
|
||||
.data(dataByGroup)
|
||||
.enter()
|
||||
.append("g")
|
||||
.attr("class", "d3-bar-group")
|
||||
.attr("transform", function(d) { return "translate(0," + y0(d.key) + ")"; });
|
||||
|
||||
// Append text
|
||||
group.append("text")
|
||||
.attr("class", "d3-group-label")
|
||||
.attr("x", -12)
|
||||
.attr("y", function(d) { return y1(d.values[0].value / 2); })
|
||||
.attr("dy", ".35em")
|
||||
.style("text-anchor", "end")
|
||||
.text(function(d) { return d.key; });
|
||||
|
||||
// Add bars
|
||||
group.selectAll(".d3-bar")
|
||||
.data(function(d) { return d.values; })
|
||||
.enter()
|
||||
.append("rect")
|
||||
.attr("class", "d3-bar")
|
||||
.attr("x", function(d) { return x(d.date); })
|
||||
.attr("y", function(d) { return y1(d.value); })
|
||||
.attr("width", x.rangeBand())
|
||||
.attr("height", function(d) { return y0.rangeBand() - y1(d.value); })
|
||||
.style("fill", function(d) { return color(d.browser); });
|
||||
|
||||
|
||||
// Append axes
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
group.filter(function(d, i) { return !i; }).append("g")
|
||||
.attr("class", "d3-axis d3-axis-horizontal d3-axis-strong")
|
||||
.attr("transform", "translate(0," + (y0.rangeBand() + 1) + ")")
|
||||
.call(xAxis);
|
||||
|
||||
// Vertical
|
||||
var verticalAxis = svg.append("g")
|
||||
.attr("class", "d3-axis d3-axis-vertical d3-axis-strong")
|
||||
.call(yAxis);
|
||||
|
||||
// Appent text label
|
||||
verticalAxis.append("text")
|
||||
.attr('class', 'browser-label')
|
||||
.attr("x", -12)
|
||||
.attr("y", 12)
|
||||
.attr("dy", ".71em")
|
||||
.style("text-anchor", "end")
|
||||
.style("fill", "#999")
|
||||
.style("font-size", 12)
|
||||
.text("Browser");
|
||||
|
||||
|
||||
// Setup layout change
|
||||
// ------------------------------
|
||||
|
||||
// Add change event
|
||||
d3.selectAll(".stacked-multiple").on("change", change);
|
||||
|
||||
// Change value on page load
|
||||
var timeout = setTimeout(function() {
|
||||
d3.select("input[value=\"stacked\"]").property("checked", true).each(change);
|
||||
$.uniform.update();
|
||||
}, 2000);
|
||||
|
||||
// Change function
|
||||
function change() {
|
||||
clearTimeout(timeout);
|
||||
if (this.value === "multiples") transitionMultiples();
|
||||
else transitionStacked();
|
||||
}
|
||||
|
||||
// Transition to multiples
|
||||
function transitionMultiples() {
|
||||
var t = svg.transition().duration(750),
|
||||
g = t.selectAll(".d3-bar-group").attr("transform", function(d) { return "translate(0," + y0(d.key) + ")"; });
|
||||
g.selectAll(".d3-bar").attr("y", function(d) { return y1(d.value); });
|
||||
g.select(".d3-group-label").attr("y", function(d) { return y1(d.values[0].value / 2); })
|
||||
}
|
||||
|
||||
// Transition to stacked
|
||||
function transitionStacked() {
|
||||
var t = svg.transition().duration(750),
|
||||
g = t.selectAll(".d3-bar-group").attr("transform", "translate(0," + y0(y0.domain()[0]) + ")");
|
||||
g.selectAll(".d3-bar").attr("y", function(d) { return y1(d.value + d.valueOffset) });
|
||||
g.select(".d3-group-label").attr("y", function(d) { return y1(d.values[0].value / 2 + d.values[0].valueOffset); })
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
// Resize chart
|
||||
// ------------------------------
|
||||
|
||||
// Call function on window resize
|
||||
$(window).on('resize', resize);
|
||||
|
||||
// Call function on sidebar width change
|
||||
$('.sidebar-control').on('click', resize);
|
||||
|
||||
// Resize function
|
||||
//
|
||||
// Since D3 doesn't support SVG resize by default,
|
||||
// we need to manually specify parts of the graph that need to
|
||||
// be updated on window resize
|
||||
function resize() {
|
||||
|
||||
// Layout variables
|
||||
width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right;
|
||||
|
||||
|
||||
// Layout
|
||||
// -------------------------
|
||||
|
||||
// Main svg width
|
||||
container.attr("width", width + margin.left + margin.right);
|
||||
|
||||
// Width of appended group
|
||||
svg.attr("width", width + margin.left + margin.right);
|
||||
|
||||
|
||||
// Axes
|
||||
// -------------------------
|
||||
|
||||
// Horizontal range
|
||||
x.rangeRoundBands([0, width], .2);
|
||||
|
||||
// Horizontal axis
|
||||
svg.selectAll('.d3-axis-horizontal').call(xAxis);
|
||||
|
||||
|
||||
// Chart elements
|
||||
// -------------------------
|
||||
|
||||
// Line path
|
||||
svg.selectAll('.d3-bar').attr("x", function(d) { return x(d.date); }).attr("width", x.rangeBand());
|
||||
}
|
||||
}
|
||||
});
|
||||
+245
@@ -0,0 +1,245 @@
|
||||
/* ------------------------------------------------------------------------------
|
||||
*
|
||||
* # D3.js - grouped bar chart
|
||||
*
|
||||
* Demo d3.js grouped bar chart setup with .csv data source
|
||||
*
|
||||
* Version: 1.0
|
||||
* Latest update: August 1, 2015
|
||||
*
|
||||
* ---------------------------------------------------------------------------- */
|
||||
|
||||
$(function () {
|
||||
|
||||
// Initialize chart
|
||||
barGrouped('#d3-bar-grouped', 400);
|
||||
|
||||
// Chart setup
|
||||
function barGrouped(element, height) {
|
||||
|
||||
|
||||
// Basic setup
|
||||
// ------------------------------
|
||||
|
||||
// Define main variables
|
||||
var d3Container = d3.select(element),
|
||||
margin = {top: 5, right: 10, bottom: 20, left: 40},
|
||||
width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right,
|
||||
height = height - margin.top - margin.bottom - 5;
|
||||
|
||||
|
||||
|
||||
// Construct scales
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
var x0 = d3.scale.ordinal()
|
||||
.rangeRoundBands([0, width], .1);
|
||||
|
||||
var x1 = d3.scale.ordinal()
|
||||
.range([0, width]);
|
||||
|
||||
// Vertical
|
||||
var y = d3.scale.linear()
|
||||
.range([height, 0]);
|
||||
|
||||
// Colors
|
||||
var color = d3.scale.ordinal()
|
||||
.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);
|
||||
|
||||
|
||||
|
||||
// Create axes
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
var xAxis = d3.svg.axis()
|
||||
.scale(x0)
|
||||
.orient("bottom");
|
||||
|
||||
// Vertical
|
||||
var yAxis = d3.svg.axis()
|
||||
.scale(y)
|
||||
.orient("left")
|
||||
.tickFormat(d3.format(".2s"));
|
||||
|
||||
|
||||
|
||||
// Create chart
|
||||
// ------------------------------
|
||||
|
||||
// Add SVG element
|
||||
var container = d3Container.append("svg");
|
||||
|
||||
// Add SVG group
|
||||
var svg = container
|
||||
.attr("width", width + margin.left + margin.right)
|
||||
.attr("height", height + margin.top + margin.bottom)
|
||||
.append("g")
|
||||
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
|
||||
|
||||
|
||||
// Load data
|
||||
// ------------------------------
|
||||
|
||||
d3.csv("assets/demo_data/d3/bars/bars_grouped.csv", function(error, data) {
|
||||
|
||||
// Filter values by key
|
||||
var ageNames = d3.keys(data[0]).filter(function(key) { return key !== "State"; });
|
||||
|
||||
// Pull out values
|
||||
data.forEach(function(d) {
|
||||
d.ages = ageNames.map(function(name) { return {name: name, value: +d[name]}; });
|
||||
});
|
||||
|
||||
|
||||
// Set input domains
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
x0.domain(data.map(function(d) { return d.State; }));
|
||||
x1.domain(ageNames).rangeRoundBands([0, x0.rangeBand()]);
|
||||
|
||||
// Vertical
|
||||
y.domain([0, d3.max(data, function(d) { return d3.max(d.ages, function(d) { return d.value; }); })]);
|
||||
|
||||
|
||||
//
|
||||
// Append chart elements
|
||||
//
|
||||
|
||||
// Append axes
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
svg.append("g")
|
||||
.attr("class", "d3-axis d3-axis-horizontal d3-axis-strong")
|
||||
.attr("transform", "translate(0," + height + ")")
|
||||
.call(xAxis);
|
||||
|
||||
// Vertical
|
||||
var verticalAxis = svg.append("g")
|
||||
.attr("class", "d3-axis d3-axis-vertical d3-axis-strong")
|
||||
.call(yAxis);
|
||||
|
||||
// Add text label
|
||||
verticalAxis.append("text")
|
||||
.attr("transform", "rotate(-90)")
|
||||
.attr("y", 10)
|
||||
.attr("dy", ".71em")
|
||||
.style("text-anchor", "end")
|
||||
.style("fill", "#999")
|
||||
.style("font-size", 12)
|
||||
.text("Population");
|
||||
|
||||
|
||||
// Add bars
|
||||
// ------------------------------
|
||||
|
||||
// Group values
|
||||
var state = svg.selectAll(".bar-group")
|
||||
.data(data)
|
||||
.enter()
|
||||
.append("g")
|
||||
.attr("class", "bar-group")
|
||||
.attr("transform", function(d) { return "translate(" + x0(d.State) + ",0)"; });
|
||||
|
||||
// Append bars
|
||||
state.selectAll(".d3-bar")
|
||||
.data(function(d) { return d.ages; })
|
||||
.enter()
|
||||
.append("rect")
|
||||
.attr("class", "d3-bar")
|
||||
.attr("width", x1.rangeBand())
|
||||
.attr("x", function(d) { return x1(d.name); })
|
||||
.attr("y", function(d) { return y(d.value); })
|
||||
.attr("height", function(d) { return height - y(d.value); })
|
||||
.style("fill", function(d) { return color(d.name); });
|
||||
|
||||
|
||||
// Add legend
|
||||
// ------------------------------
|
||||
|
||||
// Create legend
|
||||
var legend = svg.selectAll(".d3-legend")
|
||||
.data(ageNames.slice().reverse())
|
||||
.enter()
|
||||
.append("g")
|
||||
.attr("class", "d3-legend")
|
||||
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
|
||||
|
||||
// Legend indicator
|
||||
legend.append("rect")
|
||||
.attr("x", width - 18)
|
||||
.attr("width", 18)
|
||||
.attr("height", 18)
|
||||
.style("fill", color);
|
||||
|
||||
// Legend text
|
||||
legend.append("text")
|
||||
.attr("x", width - 24)
|
||||
.attr("y", 9)
|
||||
.attr("dy", ".35em")
|
||||
.style("text-anchor", "end")
|
||||
.style("font-size", 12)
|
||||
.text(function(d) { return d; });
|
||||
});
|
||||
|
||||
|
||||
|
||||
// Resize chart
|
||||
// ------------------------------
|
||||
|
||||
// Call function on window resize
|
||||
$(window).on('resize', resize);
|
||||
|
||||
// Call function on sidebar width change
|
||||
$('.sidebar-control').on('click', resize);
|
||||
|
||||
// Resize function
|
||||
//
|
||||
// Since D3 doesn't support SVG resize by default,
|
||||
// we need to manually specify parts of the graph that need to
|
||||
// be updated on window resize
|
||||
function resize() {
|
||||
|
||||
// Layout variables
|
||||
width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right;
|
||||
|
||||
|
||||
// Layout
|
||||
// -------------------------
|
||||
|
||||
// Main svg width
|
||||
container.attr("width", width + margin.left + margin.right);
|
||||
|
||||
// Width of appended group
|
||||
svg.attr("width", width + margin.left + margin.right);
|
||||
|
||||
|
||||
// Axes
|
||||
// -------------------------
|
||||
|
||||
// Horizontal ranges
|
||||
x0.rangeRoundBands([0, width], .1);
|
||||
x1.rangeRoundBands([0, x0.rangeBand()]);
|
||||
|
||||
// Horizontal axis
|
||||
svg.selectAll('.d3-axis-horizontal').call(xAxis);
|
||||
|
||||
|
||||
// Chart elements
|
||||
// -------------------------
|
||||
|
||||
// Bar group
|
||||
svg.selectAll('.bar-group').attr("transform", function(d) { return "translate(" + x0(d.State) + ",0)"; });
|
||||
|
||||
// Bars
|
||||
svg.selectAll('.d3-bar').attr("width", x1.rangeBand()).attr("x", function(d) { return x1(d.name); });
|
||||
|
||||
// Legend
|
||||
svg.selectAll(".d3-legend text").attr("x", width - 24);
|
||||
svg.selectAll(".d3-legend rect").attr("x", width - 18);
|
||||
}
|
||||
}
|
||||
});
|
||||
+209
@@ -0,0 +1,209 @@
|
||||
/* ------------------------------------------------------------------------------
|
||||
*
|
||||
* # D3.js - horizontal bar chart
|
||||
*
|
||||
* Demo d3.js horizontal bar chart setup with .csv data source
|
||||
*
|
||||
* Version: 1.0
|
||||
* Latest update: August 1, 2015
|
||||
*
|
||||
* ---------------------------------------------------------------------------- */
|
||||
|
||||
$(function () {
|
||||
|
||||
// Initialize chart
|
||||
barVertical('#d3-bar-horizontal', 400);
|
||||
|
||||
// Chart setup
|
||||
function barVertical(element, height) {
|
||||
|
||||
|
||||
// Basic setup
|
||||
// ------------------------------
|
||||
|
||||
// Define main variables
|
||||
var d3Container = d3.select(element),
|
||||
margin = {top: 20, right: 10, bottom: 5, left: 40},
|
||||
width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right,
|
||||
height = height - margin.top - margin.bottom - 5,
|
||||
n = 12;
|
||||
|
||||
// Format data
|
||||
var format = d3.format(",.0f");
|
||||
|
||||
|
||||
|
||||
// Construct scales
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
var x = d3.scale.linear()
|
||||
.range([0, width]);
|
||||
|
||||
// Verticals
|
||||
var y = d3.scale.ordinal()
|
||||
.rangeRoundBands([0, height], .1);
|
||||
|
||||
// Colors
|
||||
var colors = d3.scale.category20();
|
||||
|
||||
|
||||
|
||||
// Create axes
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
var xAxis = d3.svg.axis()
|
||||
.scale(x)
|
||||
.orient("top")
|
||||
.tickSize(-height);
|
||||
|
||||
// Vertical
|
||||
var yAxis = d3.svg.axis()
|
||||
.scale(y)
|
||||
.orient("left")
|
||||
.tickSize(5);
|
||||
|
||||
|
||||
|
||||
// Create chart
|
||||
// ------------------------------
|
||||
|
||||
// Add SVG element
|
||||
var container = d3Container.append("svg");
|
||||
|
||||
// Add SVG group
|
||||
var svg = container
|
||||
.attr("width", width + margin.left + margin.right)
|
||||
.attr("height", height + margin.top + margin.bottom)
|
||||
.append("g")
|
||||
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
|
||||
|
||||
|
||||
|
||||
// Load data
|
||||
// ------------------------------
|
||||
|
||||
d3.csv("assets/demo_data/d3/bars/bars_horizontal.csv", function(data) {
|
||||
|
||||
// Parse numbers, and sort by value.
|
||||
data.forEach(function(d) { d.value = +d.value; });
|
||||
data.sort(function(a, b) { return b.value - a.value; });
|
||||
|
||||
|
||||
// Set input domains
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
x.domain([0, d3.max(data, function(d) { return d.value; })]);
|
||||
|
||||
// Verticals
|
||||
y.domain(data.map(function(d) { return d.name; }));
|
||||
|
||||
|
||||
//
|
||||
// Append chart elements
|
||||
//
|
||||
|
||||
// Append axes
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
svg.append("g")
|
||||
.attr("class", "d3-axis d3-axis-horizontal d3-axis-strong")
|
||||
.call(xAxis);
|
||||
|
||||
// Vertical
|
||||
svg.append("g")
|
||||
.attr("class", "d3-axis d3-axis-vertical d3-axis-strong")
|
||||
.call(yAxis);
|
||||
|
||||
// Remove lines
|
||||
svg.selectAll(".d3-axis line, .d3-axis path").attr("stroke-width", 0);
|
||||
|
||||
|
||||
// Append bars
|
||||
// ------------------------------
|
||||
|
||||
// Group bars
|
||||
var bar = svg.selectAll(".d3-bar-group")
|
||||
.data(data)
|
||||
.enter()
|
||||
.append("g")
|
||||
.attr("class", "d3-bar-group")
|
||||
.attr("fill", function(d, i) { return colors(i); })
|
||||
.attr("transform", function(d) { return "translate(0," + y(d.name) + ")"; });
|
||||
|
||||
// Add bar
|
||||
bar.append("rect")
|
||||
.attr("class", "d3-bar")
|
||||
.attr("width", function(d) { return x(d.value); })
|
||||
.attr("height", y.rangeBand());
|
||||
|
||||
// Add text label
|
||||
bar.append("text")
|
||||
.attr("class", "d3-label-value")
|
||||
.attr("x", function(d) { return x(d.value); })
|
||||
.attr("y", y.rangeBand() / 2)
|
||||
.attr("dx", -10)
|
||||
.attr("dy", ".35em")
|
||||
.style("text-anchor", "end")
|
||||
.style("fill", "#fff")
|
||||
.style("font-size", 12)
|
||||
.text(function(d) { return format(d.value); });
|
||||
});
|
||||
|
||||
|
||||
|
||||
// Resize chart
|
||||
// ------------------------------
|
||||
|
||||
// Call function on window resize
|
||||
$(window).on('resize', resize);
|
||||
|
||||
// Call function on sidebar width change
|
||||
$('.sidebar-control').on('click', resize);
|
||||
|
||||
// Resize function
|
||||
//
|
||||
// Since D3 doesn't support SVG resize by default,
|
||||
// we need to manually specify parts of the graph that need to
|
||||
// be updated on window resize
|
||||
function resize() {
|
||||
|
||||
// Layout variables
|
||||
width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right;
|
||||
|
||||
|
||||
// Layout
|
||||
// -------------------------
|
||||
|
||||
// Main svg width
|
||||
container.attr("width", width + margin.left + margin.right);
|
||||
|
||||
// Width of appended group
|
||||
svg.attr("width", width + margin.left + margin.right);
|
||||
|
||||
|
||||
// Axes
|
||||
// -------------------------
|
||||
|
||||
// Horizontal range
|
||||
x.range([0, width]);
|
||||
|
||||
// Horizontal axis
|
||||
svg.selectAll('.d3-axis-horizontal').call(xAxis);
|
||||
|
||||
|
||||
// Chart elements
|
||||
// -------------------------
|
||||
|
||||
// Line path
|
||||
svg.selectAll('.d3-bar').attr("width", function(d) { return x(d.value); })
|
||||
|
||||
// Text label
|
||||
svg.selectAll('.d3-label-value').attr("x", function(d) { return x(d.value); });
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
+244
@@ -0,0 +1,244 @@
|
||||
/* ------------------------------------------------------------------------------
|
||||
*
|
||||
* # D3.js - stacked bar chart
|
||||
*
|
||||
* Demo d3.js stacked bar chart setup with .csv data source
|
||||
*
|
||||
* Version: 1.0
|
||||
* Latest update: August 1, 2015
|
||||
*
|
||||
* ---------------------------------------------------------------------------- */
|
||||
|
||||
$(function () {
|
||||
|
||||
// Initialize chart
|
||||
barGrouped('#d3-bar-stacked', 400);
|
||||
|
||||
// Chart setup
|
||||
function barGrouped(element, height) {
|
||||
|
||||
|
||||
// Basic setup
|
||||
// ------------------------------
|
||||
|
||||
// Define main variables
|
||||
var d3Container = d3.select(element),
|
||||
margin = {top: 5, right: 10, bottom: 20, left: 40},
|
||||
width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right,
|
||||
height = height - margin.top - margin.bottom - 5;
|
||||
|
||||
|
||||
|
||||
// Construct scales
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
var x = d3.scale.ordinal()
|
||||
.rangeRoundBands([0, width], .1, .5);
|
||||
|
||||
// Vertical
|
||||
var y = d3.scale.linear()
|
||||
.rangeRound([height, 0]);
|
||||
|
||||
// Colors
|
||||
var color = d3.scale.ordinal()
|
||||
.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);
|
||||
|
||||
|
||||
|
||||
// Create axes
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
var xAxis = d3.svg.axis()
|
||||
.scale(x)
|
||||
.orient("bottom");
|
||||
|
||||
// Vertical
|
||||
var yAxis = d3.svg.axis()
|
||||
.scale(y)
|
||||
.orient("left")
|
||||
.tickFormat(d3.format(".2s"));
|
||||
|
||||
|
||||
|
||||
// Create chart
|
||||
// ------------------------------
|
||||
|
||||
// Add SVG element
|
||||
var container = d3Container.append("svg");
|
||||
|
||||
// Add SVG group
|
||||
var svg = container
|
||||
.attr("width", width + margin.left + margin.right)
|
||||
.attr("height", height + margin.top + margin.bottom)
|
||||
.append("g")
|
||||
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
|
||||
|
||||
|
||||
|
||||
// Load data
|
||||
// ------------------------------
|
||||
|
||||
d3.csv("assets/demo_data/d3/bars/bars_stacked.csv", function(error, data) {
|
||||
|
||||
// Filter values by key
|
||||
color.domain(d3.keys(data[0]).filter(function(key) { return key !== "State"; }));
|
||||
|
||||
// Pull out values
|
||||
data.forEach(function(d) {
|
||||
var y0 = 0;
|
||||
d.ages = color.domain().map(function(name) { return {name: name, y0: y0, y1: y0 += +d[name]}; });
|
||||
d.total = d.ages[d.ages.length - 1].y1;
|
||||
});
|
||||
|
||||
// Sort data
|
||||
data.sort(function(a, b) { return b.total - a.total; });
|
||||
|
||||
|
||||
// Set input domains
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
x.domain(data.map(function(d) { return d.State; }));
|
||||
|
||||
// Vertical
|
||||
y.domain([0, d3.max(data, function(d) { return d.total; })]);
|
||||
|
||||
|
||||
//
|
||||
// Append chart elements
|
||||
//
|
||||
|
||||
// Append axes
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
svg.append("g")
|
||||
.attr("class", "d3-axis d3-axis-horizontal d3-axis-strong")
|
||||
.attr("transform", "translate(0," + height + ")")
|
||||
.call(xAxis);
|
||||
|
||||
// Vertical
|
||||
var verticalAxis = svg.append("g")
|
||||
.attr("class", "d3-axis d3-axis-vertical d3-axis-strong")
|
||||
.call(yAxis);
|
||||
|
||||
// Add text label
|
||||
verticalAxis.append("text")
|
||||
.attr("transform", "rotate(-90)")
|
||||
.attr("y", 10)
|
||||
.attr("dy", ".71em")
|
||||
.style("text-anchor", "end")
|
||||
.style("fill", "#999")
|
||||
.style("font-size", 12)
|
||||
.text("Population");
|
||||
|
||||
|
||||
// Add bars
|
||||
// ------------------------------
|
||||
|
||||
// Group values
|
||||
var state = svg.selectAll(".bar-group")
|
||||
.data(data)
|
||||
.enter()
|
||||
.append("g")
|
||||
.attr("class", "bar-group")
|
||||
.attr("transform", function(d) { return "translate(" + x(d.State) + ",0)"; });
|
||||
|
||||
// Append bars
|
||||
state.selectAll(".d3-bar")
|
||||
.data(function(d) { return d.ages; })
|
||||
.enter()
|
||||
.append("rect")
|
||||
.attr("class", "d3-bar")
|
||||
.attr("width", x.rangeBand())
|
||||
.attr("y", function(d) { return y(d.y1); })
|
||||
.attr("height", function(d) { return y(d.y0) - y(d.y1); })
|
||||
.style("fill", function(d) { return color(d.name); });
|
||||
|
||||
|
||||
// Add legend
|
||||
// ------------------------------
|
||||
|
||||
// Create legend
|
||||
var legend = svg.selectAll(".d3-legend")
|
||||
.data(color.domain().slice().reverse())
|
||||
.enter()
|
||||
.append("g")
|
||||
.attr("class", "d3-legend")
|
||||
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
|
||||
|
||||
// Legend indicator
|
||||
legend.append("rect")
|
||||
.attr("x", width - 18)
|
||||
.attr("width", 18)
|
||||
.attr("height", 18)
|
||||
.style("fill", color);
|
||||
|
||||
// Legend text
|
||||
legend.append("text")
|
||||
.attr("x", width - 24)
|
||||
.attr("y", 9)
|
||||
.attr("dy", ".35em")
|
||||
.style("text-anchor", "end")
|
||||
.text(function(d) { return d; });
|
||||
});
|
||||
|
||||
|
||||
|
||||
// Resize chart
|
||||
// ------------------------------
|
||||
|
||||
// Call function on window resize
|
||||
$(window).on('resize', resize);
|
||||
|
||||
// Call function on sidebar width change
|
||||
$('.sidebar-control').on('click', resize);
|
||||
|
||||
// Resize function
|
||||
//
|
||||
// Since D3 doesn't support SVG resize by default,
|
||||
// we need to manually specify parts of the graph that need to
|
||||
// be updated on window resize
|
||||
function resize() {
|
||||
|
||||
// Layout variables
|
||||
width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right;
|
||||
|
||||
|
||||
// Layout
|
||||
// -------------------------
|
||||
|
||||
// Main svg width
|
||||
container.attr("width", width + margin.left + margin.right);
|
||||
|
||||
// Width of appended group
|
||||
svg.attr("width", width + margin.left + margin.right);
|
||||
|
||||
|
||||
// Axes
|
||||
// -------------------------
|
||||
|
||||
// Horizontal ranges
|
||||
x.rangeRoundBands([0, width], .1, .5);
|
||||
|
||||
// Horizontal axis
|
||||
svg.selectAll('.d3-axis-horizontal').call(xAxis);
|
||||
|
||||
|
||||
// Chart elements
|
||||
// -------------------------
|
||||
|
||||
// Bar group
|
||||
svg.selectAll('.bar-group').attr("transform", function(d) { return "translate(" + x(d.State) + ",0)"; });
|
||||
|
||||
// Bars
|
||||
svg.selectAll('.d3-bar').attr("width", x.rangeBand()).attr("x", function(d) { return x(d.name); });
|
||||
|
||||
// Legend
|
||||
svg.selectAll(".d3-legend text").attr("x", width - 24);
|
||||
svg.selectAll(".d3-legend rect").attr("x", width - 18);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,231 @@
|
||||
/* ------------------------------------------------------------------------------
|
||||
*
|
||||
* # D3.js - normalized bar chart
|
||||
*
|
||||
* Demo d3.js normalized bar chart setup with .csv data source
|
||||
*
|
||||
* Version: 1.0
|
||||
* Latest update: August 1, 2015
|
||||
*
|
||||
* ---------------------------------------------------------------------------- */
|
||||
|
||||
$(function () {
|
||||
|
||||
// Initialize chart
|
||||
barNormalized('#d3-bar-normalized', 400);
|
||||
|
||||
// Chart setup
|
||||
function barNormalized(element, height) {
|
||||
|
||||
|
||||
// Basic setup
|
||||
// ------------------------------
|
||||
|
||||
// Define main variables
|
||||
var d3Container = d3.select(element),
|
||||
margin = {top: 5, right: 130, bottom: 20, left: 40},
|
||||
width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right,
|
||||
height = height - margin.top - margin.bottom - 5;
|
||||
|
||||
|
||||
|
||||
// Construct scales
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
var x = d3.scale.ordinal()
|
||||
.rangeRoundBands([0, width], .1);
|
||||
|
||||
// Vertical
|
||||
var y = d3.scale.linear()
|
||||
.rangeRound([height, 0]);
|
||||
|
||||
// Color
|
||||
var color = d3.scale.ordinal()
|
||||
.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);
|
||||
|
||||
|
||||
|
||||
// Create axes
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
var xAxis = d3.svg.axis()
|
||||
.scale(x)
|
||||
.orient("bottom");
|
||||
|
||||
// Vertical
|
||||
var yAxis = d3.svg.axis()
|
||||
.scale(y)
|
||||
.orient("left")
|
||||
.tickFormat(d3.format(".0%"));
|
||||
|
||||
|
||||
|
||||
// Create chart
|
||||
// ------------------------------
|
||||
|
||||
// Add SVG element
|
||||
var container = d3Container.append("svg");
|
||||
|
||||
// Add SVG group
|
||||
var svg = container
|
||||
.attr("width", width + margin.left + margin.right)
|
||||
.attr("height", height + margin.top + margin.bottom)
|
||||
.append("g")
|
||||
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
|
||||
|
||||
|
||||
|
||||
// Load data
|
||||
// ------------------------------
|
||||
|
||||
d3.csv("assets/demo_data/d3/bars/bars_stacked.csv", function(error, data) {
|
||||
|
||||
// Filter values by key
|
||||
color.domain(d3.keys(data[0]).filter(function(key) { return key !== "State"; }));
|
||||
|
||||
// Pull out values
|
||||
data.forEach(function(d) {
|
||||
var y0 = 0;
|
||||
d.ages = color.domain().map(function(name) { return {name: name, y0: y0, y1: y0 += +d[name]}; });
|
||||
d.ages.forEach(function(d) { d.y0 /= y0; d.y1 /= y0; });
|
||||
});
|
||||
|
||||
// Sort data
|
||||
data.sort(function(a, b) { return b.ages[0].y1 - a.ages[0].y1; });
|
||||
|
||||
|
||||
// Set input domains
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
x.domain(data.map(function(d) { return d.State; }));
|
||||
|
||||
|
||||
|
||||
//
|
||||
// Append chart elements
|
||||
//
|
||||
|
||||
// Append axes
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
svg.append("g")
|
||||
.attr("class", "d3-axis d3-axis-horizontal d3-axis-strong")
|
||||
.attr("transform", "translate(0," + height + ")")
|
||||
.call(xAxis);
|
||||
|
||||
// Vertical
|
||||
svg.append("g")
|
||||
.attr("class", "d3-axis d3-axis-vertical d3-axis-strong")
|
||||
.call(yAxis);
|
||||
|
||||
|
||||
|
||||
// Add bars
|
||||
// ------------------------------
|
||||
|
||||
// Group values
|
||||
var state = svg.selectAll(".bar-group")
|
||||
.data(data)
|
||||
.enter()
|
||||
.append("g")
|
||||
.attr("class", "bar-group")
|
||||
.attr("transform", function(d) { return "translate(" + x(d.State) + ",0)"; });
|
||||
|
||||
// Append bars
|
||||
state.selectAll(".d3-bar")
|
||||
.data(function(d) { return d.ages; })
|
||||
.enter()
|
||||
.append("rect")
|
||||
.attr("class", "d3-bar")
|
||||
.attr("width", x.rangeBand())
|
||||
.attr("y", function(d) { return y(d.y1); })
|
||||
.attr("height", function(d) { return y(d.y0) - y(d.y1); })
|
||||
.style("fill", function(d) { return color(d.name); });
|
||||
|
||||
|
||||
|
||||
// Add legend
|
||||
// ------------------------------
|
||||
|
||||
// Create legend
|
||||
var legend = svg.select(".bar-group:last-child")
|
||||
.selectAll(".d3-legend")
|
||||
.data(function(d) { return d.ages; })
|
||||
.enter().append("g")
|
||||
.attr("class", "d3-legend")
|
||||
.attr("transform", function(d) { return "translate(" + x.rangeBand() + "," + y((d.y0 + d.y1) / 2) + ")"; });
|
||||
|
||||
// Legend line
|
||||
legend.append("line")
|
||||
.attr("x2", 10)
|
||||
.attr("stroke", "#333")
|
||||
.attr("shape-rendering", "crispEdges");
|
||||
|
||||
// Legend text
|
||||
legend.append("text")
|
||||
.attr("x", 15)
|
||||
.attr("dy", ".35em")
|
||||
.text(function(d) { return d.name; });
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
// Resize chart
|
||||
// ------------------------------
|
||||
|
||||
// Call function on window resize
|
||||
$(window).on('resize', resize);
|
||||
|
||||
// Call function on sidebar width change
|
||||
$('.sidebar-control').on('click', resize);
|
||||
|
||||
// Resize function
|
||||
//
|
||||
// Since D3 doesn't support SVG resize by default,
|
||||
// we need to manually specify parts of the graph that need to
|
||||
// be updated on window resize
|
||||
function resize() {
|
||||
|
||||
// Layout variables
|
||||
width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right;
|
||||
|
||||
|
||||
// Layout
|
||||
// -------------------------
|
||||
|
||||
// Main svg width
|
||||
container.attr("width", width + margin.left + margin.right);
|
||||
|
||||
// Width of appended group
|
||||
svg.attr("width", width + margin.left + margin.right);
|
||||
|
||||
|
||||
// Axes
|
||||
// -------------------------
|
||||
|
||||
// Horizontal ranges
|
||||
x.rangeRoundBands([0, width], .1);
|
||||
|
||||
// Horizontal axis
|
||||
svg.selectAll('.d3-axis-horizontal').call(xAxis);
|
||||
|
||||
|
||||
// Chart elements
|
||||
// -------------------------
|
||||
|
||||
// Bar group
|
||||
svg.selectAll('.bar-group').attr("transform", function(d) { return "translate(" + x(d.State) + ",0)"; });
|
||||
|
||||
// Bars
|
||||
svg.selectAll('.d3-bar').attr("width", x.rangeBand())
|
||||
|
||||
// Legend
|
||||
svg.selectAll(".d3-legend").attr("transform", function(d) { return "translate(" + x.rangeBand() + "," + y((d.y0 + d.y1) / 2) + ")"; });
|
||||
}
|
||||
}
|
||||
});
|
||||
+209
@@ -0,0 +1,209 @@
|
||||
/* ------------------------------------------------------------------------------
|
||||
*
|
||||
* # D3.js - bar chart with tooltip
|
||||
*
|
||||
* Demo d3.js bar chart setup with tooltip and .tsv data source
|
||||
*
|
||||
* Version: 1.0
|
||||
* Latest update: August 1, 2015
|
||||
*
|
||||
* ---------------------------------------------------------------------------- */
|
||||
|
||||
$(function () {
|
||||
|
||||
// Initialize chart
|
||||
barTooltip('#d3-bar-tooltip', 400);
|
||||
|
||||
// Chart setup
|
||||
function barTooltip(element, height) {
|
||||
|
||||
|
||||
// Basic setup
|
||||
// ------------------------------
|
||||
|
||||
// Define main variables
|
||||
var d3Container = d3.select(element),
|
||||
margin = {top: 5, right: 10, bottom: 20, left: 40},
|
||||
width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right,
|
||||
height = height - margin.top - margin.bottom - 5;
|
||||
|
||||
|
||||
|
||||
// Construct scales
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
var x = d3.scale.ordinal()
|
||||
.rangeRoundBands([0, width], .1, .5);
|
||||
|
||||
// Vertical
|
||||
var y = d3.scale.linear()
|
||||
.range([height, 0]);
|
||||
|
||||
// Color
|
||||
var color = d3.scale.category20c();
|
||||
|
||||
|
||||
|
||||
// Create axes
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
var xAxis = d3.svg.axis()
|
||||
.scale(x)
|
||||
.orient("bottom");
|
||||
|
||||
// Vertical
|
||||
var yAxis = d3.svg.axis()
|
||||
.scale(y)
|
||||
.orient("left")
|
||||
.ticks(10, "%");
|
||||
|
||||
|
||||
|
||||
// Create chart
|
||||
// ------------------------------
|
||||
|
||||
// Add SVG element
|
||||
var container = d3Container.append("svg");
|
||||
|
||||
// Add SVG group
|
||||
var svg = container
|
||||
.attr("width", width + margin.left + margin.right)
|
||||
.attr("height", height + margin.top + margin.bottom)
|
||||
.append("g")
|
||||
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
|
||||
|
||||
|
||||
|
||||
// Create tooltip
|
||||
// ------------------------------
|
||||
|
||||
// Create tooltip
|
||||
var tip = d3.tip()
|
||||
.attr('class', 'd3-tip')
|
||||
.offset([-10, 0])
|
||||
.html(function(d) {
|
||||
return d.frequency;
|
||||
});
|
||||
|
||||
// Initialize tooltip
|
||||
svg.call(tip);
|
||||
|
||||
|
||||
|
||||
// Load data
|
||||
// ------------------------------
|
||||
|
||||
d3.tsv("assets/demo_data/d3/bars/bars_tooltip.tsv", function(error, data) {
|
||||
|
||||
// Pull out values
|
||||
data.forEach(function(d) {
|
||||
d.frequency = +d.frequency;
|
||||
});
|
||||
|
||||
|
||||
// Set input domains
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
x.domain(data.map(function(d) { return d.letter; }));
|
||||
|
||||
// Vertical
|
||||
y.domain([0, d3.max(data, function(d) { return d.frequency; })]);
|
||||
|
||||
|
||||
//
|
||||
// Append chart elements
|
||||
//
|
||||
|
||||
// Append axes
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
svg.append("g")
|
||||
.attr("class", "d3-axis d3-axis-horizontal d3-axis-strong")
|
||||
.attr("transform", "translate(0," + height + ")")
|
||||
.call(xAxis);
|
||||
|
||||
// Vertical
|
||||
var verticalAxis = svg.append("g")
|
||||
.attr("class", "d3-axis d3-axis-vertical d3-axis-strong")
|
||||
.call(yAxis);
|
||||
|
||||
// Add text label
|
||||
verticalAxis.append("text")
|
||||
.attr("transform", "rotate(-90)")
|
||||
.attr("y", 10)
|
||||
.attr("dy", ".71em")
|
||||
.style("text-anchor", "end")
|
||||
.style("fill", "#999")
|
||||
.style("font-size", 12)
|
||||
.text("Frequency");
|
||||
|
||||
|
||||
// Append bars
|
||||
svg.selectAll(".d3-bar")
|
||||
.data(data)
|
||||
.enter()
|
||||
.append("rect")
|
||||
.attr("class", "d3-bar")
|
||||
.style("fill", function(d) { return color(d.letter); })
|
||||
.attr("x", function(d) { return x(d.letter); })
|
||||
.attr("width", x.rangeBand())
|
||||
.attr("y", function(d) { return y(d.frequency); })
|
||||
.attr("height", function(d) { return height - y(d.frequency); })
|
||||
.on('mouseover', tip.attr('class', 'tooltip-inner in').show)
|
||||
.on('mouseout', tip.hide);
|
||||
});
|
||||
|
||||
|
||||
|
||||
// Resize chart
|
||||
// ------------------------------
|
||||
|
||||
// Call function on window resize
|
||||
$(window).on('resize', resize);
|
||||
|
||||
// Call function on sidebar width change
|
||||
$('.sidebar-control').on('click', resize);
|
||||
|
||||
// Resize function
|
||||
//
|
||||
// Since D3 doesn't support SVG resize by default,
|
||||
// we need to manually specify parts of the graph that need to
|
||||
// be updated on window resize
|
||||
function resize() {
|
||||
|
||||
// Layout variables
|
||||
width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right;
|
||||
|
||||
|
||||
// Layout
|
||||
// -------------------------
|
||||
|
||||
// Main svg width
|
||||
container.attr("width", width + margin.left + margin.right);
|
||||
|
||||
// Width of appended group
|
||||
svg.attr("width", width + margin.left + margin.right);
|
||||
|
||||
|
||||
// Axes
|
||||
// -------------------------
|
||||
|
||||
// Horizontal range
|
||||
x.rangeRoundBands([0, width], .1, .5);
|
||||
|
||||
// Horizontal axis
|
||||
svg.selectAll('.d3-axis-horizontal').call(xAxis);
|
||||
|
||||
|
||||
// Chart elements
|
||||
// -------------------------
|
||||
|
||||
// Bars
|
||||
svg.selectAll('.d3-bar').attr("x", function(d) { return x(d.letter); }).attr("width", x.rangeBand());
|
||||
}
|
||||
}
|
||||
});
|
||||
+191
@@ -0,0 +1,191 @@
|
||||
/* ------------------------------------------------------------------------------
|
||||
*
|
||||
* # D3.js - vertical bar chart
|
||||
*
|
||||
* Demo d3.js vertical bar chart setup with .tsv data source
|
||||
*
|
||||
* Version: 1.0
|
||||
* Latest update: August 1, 2015
|
||||
*
|
||||
* ---------------------------------------------------------------------------- */
|
||||
|
||||
$(function () {
|
||||
|
||||
// Initialize chart
|
||||
barVertical('#d3-bar-vertical', 400);
|
||||
|
||||
// Chart setup
|
||||
function barVertical(element, height) {
|
||||
|
||||
|
||||
// Basic setup
|
||||
// ------------------------------
|
||||
|
||||
// Define main variables
|
||||
var d3Container = d3.select(element),
|
||||
margin = {top: 5, right: 10, bottom: 20, left: 40},
|
||||
width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right,
|
||||
height = height - margin.top - margin.bottom - 5;
|
||||
|
||||
|
||||
|
||||
// Construct scales
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
var x = d3.scale.ordinal()
|
||||
.rangeRoundBands([0, width], .1, .5);
|
||||
|
||||
// Vertical
|
||||
var y = d3.scale.linear()
|
||||
.range([height, 0]);
|
||||
|
||||
// Color
|
||||
var color = d3.scale.category20c();
|
||||
|
||||
|
||||
|
||||
// Create axes
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
var xAxis = d3.svg.axis()
|
||||
.scale(x)
|
||||
.orient("bottom");
|
||||
|
||||
// Vertical
|
||||
var yAxis = d3.svg.axis()
|
||||
.scale(y)
|
||||
.orient("left")
|
||||
.ticks(10, "%");
|
||||
|
||||
|
||||
|
||||
// Create chart
|
||||
// ------------------------------
|
||||
|
||||
// Add SVG element
|
||||
var container = d3Container.append("svg");
|
||||
|
||||
// Add SVG group
|
||||
var svg = container
|
||||
.attr("width", width + margin.left + margin.right)
|
||||
.attr("height", height + margin.top + margin.bottom)
|
||||
.append("g")
|
||||
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
|
||||
|
||||
|
||||
|
||||
// Load data
|
||||
// ------------------------------
|
||||
|
||||
d3.tsv("assets/demo_data/d3/bars/bars_basic.tsv", function(error, data) {
|
||||
|
||||
// Pull out values
|
||||
data.forEach(function(d) {
|
||||
d.frequency = +d.frequency;
|
||||
});
|
||||
|
||||
|
||||
// Set input domains
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
x.domain(data.map(function(d) { return d.letter; }));
|
||||
|
||||
// Vertical
|
||||
y.domain([0, d3.max(data, function(d) { return d.frequency; })]);
|
||||
|
||||
|
||||
//
|
||||
// Append chart elements
|
||||
//
|
||||
|
||||
// Append axes
|
||||
// ------------------------------
|
||||
|
||||
// Horizontal
|
||||
svg.append("g")
|
||||
.attr("class", "d3-axis d3-axis-horizontal d3-axis-strong")
|
||||
.attr("transform", "translate(0," + height + ")")
|
||||
.call(xAxis);
|
||||
|
||||
// Vertical
|
||||
var verticalAxis = svg.append("g")
|
||||
.attr("class", "d3-axis d3-axis-vertical d3-axis-strong")
|
||||
.call(yAxis);
|
||||
|
||||
// Add text label
|
||||
verticalAxis.append("text")
|
||||
.attr("transform", "rotate(-90)")
|
||||
.attr("y", 10)
|
||||
.attr("dy", ".71em")
|
||||
.style("text-anchor", "end")
|
||||
.style("fill", "#999")
|
||||
.style("font-size", 12)
|
||||
.text("Frequency");
|
||||
|
||||
|
||||
// Add bars
|
||||
svg.selectAll(".d3-bar")
|
||||
.data(data)
|
||||
.enter()
|
||||
.append("rect")
|
||||
.attr("class", "d3-bar")
|
||||
.attr("x", function(d) { return x(d.letter); })
|
||||
.attr("width", x.rangeBand())
|
||||
.attr("y", function(d) { return y(d.frequency); })
|
||||
.attr("height", function(d) { return height - y(d.frequency); })
|
||||
.style("fill", function(d) { return color(d.letter); });
|
||||
});
|
||||
|
||||
|
||||
|
||||
// Resize chart
|
||||
// ------------------------------
|
||||
|
||||
// Call function on window resize
|
||||
$(window).on('resize', resize);
|
||||
|
||||
// Call function on sidebar width change
|
||||
$('.sidebar-control').on('click', resize);
|
||||
|
||||
// Resize function
|
||||
//
|
||||
// Since D3 doesn't support SVG resize by default,
|
||||
// we need to manually specify parts of the graph that need to
|
||||
// be updated on window resize
|
||||
function resize() {
|
||||
|
||||
// Layout variables
|
||||
width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right;
|
||||
|
||||
|
||||
// Layout
|
||||
// -------------------------
|
||||
|
||||
// Main svg width
|
||||
container.attr("width", width + margin.left + margin.right);
|
||||
|
||||
// Width of appended group
|
||||
svg.attr("width", width + margin.left + margin.right);
|
||||
|
||||
|
||||
// Axes
|
||||
// -------------------------
|
||||
|
||||
// Horizontal range
|
||||
x.rangeRoundBands([0, width], .1, .5);
|
||||
|
||||
// Horizontal axis
|
||||
svg.selectAll('.d3-axis-horizontal').call(xAxis);
|
||||
|
||||
|
||||
// Chart elements
|
||||
// -------------------------
|
||||
|
||||
// Line path
|
||||
svg.selectAll('.d3-bar').attr("x", function(d) { return x(d.letter); }).attr("width", x.rangeBand());
|
||||
}
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user