|
| 1 | +import 'dart:developer'; |
| 2 | + |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:graphify/graphify.dart'; |
| 5 | + |
| 6 | +class TestChartClick extends StatefulWidget { |
| 7 | + const TestChartClick({super.key}); |
| 8 | + |
| 9 | + @override |
| 10 | + State<TestChartClick> createState() => _TestChartClickState(); |
| 11 | +} |
| 12 | + |
| 13 | +class _TestChartClickState extends State<TestChartClick> { |
| 14 | + late final GraphifyController _controller; |
| 15 | + final List<Map<String, dynamic>> _clickEvents = []; |
| 16 | + String _status = 'Ready to test clicks'; |
| 17 | + |
| 18 | + @override |
| 19 | + void initState() { |
| 20 | + super.initState(); |
| 21 | + _controller = GraphifyController(); |
| 22 | + _controller.chartClickedEvent.listen((data) { |
| 23 | + setState(() { |
| 24 | + _clickEvents.add(data); |
| 25 | + _status = 'Click received: ${data['name'] ?? 'Unknown'}'; |
| 26 | + }); |
| 27 | + |
| 28 | + log('TEST: Chart click received: $data'); |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + @override |
| 33 | + void dispose() { |
| 34 | + _controller.dispose(); |
| 35 | + super.dispose(); |
| 36 | + } |
| 37 | + |
| 38 | + @override |
| 39 | + Widget build(BuildContext context) { |
| 40 | + return Column( |
| 41 | + children: [ |
| 42 | + Container( |
| 43 | + width: double.infinity, |
| 44 | + padding: const EdgeInsets.all(16), |
| 45 | + child: Text( |
| 46 | + _status, |
| 47 | + style: const TextStyle(fontWeight: FontWeight.bold), |
| 48 | + textAlign: TextAlign.center, |
| 49 | + ), |
| 50 | + ), |
| 51 | + Expanded( |
| 52 | + flex: 1, |
| 53 | + child: Container( |
| 54 | + padding: const EdgeInsets.all(8), |
| 55 | + child: Expanded( |
| 56 | + child: ListView.builder( |
| 57 | + itemCount: _clickEvents.length, |
| 58 | + itemBuilder: (context, index) { |
| 59 | + final event = _clickEvents[index]; |
| 60 | + return Card( |
| 61 | + child: ListTile( |
| 62 | + title: Text('Event ${index + 1}'), |
| 63 | + subtitle: Text( |
| 64 | + 'Name: ${event['name'] ?? 'N/A'}\n' |
| 65 | + 'Value: ${event['value'] ?? 'N/A'}\n' |
| 66 | + 'Series: ${event['seriesName'] ?? 'N/A'}\n' |
| 67 | + 'Index: ${event['dataIndex'] ?? 'N/A'}', |
| 68 | + ), |
| 69 | + ), |
| 70 | + ); |
| 71 | + }, |
| 72 | + ), |
| 73 | + ), |
| 74 | + ), |
| 75 | + ), |
| 76 | + Expanded( |
| 77 | + flex: 2, |
| 78 | + child: Container( |
| 79 | + margin: const EdgeInsets.all(8), |
| 80 | + decoration: BoxDecoration( |
| 81 | + border: Border.all(color: Colors.grey), |
| 82 | + borderRadius: BorderRadius.circular(8), |
| 83 | + ), |
| 84 | + child: GraphifyView( |
| 85 | + controller: _controller, |
| 86 | + initialOptions: const { |
| 87 | + "tooltip": { |
| 88 | + "trigger": "axis", |
| 89 | + "axisPointer": {"type": "shadow"} |
| 90 | + }, |
| 91 | + "legend": { |
| 92 | + "data": ["Sales", "Marketing"] |
| 93 | + }, |
| 94 | + "xAxis": { |
| 95 | + "type": "category", |
| 96 | + "data": ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] |
| 97 | + }, |
| 98 | + "yAxis": {"type": "value"}, |
| 99 | + "series": [ |
| 100 | + { |
| 101 | + "name": "Sales", |
| 102 | + "type": "bar", |
| 103 | + "data": [120, 200, 150, 80, 70, 110, 130], |
| 104 | + "itemStyle": {"color": "#5470c6"} |
| 105 | + }, |
| 106 | + { |
| 107 | + "name": "Marketing", |
| 108 | + "type": "bar", |
| 109 | + "data": [60, 100, 75, 40, 35, 55, 65], |
| 110 | + "itemStyle": {"color": "#91cc75"} |
| 111 | + } |
| 112 | + ] |
| 113 | + }, |
| 114 | + ), |
| 115 | + ), |
| 116 | + ), |
| 117 | + ], |
| 118 | + ); |
| 119 | + } |
| 120 | +} |
0 commit comments