使图表中的某些刻度变暗
Darkening certain ticks in a chart
到目前为止的图表:
我试图使 0、5 和 11 所在的刻度变暗,但我似乎无法在 chart.js 网站上找到任何信息。我正在使用 chart.js2 和 angular5。
我隐藏了其他标签,以便根据数据的长度只显示某些标签。
ngOnInit() {
this.chart = new Chart('canvas', {
type: 'line',
data: {
labels: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9','10', '11'],
datasets: [
{
label: 'test',
data: [
100, 200, 300, 500, 100, 900, 100, 200, 500
],
borderColor: '#549cef',
backgroundColor: 'rgba(0,0,0,0.0)',
pointBackgroundColor: 'white',
pointRadius: 10,
pointBorderWidth: this.getThick(),
pointHoverBackgroundColor: '#549cef',
borderWidth: 3
}
]
},
options: {
responsive: true,
scales: {
xAxes:[{
gridLines: {
drawBorder: true,
drawOnChartArea: false,
},
ticks: {
callback: function(dataLabel, index,data){
return data.length < 5? '':
data.length<12 && (index==0 || index==(data.length-1)) ? '':
(index==0 || index == 5 || index == 11)? dataLabel: '';
}
}
}],},
到变暗 x 轴标签,
您可以在 ticks
下使用以下选项
ticks: {
fontColor: '#000000',
fontStyle: 'bold',
请参阅以下工作片段...
$(document).ready(function() {
new Chart(document.getElementById('myChart').getContext('2d'), {
type: 'line',
data: {
labels: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9','10', '11'],
datasets: [
{
label: 'test',
data: [
100, 200, 300, 500, 100, 900, 100, 200, 500
],
borderColor: '#549cef',
backgroundColor: 'rgba(0,0,0,0.0)',
pointBackgroundColor: 'white',
pointRadius: 10,
//pointBorderWidth: this.getThick(),
pointHoverBackgroundColor: '#549cef',
borderWidth: 3
}
]
},
options: {
responsive: true,
scales: {
xAxes:[{
gridLines: {
drawBorder: true,
drawOnChartArea: false,
},
ticks: {
fontColor: '#000000',
fontStyle: 'bold',
callback: function(dataLabel, index,data){
return data.length < 5? '':
data.length<12 && (index==0 || index==(data.length-1)) ? '':
(index==0 || index == 5 || index == 11)? dataLabel: '';
}
}
}],
}
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>
<canvas id="myChart"></canvas>
编辑
不确定是否可以只设置轴标签上方破折号的样式,
但您可以使用一系列颜色来单独设置每条网格线的样式。
有关示例,请参见以下工作片段...
$(document).ready(function() {
new Chart(document.getElementById('myChart').getContext('2d'), {
type: 'line',
data: {
labels: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9','10', '11'],
datasets: [
{
label: 'test',
data: [
100, 200, 300, 500, 100, 900, 100, 200, 500
],
borderColor: '#549cef',
backgroundColor: 'rgba(0,0,0,0.0)',
pointBackgroundColor: 'white',
pointRadius: 10,
//pointBorderWidth: this.getThick(),
pointHoverBackgroundColor: '#549cef',
borderWidth: 3
}
]
},
options: {
responsive: true,
scales: {
xAxes:[{
gridLines: {
drawBorder: true,
drawOnChartArea: false,
},
gridLines: {
color: ['#000000', 'rgba(0, 0, 0, 0.1)', 'rgba(0, 0, 0, 0.1)', 'rgba(0, 0, 0, 0.1)', 'rgba(0, 0, 0, 0.1)', '#000000', 'rgba(0, 0, 0, 0.1)', 'rgba(0, 0, 0, 0.1)', 'rgba(0, 0, 0, 0.1)', 'rgba(0, 0, 0, 0.1)', 'rgba(0, 0, 0, 0.1)', '#000000'],
drawBorder: false
},
ticks: {
fontColor: '#000000',
fontStyle: 'bold',
callback: function(dataLabel, index,data){
return data.length < 5? '':
data.length<12 && (index==0 || index==(data.length-1)) ? '':
(index==0 || index == 5 || index == 11)? dataLabel: '';
}
}
}],
}
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>
<canvas id="myChart"></canvas>
到目前为止的图表:
我试图使 0、5 和 11 所在的刻度变暗,但我似乎无法在 chart.js 网站上找到任何信息。我正在使用 chart.js2 和 angular5。 我隐藏了其他标签,以便根据数据的长度只显示某些标签。
ngOnInit() {
this.chart = new Chart('canvas', {
type: 'line',
data: {
labels: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9','10', '11'],
datasets: [
{
label: 'test',
data: [
100, 200, 300, 500, 100, 900, 100, 200, 500
],
borderColor: '#549cef',
backgroundColor: 'rgba(0,0,0,0.0)',
pointBackgroundColor: 'white',
pointRadius: 10,
pointBorderWidth: this.getThick(),
pointHoverBackgroundColor: '#549cef',
borderWidth: 3
}
]
},
options: {
responsive: true,
scales: {
xAxes:[{
gridLines: {
drawBorder: true,
drawOnChartArea: false,
},
ticks: {
callback: function(dataLabel, index,data){
return data.length < 5? '':
data.length<12 && (index==0 || index==(data.length-1)) ? '':
(index==0 || index == 5 || index == 11)? dataLabel: '';
}
}
}],},
到变暗 x 轴标签,
您可以在 ticks
ticks: {
fontColor: '#000000',
fontStyle: 'bold',
请参阅以下工作片段...
$(document).ready(function() {
new Chart(document.getElementById('myChart').getContext('2d'), {
type: 'line',
data: {
labels: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9','10', '11'],
datasets: [
{
label: 'test',
data: [
100, 200, 300, 500, 100, 900, 100, 200, 500
],
borderColor: '#549cef',
backgroundColor: 'rgba(0,0,0,0.0)',
pointBackgroundColor: 'white',
pointRadius: 10,
//pointBorderWidth: this.getThick(),
pointHoverBackgroundColor: '#549cef',
borderWidth: 3
}
]
},
options: {
responsive: true,
scales: {
xAxes:[{
gridLines: {
drawBorder: true,
drawOnChartArea: false,
},
ticks: {
fontColor: '#000000',
fontStyle: 'bold',
callback: function(dataLabel, index,data){
return data.length < 5? '':
data.length<12 && (index==0 || index==(data.length-1)) ? '':
(index==0 || index == 5 || index == 11)? dataLabel: '';
}
}
}],
}
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>
<canvas id="myChart"></canvas>
编辑
不确定是否可以只设置轴标签上方破折号的样式,
但您可以使用一系列颜色来单独设置每条网格线的样式。
有关示例,请参见以下工作片段...
$(document).ready(function() {
new Chart(document.getElementById('myChart').getContext('2d'), {
type: 'line',
data: {
labels: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9','10', '11'],
datasets: [
{
label: 'test',
data: [
100, 200, 300, 500, 100, 900, 100, 200, 500
],
borderColor: '#549cef',
backgroundColor: 'rgba(0,0,0,0.0)',
pointBackgroundColor: 'white',
pointRadius: 10,
//pointBorderWidth: this.getThick(),
pointHoverBackgroundColor: '#549cef',
borderWidth: 3
}
]
},
options: {
responsive: true,
scales: {
xAxes:[{
gridLines: {
drawBorder: true,
drawOnChartArea: false,
},
gridLines: {
color: ['#000000', 'rgba(0, 0, 0, 0.1)', 'rgba(0, 0, 0, 0.1)', 'rgba(0, 0, 0, 0.1)', 'rgba(0, 0, 0, 0.1)', '#000000', 'rgba(0, 0, 0, 0.1)', 'rgba(0, 0, 0, 0.1)', 'rgba(0, 0, 0, 0.1)', 'rgba(0, 0, 0, 0.1)', 'rgba(0, 0, 0, 0.1)', '#000000'],
drawBorder: false
},
ticks: {
fontColor: '#000000',
fontStyle: 'bold',
callback: function(dataLabel, index,data){
return data.length < 5? '':
data.length<12 && (index==0 || index==(data.length-1)) ? '':
(index==0 || index == 5 || index == 11)? dataLabel: '';
}
}
}],
}
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>
<canvas id="myChart"></canvas>