Chart.js 多个标签之间的数据

Chart.js multiple datas between labels

我正在使用 chart.js 来显示数据。我想在每个标签上显示几个点。

图胜于句,这里以扑克牌平台为例:

我们看到在标签之间记录了几个点。

这是我的代码 chart.js 目前有测试数据:

var config = {
                type: 'line',
                data: {
                    labels: ['2', '4', '6', '8', '10','12','14','16'],
                    datasets: [{
                        label: 'Bankroll',
                        backgroundColor: window.chartColors.grey,
                        borderColor: window.chartColors.grey,
                        data: [20,60,80,90,120,150,170,210,260,220,150,10,220,150,220,150],
                        fill: false,
                    }]
                },
                options: {
                    responsive: true,
                    title: {
                        display: true,
                        text: 'Statistiques de votre bankroll'
                    },
                    tooltips: {
                        mode: 'index',
                        intersect: false,
                    },
                    hover: {
                        mode: 'nearest',
                        intersect: true
                    },
                    animation: {
                        duration: 2000
                    },
                    scales: {
                        xAxes: [{
                            display: true,
                            scaleLabel: {
                                display: true,
                                labelString: 'Nombre de tournois'
                            }
                        }],
                        yAxes: [{
                            display: true,
                            scaleLabel: {
                                display: true,
                                labelString: 'Total Cumulé (Euros)'
                            }
                        }]
                    }
                }
            };

人尽皆知:

我们观察到我没有8个数据,而是16个,只显示了8个。我想通过显示所有点来保持相同数量的标签,这可能吗?

非常感谢! :)

您可以使用 Array.map() 从数据中扣除 data.labels

labels: data.map((v, i) => i + 1)

然后您必须为 x-axis.

定义所需的 ticks.stepSize
xAxes: [{
  ticks: {
    stepSize: 2
  },

optionally you may also define ticks.maxTicksLimit instead.

请查看您修改后的代码,看看它是如何工作的。

var data = [20, 60, 80, 90, 120, 150, 170, 210, 260, 220, 150, 10, 220, 150, 220, 150];

var config = {
  type: 'line',
  data: {
    labels: data.map((v, i) => i + 1),
    datasets: [{
      label: 'Bankroll',
      backgroundColor: 'grey',
      borderColor: 'grey',
      data: data,
      fill: false,
    }]
  },
  options: {
    responsive: true,
    title: {
      display: true,
      text: 'Statistiques de votre bankroll'
    },
    tooltips: {
      mode: 'index',
      intersect: false,
    },
    hover: {
      mode: 'nearest',
      intersect: true
    },
    animation: {
      duration: 2000
    },
    scales: {
      xAxes: [{
        ticks: {
          stepSize: 2
        },
        scaleLabel: {
          display: true,
          labelString: 'Nombre de tournois'
        }
      }],
      yAxes: [{
        scaleLabel: {
          display: true,
          labelString: 'Total Cumulé (Euros)'
        }
      }]
    }
  }
};

new Chart('canvas', config);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js"></script>
<canvas id="canvas">