格式化数据以显示图表:)

 收藏

我试图得到一个浮点图来显示我的jsonencoded数组,如下所示:

[["aa",71],["ab",69],["ac",66],["ad",61],["ae",79],["af",78]]

在数组中,第一个是我在x轴上想要的,第二个是对应的值。

Right now my flot graph outputs an empty chart and I'm thinking that the array need to be all numbers. Previously I have predefined the xaxis names like this ticks:[[0,'Räckvidd'] But now I dont know what the expected name will be since the database is growing.

现在我真的不知道该如何进行,所以如果这里有人有浮点图的经验并且知道解决我的问题的方法,我将永远感激不已!

到目前为止,这是我所做的:

<div class="graph small tab-content" id="plotarea"></div>


<?PHP 
 $sql = mysql_query("SELECT Name,Value,Id FROM DB ORDER BY Id");
  while($result = mysql_fetch_array($sql))
  {
     $data[] = array($result['Name'],$result['Value']);
  }


  $TEST = json_encode(($data), JSON_NUMERIC_CHECK);
                                        ?>

和flot脚本:

  <script type="text/javascript">
    $(window).load(function () {
            var options = {
                    lines: { show: true },
                    points: { show: true },
                    xaxis: { min:0,max:5 },
                    yaxis: { min:1  ,max:60},
               };

     var TEST = <?php echo $TEST; ?>;

     $.plot($("#plotarea"),TEST,options)});

   </script> 

完了,走吧 亚当

回复
  • 薄荷凉 回复

    谢谢,很简单:)

    现在我只需要格式化刻度线即可显示正确的名称...

  • 淡夏之 回复

    我该如何改变

    <?PHP 
     $sql = mysql_query("SELECT Name,Value,Id FROM DB ORDER BY Id");
      while($result = mysql_fetch_array($sql))
      {
         $data[] = array($result['Name'],$result['Value']);
      }
      $TEST = json_encode(($data), JSON_NUMERIC_CHECK);
                                            ?>
    

    that outputs : [["aa",71],["ab",69],["ac",66],["ad",61],["ae",79],["af",78]]
    to instead get the output: [[0,71],[1,69],[2,66],[3,61],[4,79],[5,78]]

    I recon I would replace $result['Name'] with something like: $j = count($data[]);for($i = 0; $i < $j ; $i++)

    但是我不知道如何正确获得正确的输出。

    谢谢 亚当

  • o梦倾城 回复

    好的,我现在可以正常工作了:)

    不知道这是否是最有效的编码,但是它看起来像这样:

    <?PHP 
     $sql = mysql_query("SELECT Name,Value,Id FROM DB ORDER BY Id");
      while($result = mysql_fetch_array($sql))
      {
         $data[] = array(count($data),$result['Value']);
      }
      $TEST = json_encode(($data), JSON_NUMERIC_CHECK);
    ?>
    
    
         <?PHP 
         $sql2 = mysql_query("SELECT Name,Value,Id FROM DB ORDER BY Id");
          while($result2 = mysql_fetch_array($sql2))
          {
             $data2[] = array(count($data2), $result2['Value']);
          }
          $TEST2 = json_encode(($data2), JSON_NUMERIC_CHECK);
          ?>
    

    并将我的flot脚本更改为:

      <script type="text/javascript">
        $(window).load(function () {
                var data1 = [{
                        "label": "test",
                        "data": <?php echo $TEST; ?>,
                        bars: {
                            barWidth: 0.2, 
                            order: 1,
                            lineWidth : 2,
                        }
                    }];
    $.plot($('#plotarea'), data1, {
                        grid : {hoverable : true, tickColor: '#DDDDDD',labelMargin: 10, verticalLines: false, horizontalLines: false, outlineWidth: 0},
                        xaxis: { ticks: <?php echo $TEST1; ?>},
                        yaxis: {  position: "left"}, // or "right" 
                        colors: ["#00eb0b", "#eea0a0"],
                        series : { lines: { fill: false, fillColor: { colors: [ { opacity: 0.3 }, { opacity: 0.01 } ] } }}
                    });
                    });
       </script> 
    

    谢谢你的帮助!

  • fizzuas 回复

    $ data [] = array($ result ['Name'],$ result ['Value']);
    $data[] = array(count($data), $result['Value']);