【问题标题】:Why don't my datasets show up on line graph using Chart.js?为什么我的数据集没有显示在使用 Chart.js 的折线图上?
【发布时间】:2017-02-27 23:58:27
【问题描述】:

我正在尝试使用 Chart.js 在网页上显示折线图,显示过去 5 天特定客户端在苹果商店和谷歌播放商店中的下载次数。数据正确显示,但未正确标记 X 轴:

var downloadsChartData = {
    labels: ["4 Days", "3 Days", "2 Days", "Yesterday", "Today"],
    datasets: [{
        label: "Google Play",
        fill: false,
        lineTension: 0,
        backgroundColor: "rgba(0, 230, 115, 1)",
        borderColor: "rgba(0, 230, 115, 1)",
        borderCapStyle: 'butt',
        borderJoinStyle: 'miter',
        borderWidth: 2,
        pointBorderColor: "rgba(150, 75, 75, 1)",
        pointBorderWidth: 3,
        pointRadius: 6,
        pointHoverRadius: 9,
        pointStyle: 'triangle',
        data: [{
            x: 1,
            y: 2
        }, {
            x: 2,
            y: 0
        }, {
            x: 3,
            y: 3
        }, {
            x: 4,
            y: 1
        }, {
            x: 5,
            y: 1
        }]
    }, {
        label: "iOS",
        fill: false,
        lineTension: 0,
        backgroundColor: "rgba(26, 117, 255, 1)",
        borderColor: "rgba(26, 117, 225, 1)",
        borderCapStyle: 'butt',
        borderJoinStyle: 'miter',
        borderWidth: 2,
        pointBorderColor: "rgba(150, 75, 75, 1)",
        pointBorderWidth: 3,
        pointRadius: 6,
        pointHoverRadius: 9,
        data: [{
            x: 1,
            y: 4
        }, {
            x: 2,
            y: 2
        }, {
            x: 3,
            y: 0
        }, {
            x: 4,
            y: 1
        }, {
            x: 5,
            y: 4
        }]
    }]
};

var downloadsChartOptions = {
    title: {
        display: true,
        text: 'Downloads on Google Play and App Store',
        fontSize: 30
    },
    scales: {
        xAxes: [{
            scaleLabel: {
                display: true,
                abelString: 'Date',
                fontSize: 20
            },
            type: 'linear',
            position: 'bottom',
            gridLines: {
                display: false
            }
        }],
        yAxes: [{
            scaleLabel: {
                display: true,
                labelString: 'Downloads',
                fontSize: 20
            }
        }]
    }
};

var downloadsChart = document.getElementById('downloadsChart').getContext('2d');
new Chart(downloadsChart, {
    type: 'line',
    data: downloadsChartData,
    options: downloadsChartOptions
});

我尝试将 data 字段切换为值数组,以便 Chart.js 将我在 downloadsChartData 中定义的标签识别为:

...
pointStyle: 'triangle',
data: [2, 0, 3, 1, 1]
}]
...
pointHoverRadius: 9,
data: [4, 2, 0, 1, 4]
}]

但是,当我进行此更改时,图表不仅没有正确标记 X 轴,而且还完全停止在折线图上显示数据。

有人可以发现我做错了什么吗?文档在这个问题上没有太大帮助。

【问题讨论】:

    标签: javascript chart.js


    【解决方案1】:

    在这一点上文档不是很清楚,但是对于绘制非数字数据(例如您的 X 轴表示日期的情况),您不能将 X 轴设置为具有线性比例。换句话说,如果您将轴设置为线性刻度,它将绘制数据的数字表示形式(因此,不是标签)。

    我从您的downloadsChartOptions 中删除了type: 'linear',它解决了这个问题。:

    var downloadsChartOptions = {
        title: {
            display: true,
            text: 'Downloads on Google Play and App Store',
            fontSize: 30
        },
        scales: {
            xAxes: [{
                scaleLabel: {
                    display: true,
                    labelString: 'Date',
                    fontSize: 20
                },
                //type: 'linear',
                position: 'bottom',
                gridLines: {
                    display: false
                }
            }],
            yAxes: [{
                scaleLabel: {
                    display: true,
                    labelString: 'Downloads',
                    fontSize: 20
                }
            }]
        }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-08
      • 1970-01-01
      • 2019-06-29
      相关资源
      最近更新 更多