请使用flutter设计一个用于显示曲线图的chart控件,支持手势缩放,有坐标轴显示,右上角显示曲线编号及对应颜色,支持空安全;有可传递曲线数据的增加曲线函数,新增曲线随机使用不同的颜色;有清空曲线函数;使用charts_flutter库;可设置坐标轴参数
时间: 2024-03-20 09:39:20 浏览: 200
图表控件,使用方便。。
以下是一个使用charts_flutter库实现的简单的可缩放曲线图控件,支持手势缩放、坐标轴显示、曲线编号及对应颜色显示、空安全、新增曲线及清空曲线功能。代码中有注释说明如何使用和设置坐标轴参数。
```dart
import 'package:flutter/material.dart';
import 'package:charts_flutter/flutter.dart' as charts;
class ZoomableChart extends StatefulWidget {
ZoomableChart({Key? key}) : super(key: key);
@override
_ZoomableChartState createState() => _ZoomableChartState();
}
class _ZoomableChartState extends State<ZoomableChart> {
final List<List<charts.Series<TimeSeriesData, DateTime>>> _seriesList = [];
final List<String> _seriesNames = [];
final List<Color> _seriesColors = [];
// 控制手势缩放的控制器
final _zoomController = charts.ChartController();
// 控制曲线条数的计数器
int _seriesCount = 0;
// 添加一条新曲线
void addSeries(List<TimeSeriesData> data) {
final seriesName = 'Series $_seriesCount';
final seriesColor = Color((Math.Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(1.0);
final series = _createSeries(data, seriesName, seriesColor);
setState(() {
_seriesList.add(series);
_seriesNames.add(seriesName);
_seriesColors.add(seriesColor);
_seriesCount++;
});
}
// 清空所有曲线
void clear() {
setState(() {
_seriesList.clear();
_seriesNames.clear();
_seriesColors.clear();
_seriesCount = 0;
});
}
// 创建曲线
List<charts.Series<TimeSeriesData, DateTime>> _createSeries(
List<TimeSeriesData> data, String seriesName, Color seriesColor) {
return [
charts.Series<TimeSeriesData, DateTime>(
id: seriesName,
colorFn: (_, __) => charts.ColorUtil.fromDartColor(seriesColor),
domainFn: (TimeSeriesData data, _) => data.time,
measureFn: (TimeSeriesData data, _) => data.value,
data: data,
)
];
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Zoomable Chart'),
),
body: Column(
children: [
// 曲线图
Expanded(
child: charts.TimeSeriesChart(
_seriesList.isNotEmpty ? _seriesList[0] : [],
animate: false,
dateTimeFactory: const charts.LocalDateTimeFactory(),
behaviors: [
// 手势缩放
charts.PanAndZoomBehavior(
panningCompletedCallback: () {},
zoomingCompletedCallback: () {},
maxScaleFactor: 20,
minScaleFactor: 0.5,
selectionModelTrigger: charts.SelectionModelTrigger.none,
chartController: _zoomController,
),
],
primaryMeasureAxis: charts.NumericAxisSpec(
tickProviderSpec: charts.BasicNumericTickProviderSpec(zeroBound: false),
renderSpec: charts.GridlineRendererSpec(
labelStyle: charts.TextStyleSpec(
fontSize: 12,
color: charts.MaterialPalette.gray.shadeDefault,
),
lineStyle: charts.LineStyleSpec(
color: charts.MaterialPalette.gray.shade300,
),
),
),
domainAxis: charts.DateTimeAxisSpec(
tickFormatterSpec: charts.AutoDateTimeTickFormatterSpec(
day: charts.TimeFormatterSpec(
format: 'd',
transitionFormat: 'MM/dd',
),
),
renderSpec: charts.GridlineRendererSpec(
labelStyle: charts.TextStyleSpec(
fontSize: 12,
color: charts.MaterialPalette.gray.shadeDefault,
),
lineStyle: charts.LineStyleSpec(
color: charts.MaterialPalette.gray.shade300,
),
),
),
),
),
// 曲线编号及对应颜色
SizedBox(
height: 40,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: _seriesNames.length,
itemBuilder: (context, index) {
return Row(
children: [
SizedBox(width: 8),
Container(
width: 20,
height: 20,
color: _seriesColors[index],
),
SizedBox(width: 4),
Text(_seriesNames[index]),
SizedBox(width: 8),
],
);
},
),
),
],
),
);
}
}
// 时间序列数据
class TimeSeriesData {
final DateTime time;
final double value;
TimeSeriesData(this.time, this.value);
}
```
阅读全文