为什么我得到一个未捕获的ReferenceError:Plotly未定义

时间:2017-08-24 18:23:57

标签: javascript socket.io plotly

我的网页加载时出现vector错误。下面的代码位于我的div元素下面,其中绘制了绘图,但Uncaught ReferenceError: Plotly is not defined侦听器中的Plotly.newPlot('myDiv', dataPlot, layout);行是抛出此错误的位置。

socket.on

我已将var dataPlot = [{ x: ['a', 'b', 'c', 'd'], y: [0, 0, 0, 0], type: 'bar' }]; var layout = { autosize: false, width: 500, height: 500, margin: { l: 50, r: 50, b: 100, t: 100, pad: 4 } }; // Listener, whenever the server emits 'updatechat', this updates the chat body socket.on('updatechat', function (username, data) { $('#conversation').append('<b>'+username + ':</b> ' + data + '<br>'); if (data ==='a'){ dataPlot[0]['y'][0]++; } else if (data === 'b'){ dataPlot[0]['y'][1]++; } else if (data === 'c'){ dataPlot[0]['y'][2]++; } else if (data === 'd'){ dataPlot[0]['y'][3]++; } Plotly.newPlot('myDiv', dataPlot, layout); }); $("#clearplot").click(function() { var dataPlot = [{ x: ['a', 'b', 'c', 'd'], y: [0, 0, 0, 0], type: 'bar' }] Plotly.newPlot('myDiv', dataPlot, layout); }); 添加到HTML5文档的开头,而<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>函数中的Plotly.newPlot('myDiv', dataPlot, layout);工作正常。为什么仅在clearplot侦听器中出现错误?

修改 我想出了这个问题。问题是此代码嵌入在Angular应用程序中,因此我只需将updatechat包含在模板HTML文件的头部,而不是本地HTML文件中。

1 个答案:

答案 0 :(得分:0)

您似乎忘记在代码中包含 Plotly 脚本引用。
尝试添加:
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
在 HTML 页面的 <head> 部分。

相关问题