forked from YashMangla's block: Categorical Line Chart with Gridlines
xxxxxxxxxx
<meta charset="utf-8">
<title>Categorical Line Chart with Grid guildlines</title>
<style>
.d3-tip {
cursor:pointer;
line-height: 1;
font-weight: bold;
padding: 12px;
background: rgba(0, 0, 0, 0.7);
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.7);
content: "\25BC";
position: absolute;
text-align: center;
}
/* Style northward tooltips differently */
.d3-tip.n:after {
margin: -1px 0 0 0;
top: 100%;
left: 0;
}
.axis path,
.axis line {
fill: none;
stroke: #ddd;
shape-rendering: crispEdges;
}
.x.axis path {
stroke : #000;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
</style>
<body>
<div id = "line"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-tip/0.6.8/d3-tip.min.js"></script>
<script>
var div = "#line"
var grid = true,
Y_AXIS_LABEL = "Frequencies",
main_height = 400,
lineType = "cardinal",
yFormater = d3.format(",.2s"),
textRotate = "-65",
rotation = true,
xCategories = "State";
var legend_width = 120;
var color = d3.scale.category10();
var mainWidth = 900;
var margin = {
top: 20,
right: 20, //80,
bottom: 40, //120,
left: 120 //50
},
width = mainWidth - margin.left - margin.right-legend_width,
height = main_height - margin.top - margin.bottom;
var x = d3.scale.ordinal().rangeRoundBands([0, width], 1),
y = d3.scale.linear().range([height, 0]),
xCircle = d3.scale.ordinal();
d3.json("population.json", function(data){
data.sort(function(a, b){
if(xCategories == "date"||xCategories=="Date"){
a = a[xCategories].split("-");
b = b[xCategories].split("-");
var ad = new Date(a[2], a[1], a[0]);
var bd = new Date(b[2], b[1], b[0]);
return ad - bd;
}else{
return a[xCategories] - b[xCategories];
}
});
if(grid){
var xAxis = d3.svg.axis().scale(x).orient("bottom").innerTickSize(-height).outerTickSize(0).tickPadding(10),
yAxis = d3.svg.axis().scale(y).orient("left").innerTickSize(-width).outerTickSize(0).tickPadding(10).tickFormat(function(d){ return yFormater(d); });
} else{
var xAxis = d3.svg.axis().scale(x).orient("bottom"),
yAxis = d3.svg.axis().scale(y).orient("left").tickFormat(function(d){ return yFormater(d); });
}
//color.domain(d3.keys(_this.data[0]).filter(function (key) {
color.domain(d3.keys(data[0]).filter(function (key) {
return key !== xCategories;
}));
var cities = color.domain().map(function (name) {
return {
name: name,
values: data.map(function (d) {
return {
key: d[xCategories],
Value: +d[name],
color: name
};
})
};
});
var line = d3.svg.line()
.interpolate(lineType)
.x(function (d) {
return x(d.key);
})
.y(function (d) {
return y(d.Value);
});
d3.select(div).select("svg").remove();
var svg = d3.select(div).append("svg")
.attr("width", width + margin.left + margin.right+legend_width)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function (d) {
return "<center><span>"+xCategories+" : "+d.key+"</span><br /><span>"+d.color+" : "+yFormater(d.Value)+"</span></center>";
});
svg.call(tip);
var yMin = d3.min(cities, function (c) {
return d3.min(c.values, function (v) {
return (v.Value);
});
});
var yMax = d3.max(cities, function (c) {
return d3.max(c.values, function (v) {
return v.Value;
});
});
var d_f = (yMax - yMin)/8;
yMin = yMin - 2*d_f;
yMin = yMin < 100000 ? yMin : 100000;
x.domain(data.map(function (d) {
return d[xCategories];
}));
y.domain([yMin, yMax]);
xCircle.domain(data.map(function (d) {
return d.name;
}))
.rangeRoundBands([0, x.rangeBand()], 0);
if(rotation){
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.attr("stroke-width", "1.5px")
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", ".15em")
.attr("transform", function (d) {
return "rotate("+textRotate+")"
});
} else {
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.attr("stroke-width", "1.5px")
.selectAll("text")
.style("text-anchor", "end");
}
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.attr("stroke-width", "1.5px")
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text(Y_AXIS_LABEL);
var city = svg.selectAll(".city")
.data(cities)
.enter().append("g")
.attr("class", "city");
city.append("path")
.attr("class", "line")
// .attr("transform",function(d){ return "translate(" + xCircle(d.date) + ")"; })
.attr("d", function (d) {
return line(d.values);
})
.style("stroke", function (d) {
return color(d.name);
})
.attr("stroke-width", "1.5px");
city.selectAll("circle")
.data(function (d) {
return d.values;
})
.enter()
.append("circle")
.attr("r", 5)
.attr("cx", function (d) {
return x(d.key);
})
.attr("cy", function (d) {
return y(d.Value);
})
.style("fill", function (d) {
return color(d.color);
})
.style("stroke", "none")
.style("pointer-events", "all")
.style("cursor", "poiter")
.on("mouseover", tip.show)
.on("mouseout", tip.hide);
var legend = svg.selectAll(".legend")
.data(color.domain().slice().reverse())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", width - 18+legend_width)
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
legend.append("text")
.attr("x", width - 24+legend_width)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
});
</script>
</html>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js
https://cdnjs.cloudflare.com/ajax/libs/d3-tip/0.6.8/d3-tip.min.js