JSFiddle - React, Tailwind, and code Playground
by mirko89
HTML
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="https://rawgit.com/Caged/d3-tip/master/index.js">
</script>
<body style="height:100%">
<h1 id="Title">Enterprise web sites growth from 2003 to 2010</h1>
<div id="Command-Area"></div>
<div id="tooltip" class="hidden"></div>
<div id="MainVisualization"></div>
<div id="LineChart"></div>
CSS
#MainVisualization {
float:left;
/*min-width:1024px;*/
width:50%;
margin-left:0.5%;
margin-right:0.5%;
margin-top:0.5%;
min-height:250px;
height:40%;
background-color: white;
}
#Command-Area {
width:100%;
height:2%;
background-color: green;
opacity:0.7;
}
#Title {
margin: 0;
margin-bottom: 0.5%;
text-align: center;
font-family: sans-serif;
font-size: 250%;
color: goldenrod;
}
.line {
fill:none;
stroke: steelblue;
stroke-width:1.5px;
}
#tooltip {
position: fixed;
width: 100px;
height: auto;
padding: 10px;
background-color: orange;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
-webkit-box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
-moz-box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
pointer-events: none;
}
#tooltip.hidden {
display: none;
}
#tooltip p {
margin: 0;
font-family: sans-serif;
font-size: 16px;
line-height: 20px;
}
.d3-tip {
line-height: 1;
font-weight: bold;
padding: 12px;
background: rgba(0, 0, 0, 0.8);
color: #fff;
border-radius: 2px;
}
.d3-tip:after {
box-sizing: border-box;
display: inline;
font-size: 10px;
width: 100%;
line-height: 1;
color: rgba(0, 0, 0, 0.8);
content:"\25BC";
position: absolute;
text-align: center;
}
.d3-tip.n:after {
margin: -1px 0 0 0;
top: 100%;
left: 0;
}
.axis {
font: 9px sans-serif;
}
.bar {
fill: steelblue;
}
.bar:hover {
fill: red;
}
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
JavaScript
var containerWidth, containerHeight;
var svg1, svg2;
var dataset = [{
Anno: 2003,
Italia: 46.9
}, {
Anno: 2004,
Italia: 46.3
}, {
Anno: 2005,
Italia: 54.0
}, {
Anno: 2006,
Italia: 56.7
}, {
Anno: 2007,
Italia: 56.9
}, {
Anno: 2008,
Italia: 58.1
}, {
Anno: 2009,
Italia: 59.0
}, {
Anno: 2010,
Italia: 61.3
}, {
Anno: 2011,
Italia: 62.6
}, {
Anno: 2012,
Italia: 64.5
}, {
Anno: 2013,
Italia: 67.2
}];
var dataset2 = [{
Anno: 2003,
Totale: 29.9
}, {
Anno: 2005,
Totale: 31.8
}, {
Anno: 2006,
Totale: 34.1
}, {
Anno: 2007,
Totale: 36.9
}, {
Anno: 2008,
Totale: 40.2
}, {
Anno: 2009,
Totale: 44.4
}, {
Anno: 2010,
Totale: 48.9
}, {
Anno: 2011,
Totale: 51.5
}, {
Anno: 2012,
Totale: 52.5
}, {
Anno: 2013,
Totale: 54.8
}];
containerWidth = document.getElementById("MainVisualization").offsetWidth;
containerHeight = document.getElementById("MainVisualization").offsetHeight;
console.log("------ " + containerWidth + " " + containerHeight);
var margin = {
top: 20,
right: 20,
bottom: 30,
left: 40
};
width = containerWidth - margin.left - margin.right,
height = containerHeight - margin.top - margin.bottom;
var padding = 20;
var dataset;
var parseDate = d3.time.format("%Y").parse;
var xScale = d3.time.scale().range([0, width]);
var yScale = d3.scale.linear().range([height, 0]);
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom");
var yAxis = d3.svg.axis().scale(yScale).orient("left");
var line = d3.svg.line().interpolate("linear")
.x(function (d) {
return xScale(d.Anno);
})
.y(function (d) {
return yScale(+d.Italia);
});
var tip = d3.tip()
.attr('class', 'd3-tip')
...