Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container">
  <h2>Ethnic Groups in South Africa</h2>
<p><a target="_blank"  href="http://www.statssa.gov.za/?p=14415">source: stats sa</a></p>
  <div>
   <button onclick="play()">Play Animation</button>
  </div>
<div class="graph2"></div>
</div>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css2?family=Raleway:wght@600&display=swap');

$text-color: white;

body{
  background: black;
  font-family: 'Raleway', sans-serif;
  padding: 30px;
  color: $text-color;
  font-size: 18px;
  font-weight: 400;
  display: flex;
}

a{
  text-decoration: none;
  color: $text-color;
}

.container{
  width: 300px;
  margin: 20px;
}

button{
  border: none;
  border-radius: 15px;
  background: rgba(123, 239, 178, 1);
  padding: 10px;
  font-size: 14px;
  color: black;
  cursor: pointer;
}
              
            
!

JS

              
                //data
const ethnicGroupsData = [
 { label: 'Black',value:80.7},
       { label: 'Coloured',value:8.8},
   { label: 'White',value:7.9},
   { label: 'Asian',value:2.6}]


//做出顏色比例尺,對應data順序
const color = d3.scaleOrdinal().domain(ethnicGroupsData)
.range(['#f9c74f','#f8961e','#90be6d','#43aa8b']);



const radius = 100;




// Animation
function play(){
  //先清空
  d3.select('svg').remove();
  
  //svg起手式
  const svg = d3.select('.graph2').append('svg')
.attr('width',300).attr('height',300).attr('id','graph2-svg').append("g").attr("transform", "translate(" + 300 / 2 + "," + 300 / 2 + ")");
  
  // 做餅啦
var pie = d3.pie().value(function(d) {return d.value});

// 畫圓餅圖製造機,幫每一個data做出扇形
//可以看這一篇:弧的產生器(https://ithelp.ithome.com.tw/articles/10207678)
var arcGenerator = d3.arc()
  .innerRadius(0)
  .outerRadius(radius)

// 把data塞進去
var arc = svg.selectAll('arc').data(pie(ethnicGroupsData)).enter()

//做底圖一塊一塊啦
arc
.append('path')
.attr('d',arcGenerator)
.attr('fill', function(d, i){
  return color(d.data)
})
.transition()
.duration(3000)
.attrTween("d", function(d) {
    //data裡面 有startAngle & endAngle
    var d3Interpolate = d3.interpolate({startAngle: 0, endAngle: 0}, d);
    return function(t) {
       // t就是時間間隔回傳0-1中間的數值
      //d3Interpolate(t) =>各個data在t時間的角度
      return arcGenerator(d3Interpolate(t));
    };
})

// Label
var label = d3.arc()
            .outerRadius(radius*1.5)
            .innerRadius(radius);
            
var text = arc.append("text")
  .text(function(d) { return d.data.label; })
  .style("font-family", "arial")
  .style("font-size", 15)
  .style("fill", "transparent")
  .attr("text-anchor", "middle")
  .transition()
  .delay(2000)
  .duration(1000)
  .style("fill", "white")
  .attr("transform", function(d) { 
    return "translate(" + label.centroid(d) + ")"; 
   })



// polyline
var polyline = arc.append("polyline")
  .attr("stroke", "white")
  .attr("stroke-width", 1)
  .attr("fill", "transparent")
  .transition()
  .delay(3000)
  .duration(1000)
  .attr("points",function(d){
    console.log(arcGenerator.centroid(d))
    var outer = label.centroid(d).map((i)=>i*0.9)
    console.log('outer'+outer)
    return outer+','+ arcGenerator.centroid(d)
  })
}
play();
              
            
!
999px

Console