Over a million developers have joined DZone.

How To: Plot Graphs in WPF

· Database Zone

Sign up for the Couchbase Community Newsletter to stay ahead of the curve on the latest NoSQL news, events, and webinars. Brought to you in partnership with Coucbase.

Plotting a graph from a set of test-related data is a common software-development task. In my experience, the most common approach is to import data into an Excel spreadsheet, then produce the graph manually using the Excel built-in graphing features. This works well in most situations, but if the underlying data changes frequently, creating graphs by hand can quickly become tedious.
You can automate the process using Windows Presentation Foundation (WPF) technology.

Preparing the Project for Graph Plotting 
Launch Visual Studio and create a new C# project using the WPF Application template. Although you can programmatically generate graphs using WPF primitives, I suggest using the convenient Dynamic Data Display (D3) library developed by a Microsoft Research lab.

You can download the library for free from the CodePlex open source hosting site at codeplex.com/dynamicdatadisplay. You should save a copy of this library in the root directory of your project, then add a reference to the DLL in your project by right-clicking on the project name, select the Add Reference option and point to the DLL file in your root directory.

Next you need to double-click on the file Window1.xaml to load the UI definitions for the project. You should add a reference to the graphic library DLL as follows:

xmlns:d3="http://research.microsoft.com/DynamicDataDisplay/1.0"
Title="Window1" WindowState="Normal" Height="500" Width="800" Background="Wheat">

After that, you can add the key plotting object:

<d3:ChartPlotter Name="plotter" Margin="10,10,20,10">
  <d3:ChartPlotter.HorizontalAxis>
    <d3:HorizontalDateTimeAxis Name="dateAxis"/>
  </d3:ChartPlotter.HorizontalAxis>
  <d3:ChartPlotter.VerticalAxis>
    <d3:VerticalIntegerAxis Name="countAxis"/>
  </d3:ChartPlotter.VerticalAxis>

  <d3:Header FontFamily="Arial" Content="Bug Information"/>
  <d3:VerticalAxisTitle FontFamily="Arial" Content="Count"/>
  <d3:HorizontalAxisTitle FontFamily="Arial" Content="Date"/>
</d3:ChartPlotter>

The Getting Started with NoSQL Guide will get you hands-on with NoSQL in minutes with no coding needed. Brought to you in partnership with Couchbase.

Topics:
dotnet ,how-to ,microsoft ,tools ,visual studio ,data access ,net ,graphing

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}