使谷歌图表响应

Pat*_* S. 4 html css google-visualization responsive-design

我对 HTML、CSS 和 Javascript 比较陌生。我正在编写一个 JSP 应用程序(我是一名 Java 程序员)并且一直在学习响应式网页设计。我在让一些谷歌图表与响应式设计一起工作时遇到了麻烦。我在页面上并排放置了两个图表。

我什至用了一个例子来说明如何在 codepen 上获得响应式谷歌图表。当我在 codepen 的网站上使用它时,该示例有效。但是当我将它保存在我的电脑上并打开文件时,图表不再响应。这让我觉得不是代码本身有问题,而是我的浏览器有问题。任何帮助将不胜感激。

这是我的 JSP 应用程序的代码。HTML:

<div class="charts">
            <div class="pieChartDiv">
                <div id="piechart"></div>
            </div>
            <div class="lineChartDiv">
                <div id="chart_div"></div>
            </div>
        </div>
Run Code Online (Sandbox Code Playgroud)

CSS:

.charts {
    width: 80%;
    height: 35%;
    border: 1px solid blue;
    overflow: hidden;
    margin: auto;
    margin-top: 50px;
    background: #CFC3F8;
    box-shadow: 10px 10px 5px #888888;
}

.pieChartDiv {
    padding: 0;
    float: right;
    width: 50%;
    height: 100%;
    border-left: 1px solid white;
}

.lineChartDiv {
    float: left;
    width: 50%;
    height: 100%;
}

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

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

@media only screen and (max-width: 460px) {
    .charts {
        width: 80%;
        height: 70%;
    }
    .pieChartDiv {
        width: 100%;
        height: 50%;
        border: none;
        border-bottom: 1px solid white;
    }
    .lineChartDiv {
        width: 100%;
        height: 50%;
    }
}
Run Code Online (Sandbox Code Playgroud)

图表的 Javascript:

<script type="text/javascript"
    src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('43', {packages: ['corechart']});
google.charts.setOnLoadCallback(drawChart);
      function drawChart() {

        var data = google.visualization.arrayToDataTable([
          ['Task', 'Hours per Day'],
          ['Cakes',     11],
          ['Cupcakes',      2],
          ['Cookies',  2],
          ['Other', 2],
        ]);

        var options = {
          title: 'Product Sales',
              'legend':'left',
              'is3D':true,
              'width':550,
              'height':320,
                backgroundColor: '#CFC3F8',

        };

        var chart = new google.visualization.PieChart(document.getElementById('piechart'));

        chart.draw(data, options);
      }
    </script>
<script>
google.charts.setOnLoadCallback(drawBasic);

function drawBasic() {

      var data = new google.visualization.DataTable();
      data.addColumn('number', 'X');
      data.addColumn('number', 'Sales');

      data.addRows([
        [0, 0],   [1, 10],  [2, 23],  [3, 17],  [4, 18],  [5, 9],
        [6, 11],  [7, 27],  [8, 33],  [9, 40],  [10, 32], [11, 35],
        [12, 30], [13, 40], [14, 42], [15, 47], [16, 44], [17, 48],
        [18, 52], [19, 54], [20, 42], [21, 55], [22, 56], [23, 57],
        [24, 60], [25, 50], [26, 52], [27, 51], [28, 49], [29, 53],
        [30, 55], [31, 60], [32, 61], [33, 59], [34, 62], [35, 65],
        [36, 62], [37, 58], [38, 55], [39, 61], [40, 64], [41, 65],
        [42, 63], [43, 66], [44, 67], [45, 69], [46, 69], [47, 70],
        [48, 72], [49, 68], [50, 66], [51, 65], [52, 67], [53, 70],
        [54, 71], [55, 72], [56, 73], [57, 75], [58, 70], [59, 68],
        [60, 64], [61, 60], [62, 65], [63, 67], [64, 68], [65, 69],
        [66, 70], [67, 72], [68, 75], [69, 80]
      ]);

      var options = {
        hAxis: {
          title: 'Time'
        },
        vAxis: {
          title: 'Popularity'
        },
        hAxis: {gridlines: {color: '#1E4D6B'}},
        vAxis: {gridlines: {color: '#1E4D6B'}},


      legend: { position: 'top' },
      backgroundColor: '#CFC3F8' 
      };

      var chart = new google.visualization.LineChart(document.getElementById('chart_div'));

      chart.draw(data, options);
    }
</script>
Run Code Online (Sandbox Code Playgroud)

页面截图和小页面截图:

谷歌图表

无响应图表

第一个图表似乎不会调整大小。codepen 中的示例以相同的方式起作用。

代码笔示例:

https://codepen.io/flopreynat/pen/BfLkA

我可能会离开,所以请耐心等待。任何建议都会非常有帮助。

Wou*_*ter 7

我相信这段代码可能是您的解决方案,如果窗口大小发生变化,它会调整图表的大小(“反应式响应”):

$(window).resize(function(){
        ChartFunctionName();
        });
Run Code Online (Sandbox Code Playgroud)

我是初学者,但这是我发现并用来使我的 Google 图表具有响应性并且它有效的方法:仅供参考此示例页面,您可以在其中查看 Google 图表 -> 我使用的完整代码:

function TekenBasisschoolUitstroom() 

    {

        var data = new google.visualization.arrayToDataTable([
          ['School', 'Speciaal-praktijk-kader', 'VMBO-kader', 'VMBO-T-voorheen MAVO', 'VMBO-T/HAVO', 'HAVO', 'HAVO/VWO', 
      'VWO'],
      ['De Ster', 1, 0, 0, 0, 3, 0, 1], ['OBS Driespan', 8, 3, 13, 1, 27, 0, 20] ]);



        var schoolUitstroomOpties = {
        'title':'Uitstroom basisscholen #schooljaar 2014-2015 @Harkstede',
        isStacked: 'percent',
        legend: {position: 'right', maxLines: 3},

        vAxis: {
          minValue: 0, 
          ticks: [0, .2, .4, .6, .8, 1]
          },


        'chartArea': {left:40,'width': '80%', 'height': '80%'},
        animation:{
        startup: true,
        duration: 2000,
        easing: 'out', },
    };

        var schoolUitstroomChart = new google.visualization.ColumnChart(document.getElementById('uitstroom_basisschool'));


      google.visualization.events.addOneTimeListener(schoolUitstroomChart, 'ready', selectHandler);

    var aantalScholen = data.getNumberOfRows()
      function selectHandler() {
      console.log ('schoolUitstroomChart is ready en er zijn ' + (aantalScholen) + ' scholen');
        }


        schoolUitstroomChart.draw(data, schoolUitstroomOpties);


    $(window).resize(function(){
        TekenBasisschoolUitstroom();
        }); 
  }
Run Code Online (Sandbox Code Playgroud)