Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#
Tip/Trick

How to create a Titled and Labeled Excel Pie Chart with C#

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
21 Mar 2016CPOL 18.5K   10  
Creating a simple sample Excel Pie Chart, with labeled slices using C# and Excel Interop

Pie in the Sky, not in your Eye

This assumes you are using Excel Interop to work with Excel in a C# app.

Creating a pie chart is as easy as generating some data for it, and adding the chart to a worksheet:

C#
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
. . .
private Excel.Worksheet _xlSheet;
. . .
object misValue = System.Reflection.Missing.Value;

// First column (11, in this case) will be the label
_xlSheet.Cells[12, 11] = "Apple";
_xlSheet.Cells[12, 12] = "100";
_xlSheet.Cells[12, 13] = "167";
_xlSheet.Cells[12, 14] = "67";

_xlSheet.Cells[13, 11] = "Cherry";
_xlSheet.Cells[13, 12] = "78";
_xlSheet.Cells[13, 13] = "72";
_xlSheet.Cells[13, 14] = "96";

_xlSheet.Cells[14, 11] = "Gooseberry";
_xlSheet.Cells[14, 12] = "182";
_xlSheet.Cells[14, 13] = "185";
_xlSheet.Cells[14, 14] = "165";

_xlSheet.Cells[15, 11] = "Blackberry";
_xlSheet.Cells[15, 12] = "189";
_xlSheet.Cells[15, 13] = "180";
_xlSheet.Cells[15, 14] = "165";

_xlSheet.Cells[16, 11] = "Strawberry-Rhubarb";
_xlSheet.Cells[16, 12] = "17";
_xlSheet.Cells[16, 13] = "18";
_xlSheet.Cells[16, 14] = "16";

Excel.Range chartRange;

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

// Define a range that encompasses the data above (including the label "cells")
chartRange = _xlSheet.Range[_xlSheet.Cells[12, 11], _xlSheet.Cells[16, 14]];
chart.SetSourceData(chartRange, misValue);
chart.ChartType = Excel.XlChartType.xlExplodedPie; // Many other types of charts can be speficied
// It is not enough to set the title; you also have to tell it that it has a title first
chart.HasTitle = true;
chart.ChartTitle.Text = "Favorite Pies";

...and then adding the following line to add the labels:

C#
chart.ApplyDataLabels(Excel.XlDataLabelsType.xlDataLabelsShowPercent, Excel.XlDataLabelsType.xlDataLabelsShowLabel, true, false, false, true, false, true);

And "Voila!" as the eaters of escargot pie are wont to say: you now have a wonderfully splendiferous and scrumptious pie chart (shown before the title was added and the legend was removed):

Image 1

If the Legend is only a Legend in its Own Mind

If labeling the pie pieces is enough, and you don't need the legend, you can add this line of code to prevent the legend from displaying:

C#
chart.HasLegend = false;

I recently discovered Spreadsheet Light, which seems to greatly simplify and elegantize the creation of Excel spreadsheets with C#; soon, I will post an "alternate universe" version of this article about creating a chart with Spreadsheet Light.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Across Time & Space
United States United States
I am in the process of morphing from a software developer into a portrayer of Mark Twain. My monologue (or one-man play, entitled "The Adventures of Mark Twain: As Told By Himself" and set in 1896) features Twain giving an overview of his life up till then. The performance includes the relating of interesting experiences and humorous anecdotes from Twain's boyhood and youth, his time as a riverboat pilot, his wild and woolly adventures in the Territory of Nevada and California, and experiences as a writer and world traveler, including recollections of meetings with many of the famous and powerful of the 19th century - royalty, business magnates, fellow authors, as well as intimate glimpses into his home life (his parents, siblings, wife, and children).

Peripatetic and picaresque, I have lived in eight states; specifically, besides my native California (where I was born and where I now again reside) in chronological order: New York, Montana, Alaska, Oklahoma, Wisconsin, Idaho, and Missouri.

I am also a writer of both fiction (for which I use a nom de plume, "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction, including a two-volume social and cultural history of the U.S. which covers important events from 1620-2006: http://www.lulu.com/spotlight/blackbirdcraven

Comments and Discussions

 
-- There are no messages in this forum --