从PHP生成的CSV生成Highcharts

时间:2015-05-18 18:42:46

标签: javascript php jquery csv highcharts

我对csv数据中的Highcharts感到困惑。

从此示例中:http://jsfiddle.net/gh/get/jquery/1.9.1/highslide-software/highcharts.com/tree/master/samples/highcharts/demo/line-ajax/

$.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=analytics.csv&callback=?', function (csv) { })

它可以读取csv文件,但是当我像这样设置网址时,它无法正常工作

$.getJSON(baseurl+'webtools/analytics/submissions_data', function (csv) {
    console.log(csv);
    });

我的服务器端代码:

function submissions_data() {
    header('Content-Type: text/csv; charset=utf-8');
    $qry = "SELECT DATE(created_date) dateonly, COUNT(id) AS total FROM mf_story GROUP BY dateonly ORDER BY dateonly ASC;";
    $rows = $this->db->query($qry)->result_array();

    $output = fopen('php://output', 'w');

    $column = array("Day","Submissions");
    fputcsv($output, $column);

    foreach($rows as $data){
        $date = DateTime::createFromFormat('Y-m-d', $data['dateonly']);
        $data['dateonly'] = $date->format('n/d/Y');
        fputcsv($output, $data);
    }
}

我尝试了$ .ajax

$.ajax({
    url:baseurl+'webtools/analytics/submissions_data',
            type: 'get',
            dataType: 'jsonp',
            success: function(data) {
                console.log("data read");
            },
            error: function(jqXHR, textStatus, errorThrow){
                console.log(jqXHR['responseText']); //the csv printed here
            }

令人惊讶的是,我的csv打印出错误功能。有什么想法吗?我应该怎么做才能解决这个问题?把我的highcharts代码放在错误函数中真是太丑了。

1 个答案:

答案 0 :(得分:0)

找到修复程序,我将服务器端代码更改为text / json并将csv放入字符串

function submissions_data() {
    header('Content-Type: text/json');
    $qry = "SELECT DATE(created_date) dateonly, COUNT(id) AS total FROM mf_story GROUP BY dateonly ORDER BY dateonly ASC;";
    $rows = $this->db->query($qry)->result_array();

    $out = "Day,Submissions\n";

    foreach($rows as $data){
       $date = DateTime::createFromFormat('Y-m-d', $data['dateonly']);
       $data['dateonly'] = $date->format('n/d/Y');
       $out.=$data['dateonly'].",".$data['total']."\n";
    }

    echo json_encode($out);
}

我的json可以阅读它,谢谢:D