gpt4 book ai didi

javascript - 如何创建对 X 轴和 Y 轴都使用字符串的图表?

转载 作者:行者123 更新时间:2023-11-29 18:49:15 25 4
gpt4 key购买 nike

在ChartJS的文档页面上,正好有一个部分对应我的问题,但我看不懂它instruction .它写道:

If global configuration is used, labels are drawn from one of the label arrays included in the chart data. If only data.labels is defined, this will be used. If data.xLabels is defined and the axis is horizontal, this will be used. Similarly, if data.yLabels is defined and the axis is vertical, this property will be used. Using both xLabels and yLabels together can create a chart that uses strings for both the X and Y axes.

Specifying any of the settings above defines the x axis as type: category if not defined otherwise. For more fine-grained control of category labels it is also possible to add labels as part of the category axis definition. Doing so does not apply the global defaults.

这是我尝试过的:

var options = {
type: "line",
data: {
datasets: [{
label: "My First Dataset",
data: [{
x: 'January',
y: 'A'
}, {
x: 'March',
y: 'B'
}],
fill: false,
borderColor: "rgb(75, 192, 192)",
lineTension: 0.1
}]
},
options: {
scales: {
xAxes: [{
type: 'category',
labels: ['January', 'February', 'March', 'April', 'May', 'June'],
}],
yAxes: [{
type: 'category',
labels: ["A", "B", "C"],
}]
}
}
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>

还有这个:

var options = {
type: "line",
data: {
xLabels: ["January", "February", "March", "April", "May", "June"],
yLabels: ["A", "B", "C"],
datasets: [{
label: "My First Dataset",
data: [{
x: 'January',
y: 'A'
}, {
x: 'March',
y: 'B'
}],
fill: false,
borderColor: "rgb(75, 192, 192)",
lineTension: 0.1
}]
},
options: {}
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>

不幸的是,这些都不起作用。

最佳答案

经过多次尝试和错误,我终于找到了解决方案。不得不说,chart.js的文档写的很不清晰,有待改进。

我的发现是:

  • xLabelsyLabels 方法无效。这两个参数没有明确的文档。
  • 不要关心将 type 设置为 categorychart.js 只接受数值;您必须使用自己的回调方法将值映射到字符串。

var options = {
type: "line",
data: {
labels: ['January', 'February', 'March', 'April', 'May', 'June'],
datasets: [{
label: "My First Dataset",
data: [1, 2, 3, 1, 1, 1],
fill: false,
borderColor: "rgb(75, 192, 192)",
lineTension: 0.1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
max: 3,
min: 1,
stepSize: 1,
callback: function(label, index, labels) {
switch (label) {
case 1:
return 'A';
case 2:
return 'B';
case 3:
return 'C';
}
}
}
}]
}
}
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>

关于javascript - 如何创建对 X 轴和 Y 轴都使用字符串的图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51908228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com