Code Monkey home page Code Monkey logo

vue-css-donut-chart's Introduction

vue-css-donut-chart

Lightweight Vue component for drawing pure CSS donut charts

npm npm monthly downloads npm bundle size GitHub Actions status Coverage status Snyk Vulnerabilities for npm package

Using Vue 3?
Check out the documentation for vue-css-donut-chart v2.
NPM - https://www.npmjs.com/package/vue-css-donut-chart/v/next

Live demo

Live demo can be found on the project page – https://dumptyd.github.io/vue-css-donut-chart

Playground – https://jsfiddle.net/dumptyd/ujvypcd3/

Features

◾ No external dependencies.

◾ Vue 2 and Vue 3 support.

◾ Small size footprint. npm bundle size

◾ High test coverage. Coverage status

◾ Performs automatic font size recalculation as the size of the donut changes.

◾ Supports responsive donut and pie charts.

Table of Contents

Installation

Install via yarn or npm

yarn add vue-css-donut-chart

OR

npm install vue-css-donut-chart

Registering vue-css-donut-chart

ES6

import Donut from 'vue-css-donut-chart';
import 'vue-css-donut-chart/dist/vcdonut.css';

Vue.use(Donut);

◾ In-browser using CDNs

Using unpkg
<link rel="stylesheet" href="https://unpkg.com/vue-css-donut-chart@1/dist/vcdonut.css">
<script src="https://unpkg.com/vue-css-donut-chart@1"></script>
<script>
  Vue.use(vcdonut.default);
</script>
Using jsDelivr
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vue-css-donut-chart@1/dist/vcdonut.css">
<script src="https://cdn.jsdelivr.net/npm/vue-css-donut-chart@1/dist/vcdonut.umd.min.js"></script>
<script>
  Vue.use(vcdonut.default);
</script>

Usage

Basic usage

With sane defaults in place, basic usage is as simple as passing a sections array with objects containing a value property. This will create a donut with 2 sections that take up 25% each.

<template>
  <vc-donut :sections="sections">Basic donut</vc-donut>
</template>
<script>
  export default {
    data() {
      return {
        sections: [{ value: 25 }, { value: 25 }]
      };
    }
  };
</script>

Usage with all the available props

<template>
  <vc-donut
    background="white" foreground="grey"
    :size="200" unit="px" :thickness="30"
    has-legend legend-placement="top"
    :sections="sections" :total="100"
    :start-angle="0" :auto-adjust-text-size="true"
    @section-click="handleSectionClick">
    <h1>75%</h1>
  </vc-donut>
