【问题标题】:How to add a RGBA color to google line chart?如何将 RGBA 颜色添加到谷歌折线图?
【发布时间】:2017-04-01 06:58:39
【问题描述】:

我正在尝试在客户的网站中嵌入谷歌折线图,它将根据运行时的一些 php 变量生成。我面临的问题是,我使用的图表的父背景(图表保存在上面)是 rgba(0,0,0,.7) 格式,这给背景带来了透明的感觉,而不是内容,而 google 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,但找不到我的问题的答案。请提出一个简单的出路。

【问题讨论】:

    标签: javascript html css charts google-visualization


    【解决方案1】:

    试试

        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>

    【讨论】:

    • 据我了解,这将使整个图表变得透明,而不仅仅是背景。
    • 与我的理解一样,属性是在backgroundColor 下定义的,它将应用于背景,它将变得透明而不是整个图表。希望您得到答案并在此处添加。这将有助于社区
    • 很好的答案,物有所值,here is an example 使用 rgba 作为系列颜色...
    【解决方案2】:
    <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>
    

    【讨论】:

    • 试试这个代码,我在选项中将 backgroundColor 设置为透明,并将
      内联样式设置为 RGBA 颜色,颜色会反射。
    • 我正在寻找插入 rgba() 颜色代码,我希望图表有不同的颜色代码,我不想让它透明以获取背景颜色。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签