如何在Epplus饼图中显示带小数的百分比? [英] How to display percentages with decimals in an Epplus Pie chart?

查看:228
本文介绍了如何在Epplus饼图中显示带小数的百分比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在EPPLUS饼图中,默认情况下百分比自动四舍五入,如何以编程方式用2个小数显示它们(10.75代替11)?

In a EPPLUS Pie chart, the percentages are rounded automatically by default, how can I display them with 2 decimals programatically (10.75 instead of 11) ?

我仅提供值(整数),百分比由组件自动计算。

I only provide the values (integers) and the percentages are calculated automatically by the component.

推荐答案

在EPPlus中确实看不到要设置的选项它-必须在 Serie.DataLabel 对象上。

Dont really see an option in EPPlus to set it - would have to be on the Serie.DataLabel object.

看起来它必须通过XML完成。这是一个示例(可能需要针对不同的图表类型进行调整):

Looks like it has to be done via XML. Here is an example of how to do it (might have to be tweaked for different chart types):

[TestMethod]
public void PieChartDataLabelPercent()
{
    //http://stackoverflow.com/questions/42393711/how-to-display-percentages-with-decimals-in-an-epplus-pie-chart
    var file = new FileInfo(@"c:\temp\PieChartDataLabelPercent.xlsx");
    if (file.Exists)
        file.Delete();

    var pck = new ExcelPackage(file);
    var workbook = pck.Workbook;
    var worksheet = workbook.Worksheets.Add("newsheet");

    var rand = new Random();
    var data = new List<KeyValuePair<string, int>>();
    for (var i = 0; i < 10; i++)
        data.Add(new KeyValuePair<string, int>($"Group {i}", rand.Next(10, 100)));

    //Fill the table
    var startCell = worksheet.Cells[1, 1];
    startCell.Offset(0, 0).Value = "Group Name";
    startCell.Offset(0, 1).Value = "Group Value";
    startCell.Offset(1, 0).LoadFromCollection(data);

    //Add the chart to the sheet
    var pieChart = worksheet.Drawings.AddChart("Chart1", eChartType.Pie);
    pieChart.SetPosition(data.Count + 1, 0, 0, 0);
    pieChart.SetSize(500, 400);
    pieChart.Title.Text = "Test Chart";

    //Set the data range
    var series = pieChart.Series.Add(worksheet.Cells[2, 2, data.Count + 1, 2], worksheet.Cells[2, 1, data.Count + 1, 1]);
    var pieSeries = (ExcelPieChartSerie)series;
    pieSeries.Explosion = 5;

    //Format the labels
    pieSeries.DataLabel.ShowValue = true;
    pieSeries.DataLabel.ShowPercent = true;
    pieSeries.DataLabel.ShowLeaderLines = true;
    pieSeries.DataLabel.Separator = ";  ";
    pieSeries.DataLabel.Position = eLabelPosition.BestFit;

    var xdoc = pieChart.ChartXml;
    var nsuri = xdoc.DocumentElement.NamespaceURI;
    var nsm = new XmlNamespaceManager(xdoc.NameTable);
    nsm.AddNamespace("c", nsuri);

    //Added the number format node via XML
    var numFmtNode = xdoc.CreateElement("c:numFmt", nsuri);

    var formatCodeAtt = xdoc.CreateAttribute("formatCode", nsuri);
    formatCodeAtt.Value = "0.00%";
    numFmtNode.Attributes.Append(formatCodeAtt);

    var sourceLinkedAtt = xdoc.CreateAttribute("sourceLinked", nsuri);
    sourceLinkedAtt.Value = "0";
    numFmtNode.Attributes.Append(sourceLinkedAtt);

    var dLblsNode = xdoc.SelectSingleNode("c:chartSpace/c:chart/c:plotArea/c:pieChart/c:ser/c:dLbls", nsm);
    dLblsNode.AppendChild(numFmtNode);


    //Format the legend
    pieChart.Legend.Add();
    pieChart.Legend.Position = eLegendPosition.Right;

    pck.Save();

}

哪个在输出中给出:

这篇关于如何在Epplus饼图中显示带小数的百分比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