无法将字体设置应用于Google图表

时间:2019-06-01 22:15:10

标签: html css

我正在尝试在Google图表(甘特图)上将字体更改为Roboto,在控制台中出现错误:ReferenceError:未定义Roboto

我按照Google字体网站上的说明进行操作,但是我是初学者,所以可能错过了一些东西,我无法弄清楚。

HTML:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link href="https://fonts.googleapis.com/css?family=Roboto&display=swap" rel="stylesheet"> 
    <link rel="stylesheet" type="text/css" href="style.css">
  <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
  <script type="text/javascript">
    google.charts.load('current', {'packages':['gantt']});
    google.charts.setOnLoadCallback(drawChart);

    function daysToMilliseconds(days) {
      return days * 24 * 60 * 60 * 1000;
    }

    function drawChart() {

      var data = new google.visualization.DataTable();
      data.addColumn('string', 'Task ID');
      data.addColumn('string', 'Task Name');
      data.addColumn('date', 'Start Date');
      data.addColumn('date', 'End Date');
      data.addColumn('number', 'Duration');
      data.addColumn('number', 'Percent Complete');
      data.addColumn('string', 'Dependencies');

      data.addRows([
        ['Research', 'Find sources',
         new Date(2015, 0, 1), new Date(2015, 0, 5), null,  100,  null],
        ['Write', 'Write paper',
         null, new Date(2015, 0, 9), daysToMilliseconds(3), 25, 'Research,Outline'],
        ['Cite', 'Create bibliography',
         null, new Date(2015, 0, 7), daysToMilliseconds(1), 20, 'Research'],
        ['Complete', 'Hand in paper',
         null, new Date(2015, 0, 10), daysToMilliseconds(1), 0, 'Cite,Write'],
        ['Outline', 'Outline paper',
         null, new Date(2015, 0, 6), daysToMilliseconds(1), 100, 'Research']
      ]);

        var options = {
        height: 275,
        gantt: {
          labelStyle: {
            fontName: Roboto,
            fontSize: 14,
            color: '#757575'
          }
        }
      };

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

      chart.draw(data, options);
    }
  </script>
</head>
<body>
  <div id="chart_div"></div>
</body>
</html>

CSS:

#chart_div {
font-family: 'Roboto', sans-serif;
}

当我运行网站时,图表不会加载。在控制台中,我看到错误: ReferenceError:未定义Roboto

1 个答案:

答案 0 :(得分:0)

您看到错误ReferenceError: Can't find variable: Roboto,因为必须在“ fontName”上加上引号。在这种情况下,“ Roboto”是带有字体名称的字符串,而不是变量。

因此,您应该在脚本中使用

labelStyle: {
  fontName: 'Roboto',
  fontSize: 14,
  color: '#757575'
}