ChartDirector 7.1 (.NET Edition)

The First WPF Project




This section introduces using ChartDirector in WPF. For Windows Forms, please refer to The First Windows Forms Project. For ASP.NET Web Forms, please refer to The First ASP.NET Web Forms Project. For ASP.NET MVC / Razor Pages, please refer to The First ASP.NET MVC / Razor Pages 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 "NetWPFCharts" 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 "NetWPFCharts" sample project. "NetWPFCharts" 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 WPFChartViewer 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 "NetWPFCharts").

[WPF - C#] NetWPFCharts\CSharpWPFCharts\simplebar.cs
using System; using ChartDirector; namespace CSharpWPFCharts { 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(WPFChartViewer 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'") ; } } }

The main part of the above code is the "createChart" method. It creates a chart and put it in a given WPFChartViewer 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.