我将chart.js与React一起使用。我的问题是如何每月仅显示一次月份标签(MMM)?
该图表当前具有标签:[May 15, May 18, May 21, May 24, ...] 结果想成为,我想得到:[May 15, 18, 21, 24, 27, 30, Jun 2, 5, ...] CodeSandbox
javascript - 格式化chart.js x标签-LMLPHP
折线图:

import React from 'react'
import { Line } from 'react-chartjs-2'
import date from 'date-and-time'

const startDate = new Date(2020, 4, 15)

//===fake data===
const json = '{"responses":[{"rows":[{"values":["1"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["1"]},{"values":["6"]},{"values":["7"]},{"values":["5"]},{"values":["8"]},{"values":["9"]},{"values":["2"]},{"values":["1"]},{"values":["1"]},{"values":["1"]},{"values":["6"]},{"values":["3"]},{"values":["0"]},{"values":["20"]},{"values":["9"]},{"values":["3"]},{"values":["2"]},{"values":["1"]},{"values":["13"]},{"values":["3"]},{"values":["13"]},{"values":["13"]},{"values":["7"]},{"values":["12"]},{"values":["0"]}]}]}'
const values = JSON.parse(json).responses[0].rows.map((row, index) => {
  let date = new Date(2020, 4, 20)
  date.setDate(startDate.getDate() + index)
  return {
    y: row.values[0],
    x: date,
  }
})
//===============

const options = {
  legend: {
    display: false,
  },
  hover: {
    mode: 'index',
    intersect: false,
    animationDuration: 0,
  },
  scales: {
    yAxes: [{ position: 'right' }],
    xAxes: [{
      gridLines: { display: false },
      distribution: 'linear',
      type: 'time',
      time: {
        parser: 'MMM D',
        tooltipFormat: 'MMM D',
        unit: 'day',
        unitStepSize: 3,
        displayFormats: {
          day: 'MMM D',
        },
      },
      ticks: {
        min: startDate,
        max: date.format(date.addDays(new Date(), 1), 'MMM D'),
        autoSkip: true
      },
    }],
  },
  tooltips: {
    mode: 'x-axis',
  },
}

const data = {
  datasets: [
    {
      label: 'test',
      fill: false,
      data: values,
      backgroundColor: '#fff',
      borderWidth: 2,
      lineTension: 0,
      borderColor: 'forestgreen',
      hoverBorderWidth: 2,
      pointBorderColor: 'rgba(0, 0, 0, 0)',
      pointBackgroundColor: 'rgba(0, 0, 0, 0)',
      pointHoverBackgroundColor: '#fff',
      pointHoverBorderColor: 'forestgreen',
      showLine: true,
    }
  ],
}

const LineChart = () => <Line data={data} options={options}/>

export default LineChart

最佳答案

  • displayFormatsday定义不同的 month
  • 启用 ticks.major
  • 通过 major 回调将所需的刻度标记为afterBuildTicks
  • time: {
      ...
      displayFormats: {
        day: 'D',
        month: 'MMM D',
      },
    },
    ticks: {
      major: {
        enabled: true
      }
    },
    afterBuildTicks: (scale, ticks) => {
      ticks.forEach((t, i) => t.major = i == 0 || new Date(t.value).getMonth() != new Date(ticks[i - 1].value).getMonth());
      return ticks;
    }
    
    请查看您修改后的代码,并查看其工作方式。

    const startDate = new Date(2020, 4, 15)
    
    //===fake data===
    const json = '{"responses":[{"rows":[{"values":["1"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["1"]},{"values":["6"]},{"values":["7"]},{"values":["5"]},{"values":["8"]},{"values":["9"]},{"values":["2"]},{"values":["1"]},{"values":["1"]},{"values":["1"]},{"values":["6"]},{"values":["3"]},{"values":["0"]},{"values":["20"]},{"values":["9"]},{"values":["3"]},{"values":["2"]},{"values":["1"]},{"values":["13"]},{"values":["3"]},{"values":["13"]},{"values":["13"]},{"values":["7"]},{"values":["12"]},{"values":["0"]}]}]}'
    const values = JSON.parse(json).responses[0].rows.map((row, index) => {
      let date = new Date(2020, 4, 20);
      date.setDate(startDate.getDate() + index)
      return {
        y: row.values[0],
        x: date
      }
    })
    
    //===============
    
    const options = {
      legend: {
        display: false
      },
      hover: {
        mode: 'index',
        intersect: false,
        animationDuration: 0
      },
      scales: {
        yAxes: [{
          position: 'right'
        }],
        xAxes: [{
          gridLines: {
            display: false
          },
          distribution: 'linear',
          type: 'time',
          time: {
            tooltipFormat: 'MMM D',
            unit: 'day',
            unitStepSize: 3,
            displayFormats: {
              day: 'D',
              month: 'MMM D',
            },
          },
          ticks: {
            major: {
              enabled: true
            }
          },
          afterBuildTicks: (scale, ticks) => {
            ticks.forEach((t, i) => t.major = i == 0 || new Date(t.value).getMonth() != new Date(ticks[i - 1].value).getMonth());
            return ticks;
          }
        }]
      },
      tooltips: {
        mode: 'x-axis',
      }
    };
    
    const data = {
      datasets: [{
        label: 'test',
        fill: false,
        data: values,
        backgroundColor: '#fff',
        borderWidth: 2,
        lineTension: 0,
        borderColor: 'forestgreen',
        hoverBorderWidth: 2,
        pointBorderColor: 'rgba(0, 0, 0, 0)',
        pointBackgroundColor: 'rgba(0, 0, 0, 0)',
        pointHoverBackgroundColor: '#fff',
        pointHoverBorderColor: 'forestgreen',
        showLine: true,
      }],
    };
    
    new Chart('myChart', {
      type: 'line',
      data: data,
      options: options
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js"></script>
    <canvas id="myChart" height="90"></canvas>

    关于javascript - 格式化chart.js x标签,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63169398/

    10-11 12:27