Adding Bar Chart to Excel worksheet

9 Apr 20211 minute to read

A bar chart is a chart type that presents categorical data with rectangular bars with heights or lengths proportional to the data values in Excel worksheet.

The following code snippet illustrate how to add bar chart to Excel worksheet using Flutter XlsIO.

  • dart
  • // Create a new Excel document.
    final Workbook workbook = Workbook();
    
    // Accessing worksheet via index.
    final Worksheet sheet = workbook.worksheets[0];
    
    // Setting value in the cell.
    sheet.getRangeByName('A1').setText('Items');
    sheet.getRangeByName('B1').setText('Amount(in \$)');
    sheet.getRangeByName('C1').setText('Count');
    sheet.getRangeByName('A2').setText('Beverages');
    sheet.getRangeByName('A3').setText('Condiments');
    sheet.getRangeByName('A4').setText('Confections');
    sheet.getRangeByName('A5').setText('Dairy Products');
    sheet.getRangeByName('A6').setText('Grains / Cereals');
    sheet.getRangeByName('B2').setNumber(2776);
    sheet.getRangeByName('B3').setNumber(1077);
    sheet.getRangeByName('B4').setNumber(2287);
    sheet.getRangeByName('B5').setNumber(1368);
    sheet.getRangeByName('B6').setNumber(3325);
    sheet.getRangeByName('C2').setNumber(925);
    sheet.getRangeByName('C3').setNumber(378);
    sheet.getRangeByName('C4').setNumber(880);
    sheet.getRangeByName('C5').setNumber(581);
    sheet.getRangeByName('C6').setNumber(189);
    
    // Create an instances of chart collection.
    final ChartCollection charts = ChartCollection(sheet);
    
    // Add the chart.
    final Chart chart1 = charts.add();
    
    // Set Chart Type.
    chart1.chartType = ExcelChartType.bar;
    
    // Set data range in the worksheet.
    chart1.dataRange = sheet.getRangeByName('A1:C6');
    
    chart1.isSeriesInRows = false;
    
    // set charts to worksheet.
    sheet.charts = charts;
    
    // save and dispose the workbook.
    final List<int> bytes = workbook.saveAsStream();
    workbook.dispose();
    
    File('BarChart.xlsx').writeAsBytes(bytes);