Search code examples
c#.net.net-corenpoi

NPOI Bar/Column Chart?


I need to create a simple bar chart on excel and export it. After searching examples on NPOI's repo and through code, I saw that is just possible to create line and scatter chart.

I'm considering to create a excel template with chart already created and so modify their axis.

What I need is just a confirmation that is not possible to create a bar/column chart with NPOI.

Thanks in advance!


Solution

  • Ok, after looking into NPOI github issues I found that is possible. But it has some limitations.

    I can't set decimals on chart, but after some investigations on Java thread about data format, I was able to resolve just changing the current thread culture.

    Below the code example:

    private void CreateChart(int columnQtt, int rowIndex, int tipoId, string currentTipo, ISheet sheet, int startDataRow)
    {
        XSSFDrawing drawing = (XSSFDrawing)sheet.CreateDrawingPatriarch();
        XSSFClientAnchor anchor = (XSSFClientAnchor)drawing.CreateAnchor(0, 0, 0, 0, columnQtt + 1, startDataRow - 2, columnQtt + 10, startDataRow + 12);
        XSSFChart chart = (XSSFChart)drawing.CreateChart(anchor);
    
        IBarChartData<string, double> barChartData = chart.ChartDataFactory.CreateBarChartData<string, double>();
        IChartLegend legend = chart.GetOrCreateLegend();
        legend.Position = LegendPosition.Bottom;
    
        IChartAxis bottomAxis = chart.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom);
        bottomAxis.MajorTickMark = AxisTickMark.None;
        IValueAxis leftAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left);
        leftAxis.Crosses = AxisCrosses.AutoZero;
        leftAxis.SetCrossBetween(AxisCrossBetween.Between);
    
        int endDataRow = rowIndex - 1;
    
        IChartDataSource<string> categoryAxis = DataSources.FromStringCellRange(sheet, new CellRangeAddress(startDataRow, endDataRow, 0, 0));
        IChartDataSource<double> valueAxis = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(startDataRow, endDataRow, columnQtt - 1, columnQtt - 1));
        var serie = barChartData.AddSeries(categoryAxis, valueAxis);
        serie.SetTitle(currentTipo);    
    
        chart.Plot(barChartData, bottomAxis, leftAxis);
    }