如何在 chartjs 中显示最后一个 x 浮点数值

how to display last x float number value in chartjs

我正在使用 react-chartjs-2 制作垂直条形图。

我存储了很多浮点数数组。

我尝试使用 chartjs callback option 仅在图表上显示最后一个值。

但是回调中的值是一个整数,所以取不到我想要的值

例如,

const xAsisData = [ 1.11, 4.23, 7.34, ... , 403.78 ] // includes hundreds
  scales: {
    x: {
      ticks: {
        autoSkip: false,
        callback: (value) => {
         // if value === last ? value : null

         // ! but last value is 309, not 403.78 
        },
      },

我可以使用其他选项吗?

您在回调中也获得了索引和报价数组,因此您可以像这样检查索引是否不是最终索引:

var options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        borderWidth: 1
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        borderWidth: 1
      }
    ]
  },
  options: {
    scales: {
      y: {
        ticks: {
          callback: (val, i, ticks) => (i < ticks.length - 1 ? val : null) // Replace null by empty string to still show the gridLine
        }
      }
    }
  }
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.3.2/chart.js"></script>
</body>