<i id='Gbb6Q'><tr id='Gbb6Q'><dt id='Gbb6Q'><q id='Gbb6Q'><span id='Gbb6Q'><b id='Gbb6Q'><form id='Gbb6Q'><ins id='Gbb6Q'></ins><ul id='Gbb6Q'></ul><sub id='Gbb6Q'></sub></form><legend id='Gbb6Q'></legend><bdo id='Gbb6Q'><pre id='Gbb6Q'><center id='Gbb6Q'></center></pre></bdo></b><th id='Gbb6Q'></th></span></q></dt></tr></i><div id='Gbb6Q'><tfoot id='Gbb6Q'></tfoot><dl id='Gbb6Q'><fieldset id='Gbb6Q'></fieldset></dl></div>
  • <small id='Gbb6Q'></small><noframes id='Gbb6Q'>

  • <legend id='Gbb6Q'><style id='Gbb6Q'><dir id='Gbb6Q'><q id='Gbb6Q'></q></dir></style></legend>

          <bdo id='Gbb6Q'></bdo><ul id='Gbb6Q'></ul>
        <tfoot id='Gbb6Q'></tfoot>

        ChartJS 甜甜圈图表渐变填充

        时间:2023-11-01
      1. <tfoot id='yaJt4'></tfoot>
          <bdo id='yaJt4'></bdo><ul id='yaJt4'></ul>
            <legend id='yaJt4'><style id='yaJt4'><dir id='yaJt4'><q id='yaJt4'></q></dir></style></legend>

            <i id='yaJt4'><tr id='yaJt4'><dt id='yaJt4'><q id='yaJt4'><span id='yaJt4'><b id='yaJt4'><form id='yaJt4'><ins id='yaJt4'></ins><ul id='yaJt4'></ul><sub id='yaJt4'></sub></form><legend id='yaJt4'></legend><bdo id='yaJt4'><pre id='yaJt4'><center id='yaJt4'></center></pre></bdo></b><th id='yaJt4'></th></span></q></dt></tr></i><div id='yaJt4'><tfoot id='yaJt4'></tfoot><dl id='yaJt4'><fieldset id='yaJt4'></fieldset></dl></div>

                <tbody id='yaJt4'></tbody>

                  <small id='yaJt4'></small><noframes id='yaJt4'>

                  本文介绍了ChartJS 甜甜圈图表渐变填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  所以我尝试为 ChartJS 圆环图制作渐变填充,但这仅适用于水平而不是圆形.

                  So I tried to make a gradient fill for the ChartJS doughnut chart, but this only works horizontal and not in a circle.

                  这是我正在使用的代码:

                  This is the code that I'm using:

                     var ctx = document.getElementById("chart-area").getContext("2d");
                  
                     var gradient1 = ctx.createLinearGradient(0, 0, 0, 175);
                     gradient1.addColorStop(0.0, '#ACE1DB');
                     gradient1.addColorStop(1.0, '#7FBDB9');
                  
                  
                     var gradient2 = ctx.createLinearGradient(0, 0, 400, 400);
                     gradient2.addColorStop(0, '#B5D57B');
                     gradient2.addColorStop(1, '#98AF6E');
                  
                     var gradient3 = ctx.createLinearGradient(0, 0, 0, 175);
                     gradient3.addColorStop(0, '#E36392');
                     gradient3.addColorStop(1, '#FE92BD');
                  
                     var gradient4 = ctx.createLinearGradient(0, 0, 0, 175);
                     gradient4.addColorStop(1, '#FAD35E');
                     gradient4.addColorStop(0, '#F4AD4F');
                  
                     /* ADD DATA TO THE DOUGHNUT CHART */
                     var doughnutData = [
                      {
                        value: 80,
                        color: gradient1,
                        highlight: "#E6E6E6",
                        label: "NUTRIENTS"
                      },
                      {
                        value: 20,
                        color:"#E6F1EE"
                  
                      },
                      {
                        value:50,
                        color: gradient2,
                        highlight: "#E6E6E6",
                        label: "PROTEINE"
                      },
                      {
                        value: 50,
                        color:"#E6F1EE"
                      },
                      {
                        value: 75,
                        color: gradient3,
                        highlight: "#E6E6E6",
                        label: "FETTE"
                      },
                      {
                        value:25,
                        color:"#E6F1EE"
                      },
                      {
                        value: 77,
                        color: gradient4,
                        highlight: "#E6E6E6",
                        label: "CARBS"
                      }
                      {
                        value: 23,
                        color:"#E6F1EE"
                      },
                     ];
                  

                  是否可以在半径上实现渐变,如本设计所示?

                  Is it possible to implement the gradient on a radius, as seen on this design?

                  谢谢!

                  推荐答案

                  在像圆环图这样的非线性路径上绘制线性渐变时,ChartJS 不会(正确)使用渐变填充颜色.线性渐变不会弯曲.

                  ChartJS will not (properly) use gradient fill colors when drawing a linear gradient on non-linear paths like your donut chart. A linear gradient does not curve.

                  可能性#1——使用径向渐变

                  您可以尝试使用径向渐变,看看结果是否符合您的设计需求.

                  You might experiment with a radial gradient and see if the results meets your design needs.

                  可能性 #2——使用渐变笔触(DIY 项目)

                  此外,画布的笔触会围绕一个圆圈弯曲.

                  Also, canvas's stroke will curve around a circle.

                  如果你想自己滚动"渐变圆环图,这里有示例代码和一个在圆形路径上使用渐变 strokeStyle 的演示(请参阅我之前的答案:画布中的角度渐变):

                  If you want to "roll-your-own" gradient donut chart, here's example code and a Demo that uses a gradient strokeStyle on a circular path (see my previous answer here: Angle gradient in canvas):

                  var canvas=document.getElementById("canvas");
                  var ctx=canvas.getContext("2d");
                  
                  function drawMultiRadiantCircle(xc, yc, r, radientColors) {
                    var partLength = (2 * Math.PI) / radientColors.length;
                    var start = 0;
                    var gradient = null;
                    var startColor = null,
                        endColor = null;
                  
                    for (var i = 0; i < radientColors.length; i++) {
                      startColor = radientColors[i];
                      endColor = radientColors[(i + 1) % radientColors.length];
                  
                      // x start / end of the next arc to draw
                      var xStart = xc + Math.cos(start) * r;
                      var xEnd = xc + Math.cos(start + partLength) * r;
                      // y start / end of the next arc to draw
                      var yStart = yc + Math.sin(start) * r;
                      var yEnd = yc + Math.sin(start + partLength) * r;
                  
                      ctx.beginPath();
                  
                      gradient = ctx.createLinearGradient(xStart, yStart, xEnd, yEnd);
                      gradient.addColorStop(0, startColor);
                      gradient.addColorStop(1.0, endColor);
                  
                      ctx.strokeStyle = gradient;
                      ctx.arc(xc, yc, r, start, start + partLength);
                      ctx.lineWidth = 30;
                      ctx.stroke();
                      ctx.closePath();
                  
                      start += partLength;
                    }
                  }
                  
                  var someColors = [];
                  someColors.push('#0F0');
                  someColors.push('#0FF');
                  someColors.push('#F00');
                  someColors.push('#FF0');
                  someColors.push('#F0F');
                  
                  drawMultiRadiantCircle(150, 150, 120, someColors);

                  body{ background-color: ivory; }
                  #canvas{border:1px solid red;}

                  <canvas id="canvas" width=300 height=300></canvas>

                  这篇关于ChartJS 甜甜圈图表渐变填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 vue 3 中使用 vue-chartjs:createElement 不是函数 下一篇:设置背景颜色以保存画布图表

                  相关文章

                  <legend id='orJhB'><style id='orJhB'><dir id='orJhB'><q id='orJhB'></q></dir></style></legend>

                    <i id='orJhB'><tr id='orJhB'><dt id='orJhB'><q id='orJhB'><span id='orJhB'><b id='orJhB'><form id='orJhB'><ins id='orJhB'></ins><ul id='orJhB'></ul><sub id='orJhB'></sub></form><legend id='orJhB'></legend><bdo id='orJhB'><pre id='orJhB'><center id='orJhB'></center></pre></bdo></b><th id='orJhB'></th></span></q></dt></tr></i><div id='orJhB'><tfoot id='orJhB'></tfoot><dl id='orJhB'><fieldset id='orJhB'></fieldset></dl></div>

                      <small id='orJhB'></small><noframes id='orJhB'>

                    1. <tfoot id='orJhB'></tfoot>
                      • <bdo id='orJhB'></bdo><ul id='orJhB'></ul>