From 95243c250f40ed1d430ac5306ee3c5e9bb222b06 Mon Sep 17 00:00:00 2001 From: tahaalam Date: Wed, 10 Dec 2025 10:18:09 +0300 Subject: [PATCH] graph shaded region added from horizontal spaces --- lib/features/lab/lab_view_model.dart | 31 ++++++---- .../lab/lab_results/lab_result_details.dart | 58 +++++++++++-------- lib/widgets/graph/custom_graph.dart | 55 +++++------------- 3 files changed, 70 insertions(+), 74 deletions(-) diff --git a/lib/features/lab/lab_view_model.dart b/lib/features/lab/lab_view_model.dart index 27ba643..3997afb 100644 --- a/lib/features/lab/lab_view_model.dart +++ b/lib/features/lab/lab_view_model.dart @@ -79,6 +79,7 @@ class LabViewModel extends ChangeNotifier { List indexedCharacterForUniqueTest = []; double maxY = 0.0; + double minY = double.infinity; double maxX = double.infinity; LabViewModel( @@ -317,7 +318,8 @@ class LabViewModel extends ChangeNotifier { mainLabResults.clear(); filteredGraphValues.clear(); maxY = double.negativeInfinity; - + minY = double.infinity; + maxX = double.infinity; final result = await labRepo.getPatientLabResults( laborder, Utils.isVidaPlusProject(int.parse(laborder.projectID ?? "0")), @@ -352,6 +354,9 @@ class LabViewModel extends ChangeNotifier { maxY = resultValue; maxX = maxY; } + if (resultValue < minY) { + minY = resultValue; + } if (highRefrenceValue < double.parse(element.referenceHigh ?? "0.0")) { highRefrenceValue = double.parse(element.referenceHigh ?? "0.0"); flagForHighReferenceRange = element.calculatedResultFlag; @@ -373,17 +378,23 @@ class LabViewModel extends ChangeNotifier { )); } catch (e) {} }); - maxY += transformValueInRange(50, "H"); - //todo handle the value if flags are null - if(flagForLowReferenceRange != null && flagForHighReferenceRange!= null){ - this.flagForHighReferenceRange = flagForHighReferenceRange; - this.flagForLowReferenceRange = flagForLowReferenceRange; - highTransformedReferenceValue = transformValueInRange(highRefrenceValue, flagForHighReferenceRange??""); - lowTransformedReferenceValue = transformValueInRange(lowRefenceValue, flagForLowReferenceRange??""); - this.highRefrenceValue = highRefrenceValue; - this.lowRefenceValue = lowRefenceValue; + if (flagForLowReferenceRange == null && flagForHighReferenceRange == null) { + highRefrenceValue = maxY; + lowRefenceValue = minY; } + this.flagForHighReferenceRange = flagForHighReferenceRange; + this.flagForLowReferenceRange = flagForLowReferenceRange; + highTransformedReferenceValue = double.parse(transformValueInRange(highRefrenceValue, flagForHighReferenceRange ?? "").toStringAsFixed(1)); + lowTransformedReferenceValue = double.parse(transformValueInRange(lowRefenceValue, flagForLowReferenceRange ?? "").toStringAsFixed(1)); + this.highRefrenceValue = double.parse(highRefrenceValue.toStringAsFixed(1)); + this.lowRefenceValue = double.parse(lowRefenceValue.toStringAsFixed(1)); + + if(maxY< highRefrenceValue) { + maxY = highRefrenceValue; + } + maxY += 25; + minY -= 25; LabResult recentResult = recentThree.first; checkIfGraphShouldBeDisplayed(recentResult); recentResult.verifiedOn = resultDate(DateUtil.convertStringToDate(recentResult.verifiedOnDateTime!)); diff --git a/lib/presentation/lab/lab_results/lab_result_details.dart b/lib/presentation/lab/lab_results/lab_result_details.dart index 1cbad59..d37dd72 100644 --- a/lib/presentation/lab/lab_results/lab_result_details.dart +++ b/lib/presentation/lab/lab_results/lab_result_details.dart @@ -284,15 +284,14 @@ class LabResultDetails extends StatelessWidget { leftLabelInterval: getInterval(labmodel), // maxY: (labmodel.maxY)+(getInterval(labmodel)??0)/2, maxY: (labmodel.maxY), + minY: labmodel.minY, maxX: labmodel.filteredGraphValues.length.toDouble()-.75, horizontalInterval: .1, getDrawingHorizontalLine: (value){ - print("the FLline transformed line is $value"); - print("the labmodel.highTransformedReferenceValue is ${labmodel.highTransformedReferenceValue}"); - print("thelabmodel.lowTransformedReferenceValue is ${labmodel.lowTransformedReferenceValue}"); - if(value == labmodel.highRefrenceValue ||value== labmodel.highRefrenceValue) { + value = double.parse(value.toStringAsFixed(1)); + if(value == labmodel.highRefrenceValue ||value== labmodel.lowRefenceValue) { return FlLine( - color: AppColors.greyTextColor, + color: AppColors.bgGreenColor.withOpacity(0.6), strokeWidth: 1, // dashArray: [5, 5], ); @@ -300,35 +299,22 @@ class LabResultDetails extends StatelessWidget { return FlLine( color: Colors.transparent, strokeWidth: 1, - // dashArray: [5, 5], );; }, leftLabelFormatter: (value) { + value = double.parse(value.toStringAsFixed(1)); // return leftLabels(value.toStringAsFixed(2)); - if(value == labmodel.highTransformedReferenceValue) + if(value == labmodel.highRefrenceValue) return leftLabels("High".needTranslation); - if(value== labmodel.lowTransformedReferenceValue) + if(value== labmodel.lowRefenceValue) return leftLabels("Low".needTranslation); - // switch (value.toInt()) { - // case 10: - // return leftLabels("Critical Low".needTranslation); - // case 30: - // return leftLabels("Low".needTranslation); - // case 50: - // return leftLabels("Normal".needTranslation); - // case 70: - // return leftLabels("High".needTranslation); - // case 90: - // return leftLabels( - // "Critical High".needTranslation); - // default: return SizedBox.shrink(); // } }, - graphColor:graphColor , - graphShadowColor: graphColor.withOpacity(.1), + graphColor:AppColors.blackColor, + graphShadowColor: Colors.transparent, graphGridColor: graphColor.withOpacity(.4), bottomLabelFormatter: (value, data) { if(data.isEmpty) return SizedBox.shrink(); @@ -343,6 +329,9 @@ class LabResultDetails extends StatelessWidget { } return SizedBox.shrink(); }, + rangeAnnotations: RangeAnnotations( + horizontalRangeAnnotations: _buildRangeShades(labmodel) + ), minX:(labmodel.filteredGraphValues.length == 1)?null : -.2, scrollDirection: Axis.horizontal, height: 180.h); @@ -351,6 +340,29 @@ class LabResultDetails extends StatelessWidget { } } + List _buildRangeShades( LabViewModel model,) { + List ranges = []; + + ranges.add(HorizontalRangeAnnotation( + y1:model.minY, + y2: model.lowRefenceValue, + color: AppColors.highAndLow.withOpacity(0.05), + )); + + ranges.add(HorizontalRangeAnnotation( + y1:model.lowRefenceValue, + y2: model.highRefrenceValue, + color: AppColors.bgGreenColor.withOpacity(0.05), + )); + + ranges.add(HorizontalRangeAnnotation( + y1:model.highRefrenceValue, + y2: model.maxY, + color: AppColors.criticalLowAndHigh.withOpacity(0.05), + )); + return ranges; + } + Widget labHistoryList(LabViewModel labmodel) { return SizedBox( height: labmodel.filteredGraphValues.length<3?labmodel.filteredGraphValues.length*64:180.h, diff --git a/lib/widgets/graph/custom_graph.dart b/lib/widgets/graph/custom_graph.dart index afa3c55..b955b32 100644 --- a/lib/widgets/graph/custom_graph.dart +++ b/lib/widgets/graph/custom_graph.dart @@ -59,6 +59,9 @@ class CustomGraph extends StatelessWidget { final bool? showGridLines; final GetDrawingGridLine? getDrawingHorizontalLine; final double? horizontalInterval; + final double? minY; + final bool showShadow; + final RangeAnnotations? rangeAnnotations; ///creates the left label and provide it to the chart as it will be used by other part of the application so the label will be different for every chart final Widget Function(double) leftLabelFormatter; @@ -96,14 +99,13 @@ class CustomGraph extends StatelessWidget { this.showGridLines = false, this.getDrawingHorizontalLine, this.horizontalInterval, + this.minY, + this.showShadow = false, + this.rangeAnnotations }); @override Widget build(BuildContext context) { - // var maxY = 0.0; - double interval = 20; - print("the maxY is $maxY"); - return Material( color: Colors.white, child: SizedBox( @@ -111,10 +113,8 @@ class CustomGraph extends StatelessWidget { height: height, child: LineChart( LineChartData( - minY: 0, - // maxY: ((maxY?.ceilToDouble() ?? 0.0) + interval).floorToDouble(), + minY: minY??0, maxY: maxY, - // minX: dataPoints.first.labelValue - 1, maxX: maxX, minX: minX , lineTouchData: LineTouchData( @@ -210,15 +210,17 @@ class CustomGraph extends StatelessWidget { ); }, ), + rangeAnnotations: rangeAnnotations ), ), - )); + ), + ); } + List _buildColoredLineSegments(List dataPoints) { final List allSpots = dataPoints.asMap().entries.map((entry) { double value = (makeGraphBasedOnActualValue)?double.tryParse(entry.value.actualValue)??0.0:entry.value.value; - debugPrint("the value is $value"); return FlSpot(entry.key.toDouble(), value); }).toList(); @@ -228,7 +230,7 @@ class CustomGraph extends StatelessWidget { isCurved: true, isStrokeCapRound: true, isStrokeJoinRound: true, - barWidth: 4, + barWidth: 2, gradient: LinearGradient( colors: [graphColor, graphColor], begin: Alignment.centerLeft, @@ -238,7 +240,7 @@ class CustomGraph extends StatelessWidget { show: false, ), belowBarData: BarAreaData( - show: true, + show: showShadow, gradient: LinearGradient( colors: [ graphShadowColor, @@ -253,33 +255,4 @@ class CustomGraph extends StatelessWidget { return data; } - - // Widget buildLabel(String label) { - // return Padding( - // padding: const EdgeInsets.only(right: 8), - // child: Text( - // label, - // style: TextStyle( - // fontSize: leftLabelSize ?? 8.fSize, color: leftLabelColor), - // textAlign: TextAlign.right, - // ), - // ); - // } - - -} - -// final List sampleData = [ -// DataPoint( -// value: 20, -// label: 'Jan 2024', -// ), -// DataPoint( -// value: 36, -// label: 'Feb 2024', -// ), -// DataPoint( -// value: 80, -// label: 'This result', -// ), -// ]; +} \ No newline at end of file