<tfoot id='I4olg'></tfoot>

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

    • <bdo id='I4olg'></bdo><ul id='I4olg'></ul>
    <legend id='I4olg'><style id='I4olg'><dir id='I4olg'><q id='I4olg'></q></dir></style></legend>

      如何在 chart.js 中应用渐变颜色?

      How to apply gradient color in chart.js?(如何在 chart.js 中应用渐变颜色?)

        <bdo id='dZGRl'></bdo><ul id='dZGRl'></ul>
          <tbody id='dZGRl'></tbody>

      • <small id='dZGRl'></small><noframes id='dZGRl'>

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

              <tfoot id='dZGRl'></tfoot>
              • <legend id='dZGRl'><style id='dZGRl'><dir id='dZGRl'><q id='dZGRl'></q></dir></style></legend>
              • 本文介绍了如何在 chart.js 中应用渐变颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用 chart.js 库制作折线图,在这里我想将线条的颜色更改为渐变色.

                I am making a line chart using chart.js library and here I would like to change the color of the line to gradient color.

                我尝试过的代码,

                  const gradientFill = ctx.createLinearGradient(500, 0, 100, 0);
                  gradientFill.addColorStop(0, "#80b6f4");
                  gradientFill.addColorStop(1, "#f49080");
                

                并将其添加到数据集中,例如,

                And added it in dataSets like,

                datasets: [
                  {
                    .
                    .
                    .
                    .
                    backgroundColor: gradientFill,
                    .
                    .
                  }
                ]
                

                但我看不到变化,仍然只显示一些灰色线条而不是渐变.

                But I couldn't see the changes and still some gray color line only gets displayed instead of gradient.

                工作示例:

                尝试添加 borderColor : gradientFill 但这也不起作用.

                Tried adding borderColor : gradientFill but that also doesn't work.

                要求:要求是我需要有类似 https://i.stack.imgur.com/lKGqT.png

                请帮我把颜色改成渐变色.

                Kindly please help me to change the color to gradient color.

                推荐答案

                由于您的 useEffect 钩子,您的渐变不适用,在此钩子中您使用 formatData<再次设置数据/code> 函数,但只传递数据,没有 canvasGradient.所以你也需要通过它.

                Your gradient does not apply because of your useEffect hook, in this hook you set the data again with your formatData function but only pass the data and no canvasGradient. So you will need to pass that too.

                查看第 108 行和第 109 行,注释掉它可以正常工作,因此您需要在此处重新创建渐变或在更高级别上创建它,以便您只创建一次.https://codesandbox.io/s/chart-js-react-typescript-forked-dxlj7?file=/src/App.tsx

                See with lines 108 and 109 commented out it works fine, so you will need to either recreate the gradient there or create it on a higher level so you only create it once. https://codesandbox.io/s/chart-js-react-typescript-forked-dxlj7?file=/src/App.tsx

                更新为不同的方法,在您的 formatData 函数中为 backgroundColor 使用三元运算符来检查如果不采用当前 backgroundColor 是否给出画布渐变

                Updated to different method, in your formatData function for the backgroundColor use ternary operator to check if canvas gradient is given if not take the current backgroundColor

                这篇关于如何在 chart.js 中应用渐变颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
                CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                Ordinals in words javascript(javascript中的序数)
                  <tbody id='sPaXq'></tbody>
                <i id='sPaXq'><tr id='sPaXq'><dt id='sPaXq'><q id='sPaXq'><span id='sPaXq'><b id='sPaXq'><form id='sPaXq'><ins id='sPaXq'></ins><ul id='sPaXq'></ul><sub id='sPaXq'></sub></form><legend id='sPaXq'></legend><bdo id='sPaXq'><pre id='sPaXq'><center id='sPaXq'></center></pre></bdo></b><th id='sPaXq'></th></span></q></dt></tr></i><div id='sPaXq'><tfoot id='sPaXq'></tfoot><dl id='sPaXq'><fieldset id='sPaXq'></fieldset></dl></div>

                    • <bdo id='sPaXq'></bdo><ul id='sPaXq'></ul>

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

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

                        <tfoot id='sPaXq'></tfoot>