The Chart control allows you to bind charts to any type of data source, including arrays. You can create a chart without setting its data source then load the data into the control at run time.
This walkthrough illustrates how to create a simple unbound chart.
The walkthrough is split up into the following activities:
- Creating a new Visual Studio project
- Adding an ActiveReport to the project
- Adding the chart control to the report
- Adding code to connect the chart to a data source
- Viewing the report
To complete the walkthrough, you must have access to the Northwind database.
A copy is located at C:\Program Files\Data Dynamics\ActiveReports for .NET 3.0\Data\NWIND.MDB.
When you have finished this walkthrough, you will have a report that looks similar to the following.
Creating a new Visual Studio project
To create a new Visual Studio project
- Open Visual Studio.
-
From the File menu, select New > Project.
- Select the project type and click on Windows Application.
- Change the name of your project and click OK.
Adding an ActiveReport to a Visual Studio project
To add an ActiveReport to your project
- Open a new project in Visual Studio.
- From the Project menu, select Add New Item.
- Select ActiveReports 3.0 File and rename the file rptUnboundChart.
- Click Open.
Adding the chart control to the report
To add a chart control to the report
- Click the ChartControl in the ActiveReports toolbox and draw it onto the report surface.
- With the chart control highlighted, click the Customize verb below the Properties Window to open the Chart Designer dialog.
If you do not see the verbs in Visual Studio 2005, right-click the Properties Window and select Commands. - Click the Series button on the left side, and delete Series1, Series2, and Series3.
- Click the Titles button on the left side of the Chart Designer.
- Type Unbound Chart in the header title Caption and increase the font size to 14.
- Delete the footer title.
- Click the ChartAreas button on the left side of the wizard, and expand the Axes button.
- Click AxisX to select it, enter Company Name in the AxisX Title and increase the font size to 12.
- Click AxisY to select it, enter Freight in US$ in the AxisY Title and increase the font size to 12.
- Click the Legends button on the left side of the Chart Designer.
- Clear the Visible check box at the top.
- Click Finish.
Adding code to connect the chart to a data source
To write the code in Visual Basic or C#
- Double-click in the gray area below rptUnbound. This creates an event-handling method for rptUnboundChart's ReportStart event. Add code to the handler to:
- Set the database path (place this code above the ReportStart event)
- Create the series
- Create the dataset
- Set the chart properties
- Angle the labels to avoid overlap
The following example shows what the code for the method looks like.
'Visual Basic 'Set the database path Private Function getDatabasePath() As String Dim regKey As Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.LocalMachine regKey = regKey.CreateSubKey _ ("SOFTWARE\\Data Dynamics\\ActiveReports for .NET 3.0\\SampleDB") getDatabasePath = CType(regKey.GetValue(""), String) End Function Private Sub rptUnboundChart_ReportStart(ByVal sender As Object, ByVal e As System _ .EventArgs) Handles MyBase.ReportStart 'create the series Dim series As New DataDynamics.ActiveReports.Chart.Series series.Type = Chart.ChartType.Bar3D 'connection string and data adapter Dim dbPath As String = getDatabasePath() Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + dbPath + "\\NWIND.mdb" Dim da As New System.Data.OleDb.OleDbDataAdapter("SELECT * from Orders WHERE OrderDate < #08/17/1994#", connString) 'create the dataset Dim ds As New DataSet da.Fill(ds, "Orders") 'set chart properties Me.ChartControl1.DataSource = ds Me.ChartControl1.Series.Add(series) Me.ChartControl1.Series(0).ValueMembersY = ds.Tables("Orders").Columns(7).ColumnName Me.ChartControl1.Series(0).ValueMemberX = ds.Tables("Orders").Columns(8).ColumnName 'angle the labels to avoid overlapping Me.ChartControl1.ChartAreas(0).Axes(0).LabelFont.Angle = 90 End Sub
//C# //Set the database path private string getDatabasePath() { Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.LocalMachine; regKey = regKey.CreateSubKey("SOFTWARE\\Data Dynamics\\ActiveReports for .NET 3.0\\SampleDB"); return ((string)(regKey.GetValue(""))); } private void rptUnboundChart_ReportStart(object sender, System.EventArgs eArgs) { //create the series DataDynamics.ActiveReports.Chart.Series series = new DataDynamics.ActiveReports.Chart.Series(); series.Type = DataDynamics.ActiveReports.Chart.ChartType.Bar3D; //connection string and data adapter string dbPath = getDatabasePath(); string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + dbPath + "\\NWIND.mdb"; System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter ("SELECT * from Orders WHERE OrderDate < #08/17/1994#", connString); // create the dataset System.Data.DataSet ds = new System.Data.DataSet(); da.Fill(ds, "Orders"); // set chart properties this.chartControl1.DataSource = ds; this.chartControl1.Series.Add(series); this.chartControl1.Series[0].ValueMembersY = ds.Tables["Orders"].Columns[7].ColumnName; this.chartControl1.Series[0].ValueMemberX = ds.Tables["Orders"].Columns[8].ColumnName; // angle the labels to avoid overlapping this.chartControl1.ChartAreas[0].Axes[0].LabelFont.Angle = 90; }
Viewing the report
To view the report
- Add the ActiveReports viewer control to a Windows Form.
- Add the code needed to set the viewer document equal to the report document. See Using the ActiveReports Windows Form Viewer for help.
You can quickly view your report at design time by clicking the Preview tab at the bottom of the designer. |