JSFiddle - React, Tailwind, and code Playground
by Nevin Madhukar
HTML
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/d3-tip/0.6.3/d3-tip.min.js"></script>
CSS
.bars:hover {
fill: brown;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
h1 {
text-align: center;
}
body {
font: 10px sans-serif;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
.d3-tip {
line-height: 1;
font-weight: bold;
padding: 12px;
background: rgba(0, 0, 0, 0.8);
color: #fff;
border-radius: 2px;
}
/* Creates a small triangle extender for the tooltip */
.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;
}
/* Style northward tooltips differently */
.d3-tip.n:after {
margin: -1px 0 0 0;
top: 100%;
left: 0;
}
JavaScript
var data=[{"name": "x1",
"position":[0,60,80,130,200]
},
{"name": "y1",
"position":[0,190,220,180,240]
},
{"name": "x2",
"position":[0,40,80,120,200]
},
{"name": "y2",
"position":[10,90,20,60,40]},
{"name": "x3",
"position":[30,70,100,130,230]
},
{"name": "y3",
"position":[20,160,170,160,150]}]
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d) {
return "<strong>Frequency:</strong> <span style='color:red'>" + d + "</span>";
});
var dataArray = new Array(), k = 0, j = 0, dataArraySize = 0, line, positionNumber,
canvas = d3.select("body").append("svg")
.attr("width", 1000)
.attr("height", 1000)
.attr("border", "black")
.call(tip);
heightScale = d3.scale.linear()
.domain([0, 250])
.range([300, 0]);
widthScale = d3.scale.linear()
.domain([0, 250])
.range([0, 900]);
Yaxis = d3.svg.axis()
.ticks(5)
.scale(heightScale)
.orient("left");
Xaxis = d3.svg.axis()
.ticks(10)
.scale(widthScale)
.orient("bottom");
jsonLength = Object.keys(data).length;
group = canvas.append("g")
.attr("class", "grp")
.attr("transform", "translate(10,10)");
// Conversion of JSON data to appropriate data format
for (j = 0; j < jsonLength; j += 2) {
var innerObjArray = [];
positionNumber = Object.keys(data[j].position).length;
for (k = 0; k < positionNumber; k++) {
var obj = {};
obj.x = data[j].position[k];
obj.y = data[j + 1].position[k];
innerObjArray.push(obj);
}
dataArray.push(innerObjArray);
}
dataArraySize = dataArray.length;
//Graphical representation of the dataArray
line = d3.svg.line()
...