doufei1893 2013-05-27 08:33
浏览 44
已采纳

使用amstockcharts的数据库数据

I am trying to use database data to draw charts with amstockcharts. However i keep failing at the moment. Here is my code:

<script type="text/javascript">
  <?php
     $conn = new PDO ("pgsql:host=localhost port=5432 dbname=dbase", "id", "pass");

     $sql = "SELECT id, time, speed, fuel_level FROM data WHERE id = 294 AND
     time BETWEEN '2013-05-11' AND '2013-05-12' LIMIT 100;"; 
     $date = array();
     $fuel = array();
     $speed = array();
     $counter = 0;
     $query = $conn->query($sql);
     while($result = $query->fetch(PDO::FETCH_ASSOC))
     {
       array_push($date, $result['time']);
       array_push($fuel, $result['fuel']);
       array_push($speed, $result['speed']);
     }
     $jsDate = json_encode($date);
     $jsFuel = json_encode($fuel);
     $jsSpeed = json_encode($speed);
     $conn = null;
     echo "var date = ".$jsDate.";
";
     echo "var fuel = ".$jsFuel.";
";
     echo "var speed = ".$jsSpeed.";
";
  ?>
  var dataObject = [];
  var object;
  for(var i=0; i<100; i++)
  {
     object = { date:date[i], fuel:fuel[i], speed:speed[i] };
     dataObject.push(object);
  }
  AmCharts.ready(function()
  {
     var chart = new AmCharts.AmStockChart();
     chart.pathToImages = "amcharts/images/";

     var dataSet = new AmCharts.DataSet();
     dataSet.dataProvider = dataObject;
     dataSet.fieldMappings = [{fromField:"speed", toField:"speed"}];   
     dataSet.categoryField = "date";
     dataSet.color = "#7f8da9";
     dataSet.title = "Test";
     chart.dataSets = [dataSet];
     chart.mainDataSet = dataSet;

     var stockPanel = new AmCharts.StockPanel();
     stockPanel.title = "Chart";
     chart.panels = [stockPanel];

     var legend = new AmCharts.StockLegend();
     stockPanel.stockLegend = legend;   

     var panelsSettings = new AmCharts.PanelsSettings();
     panelsSettings.startDuration = 1;
     chart.panelsSettings = panelsSettings;   

     var graph = new AmCharts.StockGraph();
     graph.valueField = "speed";
     graph.type = "column";
     graph.title = "speed";
     graph.fillAlphas = 1;
     stockPanel.addStockGraph(graph);

     var categoryAxesSettings = new AmCharts.CategoryAxesSettings();
     categoryAxesSettings.dashLength = 5;
     chart.categoryAxesSettings = categoryAxesSettings;

     var valueAxesSettings = new AmCharts.ValueAxesSettings();
     valueAxesSettings .dashLength = 5;
     chart.valueAxesSettings  = valueAxesSettings;

     var chartScrollbarSettings = new AmCharts.ChartScrollbarSettings();
     chartScrollbarSettings.graph = graph;
     chartScrollbarSettings.graphType = "line";
     chart.chartScrollbarSettings = chartScrollbarSettings;

     var chartCursorSettings = new AmCharts.ChartCursorSettings();
     chartCursorSettings.valueBalloonsEnabled = true;
     chart.chartCursorSettings = chartCursorSettings;
     chart.write('chartDiv');
   });
 </script>

Everything seems fine until i push data to dataObject variable, because the chart displays, but the graph is not drawn nor the axis are displayed.

  • 写回答

1条回答 默认 最新

  • dongtang4019 2013-10-11 18:55
    关注

    Replace:

    echo "var date = ".$jsDate.";
    ";
    

    with:

    echo “date: (new Date('" .str_replace('-','/',$jsDate). "')),
    ";
    

    It should help you.

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

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥15 this signal is connected to multiple drivers怎么解决
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥60 关机时蓝屏并显示KMODE_EXCEPTION_NOT_HANDLED,怎么修?
  • ¥66 如何制作支付宝扫码跳转到发红包界面