ChartDirector 6.2 (.NET Edition)

The First Windows Forms Project




This section introduces using ChartDirector in Windows Forms. If you are using ChartDirector in WPF, please refer to The First WPF Project. If you are using ChartDirector in ASP.NET Web Forms, please refer to The First ASP.NET Web Forms Project. If you are using ChartDirector in ASP.NET MVC, please refer to The First ASP.NET MVC Project.

To get a feeling of using ChartDirector, and to verify the ChartDirector development environment is set up properly, we will begin by building a very simple bar chart.

The following code module comes from the "NetWinCharts" sample project included in the ChartDirector distribution. If you have not yet tried the sample programs, it is highly recommended you try them now. Please refer to the Installation section for details. They are very useful for exploring and testing the features of ChartDirector.

The structure of the following sample code assumes it is running as part of the "NetWinCharts" sample project. "NetWinCharts" contains an explorer style user interface for browsing various sample charts. To support this, the sample code implements a "DemoModule" interface.

The key method of the "DemoModule" interface is the "createChart" method, which creates a chart and put it in a WinChartViewer control for display. The other methods are for description only (eg. name of the module, number of charts it contains). They are to facilitate browsing only, and are not specific to ChartDirector. So they will not be described here.

This section mainly illustrates the general code structure of using ChartDirector. In the next section, we will describe how to build the same chart from scratch (without running inside "NetWinCharts").

[Windows Forms - C# version] NetWinCharts\CSharpWinCharts\simplebar.cs
using System;
using ChartDirector;

namespace CSharpChartExplorer
{
    public class simplebar : DemoModule
    {
        //Name of demo module
        public string getName() { return "Simple Bar Chart (1)"; }

        //Number of charts produced in this demo module
        public int getNoOfCharts() { return 1; }

        //Main code for creating chart.
        //Note: the argument chartIndex is unused because this demo only has 1 chart.
        public void createChart(WinChartViewer viewer, int chartIndex)
        {
            // The data for the bar chart
            double[] data = {85, 156, 179.5, 211, 123};

            // The labels for the bar chart
            string[] labels = {"Mon", "Tue", "Wed", "Thu", "Fri"};

            // Create a XYChart object of size 250 x 250 pixels
            XYChart c = new XYChart(250, 250);

            // Set the plotarea at (30, 20) and of size 200 x 200 pixels
            c.setPlotArea(30, 20, 200, 200);

            // Add a bar chart layer using the given data
            c.addBarLayer(data);

            // Set the labels on the x axis.
            c.xAxis().setLabels(labels);

            // Output the chart
            viewer.Chart = c;

            //include tool tip for the chart
            viewer.ImageMap = c.getHTMLImageMap("clickable", "", "title='{xLabel}: {value} GBytes'")
                ;
        }
    }
}

[Windows Forms - VB Version] NetWinCharts\VBNetWinCharts\simplebar.vb
Imports System
Imports Microsoft.VisualBasic
Imports ChartDirector

Public Class simplebar
    Implements DemoModule

    'Name of demo module
    Public Function getName() As String Implements DemoModule.getName
        Return "Simple Bar Chart (1)"
    End Function

    'Number of charts produced in this demo module
    Public Function getNoOfCharts() As Integer Implements DemoModule.getNoOfCharts
        Return 1
    End Function

    'Main code for creating chart.
    'Note: the argument chartIndex is unused because this demo only has 1 chart.
    Public Sub createChart(viewer As WinChartViewer, chartIndex As Integer) _
        Implements DemoModule.createChart

        ' The data for the bar chart
        Dim data() As Double = {85, 156, 179.5, 211, 123}

        ' The labels for the bar chart
        Dim labels() As String = {"Mon", "Tue", "Wed", "Thu", "Fri"}

        ' Create a XYChart object of size 250 x 250 pixels
        Dim c As XYChart = New XYChart(250, 250)

        ' Set the plotarea at (30, 20) and of size 200 x 200 pixels
        c.setPlotArea(30, 20, 200, 200)

        ' Add a bar chart layer using the given data
        c.addBarLayer(data)

        ' Set the labels on the x axis.
        c.xAxis().setLabels(labels)

        ' Output the chart
        viewer.Chart = c

        'include tool tip for the chart
        viewer.ImageMap = c.getHTMLImageMap("clickable", "", "title='{xLabel}: {value} GBytes'")

    End Sub

End Class

The main part of the above code is the "createChart" method. It creates a chart and put it in a given WinChartViewer control for display. The code is explained below.

Note: The trial version of ChartDirector will include small yellow banners at the bottom of the charts it produces. These banners will disappear in the licensed version of ChartDirector.