Skip to content

Commit dc7428e

Browse files
committed
1 parent 2ba93f2 commit dc7428e

File tree

1 file changed

+50
-45
lines changed

1 file changed

+50
-45
lines changed

lib/main.dart

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ class ChartApp extends StatelessWidget {
1111
Widget build(BuildContext context) {
1212
return MaterialApp(
1313
title: 'Chart Demo',
14-
theme: ThemeData(
15-
primarySwatch: Colors.blue
16-
),
14+
theme: ThemeData(primarySwatch: Colors.blue),
1715
home: MyHomePage(),
1816
);
1917
}
@@ -28,6 +26,14 @@ class MyHomePage extends StatefulWidget {
2826
}
2927

3028
class _MyHomePageState extends State<MyHomePage> {
29+
TooltipBehavior _tooltipBehavior;
30+
31+
@override
32+
void initState() {
33+
_tooltipBehavior = TooltipBehavior(enable: true);
34+
super.initState();
35+
}
36+
3137
@override
3238
Widget build(BuildContext context) {
3339
return Scaffold(
@@ -39,53 +45,52 @@ class _MyHomePageState extends State<MyHomePage> {
3945
height: 500,
4046
width: 320,
4147
child: SfCartesianChart(
42-
backgroundColor: Colors.white,
43-
plotAreaBackgroundColor: Colors.white,
44-
onTooltipRender: (TooltipArgs args) {
45-
if (args.pointIndex == 0) {
46-
//Tooltip without header
47-
args.header = '';
48-
}
49-
if (args.pointIndex == 1) {
50-
//Tooltip with customized header
51-
args.header = 'Sold';
52-
}
53-
if (args.pointIndex == 2) {
54-
//Tooltip with X and Y positions of data points
55-
args.header = 'x : y';
56-
args.text = '${args.locationX.floor()} : ${args.locationY.floor()}';
57-
}
58-
if(args.pointIndex == 3) {
59-
//Tooltip with formatted DateTime values
60-
List<CartesianChartPoint<dynamic>> chartdata = args.dataPoints;
61-
args.header = DateFormat('d MMM yyyy').format(chartdata[3].x);
62-
args.text = '${chartdata[3].y}';
63-
}
64-
},
48+
backgroundColor: Colors.white,
49+
plotAreaBackgroundColor: Colors.white,
50+
onTooltipRender: (TooltipArgs args) {
51+
if (args.pointIndex == 0) {
52+
//Tooltip without header
53+
args.header = '';
54+
}
55+
if (args.pointIndex == 1) {
56+
//Tooltip with customized header
57+
args.header = 'Sold';
58+
}
59+
if (args.pointIndex == 2) {
60+
//Tooltip with X and Y positions of data points
61+
args.header = 'x : y';
62+
args.text =
63+
'${args.locationX.floor()} : ${args.locationY.floor()}';
64+
}
65+
if (args.pointIndex == 3) {
66+
//Tooltip with formatted DateTime values
67+
List<CartesianChartPoint<dynamic>> chartdata =
68+
args.dataPoints;
69+
args.header =
70+
DateFormat('d MMM yyyy').format(chartdata[3].x);
71+
args.text = '${chartdata[3].y}';
72+
}
73+
},
6574
primaryXAxis: DateTimeAxis(
66-
interval: 30,
67-
intervalType: DateTimeIntervalType.days
68-
),
75+
interval: 30, intervalType: DateTimeIntervalType.days),
6976
// Enable tooltip
70-
tooltipBehavior: TooltipBehavior(enable: true),
77+
tooltipBehavior: _tooltipBehavior,
7178
series: <ChartSeries<SalesData, DateTime>>[
7279
LineSeries<SalesData, DateTime>(
73-
enableTooltip: true,
74-
dataSource: <SalesData>[
75-
SalesData(DateTime(2020, 01, 31), 35),
76-
SalesData(DateTime(2020, 02, 28), 28),
77-
SalesData(DateTime(2020, 03, 31), 34),
78-
SalesData(DateTime(2020, 04, 30), 32),
79-
SalesData(DateTime(2020, 05, 31), 40)
80-
],
81-
xValueMapper: (SalesData sales, _) => sales.date,
82-
yValueMapper: (SalesData sales, _) => sales.sales,
80+
enableTooltip: true,
81+
dataSource: <SalesData>[
82+
SalesData(DateTime(2020, 01, 31), 35),
83+
SalesData(DateTime(2020, 02, 28), 28),
84+
SalesData(DateTime(2020, 03, 31), 34),
85+
SalesData(DateTime(2020, 04, 30), 32),
86+
SalesData(DateTime(2020, 05, 31), 40)
87+
],
88+
xValueMapper: (SalesData sales, _) => sales.date,
89+
yValueMapper: (SalesData sales, _) => sales.sales,
8390
)
84-
]
85-
),
91+
]),
8692
),
87-
)
88-
);
93+
));
8994
}
9095
}
9196

@@ -94,4 +99,4 @@ class SalesData {
9499

95100
final DateTime date;
96101
final double sales;
97-
}
102+
}

0 commit comments

Comments
 (0)