0

I am generating a basic Excel chart with this code:

object misValue = System.Reflection.Missing.Value;
//add data 
_xlSheet.Cells[11, 11] = "PhooBar";
_xlSheet.Cells[11, 12] = "Student1";
_xlSheet.Cells[11, 13] = "Student2";
_xlSheet.Cells[11, 14] = "Student3";

_xlSheet.Cells[12, 11] = "Term1";
_xlSheet.Cells[12, 12] = "80";
_xlSheet.Cells[12, 13] = "65";
_xlSheet.Cells[12, 14] = "45";

_xlSheet.Cells[13, 11] = "Term2";
_xlSheet.Cells[13, 12] = "78";
_xlSheet.Cells[13, 13] = "72";
_xlSheet.Cells[13, 14] = "60";

_xlSheet.Cells[14, 11] = "Term3";
_xlSheet.Cells[14, 12] = "82";
_xlSheet.Cells[14, 13] = "80";
_xlSheet.Cells[14, 14] = "65";

_xlSheet.Cells[15, 11] = "Term4";
_xlSheet.Cells[15, 12] = "75";
_xlSheet.Cells[15, 13] = "82";
_xlSheet.Cells[15, 14] = "68";

Excel.Range chartRange;

Excel.ChartObjects xlCharts = (Excel.ChartObjects)_xlSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(468, 160, 348, 268);
Excel.Chart chartPage = myChart.Chart;

chartRange = _xlSheet.get_Range("K11", "O15"); // K == 11, O == 15
chartPage.SetSourceData(chartRange, misValue);
chartPage.ChartType = Excel.XlChartType.xlPieExploded;

I have a question about controlling the label placement here.

Now I want to know why there are four pie pieces but five items on the legend:

enter image description here

ISTM that the chart engine's legend should match the pie. What's the deal?

Community
  • 1
  • 1
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

1 Answers1

0

By changing the code to this, the pie and the legend correspond:

object misValue = System.Reflection.Missing.Value;
//add data 
_xlSheet.Cells[12, 11] = "Term1";
_xlSheet.Cells[12, 12] = "80";
_xlSheet.Cells[12, 13] = "65";
_xlSheet.Cells[12, 14] = "45";

_xlSheet.Cells[13, 11] = "Term2";
_xlSheet.Cells[13, 12] = "78";
_xlSheet.Cells[13, 13] = "72";
_xlSheet.Cells[13, 14] = "60";

_xlSheet.Cells[14, 11] = "Term3";
_xlSheet.Cells[14, 12] = "82";
_xlSheet.Cells[14, 13] = "80";
_xlSheet.Cells[14, 14] = "65";

_xlSheet.Cells[15, 11] = "Term4";
_xlSheet.Cells[15, 12] = "75";
_xlSheet.Cells[15, 13] = "82";
_xlSheet.Cells[15, 14] = "68";

Excel.Range chartRange;

//return;

Excel.ChartObjects xlCharts = (Excel.ChartObjects)_xlSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(468, 160, 348, 268);
Excel.Chart chartPage = myChart.Chart;

chartRange = _xlSheet.Range[_xlSheet.Cells[12, 11], _xlSheet.Cells[15, 14]];

I changed from "get_Range" to the more efficient and easier-to-grok "Range[]" and there is now a 1:1 correspondency between the pie pieces and the legend:

enter image description here

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862