WinChartDesigner.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. //
  5. // Purpose: Windows forms chart control designer class.
  6. //
  7. #if DESIGNER
  8. using Microsoft.Win32;
  9. using System.Collections;
  10. using System.ComponentModel;
  11. using System.ComponentModel.Design;
  12. using FastReport.DataVisualization.Charting;
  13. namespace FastReport.Design.DataVisualization.Charting
  14. {
  15. /// <summary>
  16. /// Chart windows forms control designer
  17. /// </summary>
  18. internal class ChartWinDesigner : ControlDesigner
  19. {
  20. #region Fields
  21. // Reference to the chart designer
  22. static internal ChartWinDesigner controlDesigner = null;
  23. #endregion
  24. #region Methods
  25. /// <summary>
  26. /// Intialize designer.
  27. /// </summary>
  28. /// <param name="component">Component.</param>
  29. public override void Initialize(IComponent component)
  30. {
  31. // remove default verbs from the action list.
  32. // should be called before initialization.
  33. IServiceContainer svc = component.Site as IServiceContainer;
  34. //if (svc != null)
  35. //{
  36. // if (this.GetService(typeof(DesignerCommandSet)) == null)
  37. // {
  38. // svc.AddService(typeof(DesignerCommandSet), new DesignerCommandSet());
  39. // }
  40. // svc.AddService(typeof(IDesignerMessageBoxDialog), new DesignerMessageBoxDialog());
  41. //}
  42. base.Initialize(component);
  43. // Set reference to the designer
  44. ChartWinDesigner.controlDesigner = this;
  45. SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler(SystemEvents_UserPreferenceChanged);
  46. }
  47. protected override void OnMouseDragBegin(int x, int y)
  48. {
  49. base.OnMouseDragBegin(x, y);
  50. ChartWinDesigner.controlDesigner = this;
  51. }
  52. /// <summary>
  53. /// Set default values for properties of the component.
  54. /// NOTE: Replaces obsolete method: OnSetComponentDefaults()
  55. /// </summary>
  56. /// <param name="defaultValues">Default values property bags.</param>
  57. public override void InitializeNewComponent(IDictionary defaultValues)
  58. {
  59. if (Control != null && Control is Chart)
  60. {
  61. Chart chart = (Chart)Control;
  62. // If control is not initialized
  63. if (chart.ChartAreas.Count == 0 &&
  64. chart.Series.Count == 0)
  65. {
  66. // Add Default chart area
  67. chart.ChartAreas.Add(new ChartArea());
  68. // Add Default series
  69. chart.Series.Add(new Series());
  70. chart.Legends.Add(new Legend());
  71. }
  72. }
  73. base.InitializeNewComponent(defaultValues);
  74. }
  75. /// <summary>
  76. /// Releases unmanaged and - optionally - managed resources
  77. /// </summary>
  78. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  79. protected override void Dispose(bool disposing)
  80. {
  81. if (disposing)
  82. {
  83. //Free managed resources
  84. SystemEvents.UserPreferenceChanged -= SystemEvents_UserPreferenceChanged;
  85. }
  86. base.Dispose(disposing);
  87. }
  88. /// <summary>
  89. /// User changed Windows preferences
  90. /// </summary>
  91. /// <param name="sender">The sender.</param>
  92. /// <param name="e">Event arguments.</param>
  93. private void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
  94. {
  95. // If user changed system colors, make Chart repaint itself.
  96. if (e.Category == UserPreferenceCategory.Color)
  97. Control.Invalidate();
  98. }
  99. #endregion
  100. #region Data Binding
  101. /// <summary>
  102. /// Data source was changed.
  103. /// </summary>
  104. /// <param name="chartControl">Reference to the chart control.</param>
  105. internal static void OnDataSourceChanged(Chart chartControl)
  106. {
  107. if (chartControl != null)
  108. {
  109. // Clear all value members properies in the series
  110. foreach (Series series in chartControl.Series)
  111. {
  112. series.XValueMember = String.Empty;
  113. series.YValueMembers = String.Empty;
  114. }
  115. }
  116. }
  117. /// <summary>
  118. /// Gets selected data source object.
  119. /// </summary>
  120. public object GetControlDataSource()
  121. {
  122. object selectedDataSource = null;
  123. if (this.Control != null && this.Control is Chart)
  124. {
  125. selectedDataSource = this.GetControlDataSource((Chart)this.Control);
  126. }
  127. return selectedDataSource;
  128. }
  129. /// <summary>
  130. /// Gets selected data source object.
  131. /// </summary>
  132. /// <param name="chart">Chart control.</param>
  133. /// <returns>Data source.</returns>
  134. internal object GetControlDataSource(Chart chart)
  135. {
  136. object selectedDataSource = null;
  137. if (chart != null)
  138. {
  139. if (chart.DataSource != null)
  140. {
  141. object dataSourceObject = chart.DataSource;
  142. string fieldName = dataSourceObject as string;
  143. if (fieldName != null && this.Component != null)
  144. {
  145. dataSourceObject = null;
  146. ISite componentSite = this.Component.Site;
  147. if (componentSite != null)
  148. {
  149. IContainer container = (IContainer)componentSite.GetService(typeof(IContainer));
  150. if (container != null)
  151. {
  152. dataSourceObject = container.Components[fieldName];
  153. }
  154. }
  155. }
  156. if (dataSourceObject != null && ChartImage.IsValidDataSource(dataSourceObject))
  157. {
  158. selectedDataSource = dataSourceObject;
  159. }
  160. }
  161. }
  162. return selectedDataSource;
  163. }
  164. #endregion //DataBinding
  165. }
  166. }
  167. #endif