ECharts: How to use the resize event of the window?

20,765

Solution 1

var plot = echarts.init(yourDom);
plot.setOption({...});
window.onresize = function() {
  plot.resize();
};

Solution 2

        window.onresize = function() {
            $(".ga-charts").each(function(){
                var id = $(this).attr('_echarts_instance_');
                window.echarts.getInstanceById(id).resize();
            });
        };

Solution 3

var allCharts = $('.charts');

setTimeout(function(){
  for(var i=0;i<allCharts.length;i++){
    var $item = $(allCharts[i]);
    echarts.getInstanceById($item.attr('_echarts_instance_')).resize();
  }
},100) `

Solution 4

With jQuery:

    // resize all charts
    $(window).on('resize', function(){
        $("[_echarts_instance_]").each(function(){
            window.echarts.getInstanceById($(this).attr('_echarts_instance_')).resize()
        });
    });
Share:
20,765
Lolo
Author by

Lolo

Updated on July 09, 2022

Comments

  • Lolo
    Lolo almost 2 years

    Im trying the Echarts library for my graphs. I'd like to resize the plots when the window's resize trigger is fired but I can't find the way to do it.

    Thanx for your help

  • Wtower
    Wtower almost 9 years
    Hi and welcome to Stack Overflow! Although your answer may solve the problem, I would recommend you to improve its quality by including the essential parts on why it is helpful.
  • Artjom B.
    Artjom B. almost 9 years
    You can edit your answer to include such a description.
  • J. Chomel
    J. Chomel over 7 years
    While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
  • Admin
    Admin over 7 years
    @J.Chomel thx for your suggestion. i will pay attention to it.
  • J. Chomel
    J. Chomel over 7 years
    You can still edit your answer if you think it could be helpful to anyone
  • Mecanik
    Mecanik almost 7 years
    Excellent solution! There is no description needed, all you have to do is set a class for your charts eg: "ga-charts".
  • Akshay
    Akshay over 4 years
    @GeorgeP indeed that's a perfect answer but it's failing when i use same component but instances are diffrent ?
  • GeorgeP
    GeorgeP over 4 years
    are you adding it for every instance? yourDom should be for each of your charts dom elements.