dfd
unknown
dart
6 months ago
8.4 kB
3
Indexable
Never
import 'package:erp/utils/conts/color_manager.dart'; import 'package:fl_chart/fl_chart.dart'; import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import '../../../../utils/global/size_box.dart'; import '../../../../utils/resources/app_colors.dart'; class CustomBarChartWidget extends StatefulWidget { List<String> bottomTitleList; List<String> leftTitleList; List<String> firstDataList; List<String> secondDataList; Color leftBarColor; Color rightBarColor; Color avgColor; CustomBarChartWidget({ this.bottomTitleList = const ['Mn', 'Te', 'Wd', 'Tu', 'Fr', 'St', 'Su'], this.leftTitleList = const ['1K', '5K', '10K', '15K', '20K', '25K', '30K'], required this.firstDataList, required this.secondDataList, this.leftBarColor = ColorManager.yellowGraph, this.rightBarColor = ColorManager.redGraph, this.avgColor = Colors.green, super.key }); @override State<StatefulWidget> createState() => CustomBarChartWidgetState(); } class CustomBarChartWidgetState extends State<CustomBarChartWidget> { final double width = 7; List<BarChartGroupData> rawBarGroups = []; List<BarChartGroupData> showingBarGroups = []; int touchedGroupIndex = -1; @override void initState() { super.initState(); print("firs.....${widget.firstDataList}++"); // final barGroup1 = makeGroupData(0, 5, 12); // final barGroup2 = makeGroupData(1, 16, 12); // final barGroup3 = makeGroupData(2, 18, 5); // final barGroup4 = makeGroupData(3, 20, 16); // final barGroup5 = makeGroupData(4, 17, 6); // final barGroup6 = makeGroupData(5, 19, 1.5); // final barGroup7 = makeGroupData(6, 10, 1.5); //rawBarGroups.clear(); for(int i=0; i<widget.firstDataList.length; i++){ rawBarGroups.add(makeGroupData(i, double.parse(widget.firstDataList[i]), double.parse(widget.secondDataList[i]))); } // final items = [ // barGroup1, // barGroup2, // barGroup3, // barGroup4, // barGroup5, // barGroup6, // barGroup7, // ]; // // rawBarGroups = items; showingBarGroups = rawBarGroups; } @override Widget build(BuildContext context) { rawBarGroups.clear(); for(int i=0; i<widget.firstDataList.length; i++){ rawBarGroups.add(makeGroupData(i, double.parse(widget.firstDataList[i]), double.parse(widget.secondDataList[i]))); // rawBarGroups.add( makeGroupData(i, 10, 1.5)); } showingBarGroups.clear(); showingBarGroups = rawBarGroups; return Container( height: 230.h, color: Colors.white, child: Padding( padding: const EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: <Widget>[ Expanded( child: AspectRatio( aspectRatio: 1, child: BarChart( BarChartData( maxY: 20, // minY: 0, barTouchData: BarTouchData( touchTooltipData: BarTouchTooltipData( tooltipBgColor: Colors.grey, getTooltipItem: (a, b, c, d) => null, ), touchCallback: (FlTouchEvent event, response) { if (response == null || response.spot == null) { setState(() { touchedGroupIndex = -1; showingBarGroups = List.of(rawBarGroups); }); return; } touchedGroupIndex = response.spot!.touchedBarGroupIndex; setState(() { if (!event.isInterestedForInteractions) { touchedGroupIndex = -1; showingBarGroups = List.of(rawBarGroups); return; } showingBarGroups = List.of(rawBarGroups); if (touchedGroupIndex != -1) { var sum = 0.0; for (final rod in showingBarGroups[touchedGroupIndex].barRods) { sum += rod.toY; } final avg = sum / showingBarGroups[touchedGroupIndex] .barRods .length; showingBarGroups[touchedGroupIndex] = showingBarGroups[touchedGroupIndex].copyWith( barRods: showingBarGroups[touchedGroupIndex] .barRods .map((rod) { return rod.copyWith( toY: avg, color: widget.avgColor); }).toList(), ); } }); }, ), titlesData: FlTitlesData( show: true, rightTitles: const AxisTitles( sideTitles: SideTitles(showTitles: false), ), topTitles: const AxisTitles( sideTitles: SideTitles(showTitles: false), ), bottomTitles: AxisTitles( sideTitles: SideTitles( showTitles: true, getTitlesWidget: bottomTitles, reservedSize: 42, ), ), leftTitles: AxisTitles( sideTitles: SideTitles( showTitles: true, reservedSize: 28, interval: 1, getTitlesWidget: leftTitles, ), ), ), borderData: FlBorderData( show: false, ), barGroups: showingBarGroups, gridData: const FlGridData(show: false), ), ), ), ), const SizedBox( height: 12, ), ], ), ), ); } Widget leftTitles(double value, TitleMeta meta) { const style = TextStyle( color: Color(0xff7589a2), fontWeight: FontWeight.bold, fontSize: 14, ); String text; if (value >= 0) { text = widget.leftTitleList[0]; } else if (value >= 10) { text = widget.leftTitleList[1]; } else if (value == 20) { text = widget.leftTitleList[2]; } else if (value == 30) { text = widget.leftTitleList[3]; } else { return Container(); } return SideTitleWidget( axisSide: meta.axisSide, space: 0, child: Text(text, style: style), ); } Widget bottomTitles(double value, TitleMeta meta) { final titles = widget.bottomTitleList; final Widget text = Text( titles[value.toInt()], style: const TextStyle( color: Color(0xff7589a2), fontWeight: FontWeight.bold, fontSize: 14, ), ); return SideTitleWidget( axisSide: meta.axisSide, space: 16, //margin top child: text, ); } BarChartGroupData makeGroupData(int x, double y1, double y2) { return BarChartGroupData( barsSpace: 4, x: x, barRods: [ BarChartRodData( toY: y1, color: widget.leftBarColor, width: width, ), BarChartRodData( toY: y2, color: widget.rightBarColor, width: width, ), ], ); } }
Leave a Comment