【问题标题】:ChartJs showing wrong labels of data (x-axis dates)ChartJs 显示错误的数据标签(x 轴日期)
【发布时间】:2020-06-06 15:27:11
【问题描述】:

我有一个 ChartJs 图表,下面是代码

chartColor = "#FFFFFF";
var ctx = document.getElementById('bigDashboardChart').getContext("2d");

var gradientStroke = ctx.createLinearGradient(500, 0, 100, 0);
gradientStroke.addColorStop(0, '#80b6f4');
gradientStroke.addColorStop(1, chartColor);

var gradientFill = ctx.createLinearGradient(0, 200, 0, 50);
gradientFill.addColorStop(0, "rgba(128, 182, 244, 0)");
gradientFill.addColorStop(1, "rgba(255, 255, 255, 0.24)");

gradientFill2 = ctx.createLinearGradient(0, 170, 0, 50);
gradientFill2.addColorStop(0, "rgba(128, 182, 244, 0)");
gradientFill2.addColorStop(1, hexToRGB('#edd505', 0.4));

var myChart = new Chart(ctx, {
  type: 'line',
  data: {
    labels: ['2020-02-07','2020-02-08','2020-02-09','2020-02-10','2020-02-11','2020-02-12','2020-02-13'],
    datasets: [{
      label: "Ujani Total Water",
      borderColor: chartColor,
      pointBorderColor: chartColor,
      pointBackgroundColor: "#1e3d60",
      pointHoverBackgroundColor: "#1e3d60",
      pointHoverBorderColor: chartColor,
      pointBorderWidth: 1,
      pointHoverRadius: 7,
      pointHoverBorderWidth: 2,
      pointRadius: 5,
      fill: true,
      backgroundColor: gradientFill,
      borderWidth: 2,
      data: [{ 'x' : '2020-02-07', 'y' : 16483111.23},{ 'x' : '2020-02-08', 'y' : 5624132.01},{ 'x' : '2020-02-09', 'y' : 9800147.12},{ 'x' : '2020-02-10', 'y' : 17204617.82},{ 'x' : '2020-02-11', 'y' : 19318882.05},{ 'x' : '2020-02-12', 'y' : 6291494.27},{ 'x' : '2020-02-13', 'y' : 10536864.88}]
    },
    {
      label: "Chandani Total Water",
      borderColor: "#edd505",
      pointBorderColor: "#FFF",
      pointBackgroundColor: "#edd505",
      pointHoverBackgroundColor: "#edd505",
      pointHoverBorderColor: "#FFF",
      pointBorderWidth: 1,
      pointHoverRadius: 7,
      pointHoverBorderWidth: 2,
      pointRadius: 5,
      fill: true,
      backgroundColor: gradientFill2,
      borderWidth: 2,
      data: [{ 'x' : '2020-02-11', 'y' : 744864.1},{ 'x' : '2020-02-12', 'y' : 544.93},{ 'x' : '2020-02-13', 'y' : 1564922.77}]
    }]
  },
  options: {
    layout: {
      padding: {
        left: 20,
        right: 20,
        top: 0,
        bottom: 0
      }
    },
    maintainAspectRatio: false,
    tooltips: {
      backgroundColor: '#fff',
      titleFontColor: '#333',
      bodyFontColor: '#666',
      bodySpacing: 4,
      xPadding: 12,
      mode: "nearest",
      intersect: 0,
      position: "nearest"
    },
    legend: {
      position: "top",
      fillStyle: "#FFF",
      display: true
    },
    scales: {
      yAxes: [{
        stacked : false,
        ticks: {
          fontColor: "rgba(255,255,255,0.4)",
          fontStyle: "bold",
          beginAtZero: true,
          min: 0,
          maxTicksLimit: 5,
          padding: 10
        },
        gridLines: {
          drawTicks: true,
          drawBorder: false,
          display: true,
          color: "rgba(255,255,255,0.1)",
          zeroLineColor: "transparent"
          }
      }],
      xAxes: [{
        gridLines: {
          zeroLineColor: "transparent",
          display: false,

        },
        ticks: {
          beginAtZero : true,
          padding: 10,
          min: 0,
          fontColor: "rgba(255,255,255,0.4)",
          fontStyle: "bold"
        }
      }]
    }
  }
});

您也可以看到图像,两个图像中显示的第一个点的日期相同,但实际上黄色数据集的日期是从 2020-02-11 开始的。 p>

或者这是因为我没有正确实现包含多个数据集的图表?任何帮助表示赞赏。如果需要任何其他信息,请告诉我。

注意:这只是一张图表,我刚刚展示了两张图片以显示错误的起点标签。

更新:在经历其他 Stack Overflow 和其他答案后,尝试将“标签”添加到这样的单个数据集。

 {
      label: "Chandani Total Water",
      labels : ['2020-02-11','2020-02-12','2020-02-13'],
      borderColor: "#edd505",
      pointBorderColor: "#FFF",
      pointBackgroundColor: "#edd505",
      pointHoverBackgroundColor: "#edd505",
      pointHoverBorderColor: "#FFF",
      pointBorderWidth: 1,
      pointHoverRadius: 7,
      pointHoverBorderWidth: 2,
      pointRadius: 5,
      fill: true,
      backgroundColor: gradientFill2,
      borderWidth: 2,
      data: [{ 'x' : '2020-02-11', 'y' : 744864.1},{ 'x' : '2020-02-12', 'y' : 544.93},{ 'x' : '2020-02-13', 'y' : 1564922.77}]
    }

【问题讨论】:

  • 你的html代码?
  • html 部分没有太多内容。只需 1 行 ,就是这样。

标签: php html chart.js


【解决方案1】:

您应该将xAxis 定义为time cartesian axis

为此,请将以下内容添加到图表options 内的xAxis

xAxes: [{
    type: 'time',
    time: {
      unit: 'day'
    },
    ...

请注意,Chart.js 使用 Moment.js 来实现 时间轴。因此,您应该使用 Chart.js 的 bundled version,将 Moment.js 包含在单个文件中。

请看下面的可运行代码sn-p。

var chartColor = "#FFFFFF";
var ctx = document.getElementById('bigDashboardChart').getContext("2d");

var gradientStroke = ctx.createLinearGradient(500, 0, 100, 0);
gradientStroke.addColorStop(0, '#80b6f4');
gradientStroke.addColorStop(1, chartColor);

var gradientFill = ctx.createLinearGradient(0, 200, 0, 50);
gradientFill.addColorStop(0, "rgba(128, 182, 244, 0)");
gradientFill.addColorStop(1, "rgba(255, 255, 255, 0.24)");

var gradientFill2 = ctx.createLinearGradient(0, 170, 0, 50);
gradientFill2.addColorStop(0, "rgba(128, 182, 244, 0)");
gradientFill2.addColorStop(1, "rgba(237, 213, 5, 0.4)");

new Chart(ctx, {
  type: 'line',
  data: {
    labels: ['2020-02-07', '2020-02-08', '2020-02-09', '2020-02-10', '2020-02-11', '2020-02-12', '2020-02-13'],
    datasets: [{
        label: "Ujani Total Water",
        borderColor: chartColor,
        pointBorderColor: chartColor,
        pointBackgroundColor: "#1e3d60",
        pointHoverBackgroundColor: "#1e3d60",
        pointHoverBorderColor: chartColor,
        pointBorderWidth: 1,
        pointHoverRadius: 7,
        pointHoverBorderWidth: 2,
        pointRadius: 5,
        fill: true,
        backgroundColor: gradientFill,
        borderWidth: 2,
        data: [{
          'x': '2020-02-07',
          'y': 16483111.23
        }, {
          'x': '2020-02-08',
          'y': 5624132.01
        }, {
          'x': '2020-02-09',
          'y': 9800147.12
        }, {
          'x': '2020-02-10',
          'y': 17204617.82
        }, {
          'x': '2020-02-11',
          'y': 19318882.05
        }, {
          'x': '2020-02-12',
          'y': 6291494.27
        }, {
          'x': '2020-02-13',
          'y': 10536864.88
        }]
      },
      {
        label: "Chandani Total Water",
        borderColor: "#edd505",
        pointBorderColor: "#FFF",
        pointBackgroundColor: "#edd505",
        pointHoverBackgroundColor: "#edd505",
        pointHoverBorderColor: "#FFF",
        pointBorderWidth: 1,
        pointHoverRadius: 7,
        pointHoverBorderWidth: 2,
        pointRadius: 5,
        fill: true,
        backgroundColor: gradientFill2,
        borderWidth: 2,
        data: [{
          'x': '2020-02-11',
          'y': 744864.1
        }, {
          'x': '2020-02-12',
          'y': 544.93
        }, {
          'x': '2020-02-13',
          'y': 1564922.77
        }]
      }
    ]
  },
  options: {
    layout: {
      padding: {
        left: 20,
        right: 20,
        top: 0,
        bottom: 0
      }
    },
    maintainAspectRatio: false,
    tooltips: {
      backgroundColor: '#fff',
      titleFontColor: '#333',
      bodyFontColor: '#666',
      bodySpacing: 4,
      xPadding: 12,
      mode: "nearest",
      intersect: 0,
      position: "nearest"
    },
    legend: {
      position: "top",
      fillStyle: "#FFF",
      display: true
    },
    scales: {
      yAxes: [{
        stacked: false,
        ticks: {
          fontColor: "rgba(255,255,255,0.4)",
          fontStyle: "bold",
          beginAtZero: true,
          min: 0,
          maxTicksLimit: 5,
          padding: 10
        },
        gridLines: {
          drawTicks: true,
          drawBorder: false,
          display: true,
          color: "rgba(255,255,255,0.1)",
          zeroLineColor: "transparent"
        }
      }],
      xAxes: [{
        type: 'time',
        time: {
          unit: 'day'
        },
        gridLines: {
          zeroLineColor: "transparent",
          display: false
        },
        ticks: {
          beginAtZero: true,
          padding: 10,
          min: 0,
          fontColor: "rgba(255,255,255,0.4)",
          fontStyle: "bold"
        }
      }]
    }
  }
});
body {
  background-color: black
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.js"></script> 
<canvas id="bigDashboardChart"></canvas>

【讨论】:

  • 您好,将这个添加到xAxis后,图表根本没有显示。
  • 你是不是伪造了一个逗号?
  • 实际上不,仔细检查,我也在这里尝试这个答案stackoverflow.com/questions/38101859/…,因为它符合我的情况。
  • 我用一个可运行的代码 sn-p 更新了我的答案,这应该说明我的提议有效。
  • 你是对的,谢谢你的解释。虽然我在使用现成的模板并且不想弄乱其他功能时包含了 moment.js 文件。谢谢。
猜你喜欢
  • 2021-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多