首页 文章

更改谷歌条形图上的字体

提问于
浏览
1

我正在尝试更改Google条形图上所有内容的字体 . 它适用于除工具提示之外的所有内容 . 至少,我认为它被称为工具提示 - 它是鼠标悬停在栏上时显示的字体 . 根据文档,我认为我正确地做到了:

<!doctype html>
<html>
    <head>
        <style>
            @font-face {
                font-family: bebas;
                src: url(BebasNeue.otf);
            }
        </style>
    </head>
    <body>
        <h1 style="font-family:bebas;">This font changed</h1>
        <script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1.1','packages':['bar']}]}"></script>
               <div id="cheese_plot" style="width: 900px; height: 500px; font-family:bebas;"></div>

        <script type="text/javascript">
            google.setOnLoadCallback(drawStuff);

            var rawdata = [["Swiss", 90],
                ["Cheddah", 75],
                ["'Merican", 35],
                ["Le bleu", 65],
                ['Stinky', 70]];
            var labels = [['Cheese', 'Pieces']];

            var fullArray = labels.concat(rawdata);

            function drawStuff() {
                var data = new google.visualization.arrayToDataTable(
                    fullArray
                );



                var options = {
                    annotations: {textStyle: { fontName: 'bebas' }},
                    hAxis: {textStyle: { fontName: 'bebas' }, titleTextStyle: { fontName: 'bebas' }},
                    vAxis: {textStyle: { fontName: 'bebas' }, titleTextStyle: { fontName: 'bebas' }},
                    titleTextStyle: { fontName: 'bebas' },
                    tooltip: {textStyle: {fontName: 'bebas' }},
                    fontName: 'bebas',
                    fontSize: 24,
                    title: 'Cheeseses',
                    width: 900,
                    legend: { position: 'none' },
                    chart: { title: 'Cheese',
                    subtitle: 'pieces' },
                    bars: 'horizontal',
                    axes: {
                    x:  {0: { side: 'top', label: 'Pieces'}}},
                    bar: { groupWidth: "90%" }
                };

                var chart = new google.charts.Bar(document.getElementById('cheese_plot'));
                chart.draw(data, google.charts.Bar.convertOptions(options));
              };
        </script>
    </body>
</html>

以下是文档:https://developers.google.com/chart/interactive/docs/gallery/barchart?hl=it#data-format

我想使用我从这里得到的自定义字体:http://www.dafont.com/bebas-neue.font

这是一个谷歌图表错误,我做错了什么;这有可能吗?

1 回答

  • 3

    材料图表仍处于测试阶段,因此 convertOptions 可能只是不适用于工具提示 . 请改用CSS .

    #cheese_plot text{
        font-family:'bebas' !important;
    }
    

    Edit: 工具提示嵌套在两个 <g> 元素中,而其他 text 块都没有,因此您可以像这样隔离工具提示CSS,如果您愿意:

    #cheese_plot g g text{
        font-family:'bebas' !important;
        fill:red !important;
    }
    

    JSFiddle

相关问题