dart 如何在fl_chart中的bottomTitle上显示日期

mfuanj7w  于 2023-02-27  发布在  其他
关注(0)|答案(1)|浏览(133)

因此,我很难弄清楚如何将从PHP脚本获得的日期显示到FlChart中。
我从PHP脚本中得到的数据看起来像这样,所以基本上这意味着我得到了测量当天的平均值。

[
  {
    "Datum": "Sun",
    "Avg": 166
  },
  {
    "Datum": "Mon",
    "Avg": 333
  },
  {
    "Datum": "Sat",
    "Avg": 123
  }
]

我的类看起来像这样

class AvgDataBaseData {
  final int? avg;
  final String? date;
  AvgDataBaseData({this.avg, this.date});
  factory AvgDataBaseData.fromJson(Map<String, dynamic> json) {
    return AvgDataBaseData(avg: json['Avg'], date: json['Datum']);
  }
}

显示折线图的代码

class AvgDataBaseList extends StatelessWidget {
  const AvgDataBaseList({super.key, required this.AvgDataBases});
  final List<AvgDataBaseData> AvgDataBases;

  @override
  Widget build(BuildContext context) {
    List<FlSpot> spots = AvgDataBases.asMap().entries.map((e) {
      return FlSpot(e.key.toDouble(), e.value.avg!.toDouble());
    }).toList();

    SideTitles _bottomTitles() {
      return SideTitles(
        showTitles: true,
        getTitlesWidget: (value, meta) {
          var date = AvgDataBases.asMap().entries.map((e) => Text(e.value.date.toString()),).toList();
          return SideTitleWidget(axisSide: meta.axisSide, child: Text(date.toString()));
        },
      );
    }

    return Container(
        margin: const EdgeInsets.symmetric(horizontal: 50),
        child: LineChart(LineChartData(
          borderData: borderData,
          titlesData: FlTitlesData(
            show: true,
            bottomTitles: AxisTitles(
              sideTitles: _bottomTitles(),
            ),
            leftTitles: AxisTitles(
              sideTitles: SideTitles(showTitles: false),
            ),
            topTitles: AxisTitles(
              sideTitles: SideTitles(showTitles: false),
            ),
            rightTitles: AxisTitles(
              sideTitles: SideTitles(showTitles: false),
            ),
          ),
          lineBarsData: [
            LineChartBarData(
                barWidth: 5,
                spots: spots,
                isCurved: true,
                gradient: LinearGradient(colors: gradientColors),
                belowBarData: BarAreaData(
                    show: true,
                    gradient: LinearGradient(
                      colors: gradientColors
                          .map((color) => color.withOpacity(0.3))
                          .toList(),
                    )))
          ],
          //to deactive the grid
          gridData: gridData,
          //titlesData: titlesData)),
          // titlesData: FlTitlesData(
          //     rightTitles: AxisTitles(
          //       sideTitles: SideTitles(
          //         showTitles: true,
          //         reservedSize: 40,
          //       ),
          //     ),
          //     show: false))),
        )));
  }
}

我可以在行图表中显示平均值,但无法显示测量平均值的日期。我尝试如下返回SideTitleWidget

SideTitles _bottomTitles() {
      return SideTitles(
        showTitles: true,
        getTitlesWidget: (value, meta) {
          var date = AvgDataBases.asMap().entries.map((e) => Text(e.value.date.toString()),).toList();
          return SideTitleWidget(axisSide: meta.axisSide, child: Text(date.toString()));
        },
      );
    }

但这不是我想要的。

r7s23pms

r7s23pms1#

首先,您需要提供一些参数

LineChartData(
          minX: 0,
          maxX: AvgDataBases.length.toDouble(),
          minY: 0,
          maxY: maxY.toDouble(),

下面的标题将是

SideTitles _bottomTitles() {
      return SideTitles(
        showTitles: true,
        interval: 1,
        getTitlesWidget: (value, meta) {
          var date = value.toInt() < AvgDataBases.length
              ? AvgDataBases[value.toInt()].date
              : "";
          return SideTitleWidget(axisSide: meta.axisSide, child: Text("$date"));
        },
      );
    }

测试片段

class AvgDataBaseList extends StatelessWidget {
  // const AvgDataBaseList({super.key, required this.AvgDataBases});
  final List<AvgDataBaseData> AvgDataBases = [
    AvgDataBaseData(avg: 166, date: "Sun"),
    AvgDataBaseData(avg: 333, date: "Mon"),
    AvgDataBaseData(avg: 123, date: "Sat"),
  ];

  late final maxY = AvgDataBases.map((e) => e.avg ?? 0).toList().reduce(max); // import dart:math
  @override
  Widget build(BuildContext context) {
    List<FlSpot> spots = AvgDataBases.asMap().entries.map((e) {
      return FlSpot(e.key.toDouble(), e.value.avg!.toDouble());
    }).toList();

    SideTitles _bottomTitles() {
      return SideTitles(
        showTitles: true,
        interval: 1,
        getTitlesWidget: (value, meta) {
          var date = value.toInt() < AvgDataBases.length
              ? AvgDataBases[value.toInt()].date
              : "";
          return SideTitleWidget(axisSide: meta.axisSide, child: Text("$date"));
        },
      );
    }

    return Container(
        margin: const EdgeInsets.symmetric(horizontal: 50),
        child: LineChart(LineChartData(
          minX: 0,
          maxX: AvgDataBases.length.toDouble(),
          minY: 0,
          maxY: maxY.toDouble(),
          baselineX: 4,
          titlesData: FlTitlesData(
            show: true,
            bottomTitles: AxisTitles(
              sideTitles: _bottomTitles(),
            ),
            leftTitles: AxisTitles(
              sideTitles: SideTitles(showTitles: false),
            ),
            topTitles: AxisTitles(
              sideTitles: SideTitles(showTitles: false),
            ),
            rightTitles: AxisTitles(
              sideTitles: SideTitles(showTitles: false),
            ),
          ),
          lineBarsData: [
            LineChartBarData(
              barWidth: 5,
              spots: spots,
              isCurved: true,
              belowBarData: BarAreaData(
                show: true,
              ),
            )
          ],
        )));
  }
}

相关问题