【问题标题】:CSV data(Not file, directly putting the data in code ) placement in Javascript codeCSV 数据(不是文件,直接将数据放入代码中)放置在 Javascript 代码中
【发布时间】:2018-04-17 11:19:40
【问题描述】:

您好,我想知道存储此 CSV 数据的正确方法,我已将其存储在 var 数据中,但它会引发错误。

var data = Events,Apps,Status
    Business Requirement Description,App1,60
    Communication to Stake Holders,App1,50
    Development Started,App1,30
    Reviewed ,App1,10
    Ready for E2E Testing,App1,5
    Business Requirement Description,App2,80
    Communication to Stake Holders,App2,40
    Development Started,App2,20
    Reviewed ,App2,10
    Ready for E2E Testing,App2,0
    Business Requirement Description,App3,100
    Communication to Stake Holders,App3,60
    Development Started,App3,0
    Reviewed ,App3,30
    Ready for E2E Testing,App3,0

我在这里使用 Highcharts 功能解析数据以创建热图:

function(data){
    var lines = data.split('\n');
    $.each(lines, function(lineNo, line) {
        var items = line.split(','); 
    });

    // Defining X-Axis: 
    $.each(items, function(itemNo, item) {
        if(itemNo == 0){
            $.each(lines, function(lineNo, line) {
                if (lineNo > 0)
                    options.xAxis.categories.push(item)
            });
        }
    }
}

【问题讨论】:

    标签: javascript csv highcharts heatmap


    【解决方案1】:

    您需要将其存储为字符串。

    var data = `
        Events,Apps,Status
        Business Requirement Description,App1,60
        Communication to Stake Holders,App1,50
        Development Started,App1,30
        Reviewed ,App1,10
        Ready for E2E Testing,App1,5
        Business Requirement Description,App2,80
        Communication to Stake Holders,App2,40
        Development Started,App2,20
        Reviewed ,App2,10
        Ready for E2E Testing,App2,0
        Business Requirement Description,App3,100
        Communication to Stake Holders,App3,60
        Development Started,App3,0
        Reviewed ,App3,30
        Ready for E2E Testing,App3,0`
    

    如果您的环境不支持反引号(某些较旧的浏览器不支持反引号):

     var data = "Events,Apps,Status\nBusiness Requirement Description,App1,60\nCommunication to Stake Holders,App1,50\nDevelopment Started,App1,30\nReviewed ,App1,10\nReady for E2E Testing,App1,5\nBusiness Requirement Description,App2,80\nCommunication to Stake Holders,App2,40\nDevelopment Started,App2,20\nReviewed ,App2,10\nReady for E2E Testing,App2,0\nBusiness Requirement Description,App3,100\nCommunication to Stake Holders,App3,60\nDevelopment Started,App3,0\nReviewed ,App3,30\nReady for E2E Testing,App3,0"
    

    注意事项:

    如果您将此数据硬编码到文件中,您也可以使用在线CSV to JSON converter。可以直接将 JSON 对象作为 JS 对象使用。

    【讨论】:

      【解决方案2】:

      您需要将 CSV 字符串括在引号中:

      var data = "Events,Apps,Status
      Business Requirement Description,App1,60
      Communication to Stake Holders,App1,50
      Development Started,App1,30
      Reviewed ,App1,10
      Ready for E2E Testing,App1,5
      Business Requirement Description,App2,80
      Communication to Stake Holders,App2,40
      Development Started,App2,20
      Reviewed ,App2,10
      Ready for E2E Testing,App2,0
      Business Requirement Description,App3,100
      Communication to Stake Holders,App3,60
      Development Started,App3,0
      Reviewed ,App3,30
      Ready for E2E Testing,App3,0"
      

      【讨论】:

        猜你喜欢
        • 2013-07-03
        • 1970-01-01
        • 1970-01-01
        • 2018-08-07
        • 1970-01-01
        • 1970-01-01
        • 2019-11-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多