set param to switch display of the axis lines
This commit is contained in:
parent
1b651874b2
commit
73f91e110e
@ -11,6 +11,7 @@ class RealTimeGraph extends StatefulWidget {
|
|||||||
this.updateDelay = const Duration(milliseconds: 50),
|
this.updateDelay = const Duration(milliseconds: 50),
|
||||||
this.displayMode = ChartDisplay.line,
|
this.displayMode = ChartDisplay.line,
|
||||||
this.displayYAxisValues = true,
|
this.displayYAxisValues = true,
|
||||||
|
this.displayYAxisLines = true,
|
||||||
this.axisColor = Colors.black87,
|
this.axisColor = Colors.black87,
|
||||||
this.graphColor = Colors.black45,
|
this.graphColor = Colors.black45,
|
||||||
this.pointsSpacing = 3.0,
|
this.pointsSpacing = 3.0,
|
||||||
@ -32,6 +33,9 @@ class RealTimeGraph extends StatefulWidget {
|
|||||||
// Flag to display Y-axis values or not.
|
// Flag to display Y-axis values or not.
|
||||||
final bool displayYAxisValues;
|
final bool displayYAxisValues;
|
||||||
|
|
||||||
|
// Flag to display the X-Y-axis lines or not.
|
||||||
|
final bool displayYAxisLines;
|
||||||
|
|
||||||
// The stream to listen to for new data.
|
// The stream to listen to for new data.
|
||||||
final Stream<double> stream;
|
final Stream<double> stream;
|
||||||
|
|
||||||
@ -65,11 +69,16 @@ class RealTimeGraph extends StatefulWidget {
|
|||||||
|
|
||||||
class RealTimeGraphState extends State<RealTimeGraph>
|
class RealTimeGraphState extends State<RealTimeGraph>
|
||||||
with TickerProviderStateMixin {
|
with TickerProviderStateMixin {
|
||||||
|
// Subscription to the stream provided in the constructor
|
||||||
StreamSubscription<double>? streamSubscription;
|
StreamSubscription<double>? streamSubscription;
|
||||||
|
|
||||||
|
// List of data points to be displayed on the graph
|
||||||
List<Point<double>> _data = [];
|
List<Point<double>> _data = [];
|
||||||
|
|
||||||
|
// Timer to periodically update the data for visualization
|
||||||
Timer? timer;
|
Timer? timer;
|
||||||
|
|
||||||
|
// Width of the canvas for the graph
|
||||||
double canvasWidth = 1000;
|
double canvasWidth = 1000;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -99,12 +108,15 @@ class RealTimeGraphState extends State<RealTimeGraph>
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Maximum value of the y-axis of the graph
|
||||||
double get maxValue {
|
double get maxValue {
|
||||||
return _data.isEmpty ? 0 : _data.map((point) => point.y).reduce(max);
|
return _data.isEmpty ? 0 : _data.map((point) => point.y).reduce(max);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Minimum value of the y-axis of the graph
|
||||||
double get minValue => widget.minValue;
|
double get minValue => widget.minValue;
|
||||||
|
|
||||||
|
// Median value of the y-axis of the graph
|
||||||
double get medianValue => (maxValue + minValue) / 2;
|
double get medianValue => (maxValue + minValue) / 2;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -112,16 +124,21 @@ class RealTimeGraphState extends State<RealTimeGraph>
|
|||||||
return Row(
|
return Row(
|
||||||
mainAxisSize: MainAxisSize.max,
|
mainAxisSize: MainAxisSize.max,
|
||||||
children: [
|
children: [
|
||||||
|
// Display the values of the y-axis if the option is enabled
|
||||||
if (widget.displayYAxisValues)
|
if (widget.displayYAxisValues)
|
||||||
Column(
|
Column(
|
||||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||||
children: [
|
children: [
|
||||||
|
// Call the custom axis text builder if provided
|
||||||
|
// or use the default text builder otherwise
|
||||||
widget.axisTextBuilder?.call(maxValue) ?? textBuilder(maxValue),
|
widget.axisTextBuilder?.call(maxValue) ?? textBuilder(maxValue),
|
||||||
widget.axisTextBuilder?.call(medianValue) ??
|
widget.axisTextBuilder?.call(medianValue) ??
|
||||||
textBuilder(medianValue),
|
textBuilder(medianValue),
|
||||||
widget.axisTextBuilder?.call(minValue) ?? textBuilder(minValue),
|
widget.axisTextBuilder?.call(minValue) ?? textBuilder(minValue),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
// Display the y-axis line
|
||||||
|
if (widget.displayYAxisLines)
|
||||||
Container(
|
Container(
|
||||||
color: widget.axisColor,
|
color: widget.axisColor,
|
||||||
width: widget.axisStroke,
|
width: widget.axisStroke,
|
||||||
@ -172,6 +189,7 @@ class RealTimeGraphState extends State<RealTimeGraph>
|
|||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
if (widget.displayYAxisLines)
|
||||||
Container(
|
Container(
|
||||||
color: widget.axisColor,
|
color: widget.axisColor,
|
||||||
height: widget.axisStroke,
|
height: widget.axisStroke,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user