最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I get access to a Apache ECharts through id of DOM-Container? - Stack Overflow

programmeradmin7浏览0评论

I draw many piecharts on the map, at some point in time I need to update those piecharts using setOption. All I found they suggest saving the echart object inside the variable then update base on variables like below:

//Create
let echartsObj = echarts.init(document.getElementById("someId"));
    ...
};
//Update
echartsObj.setOption(option)

But this is not working for me because I have many echart instances and I should match them base on their id like below:

//It is not updating but I want something like that for updating echarts
someFunctionOrInstance("#echartId").setOption(option)

As I know, this approach is doable with other chart libraries like chart.js

I draw many piecharts on the map, at some point in time I need to update those piecharts using setOption. All I found they suggest saving the echart object inside the variable then update base on variables like below:

//Create
let echartsObj = echarts.init(document.getElementById("someId"));
    ...
};
//Update
echartsObj.setOption(option)

But this is not working for me because I have many echart instances and I should match them base on their id like below:

//It is not updating but I want something like that for updating echarts
someFunctionOrInstance("#echartId").setOption(option)

As I know, this approach is doable with other chart libraries like chart.js

Share Improve this question edited Sep 1, 2021 at 13:57 N_Z asked Sep 1, 2021 at 13:11 N_ZN_Z 9271 gold badge11 silver badges22 bronze badges 3
  • setOption is a method of the object that echarts.init returned, but $("#echartId") returns a jQuery instance. "But this is not working for me because I have many echart instances" - so store them into an object then, using the ID as key - then you can simply look them up in the object via that same ID ...? – C3roe Commented Sep 1, 2021 at 13:18
  • Is there any way besides saving inside object? Can we return echart instance using identifer (id, class, name) or there is no such ways?! – N_Z Commented Sep 1, 2021 at 13:39
  • I'm guessing echarts.apache/en/api.html#echarts.getInstanceByDom could do that (not directly via ID, you need to pass in a div or canvas element, but those you can select via their ID first.) – C3roe Commented Sep 1, 2021 at 13:47
Add a ment  | 

2 Answers 2

Reset to default 4

setOption is a method of the object that echarts.init returned, but $("#echartId") returns a jQuery instance.

One option would be to store these into an object, using the ID as key - then you have easy access to them, directly via that.

The other would be to use the echarts.getInstanceByDom method - that does not select by ID directly, but needs to get a div or canvas element passed in, and will then return the correct instance for that element.

window.echartid.setOption(option)

this will do.

发布评论

评论列表(0)

  1. 暂无评论