Charts.js-来自JSON的onComplete JavaScript函数

时间:2019-03-01 08:30:32

标签: json chart.js

我正在使用Charts.js,数据和选项来自json中的AJAX请求。 当我需要作为选项添加onComplete事件上的回调时会出现问题,因为JSON应该看起来像

"animation" : {
      "onComplete" : function(animation){alert('ok')}
    },

这是无效的JSON。

我试图以简单文本(datatype: 'text')的形式检索数据,但是当同样传递Charts.js时,我遇到了一个错误:

Uncaught TypeError: Cannot create property 'data' on string

这可能是我正在检索的json

 {
  "type" : "line",
  "data" : {
    "datasets" : [ {
      "data" : [ {
        "t" : 1551096300000,
        "y" : 22.8125
      }, {
        "t" : 1551096600000,
        "y" : 22.8125
      }, {
        "t" : 1551096900000,
        "y" : 22.8125
      }, {
        "t" : 1551097200000,
        "y" : 22.8125
      }, {
        "t" : 1551097500000,
        "y" : 22.8125
      }, {
        "t" : 1551097800000,
        "y" : 19.3125
      }],
      "label" : "Sample data",
      "fill" : false,
      "backgroundColor" : "rgba(0,128,0,1.000)",
      "borderWidth" : 2,
      "borderColor" : "rgba(0,128,0,1.000)"
    },
  "options" : {
    "responsive" : true,
    "maintainAspectRatio" : true,
    "title" : {
      "display" : true,
      "position" : "top",
      "text" : "Temperature (°C)"
    },
    "legend" : {
      "position" : "bottom"
    },
    "hover" : {
      "mode" : "dataset"
    },
    "animation" : {
      "onComplete" : function(animation){alert('ok')}
    },
    "scales" : {
      "xAxes" : [ {
        "type" : "time",
        "time" : {
          "displayFormats" : {
            "millisecond" : null,
            "second" : null,
            "minute" : "HH:mm",
            "hour" : "DD/MM HH:mm",
            "day" : "DD/MM HH:mm",
            "week" : null,
            "month" : "DD/MM HH",
            "quarter" : null,
            "year" : null
          },
          "tooltipFormat" : "DD/MM/YY HH:mm"
        }
      } ]
    },
    "elements" : {
      "point" : {
        "radius" : 1,
        "hitRadius" : 2,
        "hoverRadius" : 2
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

首先,您忘记关闭第29行的数据集数组。

第二次在JSON结果中定义函数无效(第45行)。 检查Is it valid to define functions in JSON results?

再一次,您忘了在结尾处关闭花括号。

{
    "type": "line",
    "data": {
        "datasets": [{
                "data": [{
                    "t": 1551096300000,
                    "y": 22.8125
                }, {
                    "t": 1551096600000,
                    "y": 22.8125
                }, {
                    "t": 1551096900000,
                    "y": 22.8125
                }, {
                    "t": 1551097200000,
                    "y": 22.8125
                }, {
                    "t": 1551097500000,
                    "y": 22.8125
                }, {
                    "t": 1551097800000,
                    "y": 19.3125
                }],
                "label": "Sample data",
                "fill": false,
                "backgroundColor": "rgba(0,128,0,1.000)",
                "borderWidth": 2,
                "borderColor": "rgba(0,128,0,1.000)"
            }],
            "options": {
                "responsive": true,
                "maintainAspectRatio": true,
                "title": {
                    "display": true,
                    "position": "top",
                    "text": "Temperature (°C)"
                },
                "legend": {
                    "position": "bottom"
                },
                "hover": {
                    "mode": "dataset"
                },
                "animation": {
                    "onComplete": "" 
                },
                "scales": {
                    "xAxes": [{
                        "type": "time",
                        "time": {
                            "displayFormats": {
                                "millisecond": null,
                                "second": null,
                                "minute": "HH:mm",
                                "hour": "DD/MM HH:mm",
                                "day": "DD/MM HH:mm",
                                "week": null,
                                "month": "DD/MM HH",
                                "quarter": null,
                                "year": null
                            },
                            "tooltipFormat": "DD/MM/YY HH:mm"
                        }
                    }]
                },
                "elements": {
                    "point": {
                        "radius": 1,
                        "hitRadius": 2,
                        "hoverRadius": 2
                    }
                }
            }
        }
}