2

I am trying to embed google line chart in a client's website, which will be generated on basis of some php variables in run-time. The issue that I am facing is, the chart's parent's background ( on which the chart is kept) that I am using is in rgba(0,0,0,.7) format, which gives a transparent feel to the background not to the content, and the background that google api gives is a white one, I tried to change the color of the background, but it accepts hex values "#000", which do not provide transparency. Can anyone suggest a way to give a rgba() as color to google chart. Sorry I cannot share the exact code, but the below can be taken as reference.

  <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>

I went through this and this but cannot find an answer to my question. Please suggest a simple way out.

Community
  • 1
  • 1
Vikash Mishra
  • 349
  • 3
  • 18

2 Answers2

2

try with

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

this uses hex color with opacity for transparent

<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>
Deep 3015
  • 9,929
  • 5
  • 30
  • 54
  • As per my understanding this will make the entire chart go transparent not only the background. – Vikash Mishra Apr 01 '17 at 08:53
  • At the same with my understanding also, attributes are defined under `backgroundColor` it will applied to background and it wil go transparent and not the entire chart. Hope you get the answer and add here. This will help community – Deep 3015 Apr 01 '17 at 16:26
  • good answer, for what its worth, [here is an example](http://stackoverflow.com/a/40284460/5090771) of using rgba for the series colors... – WhiteHat Apr 02 '17 at 00:36
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>
Kinjal Akhani
  • 168
  • 12
  • Try this code in this i have set backgroundColor to transparent in options and set
    inline style to RGBA color and the color will reflect.
    – Kinjal Akhani Apr 01 '17 at 07:51
  • I am looking to insert rgba() color code, I want the chart to have a different color code for it, I dont want to make it transparent to take the background color. – Vikash Mishra Apr 01 '17 at 08:36