因此,我很难弄清楚如何将从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()));
},
);
}
但这不是我想要的。
1条答案
按热度按时间r7s23pms1#
首先,您需要提供一些参数
下面的标题将是
测试片段