Skip to content

Commit 03b3fa0

Browse files
Merge pull request #2 from SyncfusionExamples/blog_sample
Updated the sample and readme file
2 parents 4c17c24 + 3305ae1 commit 03b3fa0

File tree

2 files changed

+67
-74
lines changed

2 files changed

+67
-74
lines changed

README.md

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
1-
# supabase_realtime_chart
1+
# How to integrate the Supabase database with the Syncfusion Flutter Chart
22

3-
A new Flutter project.
3+
This repository contains a sample that demonstrates how to integrate the Supabase database with [Syncfusion Flutter Chart](https://help.syncfusion.com/flutter/cartesian-charts/getting-started) widget and realtime data update.
44

5-
## Getting Started
65

7-
This project is a starting point for a Flutter application.
6+
## Syncfusion widgets:
87

9-
A few resources to get you started if this is your first Flutter project:
8+
This project used the following Syncfusion widget(s):
9+
* [SfCartesianChart](https://www.syncfusion.com/flutter-widgets/flutter-charts)
1010

11-
- [Lab: Write your first Flutter app](https://docs.flutter.dev/get-started/codelab)
12-
- [Cookbook: Useful Flutter samples](https://docs.flutter.dev/cookbook)
11+
## Supported platforms
1312

14-
For help getting started with Flutter development, view the
15-
[online documentation](https://docs.flutter.dev/), which offers tutorials,
16-
samples, guidance on mobile development, and a full API reference.
13+
Refer to the following link to know about the supported platform - [Platforms](https://help.syncfusion.com/flutter/system-requirements#supported-platforms)
14+
15+
## Requirements to run the sample
16+
17+
Refer to the following link to know about system requirements - [System Requirements](https://help.syncfusion.com/flutter/system-requirements)
18+
19+
## How to run the sample
20+
21+
1. Clone the sample and open it in preferred IDE.
22+
23+
*Note: If you download the sample using the "Download ZIP" option, right-click it, select Properties, and then select Unblock.*
24+
25+
2. Run the application.
26+
27+
## License
28+
29+
Syncfusion has no liability for any damage or consequence that may arise by using or viewing the samples. The samples are for demonstrative purposes, and if you choose to use or access the samples, you agree to not hold Syncfusion liable, in any form, for any damage that is related to use, for accessing, or viewing the samples. By accessing, viewing, or seeing the samples, you acknowledge and agree Syncfusion’s samples will not allow you seek injunctive relief in any form for any claim related to the sample. If you do not agree to this, do not view, access, utilize, or otherwise do anything with Syncfusion’s samples.

lib/main.dart

Lines changed: 44 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
library event_calendar;
2-
31
import 'dart:math';
42

53
import 'package:flutter/material.dart';
@@ -10,8 +8,8 @@ Future<void> main() async {
108
WidgetsFlutterBinding.ensureInitialized();
119

1210
await Supabase.initialize(
13-
url: 'YOUR_SUPABASE_URL',
14-
anonKey: 'YOUR_SUPABASE_ANON_KEY',
11+
url: 'Your Supabase URL',
12+
anonKey: 'Your Supabase Anon Key',
1513
);
1614
runApp(MyApp());
1715
}
@@ -22,23 +20,24 @@ class MyApp extends StatelessWidget {
2220
@override
2321
Widget build(BuildContext context) {
2422
return const MaterialApp(
25-
title: 'Supabase Calendar',
23+
title: 'Supabase Chart',
2624
debugShowCheckedModeBanner: false,
27-
home: EventCalendar(),
25+
home: LiveChart(),
2826
);
2927
}
3028
}
3129

32-
class EventCalendar extends StatefulWidget {
33-
const EventCalendar({super.key});
30+
class LiveChart extends StatefulWidget {
31+
const LiveChart({super.key});
3432

3533
@override
36-
EventCalendarState createState() => EventCalendarState();
34+
LiveChartState createState() => LiveChartState();
3735
}
3836

39-
class EventCalendarState extends State<EventCalendar> {
40-
List<ChartData> chartData = <ChartData>[];
41-
final _future = supabase.from('gold_rate').stream(primaryKey: ['id']);
37+
class LiveChartState extends State<LiveChart> {
38+
List<ChartData> _chartData = <ChartData>[];
39+
final Stream<List<Map<String, dynamic>>> _future =
40+
supabase.from('table_name').stream(primaryKey: ['id']);
4241
final SupabaseClient _client = Supabase.instance.client;
4342

4443
@override
@@ -72,39 +71,23 @@ class EventCalendarState extends State<EventCalendar> {
7271
return const Center(child: CircularProgressIndicator());
7372
}
7473
final countries = snapshot.data!;
75-
chartData =
74+
_chartData =
7675
countries.map((e) => ChartData.fromSnapShot(e)).toList();
77-
return Padding(
78-
padding:
79-
const EdgeInsets.only(top: 18.0, left: 30, right: 30),
80-
child: SfCartesianChart(
81-
plotAreaBorderWidth: 0,
82-
primaryXAxis: DateTimeCategoryAxis(
83-
interval: 1,
84-
axisLabelFormatter: (args) {
85-
return ChartAxisLabel(
86-
args.text.replaceAll(' ', '\n'), args.textStyle);
87-
},
88-
majorGridLines: const MajorGridLines(width: 0),
89-
),
90-
primaryYAxis: const NumericAxis(
91-
interval: 6,
92-
rangePadding: ChartRangePadding.additional,
93-
),
94-
series: <CartesianSeries>[
95-
CandleSeries<ChartData, DateTime>(
96-
animationDuration: 0,
97-
dataSource: chartData,
98-
xValueMapper: (ChartData data, _) => data.timestamp,
99-
lowValueMapper: (ChartData data, _) => data.low,
100-
highValueMapper: (ChartData data, _) => data.high,
101-
openValueMapper: (ChartData data, _) => data.open,
102-
closeValueMapper: (ChartData data, _) => data.close,
103-
// dataLabelSettings:
104-
// const DataLabelSettings(isVisible: true),
105-
)
106-
],
76+
return SfCartesianChart(
77+
primaryXAxis: const DateTimeCategoryAxis(),
78+
primaryYAxis: const NumericAxis(
79+
interval: 4,
10780
),
81+
series: <CartesianSeries>[
82+
ColumnSeries<ChartData, DateTime>(
83+
animationDuration: 0,
84+
dataSource: _chartData,
85+
xValueMapper: (ChartData data, int index) =>
86+
data.timestamp,
87+
yValueMapper: (ChartData data, int index) =>
88+
data.yValue,
89+
)
90+
],
10891
);
10992
},
11093
),
@@ -116,25 +99,30 @@ class EventCalendarState extends State<EventCalendar> {
11699
children: [
117100
TextButton(
118101
onPressed: () async {
119-
await _client.from('gold_rate').insert({
120-
'id': chartData.isEmpty ? 1 : chartData.length + 1,
121-
'date': chartData.last.timestamp
102+
await _client.from('table_name').insert({
103+
'id': _chartData.isEmpty ? 1 : _chartData.length + 1,
104+
'date': _chartData.last.timestamp
122105
.add(const Duration(days: 1))
123106
.toIso8601String(),
124-
'low': 106.4,
125-
'high': 112.5,
126-
'open': 107.2,
127-
'close': 110.9,
107+
'yValue': _getRandomInt(105, 120),
128108
});
129109
},
130110
child: const Text('Add data point at last'),
131111
),
112+
TextButton(
113+
onPressed: () async {
114+
await _client.from('table_name').update({
115+
'yValue': _getRandomInt(105, 120),
116+
}).eq('id', 5);
117+
},
118+
child: const Text('Update y value of 5th segment'),
119+
),
132120
TextButton(
133121
onPressed: () async {
134122
await _client
135-
.from('gold_rate')
123+
.from('table_name')
136124
.delete()
137-
.eq('id', chartData.last.id);
125+
.eq('id', _chartData.last.id);
138126
},
139127
child: const Text('Remove data point at last'),
140128
),
@@ -153,30 +141,22 @@ class EventCalendarState extends State<EventCalendar> {
153141
}
154142
}
155143

144+
// Custom data model class with constructor to convert the data from snapshot
156145
class ChartData {
157146
ChartData({
158147
required this.id,
159148
required this.timestamp,
160-
required this.open,
161-
required this.close,
162-
required this.high,
163-
required this.low,
149+
required this.yValue,
164150
});
165151

166152
num id;
167153
DateTime timestamp;
168-
num open;
169-
num close;
170-
num high;
171-
num low;
154+
num yValue;
172155

173156
static ChartData fromSnapShot(Map<String, dynamic> dataSnapshot) {
174157
return ChartData(
175158
id: dataSnapshot['id'],
176159
timestamp: DateTime.parse(dataSnapshot['date']),
177-
open: dataSnapshot['open'],
178-
close: dataSnapshot['close'],
179-
high: dataSnapshot['high'],
180-
low: dataSnapshot['low']);
160+
yValue: dataSnapshot['yValue']);
181161
}
182162
}

0 commit comments

Comments
 (0)