Pen Settings



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


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


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.


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.


                <div class="container">
  <h2>Ethnic Groups in South Africa</h2>
<p><a target="_blank"  href="">source: stats sa</a></p>
   <button onclick="play()">Play Animation</button>
<div class="graph2"></div>


                @import url('');

$text-color: white;

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

  text-decoration: none;
  color: $text-color;

  width: 300px;
  margin: 20px;

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


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

const color = d3.scaleOrdinal().domain(ethnicGroupsData)

const radius = 100;

// Animation
function play(){
  const svg ='.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做出扇形
var arcGenerator = d3.arc()

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

.attr('fill', function(d, i){
  return color(
.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()
var text = arc.append("text")
  .text(function(d) { return; })
  .style("font-family", "arial")
  .style("font-size", 15)
  .style("fill", "transparent")
  .attr("text-anchor", "middle")
  .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")
    var outer = label.centroid(d).map((i)=>i*0.9)
    return outer+','+ arcGenerator.centroid(d)