Pie sort.
From D3 in Depth book by Peter Cook.
| license: gpl-3.0 | |
| height: 230 | |
| border: no |
Pie sort.
From D3 in Depth book by Peter Cook.
| <!DOCTYPE html> | |
| <meta charset="utf-8"> | |
| <head> | |
| <title>Pie sort</title> | |
| </head> | |
| <style> | |
| svg { max-height: 98vh; } | |
| path { | |
| fill: orange; | |
| stroke: white; | |
| } | |
| text { | |
| font-family: "Helvetica Neue", Helvetica, sans-serif; | |
| font-size: 25px; | |
| font-weight: bold; | |
| fill: white; | |
| text-anchor: middle; | |
| } | |
| </style> | |
| <body> | |
| <svg width="100%" height="100%" viewBox="0 -150 300 300"> | |
| <g transform="translate(150, 0)"></g> | |
| </svg> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script> | |
| <script> | |
| var pieGenerator = d3.pie() | |
| .value(function(d) {return d.quantity;}) | |
| .sort(function(a, b) { | |
| console.log(b) | |
| return a.starts <= b.starts; | |
| }); | |
| var months = [ | |
| {}, // Don't know why that's needed | |
| { | |
| name: 'Capricorn', | |
| symbol: '♑', | |
| starts: new Date('2021/01/21 17:15'), | |
| ends: new Date('2021/02/16 17:47'), | |
| color: 'gold', | |
| alliance: 'blue', | |
| }, | |
| { | |
| name: 'Aquarius', | |
| symbol: '♒', | |
| starts: new Date('2021/02/16 17:47'), | |
| ends: new Date('2021/03/11 17:47'), | |
| color: 'brown', | |
| alliance: 'purple', | |
| }, | |
| { | |
| name: 'Pisces', | |
| symbol: '♓', | |
| starts: new Date('2021/03/11 17:47'), | |
| ends: new Date('2021/04/18 17:45'), | |
| color: 'silver', | |
| alliance: 'purple', | |
| }, | |
| { | |
| name: 'Aries', | |
| symbol: '♈', | |
| starts: new Date('2021/04/18 17:45'), | |
| ends: new Date('2021/05/13 17:45'), | |
| color: 'black', | |
| alliance: 'red', | |
| }, | |
| { | |
| name: 'Taurus', | |
| symbol: '♉', | |
| starts: new Date('2021/05/13 17:45'), | |
| ends: new Date('2021/06/21 17:45'), | |
| color: 'green', | |
| alliance: 'red', | |
| }, | |
| { | |
| name: 'Gemini', | |
| symbol: '♊', | |
| starts: new Date('2021/06/21 17:45'), | |
| ends: new Date('2021/07/20 17:45'), | |
| color: 'yellow', | |
| alliance: 'purple', | |
| }, | |
| { | |
| name: 'Cancer', | |
| symbol: '♋', | |
| starts: new Date('2021/07/20 17:45'), | |
| ends: new Date('2021/08/10 17:45'), | |
| color: 'violet', | |
| alliance: 'blue', | |
| }, | |
| { | |
| name: 'Leo', | |
| symbol: '♌', | |
| starts: new Date('2021/08/10 17:45'), | |
| ends: new Date('2021/09/16 17:45'), | |
| color: '#f5c389', //'peach', | |
| alliance: 'blue', | |
| }, | |
| { | |
| name: 'Virgo', | |
| symbol: '♍', | |
| starts: new Date('2021/09/16 17:45'), | |
| ends: new Date('2021/10/30 17:45'), | |
| color: '#c99f32', //'copper', | |
| alliance: 'purple', | |
| }, | |
| { | |
| name: 'Libra', | |
| symbol: '♎', | |
| starts: new Date('2021/10/30 17:45'), | |
| ends: new Date('2021/11/23 17:45'), | |
| color: 'white', | |
| alliance: 'red', | |
| }, | |
| { | |
| name: 'Scorpio', | |
| symbol: '♏', | |
| starts: new Date('2021/11/23 17:45'), | |
| ends: new Date('2021/11/29 17:45'), | |
| color: 'pink', | |
| alliance: 'red', | |
| }, | |
| { | |
| name: 'Ophiuchus', | |
| symbol: '⛎', | |
| starts: new Date('2021/11/29 17:45'), | |
| ends: new Date('2021/12/27 17:45'), | |
| color: 'cyan', | |
| alliance: 'purple', | |
| }, | |
| { | |
| name: 'Sagittarius', | |
| symbol: '♐', | |
| starts: new Date('2021/12/27 17:45'), | |
| ends: new Date('2022/01/20 17:45'), | |
| color: 'orange', | |
| alliance: 'blue', | |
| }, | |
| ] | |
| for(var i = 0; i < months.length; i++) { | |
| months[i].quantity = (months[i].ends - months[i].starts) / (60 * 60 * 60 * 24 * 10) | |
| } | |
| // Create an arc generator with configuration | |
| var arcGenerator = d3.arc() | |
| .innerRadius(30) | |
| .outerRadius(150); | |
| var arcData = pieGenerator(months); | |
| // Create a path element and set its d attribute | |
| d3.select('g') | |
| .selectAll('path') | |
| .data(arcData) | |
| .enter() | |
| .append('path') | |
| .attr('d', arcGenerator) | |
| .style('fill', ({ data }) => data.color) | |
| // Labels | |
| d3.select('g') | |
| .selectAll('text') | |
| .data(arcData) | |
| .enter() | |
| .append('text') | |
| .each(function(d) { | |
| var centroid = arcGenerator.centroid(d); | |
| d3.select(this) | |
| .attr('x', centroid[0]) | |
| .attr('y', centroid[1]) | |
| .attr('dy', '0.33em') | |
| .style('fill', ({ data }) => data.alliance) | |
| .attr('title', d.data.name) | |
| .text(d.data.symbol) | |
| }) | |
| // Readable | |
| d3.select('body') | |
| .append('ul') | |
| .data(months) | |
| .enter() | |
| .append('li') | |
| .each(function(d) { | |
| d3.select(this) | |
| .text(`${d.symbol}: ${d.name} <${d.starts.getMonth() + 1}/${d.starts.getDate()} - ${d.ends.getMonth() + 1}/${d.ends.getDate()}>`) | |
| }) | |
| </script> | |
| </body> | |
| </html> |
| �PNG | |