</template>
<script>
  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.`);
      }
    }
  };
</script>

For brevity, only the section-click event is demonstrated in the above example. You can use all the other section-* events the same way.

Using the component as a pie chart

Making the component look like a pie chart is as simple as setting the thickness to 100.

<template>
  <vc-donut
    :sections="[{ value: 35 }, { value: 15 }, { value: 15 }, { value: 35 }]"
    :thickness="100">
  </vc-donut>
</template>

Note: setting thickness to 100 will completely hide the diagram's text or slot content. The content will still be present in the DOM, however it won't be visible for obvious reasons.

API

Props

Prop Type Required Default Description
size Number No 250 Diameter of the donut. Can be any positive value.
unit String No 'px' Unit to use for size. Can be any valid CSS unit. Use % to make the donut responsive.
thickness Number No 20 Percent 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.
text String No Text to show in the middle of the donut. This can also be provided through the default slot.
background String No '#ffffff' Background color of the donut. In most cases, this should be the background color of the parent element.
foreground String No '#eeeeee' Foreground color of the donut. This is the color that is shown for empty region of the donut ring.
start-angle Number No 0 Angle measure in degrees where the first section should start.
total Number No 100 Total for calculating the percentage for each section.
has-legend Boolean No false Whether the donut should have a legend.
legend-placement String No 'bottom' Where the legend should be placed. Valid values are top, right, bottom and left. Doesn't have an effect if has-legend is not true.
auto-adjust-text-size Boolean No true Whether the font-size of the donut content is calculated automatically to fit the available space.
sections Array
No [] An array of objects. Each object in the array represents a section.
section.value Number Yes Value 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.color String Read description Read description Color 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.label String No 'Section <section number>' Name of the section. This is used in the legend as well as the tooltip text of the section.

Events

All the section-* listeners are called with the section object on which the event occurred and the native Event object as arguments respectively. Consider adding a custom property (eg: name) to the section objects to uniquely identify them.

Event Parameter Description
section-click section, event Emitted when a section is clicked.
section-mouseenter section, event Emitted when the mouseenter event occurs on a section.
section-mouseleave section, event Emitted when the mouseleave event occurs on a section.
section-mouseover section, event Emitted when the mouseover event occurs on a section.
section-mouseout section, event Emitted when the mouseout event occurs on a section.
section-mousemove section, event Emitted when the mousemove event occurs on a section.

Slots

Slot Description
default slot If you want more control over the content of the chart, default slot can be used instead of the text prop.
legend Slot for plugging in your own legend.

Contributing

Issueshttps://github.com/dumptyd/vue-css-donut-chart/issues

License

Code released under MIT license.

vue-css-donut-chart's People

Contributors

christikaes avatar dumptyd avatar frados avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

vue-css-donut-chart's Issues

Request: show each item count in legend

Hello, nice chart!

What about showing the total count for each section in the legend? Or even in a tooltip while hovering each slice?

My service is returning an array of objects, each contains it's value... so let's say there are 3 sections, first with value = 3, second = 3, third = 6, with this formula I've converted these values into percentages:

series_data.map(s => ({ 
    "label": s.label, 
    "value": s.value / data.total * 100, 
    color: s.color 
}))

Total is the sum of all the values. Yeah I know...it's easy but I think that would be nice to have it implemented as an option, in order to avoid this conversion every time. :-)

Cannot read properties of undefined (reading '_c')

I've ran but show this error:
Cannot read properties of undefined (reading '_c')

My code:

import Donut from 'vue-css-donut-chart';

const app = createApp();
app.use(Donut);

...

<template>
  <vc-donut total="35">
    Test donut
  </vc-donut>
</template>

How to create custom tooltip/label in the chart

Im facing issue with this package is that it do not offers any way to create custom tooltip if there is any way can you assists me :) Btw its wonderful package to use donut chart having good options and easy to use too

Invalid prop "sections" error

when I update data(this is prop to donut-chart) I got the error like this:

//-------------------------------------------------- error here
app.js:55270 [Vue warn]: Invalid prop: custom validator check failed for prop "sections".

found in

---> at Donut.vue
at resources/assets/js/components/result/HospitalPieStatus.vue

//-------------------------------------------------- error here

this data is Array so I don't understand what's wrong
this(below) is my data and I use v-for statment this object and insert each array
to donut chart as prop

hospitalStatusNew2:Object
1:Array[3]
2:Array[3]
3:Array[3]
4:Array[3]

So I'm wondering if is this useful and right for dynamic data change and rendering

How to use with nuxt?

I try use with nuxt, but don't worked!

I'm importing inside of the component

index.vue

<template>
<vc-donut :sections="sections">Basic donut</vc-donut> 
< /template>

<script>
import Donut from "vue-css-donut-chart";
import "vue-css-donut-chart/dist/vcdonut.css";
Vue.use(Donut);

data() {
      return {
        sections: [{ value: 25 }, { value: 25 }]
      };
    }
</script>

When unit is set to %, diagram breaks.

Using In-browser resource

<script src="https://unpkg.com/vue-css-donut-chart"></script>

When unit is set to %, diagram breaks.

Had to revert to 1.0.2 version.

Way to import the component directly to a SFC? Donut is undefined

Hi, first of - thank you for this amazingly looking, adequately simple component! Good job.

What I would like to do: I want to import and use the donut component similarly as I do with all of my own - just an import from a node_module, mentioning it as a component in the "components" field of Vue instance and then putting it into the template, passing props - similarly to the way it is done in this repo's demo page. I am using SFCs and TS.

What happened: Of course, an yarn add. If it should work out of the box as the above, then it did not on my end - I will gladly investigate the reasons further if that's the case. As I want the app to be self contained, I picked the ES6 method from the docs as the most adequate.

// main.ts

import Donut from 'vue-css-donut-chart';
import App from './App.vue';
import router from './router';
import store from './store/store';
import './registerServiceWorker';
import 'vue-css-donut-chart/dist/vcdonut.css';

Vue.use(Donut);

Vue.config.productionTip = false;

new Vue({
	router,
	store,
	render: (h) => h(App)
}).$mount('#app');

Imported the Donut and its styles inside my main.ts, did Vue.ues(Donut) before the new Vue is called and expected miracles, assuming it would be available from anywhere in my app. Of course it wasn't, referring to the Donut in the "components" vue instance field returns undefined without an explicit import into my SFC, which in turn seems contradictory to already importing it in the root instance. The way of importing the component into the SFC was not clear to me, too.

Vue docs suggest the way to enable access to the .vue file directly through the "browser" field in package.json, but it is not implemented it the package.

I thinks I am missing a piece in my Vue knowledge and I would very much appreciate someone enlightening me on the way to use this beautiful component in a vue-cli generated project.

Cheers 🍻

Animation

Hey excellent css donut chart!

I think the only thing it is missing is an animation for when the chart loads. Most donut charting libraries will bring the donut chart in slowly around the radius of the chart.

Anyway you could implement this quickly? I'm strapped for time but if you are also strapped for time I can submit a PR.

Best regards.

How can i use a variable to set the value?

I'm trying to set a chart, and the value of this chart is inside a variable, but i'm facing errors.
The variable is a float and is a valid value like "26.47".

Can please anyone can help me solve this issue?

Using Vue: 2.6.11, vue-css-donut-chart: 1.3.0 with vuetify 2.4.0;
image
image

using start-angle prop causes artifact to appear on chart

(using Chrome 83)

image
As you can see in the upper right hand corner an artifact appears in the green section.

locally, my fix is to not use the start-angle prop, and instead override the .cdc class using translateZ

example:
transform: translateZ(1px) rotate(200deg);

Sum of all the sections' values (100.00000000000001) should not exceed `total` (100)"

I recently stumbled upon this error. If I pass exactly this array to the chart receive this error
doesntWork =[{color: 'red', value:8.2}, {color: 'green', value:34.97}, {color: 'blue', value:30.6}, {color: 'yellow', value:26.23}]
But if I switch order of the array it works.
works = [{color: 'green', value:34.97}, {color: 'red', value:26.23}, {color: 'yellow', value:8.2}, {color: 'blue', value:30.6}, ]

HTML in text field

Wouldn't it be better practice to have a field asking for enabling HTML and then enabling the use of v-html. than comparing the input format to be HTML and run a v-html to DonutHTML?

Vue 3 usage

I tried using your package with vue 3, but it doesn't work properly
How do I set it up for vue 3

This is my code buh didn't work

import { createApp } from 'vue'
import App from './App.vue'
import Donut from 'vue-css-donut-chart';
import 'vue-css-donut-chart/dist/vcdonut.css';

const app = createApp(App)
app.use(Donut)
app.mount('#app')

Center - Transparent Background

Would be great if the center of the donut had a transparent background. Haven't looked through the code for how it is currently implemented. Using this inside a striped table and want the table color to come through.

No tests?

I've stumbled upon this component but I was wondering if there was any intention to add any test coverage to the code base

Bug with UI

Chart sections from the example below consists of 12 items with value equal to 8.33 for each. 8.33 * 12 = 99.96 For some reason though this is breaking UI in the middle of the chart (180 deg).
See https://jsfiddle.net/0g54jzwv/12/

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.