Skip to content

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcw committed Jul 11, 2014
2 parents 915c70c + 435407b commit d3328e5
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
11 changes: 11 additions & 0 deletions examples/groupedbars.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
file,geojson,pbf,pbfpacked
test/data/feature.json,131,63,47
test/data/geometry.json,68,36,29
test/data/geometrycollection.json,271,129,68
test/data/linestring.json,175,100,55
test/data/multilinestring.json,212,150,76
test/data/multipoint.json,175,100,55
test/data/multipolygon.json,369,373,157
test/data/point.json,176,73,57
test/data/polygon.json,276,250,99
test/data/singlegeometrycollection.json,163,105,53
25 changes: 25 additions & 0 deletions examples/histogram.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
value
1
1
1
1
2
2
2
2
2
2
2
3
3
3
4
5
6
6
7
7
7
7
8
9
50 changes: 26 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env node

var concat = require('concat-stream'),
fs = require('fs'),
var fs = require('fs'),
GitHubApi = require('github'),
github = new GitHubApi({
version: '3.0.0',
protocol: 'https'
}),
rw = require('rw'),
argv = require('minimist')(process.argv.slice(2), {
boolean: 'help'
}),
Expand All @@ -20,7 +20,11 @@ var typenames = charts.map(function(c) {
if (argv.help) {
console.log('usage: chartpipe < data');
console.log('usage: process | chartpipe');
console.log('usage: chartpipe file.csv');
console.log('usage: chartpipe --type=groupedbars file.csv');
console.log('usage: chartpipe --type=histogram file.csv');
console.log('available charts: ' + typenames);
return;
}

var type = argv.type || 'groupedbars';
Expand All @@ -29,25 +33,23 @@ if (charts.indexOf(type + '.html') === -1) {
throw new Error('chart type ' + type + ' not found, choices: ' + typenames);
}

console.log('Waiting for input...');

process.stdin.pipe(concat(function(data) {
github.gists.create({
description: '/dev/chartpipe',
public: true,
files: {
'data.csv': {
content: data.toString()
},
'index.html': {
content: fs.readFileSync(__dirname + '/templates/' + type + '.html', 'utf8')
}
}
}, function (err, res) {
if (err) {
console.error('Unable to create Gist:' + JSON.stringify(res));
} else {
open('http://bl.ocks.org/' + res.id);
}
});
}));
var input = argv._.length ?
fs.readFileSync(argv._[0], 'utf8') :
rw.readFileSync('/dev/stdin', 'utf8');

var indexhtml = fs.readFileSync(__dirname + '/templates/' + type + '.html', 'utf8');

github.gists.create({
description: '/dev/chartpipe',
public: true,
files: {
'data.csv': { content: input },
'index.html': { content: indexhtml }
}
}, function (err, res) {
if (err) {
console.error('Unable to create Gist:' + JSON.stringify(res));
} else {
open('http://bl.ocks.org/' + res.id);
}
});
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"dependencies": {
"github": "~0.2.1",
"open": "0.0.5",
"concat-stream": "~1.4.6",
"minimist": "~0.2.0"
"minimist": "~0.2.0",
"rw": "~0.1.1"
}
}
85 changes: 85 additions & 0 deletions templates/histogram.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<!DOCTYPE html>
<meta charset="utf-8">
<style>

body {
font: 10px sans-serif;
}

.bar {
fill: steelblue;
shape-rendering: crispEdges;
}

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

.y.axis path {
display: none;
}

</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>

var margin = {top: 20, right: 30, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;

var x = d3.scale.linear()
.domain([0, 10])
.range([0, width]);

var y = d3.scale.linear()
.domain([0, 10])
.range([height, 0]);

var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");

var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(d3.format("2r"));

var histogram = d3.layout.histogram()
.bins(x.ticks(10));

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 + ")");

svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)

svg.append("g")
.attr("class", "y axis")
.call(yAxis);

d3.csv("data.csv", function(error, input) {

var dataset = input.map(function(d) { return d["value"]; });
var data = histogram(dataset);

svg.selectAll(".bar")
.data(data)
.enter().insert("rect", ".axis")
.attr("class", "bar")
.attr("x", function(d) { return x(d.x) + 1; })
.attr("y", function(d) { return y(d.y); })
.attr("width", x(data[0].dx + data[0].x) - x(data[0].x) - 1)
.attr("height", function(d) { return height - y(d.y); });

});

</script>

0 comments on commit d3328e5

Please sign in to comment.