3

I have an ExcelBarChart that I am creating using EPPlus.

I'm trying to change the position of the data label to Inside Base, but when I look inside the ExcelBarChart object I see an ExcelChartDataLabel property, and inside that a property named eLabelPosition.

eLabelPosition is protected however and inaccessible.

What is the proper way to set the data label position using EPPlus?

Please see the relevant code below:

var chart = (ExcelBarChart)chartWorksheet.Drawings.AddChart("changesVisualized", eChartType.ColumnClustered);
chart.SetSize(1000, 500);
chart.SetPosition(0,0);
chart.Title.Text = row.Name + "Volume " + date1.ToString("MM/dd/yyyy") + " - " + date2.ToString("MM/dd/yyyy");
chart.DataLabel.ShowValue = true;

var thisYearSeries = (ExcelChartSerie)(chart.Series.Add(worksheet.Cells["B4,D4,F4,H4,J4"], worksheet.Cells["B3,D3,F3,H3,J3"]));
thisYearSeries.Header = "This Year's Volume";                   

var lastYearSeries = (ExcelChartSerie)(chart.Series.Add(chartWorksheet.Cells["A1,B1,C1,D1,E1"], worksheet.Cells["B3,D3,F3,H3,J3"]));
lastYearSeries.Header = "Last Year's Volume";
Adam
  • 1,483
  • 4
  • 21
  • 50

1 Answers1

6

You can do something like this:

var barChart = worksheet.Drawings.AddChart("Chart1", eChartType.ColumnClustered);
barChart.SetPosition(data.Count + 1, 0, 0, 0);
barChart.Title.Text = "Test Chart";
barChart.Title.Font.Bold = true;
barChart.Title.Font.Size = 12;

var serie = barChart.Series.Add(worksheet.Cells[2, 2, data.Count + 1, 2], worksheet.Cells[2, 1, data.Count + 1, 1]);
var barSeries = (ExcelBarChartSerie)serie;
barSeries.DataLabel.Font.Bold = true;
barSeries.DataLabel.ShowValue = true;
barSeries.DataLabel.ShowPercent = true;
barSeries.DataLabel.ShowLeaderLines = true;
barSeries.DataLabel.Separator = ";";
barSeries.DataLabel.Position = eLabelPosition.InBase;

Adapted from my example here:

How do I modify a chart series using EPPLus?

Community
  • 1
  • 1
Ernie S
  • 13,902
  • 4
  • 52
  • 79
  • Hi @Ernie, I modified my code to show you exactly what I am working with. I'm using the `ExcelChartSerie` object which doesn't have a DataLabel property. – Adam Jun 21 '16 at 12:18
  • @Adam I see what you mean now. See my edits. You should be able to just cast to a `ExcelBarChartSerie` instead. – Ernie S Jun 21 '16 at 12:55
  • I might be using the wrong version of EPPlus. It doesn't look like I have `ExcelBartChartSerie`. I'm using version 3.1.3.0, runtime version 2.0.50727 – Adam Jun 21 '16 at 14:52
  • @Adam Ahh...yes, it was not around in v3. If you are able to I strongly recommend you upgrade to 4. Not just to solve you immediate problem but also because they make some significant changes to the cell store to use much less RAM (among many other things). If you cannot, you will have to do you own XML manipulation to the chart object. – Ernie S Jun 21 '16 at 15:56