Skip to content

Instantly share code, notes, and snippets.

@darshit-shah
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darshit-shah/8dcadf25fae40257b0a9 to your computer and use it in GitHub Desktop.
Save darshit-shah/8dcadf25fae40257b0a9 to your computer and use it in GitHub Desktop.
Bar Chart - Floating Scale

For reference, I have taken sample code from Mike Bostock's Bar Chart Example. At all places I have commented original code and replaced it with new code to convert given example with Floating Scale.

letter frequency
A 0.082
B 0.015
C 0.028
D 0.043
E 0.127
F 0.023
G 0.02
H 0.061
I 0.07
J 0.002
K 0.008
L 1
M 1.05
N 0.067
O 0.075
P 0.019
Q 1.1
R 0.06
S 1.05
T 0.091
U 0.028
V 0.01
W 0.024
X 0.002
Y 0.02
Z 0.001
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body
{
font: sans-serif;
}
.bar
{
fill: steelblue;
}
.bar:hover
{
fill: brown;
}
.axis
{
font: 10px sans-serif;
}
.axis path, .axis line
{
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path
{
display: none;
}
.floating
{
stroke: #FF0000;
}
.floatingLabel
{
fill: #FF0000;
}
</style>
<body>
<div style="padding: 10px;">
<label>
Add new <b><i>Floating</i></b> axis line <i>(Value in %)</i></label>
<input id="myValue" type="text" value="12" />
<input type="button" value="Add" onclick="updateValues()" />
</div>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="https://darshit-shah.github.io/d3.floatingScale.js"></script>
<script>
var margin = { top: 20, right: 50, bottom: 30, left: 40 },
width = 960 - margin.left - margin.right,
height = 450 - margin.top - margin.bottom;
var x = d3.scale.ordinal().rangeRoundBands([0, width], .1);
//Replaced
//var y = d3.scale.linear().range([height, 0]);
var y = d3.svg.floatingScale().range([height, 0]).ticks(5, "%");
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
//Replaced
//var yAxis = d3.svg.axis().scale(y).orient("left");
var yAxis = y.axis().orient("left");
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var data = null;
var min = Infinity;
var max = -Infinity;
d3.csv("data.csv", type, function (error, file_data) {
data = file_data;
file_data = null;
x.domain(data.map(function (d) { return d.letter; }));
y.domain([0, d3.max(data, function (d) { return d.frequency; })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Frequency");
var bars = svg.selectAll(".bar")
.data(data);
bars.enter().append("rect")
.attr("class", "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); });
bars.exit().remove();
//added extra
y.updateChart(updateChart).chart(svg);
});
function updateChart(delay, duration) {
//added extra
svg.selectAll(".bar").transition().delay(delay).duration(duration).attr("y", function (d) {
return y(d.frequency);
})
.attr("height", function (d) {
return height - y(d.frequency);
});
svg.selectAll(".y.axis").transition().delay(delay).duration(duration).call(yAxis);
}
function type(d) {
d.frequency = +d.frequency;
return d;
}
function updateValues() {
var value = parseFloat(document.querySelector('#myValue').value.trim()) / 100;
y.addFloatingScaleLine(value);
}
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment