最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I format the x-axis using a timestamp via chart.js? - Stack Overflow

programmeradmin1浏览0评论

I am wanting to display the temperature readings from a database on a graph. However, at the moment the x-axis is displaying incorrect values that do not correspond with my test dataset.

The x-axis is meant to be in the time domain using timestamps, instead, the graph uses each character from the label 'Reddd' as a value for the x-axis.

The y-axis is correct and corresponds to the test data points.

The function below includes the javascript code for constructing the graph.

function makeGraph() {
    const ctx = document.getElementById('canvas').getContext('2d');
    const myChart = new Chart(ctx, {
        type: 'line',
        data: {
            labels: 'Reddd',
            datasets: [{
                label: 'Temperature',
                data: [{"x": 1647281788963, "y": 22.9}, {"x": 1647281994496, "y": 26.9}, {"x": 1647282200029, "y": 21.9}, {"x": 1647282405562, "y": 24.9}, {"x": 1647282611094, "y": 28.9}],
                backgroundColor: 'transparent',
                borderColor: 'red',
                borderWidth: 2
            }]
        },
        options: {
            scales: {
                x: {
                    type: 'time',
                    time: {
                        // Luxon format string
                        tooltipFormat: 'DD T'

                    },
                    title: {
                        display: true,
                        text: 'Date'
                    }
                },
                y: {
                    title: {
                        display: true,
                        text: 'value'
                    }
                }
            }
        }
    });


}

This is the output of the graph:

If I dont include a string after the labels: statment, the graph will look this:

I am wanting to display the temperature readings from a database on a graph. However, at the moment the x-axis is displaying incorrect values that do not correspond with my test dataset.

The x-axis is meant to be in the time domain using timestamps, instead, the graph uses each character from the label 'Reddd' as a value for the x-axis.

The y-axis is correct and corresponds to the test data points.

The function below includes the javascript code for constructing the graph.

function makeGraph() {
    const ctx = document.getElementById('canvas').getContext('2d');
    const myChart = new Chart(ctx, {
        type: 'line',
        data: {
            labels: 'Reddd',
            datasets: [{
                label: 'Temperature',
                data: [{"x": 1647281788963, "y": 22.9}, {"x": 1647281994496, "y": 26.9}, {"x": 1647282200029, "y": 21.9}, {"x": 1647282405562, "y": 24.9}, {"x": 1647282611094, "y": 28.9}],
                backgroundColor: 'transparent',
                borderColor: 'red',
                borderWidth: 2
            }]
        },
        options: {
            scales: {
                x: {
                    type: 'time',
                    time: {
                        // Luxon format string
                        tooltipFormat: 'DD T'

                    },
                    title: {
                        display: true,
                        text: 'Date'
                    }
                },
                y: {
                    title: {
                        display: true,
                        text: 'value'
                    }
                }
            }
        }
    });


}

This is the output of the graph:

If I dont include a string after the labels: statment, the graph will look this:

Share Improve this question asked Mar 23, 2022 at 14:47 e_robinsone_robinson 411 silver badge7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

First, you must not define data.labels.

Further, the x-axis could be defined as follows:

x: {
  type: 'time',
  time: {
    unit: 'minute',
    displayFormats: {
      minute: 'DD T'
    },
    tooltipFormat: 'DD T'
  },
  ...

Please take a look at your amended and runnable code below and see how it works.

new Chart('canvas', {
  type: 'line',
  data: {
    datasets: [{
      label: 'Temperature',
      data: [
        {"x": 1647281788963, "y": 22.9}, 
        {"x": 1647281994496, "y": 26.9}, 
        {"x": 1647282200029, "y": 21.9}, 
        {"x": 1647282405562, "y": 24.9}, 
        {"x": 1647282611094, "y": 28.9}
      ],               
      backgroundColor: 'transparent',
      borderColor: 'red',
      borderWidth: 2,
      tension: 0.5
    }]
  },
  options: {
    scales: {
      x: {
        type: 'time',
        time: {
          unit: 'minute',
          displayFormats: {
              minute: 'DD T'
          },
          tooltipFormat: 'DD T'
        },
        title: {
          display: true,
          text: 'Date'
        }
      },
      y: {
        title: {
          display: true,
          text: 'value'
        }
      }
    }
  }
});
<script src="https://cdn.jsdelivr/npm/chart.js@^3"></script>
<script src="https://cdn.jsdelivr/npm/luxon@^2"></script>
<script src="https://cdn.jsdelivr/npm/chartjs-adapter-luxon@^1"></script>
<canvas id="canvas" height="120"></canvas>

发布评论

评论列表(0)

  1. 暂无评论