python - Sqlite3 Db 转 Json,用于 Highcharts?

标签 python json highcharts sqlite

我目前正在使用数据库,我想使用 highcharts 在网页上显示其值。

这是我用来在网络应用程序中获取数据的方法:

@app.route("/data.json")
def data():
   connection = sqlite3.connect("/home/pi/database/Main_Database.db")
   cursor = connection.cursor()
   cursor.execute("SELECT epochTime, data_x from table")
   results = cursor.fetchall()
   return json.dumps(results)

然后我目前通过在我的 html 中执行此操作来获取此值:

$.getJSON('http://192.168.1.xx/data.json', function (data) {
    // Create the chart
    $('#container').highcharts('StockChart', {
        rangeSelector : {
            selected : 1
        },
        title : {
            text : 'title'
        },
        series : [{
            name : 'Value',
            data : data,
            tooltip: {
                valueDecimals: 2
            },   .......

如果我只想显示一个数据数组,则此方法有效。 如果我想显示多个数组,那么看起来每个数组前面都必须有其名称,尊重某种解析(我检查了 highcharts 使用的数据样本)。

示例:

data1:[(epochTime, 200),(epochTime,400)];data2:[(epochTime, 2),(epochTime,4)]

例如,我在 json.dumps 来自两个不同表的两个数组时遇到一些麻烦。我尝试使用以下命令:json.dumps({data1:results})。 但结果仍然不可读。

你有什么建议吗?或者使用 sqlite 的 webapp/highcharts 的示例/模板?

非常感谢!

最佳答案

我认为这应该有效:

在 Controller 中:
获取 2 个结果并将它们放入字典中。

@app.route("/data.json")
def data():
   connection = sqlite3.connect("/home/pi/database/Main_Database.db")
   cursor = connection.cursor()
   cursor.execute("SELECT epochTime, data_x from table")
   results1 = cursor.fetchall()
   cursor.execute("SELECT epochTime, data_x from table2")
   results2 = cursor.fetchall()
   return json.dumps({'result1': results1,
                      'result2': results2})

在页面上:

$.getJSON('http://192.168.1.xx/data.json', function (data) {
    // Create the chart
    $('#container').highcharts('StockChart', {
        rangeSelector : {
            selected : 1
        },
        title : {
            text : 'title'
        },
        series : [{
            name : 'Value1',
            data : data.result1,//read result1
            tooltip: {
                valueDecimals: 2
            },
            {
            name : 'Value2',
            data : data.result2,//read result2
            tooltip: {
                valueDecimals: 2
            },   .......

关于python - Sqlite3 Db 转 Json,用于 Highcharts?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47889499/

相关文章:

javascript - javascript 文件中的 Angular 分割 json

Python/Numpy - 计算相等数组元素的总和

python - 使用 pandas groupby 或其他函数对多个数据帧进行子集化的简单方法?

python - Opencv:BoW计算SURF描述符

sql - 在 Postgres 上的关系表上聚合 Json 对象

java - 使用 foreach 循环创建 JSON 文档

javascript - 如何隐藏底部垂直线 highcharts.js

mysql - Highcharts - 来自 MySQL 的预期格式的系列数据

python - 如何防止 suds 通过网络获取 xml.xsd?

javascript - 编写 JSON 解析器以格式化饼图 (HighCharts) 的数据