javascript - Can you set seperate colors for different axes in Chart.js?
问题描述
I've got a simple chart.js chart with dual axes. To improve readability, I'd like to color coordinate the chart & scale data. Is this possible?
For example, in my sample below, I'd like to have the temp values on left axis shown in red and the SG values on the right axis shown in blue.
I've tried adding fontColor
and scaleFontColor
options under the specific axis options, but no luck.
Here is my code:
var canvas = document.getElementById('myChart');
var chartData = {
labels: ['Mon', 'Tue', 'Wed', 'Thur', 'Fri'],
datasets: [{
label: 'Temp F',
yAxisID: 'F',
display: true,
fill: false,
borderColor: '#ff6384', //red
data: [100, 96, 84, 76, 69]
}, {
//type: 'line',
label: 'Gravity',
yAxisID: 'G',
display: true,
fill: false,
borderColor: '#36a2eb', //blue
data: [1.07, 1.055, 1.020, 1.015, 1.012]
}]
};
var myChart = new Chart(canvas, {
type: 'line',
data: chartData,
options: {
title: {
display: true,
text: 'Mmm Beer!'
},
scales: {
yAxes: [{
id: 'F',
position: 'left',
scaleFontColor: '#ff6384', //red
ticks: {
suggestedMin: 32,
suggestedMax: 100
}
}, {
id: 'G',
position: 'right',
ticks: {
suggestedMin: 1,
suggestedMax: 1.1
},
gridLines: {
drawOnChartArea: false, // only want the grid lines for one axis to show up
},
}]
}
}
});
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js"></script>
<div>
<canvas id="myChart"></canvas>
</div>
解决方案
I think I have solved the problem. As you are trying to change the color of both Y axis values. You need to add fontColor
property inside ticks
object for the specific yAxes
. Then assign any color value you want to fontColor
property of ticks
object.
Find the changes here
var canvas = document.getElementById('myChart');
var chartData = {
labels: ['Mon', 'Tue', 'Wed', 'Thur', 'Fri'],
datasets: [{
label: 'Temp F',
yAxisID: 'F',
display: true,
fill: false,
borderColor: '#ff6384', //red
data: [100, 96, 84, 76, 69]
}, {
//type: 'line',
label: 'Gravity',
yAxisID: 'G',
display: true,
fill: false,
borderColor: '#36a2eb', //blue
data: [1.07, 1.055, 1.020, 1.015, 1.012]
}]
};
// @ts-ignore
var myChart = new Chart(canvas, {
type: 'line',
data: chartData,
options: {
title: {
display: true,
text: 'Mmm Beer!'
},
scales: {
yAxes: [{
id: 'F',
position: 'left',
ticks: {
fontColor: '#ff6384', //red
suggestedMin: 32,
suggestedMax: 100
}
}, {
id: 'G',
position: 'right',
ticks: {
fontColor: '#36a2eb', //blue
suggestedMin: 1,
suggestedMax: 1.1
},
gridLines: {
drawOnChartArea: false, // only want the grid lines for one axis to show up
},
}]
}
}
});
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js"></script>
<div>
<canvas class=".content" id="myChart"></canvas>
</div>
Hope, this will help you. Thanks!
推荐阅读
- c++ - 为什么要使用 Dereference 运算符来声明指针?
- java - 我有一个检测脏话的我的世界插件。玩家诅咒时如何执行命令?
- graphql - GraphQL 使用来自返回结果集的动态 ID 来呈现正确的数据集
- typescript - 仅将可区分联合类型限制为当前属性
- android - 颤振 | showModalBottomSheet 基于键盘高度的底部填充,但动画很慢
- android - 如何让服务器主动通知客户端资源变化?
- sql - SQL 来识别某些列与过滤器不同的 ID
- android - 有没有办法在明暗模式之间切换后保留变量状态的值以显示在文本视图中
- java - 调用 MediaProjection.createVirtualDisplay 时无法启动已经启动的 MediaProjection 并且很少出现 NullPointerException
- javascript - VueJS 适用于 chrome 但不适用于 Safari