Search code examples
c#excelchartslinechartnpoi

C# NPOI draw line chart to Excel when cell value on range 0.001 to 9.999 (fractional numbers)


C# NPOI draw line chart to Excel when cell value on range 0.001 to 9.999 (fractional numbers)

I can draw chart when cell value is similar (0, 1, 2, ... 9) or other integer value but when i try use (0,293 or other fractional numbers) i saw error:

"Removed part: /xl/drawings/drawing1.xml part. (Drawing figure)"
(when open Excel file).

I tried searched solution in google and github but dont found simular situation. Maybe some one faced with this issue.

Thanks for any help.

Updated added report files Good report Repot with error message

**Added code for explanation issues:**

class Program
{
    const int NUM_OF_ROWS = 3;
    const int NUM_OF_COLUMNS = 10;

      static void CreateChart(IDrawing drawing, ISheet sheet, IClientAnchor anchor)
    {
        IChart chart = drawing.CreateChart(anchor);
        IChartLegend legend = chart.GetOrCreateLegend();
        legend.Position = LegendPosition.TopRight;

        ILineChartData<double, double> data = chart.ChartDataFactory.CreateLineChartData<double, double>();

        // Use a category axis for the bottom axis.
        IChartAxis bottomAxis = chart.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom);
        IValueAxis leftAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left);

        IChartDataSource<double> xs = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
        IChartDataSource<double> ys1 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));

        data.AddSeries(xs, ys1);

        chart.Plot(data, bottomAxis, leftAxis);
    }

    static void Main(string[] args)
    {
        IWorkbook wb = new XSSFWorkbook();
        ISheet sheet = wb.CreateSheet("linechart");

        // Create a row and put some cells in it. Rows are 0 based.
        IRow row;
        ICell cell;
        for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++)
        {
            row = sheet.CreateRow((short)rowIndex);
            for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++)
            {
                cell = row.CreateCell((short)colIndex);

                //This generate graph
                //cell.SetCellValue(colIndex * (rowIndex + 1));

                //This make error when open Excel file
                cell.SetCellValue(colIndex * (rowIndex + 1) + 0.1);
            }
        }

        IDrawing drawing = sheet.CreateDrawingPatriarch();
        IClientAnchor anchor1 = drawing.CreateAnchor(0, 0, 0, 0, 0, 5, 10, 8);
        CreateChart(drawing, sheet, anchor1);
        //Write to Excel file
        using (FileStream fs =File.Create("test1.xlsx"))
        {
            wb.Write(fs);
        }
    }
}

Solution

  • I found solution - not perfect but it is works.

    1. Set cell type to string.
    2. Draw chart (with default value(0) becouse we use string cell type for draw)
    3. Update cell type to numeric then chart will auto refresh with numeric data.