JSFiddle - React, Tailwind, and code Playground
by eprouver
HTML
<svg id="test" width="960" height="500">
<defs id="mdef">
<pattern id="image" x="0" y="0" height="40" width="40">
<image x="0" y="0" width="40" height="40" xlink:href="http://www.e-pint.com/epint.jpg"></image>
</pattern>
</defs>
</svg>
CSS
body {
font: 10px sans-serif;
background: steelblue;
}
.axis path, .axis line {
fill: none;
stroke: #fff;
shape-rendering: crispEdges;
}
.tick {
fill: white;
}
.line {
fill: none;
stroke: white;
stroke-width: 5px;
}
.circle {
stroke: white;
stroke-width: 2px;
fill: none;
}
JavaScript
var margin = {
top: 50,
right: 50,
bottom: 50,
left: 50
},
width = window.innerWidth - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var parseDate = d3.time.format("%d-%b-%y").parse;
var x = d3.time.scale()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.ticks(d3.time.days)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var startline = d3.svg.line()
.x(function (d) {
return x(d.date);
})
.y(function (d) {
return y(0);
});
var line = d3.svg.line()
.x(function (d) {
return x(d.date);
})
.y(function (d) {
return y(d.close);
});
var svg = d3.select("#test")
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var data = [{
"date": "29-Apr-12",
"close": Math.random() * 300
}, {
"date": "28-Apr-12",
"close": Math.random() * 300
}, {
"date": "27-Apr-12",
"close": Math.random() * 300
}, {
"date": "26-Apr-12",
"close": Math.random() * 300
}, {
"date": "25-Apr-12",
"close": Math.random() * 300
}, {
"date": "24-Apr-12",
"close": Math.random() * 300
}, {
"date": "23-Apr-12",
"close": Math.random() * 300
}];
data.forEach(function (d) {
d.date = parseDate(d.date);
d.close = +d.close;
});
x.domain(d3.extent(data, function (d) {
return d.date;
}));
y.domain([0, 300]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", startline)
.transition()
.duration(800)
.attr("d", line);
svg.selectAll('circle')
.data(data)
.enter().append('circle')
.style("fill", "green")
.attr('class', 'circle')
.attr("cx", function (d, i) {
...