D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
Sokrates86
Full window
Github gist
piechart
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> <style> body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; font-family: "Tahoma",sans-serif;} svg { width:100%; height: 100% } #show{ display: initial; } #hidden { display: none; } </style> </head> <body> <h1> PRODUCT</h1> <script> //DATASET var wholePopulation = [ { "sample": "Finnish HHs", "percentage": 100, "amount": 1871000 } ]; var inGeneral = [ { "sample": "Buys product in general", "percentage": 63, "amount": 1178000 }, { "sample": "Doesn't buy product in general", "percentage": 37, "amount": 693000 }]; var ourProduct = [ { "sample": "Buys product", "percentage": 17, "amount": 200000 }, { "sample": "Doesn't buy product", "percentage": 46, "amount": 978000 } ]; var afterIncrease = [ { "sample": "Buys product after 5% increase", "percentage": 22, "amount": 258000 }, { "sample": "Doesn't buy product", "percentage": 41, "amount": 920000 }, ]; //tausta-asetukset var margin = {top: 30, right: 20, bottom: 30, left: 30}, w = 600 - margin.left - margin.right h = 500 - margin.top - margin.bottom; var svg = d3.select("body") .append("svg") .attr("width", w + margin.left + margin.right) .attr("height", h + margin.top + margin.bottom ) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); var color1 = d3.scale.linear() .range(["#5F79FF","#d3d3d3","#00bce4", "#909981"]); var color2 = d3.scale.linear() .range(["#ffdf1f","#d3d3d3", "#bf311a", "#909981"]); var color3 = d3.scale.linear() .range(["#5F79FF", "#d3d3d3","#00bce4", "#909981"]); var color4 = d3.scale.linear() .range(["#ffdf1f", "#7e7e7e", "#A7B2CC","#00bce4"]); var pieXcenter = w/2; var pieYcenter = h/2; var pie = d3.layout.pie() .value(function(d) { return d.amount; }); var pieOurProduct = d3.layout.pie() .value(function(d) { return d.amount; }) .endAngle(function(d) { return cScale(1178000)}) .sort(null); var cScale = d3.scale.linear().domain([0, 1871000]).range([0, 2 * Math.PI]); //wholePopulation var outerRadiusWholePopulation = 0; var innerRadiusWholePopulation = w / 9; var arcWholePopulation = d3.svg.arc() .innerRadius(innerRadiusWholePopulation) .outerRadius(outerRadiusWholePopulation); var arcsWholePopulation = svg.selectAll("#wholePopulation") .data(pie(wholePopulation)) .enter() .append("g") .attr("class", "clickWholePopulation") .attr("id", "show") .attr("transform", "translate(" + pieXcenter + "," + pieYcenter + ")"); arcsWholePopulation.append("path") .attr("fill", function(d,i) {return color1(i); }) .attr("d", arcWholePopulation) .on("mouseover", function(d) { return d3.select(this).attr("stroke", "yellow").attr("stroke-width", 2);}) .on("mouseout", function(d) { return d3.select(this).attr("stroke", "none").attr("stroke-width", 2);}); arcsWholePopulation.append("text") .attr("transform", function(d) { return "translate(" + arcWholePopulation.centroid(d) + ")"; }) .attr("text-anchor", "middle") .attr("class", "value") .text(function(d) { return d.value;}) .attr("fill", "black"); //inGeneral var outerRadiusInGeneral = w / 4.9; var innerRadiusInGeneral = w / 9; var arcInGeneral = d3.svg.arc() .innerRadius(innerRadiusInGeneral) .outerRadius(outerRadiusInGeneral); var arcsInGeneral = svg.selectAll("#inGeneral") .data(pie(inGeneral)) .enter() .append("g") .attr("class", "clickInGeneral") .attr("id", "hidden") .attr("transform", "translate(" + pieXcenter + "," + pieYcenter + ")"); arcsInGeneral.append("path") .attr("fill", function(d,i) {return color2(i); }) .attr("d", arcInGeneral) .on("mouseover", function(d) { return d3.select(this).attr("stroke", "yellow").attr("stroke-width", 2);}) .on("mouseout", function(d) { return d3.select(this).attr("stroke", "none").attr("stroke-width", 2);}); arcsInGeneral.append("text") .attr("transform", function(d) { return "translate(" + arcInGeneral.centroid(d) + ")"; }) .attr("text-anchor", "middle") .text(function(d) { return d.value;}) .attr("fill", "black"); //ourProduct var outerRadiusOurProduct = w / 3.3; var innerRadiusOurProduct = w / 4.9; var arcOurProduct = d3.svg.arc() .innerRadius(innerRadiusOurProduct) .outerRadius(outerRadiusOurProduct) //.startAngle(function(d) { return cScale(d.startAmount); }) //.endAngle(function(d) { return cScale(d.endAmount); }) ; var arcsOurProduct = svg.selectAll("#ourProduct") .data(pieOurProduct(ourProduct)) .enter() .append("g") .attr("class", "clickOurProduct") .attr("id", "hidden") .attr("transform", "translate(" + pieXcenter + "," + pieYcenter + ")"); arcsOurProduct.append("path") .attr("fill", function(d,i) {return color3(i); }) .attr("d", arcOurProduct) .on("mouseover", function(d) { return d3.select(this).attr("stroke", "yellow").attr("stroke-width", 2);}) .on("mouseout", function(d) { return d3.select(this).attr("stroke", "none").attr("stroke-width", 2);}); arcsOurProduct.append("text") .attr("transform", function(d) { return "translate(" + arcOurProduct.centroid(d) + ")"; }) .attr("text-anchor", "middle") .text(function(d) { return d.value;}) .attr("fill", "black"); //afterIncrease var outerRadiusAfterIncrease = w / 2.5; var innerRadiusAfterIncrease = w / 3.3; var arcAfterIncrease = d3.svg.arc() .innerRadius(innerRadiusAfterIncrease) .outerRadius(outerRadiusAfterIncrease) //.startAngle(function(d) { return cScale(d.startAmount); }) //.endAngle(function(d) { return cScale(d.endAmount); }) ; var arcsAfterIncrease = svg.selectAll("#afterIncrease") .data(pieOurProduct(afterIncrease)) .enter() .append("g") .attr("class", "clickAfterIncrease") .attr("id", "hidden") .attr("transform", "translate(" + pieXcenter + "," + pieYcenter + ")"); arcsAfterIncrease.append("path") .attr("fill", function(d,i) {return color4(i); }) .attr("d", arcAfterIncrease) .on("mouseover", function(d) { return d3.select(this).attr("stroke", "yellow").attr("stroke-width", 2);}) .on("mouseout", function(d) { return d3.select(this).attr("stroke", "none").attr("stroke-width", 2);}); arcsAfterIncrease.append("text") .attr("transform", function(d) { return "translate(" + arcAfterIncrease.centroid(d) + ")"; }) .attr("text-anchor", "middle") .text(function(d) { return d.value;}) .attr("fill", "black"); //dashed var outerRadiusOurProduct = w / 2.5; var innerRadiusOurProduct = w / 3.3; var arcOurProduct = d3.svg.arc() .innerRadius(innerRadiusOurProduct) .outerRadius(outerRadiusOurProduct) //.startAngle(function(d) { return cScale(d.startAmount); }) //.endAngle(function(d) { return cScale(d.endAmount); }) ; var arcsOurProduct = svg.selectAll("#dashed") .data(pieOurProduct(ourProduct)) .enter() .append("g") .attr("class", "clickDashed") .attr("id", "hidden") .attr("transform", "translate(" + pieXcenter + "," + pieYcenter + ")"); arcsOurProduct.append("path") .attr("fill", "none") .attr("stroke", function(d,i) { if (i == 1) { return "black";} else { return "none"; }}) .attr("d", arcOurProduct) .attr("stroke-dasharray", function(d,i) { if (i < 2 && i > 0) { return "3,3";} else { return "0,0"; }}); //SHOW & HIDE d3.select(".clickWholePopulation") .on("click", function() { d3.selectAll(".clickInGeneral") .attr("id", "show") }); d3.selectAll(".clickInGeneral") .on("click", function() { d3.selectAll(".clickOurProduct") .attr("id", "show") }); d3.selectAll(".clickOurProduct") .on("click", function() { d3.selectAll(".clickAfterIncrease") .attr("id", "show") }); d3.selectAll(".clickAfterIncrease") .on("click", function() { d3.selectAll(".clickDashed") .attr("id", "show") }); </script> </body>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js