3

I am stuck on an issue with an EPPlus (4.0.1). My bar graph is plotting horizontally but I would like it to be vertical. I am trying to change the "chart.Direction" property, but I am given an error because it is read-only. I also do not see a way to set the variable in the constructor for my 'ExcelBarChart' variable. Sorry if this is a basic question, I am reading the documentation but cannot figure it out. chart.Direction might not even be the correct property for all I know. I am programming in C#. Thanks in advance for any answers.

OfficeOpenXml.Drawing.Chart.ExcelBarChart chart = (OfficeOpenXml.Drawing.Chart.ExcelBarChart)ws.Drawings.AddChart("barChart", OfficeOpenXml.Drawing.Chart.eChartType.BarClustered);
chart.SetSize(widthPx, heightPx);
chart.SetPosition(startTopPx, startLeftPx);
chart.Title.Text = "Clustered Bar Graph Report";
chart.Direction = eDirection.Column; // error: Property or indexer 'OfficeOpenXml.Drawing.Chart.ExcelBarChart.Direction' cannot be assigned to -- it is read only

ws.Cells["A1"].LoadFromDataTable(data, true); // load dataTable into Cells
int fromRow = 2;
int toRow = 2;
int fromCol = 2;
int toCol = 5;

int xRow = 2;
int xCol = 1;
chart.Series.Add(ExcelRange.GetAddress(fromRow, fromCol, toRow, toCol), 
                                         ExcelRange.GetAddress(xRow, xCol));
Corey Adler
  • 15,897
  • 18
  • 66
  • 80
jnel899
  • 563
  • 2
  • 8
  • 21

1 Answers1

7

I think you want to use eChartType.ColumnClustered instead of eChartType.BarClustered since Direction is meant to be set at construction (took a peak at the source code). Like this:

[TestMethod]
public void Vertical_Bar_Chart()
{
    var existingFile = new FileInfo(@"c:\temp\temp.xlsx");
    if (existingFile.Exists)
        existingFile.Delete();

    using (var package = new ExcelPackage(existingFile))
    {
        var workbook = package.Workbook;
        var ws = workbook.Worksheets.Add("newsheet");

        //Some data
        ws.Cells["A12"].Value = "wer";
        ws.Cells["A13"].Value = "sdf";
        ws.Cells["A14"].Value = "wer";
        ws.Cells["A15"].Value = "ghgh";

        ws.Cells["B12"].Value = 53;
        ws.Cells["B13"].Value = 36;
        ws.Cells["B14"].Value = 43;
        ws.Cells["B15"].Value = 86;

        //Create the chart
        var chart = (ExcelBarChart)ws.Drawings.AddChart("barChart", eChartType.ColumnClustered);
        chart.SetSize(300 ,300);
        chart.SetPosition(10,10);
        chart.Title.Text = "Clustered Bar Graph Report";
        //chart.Direction = eDirection.Column; // error: Property or indexer 'OfficeOpenXml.Drawing.Chart.ExcelBarChart.Direction' cannot be assigned to -- it is read only

        chart.Series.Add(ExcelRange.GetAddress(12, 2, 15, 2), ExcelRange.GetAddress(12, 1, 15, 1));

        package.Save();

    }
}
Ernie S
  • 13,902
  • 4
  • 52
  • 79
  • I knew there was something simple I was missing. Thanks! – jnel899 Jan 07 '15 at 17:44
  • hi,this work for me ,can you please explain this line chart.Series.Add(ExcelRange.GetAddress(12, 2, 15, 2), ExcelRange.GetAddress(12, 1, 15, 1)); i got first GetAddress GetAddress(fromRow, fromCol, toRow, toCol) bt what is the second one – Sreepathy Sp Jun 22 '16 at 05:30
  • 1
    @SreepathySp `GetAddress` simply converts the numeric range to its string representation. So, if you were to debug that line you would see it calling `chart.Series.Add("B12:B15", "A12:A15")` where the first string is the Y Axis for the chart and the Second string is the X Axis. Another way you can do it is to reference the worksheet cells directly like `chart.Series.Add(worksheet.Cells[12, 2, 15, 2], worksheet.Cells[12, 1, 15, 1])`. – Ernie S Jun 22 '16 at 12:17
  • thnks ernie ,1 more help ,how to change bar color ,because i have to 2 tabs and 2 different bar chart in order to distinguish both of them – Sreepathy Sp Jun 22 '16 at 12:20
  • @SreepathySp Thats a bit more involved. See if this helps: http://stackoverflow.com/questions/36520427/legend-color-is-incorrect-in-excel-chart-created-using-epplus/36532733#36532733 – Ernie S Jun 22 '16 at 12:29