JSFiddle - React, Tailwind, and code Playground
by mjmitche
HTML
<div id="graph">
</div>
CSS
#csvdata {
display: none;
}
#graph{
font: 12px Arial;
width:800px;
height:260px;
}
path {
stroke: steelblue;
stroke-width: 2;
fill: none;
}
.axis path,
.axis line {
fill: none;
stroke: grey;
stroke-width: 1;
shape-rendering: crispEdges;
}
JavaScript
(function() {
// Set the dimensions of the canvas / graph
var margin = {top: 30, right: 100, bottom: 30, left: 100},
width = 800 - margin.left - margin.right,
height = 270 - margin.top - margin.bottom;
// Parse the date / time
var parseDate = d3.time.format("%H:%M").parse,
formatDate = d3.time.format("%H:%M"),
bisectDate = d3.bisector(function(d) { return d.zeit; }).left;
// Set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
// Define the axes
var xAxis = d3.svg.axis().scale(x)
.orient("bottom").ticks(12);
var yAxis = d3.svg.axis().scale(y)
.orient("left").ticks(12);
// Define the line
var valueline = d3.svg.line()
.x(function(d) { return x(d.zeit); })
.y(function(d) { return y(d.count); });
// Adds the svg canvas
var svg = d3.select("#graph")
.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 lineSvg = svg.append("g");
var focus = svg.append("g")
.style("display", "none");
d3.csv("https://dl.dropboxusercontent.com/u/10328969/report.csv", function(error, data) {
// d3.csv.parse(raw, function(error, data) {
alert("data", data);
data.forEach(function(d) {
// console.log("data in raw", d);
d.zeit = parseDate(d.zeit);
d.total = +d.total;
d.count = d.count;
});
// Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.zeit; }));
y.domain([0, d3.max(data, function(d) { return d.count; })]);
// Add the valueline path.
lineSvg.append("path")
.attr("class", "line")
.attr("d", valueline(data));
// Add the X Axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Add...