Google Chart 未占用 100% 的空间

Rod*_*lis 6 charts google-visualization

首先,对这个例子大喊大叫:https : //www.c-sharpcorner.com/UploadFile/ca9151/google-charts-api-using-database-in-Asp-Net/ 如果你想要一个简单的演练构建谷歌图表,那就完美了。

在说......在那个例子中,在我的项目中......我无法让图表从指定部分的左侧开始。它不断缩进。当我隐藏轴标签时,情况会更糟,因为由于缺少单词,空间会变得更大。

在此处输入图片说明

有谁知道如何让它们占据 100% 的宽度?(在图像中,我希望图表从红线开始,一直到 div 的另一侧)

提前致谢。

Whi*_*Hat 8

默认情况下,图表不会填充容器的宽度和高度。

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

chartArea: {
  height: '100%',
  width: '100%',
  top: 48,
  left: 48,
  right: 16,
  bottom: 48
},
height: '100%',
width: '100%',
Run Code Online (Sandbox Code Playgroud)

在“调整大小”上重新绘制图表以使其响应,
请参阅以下工作片段...

chartArea: {
  height: '100%',
  width: '100%',
  top: 48,
  left: 48,
  right: 16,
  bottom: 48
},
height: '100%',
width: '100%',
Run Code Online (Sandbox Code Playgroud)
google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  var data = google.visualization.arrayToDataTable([
    ['Month', 'Bolivia', 'Ecuador', 'Madagascar', 'Average'],
    ['2019/01', 1, 2, 7, 3],
    ['2019/02', 5, 1, 3, 3.5],
    ['2019/03', 4, 3, 9, 6],
    ['2019/04', 1, 3, 8, 5],
    ['2019/05', 2, 6, 8, 4],
    ['2019/06', 3, 1, 9, 3],
  ]);

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

    title : 'Monthly Coffee Production by Country',
    vAxis: {title: 'Cups'},
    hAxis: {title: 'Month'},
    seriesType: 'bars',
    series: {3: {type: 'area'}}
  };

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

  drawChart();
  window.addEventListener('resize', drawChart, false);
  function drawChart() {
    chart.draw(data, options);
  }
});
Run Code Online (Sandbox Code Playgroud)
html, body {
  height: 100%;
  margin: 0px 0px 0px 0px;
  overflow: hidden;
  padding: 0px 0px 0px 0px;
}

#chart {
  height: 100%;
}
Run Code Online (Sandbox Code Playgroud)