亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法在“CanvasRenderingContext2D”上執行

無法在“CanvasRenderingContext2D”上執行

烙印99 2022-07-15 09:49:18
我正在嘗試在我的 chartJs 圖表的插件下創建一個線性漸變。不幸的是,我收到一個名為的錯誤:無法在“CanvasRenderingContext2D”上執行“createLinearGradient”:提供的雙精度值是無限的。我正在嘗試在插件中添加我的 linearGradient,因為我希望該漸變在每個比例上對齊。這是我在下面嘗試的 barChart = new Chart(elem, {            plugins: [                {                  id: "responsiveGradient",                  afterLayout: function(chart, options) {                   var scales = chart.scales;                    var color = chart.ctx.createLinearGradient(                       scales["x-axis-0"].left,                      scales["y-axis-0"].bottom,                      scales["x-axis-0"].right,                      scales["y-axis-0"].top                      );                    // add gradients stops                    color.addColorStop(0, "black");                    color.addColorStop(0.25, "red");                    color.addColorStop(0.5, "orange");                    color.addColorStop(0.75, "yellow");                    color.addColorStop(1, "green");                    // changes the background color option                    chart.data.datasets[0].backgroundColor = color;                   }                }              ],            type: 'horizontalBar',            data: datasets,            options: {                maintainAspectRatio: false,                tooltips: { enabled: false },                title: {                  display: false,                },                responsive: true,                legend: {                  display: false,                  position: "top"                },                scales: {                  xAxes: [{                    ticks: {                      beginAtZero: false,                      min:0.5,                      max:0.8,                      maxTicksLimit: 6,                    },
查看完整描述

1 回答

?
呼喚遠方

TA貢獻1856條經驗 獲得超11個贊

問題出在plugins.afterLayout函數的最后一行。沒有諸如chart.data, usechart.config.data之類的對象。


// chart.data.datasets[0].backgroundColor = color;  // replace this line

chart.config.data.datasets[0].backgroundColor = color;

請在下面查看您修改后的代碼(我不得不對您的數據做出假設)。


const datasets = {

  labels: ['A', 'B', 'C'],

  datasets: [{

    label: 'data',

    data: [0.6, 0.7, 0.8],

    barThickness: 5

  }]

};


new Chart("myChart", {

  plugins: [{

    id: "responsiveGradient",

    afterLayout: (chart, options) => {

      var scales = chart.scales;

      var color = chart.chart.ctx.createLinearGradient(

        scales["x-axis-0"].left,

        scales["y-axis-0"].bottom,

        scales["x-axis-0"].right,

        scales["y-axis-0"].top

      );

      // add gradients stops

      color.addColorStop(0, "black");

      color.addColorStop(0.25, "red");

      color.addColorStop(0.5, "orange");

      color.addColorStop(0.75, "yellow");

      color.addColorStop(1, "green");

      // changes the background color option

      chart.config.data.datasets[0].backgroundColor = color;

    }

  }],

  type: 'horizontalBar',

  data: datasets,

  options: {

    maintainAspectRatio: false,

    tooltips: {

      enabled: false

    },

    title: {

      display: false,

    },

    responsive: true,

    legend: {

      display: false,

      position: "top"

    },

    scales: {

      xAxes: [{

        ticks: {

          beginAtZero: false,

          min: 0.5,

          max: 0.8,

          maxTicksLimit: 6,

        },

        scaleLabel: {

          display: false

        },            

        gridLines: {

          display: false,

          zeroLineColor: "transparent",

        }

      }],

      yAxes: [{

        ticks: {

          maxTicksLimit: 6,

          padding: 15,

        },

        gridLines: {

          drawTicks: false,

          display: false

        }

      }]

    }

  }

});

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>

<canvas id="myChart" height="100"></canvas>


查看完整回答
反對 回復 2022-07-15
  • 1 回答
  • 0 關注
  • 455 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號