1: static class ChartBehavior
2: {
3:
4: #region Line chart view
5: public static LineChartViewModel GetLineChartView(Visifire.Charts.Chart chart)
6: {
7: return (LineChartViewModel)chart.GetValue(LineChartViewProperty);
8: }
9: public static void SetLineChartView(Visifire.Charts.Chart chart, LineChartViewModel value)
10: {
11: chart.SetValue(LineChartViewProperty, value);
12: }
13:
14: public static readonly DependencyProperty LineChartViewProperty =
15: DependencyProperty.RegisterAttached(
16: "LineChartView",
17: typeof(LineChartViewModel),
18: typeof(ChartBehavior),
19: new UIPropertyMetadata(OnAutoSelectedChanged));
20: #endregion
21:
22: #region ListCategorie
23: public static readonly DependencyProperty ListCategorieProperty =
24: DependencyProperty.RegisterAttached(
25: "ListCategorie",
26: typeof(string),
27: typeof(ChartBehavior),
28: new UIPropertyMetadata(OnCategorieChanged));
29: public static string GetListCategorie(Visifire.Charts.Chart chart)
30: {
31: return (string)chart.GetValue(ListCategorieProperty);
32: }
33: public static void SetListCategorie(Visifire.Charts.Chart chart,
34: string value)
35: {
36: chart.SetValue(ListCategorieProperty, value);
37: }
38: static void OnCategorieChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
39: {
40: //Ottengo il sorgente
41: Visifire.Charts.Chart source = (sender as Visifire.Charts.Chart);
42: //Se nullo significa che la property non è attaccata al grafico
43: if (source == null) return;
44:
45: if (source.Tag == null)
46: source.Tag = new DataChart();
47:
48: DataChart dati = (DataChart)source.Tag;
49: dati.Categorie = (string)e.NewValue;
50:
51: UpdateChart(source);
52: }
53: #endregion
54:
55: #region Panel CheckBox
56: public static readonly DependencyProperty PanelCheckBoxProperty =
57: DependencyProperty.RegisterAttached(
58: "PanelCheckBox",
59: typeof(FrameworkElement),
60: typeof(ChartBehavior),
61: new UIPropertyMetadata(OnPanelCheckBoxChanged));
62:
63: public static FrameworkElement GetPanelCheckBox(Visifire.Charts.Chart chart)
64: {
65: return (FrameworkElement)chart.GetValue(PanelCheckBoxProperty);
66: }
67: public static void SetPanelCheckBox(Visifire.Charts.Chart chart,
68: FrameworkElement value)
69: {
70: chart.SetValue(PanelCheckBoxProperty, value);
71: }
72: static void OnPanelCheckBoxChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
73: {
74: Visifire.Charts.Chart source = (sender as Visifire.Charts.Chart);
75: if (source == null) return;
76:
77: if (source.Tag == null)
78: source.Tag = new DataChart();
79:
80: DataChart dati = (DataChart)source.Tag;
81: dati.PanelCheckBox = (FrameworkElement)e.NewValue;
82:
83: UpdateChart(source);
84: }
85: #endregion
86:
87: static void UpdateChart(Visifire.Charts.Chart source)
88: {
89: if (source == null) return;
90: if (source.Tag == null) return;
91:
92: DataChart dati = (DataChart)source.Tag;
93: if (dati.View == null) return;
94:
95: System.Windows.Controls.Panel pnl = null;
96: if (dati.PanelCheckBox != null )
97: {
98: pnl = (System.Windows.Controls.Panel)dati.PanelCheckBox;
99: if (pnl !=null)
100: pnl.Children.Clear();
101:
102: }
103:
104: if (dati.Categorie != null)
105: {
106: LineChartViewModel lcvm = dati.View;
107:
108: lcvm.Categorie = dati.Categorie;
109:
110: foreach (Visifire.Charts.DataSeries ds in lcvm.ChartSeries)
111: {
112: source.Series.Add(ds);
113: foreach (Visifire.Charts.DataPoint dp in ds.DataPoints)
114: {
115: dp.Chart = source;
116: dp.Bind();
117: }
118: if (pnl != null)
119: {
120: System.Windows.Controls.CheckBox cb = new System.Windows.Controls.CheckBox();
121: cb.Content = ds.LegendText;
122: ds.Tag = cb;
123: cb.Tag = ds;
124: cb.Checked += new RoutedEventHandler(cb_Checked);
125: cb.Unchecked += new RoutedEventHandler(cb_Unchecked);
126: cb.IsChecked = true;
127: pnl.Children.Add(cb);
128: }
129:
130: }
131: }
132: }
133:
134:
135: static void cb_Unchecked(object sender, RoutedEventArgs e)
136: {
137: System.Windows.Controls.CheckBox cb = (System.Windows.Controls.CheckBox)sender;
138: Visifire.Charts.DataSeries ds = (Visifire.Charts.DataSeries)cb.Tag;
139: ds.Visibility = Visibility.Collapsed;
140:
141: }
142:
143: static void cb_Checked(object sender, RoutedEventArgs e)
144: {
145: System.Windows.Controls.CheckBox cb = (System.Windows.Controls.CheckBox)sender;
146: Visifire.Charts.DataSeries ds = (Visifire.Charts.DataSeries)cb.Tag;
147: ds.Visibility = Visibility.Visible;
148: }
149:
150: static void OnAutoSelectedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
151: {
152: Visifire.Charts.Chart source = (sender as Visifire.Charts.Chart);
153: if (source == null) return;
154:
155: if (source.Tag == null)
156: source.Tag = new DataChart();
157:
158: DataChart dati = (DataChart)source.Tag;
159: LineChartViewModel lcvm = (LineChartViewModel)e.NewValue;
160: dati.View = lcvm;
161:
162: UpdateChart(source);
163: }
164:
165:
166:
167: public class DataChart
168: {
169: public string Categorie
170: {
171: get;set;
172: }
173:
174: public LineChartViewModel View
175: {
176: get;
177: set;
178: }
179:
180: public FrameworkElement PanelCheckBox
181: {
182: get;
183: set;
184: }
185: }
186: }