本文介绍了Foundation 6 中的 Google Chart 太窄了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的网站转换为使用 Foundation 6.它的大部分工作正常,但我在使用 Google 图表时遇到了问题.我已将图表放在大 12 单元格中:

<div id="chart_div">

但图表太窄 - 它应该和顶部菜单一样宽:

解决方案

默认情况下,图表不会完全填满容器的宽度
请参阅以下代码段中的 cyan 部分...

google.charts.load('current', {包:['corechart']}).then(函数(){var statsData = 新的 google.visualization.DataTable();statsData.addColumn('number', 'x');statsData.addColumn('number', 'y');statsData.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]]);变量选项 = {背景颜色:'青色',颜色:['洋红色'],标题:'标题',高度:600,传奇: {位置:'底部'},酒吧: {组宽度:'75%'},isStacked: 假};var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));图表绘制(统计数据,选项);});
<script src="https://www.gstatic.com/charts/loader.js"></script><div id="chart_div"></div>

使用 chartArea 选项将图表拉伸到容器的边缘,
使用 topleftbottomright 为轴标签、标题等留出空间.>

chartArea: {高度:'100%',宽度: '100%',顶部:48,左:48,右:16,底部:48},

'resize' 上重新绘制图表以使其具有响应性...

请参阅以下工作片段...

google.charts.load('current', {包:['corechart']}).then(函数(){var statsData = 新的 google.visualization.DataTable();statsData.addColumn('number', 'x');statsData.addColumn('number', 'y');statsData.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]]);变量选项 = {背景颜色:'青色',颜色:['洋红色'],//设置图表区域大小图表区域:{高度:'100%',宽度: '100%',顶部:48,左:48,右:16,底部:48},高度:'100%',宽度: '100%',标题:'标题',传奇: {位置:'底部'},酒吧: {组宽度:'75%'},isStacked: 假};var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));绘制图表();window.addEventListener('resize', drawChart, false);函数 drawChart() {图表绘制(统计数据,选项);}});
html, body {高度:100%;边距:0px 0px 0px 0px;填充:0px 0px 0px 0px;}#chart_div {高度:600px;}
<script src="https://www.gstatic.com/charts/loader.js"></script><div id="chart_div"></div>

I am trying to convert my website to use Foundation 6. Most of it works OK, but I have a problem with Google charts. I have put my chart inside large-12 cell:

<div class="large-12 cell">
            <div id="chart_div">
            </div>
 </div>

but the chart is too narrow - it should be as wide as the top menu:

解决方案

by default, the chart does not fully fill the width of the container
see cyan section in following snippet...

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  var statsData = new google.visualization.DataTable();
  statsData.addColumn('number', 'x');
  statsData.addColumn('number', 'y');
  statsData.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 = {
    backgroundColor: 'cyan',
    colors: ['magenta'],
    title: 'title',
    height: 600,
    legend: {
      position: 'bottom'
    },
    bar: {
      groupWidth: '75%'
    },
    isStacked: false
  };

  var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
  chart.draw(statsData, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

use chartArea options to stretch the chart to the edges of the container,
use top, left, bottom, right to leave room for the axis labels, title, etc.

chartArea: {
  height: '100%',
  width: '100%',
  top: 48,
  left: 48,
  right: 16,
  bottom: 48
},

redraw the chart on 'resize' to make it responsive...

see following working snippet...

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  var statsData = new google.visualization.DataTable();
  statsData.addColumn('number', 'x');
  statsData.addColumn('number', 'y');
  statsData.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 = {
    backgroundColor: 'cyan',
    colors: ['magenta'],

    // set chart area size
    chartArea: {
      height: '100%',
      width: '100%',
      top: 48,
      left: 48,
      right: 16,
      bottom: 48
    },
    height: '100%',
    width: '100%',

    title: 'title',
    legend: {
      position: 'bottom'
    },
    bar: {
      groupWidth: '75%'
    },
    isStacked: false
  };

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

  drawChart();
  window.addEventListener('resize', drawChart, false);
  function drawChart() {
    chart.draw(statsData, options);
  }

});
html, body {
  height: 100%;
  margin: 0px 0px 0px 0px;
  padding: 0px 0px 0px 0px;
}

#chart_div {
  height: 600px;
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

这篇关于Foundation 6 中的 Google Chart 太窄了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:59