Morris.js chart not working inside of a bootstrap tab

前端 未结 2 1261
[愿得一人]
[愿得一人] 2021-02-14 02:23

I have a situation where I am trying to use MorrisJS charts inside two different bootstrap tabs. The chart loads fine in the first (default) tab but when I click into the secon

2条回答
  •  故里飘歌
    2021-02-14 02:37

    I had the same problem and finally solved it. And just wanted to share. Hope it helps you!.

      $('a[data-toggle="tab"]').on('shown.bs.tab', function(e) {
        var target = $(e.target).attr("href") // activated tab
    
        switch (target) {
    
          case "#bootstrap_tab_id": // bootstrap tab id
    
          $("#morris_chart").empty(); // destroy chart
    
          bar =  Morris.Donut({        // set chart
            element: 'morris_chart',
            resize: true,
    
            data: [
              {label: "Download Sales", value: 12},
              {label: "In-Store Sales", value: 30},
              {label: "Mail-Order Sales", value: 20}
            ]
          }); 
    
            bar.redraw();
            $(window).trigger('resize');
            break;
          default:
          $("#morris_chart").empty();
    
           break;
        }
      });
    

提交回复
热议问题