为什么chartjs中的borderColor函数运行多次,如何减少?

Why is borderColor function in chartjs running multiple times, and how to reduce it?

在 chartjs 的图表中,函数 运行 多次(大约 25),我怎样才能减少它?

这里有一个fiddle安慰次数是运行:https://jsfiddle.net/abhishek_soni/38mfez7g/26/

代码如下:

var ctx = document.getElementById('myChart').getContext('2d');

let i=0, j=0, k=0; 

var myChart = new Chart(ctx, {
  type: 'line',
  data: {
    labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
    datasets: [{
      label: '# of Votes',
      data: [12, 19, 3, 5, 2, 3],
      backgroundColor: [
        'rgba(255, 99, 132, 0.2)',
        'rgba(54, 162, 235, 0.2)',
        'rgba(255, 206, 86, 0.2)',
        'rgba(75, 192, 192, 0.2)',
        'rgba(153, 102, 255, 0.2)',
        'rgba(255, 159, 64, 0.2)'
      ],
      borderColor: function() {
      ++i;
      console.log('why this bordercolor running this many times : ', i)
       return 'green'
      },
      borderWidth: 1
    }]
  },
  options: {
    responsive: function() {
      k++;
      console.log('why this option running this many times : ', k)
       return false
      },
    maintainAspectRatio: false,
    scales: {
      y: {
        beginAtZero: function() {
        j++;
        console.log('why this scale running this many times : ', j)
        return true
      }
      }
    }
  }
});
<canvas id="myChart" width="200" height="200"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.0/chart.min.js" integrity="sha512-asxKqQghC1oBShyhiBwA+YgotaSYKxGP1rcSYTDrB0U6DxwlJjU59B67U8+5/++uFjcuVM8Hh5cokLjZlhm3Vg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@2"></script>

正如我在 ChartJs 的文档中读到的那样,borderColor 应该是 Color 并且它是 Scribtable 属性,这意味着您可以传递一个带有上下文和选项的函数作为参数以具有灵活的 ui

因此每次上下文和选项更改时都应调用它。

你可以计算你的颜色值并传递它而不是传递函数。

每次图表需要绘制边框时都会调用 bordercolor 函数。如果您假设它会被调用一次,您可以创建自己的函数并为 bordercolor 赋予该函数的 return 值,而不是函数本身。

I don't know, why it's not recognizing variable gradient in jsfiddle

如果您正在尝试创建渐变,为什么调用此函数的次数在任何方面都很重要?我不认为你明白你在问什么。

这是你的渐变示例,假设这是你实际需要的:JSFiddle

var ctx = document.getElementById('myChart').getContext('2d');

let width, height, gradient;
function getGradient(ctx, chartArea) {
  const chartWidth = chartArea.right - chartArea.left;
  const chartHeight = chartArea.bottom - chartArea.top;
  if (gradient === null || width !== chartWidth || height !== chartHeight) {
    // Create the gradient because this is either the first render
    // or the size of the chart has changed
    width = chartWidth;
    height = chartHeight;
    gradient = ctx.createLinearGradient(0, chartArea.bottom, 0, chartArea.top);
    gradient.addColorStop(0, "rgb(54, 162, 235)");
    gradient.addColorStop(0.5, "rgb(255, 205, 86)");
    gradient.addColorStop(1, "rgb(255, 99, 132)");
  }

  return gradient;
}

var myChart = new Chart(ctx, {
  type: 'line',
  data: {
    labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
    datasets: [{
      label: '# of Votes',
      data: [12, 19, 3, 5, 2, 3],
      backgroundColor: [
        'rgba(255, 99, 132, 0.2)',
        'rgba(54, 162, 235, 0.2)',
        'rgba(255, 206, 86, 0.2)',
        'rgba(75, 192, 192, 0.2)',
        'rgba(153, 102, 255, 0.2)',
        'rgba(255, 159, 64, 0.2)'
      ],
      borderColor: function(context) {
        const chart = context.chart;
        const {ctx, chartArea} = chart;

        if (!chartArea) {
          // This case happens on initial chart load
          return null;
        }
        return getGradient(ctx, chartArea);
      },
      borderWidth: 1
     }]
  },
  options: {
    responsive: false,
    maintainAspectRatio: false,
    scales: {
      y: {
        beginAtZero: true,
      }
    },
  }
});

取自here.

我能够弄清楚,默认图表和 chartjs 文档的问题,一般来说,是它没有提到所有默认选项每次都是 运行,而且它没有当你在options中单独定义elements options时会发生,我们可以定义。

例如在折线图中,我们可以做options>elements>line>properties,这只会在绘制图表后运行显着提高效率并减少处理,也可以减少通过在图表上禁用动画(选项> 动画:false)运行 函数减半

所以在我的例子中,通过正确设置上述内容,我能够将 25 次 运行ning 功能带到 4 次 运行ning 功能。

这是更新后的 js fiddle:https://jsfiddle.net/abhishek_soni/38mfez7g/57/

此外,这是代码片段:

var ctx = document.getElementById('myChart').getContext('2d');

let i = 0,
  j = 0,
  k = 0;

var myChart = new Chart(ctx, {
  type: 'line',
  data: {
    labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
    datasets: [{
      label: '# of Votes',
      data: [12, 19, 3, 5, 2, 3],
      backgroundColor: [
        'rgba(255, 99, 132, 0.2)',
        'rgba(54, 162, 235, 0.2)',
        'rgba(255, 206, 86, 0.2)',
        'rgba(75, 192, 192, 0.2)',
        'rgba(153, 102, 255, 0.2)',
        'rgba(255, 159, 64, 0.2)'
      ],

    }]
  },
  options: {
    animation: false, // this reduces it by half as for all animations it's renders twice
    elements: { // this reduces it by number of options, as this is applied after the default options are run.
      line: {
        borderWidth: 1,
        borderColor: function() {
          ++i;
          console.log('why this bordercolor running this many times : ', i)
          return 'green'
        }
      },
    },
    responsive: function() {
      k++;
      console.log('why this option running this many times : ', k)
      return false
    },
    maintainAspectRatio: false,
    scales: {
      y: {
        beginAtZero: function() {
          j++;
          console.log('why this scale running this many times : ', j)
          return true
        }
      }
    }
  }
});
<canvas id="myChart" width="200" height="200"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.0/chart.min.js" integrity="sha512-asxKqQghC1oBShyhiBwA+YgotaSYKxGP1rcSYTDrB0U6DxwlJjU59B67U8+5/++uFjcuVM8Hh5cokLjZlhm3Vg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@2"></script>