5

I would like to create a Google Gauge charts which should be web-responsive. I heard about setting the width to '100%' should work out, but it actually doesn't make any changes.

Following you can see my code.

<script type='text/javascript'>
      google.load('visualization', '1', {packages:['gauge']});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Label', 'Value'],
          ['Happiness', 40],

        ]);

        var options = {
          width: '100%', height: '100%',
          redFrom: 0, redTo: 40,
          yellowFrom:40, yellowTo: 70,
          greenFrom: 70, greenTo: 100,
          minorTicks: 5
        };

        var chart = new google.visualization.Gauge(document.getElementById('test'));
        chart.draw(data, options);
      }
    </script>

Please see my example .Example Test

Does anyone know how to make it web-responsive?

4

2 回答 2

17

图表不会自动调整大小。您必须设置一个调用chart.draw(data, options);调整大小的事件处理程序:

function drawChart () {
    var data = google.visualization.arrayToDataTable([
        ['Label', 'Value'],
        ['Happiness', 40]
    ]);

    var options = {
        redFrom: 0,
        redTo: 40,
        yellowFrom:40,
        yellowTo: 70,
        greenFrom: 70,
        greenTo: 100,
        minorTicks: 5
    };

    var chart = new google.visualization.Gauge(document.getElementById('test'));
    chart.draw(data, options);

    function resizeHandler () {
        chart.draw(data, options);
    }
    if (window.addEventListener) {
        window.addEventListener('resize', resizeHandler, false);
    }
    else if (window.attachEvent) {
        window.attachEvent('onresize', resizeHandler);
    }
}

在选项中设置height和设置width为 100% 不会为您做任何事情。您需要在 CSS 中为您的容器 div 设置它们:

#test {
    height: 100%;
    width: 100%;
}

请记住以下几点:除非父元素具有特定高度,否则将高度设置为 100% 不会做任何事情;此外,Gauge 的高度和宽度由较小的尺寸决定。因此,如果您在不指定容器(或其父容器)高度的情况下增加屏幕大小#test,可能的结果是图表不会改变大小。

于 2013-09-24T14:49:44.517 回答
0

您可以只在选项中分配宽度 100% 和一个带有 width="100%" 的响应式 div 标签,或者在这里可以看到现场演示的小提琴链接。

//这里是html

 <div class="row"> 
   <div class="col-xs-12 col-sm-6">
    <div id="chart_div"></div>
  </div>
 </div>

//这里是js

 var options = {'title':'How Much Pizza I Ate Last Night',
                   'width':'100%',
                  };
于 2014-05-07T10:07:49.393 回答