【问题标题】:Slices in CSS pie chart are offsetCSS饼图中的切片是偏移的
【发布时间】:2014-06-24 22:41:23
【问题描述】:

我正在尝试使用 CSS 创建半个饼图。它看起来很不错,但是切片稍微偏移了一些东西。正如您在CodePen example 中看到的那样,前面的切片比后面的切片延伸得更远。为什么?

<div class="container">
    <div class="slice slice1"></div>
    <div class="slice slice2"></div>
    <div class="slice slice3"></div>
</div>

还有 CSS:

.container {
   width: 300px;
   height: 300px;
   margin-left: 50px;
   position: absolute;
   clip: rect(0px, 300px, 150px, 0px);
}

.slice {
  width: 300px;
  height: 150px;
  position: absolute;
  border-radius: 300px 300px 0px 0px;
  -webkit-transform-origin: center bottom;
}

.slice1 { 
  background-color: red;
  -webkit-transform:rotate(0deg);
  -moz-transform:rotate(0deg);
  transform:rotate(0deg);
}
.slice2 { 
  background-color: green;
  -webkit-transform:rotate(11deg);
  -moz-transform:rotate(11deg);
  transform:rotate(11deg);
}
.slice3 { 
  background-color: yellow;
  -webkit-transform:rotate(73deg);
  -moz-transform:rotate(73deg);
  transform:rotate(73deg);
}

【问题讨论】:

    标签: css pie-chart


    【解决方案1】:

    我找到了一个CSS饼图代码,刚刚修改成半圆

    HTML

    <div id="piechart">
       <div class="pie" data-start="0" data-value="30"></div>
       <div class="pie highlight" data-start="30" data-value="30"></div>
       <div class="pie" data-start="60" data-value="40"></div>
       <div class="pie big" data-start="100" data-value="260"></div>
    </div>
    

    CSS

    #piechart {
      position:absolute;
      clip:rect(0px, 251px, 100px, 0px);
    }
    
    /* 
      make each pie piece a rectangle twice as high as it is wide.
      move the transform origin to the middle of the left side.
      Also ensure that overflow is set to hidden.
    */
      .pie {
            position:absolute;
            width:100px;
            height:200px;
            overflow:hidden;
            left:150px;
            -moz-transform-origin:left center;
            -ms-transform-origin:left center;
            -o-transform-origin:left center;
            -webkit-transform-origin:left center;
            transform-origin:left center;
        }
    /*
      unless the piece represents more than 50% of the whole chart.
      then make it a square, and ensure the transform origin is
      back in the center.
    
      NOTE: since this is only ever a single piece, you could
      move this to a piece specific rule and remove the extra class
    */
        .pie.big {
            width:200px;
            height:200px;
            left:50px;
            -moz-transform-origin:center center;
            -ms-transform-origin:center center;
            -o-transform-origin:center center;
            -webkit-transform-origin:center center;
            transform-origin:center center;
        }
    /*
      this is the actual visible part of the pie. 
      Give it the same dimensions as the regular piece.
      Use border radius make it a half circle.
      move transform origin to the middle of the right side.
      Push it out to the left of the containing box.
    */
        .pie:BEFORE {
            content:"";
            position:absolute;
            width:100px;
            height:200px;
            left:-100px;
            border-radius:100px 0 0 100px;
            -moz-transform-origin:right center;
            -ms-transform-origin:right center;
            -o-transform-origin:right center;
            -webkit-transform-origin:right center;
            transform-origin:right center;
    
        }
     /* if it's part of a big piece, bring it back into the square */
        .pie.big:BEFORE {
            left:0px;
        }
    /* 
      big pieces will also need a second semicircle, pointed in the
      opposite direction to hide the first part behind.
    */
        .pie.big:AFTER {
            content:"";
            position:absolute;
            width:100px;
            height:200px;
            left:100px;
            border-radius:0 100px 100px 0;
        }
    /*
      add colour to each piece.
    */
        .pie:nth-of-type(1):BEFORE,
        .pie:nth-of-type(1):AFTER {
            background-color:blue;  
        }
        .pie:nth-of-type(2):AFTER,
        .pie:nth-of-type(2):BEFORE {
            background-color:green; 
        }
        .pie:nth-of-type(3):AFTER,
        .pie:nth-of-type(3):BEFORE {
            background-color:red;   
        }
        .pie:nth-of-type(4):AFTER,
        .pie:nth-of-type(4):BEFORE {
            background-color:orange;    
        }
    /*
      now rotate each piece based on their cumulative starting
      position
    */
        .pie[data-start="30"] {
            -moz-transform: rotate(30deg); /* Firefox */
            -ms-transform: rotate(30deg); /* IE */
            -webkit-transform: rotate(30deg); /* Safari and Chrome */
            -o-transform: rotate(30deg); /* Opera */
            transform:rotate(30deg);
        }
        .pie[data-start="60"] {
            -moz-transform: rotate(60deg); /* Firefox */
            -ms-transform: rotate(60deg); /* IE */
            -webkit-transform: rotate(60deg); /* Safari and Chrome */
            -o-transform: rotate(60deg); /* Opera */
            transform:rotate(60deg);
        }
        .pie[data-start="100"] {
            -moz-transform: rotate(100deg); /* Firefox */
            -ms-transform: rotate(100deg); /* IE */
            -webkit-transform: rotate(100deg); /* Safari and Chrome */
            -o-transform: rotate(100deg); /* Opera */
            transform:rotate(100deg);
        }
    /*
      and rotate the amount of the pie that's showing.
    
      NOTE: add an extra degree to all but the final piece, 
      to fill in unsightly gaps.
    */
        .pie[data-value="30"]:BEFORE {
            -moz-transform: rotate(31deg); /* Firefox */
            -ms-transform: rotate(31deg); /* IE */
            -webkit-transform: rotate(31deg); /* Safari and Chrome */
            -o-transform: rotate(31deg); /* Opera */
            transform:rotate(31deg);
        }
        .pie[data-value="40"]:BEFORE {
            -moz-transform: rotate(41deg); /* Firefox */
            -ms-transform: rotate(41deg); /* IE */
            -webkit-transform: rotate(41deg); /* Safari and Chrome */
            -o-transform: rotate(41deg); /* Opera */
            transform:rotate(41deg);
        }
        .pie[data-value="260"]:BEFORE {
            -moz-transform: rotate(260deg); /* Firefox */
            -ms-transform: rotate(260deg); /* IE */
            -webkit-transform: rotate(260deg); /* Safari and Chrome */
            -o-transform: rotate(260deg); /* Opera */
            transform:rotate(260deg);
        }
    /*
    NOTE: you could also apply custom classes (i.e. .s0 .v30)
    but if the CSS3 attr() function proposal ever gets implemented,
    then all the above custom piece rules could be replaced with
    the following:
    
    .pie[data-start] {
       transform:rotate(attr(data-start,deg,0);
    }
    .pie[data-value]:BEFORE {
       transform:rotate(attr(data-value,deg,0);
    }
    */
    


    饼图代码来自http://codepen.io/AtomicNoggin/pen/fEish

    【讨论】:

    • 谢谢,但我很好奇我的例子中的效果。
    • 你对哪个效果很好奇?
    • 切片与像素不完全匹配。您可以在我提供的 Codepen 示例中看到效果。
    • 您是在谈论黄色切片上边缘的绿色和红色像素吗?
    • 您是否可以获取我在上面发布的现有饼图代码并使用它?只需更改 html 中的 data-start 和 data-value 值以及饼图本身的大小。它最终会为您省去很多麻烦
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多