分析迷你图中的动态数据


Parsing Dynamic Data in Sparklines

我想解析Sparkline中的动态数据
我的代码:

$(".daily-visitors").sparkline([1,5,5.5,5.4,5.8,6,8,9,13,12,10,11.5,9,8,5,8,9], {
  type: 'line',
  width: '100%',
  height: '55',
  lineColor: '#ff4e50',
  fillColor: '#ffd2d3',
  lineWidth: 2,
  spotColor: '#a9282a',
  minSpotColor: '#a9282a',
  maxSpotColor: '#a9282a',
  highlightSpotColor: '#a9282a',
  highlightLineColor: '#f4c3c4',
  spotRadius: 2,
  drawNormalOnTop: true
 });

我想要使用PHP从MYSQL中获得动态数据。

我建议使用一个新的php文件,从中使用POST请求获取JSON数据。示例查询:

$query = mysql_query("SELECT daily_visitors FROM stats ORDER BY when DESC LIMIT 30");

然后你准备好让它展示给闪闪发光的人。获取数组后,您可以执行类似操作,并以JSON格式返回数据:

echo json_encode($data);

这相当于这样的东西:

[1,5,5.5,5.4,5.8,6,8,9,13,12,10,11.5,9,8,5,8,9] 

然后,您可以从您的网站POST请求这些数据:

$.post("url to your php file", { examplePostArgument:"test" }, function(data){
$(".daily-visitors").sparkline(data, {
    type: 'line',
    width: '100%',
    height: '55',
    lineColor: '#ff4e50',
    fillColor: '#ffd2d3',
    lineWidth: 2,
    spotColor: '#a9282a',
    minSpotColor: '#a9282a',
    maxSpotColor: '#a9282a',
    highlightSpotColor: '#a9282a',
    highlightLineColor: '#f4c3c4',
    spotRadius: 2,
    drawNormalOnTop: true
 });
});

这个示例PostArgument将使用这个超全局数组在您的PHP文件中访问:

$_POST["examplePostArgument"];