简体   繁体   English

具有可变百分比的CSS3饼图

[英]CSS3 pie chart with variable percentage

In HTML, I would like to do something like this: 在HTML中,我想做这样的事情:

<div class="thermometer">
  <div class="circle purple">
    <div class="pie-piece percent=75%">
    </div>
  </div>
</div>

where the result would be a 75% filled-in purple circle. 结果将是75%填充紫色圆圈。 (same thing as a pizza cut into 4 pieces, and one piece is missing) (The 75 comes from a database and must not be in the CSS) (比萨饼切成4块,一块丢失)(75来自数据库,不得在CSS中)

My CSS for circle is this: 我的圈子CSS是这样的:

.thermometer .circle {
    position: absolute;
    width:26px;
    height:26px;
    -moz-border-radius:13px;
    -webkit-border-radius:13px;
    border-radius:13px;
    border: 1px solid #000000;
}
.thermometer .green { background-color: green; }
(other colors)

I looked at examples using "hold" and "clip" property, but didn't understand how to do it with a variable. 我查看了使用“hold”和“clip”属性的示例,但不明白如何使用变量。 How can I write CSS for "pie-piece"? 如何为“馅饼”编写CSS?

If you are referring to this tutorial, the "Hold" and "pieSlice1" are just the names of the class & ID. 如果您指的是教程,则“Hold”和“pieSlice1”只是类和ID的名称。

You could predefine aa degree then use jQuery and change the CSS depending on what you get from the database. 您可以预先定义一个学位然后使用jQuery并根据您从数据库获得的内容更改CSS。 Check out this post for some more information. 查看帖子了解更多信息。

.css({ WebkitTransform: 'rotate(' + degree + 'deg)'});

http://jsfiddle.net/t7zLP/1/ http://jsfiddle.net/t7zLP/1/

Took it seriously, so answering with bit delay. 认真对待它,所以回答有点延迟。

First, possibly something like this could be done better using canvas drawing (need check it), but just for fun made crazy mix with LESS+HTML+CSS3 and bit JavaScript which seems work anyway. 首先,可能使用画布绘图(需要检查)可以更好地完成这样的事情,但只是为了有趣的疯狂混合LESS + HTML + CSS3和位JavaScript似乎仍然有效。

Here is working example https://c9.io/dmi3y/css3pie/workspace/index.html Should works with all modern browsers, and IE9 +, possibly you could add support to IE versions lower than 9. For this needed support for border radius and transforms. 这是工作示例https://c9.io/dmi3y/css3pie/workspace/index.html应该适用于所有现代浏览器和IE9 +,可能你可以添加对低于9的IE版本的支持。这需要支持边框半径和变换。 Latter one could be done with Matrix filter, and here is something to read plus working solution in .htc file http://samuli.hakoniemi.net/cross-browser-rotation-transformation-with-css/ , but I do not tested it and personally would not care about older browsers much. 后者可以使用Matrix过滤器完成,这里有一些内容可以阅读.htc文件http://samuli.hakoniemi.net/cross-browser-rotation-transformation-with-css/中的工作解决方案,但我没有测试过它和个人不会太在意旧浏览器。 I would just add notice about if you want see something upgrade browser. 我想添加一些关于你是否想看到升级版浏览器的通知。 Also in FireFox there is some artefacts, possibly because I am using pseudo classes ::before and ::after and may be using real elements will improve this. 同样在FireFox中有一些人工制品,可能是因为我使用伪类::before::after并且可能正在使用真实元素将改善这一点。

Tech information. 技术信息。 Here is 9cloud code https://c9.io/dmi3y/css3pie . 这是9cloud代码https://c9.io/dmi3y/css3pie The core idea is using dynamically generated CSS using client side LESS. 核心思想是使用客户端LESS动态生成CSS。 So with bit jQuery code which is taken for convenience and easily could be converted to any other library/core js. 所以使用bit jQuery代码是为了方便起见而且很容易转换为任何其他库/核心js。

Mile stones: 英里石头:

in your markup you define how many area should be filled in degrees 在您的标记中,您可以定义应以度为单位填充的区域数

<div class="circle" id="pieOne" data-fill="30deg"></div> 

and basically to make it works this is everything you need. 并且基本上使它工作这是你需要的一切。

This is brief explanation what's going on: 这是简要说明发生了什么:

Taking this value I do override LESS variables in embed styles and create CSS with less.refreshStyles() 取这个值我会在嵌入样式中覆盖LESS变量并使用less.refreshStyles()创建CSS

<style type="text/less" id="lessPie">
    @import 'styles/pie';
    @fillDegree: #dataDegree#; // from 0deg to 180deg
    @baseRotate: 40deg;
</style>
<script type="text/javascript">
    !function(){
        var lessPieText = $('#lessPie').text();
        $(function(){
            var pieOneDataFill = $('#pieOne').attr('data-fill');
            while (parseInt(pieOneDataFill) > 180) {
                pieOneDataFill = (parseInt(pieOneDataFill) - 180) + 'deg';
            }
            while (parseInt(pieOneDataFill) < 0) {
                pieOneDataFill = (parseInt(pieOneDataFill) + 180) + 'deg';
            }
            $('#lessPie').text(lessPieText.replace('#dataDegree#', pieOneDataFill));
            less.refreshStyles();

            // update % value
            // 180deg         = 100%
            // pieOneDataFill = x%

            var percentValue = (parseInt(pieOneDataFill) * 100) / 180;
            $('#pieOneLegend').find('span').text(Math.floor(percentValue) + '%').end().show();

        });
    }()
</script>

As additional bonus you may rotate pie with @baseRotate: 40deg; 作为额外奖励,您可以使用@baseRotate: 40deg;旋转馅饼@baseRotate: 40deg; value. 值。 Also shown legend with % value. 还显示了带%值的图例。

That's pretty much everything. 这几乎就是一切。 For a while it support only one pie per page (or rather one type of pie) with one value. 有一段时间它只支持每页一个馅饼(或者更确切地说是一种馅饼),只有一个值。 I will push this on github later and probably will work on project as it sounds to become interesting. 我稍后会在github上推送它,并且可能会对项目起作用,因为它听起来很有趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM