Formating lines in a LineChart

Jan 30, 2012 at 4:19 PM

Hi,

I just downloaded EPPlus 3.0 beta and started playing around. But I couldn't find a way to change the colors of lines and markers of a Line Chart. Is there any to do this?

Thanks in advance,

Rodrigo Basniak

Coordinator
Jan 31, 2012 at 5:32 PM

No, I haven't implemented that yet. But it shoulden't be to hard to implement. (A new property on the ExcelLineChartSerie is my guess).

Feel free to have a look at it, otherwise I will put it on my todo list.

Jan

May 20, 2013 at 12:00 PM
Hi,

I also need these functions, different lines color, line styles (dotted, dashed, solid...).
Any news about this??

Best regards
Cristian Barzaghi
May 28, 2013 at 10:44 AM
Edited May 28, 2013 at 10:45 AM
Hi,

I also needed to set custom line colors for different series in one chart, so I went ahead and added this to ExcelLineChartSerie.cs:
string LINECOLOR_PATH = "c:spPr/a:ln/a:solidFill/a:srgbClr/@val";
/// <summary>
/// Line color
/// </summary>
public string LineColor
{
    get
    {
        return GetXmlNodeString(LINECOLOR_PATH);
    }
    set
    {
        SetXmlNodeString(LINECOLOR_PATH, value, true);
    }
}
It only works with RGB color codes, i.e. using chartSerieObject.LineColor = "FF0000" for a red line, but that's fine for my purpose.

Cheers,

Kaptein
May 28, 2013 at 11:38 AM
Thanks Kaptein!!

this will usefull.
can you tell me were I can found a full xml reference for excel chart path definition??

Best regards
Cristian
May 28, 2013 at 1:00 PM
No problem, hope it was useful.

I don't know where to find a full reference - and I would probably not use it if I had it for the sake of time. I just created two .xlsx files, with and without custom colors, extracted the xml and compared the chart1.xml files in Notepad++. Found the path and used that to update the library code.

Cheers,

Kaptein
May 28, 2013 at 1:37 PM
Tnx for tips!

for a dashed line style the path is similar to color: c:spPr/a:ln/a:prstDash/@val
for the values refer to the OOXML datatype:
http://www.schemacentral.com/sc/ooxml/a-val-84.html

I implemented it like the Marker property.
That worked for me!!
Hope this help someone.

Cristian
Mar 5, 2014 at 12:43 PM
Is this going to be integrated into the main source (or maybe it is but not available via Nuget?)
Aug 11, 2014 at 9:26 PM
Kaptein,

Thank you very much. You saved me many painful hours of work.
Sep 18, 2014 at 11:31 AM
Edited Sep 18, 2014 at 11:36 AM
Hi

