javascript - AnyChart Firebase
问题描述
我是 AnyChart 图表库的新手。我想使用 AnyChart 绘制从 Firebase 提取的数据。我现在面临在 AnyChart 上绘制数组数据的问题。我现在面临使用以下代码从 Firebase 中提取数组数据集的问题:
dbRef2.limitToLast(20).on('child_added',function(snap) {
var Time2 = snap.val().timestamp;
var Humidex2 = snap.val().Humidex;
dataSetFirebaseHumidex2.push({x: Time2 ,y: Humidex2});
console.log(dataSetFirebaseHumidex2);
});
anychart.onDocumentReady(function () {
// create a data set
var data = anychart.data.set();
data.data([dataSetFirebaseHumidex2]);
// map the data
var seriesData_1 = data.mapAs({x: 0, y: 1});
// create a chart
var chart = anychart.area();
// create the first series, set the data and name
var series1 = chart.spline(data);
series1.name("S1");
// configure the visual settings of the first series
series1.normal().stroke("#00cc99", 1, "10 5", "round");
series1.hovered().stroke("#00cc99", 2, "10 5", "round");
series1.selected().stroke("#00cc99", 4, "10 5", "round");
// set the chart title
chart.title("Humidex Index");
// set the titles of the axes
chart.xAxis().title("Time");
chart.yAxis().title("Humidex");
// set the container id
chart.container("container");
// initiate drawing the chart
chart.draw();
});
有人可以建议吗?非常感激你的帮助。
解决方案
这段代码应该这样修改:
anychart.onDocumentReady(function () {
// create a data set
var data = anychart.data.set(dataSetFirebaseHumidex2);
// map the data
var seriesData_1 = data.mapAs({x: 'x', value: 'y'});
// create a chart
var chart = anychart.area();
// create the first series, set the data and name
//var series1 = chart.splineArea(data);
var series1 = chart.spline(data);
series1.name("Meter 1");
// configure the visual settings of the first series
series1.normal().stroke("#00cc99", 1, "10 5", "round");
series1.hovered().stroke("#00cc99", 2, "10 5", "round");
series1.selected().stroke("#00cc99", 4, "10 5", "round");
// set the chart title
chart.title("Total Active Power Consumption (ShellKK)");
// set the titles of the axes
chart.xAxis().title("Time");
chart.yAxis().title("Total Active Power, kWh");
//enable dateTime xScale
chart.xScale(anychart.scales.dateTime());
// set the container id
chart.container("container").draw();
});
但是,请确保在执行此代码之前dataSetFirebaseHumidex2变量已经包含以下格式的数据(对象数组):
[
{x: 1526446204973, y: 12},
{x: 1526456204973, y: 14},
....
......
{x: 1526466204973, y: 15}
]
推荐阅读
- vue.js - 如何在我的数据过滤组件中包含 .filter 功能
- flutter - Flutter 等待小部件加载或等待设置
- ios - 通过 Xcode 使用 xcframework 时 iOS 模拟器崩溃
- google-api - 无法按 ID 获取特定的 YouTube 订阅
- ruby-on-rails - ActiveAdmin 降价编辑器
- centos7 - 努力在 CentOS7 上用 yum 安装 Puppetserver
- python - 使用 flask_socketio 将视频流式传输到 Flask 应用程序
- oracle - Shell 脚本中的 Oracle 匿名块调用
- android - 从服务器接收到一个极长的令牌,如何使它多行,这样我就不必拖动滚动条来复制它?
- android - 在辅助功能服务中的另一个应用程序上显示 Toast