如何将值(不仅仅是百分比)添加到 Excel 饼图?
How can I add the value (not just the percentage) to an Excel pie chart?
我有这个代码:
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);
. . .
...生成此饼图:
我非常喜欢它,但我也需要在馅饼块中打印价值(例如 "Contracted Items" 块中印有“361,779 美元”,另一块也有适当的价值) .我该怎么做?
在 C# 中,"true/false" 决定显示的值:
来自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
)
因此,根据需要调整 True
和 False
。
根据蝙蝠侠的信息进行实验,我想到了这个:
contractChart.ApplyDataLabels(XlDataLabelsType.xlDataLabelsShowValue, false, true, false, false, false, true, true, false, false);
...产生这个:
我希望百分比不要有前导“0”(它显示“045”和“055”而不是更明智和经济的“45”和“55”),但我可以生活和他们在一起。
我有这个代码:
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);
. . .
...生成此饼图:
我非常喜欢它,但我也需要在馅饼块中打印价值(例如 "Contracted Items" 块中印有“361,779 美元”,另一块也有适当的价值) .我该怎么做?
在 C# 中,"true/false" 决定显示的值:
来自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
)
因此,根据需要调整 True
和 False
。
根据蝙蝠侠的信息进行实验,我想到了这个:
contractChart.ApplyDataLabels(XlDataLabelsType.xlDataLabelsShowValue, false, true, false, false, false, true, true, false, false);
...产生这个:
我希望百分比不要有前导“0”(它显示“045”和“055”而不是更明智和经济的“45”和“55”),但我可以生活和他们在一起。