Here some code (based on Kaptein's code for which thanks). It van be added to both ExcelScatterSeries and ExcelLineSeries to add LineColor, LineWith and MarkerSize support to these two chart series.
       #region EXTENSIONS

        string LINECOLOR_PATH = "c:spPr/a:ln/a:solidFill/a:srgbClr/@val";

        /// <summary>
        /// Line color.
        /// </summary>
        ///
        /// <value>
        /// The color of the line.
        /// </value>
        public Color LineColor
        {
            get
            {
                string color = GetXmlNodeString(LINECOLOR_PATH);
                if (color == "")
                {
                    return Color.Black;
                }
                else
                {
                    return Color.FromArgb(Convert.ToInt32(color, 16));
                }
            }
            set
            {
                SetXmlNodeString(LINECOLOR_PATH, value.ToArgb().ToString("X").Substring(2), true);
            }
        }

        string MARKERSIZE_PATH = "c:marker/c:size/@val";

        /// <summary>
        /// Gets or sets the size of the marker.
        /// </summary>
        ///
        /// <remarks>
        /// value between 2 and 72.
        /// </remarks>
        ///
        /// <value>
        /// The size of the marker.
        /// </value>
        public int MarkerSize
        {
            get
            {
                string size = GetXmlNodeString(MARKERSIZE_PATH);
                if (size == "")
                {
                    return 5;
                }
                else
                {
                    return Int32.Parse(GetXmlNodeString(MARKERSIZE_PATH));
                }
            }
            set
            {
                int size = value;
                size = Math.Max(2, size);
                size = Math.Min(72, size);
                SetXmlNodeString(MARKERSIZE_PATH, size.ToString(), true);
            }
        }

        string LINEWIDTH_PATH = "c:spPr/a:ln/@w";

        /// <summary>
        /// Gets or sets the width of the line in pt.
        /// </summary>
        ///
        /// <value>
        /// The width of the line.
        /// </value>
        public double LineWidth
        {
            get
            {
                string size = GetXmlNodeString(LINEWIDTH_PATH);
                if (size == "")
                {
                    return 2.25;
                }
                else
                {
                    return double.Parse(GetXmlNodeString(LINEWIDTH_PATH)) / 12700;
                }
            }
            set
            {
                SetXmlNodeString(LINEWIDTH_PATH, ((int)(12700 * value)).ToString(), true);
            }
        }

        #endregion EXTENSIONS
regards
wvdv
Oct 23, 2014 at 4:20 PM
The code shared by wvd_vegt works great (don't forget to add "using System.Drawing;")

It would be great if this could be incorporated into EPPlus at some point. The default width / marker size on Excel line charts is HUGE. If you any substantial amount of data to chart, the resolution of the data points get lost in the thickness of the lines. This code is quite helpful!

Thanks for everyone's work on this. Thanks for such a great .dll!
Oct 24, 2014 at 3:58 AM
MARKER COLOR.

The last feature that would make it complete. Being able to change the line color without the marker color looks a bit silly on marker-type charts.

Just a request. If I had the knowledge, I'd gladly contribute myself!

Thanks!!!
Jan 7, 2015 at 5:23 AM
Edited Jan 7, 2015 at 5:24 AM
While waiting for Kaptein's code to be part of a future release, I have posted extension methods on this StackOverflow topic, which can be helpful if you don't want to recompile EPPlus yourself. They allow setting the color and thickness of a line chart series, and the color of a pie chart data point, but can easily be tweaked for other needs.
Jan 10, 2015 at 2:08 AM
Edited Jan 10, 2015 at 2:10 AM
The examples here were very useful and allowed line color, etc modification. I'm struggling with marker colors though. I was able to modify the marker border color, but cannot quite get the fill color to work. The following example works for MarkerBorderColorPath, but I have tried many combinations of MarkerFillColorPath. The latest shown below. Does anyone know the correct xpath for this? Thanks,
private const string MarkerBorderColorPath = "c:marker/c:spPr/a:ln/a:solidFill/a:srgbClr/@val";
private const string MarkerFillColorPath = "c:marker/c:spPr/a:group[2]/a:solidFill/a:srgbClr/@val";

public Color MarkerColor
{
    get
    {
        string color = GetXmlNodeString(MarkerBorderColorPath);
        if (color == "")
        {
            return Color.Black;
        }
        return Color.FromArgb(Convert.ToInt32(color, 16));
    }
    set
    {
        SetXmlNodeString(MarkerBorderColorPath, value.ToArgb().ToString("X").Substring(2), true);
        SetXmlNodeString(MarkerFillColorPath, value.ToArgb().ToString("X").Substring(2), true);
    }
}
Thank you,
Jan 10, 2015 at 5:12 PM
Ok, I found the tutorial at https://epplus.codeplex.com/discussions/346525. That was the information I needed. The following xpath is the result.
private const string MarkerBorderColorPath = "c:marker/c:spPr/a:ln/a:solidFill/a:srgbClr/@val";
private const string MarkerFillColorPath = "c:marker/c:spPr/a:solidFill/a:srgbClr/@val";