How can I display the last data label of a series within a line chart in .net with Epplus?

I have a line chart in an Excel document. I need to display the last data label of a data series, my code looks like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>using OfficeOpenXml;
using OfficeOpenXml.Drawing.Chart;
class Program
{
static void Main()
{
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
var stream = new MemoryStream();
using (var package = new ExcelPackage())
{
var worksheet = package.Workbook.Worksheets.Add("Sheet1");
// Add headers for the data
worksheet.Cells["A1"].Value = "Date";
worksheet.Cells["B1"].Value = "Value";
// Define the start and end dates
DateTime startDate = new DateTime(2023, 1, 1);
DateTime endDate = new DateTime(2024, 1, 1);
// Add records for each day between the start and end dates
int row = 2;
for (DateTime date = startDate; date <= endDate; date = date.AddDays(1))
{
var r = new Random();
worksheet.Cells[$"A{row}"].Value = date; // Date
worksheet.Cells[$"A{row}"].Style.Numberformat.Format = "dd-mmm-yy";
worksheet.Cells[$"B{row}"].Value = r.NextDouble(); // Value
row++;
}
// Add a line chart
var chart = worksheet.Drawings.AddChart("lineChart", eChartType.Line) as ExcelLineChart;
chart.Title.Text = "Sample Line Chart";
chart.SetPosition(1, 0, 3, 0);
chart.SetSize(1200, 600);
// Add the main series to the chart
var series = chart.Series.Add(worksheet.Cells[$"B2:B{row - 1}"], worksheet.Cells[$"A2:A{row - 1}"]);
series.Header = "Series 1";
// Displays the series DataLabels.
series.DataLabel.ShowValue = true;
series.DataLabel.Font.Size = 12;
series.DataLabel.Position = eLabelPosition.Top;
// Save the package
var filePath = Path.Combine(Environment.CurrentDirectory, "SampleData.xlsx");
var fi = new FileInfo(filePath);
package.SaveAs(fi);
Console.WriteLine($"Data and chart created successfully at: {filePath}");
}
}
}
</code>
<code>using OfficeOpenXml; using OfficeOpenXml.Drawing.Chart; class Program { static void Main() { ExcelPackage.LicenseContext = LicenseContext.NonCommercial; var stream = new MemoryStream(); using (var package = new ExcelPackage()) { var worksheet = package.Workbook.Worksheets.Add("Sheet1"); // Add headers for the data worksheet.Cells["A1"].Value = "Date"; worksheet.Cells["B1"].Value = "Value"; // Define the start and end dates DateTime startDate = new DateTime(2023, 1, 1); DateTime endDate = new DateTime(2024, 1, 1); // Add records for each day between the start and end dates int row = 2; for (DateTime date = startDate; date <= endDate; date = date.AddDays(1)) { var r = new Random(); worksheet.Cells[$"A{row}"].Value = date; // Date worksheet.Cells[$"A{row}"].Style.Numberformat.Format = "dd-mmm-yy"; worksheet.Cells[$"B{row}"].Value = r.NextDouble(); // Value row++; } // Add a line chart var chart = worksheet.Drawings.AddChart("lineChart", eChartType.Line) as ExcelLineChart; chart.Title.Text = "Sample Line Chart"; chart.SetPosition(1, 0, 3, 0); chart.SetSize(1200, 600); // Add the main series to the chart var series = chart.Series.Add(worksheet.Cells[$"B2:B{row - 1}"], worksheet.Cells[$"A2:A{row - 1}"]); series.Header = "Series 1"; // Displays the series DataLabels. series.DataLabel.ShowValue = true; series.DataLabel.Font.Size = 12; series.DataLabel.Position = eLabelPosition.Top; // Save the package var filePath = Path.Combine(Environment.CurrentDirectory, "SampleData.xlsx"); var fi = new FileInfo(filePath); package.SaveAs(fi); Console.WriteLine($"Data and chart created successfully at: {filePath}"); } } } </code>
using OfficeOpenXml;
using OfficeOpenXml.Drawing.Chart;

class Program
{
    static void Main()
    {
        ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
        var stream = new MemoryStream();

        using (var package = new ExcelPackage())
        {
            var worksheet = package.Workbook.Worksheets.Add("Sheet1");

            // Add headers for the data
            worksheet.Cells["A1"].Value = "Date";
            worksheet.Cells["B1"].Value = "Value";

            // Define the start and end dates
            DateTime startDate = new DateTime(2023, 1, 1);
            DateTime endDate = new DateTime(2024, 1, 1);

            // Add records for each day between the start and end dates
            int row = 2;
            for (DateTime date = startDate; date <= endDate; date = date.AddDays(1))
            {
                var r = new Random();
                worksheet.Cells[$"A{row}"].Value = date; // Date
                worksheet.Cells[$"A{row}"].Style.Numberformat.Format = "dd-mmm-yy";

                worksheet.Cells[$"B{row}"].Value = r.NextDouble(); // Value
                row++;
            }

            // Add a line chart
            var chart = worksheet.Drawings.AddChart("lineChart", eChartType.Line) as ExcelLineChart;
            chart.Title.Text = "Sample Line Chart";
            chart.SetPosition(1, 0, 3, 0);
            chart.SetSize(1200, 600);

            // Add the main series to the chart
            var series = chart.Series.Add(worksheet.Cells[$"B2:B{row - 1}"], worksheet.Cells[$"A2:A{row - 1}"]);
            series.Header = "Series 1";

            // Displays the series DataLabels.
            series.DataLabel.ShowValue = true;
            series.DataLabel.Font.Size = 12;
            series.DataLabel.Position = eLabelPosition.Top;

            // Save the package
            var filePath = Path.Combine(Environment.CurrentDirectory, "SampleData.xlsx");
            var fi = new FileInfo(filePath);
            package.SaveAs(fi);

            Console.WriteLine($"Data and chart created successfully at: {filePath}");
        }
    }
}

Result of this code:

I need to show only the last value in the series.

I found an example applied in VB.

New contributor

jormope271297 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật