向Google组合图添加折线

时间:2019-03-01 11:28:10

标签: php sql charts google-visualization

我有一个组合图在x轴上显示员工的姓名和他们休假的天数,我想在图表中添加一行以显示他们可以休假的最长假天数。我该怎么办?手动将其添加到图表中 以下是我的代码

<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(drawVisualization);

  function drawVisualization() {
    // Some raw data (not necessarily accurate)
    var data = google.visualization.arrayToDataTable([
       ['Employee', 'Holiday Days Taken'],
       <?php 
     while($row=$res->fetch_assoc())
     {
        echo "['".$row["name"]."',".$row["Holidays_taken"]."],"; 
     }
     ?>
                 
                    ]);
                       data.addColumn('number', 'Average');
                        data.addRows([
    [null, null, 20]
    
]);
                 

    var options = {
      title : 'Monthly Coffee Production by Country',
      vAxis: {title: 'No. of Holidays Taken'},
      hAxis: {title: 'Employee'},
      seriesType: 'bars',
      series: {5: {type: 'line'}}
    };

    var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  }
</script>

我们将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:0)

在组合图表中添加一条线,
向数据表中添加另一列以获取最大值。
然后将最大值添加到每一行...

var data = google.visualization.arrayToDataTable([
  ['Employee', 'Holiday Days Taken', 'Max'],
  ['Jim', 5, 12],
  ['Bob', 3, 12],
  ['Tom', 6, 12],
]);

在图表选项中,将最大的系列类型更改为线。
序列号是数据表中y轴列的从零开始的索引。
在这种情况下,有两个y轴列,
所以序列号是1 ...

series: {1: {type: 'line'}}

请参阅以下工作片段...

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  var data = google.visualization.arrayToDataTable([
    ['Employee', 'Holiday Days Taken', 'Max'],
    ['Jim', 5, 12],
    ['Bob', 3, 12],
    ['Tom', 6, 12],
  ]);

  var options = {
    title : 'Monthly Coffee Production by Country',
    vAxis: {title: 'No. of Holidays Taken'},
    hAxis: {title: 'Employee'},
    seriesType: 'bars',
    series: {1: {type: 'line'}}
  };

  var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
  chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>