Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to generate Dual Y-axis chart report in excel using NPOI 2.6.2 & C# #1191

Open
3 tasks
ghost opened this issue Sep 24, 2023 · 0 comments
Open
3 tasks

Comments

@ghost
Copy link

ghost commented Sep 24, 2023

2.6.2

XLSX

  • [ Yes] XLSX
  • [Yes ] XLS
  • DOCX
  • XLSM
  • OTHER

Use Case

Not able to generate Dual Y-axis chart report in excel using NPOI 2.6.2 & C#

Description

Am not able to generate chart report excel with Secondary Y-Axis below is the code I have already developed for Primary Y-axis but my requirement is to create secondary Y-axis as well which is am not able to achieve with NPOI. Could you please help me out achieving this requirement and also guide me how to achieve with below mentioned code.
using NPOI.SS.UserModel; using NPOI.SS.UserModel.Charts; using NPOI.XSSF.UserModel; using System.IO; using NPOI.SS.Util; using Newtonsoft.Json;
Trend-Template.xlsx
BasicLineChartReport.xlsx

private void GenerateSingleAxisReport()
{
try
{
IWorkbook workbook = new XSSFWorkbook();
ISheet sheet = workbook.CreateSheet("Profile");

    // Sample data for the chart
    // Replace jsonText with your actual JSON string
    string jsonText = File.ReadAllText("C:\\GIT\\WpfApp1\\WpfApp1\\file1.json");
    sheet.CreateRow(0).CreateCell(0).SetCellValue("Time");
    sheet.GetRow(0).CreateCell(1).SetCellValue("Feed W");
    sheet.GetRow(0).CreateCell(2).SetCellValue("V1 Level Fraction");
    sheet.GetRow(0).CreateCell(3).SetCellValue("T1 Temperature");
    sheet.GetRow(0).CreateCell(4).SetCellValue("T1 Vapor");
    List<DataRecord> dataRecords = JsonConvert.DeserializeObject<List<DataRecord>>(jsonText);

    // Now you can access the data
    int i = 1;
    foreach (var record in dataRecords)
    {
        sheet.CreateRow(i).CreateCell(0).SetCellValue(record.Time);
        sheet.GetRow(i).CreateCell(1).SetCellValue(record.FeedW);
        sheet.GetRow(i).CreateCell(2).SetCellValue(record.V1LevelFraction);
        sheet.GetRow(i).CreateCell(3).SetCellValue(record.T1Temperature);
        sheet.GetRow(i).CreateCell(4).SetCellValue(record.T1Vapor);
        i++;
    }
    IDrawing patriarch = sheet.CreateDrawingPatriarch();
    IClientAnchor anchor = patriarch.CreateAnchor(0, 0, 0, 0, 6, 0, 15, 15);

    IChart chart = patriarch.CreateChart(anchor);
    // Add Title to Chart
    chart.SetTitle("Combined Trend Chart");  // Set the title here
    IChartLegend legend = chart.GetOrCreateLegend();
    legend.Position = LegendPosition.Bottom;

    // Scatter Chart
    IScatterChartData<double, double> data = chart.ChartDataFactory.CreateScatterChartData<double, double>();

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

    // Series 1
    IChartDataSource<double> xs1 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(0, 60, 0, 0));
    IChartDataSource<double> ys1 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(0, 60, 1, 1));
    IScatterChartSeries<double, double> series1 = data.AddSeries(xs1, ys1);
    series1.SetTitle("V1 Level Fraction");

    // Series 2
    IChartDataSource<double> xs2 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(0, 60, 0, 0));
    IChartDataSource<double> ys2 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(0, 60, 2, 2));
    IScatterChartSeries<double, double> series2 = data.AddSeries(xs2, ys2);
    series2.SetTitle("T1 Temperature C");

    // Series 3
    IChartDataSource<double> xs3 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(0, 60, 0, 0));
    IChartDataSource<double> ys3 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(0, 60, 3, 3));
    IScatterChartSeries<double, double> series3 = data.AddSeries(xs3, ys3);
    series3.SetTitle("T1 Vapor");

    // Series 4
    IChartDataSource<double> xs4 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(0, 60, 0, 0));
    IChartDataSource<double> ys4 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(0, 60, 4, 4));
    IScatterChartSeries<double, double> series4 = data.AddSeries(xs4, ys4);
    series4.SetTitle("Feed W");


    chart.Plot(data, bottomAxis, leftAxis);
    for (int m = 0; m <= 20; m++)
    {
        sheet.AutoSizeColumn(m);
    }
    
    using (FileStream stream = new FileStream("BasicLineChartReport.xlsx", FileMode.Create, FileAccess.Write))
    {
        workbook.Write(stream);
    }

    MessageBox.Show("Excel report generated successfully.");
}
catch (Exception ex)
{
    MessageBox.Show($"Error generating Excel chart report: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}

}
BasicLineChartReport.xlsx

@ghost ghost added the enhancement label Sep 24, 2023
@tonyqus tonyqus added the chart label Apr 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant