Skip to content

Instantly share code, notes, and snippets.

@cflavs
Created May 9, 2016 00:43
Show Gist options
  • Save cflavs/3f6cae02edafcc0a8b0394cbb88a3c3e to your computer and use it in GitHub Desktop.
Save cflavs/3f6cae02edafcc0a8b0394cbb88a3c3e to your computer and use it in GitHub Desktop.
Multline Final
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body{
font:8px sans-serif;
}
.axis path,
.axis line{
fill:none;
stroke:#000;
shape-rendering:crispEdges;
}
.x.axis path{
display:none;
}
.line{
fill:none;
stroke:steelblue;
stroke-width:1 px;
}
.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;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>
<script>
var margin = {top: 20, right: 80, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var parseDate = d3.time.format("%Y").parse;
var x = d3.time.scale()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var color = d3.scale.category10();
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(2);
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d) {
return "<strong> Estado: </strong> <span style='color:red'>" + d.name + "</span>";
})
var line = d3.svg.line()
.interpolate("basis")
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.temperature); });
var svg = d3.select("body").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 + ")");
svg.call(tip);
function dashboard(data) {
color.domain(d3.keys(data[0]).filter(function(key) { return key !== "date"; }));
data.forEach(function(d) {
d.date = parseDate(d.date);
});
var cities = color.domain().map(function(name) {
return {
name: name,
values: data.map(function(d) {
return {date: d.date, temperature: +d[name]};
})
};
});
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([
d3.min(cities, function(c) { return d3.min(c.values, function(v) { return v.temperature; }); }),
d3.max(cities, function(c) { return d3.max(c.values, function(v) { return v.temperature; }); })
]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Homicios Dolosos");
var city = svg.selectAll(".city")
.data(cities)
.enter().append("g")
.attr("class", "city");
city.append("path")
.attr("class", "line")
.attr("d", function(d) { return line(d.values); })
.on('mouseover', tip.show)
.on('mouseout', tip.hide)
.style("stroke", function(d) { return color(d.name); });
}
</script>
<script>
var freqData=[
{date:"2010",AC:0,AL:257.53,AP:0,AM:739.43,BA:365.79,CE:235.34,DF:229.14,ES:159.08,GO:82.02,MA:53.39,MT:173.13,MS:45.45,MG:49.76,PA:285.25,PB:47.38,PR:0}
,{date:"2010",PE:309.01,PI:91.63 ,RJ:446.62,RN:252.58,RS:219.42,RO:112.85,RR:51.81,SC:9.87,SP:110.73,SE:128.56 ,TO:36.49}
,{date:"2011",AC:0,AL:332.25 ,AP:25.31,AM:1101.94,BA:346.64,CE:144.95,DF:207.89,ES:232.32,GO:155.45,MA:115.1,MT:166.88,MS:38.22,MG:53.05,PA:117.74,PB:63.09}
,{date:"2011",PR:0, PE:351.08,PI:106.83,RJ:387.54,RN:0,RS:212.94,RO:139.34,RR:43.14,SC:25.66,SP:360.48,SE:175.94,TO:26.98}
,{date:"2012",AC:0,AL:425.57,AP:96.16,AM:739.76,BA:28.53,CE:293.57,DF:289.94,ES:166.6,GO:75.17 ,MA:138.19,MT:175.7,MS:28.69,MG:59.02,PA:0,PB:51.32 }
,{date:"2012",PR:0, PE:316.52,PI:148.66,RJ:423.27,RN:378.65,RS:218.4,RO:119.27,RR:51.24,SC:63.24,SP:374.58 ,SE:226.52,TO:29.89}
,{date:"2013",AC:0,AL:474.13,AP:101.11,AM:313.89,BA:450.84,CE:432.66,DF:291.56,ES:59.11,GO:194.67,MA:194.32,MT:229.56,MS:42.34,MG:46.36,PA:300.51,PB:42.67 }
,{date:"2013",PR:128.11, PE:260,PI:148.66,RJ:537.16,RN:202.67,RS:215.65,RO:114.42,RR:130.06,SC:63.82,SP:423.95,SE:296.11,TO:61.02}
];
dashboard(freqData);
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment