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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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 id="root"></div>
              
            
!

CSS

              
                html, body {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: sans-serif;
}
p {
  text-align: center;
  line-height: 1.4;
}
a {
  color: DeepPink;
  text-decoration: none;
}

.pie-chart {
  --pie-size: 200px;
  --pie-bg: MidnightBlue;
  --slice-1-bg: MediumPurple;
  --slice-2-bg: DarkSlateBlue;
  --slice-3-bg: DarkMagenta;
  
  &__title {
    text-align: center;
    text-transform: uppercase;
  }
  &__pie {
    transform: rotate(-90deg);
    height: var(--pie-size);
    width: var(--pie-size);
    display: block;
    margin: 0 auto;
    
    &_bg {
      fill: var(--pie-bg);
    }
  }
  @for $i from 1 through 3 { 
    path:nth-of-type( #{$i}) { fill: var(--slice-#{$i}-bg) }
  }
}
              
            
!

JS

              
                const BASE_CLASS = `pie-chart`
class PieChart extends React.PureComponent {
  constructor( props ) {
    super( props )
  }
  
  static getCoordinatesForPercent( percent ) {
    const x = Math.cos(2 * Math.PI * percent)
    const y = Math.sin(2 * Math.PI * percent)
    return [ x, y ]
  }
  
  createSlices( slices ) {
    let cumulativePercent = 0
    return slices.map( (percent, index) => {
      // destructuring assignment sets the two variables at once
      const [startX, startY] = PieChart.getCoordinatesForPercent( cumulativePercent )
      // each slice starts where the last slice ended, so keep a cumulative percent
      cumulativePercent = cumulativePercent + percent
      const [endX, endY] = PieChart.getCoordinatesForPercent( cumulativePercent )
      // if the slice is more than 50%, take the large arc (the long way around)
      const largeArcFlag = percent > .5 ? 1 : 0
      // some SVG path here
      // • M = Move
      // • A = Arc
      // • L = Line
      const pathData = [
        `M ${startX} ${startY}`,
        `A 1 1 0 ${largeArcFlag} 1 ${endX} ${endY}`,
        `L 0 0`,
      ].join(` `)
      return <path key={ index } d={ pathData } />
    })
  }

  render() {
    const { slices } = this.props
    this.cumulativePercent = 0
    return (
      <div className={BASE_CLASS}>
        <p className={`${BASE_CLASS}__title`}>React Pie Chart</p>
        <svg viewBox="-1 -1 2 2" className={`${BASE_CLASS}__pie`}>
          <defs>
            <mask id="pie-mask">
              <rect fill="white" x="-1" y="-1" width="2" height="2" />
              <circle fill="black" cx="0" cy="0" r=".65" />
            </mask>
          </defs>          
          <g mask="url(#pie-mask)">
            <circle className={`${BASE_CLASS}__pie_bg`} cx="0" cy="0" r="1"/>
            { this.createSlices( slices )  }            
          </g>
        </svg>
      </div>
    )
  }
}

const rSlices = [ 0.1, 0.65, 0.2 ]

function Demo() {
  return (
    <React.Fragment>
      <PieChart slices={ rSlices } />
      <p>based on <a href="https://hackernoon.com/a-simple-pie-chart-in-svg-dbdd653b6936" target="_blank">a simple pie chart in svg</a>
        <br />
          by <a href="https://hackernoon.com/@david.gilbertson" target="_blank">david gilbertson</a>
        </p>
    </React.Fragment>
  )
}

ReactDOM.render(
  <Demo />,
  document.getElementById('root')
)
              
            
!
999px

Console