Mike Taulty's Blog
Bits and Bytes from Microsoft UK
Drawing a XAML graph from VB

Blogs

Mike Taulty's Blog

Elsewhere

I did a thing at TechEd where I took a set of process data from my machine and turned it into a basic bar chart by writing some LINQ to XML and producing XAML as the output. I say "basic" because the chart was built from a bunch of ProgressBar elements with each of their values set to match the number of threads in a given process.

Following on from that, a number of people have asked me "How'd you generate XAML graphs using LINQ to XML" and so I thought I'd paste something here to share;

Imports <xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">

Public Class Form1

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim data As New List(Of PointF)
    Dim width = 8 * 96
    Dim height = 6 * 96

    For i As Single = 0 To (Math.PI * 2) Step (Math.PI / 50)
      data.Add(New PointF(i, Math.Sin(i)))
    Next

    Dim xml = <Grid Width=<%= width %> Height=<%= height %> Background="Black">
                <Polyline Stroke="White" Stretch="Fill" StrokeThickness="2">
                  <Polyline.Points>
                    <%= From p In data Select <Point X=<%= p.X %> Y=<%= p.Y %>/> %>
                  </Polyline.Points>
                </Polyline>
              </Grid>

    xml.Save("c:\temp\graph.xaml")
    Process.Start("c:\temp\graph.xaml")

  End Sub

End Class

So, that's just a simple example using a Polyline in order to draw a Sine wave. You can go as far as you like with this stuff in that you can generate whatever XML you want from LINQ to XML and (consequently) whatever XAML you want :-)

Update:Ian quite rightly points out that this is not necessarily the only (or necessarily best) way to dynamically create a graph of WPF objects using LINQ. The reason why I went down this path was that it came from a "LINQ to XML" session where I was trying to make something visual in a cheap-and-cheerful manner so that perhaps explains the XAML based slant here :-)


Posted Tue, Nov 13 2007 1:30 AM by mtaulty

Comments

Anime and other things » Blog Archive » Drawing a XAML graph from VB wrote Anime and other things &raquo; Blog Archive &raquo; Drawing a XAML graph from VB
on Tue, Nov 13 2007 6:20 AM
?????????????????? ???????????????????? ????????????, ???? ???????????????????????????? ????????… « ???????? ???????????? ?????????????? wrote ?????????????????? ???????????????????? ????????????, ???? ???????????????????????????? ????????&#8230; &laquo; ???????? ???????????? ??????????????
on Mon, Jan 14 2008 2:18 PM