Permalink
Browse files

Implemented core Legend functionality

  • Loading branch information...
1 parent f729348 commit 59eaace77b15fec82b66168b107dfdf42af9e28c @ailon ailon committed Jan 22, 2010
View
@@ -3,16 +3,16 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:amq="clr-namespace:AmCharts.Windows.QuickCharts;assembly=AmCharts.Windows.QuickCharts"
Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
- <Grid>
+ <Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<amq:SerialChart DataSource="{Binding Data}" CategoryValuePath="cat1">
<amq:SerialChart.Graphs>
- <amq:LineGraph ValueMemberPath="val1" />
- <!--<amq:ColumnGraph ValueMemberPath="val2" />-->
- <amq:LineGraph ValueMemberPath="val3" />
+ <amq:LineGraph ValueMemberPath="val1" Title="Line #1" Brush="Red" />
+ <amq:ColumnGraph ValueMemberPath="val2" Title="Column #1" />
+ <amq:LineGraph ValueMemberPath="val3" Title="Line #2" />
</amq:SerialChart.Graphs>
</amq:SerialChart>
@@ -61,6 +61,15 @@
<Compile Include="..\QuickChartsWPF\DiscreetClearObservableCollection.cs">
<Link>DiscreetClearObservableCollection.cs</Link>
</Compile>
+ <Compile Include="..\QuickChartsWPF\ILegendItem.cs">
+ <Link>ILegendItem.cs</Link>
+ </Compile>
+ <Compile Include="..\QuickChartsWPF\Legend.cs">
+ <Link>Legend.cs</Link>
+ </Compile>
+ <Compile Include="..\QuickChartsWPF\LegendItem.cs">
+ <Link>LegendItem.cs</Link>
+ </Compile>
<Compile Include="..\QuickChartsWPF\LineGraph.cs">
<Link>LineGraph.cs</Link>
</Compile>
@@ -95,6 +104,11 @@
<Generator>MSBuild:MarkupCompilePass1</Generator>
<SubType>Designer</SubType>
</Page>
+ <Page Include="..\QuickChartsWPF\Themes\Legend.xaml">
+ <Link>Themes\Legend.xaml</Link>
+ <Generator>MSBuild:MarkupCompilePass1</Generator>
+ <SubType>Designer</SubType>
+ </Page>
<Page Include="..\QuickChartsWPF\Themes\LineGraph.xaml">
<Link>Themes\LineGraph.xaml</Link>
<Generator>MSBuild:MarkupCompilePass1</Generator>
@@ -9,5 +9,6 @@
<ResourceDictionary Source="/AmCharts.Windows.QuickCharts.SL;component/CategoryAxis.xaml" />
<ResourceDictionary Source="/AmCharts.Windows.QuickCharts.SL;component/ColumnGraph.xaml" />
<ResourceDictionary Source="/AmCharts.Windows.QuickCharts.SL;component/LineGraph.xaml" />
+ <ResourceDictionary Source="/AmCharts.Windows.QuickCharts.SL;component/Legend.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
@@ -11,9 +11,9 @@
</Grid.RowDefinitions>
<amq:SerialChart DataSource="{Binding Data}" CategoryValuePath="cat1">
<amq:SerialChart.Graphs>
- <amq:LineGraph ValueMemberPath="val1" />
- <amq:ColumnGraph ValueMemberPath="val2" />
- <amq:LineGraph ValueMemberPath="val3" />
+ <amq:LineGraph ValueMemberPath="val1" Title="Line #1" />
+ <amq:ColumnGraph ValueMemberPath="val2" Title="Column #2" Brush="Aqua" />
+ <amq:LineGraph ValueMemberPath="val3" Title="Line #3" />
</amq:SerialChart.Graphs>
</amq:SerialChart>
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Windows.Media;
+
+namespace AmCharts.Windows.QuickCharts
+{
+ public interface ILegendItem
+ {
+ string Title { get; set; }
+ Brush Brush { get; set; }
+ }
+}
View
@@ -0,0 +1,75 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.Collections.Specialized;
+using System.Linq;
+using System.Text;
+using System.Windows;
+using System.Windows.Controls;
+
+namespace AmCharts.Windows.QuickCharts
+{
+ public class Legend : ItemsControl
+ {
+ public Legend()
+ {
+ this.DefaultStyleKey = typeof(Legend);
+
+ this.ItemsSource = _itemsSource;
+ }
+
+ private ObservableCollection<LegendItem> _itemsSource = new ObservableCollection<LegendItem>();
+
+ private IEnumerable<ILegendItem> _legendItemsSource;
+ public IEnumerable<ILegendItem> LegendItemsSource
+ {
+ get { return _legendItemsSource; }
+ set
+ {
+ if (value is INotifyCollectionChanged)
+ {
+ (value as INotifyCollectionChanged).CollectionChanged += new NotifyCollectionChangedEventHandler(LegendItemsSource_CollectionChanged);
+ }
+ _legendItemsSource = value;
+ _itemsSource.Clear();
+ AddLegendItems(value.ToList());
+ }
+ }
+
+ void LegendItemsSource_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
+ {
+ if (e.Action == NotifyCollectionChangedAction.Reset)
+ {
+ _itemsSource.Clear();
+ }
+ else
+ {
+ if (e.OldItems != null)
+ {
+ foreach (ILegendItem item in e.NewItems)
+ {
+ _itemsSource.Remove(_itemsSource.First(p => p.OriginalItem == item));
+ }
+ }
+
+ AddLegendItems(e.NewItems);
+ }
+ }
+
+ private void AddLegendItems(IList items)
+ {
+ if (items != null)
+ {
+ foreach (ILegendItem item in items)
+ {
+ _itemsSource.Add(new LegendItem() {
+ Title = item.Title,
+ Brush = item.Brush,
+ OriginalItem = item
+ });
+ }
+ }
+ }
+ }
+}
@@ -0,0 +1,28 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace AmCharts.Windows.QuickCharts
+{
+ public class LegendItem : ILegendItem
+ {
+ public string Title
+ {
+ get;
+ set;
+ }
+
+ public System.Windows.Media.Brush Brush
+ {
+ get;
+ set;
+ }
+
+ public ILegendItem OriginalItem
+ {
+ get;
+ set;
+ }
+ }
+}
@@ -69,6 +69,10 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
+ <Page Include="Themes\Legend.xaml">
+ <Generator>MSBuild:Compile</Generator>
+ <SubType>Designer</SubType>
+ </Page>
<Page Include="Themes\ValueGrid.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
@@ -96,6 +100,9 @@
<Compile Include="CategoryAxis.cs" />
<Compile Include="DataPathEventArgs.cs" />
<Compile Include="DiscreetClearObservableCollection.cs" />
+ <Compile Include="ILegendItem.cs" />
+ <Compile Include="Legend.cs" />
+ <Compile Include="LegendItem.cs" />
<Compile Include="LineGraph.cs" />
<Compile Include="Properties\AssemblyInfo.cs">
<SubType>Code</SubType>
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
+using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
@@ -45,7 +46,9 @@ void _graphs_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e
private void AddGraphToCanvas(SerialGraph graph)
{
if (_graphCanvas != null && !_graphCanvas.Children.Contains(graph))
+ {
_graphCanvas.Children.Add(graph);
+ }
}
private void RemoveGraphFromCanvas(SerialGraph graph)
@@ -83,6 +86,9 @@ public override void OnApplyTemplate()
_valueGrid = (ValueGrid)TreeHelper.TemplateFindName("PART_ValueGrid", this);
_categoryAxis = (CategoryAxis)TreeHelper.TemplateFindName("PART_CategoryAxis", this);
+
+ _legend = (Legend)TreeHelper.TemplateFindName("PART_Legend", this);
+ _legend.LegendItemsSource = this.Graphs.Cast<ILegendItem>(); // TODO: handle changes in Graphs
}
void _graphCanvasDecorator_SizeChanged(object sender, SizeChangedEventArgs e)
@@ -100,6 +106,8 @@ void _graphCanvasDecorator_SizeChanged(object sender, SizeChangedEventArgs e)
private ValueAxis _valueAxis;
private ValueGrid _valueGrid;
+ private Legend _legend;
+
public static readonly DependencyProperty DataSourceProperty = DependencyProperty.Register(
"DataSource", typeof(IEnumerable), typeof(SerialChart),
new PropertyMetadata(null, new PropertyChangedCallback(SerialChart.DataSourceProperty_Changed)));
@@ -8,7 +8,7 @@
namespace AmCharts.Windows.QuickCharts
{
- public abstract class SerialGraph : Control
+ public abstract class SerialGraph : Control, ILegendItem
{
public event EventHandler<DataPathEventArgs> ValueMemberPathChanged;
@@ -67,5 +67,26 @@ protected double XStep
}
}
+ public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(
+ "Title", typeof(string), typeof(SerialGraph),
+ new PropertyMetadata(null)
+ );
+
+ public string Title
+ {
+ get { return (string)GetValue(SerialGraph.TitleProperty); }
+ set { SetValue(SerialGraph.TitleProperty, value); }
+ }
+
+ public static readonly DependencyProperty BrushProperty = DependencyProperty.Register(
+ "Brush", typeof(Brush), typeof(SerialGraph),
+ new PropertyMetadata(null)
+ );
+
+ public Brush Brush
+ {
+ get { return (Brush)GetValue(SerialGraph.BrushProperty); }
+ set { SetValue(SerialGraph.BrushProperty, value); }
+ }
}
}
@@ -9,6 +9,7 @@
<ResourceDictionary Source="/AmCharts.Windows.QuickCharts;component/Themes/CategoryAxis.xaml" />
<ResourceDictionary Source="/AmCharts.Windows.QuickCharts;component/Themes/ColumnGraph.xaml" />
<ResourceDictionary Source="/AmCharts.Windows.QuickCharts;component/Themes/LineGraph.xaml" />
+ <ResourceDictionary Source="/AmCharts.Windows.QuickCharts;component/Themes/Legend.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
@@ -0,0 +1,38 @@
+<ResourceDictionary
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+ xmlns:amq="clr-namespace:AmCharts.Windows.QuickCharts"
+ >
+ <Style TargetType="amq:Legend">
+ <Setter Property="Template">
+ <Setter.Value>
+ <ControlTemplate TargetType="amq:Legend">
+ <Border>
+ <ItemsPresenter />
+ </Border>
+ </ControlTemplate>
+ </Setter.Value>
+ </Setter>
+
+ <!--<Setter Property="ItemsPanel">
+ <Setter.Value>
+ <ItemsPanelTemplate>
+ <StackPanel />
+ </ItemsPanelTemplate>
+ </Setter.Value>
+ </Setter>-->
+
+ <Setter Property="ItemTemplate">
+ <Setter.Value>
+ <DataTemplate>
+ <StackPanel Orientation="Horizontal">
+ <Rectangle Fill="{Binding Brush}" Height="10" Width="10" Margin="5" />
+ <TextBlock Text="{Binding Title}" VerticalAlignment="Center" />
+ </StackPanel>
+ </DataTemplate>
+ </Setter.Value>
+ </Setter>
+
+
+ </Style>
+</ResourceDictionary>
@@ -25,6 +25,8 @@
<Border x:Name="PART_GraphCanvasDecorator" Grid.Column="1" Grid.Row="0">
<Canvas x:Name="PART_GraphCanvas" />
</Border>
+
+ <amq:Legend x:Name="PART_Legend" Grid.Column="2" Grid.Row="0" />
</Grid>
</ControlTemplate>
</Setter.Value>

0 comments on commit 59eaace

Please sign in to comment.