Google Chart 不受指定选项影响。

huangapple go评论64阅读模式
英文:

Google Chart not affected with specified options

问题

以下是您要翻译的部分:

I'm using Google Charts API and charts are displaying correctly, but any options don't seem to be working. I'm trying to customise the bar colours and remove the legend, but it's not working. Can't work out what I'm missing as it seems to be as per the documentation.
Any ideas?

function show_workpack_chart ($project, $con) {
// Get the data (works ok but removed here)
$dataset = "['Workpack Budget vs Actual Hours', 'Budget Hours', 'Hours Spent']";
$values = "";
while ($row = mysqli_fetch_assoc($result)) {
$values .= "['" . $row["name"] . "', " . $row["manhours"] . ", " . $row["hoursspent"] . "],";
}

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

  function drawChart() {
    var data = google.visualization.arrayToDataTable([
      " . $dataset . ", " . $values . "
    ]);

    var options = {
      chart: {
        title: 'Workpack Overview',
        subtitle: 'Hours spent per workpack',
        legend: 'none',
        colors: ['#e0440e', '#e6693e']
      }
    };

    var chart = new google.charts.Bar(document.getElementById('columnchart_hours'));

    chart.draw(data, google.charts.Bar.convertOptions(options));
  }
</script>";

// Now draw the chart
echo '<div id="columnchart_hours" style="width: 100%; height: 350px;"></div>';

}

英文:

I'm using Google Charts API and charts are displaying correctly, but any options don't seem to be working. I'm trying to customise the bar colours and remove the legend, but it's not working. Can't work out what I'm missing as it seems to be as per the documentation.
Any ideas?

function show_workpack_chart ($project, $con) {        
    // Get the data (works ok but removed here)
    $dataset = &quot;[&#39;Workpack Budget vs Actual Hours&#39;, &#39;Budget Hours&#39;, &#39;Hours Spent&#39;]&quot;;
    $values = &quot;&quot;;
        while ($row = mysqli_fetch_assoc($result)) {
        $values .= &quot;[&#39;&quot; . $row[&quot;name&quot;] . &quot;&#39;, &quot; . $row[&quot;manhours&quot;] . &quot;, &quot; . $row[&quot;hoursspent&quot;] . &quot;,],&quot;;
        
        }    
    
    // Load the charts library
    echo &#39;&lt;script type=&quot;text/javascript&quot; src=&quot;https://www.gstatic.com/charts/loader.js&quot;&gt;&lt;/script&gt;&#39;;
    echo &quot;&lt;script type=\&quot;text/javascript\&quot;&gt;
      google.charts.load(&#39;current&#39;, {&#39;packages&#39;:[&#39;bar&#39;]});
      google.charts.setOnLoadCallback(drawChart);

      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          &quot; . $dataset . &quot;, &quot; . $values . &quot;
        ]);

        var options = {
          chart: {
            title: &#39;Workpack Overview&#39;,
            subtitle: &#39;Hours spent per workpack&#39;,
            legend: &#39;none&#39;,
            colors: [&#39;#e0440e&#39;, &#39;#e6693e&#39;]
            
          }
        };

        var chart = new google.charts.Bar(document.getElementById(&#39;columnchart_hours&#39;));

        chart.draw(data, google.charts.Bar.convertOptions(options));
      }
    &lt;/script&gt;&quot;;
    
    // Now draw the chart
    echo &#39;&lt;div id=&quot;columnchart_hours&quot; style=&quot;width: 100%; height: 350px;&quot;&gt;&lt;/div&gt;&#39;;
}

Specifically, the following has no effect:

legend: &#39;none&#39;,
colors: [&#39;#e0440e&#39;, &#39;#e6693e&#39;]

答案1

得分: 1

the colors and legend options should not be defined within chart

var options = {
  chart: {
    title: 'Workpack Overview',
    subtitle: 'Hours spent per workpack'
  },
  legend: 'none',
  colors: ['#e0440e', '#e6693e']
};

EDIT

to remove the legend, try using the following option...

legend: {
  position: 'none'
},

see following working snippet...

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

google.charts.load('current', {
  packages: ['bar']
}).then(drawChart);

function drawChart() {
  var data = google.visualization.arrayToDataTable([
    ['Workpack Budget vs Actual Hours', 'Budget Hours', 'Hours Spent'],
    ['Test', 12, 10]
  ]);

  var options = {
    chart: {
      title: 'Workpack Overview',
      subtitle: 'Hours spent per workpack',
    },
    legend: {
      position: 'none'
    },
    colors: ['#e0440e', '#e6693e']
  };

  var chart = new google.charts.Bar(document.getElementById('columnchart_hours'));
  chart.draw(data, google.charts.Bar.convertOptions(options));
}


<!-- language: lang-html -->

<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="columnchart_hours" style="width: 100%; height: 350px;"></div>


<!-- end snippet -->
英文:

the colors and legend options should not be defined within chart

    var options = {
      chart: {
        title: &#39;Workpack Overview&#39;,
        subtitle: &#39;Hours spent per workpack&#39;
      },
      legend: &#39;none&#39;,
      colors: [&#39;#e0440e&#39;, &#39;#e6693e&#39;]
    };

EDIT

to remove the legend, try using the following option...

legend: {
  position: &#39;none&#39;
},

see following working snippet...

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

google.charts.load(&#39;current&#39;, {
  packages: [&#39;bar&#39;]
}).then(drawChart);

function drawChart() {
  var data = google.visualization.arrayToDataTable([
    [&#39;Workpack Budget vs Actual Hours&#39;, &#39;Budget Hours&#39;, &#39;Hours Spent&#39;],
    [&#39;Test&#39;, 12, 10]
  ]);

  var options = {
    chart: {
      title: &#39;Workpack Overview&#39;,
      subtitle: &#39;Hours spent per workpack&#39;,
    },
    legend: {
      position: &#39;none&#39;
    },
    colors: [&#39;#e0440e&#39;, &#39;#e6693e&#39;]
  };

  var chart = new google.charts.Bar(document.getElementById(&#39;columnchart_hours&#39;));
  chart.draw(data, google.charts.Bar.convertOptions(options));
}

<!-- language: lang-html -->

&lt;script src=&quot;https://www.gstatic.com/charts/loader.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;columnchart_hours&quot; style=&quot;width: 100%; height: 350px;&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月5日 02:58:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402003.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定