Somewhere to Draw

PieChart

Using

Example

BarChart

LineChart




WPF Tutorial Contents

APL  Home Page



Somewhere to Draw


Some XAML to provide a place to draw...

 <Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    Title="MyApp">
  <Grid >
    <charting:Chart Name="chartMain" >
    </charting:Chart>
  </Grid>
</Window>

PieChart


Using


We need to add entries to ⎕USING, not just for the charting controls but also for other object types that we'll also need - the smarter sort of programmer could probably work out a way to specify what's needed just when it's needed, but for convenience I've packaged it all together.  The location of the WPF Toolkit dll seems a bit dubious, you may find it somewhere else.

chartpath←'c:\program files (x86)\wpf Toolkit\v3.5.50211.1\'         ⎕USING,←⊂'System.Windows.Controls.DataVisualization.Charting,',chartpath,'System.Windows.Controls.DataVisualization.Toolkit.dll'
⎕USING,←⊂'System.Collections,mscorlib.dll'


Example


This should show a piechart (using a SortedList isn't ideal, what I really wanted was to use KeyValuePair but it seems there are obstacles thrown in APL's way when we try)...

 foo w;f;dt;n;c;⎕USING;mys;data;entry
 ⎕USING←#.UTIL.WPF.Using⊂'Chart'
 f n c←#.UTIL.XAML.LoadXAML w(⊂'Chart')
 f.c←f.FindName⊂'chartMain'
 data←⎕NEW SortedList
 :For entry :In ('Dog' 30)('Cat' 25)('Rat' 5)('Hamster' 8)('Rabbit' 12)
     data.Add entry
 :EndFor
 mys←⎕NEW PieSeries
 mys.ItemsSource←data
 mys.IndependentValuePath←⊂'Key'
 mys.DependentValuePath←⊂'Value'
 mys.IsSelectionEnabled←1
 f.c.DataContext←data
 f.c.Series.Add mys
 f.c.Title←'A Chart'
 f.c.LegendTitle←'Here is the legend'
 {}f.ShowDialog

BarChart


Making a BarChart seems relatively straightforward, just change PieSeries above to BarSeries.

And it also proves fairly simple to show more than one series (also omitted some stuff here that proved unnecessary - at least for this purpose)...

 foob w;f;dt;n;c;⎕USING;mys;data;entry;data1;mys1
 ⎕USING←#.UTIL.WPF.Using⊂'Chart'
 f n c←#.UTIL.XAML.LoadXAML w(⊂'Chart')
 f.c←f.FindName⊂'chartMain'
 data←⎕NEW SortedList
 :For entry :In ('Dog' 30)('Cat' 25)('Rat' 5)('Hamster' 8)('Rabbit' 12)
     data.Add entry
 :EndFor
 mys←⎕NEW BarSeries
 mys.ItemsSource←data
 mys.Title←'This is the first series...'
 mys.IndependentValuePath←⊂'Key'
 mys.DependentValuePath←⊂'Value'
 f.c.Series.Add mys
 data1←⎕NEW SortedList
 :For entry :In ('Dog' 20)('Cat' 20)('Rat' 15)('Hamster' 10)('Rabbit' 12)
     data1.Add entry
 :EndFor
 mys1←⎕NEW BarSeries
 mys1.ItemsSource←data1
 mys1.Title←'...and this is the second'
 mys1.IndependentValuePath←⊂'Key'
 mys1.DependentValuePath←⊂'Value'
 f.c.Series.Add mys1
 f.c.Title←'A Chart'
 f.c.LegendTitle←'Here is the legend'
 {}f.ShowDialog

LineChart

Turns out a simple mutation lets us draw a LineChart (notice that it seems to deal adequately with missing values)...

fool w;f;dt;n;c;⎕USING;mys;data;entry;data1;mys1
 ⎕USING←#.UTIL.WPF.Using⊂'Chart'
 f n c←#.UTIL.XAML.LoadXAML w(⊂'Chart')
 f.c←f.FindName⊂'chartMain'
 data←⎕NEW SortedList
 :For entry :In (1900 30)(1901 25)(1902 5)(1903 8)(1904 12)
     data.Add entry
 :EndFor
 mys←⎕NEW LineSeries
 mys.ItemsSource←data
 mys.Title←'This is the first series...'
 mys.IndependentValuePath←⊂'Key'
 mys.DependentValuePath←⊂'Value'
 f.c.Series.Add mys
 data1←⎕NEW SortedList
 :For entry :In (1900 20)(1902 15)(1903 10)(1904 12)
     data1.Add entry
 :EndFor
 mys1←⎕NEW LineSeries
 mys1.ItemsSource←data1
 mys1.Title←'...and this is the second'
 mys1.IndependentValuePath←⊂'Key'
 mys1.DependentValuePath←⊂'Value'
 f.c.Series.Add mys1
 f.c.Title←'A Chart'
 f.c.LegendTitle←'Here is the legend'
 {}f.ShowDialog