Janardhan G Janardhan G - 3 years ago 330
C# Question

epplus charts without gridlines in c# ( Its a web Application)

Graph Without GridLinesI am trying to create a chart in excel file using epplus and c#.
I am able to create graph , I am trying to remove the gridlines in graph as shown in the images . I found solution to do it in windows application , but i wanted to do it in web application , below is the code i use to generate graph.

ExcelRange erLossesRangeMacInv = worksheet.Cells["G5:G10"];
var chartOEE=(ExcelBarChart)worksheetGraph.Drawings.AddChart("barChartOEE", eChartType.ColumnClustered);
chartOEE.SetSize(300, 300);//Setting size of graph
chartOEE.SetPosition(10, 10); // position of graph in excel
chartOEE.Title.Text = "OEE";
ExcelRange erLossesRangeOEE = worksheet.Cells["M5:M10"];
chartOEE.Style = eChartStyle.Style10;
chartOEE.Legend.Remove();
chartOEE.DataLabel.ShowValue = true;
chartOEE.YAxis.MaxValue = 100;
chartOEE.Series.Add(erLossesRangeOEE, erLossesRangeMacInv);


I have found code that does work in windows forms.

chartOEE.ChartAreas["ChartArea1"].AxisX.MajorGrid.Enabled = false;


How to do it in "Web Application" using epplus and c#.

Answer Source

UPDATE

I created a PR which has been accepted that allow removing grid lines. Should will be in the next release (whenever that is) so look out for it:

https://epplus.codeplex.com/SourceControl/network/forks/aesalazar/Epplus/contribution/9025

https://github.com/JanKallman/EPPlus/blob/master/EPPlus/Drawing/Chart/ExcelChartAxis.cs#L895

Which can be used like this:

axis.RemoveGridlines(); //Removes major and minor
axis.RemoveGridlines(true, true); //Can choose major or minor with bools.

ORIGINAL

Its strange that they have not added that as an option to EPPlus since it only requires basic xml manipulation. Web or desktop should make no difference as far as EPPlus goes, you simply need to remove the xml references. This should do it:

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

    //datatable is just some made up Table object
    worksheet.Cells.LoadFromDataTable(datatable, true);

    var chart = worksheet.Drawings.AddChart("chart test", eChartType.XYScatter);
    var series = chart.Series.Add(worksheet.Cells["B2:B11"], worksheet.Cells["A2:A11"]);

    //Get reference to the worksheet xml for proper namespace
    var chartXml = chart.ChartXml;
    var nsuri = chartXml.DocumentElement.NamespaceURI;
    var nsm = new XmlNamespaceManager(chartXml.NameTable);
    nsm.AddNamespace("c", nsuri);

    //XY Scatter plots have 2 value axis and no category
    var valAxisNodes = chartXml.SelectNodes("c:chartSpace/c:chart/c:plotArea/c:valAx", nsm);
    if (valAxisNodes != null && valAxisNodes.Count > 0)
        foreach (XmlNode valAxisNode in valAxisNodes)
        {
            var major = valAxisNode.SelectSingleNode("c:majorGridlines", nsm);
            if (major != null)
                valAxisNode.RemoveChild(major);

            var minor = valAxisNode.SelectSingleNode("c:minorGridlines", nsm);
            if (minor != null)
                valAxisNode.RemoveChild(minor);
        }

    //Other charts can have a category axis
    var catAxisNodes = chartXml.SelectNodes("c:chartSpace/c:chart/c:plotArea/c:catAx", nsm);
    if (catAxisNodes != null && catAxisNodes.Count > 0)
        foreach (XmlNode catAxisNode in catAxisNodes)
        {
            var major = catAxisNode.SelectSingleNode("c:majorGridlines", nsm);
            if (major != null)
                catAxisNode.RemoveChild(major);

            var minor = catAxisNode.SelectSingleNode("c:minorGridlines", nsm);
            if (minor != null)
                catAxisNode.RemoveChild(minor);
        }

    pck.Save();
}

Which gives this:

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download