【问题标题】:How to make a bubble chart from CSV file?如何从 CSV 文件制作气泡图?
【发布时间】:2015-08-22 04:56:13
【问题描述】:

所以我有以下文件,data.csv。看起来像这样:

TestID,Cronbach,Percent Correct,Population
12416,0.866,0.17,26
12421,0.945,0.21,8
12385,0.777,0.40,258
12412,0.85,0.44,34
12407,0.831,0.45,48

我希望它看起来像这样:

[
["Test ID", "Cronbach", "Percent Correct", "Population"],
["12416", 0.866, 0.17, 26],
["12421", 0.945, 0.21, 8],
["12385", 0.777, 0.40, 258],
["12412", 0.85, 0.44, 34],
["12407", 0.831, 0.45, 48]
]

有没有办法可以在 php 中制作转换代码来将我的 CSV 文件转换为上述格式。我需要这个,因为我想将代码放入Google Bubble Chart

问:我应该如何制作可以将其转换为可接受格式以适应 Google 气泡图的代码?

【问题讨论】:

    标签: javascript php csv google-visualization bubble-chart


    【解决方案1】:

    以下示例演示如何解析 csv 文件内容:

    function prepareChartData(data) {
        var items = [];
        var lines = data.split(/\r\n|\n/);
    
        lines.forEach(function(line,i){
             if(line.length > 0){
                 var item =  line.split(','); 
                 if(i > 0){
                     item[1] = parseFloat(item[1]);
                     item[2] = parseFloat(item[2]);
                     item[3] = parseInt(item[3]);   
                 }
                 items.push(item);
             }
        });
        return items;
    }
    

    结果

    [
      [
        "TestID",
        "Cronbach",
        "Percent Correct",
        "Population"
      ],
      [
        "12416",
        0.866,
        0.17,
        26
      ],
      [
        "12421",
        0.945,
        0.21,
        8
      ],
      [
        "12385",
        0.777,
        0.4,
        258
      ],
      [
        "12412",
        0.85,
        0.44,
        34
      ],
      [
        "12407",
        0.831,
        0.45,
        48
      ]
    ]
    

    完整示例

    google.load("visualization", "1", { packages: ["corechart"] });
    google.setOnLoadCallback(function(){
        readChartData()
        .then(prepareChartData)
        .done(drawChart);
    });
    
    function drawChart(items) {
    
        var data = google.visualization.arrayToDataTable(items);
        var options = {
            title: 'Chart'
        };
        var chart = new google.visualization.BubbleChart(document.getElementById('chart_div'));
        chart.draw(data, options);
    }
    
    
    function readChartData(complete)
    {
        return $.ajax({
            type: "GET",
            url: "data.csv",
            dataType: "text"
        });  
    }
    
    
    function prepareChartData(data) {
        var items = [];
        var lines = data.split(/\r\n|\n/);
    
        lines.forEach(function(line,i){
             if(line.length > 0){
                 var item =  line.split(','); 
                 if(i > 0){
                     item[1] = parseFloat(item[1]);
                     item[2] = parseFloat(item[2]);
                     item[3] = parseInt(item[3]);   
                 }
                 items.push(item);
             }
        });
        return items;
    }
    

    【讨论】:

    猜你喜欢
    • 2021-04-28
    • 2015-05-06
    • 2016-10-17
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2016-03-01
    • 1970-01-01
    相关资源
    最近更新 更多