如何将RGBA颜色添加到谷歌折线图?

时间:2017-04-01 06:58:40

标签: javascript html css charts google-visualization

我正在尝试在客户端的网站中嵌入谷歌线图,这将在运行时基于一些php变量生成。我面临的问题是,我正在使用的图表的父母背景(保存图表)是rgba(0,0,0,.7)格式,它给背景提供透明的感觉而不是内容,以及谷歌api给出的背景是白色的,我试图改变背景的颜色,但它接受十六进制值“#000”,它不提供透明度。任何人都可以建议一种方法来给谷歌图表的颜色rgba()。抱歉,我无法分享确切的代码,但以下内容可以作为参考。

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

      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Year', 'Sales', 'Expenses'],
          ['2004',  1000,      400],
          ['2005',  1170,      460],
          ['2006',  660,       1120],
          ['2007',  1030,      540]
        ]);

        var options = {
          title: 'Company Performance',
          curveType: 'function',
          legend: { position: 'bottom' },
          backgroundColor:'#000';
        };

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

        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="curve_chart" style="width: 900px; height: 500px"></div>
  </body>
</html>

我经历了thisthis,但找不到我的问题的答案。请提出一个简单的方法。

2 个答案:

答案 0 :(得分:2)

尝试

    backgroundColor: {
      'fill': '#000',
      'fillOpacity': 0.1 
    },

这使用带透明度的不透明度的十六进制颜色

<html>

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

    function drawChart() {
      var data = google.visualization.arrayToDataTable([
        ['Year', 'Sales', 'Expenses'],
        ['2004', 1000, 400],
        ['2005', 1170, 460],
        ['2006', 660, 1120],
        ['2007', 1030, 540]
      ]);

      var options = {
        title: 'Company Performance',
        curveType: 'function',
        legend: {
          position: 'bottom'
        },
        backgroundColor: {
          'fill': '#000',
          'fillOpacity': 0.1
        },

      };

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

      chart.draw(data, options);
    }
  </script>
</head>

<body>
  <div id="curve_chart" style="width: 900px; height: 500px"></div>
</body>

</html>

答案 1 :(得分:0)

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

      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Year', 'Sales', 'Expenses'],
          ['2004',  1000,      400],
          ['2005',  1170,      460],
          ['2006',  660,       1120],
          ['2007',  1030,      540]
        ]);

        var options = {
          title: 'Company Performance',
          curveType: 'function',
          legend: { position: 'bottom' },
          backgroundColor:'transparent',
        };
        var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));

        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="curve_chart" style="width: 900px; height: 500px;background:rgba(0,0,0,0.7)"></div>
  </body>
</html>