Adding Image inside Linechart points in ChartJs

陌路散爱 提交于 2019-11-29 07:08:25

问题


I'm developing a project using ChartJs. I am trying to add icon image inside the line chart instead of points.

I'm attaching an image in which I demonstrate the above requirements. In that image, there is a ChartJs image and a reference image. I would like to add an image inside the line chart of ChartJs exactly like in the reference image(sun and cloud icon).

Is it possible in ChartJs?


回答1:


As said in the Chart.js line chart data structure (pointStyle attribute) :

pointStyle
String, Array< String >, Image, Array< Image >
The style of point. Options are 'circle', 'triangle', 'rect', 'rectRot', 'cross', 'crossRot', 'star', 'line', and 'dash'. If the option is an image, that image is drawn on the canvas using drawImage.

So you just need to edit your chart and put an image instead of the default circle in the pointStyle attribute of a specific data.


You can do it using Chart.js plugins like this :
// Variables 'sun' and 'cloud' are created before with `new Image()`

Chart.pluginService.register({
    afterUpdate: function(chart) {
        chart.config.data.datasets[0]._meta[0].data[7]._model.pointStyle = sun;
        chart.config.data.datasets[1]._meta[0].data[2]._model.pointStyle = cloud;
    }
});

And will give you this result.



来源:https://stackoverflow.com/questions/38918324/adding-image-inside-linechart-points-in-chartjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!