Published
Edited
Jul 4, 2020
3 forks
3 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
margin = ({top: 20, right: 30, bottom: 30, left: 50})
Insert cell
width = 960 - margin.left - margin.right
Insert cell
height = 500 - margin.top - margin.bottom
Insert cell
padding = 0.3
Insert cell
Insert cell
parms = {
var xh = 10
var xw = 10 }
Insert cell
Insert cell
Insert cell
Insert cell
data.map(function(d) { return d.name; })
Insert cell
Insert cell
x = d3.scaleBand() // was scale.ordinal()
.domain(data.map(function(d) { return d.name; }))
.range([0,width])
.padding(padding)
.round(true)
Insert cell
Insert cell
Insert cell
x.domain()
Insert cell
Insert cell
d3.max(data, function(d) { return d.end; })
Insert cell
y = d3.scaleLinear() // was scale.linear()
.domain([0, d3.max(data, function(d) { return d.end; })*1.1])
.range([height, 0])
Insert cell
y.domain()
Insert cell
xAxis = d3.axisBottom()
.scale(x)
Insert cell
yAxis = d3.axisRight()
.scale(y)
.tickFormat(function(d) { return dollarFormatter(d); })
Insert cell
Insert cell
{const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, 50]);
svg.append("g")
.call(xAxis);
return svg.node();}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
d3 = require("d3@5")
Insert cell
Insert cell
function dollarFormatter(n) {
n = Math.round(n);
var result = n;
if (Math.abs(n) >= 1000000000) {
result = Math.round(n/1000000000) + 'B';
} else if (Math.abs(n) >= 1000000) {
result = Math.round(n/1000000) + 'M';
} else if (Math.abs(n) >= 1000) {
result = Math.round(n/1000) + 'K';
}
return result;
}
Insert cell
Insert cell
dollarFormatter(1000)
Insert cell
Insert cell
html`<style>

.bar.total rect {
fill: steelblue;
}

.bar.positive rect {
fill: darkolivegreen;
}
.bar.negative rect {
fill: crimson;
}

.bar line.connector {
stroke: grey;
stroke-dasharray: 3 1;
}

.bar text {
fill: black;
font: 12px sans-serif;
text-anchor: middle;
}

.axis text {
font: 10px sans-serif;
}

.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}

</style>`
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more