Charts ChartJS:映射非数值Y和X
我试图映射非数值的y和x,但由于某些原因,它不起作用 例如 当我尝试改变时:Charts ChartJS:映射非数值Y和X,charts,chart.js,scatter-plot,linechart,scatter,Charts,Chart.js,Scatter Plot,Linechart,Scatter,我试图映射非数值的y和x,但由于某些原因,它不起作用 例如 当我尝试改变时: data: ['Request Added', 'Request Viewed'] 与 图表没有显示任何东西 我还尝试使用scales.yAxis.ticks.callback修改并映射数组,但也没有成功 [0: 'Request Added', 1: 'Request Viewed'] 编辑:基本上我需要这样的东西 Request Added x x Request Vie
data: ['Request Added', 'Request Viewed']
与
图表没有显示任何东西
我还尝试使用scales.yAxis.ticks.callback修改并映射数组,但也没有成功
[0: 'Request Added', 1: 'Request Viewed']
编辑:基本上我需要这样的东西
Request Added x x
Request Viewed x
Request Accepted x x
January, Feb, March
基本上是一个副本:
这也建议映射到一个数组,但是Y-ticks中的回调没有什么奇怪的意义?当我添加
labelY:[1,2,3,4,5,6]
时,回调“values”的第三个参数等于[-2-1 0 1 2]
要对X轴和Y轴使用类别比例,必须使用值数组的传统数据格式。根据
散点图可以通过将X轴更改为线性轴来创建。要使用散点图,数据必须作为包含X和Y属性的对象传递
这意味着您只能在至少x轴是线性比例的情况下使用数据格式{x:'',y:''}
(但我打赌它只有在x轴和y轴都是线性的情况下才有效)
由于您仅限于为数据使用一个值数组,因此必须至少使用2个数据集才能在X轴上绘制多个值(就像您正在尝试的那样)
下面是一个图表配置,它给出了您要查找的内容
var ctx = document.getElementById("canvas").getContext("2d");
var myLine = new Chart(ctx, {
type: 'line',
data: {
xLabels: ["January", "February", "March", "April", "May", "June", "July"],
yLabels: ['Request Added', 'Request Viewed', 'Request Accepted', 'Request Solved', 'Solving Confirmed'],
datasets: [{
label: "My First dataset",
data: ['Request Added', 'Request Viewed', 'Request Added'],
fill: false,
showLine: false,
borderColor: chartColors.red,
backgroundColor: chartColors.red
}, {
label: "My First dataset",
data: [null, 'Request Accepted', 'Request Accepted'],
fill: false,
showLine: false,
borderColor: chartColors.red,
backgroundColor: chartColors.red
}]
},
options: {
responsive: true,
title:{
display: true,
text: 'Chart.js - Non Numeric X and Y Axis'
},
legend: {
display: false
},
scales: {
xAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Month'
}
}],
yAxes: [{
type: 'category',
position: 'left',
display: true,
scaleLabel: {
display: true,
labelString: 'Request State'
},
ticks: {
reverse: true
},
}]
}
}
});
你可以从这里看到它是什么样子
如果出于任何原因而使用{x:'',y:''}
数据格式,则必须将两种比例更改为线性,将数据映射为数值,然后使用属性将数值记号映射回字符串值
下面是一个演示此方法的示例
var xMap = ["January", "February", "March", "April", "May", "June", "July"];
var yMap = ['Request Added', 'Request Viewed', 'Request Accepted', 'Request Solved', 'Solving Confirmed'];
var mapDataPoint = function(xValue, yValue) {
return {
x: xMap.indexOf(xValue),
y: yMap.indexOf(yValue)
};
};
var ctx2 = document.getElementById("canvas2").getContext("2d");
var myLine2 = new Chart(ctx2, {
type: 'line',
data: {
datasets: [{
label: "My First dataset",
data: [
mapDataPoint("January", "Request Added"),
mapDataPoint("February", "Request Viewed"),
mapDataPoint("February", "Request Accepted"),
mapDataPoint("March", "Request Added"),
mapDataPoint("March", "Request Accepted"),
],
fill: false,
showLine: false,
borderColor: chartColors.red,
backgroundColor: chartColors.red
}]
},
options: {
responsive: true,
title: {
display: true,
text: 'Chart.js - Scatter Chart Mapping X and Y to Non Numeric'
},
legend: {
display: false
},
scales: {
xAxes: [{
type: 'linear',
position: 'bottom',
scaleLabel: {
display: true,
labelString: 'Month'
},
ticks: {
min: 0,
max: xMap.length - 1,
callback: function(value) {
return xMap[value];
},
},
}],
yAxes: [{
scaleLabel: {
display: true,
labelString: 'Request State'
},
ticks: {
reverse: true,
min: 0,
max: yMap.length - 1,
callback: function(value) {
return yMap[value];
},
},
}]
}
}
});
同时,你也可以看到这一点。你能展示一下你的努力吗?用示例/non-numeric-y.html/在本地展示了吗。我尝试了各种各样的变化,然后把它们藏起来
var ctx = document.getElementById("canvas").getContext("2d");
var myLine = new Chart(ctx, {
type: 'line',
data: {
xLabels: ["January", "February", "March", "April", "May", "June", "July"],
yLabels: ['Request Added', 'Request Viewed', 'Request Accepted', 'Request Solved', 'Solving Confirmed'],
datasets: [{
label: "My First dataset",
data: ['Request Added', 'Request Viewed', 'Request Added'],
fill: false,
showLine: false,
borderColor: chartColors.red,
backgroundColor: chartColors.red
}, {
label: "My First dataset",
data: [null, 'Request Accepted', 'Request Accepted'],
fill: false,
showLine: false,
borderColor: chartColors.red,
backgroundColor: chartColors.red
}]
},
options: {
responsive: true,
title:{
display: true,
text: 'Chart.js - Non Numeric X and Y Axis'
},
legend: {
display: false
},
scales: {
xAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Month'
}
}],
yAxes: [{
type: 'category',
position: 'left',
display: true,
scaleLabel: {
display: true,
labelString: 'Request State'
},
ticks: {
reverse: true
},
}]
}
}
});
var xMap = ["January", "February", "March", "April", "May", "June", "July"];
var yMap = ['Request Added', 'Request Viewed', 'Request Accepted', 'Request Solved', 'Solving Confirmed'];
var mapDataPoint = function(xValue, yValue) {
return {
x: xMap.indexOf(xValue),
y: yMap.indexOf(yValue)
};
};
var ctx2 = document.getElementById("canvas2").getContext("2d");
var myLine2 = new Chart(ctx2, {
type: 'line',
data: {
datasets: [{
label: "My First dataset",
data: [
mapDataPoint("January", "Request Added"),
mapDataPoint("February", "Request Viewed"),
mapDataPoint("February", "Request Accepted"),
mapDataPoint("March", "Request Added"),
mapDataPoint("March", "Request Accepted"),
],
fill: false,
showLine: false,
borderColor: chartColors.red,
backgroundColor: chartColors.red
}]
},
options: {
responsive: true,
title: {
display: true,
text: 'Chart.js - Scatter Chart Mapping X and Y to Non Numeric'
},
legend: {
display: false
},
scales: {
xAxes: [{
type: 'linear',
position: 'bottom',
scaleLabel: {
display: true,
labelString: 'Month'
},
ticks: {
min: 0,
max: xMap.length - 1,
callback: function(value) {
return xMap[value];
},
},
}],
yAxes: [{
scaleLabel: {
display: true,
labelString: 'Request State'
},
ticks: {
reverse: true,
min: 0,
max: yMap.length - 1,
callback: function(value) {
return yMap[value];
},
},
}]
}
}
});