September 22, 2022

A Lightweight Vue Component For Drawing Pure CSS Donut Charts

CSS Donut Chart is a lightweight vue component for drawing donut charts on applications using CSS. Compatible with both Vue 3 and Vue.2.

css donut chart js, vue css donut chart, donut pie chart css animation, svg donut chart, donut chart html css javascript, svg donut chart

How to make use of it:

Install and download:

# Yarn
$ yarn add vue-css-donut-chart
# NPM
$ npm install vue-css-donut-chart --save

1. Import and register the component.

import Donut from 'vue-css-donut-chart';
import 'vue-css-donut-chart/dist/vcdonut.css';
Vue.use(Donut);

2. The example shows how to create a basic pie chart.

<template>
  <vc-donut :sections="sections">Basic donut</vc-donut>
</template>
export default {
  data() {
    return {
      sections: [
        { label: 'Red section', value: 25, color: 'red' },
        { label: 'Green section', value: 25, color: 'green' },
        { label: 'Blue section', value: 25, color: 'blue' }
      ]
    };
  },
  methods: {
    handleSectionClick(section, event) {
      console.log(`${section.label} clicked.`);
    }
  }
};

3. Component props available.

// diameter of the donut
size: { type: Number, default: 250, validator: v => v > 0 },
// unit to use for `size`
unit: { type: String, default: 'px' },
// percentage of donut ring's thickness
thickness: {
  type: Number,
  default: 20,
  validator: v => v >= 0 && v <= 100
},
// text in the middle of the donut, this can also be passed using the default slot
text: { type: String, default: null },
autoAdjustTextSize: { type: Boolean, default: true },
// color to use for the middle of the donut
// set this to `transparent` or `thickness` to 100 to make a pie chart instead
background: { type: String, default: '#ffffff' },
// color to use for the empty ring areas
foreground: { type: String, default: '#eeeeee' },
// sections of the donut, must have a `value` property
// other valid properties are `label` and `color` (default is `dodgerblue`)
sections: {
  type: Array,
  default: () => [],
  validator(sections) {
    for (let i = 0; i < sections.length; ++i) {
      /* istanbul ignore if - already covered by unit tests for sectionValidator */
      if (!sectionValidator(sections[i])) return false;
    }
    return true;
  }
},
total: { type: Number, default: 100, validator: v => v > 0 },
hasLegend: { type: Boolean, default: false },
legendPlacement: {
  type: String,
  default: placement.BOTTOM,
  validator: val => !!placement[val.toUpperCase()]
},
// degree angle at which the first section begins
startAngle: { type: Number, default: 0 }

API

Props

PropTypeRequiredDefaultDescription
sizeNumberNo250Diameter of the donut. Can be any positive value.
unitStringNo'px'Unit to use for size. Can be any valid CSS unit. Use % to make the donut responsive.
thicknessNumberNo20Percent thickness of the donut ring relative to size. Can be any positive value between 0-100 (inclusive). Set this to 0 to draw a pie chart instead.
textStringNoText to show in the middle of the donut. This can also be provided through the default slot.
backgroundStringNo'#ffffff'Background color of the donut. In most cases, this should be the background color of the parent element.
foregroundStringNo'#eeeeee'Foreground color of the donut. This is the color that is shown for empty region of the donut ring.
start-angleNumberNo0Angle measure in degrees where the first section should start.
totalNumberNo100Total for calculating the percentage for each section.
has-legendBooleanNofalseWhether the donut should have a legend.
legend-placementStringNo'bottom'Where the legend should be placed. Valid values are toprightbottom and left. Doesn’t have an effect if has-legend is not true.
auto-adjust-text-sizeBooleanNotrueWhether the font-size of the donut content is calculated automatically to fit the available space.
sectionsArray

 

No[]An array of objects. Each object in the array represents a section.
section.valueNumberYesValue of the section. The component determines what percent of the donut should be taken by a section based on this value and the total prop. Sum of all the sections’ value should not exceed total, an error is thrown otherwise.
section.colorStringRead descriptionRead descriptionColor of the section. The component comes with 24 predefined colors, so this property is optional if you have <= 24 sections without the color property.
section.labelStringNo'Section <section number>'Name of the section. This is used in the legend as well as the tooltip text of the section.

Pure CSS Donut Charts Plugin/Github


See Demo And Download

Official Website(dumptyd): Click Here

This superior jQuery/javascript plugin is developed by dumptyd. For extra Advanced usage, please go to the official website.

Share

You may also like...

Leave a Reply

Your email address will not be published.