【问题标题】:Sqlite3 Db to Json, used for Highcharts?Sqlite3 Db 到 Json,用于 Highcharts?
【发布时间】:2018-06-02 00:54:13
【问题描述】:

我目前正在使用数据库,我想在网页上显示它的值,使用 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 的示例/模板?

非常感谢!

【问题讨论】:

  • 您想在一个图表中显示多个系列还是要显示多个图表?
  • 您好,一张图表中有多个系列。这是我想在 json.dumps 上获得的那种数据 -----> var x=[[epochtime, 1],[epochtime, 2]]; var y=[[epochtime, 1],[epochtime, 2]],etc...我知道这种解析是有效的,我不确定其他形式是否可以解决问题。但是我是一个初学者,我找不到正确的方法。
  • 这是我开始的 jsfiddle(不是我的)。 jsfiddle.net/mhardik/gpR9d/4

标签: python json highcharts sqlite


【解决方案1】:

我认为这应该可行:

在控制器中:
获取 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
            },   .......

【讨论】:

    猜你喜欢
    • 2013-10-29
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-06
    相关资源
    最近更新 更多