DiscoverModule/lib/ui_screen/chart.dart

252 lines
7.3 KiB
Dart
Raw Normal View History

2024-05-20 10:29:02 +00:00
// import 'package:flutter/material.dart';
// import 'package:charts_flutter/flutter.dart' as charts;
// class Chart extends StatefulWidget {
// const Chart({super.key});
// @override
// State<Chart> createState() => _ChartState();
// }
// class _ChartState extends State<Chart> {
// @override
// Widget build(BuildContext context) {
// return Center(
// child: Container(
// height: 200,
// child: _buildChart(),
// ),
// );
// }
// }
// Widget _buildChart() {
// final data = [
// LinearSales("Jan", [50, 100]),
// LinearSales("Feb", [75, 25]),
// LinearSales("Mar", [50, 0]),
// LinearSales("Apr", [25, 85]),
// LinearSales("May", [10, 20]),
// ];
// final chart = charts.BarChart(
// [
// charts.Series<LinearSales, String>(
// id: 'Sales',
// colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault,
// domainFn: (LinearSales sales, _) => sales.month,
// // measureFn: (LinearSales sales, _) => sales.sales,
// measureFn: (LinearSales sales, _) =>
// sales.sales.reduce((value, element) => value + element),
// // measureFn: (LinearSales sales, _) {
// // sales.month;
// // sales.affliations;
// // },
// data: data,
// ),
// ],
// animate: true,
// );
// return chart;
// }
// class LinearSales {
// final String month;
// final List<int> sales; // List of values for the y-axis
// // final int sales;
// // final int affliations;
// // LinearSales(this.month, this.sales, this.affliations);
// LinearSales(this.month, this.sales);
// }
import 'package:flutter/material.dart';
import 'package:charts_flutter/flutter.dart' as charts;
class Chart extends StatefulWidget {
const Chart({super.key});
@override
State<Chart> createState() => _ChartState();
}
class _ChartState extends State<Chart> {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
height: 200,
child: _buildChart(),
),
);
}
}
Widget _buildChart() {
final data = [
LinearSales("Jan12weas", [50, 100]),
LinearSales("Febghfctgfc", [75, 35]),
LinearSales("Mardfdxfdx", [50, 20]),
LinearSales("Apr", [25, 85]),
LinearSales("May", [10, 50]),
LinearSales("May1", [10, 50]),
];
final chart = charts.BarChart(
[
charts.Series<LinearSales, String>(
id: 'Sales',
colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault,
domainFn: (LinearSales sales, _) => sales.month,
measureFn: (LinearSales sales, _) =>
sales.sales.reduce((value, element) => value + element),
data: data,
fillColorFn: (LinearSales sales, _) {
print("Salesss");
print(sales.sales.length);
if (sales.sales.length == 2) {
print("hii");
print(sales.sales.indexOf(
sales.sales.reduce((value, element) => value + element)) ==
1);
print(sales.sales.indexOf(
sales.sales.reduce((value, element) => value + element)));
//print(sales.sales.indexOf(2));
// final maxIndex = sales.sales.indexOf(sales.sales
// .reduce((value, element) => value > element ? value : element));
// return sales.sales[0] == 50
// ? charts.MaterialPalette.red.shadeDefault.lighter
// : charts.MaterialPalette.green.shadeDefault.lighter;
final totalSales =
sales.sales.reduce((value, element) => value + element);
print("totalSales $totalSales");
print(sales.sales[0]);
int index = data.indexOf(sales);
print("totalSales $index");
return totalSales == sales.sales[0]
? charts.MaterialPalette.red.shadeDefault
: charts.MaterialPalette.green.shadeDefault;
} else {
// If there are more than two values, assign color based on the total sales
final totalSales =
sales.sales.reduce((value, element) => value + element);
return totalSales == sales.sales[0]
? charts.MaterialPalette.red.shadeDefault.lighter
: charts.MaterialPalette.green.shadeDefault.lighter;
}
},
),
],
animate: true,
);
return SizedBox(
height: 300, // Adjust the height as needed
child: chart,
);
}
class LinearSales {
final String month;
final List<int> sales;
LinearSales(this.month, this.sales);
}
// Widget _buildChart() {
// final data = [
// LinearSales("Jan", [50, 100]),
// LinearSales("Feb", [75, 35]),
// LinearSales("Mar", [50, 20]),
// LinearSales("Apr", [25, 85]),
// LinearSales("May", [10, 50]),
// ];
// final chart = charts.BarChart(
// [
// charts.Series<LinearSales, String>(
// id: 'Sales',
// colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault,
// domainFn: (LinearSales sales, _) => sales.month,
// measureFn: (LinearSales sales, _) =>
// sales.sales.reduce((value, element) => value + element),
// data: data,
// fillColorFn: (LinearSales sales, _) {
// final totalSales =
// sales.sales.reduce((value, element) => value + element);
// print("Salessss: ");
// print(sales.sales[0]);
// if (sales.sales.length > 1) {
// return totalSales == sales.sales[0]
// ? charts.MaterialPalette.red.shadeDefault.lighter
// : charts.MaterialPalette.red.shadeDefault.lighter;
// } else {
// return totalSales == sales.sales[0]
// ? charts.MaterialPalette.red.shadeDefault.lighter
// : charts.MaterialPalette.green.shadeDefault.lighter;
// }
// },
// ),
// ],
// animate: true,
// );
// return SizedBox(
// height: 300, // Adjust the height as needed
// child: chart,
// );
// }
// class LinearSales {
// final String month;
// final List<int> sales;
// LinearSales(this.month, this.sales);
// }
// Widget _buildChart() {
// final data = [
// LinearSales("Jan", [50, 100]),
// LinearSales("Feb", [75, 35]),
// LinearSales("Mar", [50, 20]),
// LinearSales("Apr", [25, 85]),
// LinearSales("May", [10, 50]),
// ];
// final chart = charts.BarChart(
// [
// charts.Series<LinearSales, String>(
// id: 'Sales',
// colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault,
// domainFn: (LinearSales sales, _) => sales.month,
// // measureFn: (LinearSales sales, _) => sales.sales,
// measureFn: (LinearSales sales, _) =>
// sales.sales.reduce((value, element) => value + element),
// // measureFn: (LinearSales sales, _) {
// // sales.month;
// // sales.affliations;
// // },
// data: data,
// ),
// ],
// animate: true,
// );
// return chart;
// }
// class LinearSales {
// final String month;
// final List<int> sales; // List of values for the y-axis
// // final int sales;
// // final int affliations;
// // LinearSales(this.month, this.sales, this.affliations);
// LinearSales(this.month, this.sales);
// }