Index: Core/Components/src/Core.Components.OxyPlot.Forms/StackChartControl.cs
===================================================================
diff -u -r9158f31e4f26c19087e171520a95de64837e457a -r03b624caf889c8ad42738b3b8926b47306be0990
--- Core/Components/src/Core.Components.OxyPlot.Forms/StackChartControl.cs (.../StackChartControl.cs) (revision 9158f31e4f26c19087e171520a95de64837e457a)
+++ Core/Components/src/Core.Components.OxyPlot.Forms/StackChartControl.cs (.../StackChartControl.cs) (revision 03b624caf889c8ad42738b3b8926b47306be0990)
@@ -21,8 +21,8 @@
using System.Collections.Generic;
using System.Drawing;
-using System.Linq;
using System.Windows.Forms;
+using Core.Common.Base;
using Core.Components.OxyPlot.DataSeries.Stack;
using Core.Components.Stack.Data;
using Core.Components.Stack.Forms;
@@ -37,6 +37,8 @@
private readonly CategoryPlotView plotView;
private StackChartData data;
+ private Observer stackChartDataObserver;
+
///
/// Creates a new .
///
@@ -52,6 +54,8 @@
};
Controls.Add(plotView);
+
+ stackChartDataObserver = new Observer(HandleStackChartDataChange);
}
public StackChartData Data
@@ -64,16 +68,15 @@
{
if (data != null)
{
- plotView.ClearLabels();
- plotView.Model.Series.Clear();
+ ClearPlot();
}
data = value;
+ stackChartDataObserver.Observable = data;
if (data != null)
{
- AddLabels();
- DrawColumns();
+ DrawPlot();
}
}
}
@@ -93,12 +96,31 @@
protected override void Dispose(bool disposing)
{
plotView.Dispose();
+ stackChartDataObserver.Dispose();
base.Dispose(disposing);
}
- private void AddLabels()
+ private void HandleStackChartDataChange()
{
+ ClearPlot();
+ DrawPlot();
+ }
+
+ private void DrawPlot()
+ {
+ DrawLabels();
+ DrawColumns();
+ }
+
+ private void ClearPlot()
+ {
+ plotView.ClearLabels();
+ plotView.Model.Series.Clear();
+ }
+
+ private void DrawLabels()
+ {
plotView.AddLabels(data.Columns);
}
Index: Core/Components/test/Core.Components.OxyPlot.Forms.Test/StackChartControlTest.cs
===================================================================
diff -u -r5546276dfbf78b807ade6ffcb6d1e4cbd48f0310 -r03b624caf889c8ad42738b3b8926b47306be0990
--- Core/Components/test/Core.Components.OxyPlot.Forms.Test/StackChartControlTest.cs (.../StackChartControlTest.cs) (revision 5546276dfbf78b807ade6ffcb6d1e4cbd48f0310)
+++ Core/Components/test/Core.Components.OxyPlot.Forms.Test/StackChartControlTest.cs (.../StackChartControlTest.cs) (revision 03b624caf889c8ad42738b3b8926b47306be0990)
@@ -103,6 +103,7 @@
chart.Data = data;
+ // Precondition
CategoryPlotView plotView = chart.Controls.OfType().First();
AssertSeriesAndColumns(plotView);
@@ -158,6 +159,7 @@
chart.Data = data;
+ // Precondition
CategoryPlotView plotView = chart.Controls.OfType().First();
AssertSeriesAndColumns(plotView);
@@ -175,6 +177,61 @@
}
[Test]
+ public void GivenChartControlWithData_WhenDataNotified_ThenChartControlUpdated()
+ {
+ // Given
+ using (var chart = new StackChartControl())
+ {
+ var data = new StackChartData();
+ data.AddColumn("Column 1");
+ data.AddColumn("Column 2");
+ data.AddRow("Row 1", new List
+ {
+ 0.4,
+ 0.2
+ });
+ data.AddRow("Row 2", new List
+ {
+ 0.6,
+ 0.8
+ });
+
+ chart.Data = data;
+
+ CategoryPlotView plotView = chart.Controls.OfType().First();
+ AssertSeriesAndColumns(plotView);
+
+ // When
+ data.Clear();
+ data.AddColumn("New column 1");
+ data.AddColumn("New column 2");
+ data.AddRow("New row 1", new List
+ {
+ 0.3,
+ 0.8
+ });
+ data.AddRow("New row 2", new List
+ {
+ 0.8,
+ 0.2
+ });
+ data.NotifyObservers();
+
+ // Then
+ ElementCollection series = plotView.Model.Series;
+ Assert.AreEqual(2, series.Count);
+ Assert.AreEqual("New row 1", series[0].Title);
+ Assert.AreEqual("New row 2", series[1].Title);
+
+ CategoryAxis axis = plotView.Model.Axes.OfType().First();
+
+ Assert.AreEqual(2, axis.Labels.Count);
+ Assert.AreEqual("New column 1", axis.Labels[0]);
+ Assert.AreEqual("New column 2", axis.Labels[1]);
+ }
+ }
+
+ [Test]
[TestCase("Title")]
[TestCase("Test")]
[TestCase("Label")]