0

For last 2 days I stuck with a line chart issue with EPPlus. My objective is to create a line chart with EPPlus and line style will be dash line like below image. picture attached.

enter image description here

I search a lot Google for 2 days still got no relevant hint to have dash line in chart. I am not sure that at all EPPlus support dash line. I am using EPPlus version 6.0 and .NET Framework version 4.8. This is a sample code which generate chart in excel sheet with one line but I need that line will be dash line. please someone see my code and tell me what is missing in my code for which I could not have dash line style of line chart.

ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
var newFile = new FileInfo(filepath);
using (ExcelPackage xlPackage = new ExcelPackage(newFile))
{
    ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets.Add("test");
    worksheet.Cells["A1"].Value = 1;
    worksheet.Cells["A2"].Value = 2;
    worksheet.Cells["A3"].Value = 3;
    worksheet.Cells["A4"].Value = 4;
    worksheet.Cells["A5"].Value = 5;
    worksheet.Cells["A6"].Value = 6;

    worksheet.Cells["B1"].Value = 10000;
    worksheet.Cells["B2"].Value = 10100;
    worksheet.Cells["B3"].Value = 10200;
    worksheet.Cells["B4"].Value = 10150;
    worksheet.Cells["B5"].Value = 10250;
    worksheet.Cells["B6"].Value = 10200;

    //ExcelChart chart = worksheet.Drawings.AddChart("LineChart", eChartType.XYScatterSmooth);
    ExcelChart chart = worksheet.Drawings.AddChart("LineChart", eChartType.XYScatterSmoothNoMarkers);
    chart.Series.Add(ExcelRange.GetAddress(1, 2, worksheet.Dimension.End.Row, 2),
                ExcelRange.GetAddress(1, 1, worksheet.Dimension.End.Row, 1));

    var Series = chart.Series[0];
    //chart.Axis[0].MinorGridlines.Fill.Color = Color.Red;
    //chart.Axis[0].MinorGridlines.LineStyle = eLineStyle.LongDashDot;
    chart.Axis[0].RemoveGridlines();
    chart.Axis[1].RemoveGridlines();
    chart.Axis[0].Border.LineStyle = eLineStyle.SystemDash;
    //chart.XAxis.Border.LineStyle = eLineStyle.Dash;
    chart.Series[0].Header = "Blah";
    //chart.Series[0].Border.LineStyle = eLineStyle.DashDot;
    //chart.Axis[0].Border.LineStyle = eLineStyle.Dash;
    
    xlPackage.Save();
    MessageBox.Show("Done");
}

I also check this post but could not implement it in my code Office Open XML: Drawing Dashed Line in Chart

Please push me to right direction to achieve my goal. Thanks in advance.

Thomas
  • 33,544
  • 126
  • 357
  • 626
  • I don't think there is such a functionality in epplus but you can work around it by directly editing the series's xml document and add the `` (see https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.drawing.charts.chartshapeproperties for details) – Ilia Maskov Oct 27 '22 at 21:16

2 Answers2

0

This works for me with same versions as you.
You need to add a reference to System.Drawing for the color:

// ...(code above this line did not change)

ExcelChart chart = worksheet.Drawings.AddChart("LineChart", eChartType.XYScatterSmoothNoMarkers);
chart.Series.Add(
    ExcelRange.GetAddress(1, 2, worksheet.Dimension.End.Row, 2),
    ExcelRange.GetAddress(1, 1, worksheet.Dimension.End.Row, 1));

chart.Series[0].Header = "Blah";

// Add this:
chart.Series[0].Border.LineStyle = OfficeOpenXml.Drawing.eLineStyle.Dash;
chart.Series[0].Border.Width = 2;
chart.Series[0].Border.Fill.Color = System.Drawing.Color.Blue;

chart.Axis[0].RemoveGridlines();
chart.Axis[1].RemoveGridlines();

xlPackage.Save();

Result:

enter image description here

evilmandarine
  • 4,241
  • 4
  • 17
  • 40
-1

