请参阅小提琴URL:https://jsfiddle.net/vL0ta76w/
当前行为 Chart.js中的“点样式”属性提供了某些图标,例如圆形,三角形,矩形等,这些图标将显示在折线图上
var options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [
{
label: '# of Votes',
data: [12, -19, 3, 5, 2, 3],
borderWidth: 1,
pointStyle:'triangle',
pointRadius:'10',
pointBackgroundColor:'red'
},
],
pointStyle:'triangle'
},
options: {
scales: {
yAxes: [{
ticks: {
reverse: false
}
}]
}
}
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
canvas { background-color : #eee;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.3.0/Chart.js"></script>
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
</body>
预期结果:
但是可以显示倒三角形吗?任何显示倒三角形或可插入的自定义图像的属性 通过该方法,如果数据集具有负值,我可以设法更改颜色。,但是在将图像反转或旋转180度时遇到问题
答案 0 :(得分:1)
图表js允许提供pointStyle
的数组而不是仅提供1。它还可以提供图像来代替预设的指针样式。
var image = document.getElementById('source');
var options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, -19, 3, 5, 2, 3],
borderWidth: 1,
pointStyle: ['triangle', image, 'triangle', 'triangle', 'triangle', 'triangle'],
pointRadius: '10',
pointBackgroundColor: 'red'
},
],
pointStyle: 'triangle'
},
options: {
scales: {
yAxes: [{
ticks: {
reverse: false
}
}]
}
}
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.3.0/Chart.js"></script>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<div style="display:none;">
<img id="source" src="https://image.flaticon.com/icons/png/128/25/25224.png">
</div>
然后要使其动态化,可以在数据集中映射数据以生成pointerStyle
数组
let pointerStyles = [12,-19,2,3,4].map(value=>{
return value >=0 ? 'triangle' : 'other-triangle'
});
console.log(pointerStyles);