Skip to content

Instantly share code, notes, and snippets.

@milkbread
Created July 12, 2014 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milkbread/e9fa603aa94f5705b802 to your computer and use it in GitHub Desktop.
Save milkbread/e9fa603aa94f5705b802 to your computer and use it in GitHub Desktop.
circulaChartExperiment
div.circChart {
display: inline-block;
margin: 6px;
}
div.legend {
display: inline-block;
vertical-align: top;
margin-left: 20px;
}
div.legendRect {
width: 10px;
height: 10px;
margin-top: 14px;
}
div.legendRect,
div.legendText {
display: inline-block;
}
div.legendText {
margin-left: 10px;
}
div.month {
text-align: center;
font-family: sans-serif;
font-size: 24px;
margin-bottom: 10px;
}
.circChart text {
font-family: sans-serif;
font-size: 16px;
}
.arc text {
font-size: 12px;
fill: white;
font-weight: bold;
}
.arc path {
}
.center text {
fill: black;
}
.center text#total {
font-size: 18px;
}
.center text#current {
font-size: 14px;
}
function addCircChart(total, current, container, month, color, radius) {
var w = radius,
h = radius;
var outerRadius = w / 2;
var innerRadius = radius / 2.9;
var arc = d3.svg.arc()
.innerRadius(innerRadius)
.outerRadius(outerRadius);
var pie = d3.layout.pie();
container.append("div")
.text(month)
.attr("class", "month");
//Create SVG element
var svg = container.append("svg")
.attr("width", w)
.attr("height", h);
//Set up groups
var arcs = svg.selectAll("g.arc")
.data(pie([total-current, current]))
.enter()
.append("g")
.attr("class", "arc")
.attr("transform", "translate(" + outerRadius + "," + outerRadius + ")");
//Draw arc paths
arcs.append("path")
.attr("d", arc)
.attr("fill", function(d, i) {
return i===0 ? "#ccc" : color;
});
//Labels
arcs.append("text")
.attr("transform", function(d) {
return "translate(" + arc.centroid(d) + ")";
})
.attr("text-anchor", "middle")
.text(function(d, i) {
return d.value;
});
var center = svg.append("g")
.attr("class", "center");
var infoTotal = center.append("text")
.attr("id", "total")
.text("Ziel: " + total);
var infoCurrent = center.append("text")
.attr("id", "current")
.text("Ist: " + current);
infoTotal.attr("transform", "translate(" + ((w/2) - (parseInt(d3.select("#total").style("width"))/2)) + "," + (h/2) + ")");
infoCurrent.attr("transform", "translate(" + ((w/2) - (parseInt(d3.select("#current").style("width"))/2)) + "," + ((h/2) + (parseInt(d3.select("#current").style("height"))+2)) + ")");
// center point
// svg.append("circle")
// .attr("cx", w/2)
// .attr("cy", h/2)
// .attr("r", 2 )
// .style("fill", "#f00");
}
function addCircCharts(data, container) {
data.forEach(function(d) {
addCircChart(d.goal, d.current, container.append("div").attr("class", "circChart"), d.month, d.color, d.radius);
});
}
function addChartLegend(data, container) {
var legend = container.append("div").attr("class", "legend")
var legenRows = legend.selectAll("div.legendRect")
.data(data)
.enter()
.append("div")
.attr("class", "row");
var legendRects = legenRows.append("div")
.attr("class", "legendRect")
.style("background-color", function(d) {return d.color;});
var legendTexts = legenRows.append("div")
.attr("class", "legendText")
.text(function(d) {return d.event + " - noch " + (d.goal - d.current) + " fehlen";});
}
function addRegioCircCharts(data, container) {
addCircCharts(data, container);
addChartLegend(data, container);
}
<html>
<head>
<title>CircularChartExample</title>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="circularChart.js"></script>
<link rel="stylesheet" href="circChartStyles.css" />
</head>
<body>
<script type="text/javascript">
var container = d3.select("body");
function renderCircChart(error, data) {
data[0].radius = 150;
data[0].color = "#f00";
data[1].radius = 150;
data[1].color = "#0f0";
addRegioCircCharts(data, container);
}
d3.csv("statDatenKreise.csv", function(d) {
return {
month: d.Monat,
event: d.Event,
goal: d.Soll,
current: d.Ist
};
}, renderCircChart);
</script>
<body>
</html>
Monat Soll Ist Event
Juni 600 540 Buffet
Juli 620 150 Beach
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment