0

我想在颤振中建立一个同步融合图表。我从 firebase 实时数据库中检索数据。数据将显示在图表中。我在这里遇到了问题。他们说'毫秒自纪元'。但我的代码中没有它。但是有一个问题。这是我的代码:

import 'dart:async';

import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_charts/charts.dart';

class Chart extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: HolyChart(),
      ),
    );
  }
}

class HolyChart extends StatefulWidget {
  @override
  _HolyChartState createState() => _HolyChartState();
}

class _HolyChartState extends State<HolyChart> {
  Timer _timer;
  int _count = 0;
  ChartSeriesController _seriesController;
  final _dbReference = FirebaseDatabase.instance.reference();
  List _chartData = <ChartData>[];
  int _values;

  @override
  void dispose() {
    _timer?.cancel();
    super.dispose();
  }

  _updateData(Timer timer) {
    _chartData.add(ChartData(_count, _values));

    if (_chartData.length == 20) {
      _chartData.removeAt(0);
      _seriesController.updateDataSource(
          addedDataIndexes: <int>[_chartData.length - 1],
          removedDataIndexes: <int>[0]);
    }

    _count = _count + 1;
  }

  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
      stream: _dbReference.child("Data").onValue,
      builder: (context, snapshot) {
        Widget widget;

        if (snapshot.hasData &&
            !snapshot.hasError &&
            snapshot.data.snapshot.value != null) {
          _values = snapshot.data.snapshot.value["Moisture"];

          if (_values != null) {
            _timer = Timer.periodic(Duration(seconds: 3), _updateData(_timer));
          }

          widget = Container(
            child: SfCartesianChart(
              tooltipBehavior: TooltipBehavior(enable: true),
              primaryXAxis: DateTimeAxis(),
              series: <LineSeries<ChartData, int>>[
                LineSeries<ChartData, int>(
                  dataSource: _chartData,
                  xValueMapper: (ChartData data, _) => data.xAxisValue,
                  yValueMapper: (ChartData data, _) => data.yAxisValue,
                )
              ],
            ),
          );
        } else {
          widget = Center(child: CircularProgressIndicator());
        }

        return widget;
      },
    );
  }
}

class ChartData {
  ChartData(this.xAxisValue, this.yAxisValue);
  ChartData.fromMap(this.xAxisValue, this.yAxisValue);

  final int xAxisValue;
  final int yAxisValue;
}

这就是我的代码的问题。函数'millisecondsSinceEpoch'是什么意思?b

The following NoSuchMethodError was thrown building LayoutBuilder:
Class 'int' has no instance getter 'millisecondsSinceEpoch'.
Receiver: 0
Tried calling: millisecondsSinceEpoch

谁能帮我??非常感谢你的帮助...

4

2 回答 2

0

感谢您对我们的 Flutter 图表感兴趣。我们已经分析了您的查询和给定的代码,并想告诉您,您将 DateTimeAxis 用于 primaryXAxis,但您的数据源值不是 DateTime 值。因此,请在图表中使用 NumericAxis 来处理数值数据。要了解有关我们图表的更多信息,请查找帮助文档。

谢谢,陀罗尼塔兰。磷

于 2021-04-14T14:22:43.273 回答
0

我的猜测是您使用primaryXAxis: DateTimeAxis(),但您的 x/y 值都不是日期。SfCartesianChart尝试将 x 或 y 转换为日期(相millisecondsSinceEpoch对于日期的使用),但它们都是 int。

看看这个(搜索DateTimeAxis,有不同的例子):https ://help.syncfusion.com/flutter/cartesian-charts/axis-customization

于 2021-04-14T07:23:24.267 回答