c3.js的格式

时间:2017-10-26 22:46:02

标签: python c3.js

我有一个数据框,可以从python输出我想要的信息,pandas会将它转换为json,如下所示,c3.js读取它的最佳格式是什么?

'{"schema": {"fields":[{"name":"Employee Department","type":"string"},
{"name":"Total_Employees","type":"integer"},
{"name":"Completed","type":"integer"},
{"name":"Remaining","type":"integer"}],"primaryKey":["Employee Department"],"pandas_version":"0.20.0"}, 
"data": 
    [
    {"Employee Department":"AQPSD","Total_Employees":163,"Completed":70,"Remaining":93},
    {"Employee Department":"ASD","Total_Employees":149,"Completed":77,"Remaining":72},
    {"Employee Department":"CO","Total_Employees":37,"Completed":8,"Remaining":29},
    {"Employee Department":"ECARS","Total_Employees":243,"Completed":158,"Remaining":85},
    {"Employee Department":"ED","Total_Employees":172,"Completed":70,"Remaining":102},
    {"Employee Department":"EO","Total_Employees":70,"Completed":24,"Remaining":46},
    {"Employee Department":"ISD","Total_Employees":138,"Completed":54,"Remaining":84},
    {"Employee Department":"MLD","Total_Employees":180,"Completed":101,"Remaining":79},
    {"Employee Department":"MSCD","Total_Employees":161,"Completed":79,"Remaining":82},
    {"Employee Department":"OIS","Total_Employees":93,"Completed":41,"Remaining":52},
    {"Employee Department":"RD","Total_Employees":79,"Completed":38,"Remaining":41},
    {"Employee Department":"TTD","Total_Employees":102,"Completed":45,"Remaining":57}]
}'

1 个答案:

答案 0 :(得分:1)

如果您使用df.to_json(orient='records'),其中df是您的数据框,则数据将以c3.js期望的JSON格式序列化。

假设您使用的是Flask之类的东西,您的控制器可以返回该json,并且您的前端可以调用该路径(例如,使用$.ajax())。然后,当您收到有效负载时,可以使用JSON.parse()将其直接传递给c3.generate函数,如下所示:

        c3.generate({
                data: {
                    json: // here is where your data goes
                }
        });