Skip to content

Commit

Permalink
Add bullet example. Update to 1.9.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Apr 9, 2011
1 parent 1147061 commit 68c9bdb
Show file tree
Hide file tree
Showing 36 changed files with 495 additions and 191 deletions.
23 changes: 21 additions & 2 deletions _includes/Makefile
@@ -1,6 +1,8 @@
SRC_FILES = \
../d3.js \
../d3.min.js \
../d3.chart.js \
../d3.chart.min.js \
../d3.csv.js \
../d3.csv.min.js \
../d3.geo.js \
Expand Down Expand Up @@ -45,7 +47,10 @@ EX_FILES = \
../ex/stack.js \
../ex/voronoi.css \
../ex/voronoi.js \
../ex/sunburst.js
../ex/sunburst.js \
../ex/bullet.js \
../ex/bullet.css \
../ex/bullets.json

INCLUDE_FILES = \
cartogram.js \
Expand All @@ -60,7 +65,9 @@ INCLUDE_FILES = \
stream_layers.js \
stack.js \
voronoi.js \
sunburst.js
sunburst.js \
bullet.js \
bullets.json

.PHONY all: $(SRC_FILES) $(EX_FILES) $(INCLUDE_FILES)

Expand All @@ -76,6 +83,18 @@ $(SRC_FILES):
@rm -rf $@
git cat-file blob master:examples/calendar/calendar.css > $@

../ex/bullet.js bullet.js:
@rm -rf $@
git cat-file blob master:examples/bullet/bullet.js > $@

../ex/bullet.css:
@rm -rf $@
git cat-file blob master:examples/bullet/bullet.css > $@

../ex/bullets.json bullets.json:
@rm -rf $@
git cat-file blob master:examples/bullet/bullets.json > $@

../ex/cartogram.css:
@rm -rf $@
git cat-file blob master:examples/cartogram/cartogram.css > $@
Expand Down
53 changes: 53 additions & 0 deletions _includes/bullet.js
@@ -0,0 +1,53 @@
var w = 960,
h = 50,
m = [5, 40, 20, 120]; // top right bottom left

var chart = d3.chart.bullet()
.width(w - m[1] - m[3])
.height(h - m[0] - m[2])
.duration(1000);

d3.json("bullets.json", function(data) {

var vis = d3.select("#chart").selectAll("svg")
.data(data)
.enter().append("svg:svg")
.attr("class", "bullet")
.attr("width", w)
.attr("height", h)
.append("svg:g")
.attr("transform", "translate(" + m[3] + "," + m[0] + ")")
.call(chart);

var title = vis.append("svg:g")
.attr("text-anchor", "end")
.attr("transform", "translate(-6," + (h - m[0] - m[2]) / 2 + ")");

title.append("svg:text")
.attr("class", "title")
.text(function(d) { return d.title; });

title.append("svg:text")
.attr("class", "subtitle")
.attr("dy", "1em")
.text(function(d) { return d.subtitle; });

window.transition = function() {
vis.map(randomize).call(chart);
};
});

function randomize(d) {
if (!d.randomizer) d.randomizer = randomizer(d);
d.ranges = d.ranges.map(d.randomizer);
d.markers = d.markers.map(d.randomizer);
d.measures = d.measures.map(d.randomizer);
return d;
}

