Skip to Main Content
Adding more elements to your Document – Part IV

Adding more elements to your Document – Part IV

Learn more about Xceed Words for .NET

 

This week, we will look at how to add Charts to your documents.

Charts

When you want to catch the eye of your reader, a Chart offers the ability to display information and data in a more visual way than what a Table can.

 

Creating a Chart

There are currently 3 chart types available: BarChart, LineChart and PieChart.

// Create a new BarChart
var c1 = new BarChart();

// Create a new LineChart
var c2 = new LineChart();

// Create a new PieChart
var c3 = new PieChart();

 

Chart Legend

A ChartLegend is not required, but it can make it easier for the reader to understand the data displayed in the chart. Adding a ChartLegend is done by calling the AddLegend method on the chart. The parameters to include are the position of the chart (Bottom, Left, Right, Top or TopRight), and if the chart elements are allowed to overlap this ChartLegend (true/false).

// Add a ChartLegent
c.AddLegend( ChartLegendPosition.Left, false );

 

Removing the existing ChartLegend from a Chart is done by calling its RemoveLegend method.

// Remove the ChartLegend
c.RemoveLegend();

 

Chart Data

In an actual scenario, you would most likely have a database to get data from. For our example, we will create some data ourselves using a simple ChartData class that contains 2 properties: Category (string) and Expenses (double).

// Create the data.
var canada = new List()
{
  new ChartData() { Category = "Food", Expenses = 100 },
  new ChartData() { Category = "Housing", Expenses = 120 },
  new ChartData() { Category = "Transportation", Expenses = 140 },
  new ChartData() { Category = "Health Care", Expenses = 150 }
};
var usa = new List()
{
  new ChartData() { Category = "Food", Expenses = 200 },
  new ChartData() { Category = "Housing", Expenses = 150 },
  new ChartData() { Category = "Transportation", Expenses = 110 },
  new ChartData() { Category = "Health Care", Expenses = 100 }
};
var brazil = new List()
{
  new ChartData() { Category = "Food", Expenses = 125 },
  new ChartData() { Category = "Housing", Expenses = 80 },
  new ChartData() { Category = "Transportation", Expenses = 110 },
  new ChartData() { Category = "Health Care", Expenses = 60 }
};

 

 Next, we will create Series with the data and add it to the Chart by calling its AddSeries method.

// Create and add series by binding X and Y.
var s1 = new Series( "Brazil" );
s1.Bind( brazil, "Category", "Expenses" );
c.AddSeries( s1 );

var s2 = new Series( "USA" );
s2.Bind( usa, "Category", "Expenses" );
c.AddSeries( s2 );

var s3 = new Series( "Canada" );
s3.Bind( canada, "Category", "Expenses" );
c.AddSeries( s3 );

 

Adding Charts

Once the Chart is ready, adding it to a document is done by calling either InsertChart or InsertChartAfterParagraph on the Document.

// Create a new chart
var chart = new BarChart();

// Add data to the chart
// ...

// Option 1: Insert the chart at the end of the document
document.InsertChart( chart );

// Option 2: Insert the chart after the specified paragraph in the document
var paragraph = document.paragraphs[ 3 ];
document.InsertChart( chart , paragraph );

Note: Both the InsertChart and InsertChartAfterParagraph also support 2 additional optional parameters, to indicate a width and height for the chart. 

  

Removing Charts

Removing an existing Chart from a document is done by calling Remove on the Chart's XML.

// Remove a Chart
chart.Xml.Remove();

  

For more information, please refer to the documentation.

Join more than 100,000 satisfied customers now!

IBM
Deloitte
Microsoft
NASA
Bank of America
JP Morgan
Apple