Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

echarts Media queries not working with Bootstrap (carousel)

I have a bootstrap carousel with 2 slides. Both the slides uses bootstrap grids of 2x2 to display charts on them. They are working fine , however I am trying to implement responsive media queries and can't seem to make it work. I used baseOptions and media options to define but the charts do not load up and the console doesn't show any error.

I have tried to define the <div> container with inline style width and height i.e. style="width: 100%;height:400px;" and I have also tried to define the width and height in CSS like so-

.mychart {
        width: 100%;
        height: 400px;
      }

The javascript looks like below.

<div id="chart1" style="width: 100%;height:400px;"></div> //with inline style
<div id="chart1" class="mychart"></div> //with CSS class

var myChart1 = echarts.init(document.getElementById('chart1'));
var lpnArr  = [101, 43, 10, 250];
option = {
               title : {
                          text: 'My data Pie',
                          subtext: 'Data as of last batch',
                          x:'center'
                        },
           tooltip : {
                        trigger: 'item',
                        formatter: "{b} : {c} ({d}%)"
                        },
           legend: {
                        orient : 'vertical',
                        x : 'left',
                        data:['Processed','Unprocessed','In RIB','Errors']
                       },
          color: ['#4bc14f','#ff7f50','#da70d6','#d8316f'],
          toolbox: {
                            show : true,
                            feature : {
                                              mark : {show: false},
                                              dataView : {show: false},
                                              magicType : { show: false},
                                              dataZoom : { show : false},
                                              restore : {show: true},
                                              saveAsImage : {show: true}
                                           }
                          },
          calculable : true,
          series : [
                        {
                            name:'Processed',
                            type:'pie',
                            radius : ['50%', '70%'],
                            startAngle : 230,
                            center: ['35%', '50%'],
                            itemStyle : labelFormatter,   //custom formatter
                            data: lpnArr //load chart with data array lpnArr
                        }
                      ]
        }

var mediaQuery = [
                              {   
                                  option: {
                                      series: [
                                          {
                                              radius: ['50%', '70%'],
                                              center: ['35%', '50%']
                                          }
                                      ]
                                  }
                              },
                              {
                                  query: {
                                      minAspectRatio: 1
                                  },
                                  option: {
                                      series: [
                                          {
                                              radius: ['50%', '70%'],
                                              center: ['35%', '50%']
                                          }
                                      ]
                                  }
                              },
                              {
                                  query: {
                                      maxAspectRatio: 1
                                  },
                                  option: {
                                      series: [
                                          {
                                              radius: ['50%', '70%'],
                                              center: ['50%', '35%']
                                          }
                                      ]
                                  }
                              },
                              {
                                  query: {
                                      maxWidth: 500
                                  },
                                  option: {
                                      series: [
                                          {
                                              radius: ['50%', '70%'],
                                              center: ['50%', '35%']
                                          }
                                      ]
                                  }
                              }
                          ];

myChart1.setOption({baseOption : option, 
                         media : mediaQuery});

if I use it without the media query it works just fine. like this

myChart1.setOption(option);

Using the media query, as per the documentation along with baseOptions and media, the charts simply don't load and it doesn't show me any error in the console as well, so I am not sure if I am using it correctly or not.

By the way, I also have this in the end of the script to be able to resize the charts when the window is resized but then I realized that this is not truly responsive -

$(window).on('resize', function(){
                  myChart1.resize();
                });

The bootstrap chart elements are <div> elements within other div with a class of BS container-fluid. The carousel code can be found in this JSFiddle.

like image 644
AnBisw Avatar asked May 03 '17 02:05

AnBisw


1 Answers

Media query was introduced in echarts version 3, whereas I have been using version 2 and thus it wasn't working. It's extremely odd that using media with version 2 doesn't show any kind of error, neither does baseOption show any error so I was left scratching my head. Documentation isn't clear enough to say that media queries is only supported in version 3. So after scouring through the code and GitHub threads for hours and trying out all their js versions I came to this conclusion.

The downside of using version 3 echarts is that it is missing the "Tree" chart type and it's a lot more pickier than any prior version. Also, it's twice as much slower than version 2.

like image 138
AnBisw Avatar answered Oct 13 '22 17:10

AnBisw