-1

enter image description here

I tried the Stacked column graph of the Syncfusion plugin but not worked this way. Resulted Stacked bar graph is:-

enter image description here

Please help me to fix this. Thank You.

Sumit Tifane
  • 379
  • 1
  • 3
  • 7

1 Answers1

4

You can use the popular flutter_echarts package to draw a stacked chart, simply by passing the option to the Echarts widget (Read how to modify the properties of the chart here). Sample code below:


import 'package:flutter/material.dart';
import 'package:flutter_echarts/flutter_echarts.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      home: SomeScreen(),
    );
  }
}

class SomeScreen extends StatefulWidget {
  @override
  _SomeScreenState createState() => _SomeScreenState();
}

class _SomeScreenState extends State<SomeScreen> {
  getOption(data) {
    var option = '''{
    legend: {
      data: // Use your data here
    },
    grid: {left: '3%', right: '4%', bottom: '3%', containLabel: true},
    xAxis: [
      {
        type: 'category',
        data: // Use your data here,
      }
    ],
    yAxis: [
      {type: 'value'}
    ],
    series: [
      {
        name: // Use your data here,
        type: 'bar',
        stack: 'test',
        emphasis: {focus: 'series'},
        data: // Use your data here
      },
      {
        name: // Use your data here,
        type: 'bar',
        stack: 'test',
        emphasis: {focus: 'series'},
        data: // Use your data here
      },
      {
        name: // Use your data here,
        type: 'bar',
        stack: 'test',
        emphasis: {focus: 'series'},
        data: // Use your data here
      },
      {
        name: // Use your data here,
        type: 'bar',
        stack: 'test',
        emphasis: {focus: 'series'},
        data: // Use your data here
      },
      {
        name: // Use your data here,
        type: 'bar',
        stack: 'test',
        emphasis: {focus: 'series'},
        data: // Use your data here
      },
    ]
  }''';
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Container(
          color: Colors.white,
          width: 400,
          height: 500,
          child: FutureBuilder(
              future: _callAPI(), // Make a call to your API
              builder: (context, snapshot) {
                if (!snapshot.hasData) return Container();
                var data = snapshot.data;
                return Echarts(
                  option: getOption(data),
                );
              }
          ),
        ),
      ),
    );
  }
}

Result:
enter image description here

Bach
  • 2,928
  • 1
  • 6
  • 16
  • Good Solution, but I want to feed the data from API and this gets only fix value. Thanks for the help @Bach – Sumit Tifane Mar 27 '21 at 06:00
  • 1
    @SumitTifane I use fix data for this example. Try fetching the data then use them to create the `option` string, then you can display it as usual – Bach Mar 29 '21 at 02:35
  • 1
    @SumitTifane Just updated my answer to help you get the idea better. Simply passing your data from API in to create the `option` – Bach Mar 29 '21 at 02:43
  • @Bach I also stuck same problem can you explain using JSON API, Thanks in advanced – Ravindra S. Patil Apr 20 '22 at 11:07