|
| 1 | +import 'package:cristalyse/cristalyse.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:intl/intl.dart'; |
| 4 | + |
| 5 | +class TimeBasedLineChartWidget extends StatefulWidget { |
| 6 | + final ChartTheme currentTheme; |
| 7 | + final List<Map<String, dynamic>> data; |
| 8 | + final double sliderValue; |
| 9 | + |
| 10 | + const TimeBasedLineChartWidget({ |
| 11 | + super.key, |
| 12 | + required this.currentTheme, |
| 13 | + required this.data, |
| 14 | + required this.sliderValue, |
| 15 | + }); |
| 16 | + |
| 17 | + @override |
| 18 | + State<TimeBasedLineChartWidget> createState() => |
| 19 | + _TimeBasedLineChartWidgetState(); |
| 20 | +} |
| 21 | + |
| 22 | +class _TimeBasedLineChartWidgetState extends State<TimeBasedLineChartWidget> { |
| 23 | + bool _simpleLinear = false; |
| 24 | + |
| 25 | + @override |
| 26 | + Widget build(BuildContext context) { |
| 27 | + return SingleChildScrollView( |
| 28 | + padding: const EdgeInsets.all(16), |
| 29 | + child: Column( |
| 30 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 31 | + children: [ |
| 32 | + Text( |
| 33 | + 'Animated Line Chart', |
| 34 | + style: TextStyle( |
| 35 | + fontSize: 18, |
| 36 | + fontWeight: FontWeight.w600, |
| 37 | + color: widget.currentTheme.axisColor, |
| 38 | + ), |
| 39 | + ), |
| 40 | + const SizedBox(height: 16), |
| 41 | + SizedBox( |
| 42 | + height: 400, |
| 43 | + child: CristalyseChart() |
| 44 | + .data(_data) |
| 45 | + .mapping(x: 'x', y: 'y') |
| 46 | + .geomLine( |
| 47 | + strokeWidth: 1.0 + widget.sliderValue * 9.0, alpha: 0.9) |
| 48 | + .scaleXContinuous( |
| 49 | + title: 'Timestamp', |
| 50 | + tickConfig: TickConfig(simpleLinear: _simpleLinear), |
| 51 | + labels: (value) { |
| 52 | + final date = |
| 53 | + DateTime.fromMillisecondsSinceEpoch(value.toInt()); |
| 54 | + return DateFormat('MM/dd HH:mm:ss').format(date); |
| 55 | + }) |
| 56 | + .scaleYContinuous( |
| 57 | + title: 'Value (units)', |
| 58 | + tickConfig: TickConfig(simpleLinear: _simpleLinear)) |
| 59 | + .theme(widget.currentTheme) |
| 60 | + .build(), |
| 61 | + ), |
| 62 | + // toggle switch |
| 63 | + SwitchListTile( |
| 64 | + title: const Text('Use simple linear ticks'), |
| 65 | + value: _simpleLinear, |
| 66 | + onChanged: (value) { |
| 67 | + setState(() { |
| 68 | + _simpleLinear = value; |
| 69 | + }); |
| 70 | + }, |
| 71 | + ), |
| 72 | + ], |
| 73 | + ), |
| 74 | + ); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// Keep the original function for backward compatibility |
| 79 | +Widget buildTimeBasedLineChartTab(ChartTheme currentTheme, |
| 80 | + List<Map<String, dynamic>> data, double sliderValue) { |
| 81 | + return TimeBasedLineChartWidget( |
| 82 | + currentTheme: currentTheme, |
| 83 | + data: data, |
| 84 | + sliderValue: sliderValue, |
| 85 | + ); |
| 86 | +} |
| 87 | + |
| 88 | +const _data = [ |
| 89 | + {'x': 1761421958320.0, 'y': 4.900000095367432, 's': 'cooler.temp.avg'}, |
| 90 | + {'x': 1761421959321.0, 'y': 5.0, 's': 'cooler.temp.avg'}, |
| 91 | + {'x': 1761421960320.0, 'y': 5.0, 's': 'cooler.temp.avg'}, |
| 92 | + {'x': 1761421961320.0, 'y': 4.900000095367432, 's': 'cooler.temp.avg'} |
| 93 | +]; |
0 commit comments