Skip to content

Commit fb2127a

Browse files
committed
FLUT-944416-[others]: Updated code snippet
1 parent 79c2b18 commit fb2127a

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

lib/main.dart

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ class _CoalConsumptionChartState extends State<CoalConsumptionChart> {
165165
return SfCartesianChart(
166166
plotAreaBorderWidth: 0,
167167
title: ChartTitle(
168-
text: 'Global Coal Consumption Trends',
168+
text: 'Visualize Global Coal Consumption Trends',
169169
textStyle: TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
170170
primaryXAxis: _createCategoryAxis(),
171171
primaryYAxis: _createNumericAxis(),
@@ -275,8 +275,8 @@ class _CoalConsumptionChartState extends State<CoalConsumptionChart> {
275275
yValueMapper: regionDataMappers[index],
276276
onCreateRenderer: (ChartSeries<dynamic, dynamic> series) =>
277277
StackedAreaRendererExtension(
278-
series as StackedAreaSeries<CoalConsumptionData, String>,
279-
index),
278+
series as StackedAreaSeries<CoalConsumptionData, String>,
279+
),
280280
);
281281
});
282282
}
@@ -302,27 +302,20 @@ class ChartData {
302302

303303
class StackedAreaRendererExtension<T, D>
304304
extends StackedAreaSeriesRenderer<T, D> {
305-
final int seriesIndex;
306305
static final Map<int, List<Offset>> bottomBoundary = {};
307306

308-
StackedAreaRendererExtension(StackedAreaSeries<T, D> series, this.seriesIndex)
309-
: super();
307+
StackedAreaRendererExtension(StackedAreaSeries<T, D> series) : super();
310308

311309
@override
312310
StackedAreaSegment<T, D> createSegment() {
313-
return StackedAreaSegmentExtension<T, D>(seriesIndex);
311+
return StackedAreaSegmentExtension<T, D>();
314312
}
315313
}
316314

317315
class StackedAreaSegmentExtension<T, D> extends StackedAreaSegment<T, D> {
318-
final int seriesIndex;
319316
ui.Image? backgroundImage;
320317
static final Map<int, ui.Image> _loadedImages = {};
321318

322-
StackedAreaSegmentExtension(this.seriesIndex) {
323-
_loadImageForSeries();
324-
}
325-
326319
final List<Color> overlayColor = [
327320
Colors.yellow.withValues(alpha: 0.5),
328321
Colors.green.withValues(alpha: 0.5),
@@ -337,25 +330,34 @@ class StackedAreaSegmentExtension<T, D> extends StackedAreaSegment<T, D> {
337330
'assets/coal3.jpg',
338331
];
339332

340-
void _loadImageForSeries() async {
341-
if (_loadedImages.containsKey(seriesIndex)) {
342-
backgroundImage = _loadedImages[seriesIndex];
333+
Future<void> _loadImageForSeries() async {
334+
if (_loadedImages.containsKey(series.index)) {
335+
backgroundImage = _loadedImages[series.index];
343336
} else {
344-
//Load image based on seriesIndex.
345-
final ByteData data = await rootBundle.load(images[seriesIndex]);
346-
final codec = await ui.instantiateImageCodec(data.buffer.asUint8List());
347-
final frame = await codec.getNextFrame();
348-
backgroundImage = frame.image;
349-
_loadedImages[seriesIndex] = backgroundImage!;
337+
try {
338+
final ByteData data = await rootBundle.load(images[series.index]);
339+
final codec = await ui.instantiateImageCodec(data.buffer.asUint8List());
340+
final frame = await codec.getNextFrame();
341+
backgroundImage = frame.image;
342+
_loadedImages[series.index] = backgroundImage!;
343+
} catch (e) {
344+
debugPrint("Image loading error: $e");
345+
}
350346
}
351347
}
352348

353349
@override
354350
void onPaint(Canvas canvas) {
355-
if (points.isEmpty || backgroundImage == null) return;
351+
if (points.isEmpty) return;
352+
353+
// Ensure the image is loaded before painting
354+
if (backgroundImage == null) {
355+
_loadImageForSeries();
356+
return;
357+
}
356358

357359
Path segmentPath = _generateSegmentPath(canvas);
358-
_storeBottomBoundaryPoints();
360+
_buildBottomBoundaryPoints();
359361

360362
// Paint for the image background
361363
Paint fillPaint = Paint()
@@ -365,14 +367,14 @@ class StackedAreaSegmentExtension<T, D> extends StackedAreaSegment<T, D> {
365367
backgroundImage!,
366368
TileMode.repeated,
367369
TileMode.repeated,
368-
Matrix4.diagonal3Values(1, 1, 1).storage,
370+
Matrix4.identity().storage,
369371
);
370372

371373
canvas.drawPath(segmentPath, fillPaint);
372374

373375
// Overlay color
374376
final overlayPaint = Paint()
375-
..color = overlayColor[seriesIndex]
377+
..color = overlayColor[series.index]
376378
..style = PaintingStyle.fill;
377379
canvas.drawPath(segmentPath, overlayPaint);
378380

@@ -391,8 +393,8 @@ class StackedAreaSegmentExtension<T, D> extends StackedAreaSegment<T, D> {
391393
}
392394

393395
List<Offset> bottomPoints =
394-
StackedAreaRendererExtension.bottomBoundary[seriesIndex - 1] ?? [];
395-
if (seriesIndex == 0 || bottomPoints.isEmpty) {
396+
StackedAreaRendererExtension.bottomBoundary[series.index - 1] ?? [];
397+
if (series.index == 0 || bottomPoints.isEmpty) {
396398
double chartBottomY = canvas.getLocalClipBounds().bottom;
397399
for (int i = points.length - 1; i >= 0; i--) {
398400
path.lineTo(points[i].dx, chartBottomY);
@@ -405,8 +407,8 @@ class StackedAreaSegmentExtension<T, D> extends StackedAreaSegment<T, D> {
405407
return path..close();
406408
}
407409

408-
void _storeBottomBoundaryPoints() {
409-
StackedAreaRendererExtension.bottomBoundary[seriesIndex] =
410+
void _buildBottomBoundaryPoints() {
411+
StackedAreaRendererExtension.bottomBoundary[series.index] =
410412
List.from(points);
411413
}
412414
}

0 commit comments

Comments
 (0)