#region chart summary 4 ExcelWorksheet Worksheet4 = p.Workbook.Worksheets.Add("Graph(4)"); var summary_Chart_four = Worksheet4.Drawings.AddChart("chart", eChartType.LineMarkers); // var summary_Chart_four = Worksheet4.Drawings.AddChart("chart", eChartType.XYScatterLines) as ExcelScatterChart;

            //var summary_Chart_four = Worksheet4.Drawings.AddChart("chart", (eChartType)System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash);
            //var summary_Chart_four = myWorksheet.Drawings.AddChart("chart", (eChartType)System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash);
            summary_Chart_four.ShowDataLabelsOverMaximum = true;
            Worksheet4.Cells.Style.Font.Name = "Arial";
            Worksheet4.Cells.Style.Font.Size = 10;
            Worksheet4.Cells.Style.Font.Bold = false;
            loopCnt = 0;
            for (int i = 0; i < (cVM.PeerGroups.Count + cVM.IndexGroups.Count) * 4; i += 4)
            {
                int rowIndex = 10 + i;
                int endRowIndex = 14 + i;
                string startingColumn_series = "C" + rowIndex;
                string endColumn_series = column + rowIndex;
                string lastColumn_xaxis = column + 6;
                int headerRowIndex = 9 + i;
                string seriesHeader = "A" + headerRowIndex;
                var series_i = summary_Chart_four.Series.Add(myWorksheet_summary_data.Cells[startingColumn_series + ":" + endColumn_series], myWorksheet_summary_data.Cells["C6:" + lastColumn_xaxis]);//working
                series_i.Header = myWorksheet_summary_data.Cells[seriesHeader].Value.ToString();
                summary_Chart_four.YAxis.Font.Size = 10;
                summary_Chart_four.XAxis.Font.Size = 10;
                //summary_Chart_four.XAxis.Format =DateTime.Now.Year.ToString();
                summary_Chart_four.YAxis.Format = "$0";

                if (loopCnt == 0)
                {
                    string LineColor = "#000080";
                    series_i.Border.LineStyle = eLineStyle.Solid;
                    //series_i.LineColor = LineColor.ConvertToColor();
                    //series_i.LineWidth = 2.0;
                    //series_i.Marker.Style = eMarkerStyle.Diamond;
                    //series_i.Marker.Size = 8;
                    //string BGColor = "#99ccff";
                    //series_i.Marker.Fill.Color = BGColor.ConvertToColor();
                    //string strBorderColor = "#000080";
                    //series_i.Marker.Border.Fill.Color = strBorderColor.ConvertToColor();
                    ((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).LineColor = LineColor.ConvertToColor();
                    ((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).LineWidth = 2.0;
                    ((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Style = eMarkerStyle.Diamond;
                    ((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Size = 8;
                    string BGColor = "#99ccff";
                    //((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Border.LineCap
                    ((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Fill.Color = BGColor.ConvertToColor();
                    string strBorderColor = "#000080";
                    ((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Border.Fill.Color = strBorderColor.ConvertToColor();
                    ((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Border.Width = 0.75;
                }
                else if (loopCnt == 1)
                {
                    string LineColor = "#003300";
                    series_i.Border.LineStyle = eLineStyle.LongDash;
                    //series_i.LineColor = LineColor.ConvertToColor();
                    //series_i.LineWidth = 2.0;
                    //series_i.Marker.Style = eMarkerStyle.Square;
                    ////((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Style = eMarkerStyle.Dash;
                    //series_i.Marker.Size = 7;
                    string BGColor = "#c0c0c0";
                    //series_i.Marker.Fill.Color = BGColor.ConvertToColor();
                    //string strBorderColor = "#003300";
                    //series_i.Marker.Border.Fill.Color = strBorderColor.ConvertToColor();

                    ((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).LineColor = LineColor.ConvertToColor();
                    ((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).LineWidth = 2.0;
                    ((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Style = eMarkerStyle.Square;
                    ((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Size = 7;
                    //((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Border.LineCap
                    ((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Fill.Color = BGColor.ConvertToColor();
                    string strBorderColor = "#003300";
                    ((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Border.Fill.Color = strBorderColor.ConvertToColor();
                    ((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Border.Width = 0.75;

                }

                else if (loopCnt == 2)
                {
                    string LineColor = "#F79646";
                    series_i.Border.LineStyle = eLineStyle.Dot;
                    ////series_i.LineColor = LineColor.ConvertToColor();
                    ////series_i.LineWidth = 2.0;
                    ////series_i.Marker.Style = eMarkerStyle.Circle;
                    //////((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Style = eMarkerStyle.Dot;
                    ////series_i.Marker.Size = 8;
                    string BGColor = "#ccffcc";
                    ////series_i.Marker.Fill.Color = BGColor.ConvertToColor();
                    string strBorderColor = "#F79646";
                    ////series_i.Marker.Border.Fill.Color = strBorderColor.ConvertToColor();
                    ((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).LineColor = LineColor.ConvertToColor();
                    ((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).LineWidth = 2.0;
                    ((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Style = eMarkerStyle.Circle;
                    ((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Size = 8;
                    //((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Border.LineCap
                    ((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Fill.Color = BGColor.ConvertToColor();
                    ((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Border.Fill.Color = strBorderColor.ConvertToColor();

                    ((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Border.Width = 0.75;


                }
                else if (loopCnt == 3)
                {
                    string LineColor = "#c0504d";
                    series_i.Border.LineStyle = eLineStyle.LongDashDotDot;
                    ////series_i.LineColor = LineColor.ConvertToColor();
                    ////series_i.LineWidth = 2.0;
                    ////series_i.Marker.Style = eMarkerStyle.Triangle;
                    //////((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Style = eMarkerStyle.Dot;
                    ////series_i.Marker.Size = 8;
                    string BGColor = "#c0504d";
                    ////series_i.Marker.Fill.Color = BGColor.ConvertToColor();
                    ((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).LineColor = LineColor.ConvertToColor();
                    ((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).LineWidth = 2.0;
                    ((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Style = eMarkerStyle.Triangle;
                    ((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Size = 8;
                    //((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Border.LineCap
                    ((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Fill.Color = BGColor.ConvertToColor();
                    string strBorderColor = "#003300";
                    ((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Border.Fill.Color = strBorderColor.ConvertToColor();
                    ((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Border.Width = 0.75;

                }

                //else if (loopCnt == 4)
                //{
                //    string LineColor = "#c0504d";
                //    series_i.Border.LineStyle = eLineStyle.LongDashDotDot;
                //    series_i.LineColor = LineColor.ConvertToColor();
                //    series_i.LineWidth = 1.5;
                //    series_i.Marker.Style = eMarkerStyle.Square;
                //    //((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Marker.Style = eMarkerStyle.Dot;
                //    series_i.Marker.Size = 8;
                //    string BGColor = "#c0504d";
                //    series_i.Marker.Fill.Color = BGColor.ConvertToColor();

                //}
                else
                {
                    ////series_i.LineColor = System.Drawing.Color.Aqua;
                    ////series_i.LineWidth = 1.5;
                    ////series_i.Marker.Style = eMarkerStyle.Circle;
                    ////series_i.Marker.Size = 8;
                    ////series_i.Marker.Fill.Color = System.Drawing.Color.Aqua;
                }
                //series_i.Smooth = eLineStyle.Dash;
                //((OfficeOpenXml.Drawing.Chart.ExcelLineChartSerie)series_i).Series=eChartType.d


                loopCnt++;
            }


            //var series_four = summary_Chart_four.Series.Add(myWorksheet_summary_data.Cells["C10:" + lastColumn_series1], myWorksheet_summary_data.Cells["C6:" + lastColumn_xaxis]);//working
            //series_four.Header = myWorksheet_summary_data.Cells["A9"].Value.ToString();

            //var series_four1 = summary_Chart_four.Series.Add(myWorksheet_summary_data.Cells["C14:" + lastColumn_series2], myWorksheet_summary_data.Cells["C6:" + lastColumn_xaxis]);

            //series_four1.Header = myWorksheet_summary_data.Cells["A13"].Value.ToString();




            summary_Chart_four.Border.Fill.Color = System.Drawing.Color.Black;
            //summary_Chart_four.Title.Text = "Comparison of " + cVM.YearsBack + " Year Cumulative Total Return" + Environment.NewLine + " Assumes Initial Investment of $100" + Environment.NewLine + strMonth + " " + strSelectedDt.Year;
            summary_Chart_four.Title.Text = "COMPARISON OF CUMULATIVE TOTAL RETURN";
            summary_Chart_four.SetSize(960, 600);
            // Add to 6th row and to the 6th column
            summary_Chart_four.Legend.Font.Size = 10;
            ((OfficeOpenXml.Drawing.Chart.ExcelChartStandard)summary_Chart_four).Legend.Font.SetFromFont("Calibri", 11, false, false, false, false);
            ((OfficeOpenXml.Drawing.Chart.ExcelChartStandard)summary_Chart_four).YAxis.Font.SetFromFont("Arial", 10, false, false, false, false);
            ((OfficeOpenXml.Drawing.Chart.ExcelChartStandard)summary_Chart_four).XAxis.Font.SetFromFont("Arial", 10, false, false, false, false);
            ((OfficeOpenXml.Drawing.Chart.ExcelChartStandard)summary_Chart_four).Title.Font.Bold = true;
            ((OfficeOpenXml.Drawing.Chart.ExcelChartStandard)summary_Chart_four).Title.Font.SetFromFont("Calibri", 16, false, false, false, false);
            //summary_Chart_four.Legend.Border.Fill.Color = System.Drawing.Color.Black;
            summary_Chart_four.SetPosition(0, 0, 0, 0);
            summary_Chart_four.XAxis.MajorTickMark = eAxisTickMark.In;
            //summary_Chart.XAxis.MajorTickMark = eAxisTickMark.In;
            summary_Chart_four.XAxis.MinorTickMark = eAxisTickMark.None;
            summary_Chart_four.YAxis.MinorTickMark = eAxisTickMark.None;
            summary_Chart_four.Legend.Position = eLegendPosition.Top;
            summary_Chart_four.YAxis.CrossBetween = eCrossBetween.MidCat;
            summary_Chart_four.XAxis.MajorGridlines.CompoundLineStyle = eCompundLineStyle.Double;
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 07 '22 at 18:24