在PHP数据上创建flot [英] Create flot on PHP data

查看:96
本文介绍了在PHP数据上创建flot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于php输出创建一个浮动条形图.我设法从php输出数据,但我也想使用标签并在xaxis上显示它们.由于某种原因,以下代码的输出无效.标签会显示,但条形图和xaxis标签不会显示.

I would like to create a flot bar graph based on a php output. I managed to output data from php, but I would also like to use labels and display them on the xaxis. For some reason the output of the code below is invalid. The labels show up, but the bars and xaxis labels do not.

PHP:

function getOverview() {

    $arr[] = array(
        'label' => "Label 1", 
        'data' => array(0, 1)
    );
    $arr[] = array(
        'label' => "Label 2", 
        'data' => array(1, 2)
    );


    echo json_encode($arr);
}

输出: [{"label":"Label 1","data":[0,1]},{"label":"Label 2","data":[1,2]}]

Output: [{"label":"Label 1","data":[0,1]},{"label":"Label 2","data":[1,2]}]

jQuery:

$(document).ready(function(){

$.ajax({
    url: 'http://localhost/getOverview.php',
    method: 'GET',
    dataType:"json",
    success: onOutboundReveived
});

function onOutboundReveived(series)
{

    var options = {
        series: {
            bars: {
                show: true,
                barWidth: .1,
                align: 'center'
            }
        },
        xaxis: {
            tickSize: 1 
        }

    };

    $.plot("#chart_filled_blue", series, options);
}
});

有人可以帮助我吗?

推荐答案

您遇到了一些问题:

1.)系列数据必须是一个数组数组.不只是一个数组:

1.) Series data needs to be an array of arrays. Not just a single array:

'data' => array(array(1, 2))

这当然是一个系列,所以它可能会超过一个点(即使您只有一个点).

This is, of course, so a series could have more than one point (even though your's has a single point).

2.)要获得xaxis标签,您有两个选择.一种,使用类别插件.第二,手动提供刻度标签:

2.) To get xaxis labels, you have two options. One, use the categories plugin. Two, manually provide the tick labels:

ticks: [[0, "zero"], [1.2, "one mark"], [2.4, "two marks"]]

在您的情况下,我只使用类别插件.您需要将最终数据修改为:

In your situation I'd just use the category plugin. You'll need to modify the final data to:

{"label":"Label 1","data":[["Label 1",1]]}

或在PHP中:

$arr[] = array(
    'label' => "Label 1", 
    'data' => array(array("Label 1", 1))
);

这是小提琴.

这篇关于在PHP数据上创建flot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