Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

javascript - 在js脚本中使用php“如果...循环”-Google图表(Use php “loop if” in js script - google chart)

I have a php website and I need add a google chart - so it is my first step with js.

(我有一个PHP网站,我需要添加一个Google图表-这是我使用JS的第一步。)

I have the below problem:

(我有以下问题:)

I need to change the original function drawVisualization() for php code.

(我需要为PHP代码更改原始函数drawVisualization() 。)

  1. I created $cosafter with the same input (php),

    (我用相同的输入(php)创建了$ cosafter ,)

  2. Made var coss with php echo (js)

    (用php echo(js)制作var coss)

  3. Put the new coss id into function (js)

    (将新的coss id放入函数(js))

Could you plese check and advise where I made a mistake?

(您能检查一下并告知我在哪里出错吗?)

Original code:

(原始代码:)

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

      function drawVisualization() {
        // Some raw data (not necessarily accurate)
        var data = google.visualization.arrayToDataTable([
          ['data', 'zysk/strata', 'Average'],
          ['2019-09-05',9.32,9.32],
          ['2019-09-06',10.88,10.1],
          ['2019-11-29',-7.86,4.1133333333333],
          ['2019-11-29',-43.61,-7.8175],
          ['2019-11-29',44,2.546],
          ['2019-11-29',4.71,2.9066666666667],
        ]);

        var options = {
          title : 'Trend ostatnich 10 transakcji',
          vAxis: {title: 'Zysk/Strata'},
          hAxis: {title: 'Data'},
          seriesType: 'bars',
          series: {1: {type: 'line', color: 'black'}}        };

        var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="chart_div" style="width: 900px; height: 500px;"></div>
  </body>
</html>

My code after changes:

(更改后的代码:)

    <?php
    require("zamkniete.php");

    $cos='';

    if ($lp22 >= 10){$ost10=$lp22-9;}else{$ost10=0;}   //show only last 10 prices

    for ($x=$ost10; $x <= $lp22; $x++){
    if($x==0){$to=$zyskpop[$x];}else{$to=($zyskpop[$x]/($x+1));}
    $cosbefore.= "['".$datatab[$x]."',".$zysktab[$x].",".$to."],</br>";
    };

    $cosafter = "[['data', 'zysk/strata', 'Average'],</br>".$cosbefore."]";

//echo $cosafter;
/*
    FYI how $cosafter looks like:
     [['data', 'zysk/strata', 'Average'],
     ['2019-09-05',9.32,9.32],
     ['2019-09-06',10.88,10.1],
     ['2019-11-29',-7.86,4.1133333333333],
     ['2019-11-29',-43.61,-7.8175],
     ['2019-11-29',44,2.546],
     ['2019-11-29',4.71,2.9066666666667],
     ]
*/


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

          var coss = "<?php echo $cosafter; ?>";

          function drawVisualization() {
            // Some raw data (not necessarily accurate)

            var data = google.visualization.arrayToDataTable(coss);

            var options = {
              title : 'Trend ostatnich 10 transakcji',
              vAxis: {title: 'Zysk/Strata'},
              hAxis: {title: 'Data'},
              seriesType: 'bars',
              series: {1: {type: 'line', color: 'black'}}        };

            var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
            chart.draw(data, options);
          }
        </script>
      </head>
      <body>
        <div id="chart_div" style="width: 900px; height: 500px;"></div>
      </body>
    </html>
  ask by Touchstone translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...