【发布时间】:2018-04-16 13:10:11
【问题描述】:
我有一个 influxdb 数据库,我想使用 highchart 将其数据显示到图表中。
为此,我从数据库中获取数据:
-
https://github.com/vicanso/influxdb-nodejs
var time; var valeur; const Influx = require('influxdb-nodejs'); const client = new Influx('http://127.0.0.1:8086/aTimeSeries'); //query last value client.queryRaw('select * from "valeurs" group by * order by desc limit 1') .then((data) => { time = Date.parse(data.results[0].series[0].values[0][0]); valeur = data.results[0].series[0].values[0][1]; console.info("[" + time + "," + valeur + "]"); }).catch(console.error); var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/json'}); res.write("[" + time + "," + valeur + "]"); res.end(); }).listen(8080);
然后为了显示它,我尝试使用 Highcharts 提供的示例:
-
https://www.highcharts.com/docs/working-with-data/live-data
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Highcharts Example</title> <!-- 1. Add these JavaScript inclusions in the head of your page --> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> <script src="https://code.highcharts.com/highcharts.js"></script> <script src="https://code.highcharts.com/modules/exporting.js"></script> <script src="https://code.highcharts.com/modules/export-data.js"></script> <!-- 2. Add the JavaScript to initialize the chart on document ready --> <script> var chart; // global /** * Request data from the server, add it to the graph and set a timeout to request again */ function requestData() { $.ajax({ url: 'http://localhost:8080/', success: function(point) { var series = chart.series[0], shift = series.data.length > 20; // shift if the series is longer than 20 // add the point chart.series[0].addPoint(eval(point), true, shift); // call it again after one second setTimeout(requestData, 1000); }, cache: false }); } $(document).ready(function() { chart = new Highcharts.Chart({ chart: { renderTo: 'container', defaultSeriesType: 'spline', events: { load: requestData } }, title: { text: 'Live random data' }, xAxis: { type: 'datetime', tickPixelInterval: 150, maxZoom: 20 * 1000 }, yAxis: { minPadding: 0.2, maxPadding: 0.2, title: { text: 'Value', margin: 80 } }, series: [{ name: 'Random data', data: [] }] }); }); </script> </head> <body> <!-- 3. Add the container --> <div id="container" style="width: 100%; height: 100%; margin: 0 auto"></div> </body>
但是由于它使用 PHP,我遇到了很多小问题,所以我尝试切换到 nodejs。
[1523957858507,40]
我的 nodejs 文件显示正确的数据,就像我的 PHP 文件一样。
我想要的是我的索引 html 从 localhost:8080 获取数据,我的 JS 发送一个 json 数组。
感谢您的帮助
【问题讨论】:
-
但是我们不理解你不理解的东西。
-
我想要的是我的索引 html 从localhost:8080 获取数据,我的 JS 发送一个 json 数组。
-
您能否添加一个示例来说明您的 ajax 调用中返回的内容?换句话说,你的
success函数上的point变量的值。
标签: javascript node.js highcharts influxdb