function randomizer(d) {
var k = d3.max(d.ranges) * .2;
return function(d) {
return Math.max(0, d + k * (Math.random() - .5));
};
}
7 changes: 7 additions & 0 deletions _includes/bullets.json
@@ -0,0 +1,7 @@
[
{"title":"Revenue","subtitle":"US$, in thousands","ranges":[150,225,300],"measures":[220,270],"markers":[250]},
{"title":"Profit","subtitle":"%","ranges":[20,25,30],"measures":[21,23],"markers":[26]},
{"title":"Order Size","subtitle":"US$, average","ranges":[350,500,600],"measures":[100,320],"markers":[550]},
{"title":"New Customers","subtitle":"count","ranges":[1400,2000,2500],"measures":[1000,1650],"markers":[2100]},
{"title":"Satisfaction","subtitle":"out of 5","ranges":[3.5,4.25,5],"measures":[3.2,4.7],"markers":[4.4]}
]
20 changes: 11 additions & 9 deletions _includes/cartogram.js
@@ -1,17 +1,17 @@
var data; // loaded asynchronously
// Ratio of Obese (BMI >= 30) in U.S. Adults, CDC 2008
var data = [
, .187, .198, , .133, .175, .151, , .1, .125, .171, , .172, .133, , .108,
.142, .167, .201, .175, .159, .169, .177, .141, .163, .117, .182, .153, .195,
.189, .134, .163, .133, .151, .145, .13, .139, .169, .164, .175, .135, .152,
.169, , .132, .167, .139, .184, .159, .14, .146, .157, , .139, .183, .16, .143
];

var svg = d3.select("#chart")
.append("svg:svg");

d3.json("us-states.json", function(json) {
var path = d3.geo.path();

// Synthesize a random data variable to visualize…
// Note: This has a baked-in sqrt transform!
json.features.forEach(function(f) {
f.properties.value = Math.sqrt(.2 + .8 * Math.random());
});

// A thick black stroke for the exterior.
svg.append("svg:g")
.attr("class", "black")
Expand Down Expand Up @@ -39,10 +39,12 @@ d3.json("us-states.json", function(json) {
x = centroid[0],
y = centroid[1];
return "translate(" + x + "," + y + ")"
+ "scale(" + d.properties.value + ")"
+ "scale(" + Math.sqrt(data[+d.id] * 5 || 0) + ")"
+ "translate(" + -x + "," + -y + ")";
})
.attr("stroke-width", function(d) { return 1 / d.properties.value; })
.attr("stroke-width", function(d) {
return 1 / Math.sqrt(data[+d.id] * 5);
})
.attr("d", path);

});
1 change: 0 additions & 1 deletion _includes/treemap.js
Expand Up @@ -45,7 +45,6 @@ d3.json("flare.json", function(json) {
});
});


function cell() {
this
.style("left", function(d) { return d.x + "px"; })
Expand Down
2 changes: 1 addition & 1 deletion _layouts/api.html
Expand Up @@ -3,7 +3,7 @@
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>d3.js{% if page.title %} ~ {{ page.title }}{% endif %}</title>
<script type="text/javascript" src="../d3.js?1.8.3"></script>
<script type="text/javascript" src="../d3.js?1.9.0"></script>
<style type="text/css">

@import url("../style.css?1.6.0");
Expand Down
2 changes: 1 addition & 1 deletion _layouts/default.html
Expand Up @@ -3,7 +3,7 @@
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>d3.js{% if page.title %} ~ {{ page.title }}{% endif %}</title>
<script type="text/javascript" src="d3.js?1.8.3"></script>
<script type="text/javascript" src="d3.js?1.9.0"></script>
<style type="text/css">

@import url("style.css?1.6.0");
Expand Down
2 changes: 1 addition & 1 deletion _layouts/ex.html
Expand Up @@ -3,7 +3,7 @@
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>d3.js{% if page.title %} ~ {{ page.title }}{% endif %}</title>
<script type="text/javascript" src="../d3.js?1.8.3"></script>
<script type="text/javascript" src="../d3.js?1.9.0"></script>
<style type="text/css">

@import url("../style.css?1.6.0");
Expand Down
2 changes: 1 addition & 1 deletion _layouts/tutorial.html
Expand Up @@ -3,7 +3,7 @@
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>d3.js{% if page.title %} ~ {{ page.title }}{% endif %}</title>
<script type="text/javascript" src="../d3.js?1.8.3"></script>
<script type="text/javascript" src="../d3.js?1.9.0"></script>
<style type="text/css">

@import url("../style.css?1.6.0");
Expand Down
Binary file added bullet.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 68c9bdb

Please sign in to comment.