【问题标题】:Creating half circle chart filled with gradient创建填充渐变的半圆图
【发布时间】:2017-06-13 16:44:02
【问题描述】:

我设法创建了带边框的简单图表。我想将简单的 border-color 更改为渐变。我试图使用 border-imagebackground 属性,但似乎不可能弯曲它以适应容器的拱形。 有没有办法在 css3 中实现这种效果?

HTML

<div class="pie">
    <span class="overlay"></span>
</div>

CSS

.pie {
    margin: 0 auto;
    position: relative;
    width: 116px;
    height: 58px;
    overflow: hidden;
}

.pie *,
.pie::before {
    box-sizing: border-box;
}

.pie::before {
    content: '';
    width: inherit;
    height: inherit;
    border: 20px solid grey;
    border-bottom: none;
    border-top-left-radius: 175px;
    border-top-right-radius: 175px;
    position: absolute;
    left:0;
}

.pie .overlay{
    position: absolute;
    top: 100%;
    left: 0;
    width: inherit;
    height: inherit;
    border: 20px solid;
    border-top: none;
    border-bottom-left-radius: 175px;
    border-bottom-right-radius: 175px;
    transform-origin: 50% 0;
    border-color:yellow;/* background: linear-gradient(to right, rgba(228,232,7,1) 0%, rgba(0,218,156,1) 100%); */
    transform: rotate(90deg); 
}

【问题讨论】:

标签: html css gradient pie-chart border-color


【解决方案1】:

如果你使用pie的伪和overlay作为白色中心,你可以这样做

.pie {
    margin: 0 auto;
    position: relative;
    width: 200px;
    height: 100px;
    border-radius: 200px 200px 0 0;
    overflow: hidden;
}
.pie::after {
    transform: rotate(-60deg);      /*  set rotation degree  */
    background: linear-gradient(to right, rgba(228,232,7,1) 0%, rgba(0,218,156,1) 100%);
    transform-origin: center bottom;
}
.pie::before {
    border: 20px solid grey;
}
.pie .overlay{
    top: 20px;                      /*  match border width  */
    left: 20px;                     /*  match border width  */
    width: calc(100% - 40px);       /*  match border width times 2  */
    height: calc(200% - 40px);      /*  match border width times 2  */
    border-radius: 100%;
    background: white;
    z-index: 1;                     /*  move it on top of the pseudo elements  */
}
.pie *,
.pie::before,
.pie::after {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    width: 100%;
    border-radius: inherit;
    box-sizing: border-box;
}
<div class="pie">
    <span class="overlay"></span>
</div>

【讨论】:

  • 是的,这正是我想要的。
【解决方案2】:

您可以使用 2 个渐变,内部遮盖外部:

.test {
    width: 200px;
    height: 100px;
    background-image: linear-gradient(white, white), linear-gradient(to left, red, green);
    background-clip: padding-box, border-box;
    background-origin: padding-box, border-box; 
    border: solid 50px transparent;
    border-radius: 200px 200px 0px 0px;
    border-width: 50px 50px 0px 50px;
}
    
&lt;div class="test"&gt;&lt;/div&gt;

.test {
    width: 200px;
    height: 100px;
    background-image: linear-gradient(white, white);
    background-clip: content-box;
    background-origin: content-box; 
    border: solid 50px transparent;
    border-radius: 200px 200px 0px 0px;
    padding: 50px 50px 0px 50px;
    position: relative;
    overflow: hidden;
}

.test:after {
    content: "";
    position: absolute;
    left: -25%;
    top: -50%;
    right: 0px;
    height: 300%;
    width: 150%;
    background-image: linear-gradient(to left, red, green);
    transform: rotate(0deg);
    transform-origin: center center;
    z-index: -1;
    animation: spin 3s infinite;
}

@keyframes spin {
    from {transform: rotate(0deg);}
    to {transform: rotate(360deg);}
}
&lt;div class="test"&gt;&lt;/div&gt;

【讨论】:

    【解决方案3】:

    您可以在.pie::after)上添加一个额外的伪元素,将它们定位为在其左上角和右上角重叠,将它们都弯曲,然后使用background 进行渐变。

    然后,将span 定位在.pie 的中心,并给它一个白色背景(而不是透明)和更高的z-index,以确保圆弧的中心保持完整。

    div {
      width: 200px;
      height: 200px;
      position: relative;
    }
    div::before {
      content: "";
      display: block;
      position: absolute;
      top: 0;
      background: linear-gradient(to right, azure, slategray);
      width: 100px;
      height: 100px;
      left: 0;
      border-top-left-radius: 100px;
    }
    div::after {
      content: "";
      display: block;
      position: absolute;
      top: 0;
      background: linear-gradient(to right, midnightblue, steelblue);
      width: 100px;
      height: 100px;
      right: 0;
      border-top-right-radius: 100px;
    }
    div span {
      display: block;
      position: absolute;
      left: calc(50% - 50px);
      top: calc(50% - 50px);
      background: white;
      width: 100px;
      height: 100px;
      border-radius: 50%;
      z-index: 1;
    }
    <div>
      <span></span>
    </div>

    【讨论】:

    • 是的,但这只是一个填充整个半圆的曲线渐变。使用点“变换:旋转(90度);”就是给不同的图表赋予不同的值来覆盖主容器。
    • 其实是两个不同渐变的部分。我只是让两个渐变彼此相对,使它们看起来像一个大的。我已经更新了颜色以显示这两个部分。如果这不是您想要的,那么最好在您的帖子中包含您想要的结果的图片。
    • 它必须更加动态。我需要用不同大小填充这个叠加渐变,比如 80%、25%、30%(半个馅饼),我按 180deg=100% 计算。
    • 你可以用更复杂的渐变来完成类似的事情,比如linear-gradient(125deg, red, red 30%, blue 30%, blue 60%, gold 60% gold),但是碎片的形状不会像你想要的那样是一个很好的楔形。需要更复杂的标记。
    • 听起来你真正想要的是一个饼图,你可以覆盖它的下半部分,然后在上面放置一个圆形 div 来制作你想要的弧形。 See here about pie charts。此外,最好根据所有这些要求更新您的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-16
    • 2010-12-07
    相关资源
    最近更新 更多