AnnotationDesign.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. //-------------------------------------------------------------
  2. // <copyright company=’Microsoft Corporation’>
  3. // Copyright © Microsoft Corporation. All Rights Reserved.
  4. // </copyright>
  5. //-------------------------------------------------------------
  6. // @owner=alexgor, deliant
  7. //=================================================================
  8. // File: AnnotationDesign.cs
  9. //
  10. // Namespace: System.Web.UI.WebControls[Windows.Forms].Charting
  11. //
  12. // Classes: AnnotationCollectionEditor, AnchorPointUITypeEditor,
  13. // AnchorPointNameTreeView, AnchorPointValueConverter,
  14. // AnnotationAxisUITypeEditor, AnnotationAxisNameTreeView,
  15. // AnnotationAxisValueConverter
  16. //
  17. // Purpose: Annotation design-time support classes.
  18. //
  19. // Reviewed:
  20. //
  21. //===================================================================
  22. #region Used namespace
  23. using System;
  24. using System.Globalization;
  25. using System.Collections;
  26. using System.Collections.Specialized;
  27. using System.ComponentModel;
  28. using System.ComponentModel.Design;
  29. using System.Data;
  30. using System.Drawing;
  31. using System.Drawing.Design;
  32. using System.Drawing.Text;
  33. using System.Drawing.Drawing2D;
  34. using System.Windows.Forms;
  35. using FastReport.Design;
  36. using FastReport.DataVisualization.Charting;
  37. using FastReport.DataVisualization.Charting.Data;
  38. using FastReport.DataVisualization.Charting.ChartTypes;
  39. using FastReport.DataVisualization.Charting.Utilities;
  40. using FastReport.DataVisualization.Charting.Borders3D;
  41. //using FastReport.DataVisualization.Charting.Design;
  42. #if VS_DESIGN_TIME
  43. using System.Web.UI.Design;
  44. using System.Web.UI.WebControls.Charting.Design;
  45. using System.Web.UI.WebControls.Charting.Data;
  46. using System.Web.UI.WebControls.Charting.Utilities;
  47. #endif
  48. #endregion
  49. #if WINFORMS_CONTROL
  50. namespace System.Windows.Forms.Charting.Design
  51. #else
  52. namespace System.Web.UI.WebControls.Charting.Design
  53. #endif
  54. {
  55. #if !SMART_CLIENT
  56. #if VS_DESIGN_TIME
  57. /// <summary>
  58. /// Designer editor for the Animation Collection collection.
  59. /// </summary>
  60. [
  61. SRDescription("DescriptionAttributeAnnotationCollectionEditor_AnnotationCollectionEditor")
  62. ]
  63. public class AnnotationCollectionEditor : ChartCollectionEditor
  64. {
  65. #region Methods
  66. /// <summary>
  67. /// Object constructor.
  68. /// </summary>
  69. public AnnotationCollectionEditor() : base(typeof(AnnotationCollection))
  70. {
  71. }
  72. /// <summary>
  73. /// Gets the data types that this collection editor can contain.
  74. /// </summary>
  75. /// <returns>An array of data types that this collection can contain.</returns>
  76. protected override Type[] CreateNewItemTypes()
  77. {
  78. return new Type[] {
  79. typeof(LineAnnotation),
  80. typeof(VerticalLineAnnotation),
  81. typeof(HorizontalLineAnnotation),
  82. typeof(TextAnnotation),
  83. typeof(RectangleAnnotation),
  84. typeof(EllipseAnnotation),
  85. typeof(ArrowAnnotation),
  86. typeof(Border3DAnnotation),
  87. typeof(CalloutAnnotation),
  88. typeof(PolylineAnnotation),
  89. typeof(PolygonAnnotation),
  90. typeof(ImageAnnotation),
  91. typeof(AnnotationGroup)
  92. };
  93. }
  94. /// <summary>
  95. /// Create annotation instance in the editor
  96. /// </summary>
  97. /// <param name="itemType">Item type.</param>
  98. /// <returns>Newly created item.</returns>
  99. protected override object CreateInstance(Type itemType)
  100. {
  101. Chart control = (Chart)GetChartReference(Context.Instance);
  102. // Call base class
  103. Annotation annotation = base.CreateInstance(itemType) as Annotation;
  104. // Generate unique name
  105. if(control != null)
  106. {
  107. control.Annotations.AssignUniqueName(annotation);
  108. }
  109. return annotation;
  110. }
  111. #endregion // Methods
  112. }
  113. /// <summary>
  114. /// UI type editor for the annotation anchor point
  115. /// </summary>
  116. public class AnchorPointUITypeEditor : System.Drawing.Design.UITypeEditor
  117. {
  118. #region Editor methods and properties
  119. /// <summary>
  120. /// Display a dropdown list with check boxes.
  121. /// </summary>
  122. /// <param name="context">Editing context.</param>
  123. /// <param name="provider">Provider.</param>
  124. /// <param name="value">Value to edit.</param>
  125. /// <returns>Result</returns>
  126. public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
  127. {
  128. if (context != null && context.Instance != null && provider != null)
  129. {
  130. IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
  131. if (edSvc != null &&
  132. context.Instance is Annotation)
  133. {
  134. // Create control for editing
  135. AnchorPointNameTreeView control = new AnchorPointNameTreeView(
  136. edSvc,
  137. (Annotation)context.Instance,
  138. value as DataPoint);
  139. // Show drop down control
  140. edSvc.DropDownControl(control);
  141. // Get new enumeration value
  142. value = control.GetNewValue();
  143. // Dispose control
  144. control.Dispose();
  145. }
  146. }
  147. return value;
  148. }
  149. /// <summary>
  150. /// Gets editing style.
  151. /// </summary>
  152. /// <param name="context">Editing context.</param>
  153. /// <returns>Editor style.</returns>
  154. public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
  155. {
  156. if (context != null && context.Instance != null)
  157. {
  158. return UITypeEditorEditStyle.DropDown;
  159. }
  160. return base.GetEditStyle(context);
  161. }
  162. #endregion
  163. }
  164. /// <summary>
  165. /// Anchor data points name tree view, which is used for the UI type editing.
  166. /// </summary>
  167. internal class AnchorPointNameTreeView : System.Windows.Forms.TreeView
  168. {
  169. #region Control fields
  170. // Annotation object to edit
  171. private Annotation annotation = null;
  172. private DataPoint dataPoint = null;
  173. IWindowsFormsEditorService edSvc = null;
  174. #endregion
  175. #region Control constructor
  176. /// <summary>
  177. /// Public constructor.
  178. /// </summary>
  179. /// <param name="edSvc">Editor service.</param>
  180. /// <param name="annotation">Annotation to edit.</param>
  181. /// <param name="dataPoint">Annotation anchor data point to edit.</param>
  182. public AnchorPointNameTreeView(
  183. IWindowsFormsEditorService edSvc,
  184. Annotation annotation,
  185. DataPoint dataPoint)
  186. {
  187. // Set editable value
  188. this.annotation = annotation;
  189. this.dataPoint = dataPoint;
  190. this.edSvc = edSvc;
  191. // Set control border style
  192. this.BorderStyle = System.Windows.Forms.BorderStyle.None;
  193. // Fill tree with data point names
  194. this.FillTree();
  195. }
  196. #endregion
  197. #region Control methods
  198. /// <summary>
  199. /// Fills data points name tree.
  200. /// </summary>
  201. private void FillTree()
  202. {
  203. bool nodeSelected = false;
  204. this.BeginUpdate();
  205. // Add "None" option
  206. System.Windows.Forms.TreeNode noPoint = this.Nodes.Add(Constants.NotSetValue);
  207. // Get chart object
  208. if(this.annotation != null &&
  209. annotation.GetAnnotationGroup() == null &&
  210. this.annotation.Chart != null)
  211. {
  212. Chart chart = this.annotation.Chart;
  213. // Loop through all series
  214. foreach(Series series in chart.Series)
  215. {
  216. System.Windows.Forms.TreeNode seriesNode = this.Nodes.Add(series.Name);
  217. seriesNode.Tag = series;
  218. // Indicate that there are no points in series
  219. if(series.Points.Count == 0)
  220. {
  221. System.Windows.Forms.TreeNode noPointNode = seriesNode.Nodes.Add("(empty)");
  222. }
  223. // Loop through all points
  224. int index = 1;
  225. foreach(DataPoint point in series.Points)
  226. {
  227. System.Windows.Forms.TreeNode dataPointNode = seriesNode.Nodes.Add("DataPoint" + index.ToString(CultureInfo.InvariantCulture));
  228. dataPointNode.Tag = point;
  229. ++index;
  230. // Check if this node should be selected
  231. if(point == dataPoint)
  232. {
  233. seriesNode.Expand();
  234. this.SelectedNode = dataPointNode;
  235. nodeSelected = true;
  236. }
  237. }
  238. }
  239. }
  240. // Select default node
  241. if(!nodeSelected)
  242. {
  243. this.SelectedNode = noPoint;
  244. }
  245. this.EndUpdate();
  246. }
  247. /// <summary>
  248. /// Gets new data point.
  249. /// </summary>
  250. /// <returns>New enum value.</returns>
  251. public DataPoint GetNewValue()
  252. {
  253. if(this.SelectedNode != null &&
  254. this.SelectedNode.Tag != null &&
  255. this.SelectedNode.Tag is DataPoint)
  256. {
  257. return (DataPoint)this.SelectedNode.Tag;
  258. }
  259. return null;
  260. }
  261. /// <summary>
  262. /// Mouse double clicked.
  263. /// </summary>
  264. protected override void OnDoubleClick(EventArgs e)
  265. {
  266. base.OnDoubleClick(e);
  267. if(this.edSvc != null)
  268. {
  269. if(GetNewValue() != null)
  270. {
  271. this.edSvc.CloseDropDown();
  272. }
  273. else if(this.SelectedNode != null &&
  274. this.SelectedNode.Text == Constants.NotSetValue)
  275. {
  276. this.edSvc.CloseDropDown();
  277. }
  278. }
  279. }
  280. #endregion
  281. }
  282. /// <summary>
  283. /// UI type editor for the annotation axes.
  284. /// </summary>
  285. public class AnnotationAxisUITypeEditor : System.Drawing.Design.UITypeEditor
  286. {
  287. #region Editor methods and properties
  288. /// <summary>
  289. /// Display a dropdown list with check boxes.
  290. /// </summary>
  291. /// <param name="context">Editing context.</param>
  292. /// <param name="provider">Provider.</param>
  293. /// <param name="value">Value to edit.</param>
  294. /// <returns>Result</returns>
  295. public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
  296. {
  297. if (context != null && context.Instance != null && provider != null)
  298. {
  299. IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
  300. if (edSvc != null &&
  301. context.Instance is Annotation)
  302. {
  303. // Check if we dealing with X or Y axis
  304. bool showXAxes = true;
  305. if(context.PropertyDescriptor != null &&
  306. context.PropertyDescriptor.Name == "AxisY")
  307. {
  308. showXAxes = false;
  309. }
  310. // Create control for editing
  311. AnnotationAxisNameTreeView control = new AnnotationAxisNameTreeView(
  312. edSvc,
  313. (Annotation)context.Instance,
  314. value as Axis,
  315. showXAxes);
  316. // Show drop down control
  317. edSvc.DropDownControl(control);
  318. // Get new enumeration value
  319. value = control.GetNewValue();
  320. // Dispose control
  321. control.Dispose();
  322. }
  323. }
  324. return value;
  325. }
  326. /// <summary>
  327. /// Gets editing style.
  328. /// </summary>
  329. /// <param name="context">Editing context.</param>
  330. /// <returns>Editor style.</returns>
  331. public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
  332. {
  333. if (context != null && context.Instance != null)
  334. {
  335. return UITypeEditorEditStyle.DropDown;
  336. }
  337. return base.GetEditStyle(context);
  338. }
  339. #endregion
  340. }
  341. /// <summary>
  342. /// Annotation axes names tree view, which is used for the UI type editing.
  343. /// </summary>
  344. internal class AnnotationAxisNameTreeView : System.Windows.Forms.TreeView
  345. {
  346. #region Control fields
  347. // Annotation object to edit
  348. private Annotation annotation = null;
  349. private Axis axis = null;
  350. IWindowsFormsEditorService edSvc = null;
  351. private bool showXAxes = true;
  352. #endregion
  353. #region Control constructor
  354. /// <summary>
  355. /// Public constructor.
  356. /// </summary>
  357. /// <param name="edSvc">Editor service.</param>
  358. /// <param name="annotation">Annotation to edit.</param>
  359. /// <param name="axis">Axis object.</param>
  360. /// <param name="showXAxes">Indicates if X or Y axis should be shown.</param>
  361. public AnnotationAxisNameTreeView(
  362. IWindowsFormsEditorService edSvc,
  363. Annotation annotation,
  364. Axis axis,
  365. bool showXAxes)
  366. {
  367. // Set editable value
  368. this.annotation = annotation;
  369. this.axis = axis;
  370. this.edSvc = edSvc;
  371. this.showXAxes = showXAxes;
  372. // Set control border style
  373. this.BorderStyle = System.Windows.Forms.BorderStyle.None;
  374. // Fill tree with data point names
  375. this.FillTree();
  376. }
  377. #endregion
  378. #region Control methods
  379. /// <summary>
  380. /// Fills data points name tree.
  381. /// </summary>
  382. private void FillTree()
  383. {
  384. bool nodeSelected = false;
  385. this.BeginUpdate();
  386. // Add "None" option
  387. System.Windows.Forms.TreeNode noPoint = this.Nodes.Add(Constants.NotSetValue);
  388. // Get chart object
  389. if(this.annotation != null &&
  390. annotation.GetAnnotationGroup() == null &&
  391. this.annotation.Chart != null)
  392. {
  393. Chart chart = this.annotation.Chart;
  394. // Loop through all chart areas
  395. foreach(ChartArea chartArea in chart.ChartAreas)
  396. {
  397. System.Windows.Forms.TreeNode areaNode = this.Nodes.Add(chartArea.Name);
  398. areaNode.Tag = chartArea;
  399. // Loop through all axes
  400. foreach(Axis curAxis in chartArea.Axes)
  401. {
  402. // Hide X or Y axes
  403. if(curAxis.Type == AxisName.Y || curAxis.Type == AxisName.Y2)
  404. {
  405. if(showXAxes)
  406. {
  407. continue;
  408. }
  409. }
  410. if(curAxis.Type == AxisName.X || curAxis.Type == AxisName.X2)
  411. {
  412. if(!showXAxes)
  413. {
  414. continue;
  415. }
  416. }
  417. // Create child node
  418. System.Windows.Forms.TreeNode axisNode = areaNode.Nodes.Add(curAxis.Name);
  419. axisNode.Tag = curAxis;
  420. // Check if this node should be selected
  421. if(axis == curAxis)
  422. {
  423. areaNode.Expand();
  424. this.SelectedNode = axisNode;
  425. nodeSelected = true;
  426. }
  427. }
  428. }
  429. }
  430. // Select default node
  431. if(!nodeSelected)
  432. {
  433. this.SelectedNode = noPoint;
  434. }
  435. this.EndUpdate();
  436. }
  437. /// <summary>
  438. /// Gets a new data point.
  439. /// </summary>
  440. /// <returns>New enum value.</returns>
  441. public Axis GetNewValue()
  442. {
  443. if(this.SelectedNode != null &&
  444. this.SelectedNode.Tag != null &&
  445. this.SelectedNode.Tag is Axis)
  446. {
  447. return (Axis)this.SelectedNode.Tag;
  448. }
  449. return null;
  450. }
  451. /// <summary>
  452. /// Mouse double clicked.
  453. /// </summary>
  454. protected override void OnDoubleClick(EventArgs e)
  455. {
  456. base.OnDoubleClick(e);
  457. if(this.edSvc != null)
  458. {
  459. if(GetNewValue() != null)
  460. {
  461. this.edSvc.CloseDropDown();
  462. }
  463. else if(this.SelectedNode != null &&
  464. this.SelectedNode.Text == Constants.NotSetValue)
  465. {
  466. this.edSvc.CloseDropDown();
  467. }
  468. }
  469. }
  470. #endregion
  471. }
  472. #endif // VS_DESIGN_TIME
  473. #endif // !SMART_CLIENT
  474. /// <summary>
  475. /// Converts anchor data point to string name.
  476. /// </summary>
  477. public class AnchorPointValueConverter : TypeConverter
  478. {
  479. #region Converter methods
  480. /// <summary>
  481. /// Converts anchor data point to string name.
  482. /// </summary>
  483. /// <param name="context">Descriptor context.</param>
  484. /// <param name="culture">Culture information.</param>
  485. /// <param name="value">Value to convert.</param>
  486. /// <param name="destinationType">Conversion destination type.</param>
  487. /// <returns>Converted object.</returns>
  488. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  489. {
  490. if (destinationType == typeof(string))
  491. {
  492. if(value == null)
  493. {
  494. return Constants.NotSetValue;
  495. }
  496. else if(value is DataPoint)
  497. {
  498. DataPoint dataPoint = (DataPoint)value;
  499. if(dataPoint.series != null)
  500. {
  501. int pointIndex = dataPoint.series.Points.IndexOf(dataPoint) + 1;
  502. return dataPoint.series.Name + " - " + SR.DescriptionTypePoint + pointIndex.ToString(CultureInfo.InvariantCulture);
  503. }
  504. }
  505. }
  506. // Call base class
  507. return base.ConvertTo(context, culture, value, destinationType);
  508. }
  509. #endregion
  510. }
  511. /// <summary>
  512. /// Converts anchor data point to string name.
  513. /// </summary>
  514. public class AnnotationAxisValueConverter : TypeConverter
  515. {
  516. #region Converter methods
  517. /// <summary>
  518. /// Converts anchor data point to string name.
  519. /// </summary>
  520. /// <param name="context">Descriptor context.</param>
  521. /// <param name="culture">Culture information.</param>
  522. /// <param name="value">Value to convert.</param>
  523. /// <param name="destinationType">Conversion destination type.</param>
  524. /// <returns>Converted object.</returns>
  525. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  526. {
  527. if (destinationType == typeof(string))
  528. {
  529. if(value == null)
  530. {
  531. return Constants.NotSetValue;
  532. }
  533. else if(value is Axis)
  534. {
  535. Axis axis = (Axis)value;
  536. if(axis.ChartArea != null)
  537. {
  538. return axis.ChartArea.Name + " - " + axis.Name;
  539. }
  540. }
  541. }
  542. // Call base class
  543. return base.ConvertTo(context, culture, value, destinationType);
  544. }
  545. #endregion
  546. }
  547. }