Skip to content
This repository has been archived by the owner on Jul 4, 2023. It is now read-only.

Commit

Permalink
Implemented core Legend functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ailon committed Jan 22, 2010
1 parent f729348 commit 59eaace
Show file tree
Hide file tree
Showing 13 changed files with 217 additions and 8 deletions.
8 changes: 4 additions & 4 deletions QuickCharts/Window1.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
14 changes: 14 additions & 0 deletions QuickChartsSL/QuickChartsSL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
1 change: 1 addition & 0 deletions QuickChartsSL/Themes/Generic.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
6 changes: 3 additions & 3 deletions QuickChartsSLApp/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
14 changes: 14 additions & 0 deletions QuickChartsWPF/ILegendItem.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
75 changes: 75 additions & 0 deletions QuickChartsWPF/Legend.cs
Original file line number Diff line number Diff line change
@@ -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
});
}
}
}
}
}
28 changes: 28 additions & 0 deletions QuickChartsWPF/LegendItem.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
7 changes: 7 additions & 0 deletions QuickChartsWPF/QuickChartsWPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
8 changes: 8 additions & 0 deletions QuickChartsWPF/SerialChart.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)));
Expand Down
23 changes: 22 additions & 1 deletion QuickChartsWPF/SerialGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace AmCharts.Windows.QuickCharts
{
public abstract class SerialGraph : Control
public abstract class SerialGraph : Control, ILegendItem
{
public event EventHandler<DataPathEventArgs> ValueMemberPathChanged;

Expand Down Expand Up @@ -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); }
}
}
}
1 change: 1 addition & 0 deletions QuickChartsWPF/Themes/Generic.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
38 changes: 38 additions & 0 deletions QuickChartsWPF/Themes/Legend.xaml
Original file line number Diff line number Diff line change
@@ -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>
2 changes: 2 additions & 0 deletions QuickChartsWPF/Themes/SerialChart.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 59eaace

Please sign in to comment.