如何将值(而不仅仅是百分比)添加到Excel饼图?

时间:2015-11-20 21:41:21

标签: c# excel charts excel-interop excel-charts

我有这段代码:

private void WriteChartSheet()
{
    _xlSheetChart = (Worksheet)_xlSheets.Item[2];

    if (_xlSheetChart != null)
    {
        _xlSheetChart.Name = ProduceUsageChartSheetName;
        // Contract vs. non-Contract pie chart 
        _xlSheetChart.Cells[1, 1] = "Contracted Items";
        _xlSheetChart.Cells[1, 2] = "Non-Contracted Items";
        _xlSheetChart.Cells[2, 1] = GetContractedItemsTotal();
        _xlSheetChart.Cells[2, 2] = GetNonContractedItemsTotal();

        ChartObjects xlCharts = (ChartObjects)_xlSheetChart.ChartObjects(Type.Missing);
        ChartObject contractChartObject = xlCharts.Add(0, 0, 300, 250); // left, top, width, height
        Chart contractChart = contractChartObject.Chart;

        Range chartRange = _xlSheetChart.get_Range("A1", "B2");
        contractChart.SetSourceData(chartRange, Missing.Value);
        contractChart.ChartType = XlChartType.xlPie;  //xl3DPie;
        contractChart.ApplyDataLabels(XlDataLabelsType.xlDataLabelsShowValue, XlDataLabelsType.xlDataLabelsShowLabel, true, false, false, true, false, true);
        . . .

...产生这个饼图:

primus

我非常喜欢它,但我也需要在饼图中打印出值(例如“合同项目”部分的“$ 361,779”,另一部分的适当值)。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

在C#中,“真/假”是决定显示什么值的原因:

来自here

void ApplyDataLabels(
    XlDataLabelsType Type = XlDataLabelsType.xlDataLabelsShowValue,
    object LegendKey,
    object AutoText,
    object HasLeaderLines,
    object ShowSeriesName,
    object ShowCategoryName,
    object ShowValue,
    object ShowPercentage,
    object ShowBubbleSize,
    object Separator
)

因此,请根据需要调整TrueFalse

答案 1 :(得分:0)

尝试蝙蝠侠的信息,我想出了这个:

contractChart.ApplyDataLabels(XlDataLabelsType.xlDataLabelsShowValue, false, true, false, false, false, true, true, false, false);

......产生这个:

enter image description here

我宁愿在百分比中没有领先的“0”(显示“045”和“055”而不是更明智和经济的“45”和“55”),但我可以和他们一起生活