1:using System;
2:using System.Collections;
3:using System.Collections.Generic;
4:using System.Collections.ObjectModel;
5:using System.Drawing;
6:using System.Globalization;
7:using System.Linq;
8:using System.Windows.Forms;
9:using Infragistics.Controls.Charts;
10:using Infragistics.Extension.Models;
11:using Infragistics.Extension.Services;
12:using Infragistics.Win.DataVisualization;
13:using HorizontalAlignment = Infragistics.Portable.Components.UI.HorizontalAlignment;
14:
15:namespace Infragistics.Samples
16: {
17:publicpartialclass SampleForm : Form
18: {
19:#region Default constructor
20:public SampleForm()
21: {
22: InitializeComponent();
23: Load += OnFormLoaded;
24: btnReset.Click += btnReset_Click;
25: }
26:#endregion// Default constructor
27:
28:#region Protected Members
29:protected DataStockService StockService;
30:protected UltraDataChart PriceChart;
31:protected UltraDataChart VolumeChart;
32:#endregion// Protected Members
33:
34:#region Events
35:
36:#region OnFormLoaded
37:privatevoid OnFormLoaded(object sender, EventArgs e)
38: {
39: InitializeForm();
40:
41: StockService = new DataStockService();
42: StockService.DataStockReceived += StockService_DataStockReceived;
43:
44: var data = StockService.DataPoints;
45:
46: PriceChart = CreatePriceChart(data);
47: VolumeChart = CreateVolumeChart(data);
48:
49: var table = new TableLayoutPanel
50: {
51: Dock = DockStyle.Fill,
52: AutoSizeMode = AutoSizeMode.GrowOnly,
53: GrowStyle = TableLayoutPanelGrowStyle.AddRows
54: };
55: table.Controls.Add(PriceChart, 0, 1);
56: table.Controls.Add(VolumeChart, 0, 2);
57:
58: PanelCenter.ClientArea.Controls.Add(table);
59: }
60:#endregion// OnFormLoaded
61:
62:#region btnReset_Click
63:void btnReset_Click(object sender, EventArgs e)
64: {
65: PriceChart.ResetZoom();
66: VolumeChart.ResetZoom();
67: }
68:#endregion// btnReset_Click
69:
70:#region StockService_DataStockReceived
71:void StockService_DataStockReceived(object sender, DataStockReceivedEventArgs e)
72: {
73://var data = StockService.DataPoints;
74: var data = new DataStockList();
75: data.AddRange(StockService.DataPoints);
76:
77: UpdatePriceChart(data);
78: UpdateVolumeChart(data);
79: }
80:#endregion// StockService_DataStockReceived
81:
82:#region OnAxisXFormatLabel
83:string OnAxisXFormatLabel(AxisLabelInfo info)
84: {
85: var item = info.Item as DataStockPoint;
86:if (item != null) return item.Date.ToString("yyyy/MM/dd");
87:
88:return info.ToString();
89: }
90:#endregion// OnAxisXFormatLabel
91:
92:#region OnAxisYFormatLabel
93:string OnAxisYFormatLabel(AxisLabelInfo info)
94: {
95: var axisValue = info.Value;
96:if (axisValue < 1000)
97:returnstring.Format("{0:0.#}", axisValue);
98:if (axisValue < 1000000)
99:returnstring.Format("{0:#,0,.#} K", axisValue);
100:if (axisValue < 1000000000)
101:returnstring.Format("{0:#,0,,.#} M", axisValue);
102:if (axisValue < 1000000000000)
103:returnstring.Format("{0:#,0,,,.#} B", axisValue);
104:
105:return axisValue.ToString();
106: }
107:#endregion// OnAxisYFormatLabel
108:
109:#endregion
110:
111:#region Methods
112:
113:#region InitializeForm
114:publicvoid InitializeForm()
115: {
116: speedEditor.ValueChanged += speedEditor_ValueChanged;
117: btnStartService.Click += btnStartService_Click;
118:
119:// Add the form's caption as the sample name
120: ultraFormManager1.FormStyleSettings.Caption = Properties.Resources.SampleTitle;
121:
122:// Add button images on the form
123:this.LoadImagesFor(SplitterTop, SplitterRight);
124:
125:// Add the sample description
126: sampleInfo.SampleDescription = Resources.SamplesBrowser.SamplesBrowser.SampleDescription;
127: sampleInfo.SampleCodeCSharp = Properties.Resources.CS_CodeView;
128: sampleInfo.SampleCodeVBasic = Properties.Resources.VB_CodeView;
129: sampleInfo.SampleTitle = Properties.Resources.CodeViewerTitle;
130:
131: speedEditor.PropertyName = Properties.Resources.SpeedEditor_Label;
132: }
133:#endregion// InitializeForm
134:
135:#region CreatePriceChart
136:public UltraDataChart CreatePriceChart(DataStockList data)
137: {
138: var chart = new UltraDataChart
139: {
140: Width = 400,
141: Height = 200,
142: Dock = DockStyle.Fill,
143: BackColor = System.Drawing.Color.White,
144: PlotAreaBackground = new SolidColorBrush { Color = Color.White },
145: Title = data.CompanyName,
146: Subtitle = Properties.Resources.StockPrice,
147: VerticalZoomable = true,
148: HorizontalZoomable = true
149: };
150:
151: var xAxis = new CategoryXAxis
152: {
153: Label = "Date",
154: DataSource = data,
155: LabelLocation = AxisLabelsLocation.OutsideBottom,
156: UseClusteringMode = true
157: };
158: xAxis.FormatLabel += OnAxisXFormatLabel;
159:
160: var yAxis = new NumericYAxis
161: {
162: LabelExtent = 50,
163: LabelHorizontalAlignment = HorizontalAlignment.Right
164: };
165: yAxis.FormatLabel += OnAxisYFormatLabel;
166:
167: var yAxis2 = new NumericYAxis
168: {
169: LabelExtent = 20,
170: LabelLocation = AxisLabelsLocation.OutsideRight
171: };
172:
173: var series = new AreaSeries
174: {
175: DataSource = data,
176: ValueMemberPath = "Open",
177: XAxis = xAxis,
178: YAxis = yAxis,
179: MarkerType = MarkerType.None,
180: IsHighlightingEnabled = true
181: };
182:
183: chart.Axes.Add(xAxis);
184: chart.Axes.Add(yAxis);
185: chart.Axes.Add(yAxis2);
186: chart.Series.Add(series);
187:return chart;
188: }
189:#endregion// CreatePriceChart
190:
191:#region CreateVolumeChart
192:public UltraDataChart CreateVolumeChart(DataStockList data)
193: {
194: var chart = new UltraDataChart
195: {
196: BackColor = System.Drawing.Color.White,
197: PlotAreaBackground = new SolidColorBrush { Color = Color.White },
198: Dock = DockStyle.Fill,
199: Width = 400,
200: Height = 200,
201: Padding = new Padding(0, 0, 20, 0),
202: Subtitle = Properties.Resources.StockVolume,
203: VerticalZoomable = true,
204: HorizontalZoomable = true
205: };
206:
207: var xAxis = new CategoryXAxis
208: {
209: Label = "Date",
210: DataSource = data,
211: LabelLocation = AxisLabelsLocation.OutsideBottom
212: };
213: xAxis.FormatLabel += OnAxisXFormatLabel;
214:
215: var yAxis = new NumericYAxis
216: {
217: LabelExtent = 50,
218: LabelHorizontalAlignment = HorizontalAlignment.Right
219: };
220: yAxis.FormatLabel += OnAxisYFormatLabel;
221:
222: var yAxis2 = new NumericYAxis
223: {
224: LabelExtent = 20,
225: LabelLocation = AxisLabelsLocation.OutsideRight
226: };
227:
228: var series = new ColumnSeries
229: {
230: DataSource = data,
231: ValueMemberPath = "Volume",
232: XAxis = xAxis,
233: YAxis = yAxis,
234: IsHighlightingEnabled = true,
235: IsTransitionInEnabled = false
236: };
237:
238: chart.Axes.Add(xAxis);
239: chart.Axes.Add(yAxis);
240: chart.Axes.Add(yAxis2);
241: chart.Series.Add(series);
242:return chart;
243: }
244:#endregion// CreateVolumeChart
245:
246:#region UpdatePriceChart
247:privatevoid UpdatePriceChart(IEnumerable data)
248: {
249: var DataSource = data asobject[] ?? data.Cast<object>().ToArray();
250:
251: var series = PriceChart.Series.FirstOrDefault();
252:if (series != null)
253: {
254: series.DataSource = DataSource;
255: series.RenderSeries(false);
256: }
257:
258: var xAxis = PriceChart.Axes[0] as CategoryXAxis;
259:if (xAxis != null)
260: {
261: xAxis.DataSource = DataSource;
262: xAxis.RenderAxis();
263: }
264: }
265:#endregion// UpdatePriceChart
266:
267:#region UpdateVolumeChart
268:privatevoid UpdateVolumeChart(IEnumerable data)
269: {
270: var DataSource = data asobject[] ?? data.Cast<object>().ToArray();
271:
272: var series = VolumeChart.Series.FirstOrDefault();
273:if (series != null)
274: {
275: series.DataSource = DataSource;
276: series.RenderSeries(false);
277: }
278:
279: var xAxis = VolumeChart.Axes[0] as CategoryXAxis;
280:if (xAxis != null)
281: {
282: xAxis.DataSource = DataSource;
283: xAxis.RenderAxis();
284: }
285: }
286:#endregion// UpdateVolumeChart
287:
288:#region speedEditor_ValueChanged
289:void speedEditor_ValueChanged(object sender, EventArgs e)
290: {
291: var value = speedEditor.PropertyValue;
292: StockService.UpdateInterval = TimeSpan.FromMilliseconds(value);
293: }
294:#endregion// speedEditor_ValueChanged
295:
296:#region btnStartService_Click
297:void btnStartService_Click(object sender, EventArgs e)
298: {
299:if (StockService.IsEnabled)
300: StockService.Stop();
301:else
302: StockService.Start();
303: }
304:#endregion// btnStartService_Click
305:
306:#endregion// Methods
307: }
308:
309:#region StockInformation class
310:publicclass StockInformation
311: {
312:publicstring CompanyName;
313:publicstring StockSymbol;
314: }
315:#endregion// StockInformation class
316: }