JSFiddle - React, Tailwind, and code Playground
by Maria Karanasou
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.js"></script>
<script src="https://bl.ocks.org/syntagmatic/raw/3341641/render-queue.js"></script>
<div id="chart"></div>
CSS
body {
background-color: #F1F3F3;
}
text {
font-family: 'Open Sans', sans-serif;
font-size: 10px;
font-weight: 900;
pointer-events: none;
}
circle {
cursor: pointer;
fill-opacity: 0.9;
}
.axis path,
.axis line {
fill: none;
stroke: coral;
/*shape-rendering: crispEdges;*/
}
.x.axis path,
.x.axis line {
display: none;
}
.y.axis path,
.y.axis line {
display: none;
}
path{
fill:lightcoral;
}
JavaScript
var path = null;
var svg = null;
var x = null;
var y = null;
var valueline = null;
var c = 10;
var xAxis = null;
var yAxis = null;
function lineChart(data, id){
// Set the dimensions of the canvas / graph
var margin = {top: 30, right: 20, bottom: 30, left: 50},
width = 1000 - margin.left - margin.right,
height = 370 - margin.top - margin.bottom;
// Parse the date / time
var parseDate = d3.time.format("%d-%b-%y").parse;
// Define the div for the tooltip
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
// Set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
// Define the axes
xAxis = d3.svg.axis().scale(x)
.orient("bottom").ticks(5);
yAxis = d3.svg.axis().scale(y)
.orient("left").ticks(5);
// Define the line
valueline = d3.svg.line()
.x(function(d) { return x(d.time); })
.y(function(d) { return y(d.loadaverage); })
// Adds the svg canvas
svg = d3.select(id)
.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 + ")");
// Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.time; }));
y.domain([0, d3.max(data, function(d) { return d.loadaverage; })]);
// Add the valueline path.
path = svg.selectAll('path')
.data(data) // pos??
.enter()
.append("path")
path.attr("class", "line")
.attr("d", valueline(data))
.on("mouseover", function(d) {
div.transition()
.duration(200)
.style("opacity", .9);
div.html(d.time + "<br/>" + d.loadaverage)
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function(d) {
div.transition()
.duration(500)
.style("opacity", 0);
});
//...