chart bar color fix

This commit is contained in:
venbaittech 2025-12-26 11:46:11 +05:30
parent 1b8ea81c16
commit a7262b3937

View File

@ -3190,6 +3190,7 @@ class ChartWidget extends StatelessWidget {
}
print('RT1 - $parsedChartBarColors');
bool hasBarColors = parsedChartBarColors.isNotEmpty;
print('RT1hasBarColors - $hasBarColors ');
return Column(
children: [
@ -4833,6 +4834,100 @@ class ChartWidget extends StatelessWidget {
Set<String> groupByValues,
) {
String groupByKeyValueData;
String cropKey = chartData['chart_type_json']['y_sub_group'];
if (chartData['chart_type_json']['chart_type'] == 'bar_chart_horizontal' ||
chartData['chart_type_json']['chart_type'] == 'horizontal_rotate') {
groupByKeyValueData = chartData['chart_type_json']['y_group'] ?? '';
} else {
groupByKeyValueData = chartData['chart_type_json']['x_group'] ?? '';
}
List<dynamic> responseData = chartData['response'];
List<BarChartGroupData> barGroups = [];
print('hormulti_bar1.1');
// First, extract all unique crop types across all groups to create a consistent color map
Set<String> uniqueCropTypes = {};
for (var entry in responseData) {
String crop = entry['ObsKey'][cropKey];
uniqueCropTypes.add(crop);
}
// Create a color map for crops
Map<String, Color> cropColorMap = {};
List<String> cropList = uniqueCropTypes.toList();
for (int i = 0; i < cropList.length; i++) {
final colorList;
if (chartData['dataset'] == 'general_education' ||
chartData['dataset'] == 'higher_education' ||
chartData['dataset'] == 'air_transport' ||
chartData['dataset'] == 'labour_force' ||
chartData['dataset'] == 'gdp' ||
chartData['dataset'] == 'hotels' ||
chartData['dataset'] == 'hotel_guests' ||
chartData['dataset'] == 'health_services' ||
chartData['dataset'] == 'clinics') {
colorList = uniqueColorsForTwo;
} else if (chartData['dataset'] == 'cropsk') {
colorList = uniqueColorsForThree;
} else if (chartData['dataset'] == 'oil_and_gas') {
colorList = uniqueColorsForFour;
} else {
colorList = uniqueColors;
}
cropColorMap[cropList[i]] = colorList[i % colorList.length];
}
// Iterate through groupByValues and populate BarChartGroupData
for (int i = 0; i < groupByValues.length; i++) {
String groupValue = groupByValues.elementAt(i);
// Filter data for the current group (grouping by CROP_TYPE)
List<dynamic> groupData = responseData.where((entry) {
return entry['ObsKey'][groupByKeyValueData] == groupValue;
}).toList();
// Create BarChartRodData for each bar in the group
List<BarChartRodData> barRods = groupData.map((data) {
// Ensure to retrieve and parse 'ObsValue' value (which should be a double)
double value =
double.tryParse(data['ObsValue']['Value'].toString()) ?? 0.0;
double minBarHeight = 0.1;
// Get the crop name to determine the color
String crop = data['ObsKey'][cropKey];
Color barColor = cropColorMap[crop] ?? Colors.grey;
return BarChartRodData(
toY: value == 0 ? minBarHeight : value, // Use the parsed value
color: barColor, // Use consistent color based on crop type
width: 20,
);
}).toList();
// Add BarChartGroupData for the group
barGroups.add(
BarChartGroupData(
x: i,
barRods: barRods,
showingTooltipIndicators: List.generate(
barRods.length,
(index) => index,
),
),
);
}
return barGroups;
}
List<BarChartGroupData> _buildHorizontalRotateBarGroups1(
dynamic chartData,
Set<String> groupByValues,
) {
String groupByKeyValueData;
if (chartData['chart_type_json']['chart_type'] == 'bar_chart_horizontal' ||
chartData['chart_type_json']['chart_type'] == 'horizontal_rotate') {
@ -5190,11 +5285,15 @@ Widget _buildChartLegend(
int index = uniqueCropTypes.toList().indexOf(
cropType,
); // Get index for cycling colors
print('HorizontalLegendsindex -$index');
Color cropColor = parsedChartBarColors.isNotEmpty &&
parsedChartBarColors.containsKey(cropType)
? parsedChartBarColors[cropType]!
: uniqueColors[index % uniqueColors.length];
print('HorizontalLegendscropColor -$cropColor');
return Row(
mainAxisSize: MainAxisSize.min,
children: [