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="app">
  <bar-chart percent="{{barValue}}" foreground-color="#badaff" background-color="#bada55"></bar-chart><br>
  Value: <input type="range" min="0" step="1" max="100" v-model="barValue"> {{barValue}}<br><br>
  
  <!-- Just define the foreground color, background color, stroke-width and radius -->
  <donut-chart percent="{{value}}" foreground-color="#badaff" background-color="#bada55" stroke-width="{{width}}" radius="{{radius}}"></donut-chart><br>
  
  <!-- Fun tool -->
  Value: <input type="range" min="0" step="1" max="100" v-model="value"> {{value}}<br>
  Radius: <input type="range" min="0" step="1" max="100" v-model="radius"> {{radius}}<br>
  Width: <input type="range" min="0" step="1" max="100" v-model="width"> {{width}}<br>
</div>

<script type="text/x-template" id="bar-template">
  <div class="bar-outer" style="width: 100%; height: {{height}}; background-color: {{backgroundColor}};">
    <div class="bar-inner" style="width: {{percent}}%; height: {{height}};  background-color: {{foregroundColor}}">
  </div>
</script>
  
<script type="text/x-template" id="donut-template">
  <svg width="200" height="200" version="1.1" xmlns="http://www.w3.org/2000/svg" style="border: 1px solid #000000;" viewBox="0 0 200 200">

    <!-- Background circle -->
    <path v-attr="d: dBg"
          fill="transparent"
          stroke="{{backgroundColor}}"
          stroke-width="{{strokeWidth}}"/>
    
    <!-- Move to start position, start drawing arc -->
    <path v-attr="d: d" 
          fill="transparent"
          stroke="{{foregroundColor}}"
          stroke-width="{{strokeWidth}}"/>
  </svg>
</script>
              
            
!

CSS

              
                body {
  background: #ffffff;
}

svg {
  width: 300px;
  height: 300px;
}
              
            
!

JS

              
                Vue.component('bar-chart', {
  props: ['percent', 'foreground-color', 'background-color'],
  template: '#bar-template',
  replace: true,
  data: function () {
    return {
      // default values
      foregroundColor: "#badaff",
      backgroundColor: "#bada55",
      percent: 25,
      height: '1em'
    }
  },
});

Vue.component('donut-chart', {
  props: ['percent', 'foreground-color', 'background-color', 
         'stroke-width', 'radius'],
  template: '#donut-template',
  replace: true,
  data: function () {
    return {
      // default values
      foregroundColor: "#badaff",
      backgroundColor: "#bada55",
      radius: 85,
      strokeWidth: 20,
      percent: 25
    }
  },
  computed: {
    // If more than 50% filled we need to switch arc drawing mode from less than 180 deg to more than 180 deg
    largeArc: function () {
      return this.percent < 50 ? 0 : 1;
    },
    // Where to put x coordinate of center of circle
    x: function () {
      return 100;
    },
    // Where to put y coordinate of center of circle
    y: function () {
      return 100 - this.radius;
    },
    // Calculate X coordinate of end of arc (+ 100 to move it to middle of image)
    // add some rounding error to make arc not disappear at 100%
    endX: function () {
      return -Math.sin(this.radians) * this.radius + 100 - 0.0001;
    },
    // Calculate Y coordinate of end of arc (+ 100 to move it to middle of image)
    endY: function () {
      return Math.cos(this.radians) * this.radius + 100;
    },
    // Calculate length of arc in radians
    radians: function () {
      var degrees = (this.percent/100)*360
      var value = degrees - 180; // Turn the circle 180 degrees counter clockwise
      
      return (value*Math.PI)/180;
    },
    // If we reach full circle we need to complete the circle, this ties into the rounding error in X coordinate above
    z: function () {
      return this.percent == 100 ? 'z' : '';
    },
    dBg: function () {
      return "M "+this.x+" "+this.y+" A "+this.radius+" "+this.radius+" 0 1 1 "+(this.x-0.0001)+" "+this.y+" z";
    },
    d: function () {
      return "M "+this.x+" "+this.y+" A "+this.radius+" "+this.radius+" 0 "+this.largeArc+" 1 "+this.endX+" "+this.endY+" "+this.z;
    }
  }
});

var vm = new Vue({
  el: '#app',
  data: {
    // Bar 
    barValue: 25,
    
    // Donut
    value: 25,
    radius: 85,
    width: 20
  }
});

              
            
!
999px

Console