【问题标题】:Point Style property with Inverted Image in Line Chart.js折线图.js中带有倒置图像的点样式属性
【发布时间】:2018-09-24 07:30:35
【问题描述】:

参考小提琴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 度时遇到问题

【问题讨论】:

  • 嗨,这个答案有帮助吗?

标签: chart.js chart.js2


【解决方案1】:

Chart js 允许提供pointStyles 的数组,而不仅仅是 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);

【讨论】:

    猜你喜欢
    • 2012-08-23
    • 2015-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多