英文:
add multi chart with multi axes label using chart js
问题
我试图一起制作条形图和折线图,但处理坐标轴标签时出现了问题
我正在使用Chart.js
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="https://cdn.jsdelivr.net/npm/chart.js@3.3.2/dist/chart.min.js"></script>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
function createDiagonalPattern(color = 'black') {
let shape = document.createElement('canvas')
shape.width = 10
shape.height = 10
let c = shape.getContext('2d')
c.strokeStyle = color
c.beginPath()
c.moveTo(2, 0)
c.lineTo(10, 8)
c.stroke()
c.beginPath()
c.moveTo(0, 8)
c.lineTo(2, 10)
c.stroke()
return c.createPattern(shape, 'repeat')
}
var barChartData = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
type: 'bar',
label: "Visitor",
data: [200, 185, 590, 621, 250, 400, 95],
fill: false,
backgroundColor: createDiagonalPattern('grey'),
borderColor: 'grey',
borderWidth: 1,
hoverBackgroundColor: '#71B37C',
hoverBorderColor: '#71B37C',
yAxisID: 'y-axis-1'
}, {
label: "Sales",
type: 'line',
data: [51, 65, 40, 49, 60, 37, 40],
fill: false,
borderColor: '#2E41CF',
backgroundColor: '#2E41CF',
pointBorderColor: '#2E41CF',
pointBackgroundColor: 'white',
pointHoverBackgroundColor: '#2E41CF',
pointHoverBorderColor: '#2E41CF',
pointStyle: 'circle',
pointRadius: 10,
pointHoverRadius: 15,
pointBorderWidth: 3,
yAxisID: 'y-axis-2'
}]
};
window.onload = function () {
var ctx = document.getElementById("canvas").getContext("2d");
window.myBar = new Chart(ctx, {
type: 'bar',
data: barChartData,
options: {
responsive: true,
tooltips: {
mode: 'label'
},
elements: {
line: {
fill: false
}
},
scales: {
x: [{
display: true,
gridLines: {
display: true
},
position: 'top',
labels: {
show: false,
}
}],
y: [{
type: "linear",
display: false,
position: "left",
id: "y-axis-1",
gridLines: {
display: false
},
labels: {
show: true,
}
}, {
type: "linear",
display: false,
position: "right",
id: "y-axis-2",
gridLines: {
display: false
},
labels: {
show: true,
}
}]
}
}
});
}
</script>
</body>
</html>
我尝试了上面提到的代码,得到了这个结果
但是,我的预期结果应该是我不能将销售额移到右侧轴标签
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function createDiagonalPattern(color = 'black') {
let shape = document.createElement('canvas')
shape.width = 10
shape.height = 10
let c = shape.getContext('2d')
c.strokeStyle = color
c.beginPath()
c.moveTo(2, 0)
c.lineTo(10, 8)
c.stroke()
c.beginPath()
c.moveTo(0, 8)
c.lineTo(2, 10)
c.stroke()
return c.createPattern(shape, 'repeat')
}
var barChartData = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
type: 'bar',
label: "Visitor",
data: [200, 185, 590, 621, 250, 400, 95],
fill: false,
backgroundColor: createDiagonalPattern('grey'),
borderColor: 'grey',
borderWidth: 1,
hoverBackgroundColor: '#71B37C',
hoverBorderColor: '#71B37C',
yAxisID: 'y1'
}, {
label: "Sales",
type:'line',
data: [51, 65, 40, 49, 60, 37, 40],
fill: false,
borderColor: '#2E41CF',
backgroundColor: '#2E41CF',
pointBorderColor: '#2E41CF',
pointBackgroundColor: 'white',
pointHoverBackgroundColor: '#2E41CF',
pointHoverBorderColor: '#2E41CF',
pointStyle: 'circle',
pointRadius: 10,
pointHoverRadius: 15,
pointBorderWidth:3,
yAxisID: 'y'
} ]
};
window.onload = function() {
var ctx = document.getElementById("canvas").getContext("2d");
window.myBar = new Chart(ctx, {
type: 'bar',
data: barChartData,
options: {
responsive: true,
tooltips: {
mode: 'label'
},
elements: {
line: {
fill: false
}
},
scales: {
y: {
type: "linear",
display: false,
position: "left",
gridLines:{
display: false
},
labels: {
show:true,
}
},
y1: {
type: "linear",
display: false,
position: "right",
gridLines:{
display: false
},
labels: {
show:true,
}
} }
}
});
}
<!-- language: lang-html -->
<canvas id="canvas"></canvas>
<script src="https://cdn.jsdelivr.net/npm/chart.js@3.3.2/dist/chart.min.js"></script>
<!-- end snippet -->
I am trying to make bar and line chart together but while handling the axes label is not working properly
I am using chart js
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="https://cdn.jsdelivr.net/npm/chart.js@3.3.2/dist/chart.min.js"></script>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
function createDiagonalPattern(color = 'black') {
let shape = document.createElement('canvas')
shape.width = 10
shape.height = 10
let c = shape.getContext('2d')
c.strokeStyle = color
c.beginPath()
c.moveTo(2, 0)
c.lineTo(10, 8)
c.stroke()
c.beginPath()
c.moveTo(0, 8)
c.lineTo(2, 10)
c.stroke()
return c.createPattern(shape, 'repeat')
}
var barChartData = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
type: 'bar',
label: "Visitor",
data: [200, 185, 590, 621, 250, 400, 95],
fill: false,
backgroundColor: createDiagonalPattern('grey'),
borderColor: 'grey',
borderWidth: 1,
hoverBackgroundColor: '#71B37C',
hoverBorderColor: '#71B37C',
yAxisID: 'y-axis-1'
}, {
label: "Sales",
type:'line',
data: [51, 65, 40, 49, 60, 37, 40],
fill: false,
borderColor: '#2E41CF',
backgroundColor: '#2E41CF',
pointBorderColor: '#2E41CF',
pointBackgroundColor: 'white',
pointHoverBackgroundColor: '#2E41CF',
pointHoverBorderColor: '#2E41CF',
pointStyle: 'circle',
pointRadius: 10,
pointHoverRadius: 15,
pointBorderWidth:3,
yAxisID: 'y-axis-2'
} ]
};
window.onload = function() {
var ctx = document.getElementById("canvas").getContext("2d");
window.myBar = new Chart(ctx, {
type: 'bar',
data: barChartData,
options: {
responsive: true,
tooltips: {
mode: 'label'
},
elements: {
line: {
fill: false
}
},
scales: {
x: [{
display: true,
gridLines: {
display: true
},
position: 'top',
labels: {
show: false,
}
}],
y: [{
type: "linear",
display: false,
position: "left",
id: "y-axis-1",
gridLines:{
display: false
},
labels: {
show:true,
}
}, {
type: "linear",
display: false,
position: "right",
id: "y-axis-2",
gridLines:{
display: false
},
labels: {
show:true,
}
}]
}
}
});
}
</script>
</body>
</html>
I have tried the code which i have already mentioned above
and got this result
my output image
but
my expected result should be I could not able to shift my sales to right axes label
答案1
得分: 1
你初始化比例尺的方式可能有误,也许这可以帮助:
scales: {
"y-axis-2": {
type: "linear",
display: true,
position: "left",
gridLines: {
display: false
},
labels: {
show: true,
}
},
"y-axis-1": {
type: "linear",
display: true,
position: "right",
gridLines: {
display: false
},
labels: {
show: true,
}
}
}
更多信息可以在这里找到:https://www.chartjs.org/docs/latest/samples/line/multi-axis.html
英文:
You are initializing the scale wrong maybe this can help
scales: {
"y-axis-2":
{
type: "linear",
display: true,
position: "left",
gridLines:{
display: false
},
labels: {
show:true,
}
}, "y-axis-1": {
type: "linear",
display: true,
position: "right",
gridLines:{
display: false
},
labels: {
show:true,
}
}
}
https://www.chartjs.org/docs/latest/samples/line/multi-axis.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论