dougupang0901 2016-09-06 12:44
浏览 70
已采纳

谷歌线图双Y轴问题

I'm working on a dual Y-axis linechart that is to have both height and weight on the Y-axes with the date displaying on the X-axis:

I'm pulling the data from my MYSQL database and encoding it into json and the passing it to my function that draws the chart as shown:

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">

    google.load('visualization', '1.0', {'packages':['corechart']});

    google.setOnLoadCallback(drawChart);

    function drawChart() 
    {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Date');
        data.addColumn('number', 'Height');
        data.addColumn('number', 'Weight');
        data.addRows([ 
        <?php 
        $chart_info = $db->prepare("SELECT `height`, `weight`, `date_captured` FROM `child_results_capture` ");
        $chart_info->execute();
        $result = $chart_info->fetchAll(PDO::FETCH_ASSOC);

        $chart_data = '';

        foreach($result as $value)
        {
            $chart_data.="['".$value['date_captured']."',".$value['height'].",".$value['weight']."],";
            // var_dump($chart_data);exit;
            // echo $chart_data;    
        }
        echo $chart_data;
        ?> 
        ]);

        var options = {

          title: 'Height-Weight graph',

        width: 900,
        height: 500,
        series: {
          0: {axis: 'Height'},
          1: {axis: 'Weight'}
        },
        axes: {
          y: {
            Height: {label: 'Height (cm)'},
            Weight: {label: 'Weight (kg)'}
          }
        }
      };

        var chart = new google.visualization.LineChart(document.getElementById('graph_chart'));
        chart.draw(data, options);
    }   
    </script>
    <div id="graph_chart"></div>

The Json returned from my php bit looks like this:

['2016-07-27',51,3.4],['2016-08-03',52,4],['2016-08-10',53,5],['2016-08-17',54,6],['2016-08-24',55,7],['2016-08-31',56,8],

And my output looks like this:

height weight linechart

As shown above, I only get one Y-axis(for the height) and my chart axes are not labelled with the corresponding names

Will appreciate being pointed in the right direction

  • 写回答

1条回答 默认 最新

  • duanrang3357 2016-09-06 13:04
    关注

    the specific configuration options used in the question to build the Dual-Y Chart
    are for Material charts only --> google.charts.Line

    var optionsMatl = {
      title: 'Height-Weight graph',
      width: 900,
      height: 500,
      series: {
        0: {axis: 'Height'},
        1: {axis: 'Weight'}
      },
      axes: {
        y: {
          Height: {label: 'Height (cm)'},
          Weight: {label: 'Weight (kg)'}
        }
      }
    };
    

    a different set of options is needed to build a Dual-Y Chart
    in a Classic chart --> google.visualization.LineChart

    var optionsCore = {
      title: 'Height-Weight graph',
      width: 900,
      height: 500,
      series: {
        1: {
          targetAxisIndex: 1
        }
      },
      vAxes: {
        0: {
          title: 'Height (cm)'
        },
        1: {
         title: 'Weight (kg)'
        }
      },
      theme: 'material'
    };
    

    from the documentation for Dual-Y Charts...

    In the Material code ..., the axes and series options together specify the dual-Y appearance of the chart. The series option specifies which axis to use for each. The axes option then makes this chart a dual-Y chart.

    In the Classic code, this differs slightly. Rather than the axes option, you will use the vAxes option (or hAxes on horizontally oriented charts). Also, instead of using names, you will use the index numbers to coordinate a series with an axis using the targetAxisIndex option.

    see following working snippet, which draw both...

    google.charts.load('current', {packages:['corechart', 'line']});
    google.charts.setOnLoadCallback(drawChart);
    
    function drawChart() {
      var data = new google.visualization.DataTable();
      data.addColumn('string', 'Date');
      data.addColumn('number', 'Height');
      data.addColumn('number', 'Weight')
      data.addRows([
        ['2016-07-27',51,3.4],
        ['2016-08-03',52,4],
        ['2016-08-10',53,5],
        ['2016-08-17',54,6],
        ['2016-08-24',55,7],
        ['2016-08-31',56,8]
      ]);
    
        var optionsMatl = {
          title: 'Height-Weight graph',
          width: 900,
          height: 500,
          series: {
            0: {axis: 'Height'},
            1: {axis: 'Weight'}
          },
          axes: {
            y: {
              Height: {label: 'Height (cm)'},
              Weight: {label: 'Weight (kg)'}
            }
          }
        };
    
      var chartMatl = new google.charts.Line(document.getElementById('chart_div_matl'));
      chartMatl.draw(data, google.charts.Line.convertOptions(optionsMatl));
    
        var optionsCore = {
          title: 'Height-Weight graph',
          width: 900,
          height: 500,
          legend: {
            position: 'top',
            alignment: 'end'
          },
          series: {
            1: {
              targetAxisIndex: 1
            }
          },
          vAxes: {
            0: {
              title: 'Height (cm)',
            },
            1: {
             title: 'Weight (kg)'
            }
          },
          theme: 'material'
        };
    
      var chartCore = new google.visualization.LineChart(document.getElementById('chart_div_core'));
      chartCore.draw(data, optionsCore);
    }
    <script src="https://www.gstatic.com/charts/loader.js"></script>
    <div>Material Chart</div>
    <div id="chart_div_matl"></div>
    <div>Core Chart</div>
    <div id="chart_div_core"></div>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么