簡體   English   中英

CSS(或HTML5 / SVG)中的單圈“圖表”到甜甜圈(圓環)圖表

[英]Single bar “chart” to donut (doughnut) chart in CSS (or HTML5/SVG)

我正在尋找將單條“圖表”轉換為圓環圖的最快/最簡單/最輕的方法。 目前數據位於div中,div由三個span元素組成,包含來自Typescript和mongoDB后端的數據。 在這種情況下,只使用CSS可以制作響應式圓環圖嗎?

這有點現在如何顯示數據: http//www.dailydoseofexcel.com/blogpix/barpie5.gif

這正是我想要實現的目標: http//www.teylyn.com/wp-content/uploads/2010/03/donut-leader-06.gif

基本上這是HTML中的結構:

<div>
<span>{{data1}}</span>
<span>{{data2}}</span>
<span>{{data3}}</span>
</div>

如果需要,我會添加更多(確切的)代碼示例,謝謝。 編輯:

代碼更具體地說,HTML:

<div class="bar-chart-container">
<span class="bar-data1">{{data1.percentage}}</span>
<span class="bar-data2">{{data2.percentage}}</span>
<span class="bar-data3">{{data3.percentage}}</span>
</div>

CSS:

.bar-chart-container {
  display: block;
}
.bar-chart {
  display: inline-block;
  font-size: 0.9em;
  white-space: normal;
}
.bar-chart.bar-data1 {
  background-color: green;
}
.bar-chart.bar-data2 {
  background-color: red;
}
.bar-chart.bar-data3 {
  background-color: gray;
 <div class="card">
   <div class="donut-chart chart1">
    <div class="slice one"></div>
    <div class="slice two"></div>
    <div class="chart-center">
      <span>{{data1}}</span>
      <span>{{data2}}</span>
    </div>
   </div>
 </div>

然后是CSS

* {
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

body {
  font-family: sans-serif;
  background: #e1e1e1;
}

.card {
  float: left;
  background: #fff;
  padding: 20px;
  margin: 0 20px 0 0;
}


// Donut Chart Mixin
.donut-chart {
  position: relative;
  border-radius: 50%;
  overflow: hidden;

     .slice {
         position: absolute;
          top: 0;
          left: 0;
          width: 100%;
         height: 100%;
  }

  .chart-center {
    position: absolute;
    border-radius: 50%;

    span {
      display: block;
      text-align: center;
    }
  }
}

@mixin donut-chart($name, $perc, $size, $width, $base, $center, $color,        $textColor: $color, $textSize: 40px) {

  $color2: $color;
  $base2: $base;
    $deg: ($perc/100*360)+deg;
    $deg1: 90deg;
    $deg2: $deg;

// If percentage is less than 50%
@if $perc < 50 {
    $base: $color;
$color: $base2;
$color2: $base2;
$deg1: ($perc/100*360+90)+deg;
    $deg2: 0deg;
}

.donut-chart {
   &#{$name} {
   width: $size;
   height: $size;
   background: $base;

  .slice {
    &.one {
      clip: rect(0 $size $size/2 0);
      -webkit-transform: rotate($deg1);
      transform: rotate($deg1);
      background: $color;
    }

    &.two {
      clip: rect(0 $size/2 $size 0);
      -webkit-transform: rotate($deg2);
      transform: rotate($deg2);
      background: $color2;
    }
  }

  .chart-center {
    top: $width;
    left: $width;
    width: $size - ($width * 2);
    height: $size - ($width * 2);
    background: $center;

    span {
      font-size: $textSize;
      line-height: $size - ($width * 2);
      color: $textColor;

      &:after {
      content: '#{$perc}%';
         }
        }
       }
     }
   }
} // mixin

// Charts
@include donut-chart('.chart1', 75, 200px, 10px, #e1e1e1, #fff, #50c690);

@include donut-chart('.chart2', 91, 200px, 25px, #e1e1e1, #fff, #48b2c1);

@include donut-chart('.chart3', 15, 120px, 5px, #e1e1e1, #fff, #353535);

@include donut-chart('.chart4', 45, 240px, 15px, #e1e1e1, #fff, #f26a4a, #f26a4a, 60px);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM