Sqlite3 Db到Json,用于Highcharts?

问题描述 投票:2回答:1

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

这是我用来获取Web应用程序中的数据:

@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的示例/模板?

非常感谢 !

python json highcharts sqlite
1个回答
0
投票

我认为这应该有效:

在控制器中: 获取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
            },   .......
© www.soinside.com 2019 - 2024. All rights reserved.