How to create a pie chart with css

王林
Release: 2020-11-06 16:14:35
forward
2188 people have browsed it

How to create a pie chart with css

Foreword:

At work, sometimes we have the need for pie charts, such as statistical charts, progress indicators, timers, etc., of course, the implementation method There are many, and there are even ready-made js libraries that can be used directly. Out of interest, I used pure CSS to implement the pie chart myself and see how it works.

(Video tutorial recommendation:css video tutorial)

Copy after login
.pie { width: 100px; height: 100px; border-radius: 50%; background: yellowgreen; background-image: linear-gradient(to right, transparent 50%, #655 0); } .pie::before { content: ''; display: block; margin-left: 50%; height: 100%; border-radius: 0 100% 100% 0/50%; background-color: inherit; transform-origin: left; transform: rotate(.2turn); }
Copy after login

Effect:

How to create a pie chart with css

Change rotate The angle of the pie chart can achieve the effect of pie charts of different sizes, but readers who have tried it themselves will find that rotating more than 50% will not show the ideal effect, but will show the following effect:

How to create a pie chart with css

To solve this problem, change the background color of the pseudo element. When the area that needs to be displayed is more than half, changing the background color is OK.

The author below shows a simple example of a progress indicator. If readers try it with their own hands, they will find a constantly changing pie chart effect.

.pie { width: 100px; height: 100px; border-radius: 50%; background: yellowgreen; background-image: linear-gradient(to right, transparent 50%, #655 0); } .pie::before { content: ''; display: block; margin-left: 50%; height: 100%; border-radius: 0 100% 100% 0/50%; background-color: inherit; transform-origin: left; animation: spin 3s linear infinite,bg 6s step-end infinite; } @keyframes spin{ to{transform: rotate(.5turn);} } @keyframes bg{ 50%{background: #655;} }
Copy after login

The effect is as follows:

How to create a pie chart with css

#Readers are best to try it out with their own hands, try to modify the properties, and understand the meaning. Only when you understand can you do it. Come up with something more interesting. If readers are interested in svg, they can try to use svg to implement pie charts. The effect achieved by svg is more colorful.

Recommended tutorial:CSS tutorial

The above is the detailed content of How to create a pie chart with css. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!