dongmou5628 2016-08-04 18:48
浏览 78

使用谷歌折线图的多线

I'm trying to display a graph using Google Line Chart but I'm not sure how I can have 2 lines in one chart and preferably only using 1 php file. 1 positive and 1 negative. Do I need 2 array and 2 query?

This is the table from my database with the values for my line chart. There is no primary key in the table.

+--------------------------------+
| direction  |  xaxis  |  yaxis  |
+--------------------------------+
|  negative  |   5     |  -2.32  |
|  negative  |   10    |  -5.69  |
|  negative  |   15    |  -9.1   |
|  negative  |   20    |  -10.8  |
|  negative  |   25    |  -7.0   |
|  positive  |   5     |   1.22  |
|  positive  |   10    |   3.9   |
|  positive  |   15    |   7.77  |
|  positive  |   20    |   11.5  |
|  positive  |   25    |   9.31  |
+--------------------------------+

This is the code I have so far:

$query= "select * from table";
$result=mysqli_query($connect, $query);

$graph=array();
while($row = mysqli_fetch_array($result)){
        $graph[]=$row;
}

    $graph=json_encode($graph);

    <html>
      <head>
        <!--Load the Ajax API-->
        <script type="text/javascript" src="https://www.google.com/jsapi"></script>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        <script type="text/javascript">

        // Load the Visualization API and the piechart package.
        google.load('visualization', '1', {'packages':['corechart']});

        // Set a callback to run when the Google Visualization API is loaded.
        google.setOnLoadCallback(drawChart);

        function drawChart(graph) {

          // Create our data table out of JSON data loaded from server.
          var data = new google.visualization.DataTable();
          data.addColumn('number','xaxis');
          data.addColumn('number','Positive');
          data.addColumn('number','Negative');

          var dataArray=[];
          $.each(graph, function(obj,obj){
             dataArray.push([parseFloat(obj.xaxis),parseFloat(obj.yaxis)]);
          }
          data.addRows(dataArray);

          var options = {
              width: 800,
              height: 600,
          curveType:'function'
            };
          // Instantiate and draw our chart, passing in some options.
          // Do not forget to check your div ID
          var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
          chart.draw(data, options);
        }
        </script>
      </head>

      <body>


    <div id="chart_div"></div>

      </body>
    </html>

This is the code to get the graph I need but the values are hard-coded

google.charts.load('current', {packages: ['corechart', 'line']});
google.charts.setOnLoadCallback(drawCurveTypes);

function drawCurveTypes() {
      var data = new google.visualization.DataTable();
      data.addColumn('number', 'X');
      data.addColumn('number', 'Positive');
      data.addColumn('number', 'Negative');

      data.addRows([
        [5, 1.22, -2.32],    
        [10, 3.9, -5.69],   
        [15, 7.77, -9.1],  
        [20, 11.5, -10.8],   
        [25, 9.31, -7.0]
      ]);

      var options = {
        hAxis: {
          title: 'Time'
        },
        vAxis: {
          title: 'Popularity'
        },
        series: {
          1: {curveType: 'function'}
        }
      };

      var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
      chart.draw(data, options);
    }

My question would be how to populate addRows so I can get the proper array format similar to the second code.

Any help would be appreciated. Please let me know if there's a better and easier way to code this.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
    • ¥20 关于URL获取的参数,无法执行二选一查询
    • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
    • ¥15 marlin编译错误,如何解决?
    • ¥15 有偿四位数,节约算法和扫描算法
    • ¥15 VUE项目怎么运行,系统打不开
    • ¥50 pointpillars等目标检测算法怎么融合注意力机制
    • ¥20 Vs code Mac系统 PHP Debug调试环境配置
    • ¥60 大一项目课,微信小程序
    • ¥15 求视频摘要youtube和ovp数据集