节点红色中的情节

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

我正在尝试使用模板节点从节点红色生成一个简单的图表。如果x和y是静态值,则正确地绘制图表,但是由于使用有效负载将数据获取到绘制变量中,因此无法打印。我已检查并正确传输了数据。请查看代码。 \

Mustache节点中的代码

下面的代码可以正常工作并正确创建图表。

<head>
  <!-- Plotly.js -->
  <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>

<body>

  <div id="myDiv"><!-- Plotly chart will be drawn inside this DIV --></div>
  <script>

var trace1 = {
  x:[1,2,3],//[{{payload.Temperature}}],
  y:[1,2,3],//[{{payload.Temperature}}],
  //mode: 'lines',
  connectgaps: true
};

var data = [trace1];

var layout = {
  title: 'Connect the Gaps Between Data',
  showlegend: true
};

Plotly.newPlot('myDiv', data, layout, {showSendToCloud: true});

</script>
</body>

下面的代码不起作用,而是只显示坐标。

<head>
  <!-- Plotly.js -->
  <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>

<body>

  <div id="myDiv"><!-- Plotly chart will be drawn inside this DIV --></div>
  <script>

var trace1 = {
  x:[{{payload.time}}],
  y:[{{payload.Temperature}}],
  mode: 'lines',
  connectgaps: true
};

var data = [trace1];

var layout = {
  title: 'Connect the Gaps Between Data',
  showlegend: true
};

Plotly.newPlot('myDiv', data, layout, {showSendToCloud: true});

</script>
</body>

注意:在调试窗口中查看的数据为ans后继]

`

x:[Tue Oct 08 2019 12:47:58 GMT-0400 (Eastern Daylight Time),Tue Oct 08 2019 12:47:28 GMT-0400 (Eastern Daylight Time),Tue Oct 08 2019 12:47:21 GMT-0400 (Eastern Daylight Time),Tue Oct 08 2019 12:46:30 GMT-0400 (Eastern Daylight Time),Tue Oct 08 2019 12:42:58 GMT-0400 (Eastern Daylight Time),Tue Oct 08 2019 12:41:17 GMT-0400 (Eastern Daylight Time),Tue Oct 08 2019 12:37:58 GMT-0400 (Eastern Daylight Time),Tue Oct 08 2019 12:37:28 GMT-0400 (Eastern Daylight Time),Tue Oct 08 2019 12:37:23 GMT-0400 (Eastern Daylight Time),Tue Oct 08 2019 12:36:17 GMT-0400 (Eastern Daylight Time)],
  y:[26.76,27.76,27.51,27.11,26.76,27.15,26.75,27.9,27.77,27.23],

`

我做错了,谢谢。

plotly mustache node-red
1个回答
1
投票

[您需要查看日期如何转换为语言环境字符串...仅将日期嵌入到javascript呈现的代码中并不会生成有效的Javascript代码。将此代码段放入注释中:

var trace1 = {
    x: [Tue Oct 08 2019 12:47:58 GMT-0400 (Eastern Daylight Time),Tue Oct 08 2019 12:47:28 GMT-0400 (Eastern Daylight Time),Tue Oct 08 2019 12:47:21 GMT-0400 (Eastern Daylight Time),Tue Oct 08 2019 12:46:30 GMT-0400 (Eastern Daylight Time),Tue Oct 08 2019 12:42:58 GMT-0400 (Eastern Daylight Time),Tue Oct 08 2019 12:41:17 GMT-0400 (Eastern Daylight Time),Tue Oct 08 2019 12:37:58 GMT-0400 (Eastern Daylight Time),Tue Oct 08 2019 12:37:28 GMT-0400 (Eastern Daylight Time),Tue Oct 08 2019 12:37:23 GMT-0400 (Eastern Daylight Time),Tue Oct 08 2019 12:36:17 GMT-0400 (Eastern Daylight Time)],
    y: [26.76,27.76,27.51,27.11,26.76,27.15,26.75,27.9,27.77,27.23],
    mode: 'lines',
    connectgaps: true
}; 

该属性x应该是任一日期的数组字符串 ...

    x: ["Tue Oct 08 2019 12:47:58 GMT-0400 (Eastern Daylight Time)", "Tue Oct 08 2019 12:47:28 GMT-0400 (Eastern Daylight Time)", ...etc... ],

...或numbers的数组,表示epoch millis ...

    x: [1570553278000, 1570553248000, ...etc... ],

您是否使用数据库查询来获取这x个值?如果是这样,我将在查询中使用一些sql函数来输出内部unix时间(秒)* 1000以获取毫秒。否则,您将必须在JavaScript代码之前 template节点中进行转换,对数组中的每个元素使用Date.parse("Tue Oct 08 2019 12:47:58 GMT-0400 (Eastern Daylight Time)") ...效率很低。

© www.soinside.com 2019 - 2024. All rights reserved.