EPPlus ColumnStacked图表数据点颜色

时间:2015-12-18 13:31:00

标签: asp.net .net epplus

我可以使用EPPlus生成列堆积图表。需要更改数据点的颜色。

我找到了enter link description here的解决方案,但它只改变了系列的第一个数据点的颜色。我也可以帮助改变其他数据点的颜色。以下是我正在寻找enter image description here

的概念

这是有助于更改数据点第一种颜色的函数

    public void SetDataPointStyle(OfficeOpenXml.Drawing.Chart.ExcelChart chart, ExcelChartSerie series, int totalDataPoint, Color color)
{
    var i = 0;
    var found = false;
    foreach (var s in chart.Series)
    {
        if (s == series)
        {
            found = true;
            break;
        }
        ++i;
    }
    if (!found) throw new InvalidOperationException("series not found.");

    var nsm = chart.WorkSheet.Drawings.NameSpaceManager;
    var nschart = nsm.LookupNamespace("c");
    var nsa = nsm.LookupNamespace("a");
    var node = chart.ChartXml.SelectSingleNode(@"c:chartSpace/c:chart/c:plotArea/c:barChart/c:ser[c:idx[@val='" + i.ToString(System.Globalization.CultureInfo.InvariantCulture) + "']]", nsm);
    var doc = chart.ChartXml;

    var spPr = doc.CreateElement("c:spPr", nschart);
    var solidFill = spPr.AppendChild(doc.CreateElement("a:solidFill", nsa));
    var srgbClr = solidFill.AppendChild(doc.CreateElement("a:srgbClr", nsa));
    var valattrib = srgbClr.Attributes.Append(doc.CreateAttribute("val"));
    valattrib.Value = ToHex(color).Substring(1);

    //var ln = spPr.AppendChild(doc.CreateElement("a:ln", nsa));
    //var lnSolidFill = ln.AppendChild(doc.CreateElement("a:solidFill", nsa));
    //var lnSrgbClr = lnSolidFill.AppendChild(doc.CreateElement("a:srgbClr", nsa));
    //var lnValattrib = lnSrgbClr.Attributes.Append(doc.CreateAttribute("val"));
    //lnValattrib.Value = ToHex(Color.Gray).Substring(1);

    node.AppendChild(spPr);
}
public String ToHex(Color c)
{
    return "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
}

SetDataPointStyle(chart,chart.Series [0],1,Color.Tan);

2 个答案:

答案 0 :(得分:3)

您必须为每个系列填充一系列数据点颜色。这是一个将系列数据点设置为随机颜色的扩展方法。只需要指定系列号码。如果选择自己的颜色,只需覆盖逻辑或发送数组即可使用:

public static void SetChartPointRandomColors(this ExcelChart chart, int serieNumber)
{
    var chartXml = chart.ChartXml;

    var nsa = chart.WorkSheet.Drawings.NameSpaceManager.LookupNamespace("a");
    var nsuri = chartXml.DocumentElement.NamespaceURI;

    var nsm = new XmlNamespaceManager(chartXml.NameTable);
    nsm.AddNamespace("a", nsa);
    nsm.AddNamespace("c", nsuri);

    var serieNode = chart.ChartXml.SelectSingleNode(@"c:chartSpace/c:chart/c:plotArea/c:barChart/c:ser[c:idx[@val='" + serieNumber + "']]", nsm);
    var serie = chart.Series[serieNumber];
    var points = serie.Series.Length;
    var rand = new Random(serieNumber);

    for (var i = 1; i <= points; i++)
    {
        var dPt = chartXml.CreateNode(XmlNodeType.Element, "dPt", nsuri);
        var idx = chartXml.CreateNode(XmlNodeType.Element, "idx", nsuri);
        var att = chartXml.CreateAttribute("val", nsuri);
        att.Value = i.ToString();
        idx.Attributes.Append(att);
        dPt.AppendChild(idx);

        var srgbClr = chartXml.CreateNode(XmlNodeType.Element, "srgbClr", nsa);
        att = chartXml.CreateAttribute("val");

        //Generate a random color - override with own logic to specify
        var color = Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256));
        att.Value = $"{color.R:X2}{color.G:X2}{color.B:X2}";
        srgbClr.Attributes.Append(att);

        var solidFill = chartXml.CreateNode(XmlNodeType.Element, "solidFill", nsa);
        solidFill.AppendChild(srgbClr);

        var spPr = chartXml.CreateNode(XmlNodeType.Element, "spPr", nsuri);
        spPr.AppendChild(solidFill);

        dPt.AppendChild(spPr);
        serieNode.AppendChild(dPt);
    }
}

以下是一个使用示例:

[TestMethod]
public void Chart_BarChart_Colors_Test()
{
    //Throw in some data
    var datatable = new DataTable("tblData");
    datatable.Columns.AddRange(new[]{new DataColumn("Col1", typeof(int)),new DataColumn("Col2", typeof(int)),new DataColumn("Col3", typeof(int))});
    for (var i = 0; i < 10; i++){var row = datatable.NewRow();row[0] = i;row[1] = i * 10;row[2] = i * 15;datatable.Rows.Add(row);}

    //Create a test file    
    var fileInfo = new FileInfo(@"c:\temp\Chart_BarChart_Colors.xlsx");
    if (fileInfo.Exists)
        fileInfo.Delete();

    using (var pck = new ExcelPackage(fileInfo))
    {
        var workbook = pck.Workbook;
        var worksheet = workbook.Worksheets.Add("Sheet1");
        worksheet.Cells.LoadFromDataTable(datatable, true);

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

        chart.SetChartPointRandomColors(0);
        chart.SetChartPointRandomColors(1);

        pck.Save();
    }
}

会给你这个:

enter image description here

答案 1 :(得分:0)

我有一个类似的用例,我需要设置圆环图切片(数据点)的颜色。这个问题/答案非常有帮助,我想我会分享结果,以防其他人遇到这个问题。

注意 1:我使用的是启用了可空性的 C# 9;如果您不使用可空性,则可以删除 !

注意 2:我没有在圆环图中使用多个系列的用例,因此将其硬编码为系列 0。如果这对您不起作用,您可以参数化 SelectSingleNode 索引。

    public void SetDoughnutChartDataPointFill(ExcelChart chart, int dataPointIdx, Color color)
    {
        var nsm = chart.WorkSheet.Drawings.NameSpaceManager;
        var nschart = nsm.LookupNamespace("c");
        var nsa = nsm.LookupNamespace("a");
        var node = chart.ChartXml.SelectSingleNode(@"c:chartSpace/c:chart/c:plotArea/c:doughnutChart/c:ser[c:idx[@val='0']]", nsm)!;
        var doc = chart.ChartXml;
        var dPt = doc.CreateElement("c:dPt", nschart);
        var cdpIdx = doc.CreateElement("c:idx", nschart);
        var valattr = cdpIdx.Attributes!.Append(doc.CreateAttribute("val"));
        valattr.Value = dataPointIdx.ToString();
        dPt.AppendChild(cdpIdx);

        var spPr = doc.CreateElement("c:spPr", nschart);
        var solidFill = spPr.AppendChild(doc.CreateElement("a:solidFill", nsa))!;
        var srgbClr = solidFill.AppendChild(doc.CreateElement("a:srgbClr", nsa))!;
        var valattrib = srgbClr.Attributes!.Append(doc.CreateAttribute("val"));
        valattrib.Value = string.Format("{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B);
        dPt.AppendChild(spPr);

        node.AppendChild(dPt);
    }