For a start, let's create a simple chart consisting of one series of data points and one chart area. Only key steps of creating a chart are shown. All properties are default (except the type of the chart), no additional configuring is made.
Creating a Chart Object
Let's start with creating the key Chart object.
Example
[C#]
Chart chart = new Chart();
[VB .NET]
Dim chart As Chart = New Chart()
Creating a Series of Data Points
As no book without content is possible, in the same way any chart can only be produced based on series of data points. So the first step you should start with is defining data points. Create at least one series of data points to produce a chart diagram and add points to the DataPoints collection of the Series object.
You can either create separate DataPoint objects and set properties for each data point or, if the default data point properties suit you, create and add them to the Series.DataPoints collection using the DataPoints.Add method and specifying point coordinates. Creating separate DataPoint objects is described in the topic related to advanced chart programming; now let's use the latter way of adding data points since it is applicable in most cases.
Series also allows to specify a type of the chart. Let's make it Line. It is simple and its each point requires defining one X and one Y values.
Example
[C#]
Series s = new Series();
s.ChartType = ChartType.Line;
s.DataPoints.Add(1, 56);
s.DataPoints.Add(2, 79);
s.DataPoints.Add(3, 82);
s.DataPoints.Add(4, 67);
s.DataPoints.Add(5, 90);
s.DataPoints.Add(6, 12);
s.DataPoints.Add(7, 79);
s.DataPoints.Add(8, 80);
s.DataPoints.Add(9, 30);
[VB .NET]
Dim s As Series = New Series()
s.ChartType = ChartType.Line
s.DataPoints.Add(1, 56)
s.DataPoints.Add(2, 79)
s.DataPoints.Add(3, 82)
s.DataPoints.Add(4, 67)
s.DataPoints.Add(5, 90)
s.DataPoints.Add(6, 12)
s.DataPoints.Add(7, 79)
s.DataPoints.Add(8, 80)
s.DataPoints.Add(9, 30)
Adding Series to SeriesCollection
Now let's add the data series to the chart series collection:
Example
[C#]
chart.SeriesCollection.Add(s);
[VB .NET]
chart.SeriesCollection.Add(s)
Obtaining the Chart Image
The chart is ready. The only thing we should do is save the chart to an image file in one of possible formats or just obtain a Bitmap object. Imagine we need to import our chart image to an MS Word document using Aspose.Word. We will choose the latter approach then:
Example
[C#]
Bitmap chartBitmap = chart.GetChartImage();
[VB .NET]
Dim chartBitmap As Bitmap = chart.GetChartImage()
Summary
Here's the complete code sample and the resulting chart image:
Example
[C#]
Chart chart = new Chart();
Series s = new Series();
s.ChartType = ChartType.Line;
s.DataPoints.Add(1, 56);
s.DataPoints.Add(2, 79);
s.DataPoints.Add(3, 82);
s.DataPoints.Add(4, 67);
s.DataPoints.Add(5, 90);
s.DataPoints.Add(6, 12);
s.DataPoints.Add(7, 79);
s.DataPoints.Add(8, 80);
s.DataPoints.Add(9, 30);
chart.SeriesCollection.Add(s);
Bitmap chartBitmap = chart.GetChartImage();
[VB .NET]
Dim chart As Chart = New Chart()
Dim s As Series = New Series()
s.ChartType = ChartType.Line
s.DataPoints.Add(1, 56)
s.DataPoints.Add(2, 79)
s.DataPoints.Add(3, 82)
s.DataPoints.Add(4, 67)
s.DataPoints.Add(5, 90)
s.DataPoints.Add(6, 12)
s.DataPoints.Add(7, 79)
s.DataPoints.Add(8, 80)
s.DataPoints.Add(9, 30)
chart.SeriesCollection.Add(s)
Dim chartBitmap As Bitmap = chart.GetChartImage()
Creating a Chart Declaratively
Instead of adding data points to a data series and subsequent adding the data series to the collection of data series step-by-step, all this can be done declaratively using XML. Simply describe chart elements in an XML file and then bind this file using the Chart.BindXML method – and you're done. Here's how the XML should look like to achieve the same result as using the above code:
Example
[XML]
<Chart xmlns='Aspose.Chart' >
<SeriesCollection>
<Series ChartType='Line' Name='MySeries' >
<DataPoints>
<DataPoint XValue='1' YValues='56' />
<DataPoint XValue='2' YValues='79' />
<DataPoint XValue='3' YValues='82' />
<DataPoint XValue='4' YValues='67' />
<DataPoint XValue='5' YValues='90' />
<DataPoint XValue='6' YValues='12' />
<DataPoint XValue='7' YValues='79' />
<DataPoint XValue='8' YValues='80' />
<DataPoint XValue='9' YValues='30' />
</DataPoints>
</Series>
<SeriesCollection>
</Chart>