131 lines
3.9 KiB
Dart
Raw Normal View History

2023-02-11 10:59:24 +01:00
// Copyright (c) 2023 Tajaouart Mounir
//
// This demo showcases the usage of the `RealTimeGraph` widget
// and provides an example of how to use it in a Flutter app.
// Website: https://tajaouart.com
2023-02-09 15:05:33 +01:00
import 'dart:math';
2023-02-09 10:26:53 +01:00
import 'package:flutter/material.dart';
2023-02-09 11:25:48 +01:00
import 'package:real_time_chart/real_time_chart.dart';
2023-02-09 10:26:53 +01:00
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
2023-02-11 10:59:24 +01:00
const MyApp({Key? key}) : super(key: key);
2023-02-09 10:26:53 +01:00
@override
Widget build(BuildContext context) {
return MaterialApp(
2023-02-11 10:59:24 +01:00
debugShowCheckedModeBanner: false,
title: 'Real-Time Chart Demo',
2023-02-09 10:26:53 +01:00
theme: ThemeData(
primarySwatch: Colors.blue,
),
2023-02-11 10:59:24 +01:00
home: const MyHomePage(title: 'Real-Time Chart Demo'),
2023-02-09 10:26:53 +01:00
);
}
}
class MyHomePage extends StatefulWidget {
2023-02-11 10:59:24 +01:00
const MyHomePage({Key? key, required this.title}) : super(key: key);
2023-02-09 10:26:53 +01:00
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
2023-02-10 17:16:00 +01:00
final stream = positiveDataStream();
2023-02-09 10:26:53 +01:00
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: SizedBox(
width: double.maxFinite,
2023-02-10 17:16:00 +01:00
child: SingleChildScrollView(
child: Column(
2023-02-11 10:59:24 +01:00
crossAxisAlignment: CrossAxisAlignment.start,
2023-02-10 17:16:00 +01:00
children: [
2023-02-11 10:59:24 +01:00
// Display a real-time graph of positive data
2023-02-10 17:16:00 +01:00
SizedBox(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.width * 0.8,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: RealTimeGraph(
stream: stream,
),
),
),
2023-02-11 10:59:24 +01:00
// Display a real-time graph of positive data as points
2023-02-10 17:16:00 +01:00
const SizedBox(height: 32),
SizedBox(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.width * 0.8,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: RealTimeGraph(
stream: stream,
displayMode: ChartDisplay.points,
),
),
),
2023-02-11 10:59:24 +01:00
// Display a real-time graph of positive and negative data
2023-02-10 17:16:00 +01:00
const SizedBox(height: 32),
2023-02-11 10:59:24 +01:00
const Padding(
padding: EdgeInsets.all(16.0),
child: Text(
'Supports negative values :',
),
),
2023-02-10 17:16:00 +01:00
SizedBox(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.width * 0.8,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: RealTimeGraph(
stream: stream.map((value) => value - 150),
supportNegativeValuesDisplay: true,
xAxisColor: Colors.black12,
2023-02-10 17:16:00 +01:00
),
),
),
2023-02-11 10:59:24 +01:00
// Display a real-time graph of positive and negative data as points
2023-02-10 17:16:00 +01:00
SizedBox(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.width * 0.8,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: RealTimeGraph(
stream: stream.map((value) => value - 150),
supportNegativeValuesDisplay: true,
displayMode: ChartDisplay.points,
xAxisColor: Colors.black12,
2023-02-10 17:16:00 +01:00
),
),
),
],
),
),
2023-02-09 10:26:53 +01:00
),
);
}
2023-02-09 11:25:48 +01:00
2023-02-10 17:16:00 +01:00
Stream<double> positiveDataStream() {
2023-02-09 15:05:33 +01:00
return Stream.periodic(const Duration(milliseconds: 500), (_) {
2023-02-10 17:16:00 +01:00
return Random().nextInt(300).toDouble();
}).asBroadcastStream();
2023-02-09 11:25:48 +01:00
}
2023-02-09 10:26:53 +01:00
}