1 //-------------------------------------------------------------
2 // <copyright company=
\92Microsoft Corporation
\92>
3 // Copyright © Microsoft Corporation. All Rights Reserved.
5 //-------------------------------------------------------------
6 // @owner=alexgor, deliant
7 //=================================================================
10 // Namespace: System.Web.UI.WebControls[Windows.Forms].Charting
12 // Classes: ChartImage, ChartPicture, ChartPaintEventArgs
14 // Purpose: This file contains classes, which are used for Image
15 // creation and chart painting. This file has also a
16 // class, which is used for Paint events arguments.
18 // Reviewed: GS - August 2, 2002
19 // AG - August 8, 2002
20 // AG - Microsoft 16, 2007
22 //===================================================================
24 #region Used Namespaces
28 using System.Drawing.Drawing2D;
29 using System.Drawing.Design;
30 using System.ComponentModel;
31 using System.ComponentModel.Design;
32 using System.Resources;
33 using System.Reflection;
36 using System.Collections;
37 using System.Drawing.Imaging;
38 using System.Drawing.Text;
40 using System.Globalization;
41 using System.Diagnostics.CodeAnalysis;
42 using System.Diagnostics;
43 using System.Security;
44 using System.Runtime.InteropServices;
45 using System.Collections.Generic;
49 using System.Windows.Forms.DataVisualization.Charting.Data;
50 using System.Windows.Forms.DataVisualization.Charting.ChartTypes;
51 using System.Windows.Forms.DataVisualization.Charting.Utilities;
52 using System.Windows.Forms.DataVisualization.Charting.Borders3D;
53 using System.Windows.Forms.DataVisualization.Charting;
58 using System.Web.UI.DataVisualization.Charting;
59 using System.Web.UI.DataVisualization.Charting.Data;
60 using System.Web.UI.DataVisualization.Charting.ChartTypes;
61 using System.Web.UI.DataVisualization.Charting.Utilities;
62 using System.Web.UI.DataVisualization.Charting.Borders3D;
69 namespace System.Windows.Forms.DataVisualization.Charting
71 namespace System.Web.UI.DataVisualization.Charting
77 #if !Microsoft_CONTROL
80 /// An enumeration of supported image types
82 public enum ChartImageType
96 [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Png")]
100 /// Enhanced Meta File (Emf) image format.
111 /// ChartImage class adds image type and data binding functionality to
112 /// the base ChartPicture class.
114 internal class ChartImage : ChartPicture
118 // Private data members, which store properties values
119 private int _compression = 0;
121 // Chart data source object
122 private object _dataSource = null;
124 // Indicates that control was bound to the data source
125 internal bool boundToDataSource = false;
127 #if !Microsoft_CONTROL
128 private ChartImageType imageType = ChartImageType.Png;
136 /// Chart internal constructor.
138 /// <param name="container">Service container</param>
139 internal ChartImage(IServiceContainer container)
144 #endregion // Constructor
149 /// Gets or sets the data source for the Chart object.
152 SRCategory("CategoryAttributeData"),
154 SRDescription("DescriptionAttributeDataSource"),
156 DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
157 SerializationVisibilityAttribute(SerializationVisibility.Hidden)
159 public object DataSource
167 if(_dataSource != value)
170 this.boundToDataSource = false;
175 #if !Microsoft_CONTROL
178 /// Image type (Jpeg, BMP, Png)
181 SRCategory("CategoryAttributeImage"),
183 DefaultValue(ChartImageType.Png),
184 SRDescription("DescriptionAttributeImageType"),
185 PersistenceMode(PersistenceMode.Attribute)
187 public ChartImageType ImageType
202 /// Image compression value
205 SRCategory("CategoryAttributeImage"),
208 SRDescription("DescriptionAttributeChartImage_Compression"),
209 #if !Microsoft_CONTROL
210 PersistenceMode(PersistenceMode.Attribute)
213 public int Compression
221 if(value < 0 || value > 100)
223 throw (new ArgumentOutOfRangeException("value", SR.ExceptionChartCompressionInvalid));
225 _compression = value;
233 #region Image Manipulation
237 /// Saves image into the metafile stream.
239 /// <param name="imageStream">Image stream.</param>
240 /// <param name="emfType">Image stream.</param>
241 [SecuritySafeCritical]
242 public void SaveIntoMetafile(Stream imageStream, EmfType emfType)
245 if (imageStream == null)
246 throw new ArgumentNullException("imageStream");
248 // Create temporary Graphics object for metafile
249 using (Bitmap bitmap = new Bitmap(this.Width, this.Height))
251 using (Graphics newGraphics = Graphics.FromImage(bitmap))
253 IntPtr hdc = IntPtr.Zero;
256 System.Security.Permissions.SecurityPermission securityPermission = new System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode);
257 securityPermission.Demand();
259 hdc = newGraphics.GetHdc();
262 // Create metafile object to record.
263 using (Metafile metaFile = new Metafile(
266 new Rectangle(0, 0, this.Width, this.Height),
267 MetafileFrameUnit.Pixel,
271 // Create graphics object to record metaFile.
272 using (Graphics metaGraphics = Graphics.FromImage(metaFile))
275 // Note: Fix for issue #3674. Some 3D borders shadows may be drawn outside
276 // of image boundaries. This causes issues when generated EMF file
277 // is placed in IE. Image looks shifted down and hot areas do not align.
278 if (this.BorderSkin.SkinStyle != BorderSkinStyle.None)
280 metaGraphics.Clip = new Region(new Rectangle(0, 0, this.Width, this.Height));
283 // Draw chart in the metafile
284 this.ChartGraph.IsMetafile = true;
285 this.Paint(metaGraphics, false);
286 this.ChartGraph.IsMetafile = false;
293 if (hdc != IntPtr.Zero)
295 newGraphics.ReleaseHdc(hdc);
302 public Bitmap GetImage()
304 return this.GetImage(96);
307 /// Create Image and draw chart picture
309 public Bitmap GetImage(float resolution)
311 // Create a new bitmap
315 while (image == null)
320 image = new Bitmap(Math.Max(1,Width), Math.Max(1,Height));
321 image.SetResolution(resolution, resolution);
324 catch (ArgumentException)
328 catch (OverflowException)
332 catch (InvalidOperationException)
336 catch (ExternalException)
343 // if failed to create the image, decrease the size and the resolution of the chart
345 float newResolution = Math.Max(resolution / 2, 96);
346 Width = (int)Math.Ceiling(Width * newResolution / resolution);
347 Height = (int)Math.Ceiling(Height * newResolution / resolution);
348 resolution = newResolution;
352 // Creates a new Graphics object from the
353 // specified Image object.
354 Graphics offScreen = Graphics.FromImage( image );
358 Color backGroundColor;
360 if (this.BackColor != Color.Empty)
361 backGroundColor = this.BackColor;
363 backGroundColor = Color.White;
365 // Get the page color if border skin is visible.
366 if (GetBorderSkinVisibility() &&
367 this.BorderSkin.PageColor != Color.Empty)
369 backGroundColor = this.BorderSkin.PageColor;
372 // draw a rctangle first with the size of the control, this prevent strange behavior when printing in the reporting services,
373 // without this rectangle, the printed picture is blurry
374 Pen pen = new Pen(backGroundColor);
375 offScreen.DrawRectangle(pen, 0, 0, Width, Height);
379 Paint( offScreen , false);
381 // Dispose Graphic object
384 // Return reference to the image
388 #endregion // Image Manipulation
393 /// Checks if the type of the data source is valid.
395 /// <param name="dataSource">Data source object to test.</param>
396 /// <returns>True if valid data source object.</returns>
397 static internal bool IsValidDataSource(object dataSource)
399 if( null != dataSource &&
401 dataSource is IEnumerable ||
402 dataSource is DataSet ||
403 dataSource is DataView ||
404 dataSource is DataTable ||
405 dataSource is System.Data.OleDb.OleDbCommand ||
406 dataSource is System.Data.SqlClient.SqlCommand ||
407 dataSource is System.Data.OleDb.OleDbDataAdapter ||
408 dataSource is System.Data.SqlClient.SqlDataAdapter ||
409 // ADDED: for VS2005 compatibility, DT Nov 25, 2005
410 dataSource.GetType().GetInterface("IDataSource") != null
424 /// Gets an list of the data source member names.
426 /// <param name="dataSource">Data source object to get the members for.</param>
427 /// <param name="usedForYValue">Indicates that member will be used for Y values.</param>
428 /// <returns>List of member names.</returns>
429 [SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily",
430 Justification = "Too large of a code change to justify making this change")]
431 static internal ArrayList GetDataSourceMemberNames(object dataSource, bool usedForYValue)
433 ArrayList names = new ArrayList();
434 if (dataSource != null)
436 // ADDED: for VS2005 compatibility, DT Nov 25, 2004
437 if (dataSource.GetType().GetInterface("IDataSource") != null)
441 MethodInfo m = dataSource.GetType().GetMethod("Select");
444 if (m.GetParameters().Length == 1)
446 // SQL derived datasource
447 Type selectArgsType = dataSource.GetType().Assembly.GetType("System.Web.UI.DataSourceSelectArguments", true);
448 ConstructorInfo ci = selectArgsType.GetConstructor(new Type[] { });
449 dataSource = m.Invoke(dataSource, new object[] { ci.Invoke(new object[] { }) });
453 // object data source
454 dataSource = m.Invoke(dataSource, new object[] { });
458 catch (TargetException)
461 catch (TargetInvocationException)
467 // Check all DataTable based data souces
468 DataTable dataTable = null;
470 if (dataSource is DataTable)
472 dataTable = (DataTable)dataSource;
474 else if (dataSource is DataView)
476 dataTable = ((DataView)dataSource).Table;
478 else if (dataSource is DataSet && ((DataSet)dataSource).Tables.Count > 0)
480 dataTable = ((DataSet)dataSource).Tables[0];
482 else if (dataSource is System.Data.OleDb.OleDbDataAdapter)
484 dataTable = new DataTable();
485 dataTable.Locale = CultureInfo.CurrentCulture;
486 dataTable = ((System.Data.OleDb.OleDbDataAdapter)dataSource).FillSchema(dataTable, SchemaType.Mapped);
488 else if (dataSource is System.Data.SqlClient.SqlDataAdapter)
490 dataTable = new DataTable();
491 dataTable.Locale = CultureInfo.CurrentCulture;
492 dataTable = ((System.Data.SqlClient.SqlDataAdapter)dataSource).FillSchema(dataTable, SchemaType.Mapped);
494 else if (dataSource is System.Data.OleDb.OleDbDataReader)
496 // Add table columns names
497 for (int fieldIndex = 0; fieldIndex < ((System.Data.OleDb.OleDbDataReader)dataSource).FieldCount; fieldIndex++)
499 if (!usedForYValue || ((System.Data.OleDb.OleDbDataReader)dataSource).GetFieldType(fieldIndex) != typeof(string))
501 names.Add(((System.Data.OleDb.OleDbDataReader)dataSource).GetName(fieldIndex));
505 else if (dataSource is System.Data.SqlClient.SqlDataReader)
507 // Add table columns names
508 for (int fieldIndex = 0; fieldIndex < ((System.Data.SqlClient.SqlDataReader)dataSource).FieldCount; fieldIndex++)
510 if (!usedForYValue || ((System.Data.SqlClient.SqlDataReader)dataSource).GetFieldType(fieldIndex) != typeof(string))
512 names.Add(((System.Data.SqlClient.SqlDataReader)dataSource).GetName(fieldIndex));
516 else if (dataSource is System.Data.OleDb.OleDbCommand)
518 System.Data.OleDb.OleDbCommand command = (System.Data.OleDb.OleDbCommand)dataSource;
519 if (command.Connection != null)
521 command.Connection.Open();
522 System.Data.OleDb.OleDbDataReader dataReader = command.ExecuteReader();
523 if (dataReader.Read())
525 for (int fieldIndex = 0; fieldIndex < dataReader.FieldCount; fieldIndex++)
527 if (!usedForYValue || dataReader.GetFieldType(fieldIndex) != typeof(string))
529 names.Add(dataReader.GetName(fieldIndex));
535 command.Connection.Close();
538 else if (dataSource is System.Data.SqlClient.SqlCommand)
540 System.Data.SqlClient.SqlCommand command = (System.Data.SqlClient.SqlCommand)dataSource;
541 if (command.Connection != null)
543 command.Connection.Open();
544 System.Data.SqlClient.SqlDataReader dataReader = command.ExecuteReader();
545 if (dataReader.Read())
547 for (int fieldIndex = 0; fieldIndex < dataReader.FieldCount; fieldIndex++)
549 if (!usedForYValue || dataReader.GetFieldType(fieldIndex) != typeof(string))
551 names.Add(dataReader.GetName(fieldIndex));
557 command.Connection.Close();
562 // Check if DataTable was set
563 if (dataTable != null)
565 // Add table columns names
566 foreach (DataColumn column in dataTable.Columns)
568 if (!usedForYValue || column.DataType != typeof(string))
570 names.Add(column.ColumnName);
575 else if (names.Count == 0 && dataSource is ITypedList)
577 foreach (PropertyDescriptor pd in ((ITypedList)dataSource).GetItemProperties(null))
579 if (!usedForYValue || pd.PropertyType != typeof(string))
585 else if (names.Count == 0 && dataSource is IEnumerable)
587 // .Net 2.0 ObjectDataSource processing
588 IEnumerator e = ((IEnumerable)dataSource).GetEnumerator();
591 foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(e.Current))
593 if (!usedForYValue || pd.PropertyType != typeof(string))
603 // Check if list still empty
604 if (names.Count == 0)
606 // Add first column or any data member name
616 /// Data binds control to the data source
618 [SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily",
619 Justification="Too large of a code change to justify making this change")]
620 internal void DataBind()
623 this.boundToDataSource = true;
625 object dataSource = this.DataSource;
626 if (dataSource != null)
629 // Convert data adapters to command object
630 if (dataSource is System.Data.OleDb.OleDbDataAdapter)
632 dataSource = ((System.Data.OleDb.OleDbDataAdapter)dataSource).SelectCommand;
634 else if (dataSource is System.Data.SqlClient.SqlDataAdapter)
636 dataSource = ((System.Data.SqlClient.SqlDataAdapter)dataSource).SelectCommand;
639 // Convert data source to recognizable source for the series
640 if (dataSource is DataSet && ((DataSet)dataSource).Tables.Count > 0)
642 dataSource = ((DataSet)dataSource).DefaultViewManager.CreateDataView(((DataSet)dataSource).Tables[0]);
645 else if (dataSource is DataTable)
647 dataSource = new DataView((DataTable)dataSource);
649 else if (dataSource is System.Data.OleDb.OleDbCommand)
651 System.Data.OleDb.OleDbCommand command = (System.Data.OleDb.OleDbCommand)dataSource;
652 command.Connection.Open();
653 System.Data.OleDb.OleDbDataReader dataReader = command.ExecuteReader();
655 this.DataBind(dataReader, null);
658 command.Connection.Close();
661 else if (dataSource is System.Data.SqlClient.SqlCommand)
663 System.Data.SqlClient.SqlCommand command = (System.Data.SqlClient.SqlCommand)dataSource;
664 command.Connection.Open();
665 System.Data.SqlClient.SqlDataReader dataReader = command.ExecuteReader();
667 this.DataBind(dataReader, null);
670 command.Connection.Close();
673 else if (dataSource is IList)
675 dataSource = dataSource as IList;
677 else if (dataSource is IListSource )
679 if (((IListSource)dataSource).ContainsListCollection && ((IListSource)dataSource).GetList().Count > 0)
681 dataSource = ((IListSource)dataSource).GetList()[0] as IEnumerable;
685 dataSource = ((IListSource)dataSource).GetList();
690 dataSource = dataSource as IEnumerable;
694 DataBind(dataSource as IEnumerable, null);
699 /// Data binds control to the data source
701 /// <param name="dataSource">Data source to bind to.</param>
702 /// <param name="seriesList">List of series to bind.</param>
703 internal void DataBind(IEnumerable dataSource, ArrayList seriesList)
706 if(dataSource != null && this.Common != null)
708 //************************************************************
709 //** If list of series is not provided - bind all of them.
710 //************************************************************
711 if(seriesList == null)
713 seriesList = new ArrayList();
714 foreach(Series series in this.Common.Chart.Series)
716 // note: added for design time data binding
717 if (this.Common.Chart.IsDesignMode())
719 if (series.YValueMembers.Length > 0)
721 seriesList.Add(series);
726 seriesList.Add(series);
731 //************************************************************
732 //** Clear all data points in data bound series
733 //************************************************************
734 foreach(Series series in seriesList)
736 if(series.XValueMember.Length > 0 || series.YValueMembers.Length > 0)
738 series.Points.Clear();
742 //************************************************************
743 //** Get and reset data enumerator.
744 //************************************************************
745 IEnumerator enumerator = dataSource.GetEnumerator();
746 if(enumerator.GetType() != typeof(System.Data.Common.DbEnumerator) )
752 // Some enumerators may not support Resetting
753 catch (InvalidOperationException)
756 catch (NotImplementedException)
759 catch (NotSupportedException)
765 //************************************************************
766 //** Loop through the enumerator.
767 //************************************************************
768 bool valueExsists = true;
769 bool autoDetectType = true;
772 // Move to the next item
773 valueExsists = enumerator.MoveNext();
775 // Loop through all series
776 foreach(Series series in seriesList)
778 if(series.XValueMember.Length > 0 || series.YValueMembers.Length > 0)
780 //************************************************************
781 //** Check and convert fields names.
782 //************************************************************
784 // Convert comma separated field names string to array of names
785 string[] yFieldNames = null;
786 if(series.YValueMembers.Length > 0)
788 yFieldNames = series.YValueMembers.Replace(",,", "\n").Split(',');
789 for(int index = 0; index < yFieldNames.Length; index++)
791 yFieldNames[index] = yFieldNames[index].Replace("\n", ",").Trim();
795 // Double check that a string object is not provided for data binding
796 if(dataSource is string)
798 throw (new ArgumentException(SR.ExceptionDataBindYValuesToString, "dataSource"));
801 // Check number of fields
802 if(yFieldNames == null || yFieldNames.GetLength(0) > series.YValuesPerPoint)
804 throw(new ArgumentOutOfRangeException("dataSource", SR.ExceptionDataPointYValuesCountMismatch(series.YValuesPerPoint.ToString(System.Globalization.CultureInfo.InvariantCulture) ) ) );
807 //************************************************************
808 //** Create new data point.
809 //************************************************************
812 // Auto detect values type
815 autoDetectType = false;
817 // Make sure Y field is not empty
818 string yField = yFieldNames[0];
820 while(yField.Length == 0 && fieldIndex < yFieldNames.Length)
822 yField = yFieldNames[fieldIndex++];
825 DataPointCollection.AutoDetectValuesType(series, enumerator, series.XValueMember.Trim(), enumerator, yField);
830 DataPoint newDataPoint = new DataPoint(series);
831 bool emptyValues = false;
832 bool xValueIsNull = false;
834 //************************************************************
835 //** Get new point X and Y values.
836 //************************************************************
837 object[] yValuesObj = new object[yFieldNames.Length];
838 object xValueObj = null;
840 // Set X to the value provided or use sequence numbers starting with 1
841 if(series.XValueMember.Length > 0)
843 xValueObj = DataPointCollection.ConvertEnumerationItem(enumerator.Current, series.XValueMember.Trim());
844 if(xValueObj is System.DBNull || xValueObj == null)
852 if(yFieldNames.Length == 0)
854 yValuesObj[0] = DataPointCollection.ConvertEnumerationItem(enumerator.Current, null);
855 if(yValuesObj[0] is System.DBNull || yValuesObj[0] == null)
863 for(int i = 0; i < yFieldNames.Length; i++)
865 if(yFieldNames[i].Length > 0)
867 yValuesObj[i] = DataPointCollection.ConvertEnumerationItem(enumerator.Current, yFieldNames[i]);
868 if(yValuesObj[i] is System.DBNull || yValuesObj[i] == null)
876 yValuesObj[i] = (((Series)seriesList[0]).IsYValueDateTime()) ? DateTime.Now.Date.ToOADate() : 0.0;
882 // Add data point if X value is not Null
887 if(xValueObj != null)
889 newDataPoint.SetValueXY(xValueObj, yValuesObj);
893 newDataPoint.SetValueXY(0, yValuesObj);
895 series.Points.DataPointInit(ref newDataPoint);
896 newDataPoint.IsEmpty = true;
897 series.Points.Add(newDataPoint);
901 if(xValueObj != null)
903 newDataPoint.SetValueXY(xValueObj, yValuesObj);
907 newDataPoint.SetValueXY(0, yValuesObj);
909 series.Points.DataPointInit(ref newDataPoint);
910 series.Points.Add(newDataPoint);
913 if (this.Common.Chart.IsDesignMode())
915 series["TempDesignData"] = "true";
921 } while(valueExsists);
928 /// Aligns data points using their axis labels.
930 /// <param name="sortAxisLabels">Indicates if points should be sorted by axis labels.</param>
931 /// <param name="sortingOrder">Sorting pointSortOrder.</param>
932 internal void AlignDataPointsByAxisLabel(bool sortAxisLabels, PointSortOrder sortingOrder)
934 // Find series which are attached to the same X axis in the same chart area
935 foreach(ChartArea chartArea in this.ChartAreas)
938 // Check if chart area is visible
939 if(chartArea.Visible)
942 // Create series list for primary and secondary X axis
943 ArrayList chartAreaSeriesPrimary = new ArrayList();
944 ArrayList chartAreaSeriesSecondary = new ArrayList();
945 foreach(Series series in this.Common.Chart.Series)
947 // Check if series belongs to the chart area
948 if (series.ChartArea == chartArea.Name)
950 if(series.XSubAxisName.Length == 0)
952 if(series.XAxisType == AxisType.Primary)
954 chartAreaSeriesPrimary.Add(series);
958 chartAreaSeriesSecondary.Add(series);
965 AlignDataPointsByAxisLabel(chartAreaSeriesPrimary, sortAxisLabels, sortingOrder);
966 AlignDataPointsByAxisLabel(chartAreaSeriesSecondary, sortAxisLabels, sortingOrder);
972 /// Aligns data points using their axis labels.
974 /// <param name="seriesList">List of series to align.</param>
975 /// <param name="sortAxisLabels">Indicates if points should be sorted by axis labels.</param>
976 /// <param name="sortingOrder">Sorting order.</param>
977 internal void AlignDataPointsByAxisLabel(
978 ArrayList seriesList,
980 PointSortOrder sortingOrder)
983 if(seriesList.Count == 0)
988 // Collect information about all points in all series
989 bool indexedX = true;
990 bool uniqueAxisLabels = true;
991 ArrayList axisLabels = new ArrayList();
992 foreach(Series series in seriesList)
994 ArrayList seriesAxisLabels = new ArrayList();
995 foreach(DataPoint point in series.Points)
997 // Check if series has indexed X values
998 if(!series.IsXValueIndexed && point.XValue != 0.0)
1004 // Add axis label to the list and make sure it's non-empty and unique
1005 if(point.AxisLabel.Length == 0)
1007 uniqueAxisLabels = false;
1010 else if(seriesAxisLabels.Contains(point.AxisLabel))
1012 uniqueAxisLabels = false;
1015 else if(!axisLabels.Contains(point.AxisLabel))
1017 axisLabels.Add(point.AxisLabel);
1020 seriesAxisLabels.Add(point.AxisLabel);
1028 if(sortingOrder == PointSortOrder.Descending)
1030 axisLabels.Reverse();
1034 // All series must be indexed
1037 throw (new InvalidOperationException(SR.ExceptionChartDataPointsAlignmentFaild));
1040 // AxisLabel can't be empty or duplicated
1041 if(!uniqueAxisLabels)
1043 throw (new InvalidOperationException(SR.ExceptionChartDataPointsAlignmentFaildAxisLabelsInvalid));
1046 // Assign unique X values for data points in all series with same axis LabelStyle
1047 if(indexedX && uniqueAxisLabels)
1049 foreach(Series series in seriesList)
1051 foreach(DataPoint point in series.Points)
1053 point.XValue = axisLabels.IndexOf(point.AxisLabel) + 1;
1056 // Sort points by X value
1057 series.Sort(PointSortOrder.Ascending, "X");
1060 // Make sure ther are no missing points
1061 foreach(Series series in seriesList)
1063 series.IsXValueIndexed = true;
1064 for(int index = 0; index < axisLabels.Count; index++)
1066 if(index >= series.Points.Count ||
1067 series.Points[index].XValue != index + 1)
1069 DataPoint newPoint = new DataPoint(series);
1070 newPoint.AxisLabel = (string)axisLabels[index];
1071 newPoint.XValue = index + 1;
1072 newPoint.YValues[0] = 0.0;
1073 newPoint.IsEmpty = true;
1074 series.Points.Insert(index, newPoint);
1084 /// Data bind chart to the table. Series will be automatically added to the chart depending on
1085 /// the number of unique values in the seriesGroupByField column of the data source.
1086 /// Data source can be the Ole(SQL)DataReader, DataView, DataSet, DataTable or DataRow.
1088 /// <param name="dataSource">Data source.</param>
1089 /// <param name="seriesGroupByField">Name of the field used to group data into series.</param>
1090 /// <param name="xField">Name of the field for X values.</param>
1091 /// <param name="yFields">Comma separated name(s) of the field(s) for Y value(s).</param>
1092 /// <param name="otherFields">Other point properties binding rule in format: PointProperty=Field[{Format}] [,PointProperty=Field[{Format}]]. For example: "Tooltip=Price{C1},Url=WebSiteName".</param>
1093 /// <param name="sort">Indicates that series should be sorted by group field.</param>
1094 /// <param name="sortingOrder">Series sorting order by group field.</param>
1095 internal void DataBindCrossTab(
1096 IEnumerable dataSource,
1097 string seriesGroupByField,
1102 PointSortOrder sortingOrder)
1105 if (dataSource == null)
1106 throw (new ArgumentNullException("dataSource", SR.ExceptionDataPointInsertionNoDataSource));
1108 if (dataSource is string)
1109 throw (new ArgumentException(SR.ExceptionDataBindSeriesToString, "dataSource"));
1111 if (String.IsNullOrEmpty(yFields))
1112 throw (new ArgumentException(SR.ExceptionChartDataPointsInsertionFailedYValuesEmpty, "yFields"));
1114 if (String.IsNullOrEmpty(seriesGroupByField))
1115 throw (new ArgumentException(SR.ExceptionDataBindSeriesGroupByParameterIsEmpty, "seriesGroupByField"));
1118 // List of series and group by field values
1119 ArrayList seriesList = new ArrayList();
1120 ArrayList groupByValueList = new ArrayList();
1122 // Convert comma separated Y values field names string to array of names
1123 string[] yFieldNames = null;
1126 yFieldNames = yFields.Replace(",,", "\n").Split(',');
1127 for(int index = 0; index < yFieldNames.Length; index++)
1129 yFieldNames[index] = yFieldNames[index].Replace("\n", ",");
1133 // Convert other fields/properties names to two arrays of names
1134 string[] otherAttributeNames = null;
1135 string[] otherFieldNames = null;
1136 string[] otherValueFormat = null;
1137 DataPointCollection.ParsePointFieldsParameter(
1139 ref otherAttributeNames,
1140 ref otherFieldNames,
1141 ref otherValueFormat);
1144 // Get and reset enumerator
1145 IEnumerator enumerator = DataPointCollection.GetDataSourceEnumerator(dataSource);
1146 if(enumerator.GetType() != typeof(System.Data.Common.DbEnumerator))
1152 // Some enumerators may not support Resetting
1153 catch (NotSupportedException)
1156 catch (NotImplementedException)
1159 catch (InvalidOperationException)
1166 bool valueExsist = true;
1167 object[] yValuesObj = new object[yFieldNames.Length];
1168 object xValueObj = null;
1169 bool autoDetectType = true;
1173 // Move to the next objects in the enumerations
1176 valueExsist = enumerator.MoveNext();
1179 // Create and initialize data point
1182 // Get value of the group by field
1183 object groupObj = DataPointCollection.ConvertEnumerationItem(
1185 seriesGroupByField);
1187 // Check series group by field and create new series if required
1188 Series series = null;
1189 int seriesIndex = groupByValueList.IndexOf(groupObj);
1190 if(seriesIndex >= 0)
1192 // Select existing series from the list
1193 series = (Series)seriesList[seriesIndex];
1197 // Create new series
1198 series = new Series();
1199 series.YValuesPerPoint = yFieldNames.GetLength(0);
1201 // If not the first series in the list copy some properties
1202 if(seriesList.Count > 0)
1204 series.XValueType = ((Series)seriesList[0]).XValueType;
1205 series.autoXValueType = ((Series)seriesList[0]).autoXValueType;
1206 series.YValueType = ((Series)seriesList[0]).YValueType;
1207 series.autoYValueType = ((Series)seriesList[0]).autoYValueType;
1210 // Try to set series name based on grouping vlaue
1211 string groupObjStr = groupObj as string;
1212 if(groupObjStr != null)
1214 series.Name = groupObjStr;
1218 series.Name = seriesGroupByField + " - " + groupObj.ToString();
1222 // Add series and group value into the lists
1223 groupByValueList.Add(groupObj);
1224 seriesList.Add(series);
1228 // Auto detect valu(s) type
1231 autoDetectType = false;
1232 DataPointCollection.AutoDetectValuesType(series, enumerator, xField, enumerator, yFieldNames[0]);
1235 // Create new data point
1236 DataPoint newDataPoint = new DataPoint(series);
1237 bool emptyValues = false;
1239 // Set X to the value provided
1240 if(xField.Length > 0)
1242 xValueObj = DataPointCollection.ConvertEnumerationItem(enumerator.Current, xField);
1243 if( DataPointCollection.IsEmptyValue(xValueObj) )
1251 if(yFieldNames.Length == 0)
1253 yValuesObj[0] = DataPointCollection.ConvertEnumerationItem(enumerator.Current, null);
1254 if( DataPointCollection.IsEmptyValue(yValuesObj[0]) )
1257 yValuesObj[0] = 0.0;
1262 for(int i = 0; i < yFieldNames.Length; i++)
1264 yValuesObj[i] = DataPointCollection.ConvertEnumerationItem(enumerator.Current, yFieldNames[i]);
1265 if( DataPointCollection.IsEmptyValue(yValuesObj[i] ) )
1268 yValuesObj[i] = 0.0;
1274 if(otherAttributeNames != null &&
1275 otherAttributeNames.Length > 0)
1277 for(int i = 0; i < otherFieldNames.Length; i++)
1279 // Get object by field name
1280 object obj = DataPointCollection.ConvertEnumerationItem(enumerator.Current, otherFieldNames[i]);
1281 if( !DataPointCollection.IsEmptyValue( obj ) )
1283 newDataPoint.SetPointCustomProperty(
1285 otherAttributeNames[i],
1286 otherValueFormat[i]);
1291 // IsEmpty value was detected
1294 if(xValueObj != null)
1296 newDataPoint.SetValueXY(xValueObj, yValuesObj);
1300 newDataPoint.SetValueXY(0, yValuesObj);
1302 DataPointCollection.DataPointInit(series, ref newDataPoint);
1303 newDataPoint.IsEmpty = true;
1304 series.Points.Add(newDataPoint);
1308 if(xValueObj != null)
1310 newDataPoint.SetValueXY(xValueObj, yValuesObj);
1314 newDataPoint.SetValueXY(0, yValuesObj);
1316 DataPointCollection.DataPointInit(series, ref newDataPoint);
1317 series.Points.Add(newDataPoint);
1321 } while(valueExsist);
1323 // Sort series usig values of group by field
1326 // Duplicate current list
1327 ArrayList oldList = (ArrayList)groupByValueList.Clone();
1330 groupByValueList.Sort();
1331 if(sortingOrder == PointSortOrder.Descending)
1333 groupByValueList.Reverse();
1336 // Change order of series in collection
1337 ArrayList sortedSeriesList = new ArrayList();
1338 foreach(object obj in groupByValueList)
1340 sortedSeriesList.Add(seriesList[oldList.IndexOf(obj)]);
1342 seriesList = sortedSeriesList;
1345 // Add all series from the list into the series collection
1346 foreach(Series series in seriesList)
1348 this.Common.Chart.Series.Add(series);
1353 /// Automatically creates and binds series to specified data table.
1354 /// Each column of the table becomes a Y value in a separate series.
1355 /// Series X value field may also be provided.
1357 /// <param name="dataSource">Data source.</param>
1358 /// <param name="xField">Name of the field for series X values.</param>
1359 internal void DataBindTable(
1360 IEnumerable dataSource,
1364 if (dataSource == null)
1365 throw new ArgumentNullException("dataSource");
1367 // Get list of member names from the data source
1368 ArrayList dataSourceFields = GetDataSourceMemberNames(dataSource, true);
1370 // Remove X value field if it's there
1371 if (xField != null && xField.Length > 0)
1374 for (int i = 0; i < dataSourceFields.Count; i++)
1376 if ( String.Equals((string)dataSourceFields[i], xField, StringComparison.OrdinalIgnoreCase ) )
1384 dataSourceFields.RemoveAt(index);
1388 // Check if field name passed as index
1389 bool parseSucceed = int.TryParse(xField, NumberStyles.Any, CultureInfo.InvariantCulture, out index);
1390 if (parseSucceed && index >= 0 && index < dataSourceFields.Count)
1392 dataSourceFields.RemoveAt(index);
1397 // Get number of series
1398 int seriesNumber = dataSourceFields.Count;
1399 if (seriesNumber > 0)
1401 // Create as many series as fields in the data source
1402 ArrayList seriesList = new ArrayList();
1404 foreach (string fieldName in dataSourceFields)
1406 Series series = new Series(fieldName);
1408 // Set binding properties
1409 series.YValueMembers = fieldName;
1410 series.XValueMember = xField;
1413 seriesList.Add(series);
1419 this.DataBind(dataSource, seriesList);
1421 // Add all series from the list into the series collection
1422 foreach (Series series in seriesList)
1424 // Clear binding properties
1425 series.YValueMembers = String.Empty;
1426 series.XValueMember = String.Empty;
1428 // Add series into the list
1429 this.Common.Chart.Series.Add(series);
1434 #endregion // Data Binding
1441 /// ChartPicture class represents chart content like legends, titles,
1442 /// chart areas and series. It provides methods for positioning and
1443 /// drawing all chart elements.
1445 internal class ChartPicture : ChartElement, IServiceProvider
1450 /// Indicates that chart exceptions should be suppressed.
1452 private bool _suppressExceptions = false;
1454 // Chart Graphic object
1455 internal ChartGraphics ChartGraph { get; set; }
1457 // Private data members, which store properties values
1458 private GradientStyle _backGradientStyle = GradientStyle.None;
1459 private Color _backSecondaryColor = Color.Empty;
1460 private Color _backColor = Color.White;
1461 private string _backImage = "";
1462 private ChartImageWrapMode _backImageWrapMode = ChartImageWrapMode.Tile;
1463 private Color _backImageTransparentColor = Color.Empty;
1464 private ChartImageAlignmentStyle _backImageAlign = ChartImageAlignmentStyle.TopLeft;
1465 private Color _borderColor = Color.White;
1466 private int _borderWidth = 1;
1467 private ChartDashStyle _borderDashStyle = ChartDashStyle.NotSet;
1468 private ChartHatchStyle _backHatchStyle = ChartHatchStyle.None;
1469 private AntiAliasingStyles _antiAliasing = AntiAliasingStyles.All;
1470 private TextAntiAliasingQuality _textAntiAliasingQuality = TextAntiAliasingQuality.High;
1471 private bool _isSoftShadows = true;
1472 private int _width = 300;
1473 private int _height = 300;
1474 private DataManipulator _dataManipulator = new DataManipulator();
1475 internal HotRegionsList hotRegionsList = null;
1476 private BorderSkin _borderSkin = null;
1477 #if !Microsoft_CONTROL
1478 private bool _isMapEnabled = true;
1479 private MapAreasCollection _mapAreas = null;
1481 // Chart areas collection
1482 private ChartAreaCollection _chartAreas = null;
1484 // Chart legend collection
1485 private LegendCollection _legends = null;
1487 // Chart title collection
1488 private TitleCollection _titles = null;
1490 // Chart annotation collection
1491 private AnnotationCollection _annotations = null;
1493 // Annotation smart labels class
1494 internal AnnotationSmartLabel annotationSmartLabel = new AnnotationSmartLabel();
1496 // Chart picture events
1497 internal event EventHandler<ChartPaintEventArgs> BeforePaint;
1498 internal event EventHandler<ChartPaintEventArgs> AfterPaint;
1500 // Chart title position rectangle
1501 private RectangleF _titlePosition = RectangleF.Empty;
1503 // Element spacing size
1504 internal const float elementSpacing = 3F;
1506 // Maximum size of the font in percentage
1507 internal const float maxTitleSize = 15F;
1509 // Printing indicator
1510 internal bool isPrinting = false;
1512 // Indicates chart selection mode
1513 internal bool isSelectionMode = false;
1515 private FontCache _fontCache = new FontCache();
1517 // Position of the chart 3D border
1518 private RectangleF _chartBorderPosition = RectangleF.Empty;
1520 #if Microsoft_CONTROL
1522 // Saving As Image indicator
1523 internal bool isSavingAsImage = false;
1525 // Indicates that chart background is restored from the double buffer
1526 // prior to drawing top level objects like annotations, cursors and selection.
1527 internal bool backgroundRestored = false;
1529 // Buffered image of non-top level chart elements
1530 internal Bitmap nonTopLevelChartBuffer = null;
1532 #endif // Microsoft_CONTROL
1536 #region Constructors
1541 /// <param name="container">Service container</param>
1542 public ChartPicture(IServiceContainer container)
1544 if(container == null)
1546 throw(new ArgumentNullException(SR.ExceptionInvalidServiceContainer));
1549 // Create and set Common Elements
1550 Common = new CommonElements(container);
1551 ChartGraph= new ChartGraphics(Common);
1552 hotRegionsList = new HotRegionsList(Common);
1554 // Create border properties class
1555 _borderSkin = new BorderSkin(this);
1557 // Create a collection of chart areas
1558 _chartAreas = new ChartAreaCollection(this);
1560 // Create a collection of legends
1561 _legends = new LegendCollection(this);
1563 // Create a collection of titles
1564 _titles = new TitleCollection(this);
1566 // Create a collection of annotations
1567 _annotations = new AnnotationCollection(this);
1569 // Set Common elements for data manipulator
1570 _dataManipulator.Common = Common;
1572 #if !Microsoft_CONTROL
1573 // Create map areas collection
1574 _mapAreas = new MapAreasCollection();
1579 /// Returns Chart service object
1581 /// <param name="serviceType">Service AxisName</param>
1582 /// <returns>Chart picture</returns>
1583 [EditorBrowsableAttribute(EditorBrowsableState.Never)]
1584 object IServiceProvider.GetService(Type serviceType)
1586 if(serviceType == typeof(ChartPicture))
1590 throw (new ArgumentException( SR.ExceptionChartPictureUnsupportedType( serviceType.ToString() ) ) );
1595 #region Painting and selection methods
1598 /// Performs empty painting.
1600 internal void PaintOffScreen()
1603 // NOTE: Fixes issue #4733
1604 if (this.Width <= 0 || this.Height <= 0)
1609 // Set process Mode to hot regions
1610 this.Common.HotRegionsList.ProcessChartMode |= ProcessMode.HotRegions;
1611 #if Microsoft_CONTROL
1612 this.Common.HotRegionsList.hitTestCalled = true;
1613 #endif // Microsoft_CONTROL
1615 // Enable selection mode
1616 this.isSelectionMode = true;
1618 // Hot Region list does not exist. Create the list.
1619 //this.common.HotRegionsList.List = new ArrayList();
1620 this.Common.HotRegionsList.Clear();
1622 // Create a new bitmap
1623 Bitmap image = new Bitmap(Math.Max(1,Width), Math.Max(1,Height));
1625 // Creates a new Graphics object from the
1626 // specified Image object.
1627 Graphics offScreen = Graphics.FromImage(image);
1629 // Connect Graphics object with Chart Graphics object
1630 ChartGraph.Graphics = offScreen;
1632 // Remember the previous dirty flag
1633 #if Microsoft_CONTROL
1634 bool oldDirtyFlag = this.Common.Chart.dirtyFlag;
1635 #endif //Microsoft_CONTROL
1638 Paint(ChartGraph.Graphics, false);
1642 // Restore the previous dirty flag
1643 #if Microsoft_CONTROL
1644 this.Common.Chart.dirtyFlag = oldDirtyFlag;
1645 #endif //Microsoft_CONTROL
1647 // Disable selection mode
1648 this.isSelectionMode = false;
1650 // Set process Mode to hot regions
1651 this.Common.HotRegionsList.ProcessChartMode |= ProcessMode.HotRegions;
1656 /// Gets text rendering quality.
1658 /// <returns>Text rendering quality.</returns>
1659 internal TextRenderingHint GetTextRenderingHint()
1661 TextRenderingHint result = TextRenderingHint.SingleBitPerPixelGridFit;
1662 if( (this.AntiAliasing & AntiAliasingStyles.Text) == AntiAliasingStyles.Text )
1664 result = TextRenderingHint.ClearTypeGridFit;
1665 if(this.TextAntiAliasingQuality == TextAntiAliasingQuality.Normal)
1667 result = TextRenderingHint.AntiAlias;
1669 else if(this.TextAntiAliasingQuality == TextAntiAliasingQuality.SystemDefault)
1671 result = TextRenderingHint.SystemDefault;
1676 result = TextRenderingHint.SingleBitPerPixelGridFit;
1682 internal bool GetBorderSkinVisibility()
1684 return _borderSkin.SkinStyle != BorderSkinStyle.None && this.Width > 20 && this.Height > 20;
1688 /// This function paints a chart.
1690 /// <param name="graph">The graph provides drawing object to the display device. A Graphics object is associated with a specific device context.</param>
1691 /// <param name="paintTopLevelElementOnly">Indicates that only chart top level elements like cursors, selection or annotation objects must be redrawn.</param>
1692 [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "3#svg")]
1693 internal void Paint(
1695 bool paintTopLevelElementOnly )
1698 #if Microsoft_CONTROL
1700 // Reset restored and saved backgound flags
1701 this.backgroundRestored = false;
1703 #endif // Microsoft_CONTROL
1705 // Reset Annotation Smart Labels
1706 this.annotationSmartLabel.Reset();
1708 // Do not draw the control if size is less than 5 pixel
1709 if (this.Width < 5 || this.Height < 5)
1714 #if Microsoft_CONTROL
1716 bool resetHotRegionList = false;
1719 this.Common.HotRegionsList.hitTestCalled
1720 || IsToolTipsEnabled()
1723 Common.HotRegionsList.ProcessChartMode = ProcessMode.HotRegions | ProcessMode.Paint;
1725 this.Common.HotRegionsList.hitTestCalled = false;
1727 // Clear list of hot regions
1728 if(paintTopLevelElementOnly)
1730 // If repainting only top level elements (annotations) -
1731 // clear top level objects hot regions only
1732 for(int index = 0; index < this.Common.HotRegionsList.List.Count; index++)
1734 HotRegion region = (HotRegion)this.Common.HotRegionsList.List[index];
1735 if(region.Type == ChartElementType.Annotation)
1737 this.Common.HotRegionsList.List.RemoveAt(index);
1744 // If repainting whole chart - clear all hot regions
1745 resetHotRegionList = true;
1750 Common.HotRegionsList.ProcessChartMode = ProcessMode.Paint;
1752 // If repainting whole chart - clear all hot regions
1753 resetHotRegionList = true;
1756 // Reset hot region list
1757 if(resetHotRegionList)
1759 this.Common.HotRegionsList.Clear();
1763 if( this.IsMapEnabled )
1765 Common.HotRegionsList.ProcessChartMode |= ProcessMode.ImageMaps | ProcessMode.Paint;
1767 // Clear any existing non-custom image map areas
1768 for(int index = 0; index < this.MapAreas.Count; index++)
1770 MapArea mapArea = this.MapAreas[index];
1771 if(!mapArea.IsCustom)
1773 this.MapAreas.RemoveAt(index);
1780 #endif //#if Microsoft_CONTROL
1782 // Check if control was data bound
1783 ChartImage chartImage = this as ChartImage;
1784 if(chartImage != null && !chartImage.boundToDataSource)
1786 if(this.Common != null && this.Common.Chart != null && !this.Common.Chart.IsDesignMode())
1788 this.Common.Chart.DataBind();
1792 // Connect Graphics object with Chart Graphics object
1793 ChartGraph.Graphics = graph;
1795 Common.graph = ChartGraph;
1797 // Set anti alias mode
1798 ChartGraph.AntiAliasing = _antiAliasing;
1799 ChartGraph.softShadows = _isSoftShadows;
1800 ChartGraph.TextRenderingHint = GetTextRenderingHint();
1804 // Check if only chart area cursors and annotations must be redrawn
1805 if(!paintTopLevelElementOnly)
1807 // Fire Before Paint event
1808 OnBeforePaint(new ChartPaintEventArgs(this.Chart, this.ChartGraph, this.Common, new ElementPosition(0, 0, 100, 100)));
1810 // Flag indicates that resize method should be called
1811 // after adjusting the intervals in 3D charts
1812 bool resizeAfterIntervalAdjusting = false;
1814 // RecalculateAxesScale paint chart areas
1815 foreach (ChartArea area in _chartAreas )
1818 // Check if area is visible
1822 area.Set3DAnglesAndReverseMode();
1823 area.SetTempValues();
1824 area.ReCalcInternal();
1826 // Resize should be called the second time
1827 if( area.Area3DStyle.Enable3D && !area.chartAreaIsCurcular)
1829 resizeAfterIntervalAdjusting = true;
1834 // Call Customize event
1835 this.Common.Chart.CallOnCustomize();
1838 Resize(ChartGraph, resizeAfterIntervalAdjusting);
1841 // This code is introduce because labels has to
1842 // be changed when scene is rotated.
1843 bool intervalReCalculated = false;
1844 foreach (ChartArea area in _chartAreas )
1846 if( area.Area3DStyle.Enable3D &&
1847 !area.chartAreaIsCurcular
1854 // Make correction for interval in 3D space
1855 intervalReCalculated = true;
1856 area.Estimate3DInterval( ChartGraph );
1857 area.ReCalcInternal();
1861 // Resize chart areas after updating 3D interval
1862 if(resizeAfterIntervalAdjusting)
1864 // NOTE: Fixes issue #6808.
1865 // In 3D chart area interval will be changed to compenstae for the axis rotation angle.
1866 // This will cause all standard labels to be changed. We need to call the customize event
1867 // the second time to give user a chance to modify those labels.
1868 if (intervalReCalculated)
1870 // Call Customize event
1871 this.Common.Chart.CallOnCustomize();
1874 // Resize chart elements
1879 //***********************************************************************
1880 //** Draw chart 3D border
1881 //***********************************************************************
1882 if (GetBorderSkinVisibility())
1884 // Fill rectangle with page color
1885 ChartGraph.FillRectangleAbs( new RectangleF( 0, 0, Width-1 , Height-1 ),
1886 _borderSkin.PageColor,
1887 ChartHatchStyle.None,
1889 ChartImageWrapMode.Tile,
1891 ChartImageAlignmentStyle.Center,
1894 _borderSkin.PageColor,
1896 ChartDashStyle.Solid,
1897 PenAlignment.Inset );
1900 ChartGraph.Draw3DBorderAbs(
1902 this._chartBorderPosition,
1907 BackImageTransparentColor,
1919 ChartGraph.FillRectangleAbs( new RectangleF( 0, 0, Width-1 , Height-1 ),
1924 BackImageTransparentColor,
1931 PenAlignment.Inset );
1934 // Call BackPaint event
1935 this.Chart.CallOnPrePaint(new ChartPaintEventArgs(this.Chart, this.ChartGraph, this.Common, new ElementPosition(0, 0, 100, 100)));
1937 // Call paint function for each chart area.
1938 foreach (ChartArea area in _chartAreas )
1941 // Check if area is visible
1945 area.Paint(ChartGraph);
1949 // This code is introduced because of GetPointsInterval method,
1950 // which is very time consuming. There is no reason to calculate
1951 // interval after painting.
1952 foreach (ChartArea area in _chartAreas )
1954 // Reset interval data
1955 area.intervalData = double.NaN;
1959 foreach(Legend legendCurrent in this.Legends)
1961 legendCurrent.Paint(ChartGraph);
1964 // Draw chart titles from the collection
1965 foreach(Title titleCurrent in this.Titles)
1967 titleCurrent.Paint(ChartGraph);
1971 this.Chart.CallOnPostPaint(new ChartPaintEventArgs(this.Chart, this.ChartGraph, this.Common, new ElementPosition(0, 0, 100, 100)));
1974 // Draw annotation objects
1975 this.Annotations.Paint(ChartGraph, paintTopLevelElementOnly);
1977 // Draw chart areas cursors in all areas.
1978 // Only if not in selection
1979 if(!this.isSelectionMode)
1981 foreach (ChartArea area in _chartAreas )
1984 // Check if area is visible
1988 area.PaintCursors(ChartGraph, paintTopLevelElementOnly);
1993 // Return default values
1994 foreach (ChartArea area in _chartAreas )
1997 // Check if area is visible
2001 area.Restore3DAnglesAndReverseMode();
2002 area.GetTempValues();
2006 catch(System.Exception)
2012 // Fire After Paint event
2013 OnAfterPaint(new ChartPaintEventArgs(this.Chart, this.ChartGraph, this.Common, new ElementPosition(0, 0, 100, 100)));
2015 // Restore temp values for each chart area
2016 foreach (ChartArea area in _chartAreas )
2019 // Check if area is visible
2023 area.Restore3DAnglesAndReverseMode();
2024 area.GetTempValues();
2028 #if !Microsoft_CONTROL
2029 if (this.Chart.IsDesignMode())
2031 this.Chart.MapAreas.RemoveNonCustom();
2033 #endif //!Microsoft_CONTROL
2038 /// Invoke before paint delegates.
2040 /// <param name="e">Event arguments.</param>
2041 protected virtual void OnBeforePaint(ChartPaintEventArgs e)
2043 if (BeforePaint != null)
2045 //Invokes the delegates.
2046 BeforePaint(this, e);
2051 /// Invoke after paint delegates.
2053 /// <param name="e">Event arguments.</param>
2054 protected virtual void OnAfterPaint(ChartPaintEventArgs e)
2056 if (AfterPaint != null)
2058 //Invokes the delegates.
2059 AfterPaint(this, e);
2063 internal override void Invalidate()
2067 #if Microsoft_CONTROL
2074 #region Resizing methods
2077 /// Resize the chart picture.
2079 /// <param name="chartGraph">Chart graphics.</param>
2080 public void Resize(ChartGraphics chartGraph)
2082 Resize(chartGraph, false);
2086 /// Resize the chart picture.
2088 /// <param name="chartGraph">Chart graphics.</param>
2089 /// <param name="calcAreaPositionOnly">Indicates that only chart area position is calculated.</param>
2090 public void Resize(ChartGraphics chartGraph, bool calcAreaPositionOnly)
2092 // Set the chart size for Common elements
2093 Common.Width = _width;
2094 Common.Height = _height;
2096 // Set the chart size for Chart graphics
2097 chartGraph.SetPictureSize( _width, _height );
2099 // Initialize chart area(s) rectangle
2100 RectangleF chartAreasRectangle = new RectangleF(0, 0, _width - 1, _height - 1);
2101 chartAreasRectangle = chartGraph.GetRelativeRectangle(chartAreasRectangle);
2103 //******************************************************
2104 //** Get the 3D border interface
2105 //******************************************************
2106 _titlePosition = RectangleF.Empty;
2107 IBorderType border3D = null;
2108 bool titleInBorder = false;
2110 if(_borderSkin.SkinStyle != BorderSkinStyle.None)
2113 this._chartBorderPosition = chartGraph.GetAbsoluteRectangle(chartAreasRectangle);
2115 // Get border interface
2116 border3D = Common.BorderTypeRegistry.GetBorderType(_borderSkin.SkinStyle.ToString());
2117 if(border3D != null)
2119 border3D.Resolution = chartGraph.Graphics.DpiX;
2120 // Check if title should be displayed in the border
2121 titleInBorder = border3D.GetTitlePositionInBorder() != RectangleF.Empty;
2122 _titlePosition = chartGraph.GetRelativeRectangle(border3D.GetTitlePositionInBorder());
2123 _titlePosition.Width = chartAreasRectangle.Width - _titlePosition.Width;
2125 // Adjust are position to the border size
2126 border3D.AdjustAreasPosition(chartGraph, ref chartAreasRectangle);
2130 //******************************************************
2131 //** Calculate position of all titles in the collection
2132 //******************************************************
2133 RectangleF frameTitlePosition = RectangleF.Empty;
2136 frameTitlePosition = new RectangleF(_titlePosition.Location, _titlePosition.Size);
2138 foreach(Title title in this.Titles)
2140 if (title.DockedToChartArea == Constants.NotSetValue &&
2141 title.Position.Auto &&
2144 title.CalcTitlePosition(chartGraph, ref chartAreasRectangle, ref frameTitlePosition, elementSpacing);
2148 //******************************************************
2149 //** Calculate position of all legends in the collection
2150 //******************************************************
2151 this.Legends.CalcLegendPosition(chartGraph, ref chartAreasRectangle, elementSpacing);
2153 //******************************************************
2154 //** Calculate position of the chart area(s)
2155 //******************************************************
2156 chartAreasRectangle.Width -= elementSpacing;
2157 chartAreasRectangle.Height -= elementSpacing;
2158 RectangleF areaPosition = new RectangleF();
2161 // Get number of chart areas that requeres automatic positioning
2163 foreach (ChartArea area in _chartAreas )
2166 // Check if area is visible
2170 if(area.Position.Auto)
2177 // Calculate how many columns & rows of areas we going to have
2178 int areaColumns = (int)Math.Floor(Math.Sqrt(areaNumber));
2183 int areaRows = (int)Math.Ceiling(((float)areaNumber) / ((float)areaColumns));
2185 // Set position for all areas
2188 foreach (ChartArea area in _chartAreas )
2191 // Check if area is visible
2195 if(area.Position.Auto)
2197 // Calculate area position
2198 areaPosition.Width = chartAreasRectangle.Width / areaColumns - elementSpacing;
2199 areaPosition.Height = chartAreasRectangle.Height / areaRows - elementSpacing;
2200 areaPosition.X = chartAreasRectangle.X + column * (chartAreasRectangle.Width / areaColumns) + elementSpacing;
2201 areaPosition.Y = chartAreasRectangle.Y + row * (chartAreasRectangle.Height / areaRows) + elementSpacing;
2203 // Calculate position of all titles in the collection docked outside of the chart area
2204 TitleCollection.CalcOutsideTitlePosition(this, chartGraph, area, ref areaPosition, elementSpacing);
2206 // Calculate position of the legend if it's docked outside of the chart area
2207 this.Legends.CalcOutsideLegendPosition(chartGraph, area, ref areaPosition, elementSpacing);
2209 // Set area position without changing the Auto flag
2210 area.Position.SetPositionNoAuto(areaPosition.X, areaPosition.Y, areaPosition.Width, areaPosition.Height);
2222 RectangleF rect = area.Position.ToRectangleF();
2224 // Calculate position of all titles in the collection docked outside of the chart area
2225 TitleCollection.CalcOutsideTitlePosition(this, chartGraph, area, ref rect, elementSpacing);
2227 // Calculate position of the legend if it's docked outside of the chart area
2228 this.Legends.CalcOutsideLegendPosition(chartGraph, area, ref rect, elementSpacing);
2233 //******************************************************
2234 //** Align chart areas Position if required
2235 //******************************************************
2236 AlignChartAreasPosition();
2238 //********************************************************
2239 //** Check if only chart area position must be calculated.
2240 //********************************************************
2241 if(!calcAreaPositionOnly)
2244 //******************************************************
2245 //** Call Resize function for each chart area.
2246 //******************************************************
2247 foreach (ChartArea area in _chartAreas )
2250 // Check if area is visible
2254 area.Resize(chartGraph);
2258 //******************************************************
2259 //** Align chart areas InnerPlotPosition if required
2260 //******************************************************
2261 AlignChartAreas(AreaAlignmentStyles.PlotPosition);
2263 //******************************************************
2264 //** Calculate position of the legend if it's inside
2265 //** chart plotting area
2266 //******************************************************
2268 // Calculate position of all titles in the collection docked outside of the chart area
2269 TitleCollection.CalcInsideTitlePosition(this, chartGraph, elementSpacing);
2271 this.Legends.CalcInsideLegendPosition(chartGraph, elementSpacing);
2276 /// Minimum and maximum do not have to be calculated
2277 /// from data series every time. It is very time
2278 /// consuming. Minimum and maximum are buffered
2279 /// and only when this flags are set Minimum and
2280 /// Maximum are refreshed from data.
2282 internal void ResetMinMaxFromData()
2284 if (_chartAreas != null)
2286 // Call ResetMinMaxFromData function for each chart area.
2287 foreach (ChartArea area in _chartAreas)
2290 // Check if area is visible
2293 area.ResetMinMaxFromData();
2300 /// RecalculateAxesScale the chart picture.
2302 public void Recalculate()
2304 // Call ReCalc function for each chart area.
2305 foreach (ChartArea area in _chartAreas )
2308 // Check if area is visible
2312 area.ReCalcInternal();
2319 #region Chart picture properties
2321 // VSTS 96787-Text Direction (RTL/LTR)
2322 #if !Microsoft_CONTROL
2323 private RightToLeft rightToLeft = RightToLeft.No;
2324 #endif //!Microsoft_CONTROL
2326 /// Gets or sets the RightToLeft type.
2329 DefaultValue(RightToLeft.No)
2331 public RightToLeft RightToLeft
2335 #if Microsoft_CONTROL
2336 return this.Common.Chart.RightToLeft;
2337 #else // !WIN_CONTROL
2338 return this.rightToLeft;
2339 #endif // WIN_CONTROL
2343 #if Microsoft_CONTROL
2344 this.Common.Chart.RightToLeft = value;
2345 #else // !Microsoft_CONTROL
2346 this.rightToLeft = value;
2347 #endif // Microsoft_CONTROL
2352 /// Indicates that non-critical chart exceptions will be suppressed.
2355 SRCategory("CategoryAttributeMisc"),
2356 DefaultValue(false),
2357 SRDescription("DescriptionAttributeSuppressExceptions"),
2359 internal bool SuppressExceptions
2363 _suppressExceptions = value;
2367 return _suppressExceptions;
2372 /// Chart border skin style.
2375 SRCategory("CategoryAttributeAppearance"),
2377 DefaultValue(BorderSkinStyle.None),
2378 SRDescription("DescriptionAttributeBorderSkin"),
2379 #if !Microsoft_CONTROL
2380 PersistenceMode(PersistenceMode.InnerProperty),
2383 public BorderSkin BorderSkin
2391 _borderSkin = value;
2395 #if ! Microsoft_CONTROL
2398 /// Indicates that chart image map is enabled.
2401 SRCategory("CategoryAttributeMap"),
2403 SRDescription("DescriptionAttributeMapEnabled"),
2404 PersistenceMode(PersistenceMode.InnerProperty),
2407 public bool IsMapEnabled
2411 return _isMapEnabled;
2415 _isMapEnabled = value;
2420 /// Chart map areas collection.
2423 SRCategory("CategoryAttributeMap"),
2424 SRDescription("DescriptionAttributeMapAreas"),
2425 PersistenceMode(PersistenceMode.InnerProperty),
2426 Editor(Editors.ChartCollectionEditor.Editor, Editors.ChartCollectionEditor.Base)
2428 public MapAreasCollection MapAreas
2438 /// Reference to chart area collection
2441 SRCategory("CategoryAttributeAppearance"),
2443 SRDescription("DescriptionAttributeChartAreas"),
2444 #if !Microsoft_CONTROL
2445 PersistenceMode(PersistenceMode.InnerProperty),
2447 Editor(Editors.ChartCollectionEditor.Editor, Editors.ChartCollectionEditor.Base)
2449 public ChartAreaCollection ChartAreas
2458 /// Chart legend collection.
2461 SRCategory("CategoryAttributeChart"),
2462 SRDescription("DescriptionAttributeLegends"),
2463 Editor(Editors.LegendCollectionEditor.Editor, Editors.LegendCollectionEditor.Base),
2464 #if !Microsoft_CONTROL
2465 PersistenceMode(PersistenceMode.InnerProperty),
2468 public LegendCollection Legends
2477 /// Chart title collection.
2480 SRCategory("CategoryAttributeCharttitle"),
2481 SRDescription("DescriptionAttributeTitles"),
2482 Editor(Editors.ChartCollectionEditor.Editor, Editors.ChartCollectionEditor.Base),
2483 #if !Microsoft_CONTROL
2484 PersistenceMode(PersistenceMode.InnerProperty),
2487 public TitleCollection Titles
2498 /// Chart annotation collection.
2501 SRCategory("CategoryAttributeChart"),
2502 SRDescription("DescriptionAttributeAnnotations3"),
2503 Editor(Editors.AnnotationCollectionEditor.Editor, Editors.AnnotationCollectionEditor.Base),
2504 #if !Microsoft_CONTROL
2505 PersistenceMode(PersistenceMode.InnerProperty),
2508 public AnnotationCollection Annotations
2512 return _annotations;
2519 /// Background color for the Chart
2523 SRCategory("CategoryAttributeAppearance"),
2525 DefaultValue(typeof(Color), "White"),
2526 SRDescription("DescriptionAttributeBackColor"),
2527 TypeConverter(typeof(ColorConverter)),
2528 Editor(Editors.ChartColorEditor.Editor, Editors.ChartColorEditor.Base),
2529 #if !Microsoft_CONTROL
2530 PersistenceMode(PersistenceMode.Attribute)
2533 public Color BackColor
2541 #if !Microsoft_CONTROL
2542 if(value == Color.Empty || value.A != 255 || value == Color.Transparent)
2544 // NOTE: Transparent colors are valid
2552 /// Border color for the Chart
2556 SRCategory("CategoryAttributeAppearance"),
2558 DefaultValue(typeof(Color), "White"),
2559 SRDescription("DescriptionAttributeBorderColor"),
2560 TypeConverter(typeof(ColorConverter)),
2561 Editor(Editors.ChartColorEditor.Editor, Editors.ChartColorEditor.Base),
2562 #if !Microsoft_CONTROL
2563 PersistenceMode(PersistenceMode.Attribute)
2566 public Color BorderColor
2570 return _borderColor;
2574 _borderColor = value;
2582 SRCategory("CategoryAttributeAppearance"),
2585 SRDescription("DescriptionAttributeWidth"),
2586 #if !Microsoft_CONTROL
2587 PersistenceMode(PersistenceMode.Attribute)
2598 this.InspectChartDimensions(value, this.Height);
2600 Common.Width = _width;
2605 /// Series Data Manipulator
2608 SRCategory("CategoryAttributeData"),
2609 SRDescription("DescriptionAttributeDataManipulator"),
2611 DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden),
2612 SerializationVisibilityAttribute(SerializationVisibility.Hidden)
2614 public DataManipulator DataManipulator
2618 return _dataManipulator;
2629 SRCategory("CategoryAttributeAppearance"),
2632 SRDescription("DescriptionAttributeHeight3"),
2633 #if !Microsoft_CONTROL
2634 PersistenceMode(PersistenceMode.Attribute)
2645 this.InspectChartDimensions(this.Width, value);
2647 Common.Height = value;
2652 /// Back Hatch style
2656 SRCategory("CategoryAttributeAppearance"),
2658 DefaultValue(ChartHatchStyle.None),
2659 SRDescription("DescriptionAttributeBackHatchStyle"),
2660 #if !Microsoft_CONTROL
2661 PersistenceMode(PersistenceMode.Attribute),
2663 Editor(Editors.HatchStyleEditor.Editor, Editors.HatchStyleEditor.Base)
2665 public ChartHatchStyle BackHatchStyle
2669 return _backHatchStyle;
2673 _backHatchStyle = value;
2678 /// Chart area background image
2681 SRCategory("CategoryAttributeAppearance"),
2684 SRDescription("DescriptionAttributeBackImage"),
2685 Editor(Editors.ImageValueEditor.Editor, Editors.ImageValueEditor.Base),
2686 #if !Microsoft_CONTROL
2687 PersistenceMode(PersistenceMode.Attribute),
2689 NotifyParentPropertyAttribute(true)
2691 public string BackImage
2704 /// Chart area background image drawing mode.
2707 SRCategory("CategoryAttributeAppearance"),
2709 DefaultValue(ChartImageWrapMode.Tile),
2710 NotifyParentPropertyAttribute(true),
2711 SRDescription("DescriptionAttributeImageWrapMode"),
2712 #if !Microsoft_CONTROL
2713 PersistenceMode(PersistenceMode.Attribute)
2716 public ChartImageWrapMode BackImageWrapMode
2720 return _backImageWrapMode;
2724 _backImageWrapMode = value;
2729 /// Background image transparent color.
2732 SRCategory("CategoryAttributeAppearance"),
2734 DefaultValue(typeof(Color), ""),
2735 NotifyParentPropertyAttribute(true),
2736 SRDescription("DescriptionAttributeImageTransparentColor"),
2737 TypeConverter(typeof(ColorConverter)),
2738 Editor(Editors.ChartColorEditor.Editor, Editors.ChartColorEditor.Base),
2739 #if !Microsoft_CONTROL
2740 PersistenceMode(PersistenceMode.Attribute)
2743 public Color BackImageTransparentColor
2747 return _backImageTransparentColor;
2751 _backImageTransparentColor = value;
2756 /// Background image alignment used by ClampUnscale drawing mode.
2759 SRCategory("CategoryAttributeAppearance"),
2761 DefaultValue(ChartImageAlignmentStyle.TopLeft),
2762 NotifyParentPropertyAttribute(true),
2763 SRDescription("DescriptionAttributeBackImageAlign"),
2764 #if !Microsoft_CONTROL
2765 PersistenceMode(PersistenceMode.Attribute)
2768 public ChartImageAlignmentStyle BackImageAlignment
2772 return _backImageAlign;
2776 _backImageAlign = value;
2781 /// Indicates that smoothing is applied while drawing shadows.
2784 SRCategory("CategoryAttributeImage"),
2787 SRDescription("DescriptionAttributeSoftShadows3"),
2788 #if !Microsoft_CONTROL
2789 PersistenceMode(PersistenceMode.Attribute)
2792 public bool IsSoftShadows
2796 return _isSoftShadows;
2800 _isSoftShadows = value;
2805 /// Specifies whether smoothing (antialiasing) is applied while drawing chart.
2808 SRCategory("CategoryAttributeImage"),
2810 DefaultValue(typeof(AntiAliasingStyles), "All"),
2811 SRDescription("DescriptionAttributeAntiAlias"),
2812 #if !Microsoft_CONTROL
2813 PersistenceMode(PersistenceMode.Attribute)
2816 public AntiAliasingStyles AntiAliasing
2820 return _antiAliasing;
2824 _antiAliasing = value;
2829 /// Specifies the quality of text antialiasing.
2832 SRCategory("CategoryAttributeImage"),
2834 DefaultValue(typeof(TextAntiAliasingQuality), "High"),
2835 SRDescription("DescriptionAttributeTextAntiAliasingQuality"),
2836 #if !Microsoft_CONTROL
2837 PersistenceMode(PersistenceMode.Attribute)
2840 public TextAntiAliasingQuality TextAntiAliasingQuality
2844 return _textAntiAliasingQuality;
2848 _textAntiAliasingQuality = value;
2853 /// A type for the background gradient
2857 SRCategory("CategoryAttributeAppearance"),
2859 DefaultValue(GradientStyle.None),
2860 SRDescription("DescriptionAttributeBackGradientStyle"),
2861 #if !Microsoft_CONTROL
2862 PersistenceMode(PersistenceMode.Attribute),
2864 Editor(Editors.GradientEditor.Editor, Editors.GradientEditor.Base)
2866 public GradientStyle BackGradientStyle
2870 return _backGradientStyle;
2874 _backGradientStyle = value;
2879 /// The second color which is used for a gradient
2883 SRCategory("CategoryAttributeAppearance"),
2885 DefaultValue(typeof(Color), ""),
2886 SRDescription("DescriptionAttributeBackSecondaryColor"),
2887 TypeConverter(typeof(ColorConverter)),
2888 Editor(Editors.ChartColorEditor.Editor, Editors.ChartColorEditor.Base),
2889 #if !Microsoft_CONTROL
2890 PersistenceMode(PersistenceMode.Attribute)
2893 public Color BackSecondaryColor
2897 return _backSecondaryColor;
2901 #if !Microsoft_CONTROL
2902 if(value != Color.Empty && (value.A != 255 || value == Color.Transparent))
2904 throw (new ArgumentException( SR.ExceptionBackSecondaryColorIsTransparent));
2907 _backSecondaryColor = value;
2912 /// The width of the border line
2916 SRCategory("CategoryAttributeAppearance"),
2919 SRDescription("DescriptionAttributeChart_BorderlineWidth"),
2920 #if !Microsoft_CONTROL
2921 PersistenceMode(PersistenceMode.Attribute)
2924 public int BorderWidth
2928 return _borderWidth;
2934 throw(new ArgumentOutOfRangeException("value", SR.ExceptionChartBorderIsNegative));
2936 _borderWidth = value;
2941 /// The style of the border line
2945 SRCategory("CategoryAttributeAppearance"),
2947 DefaultValue(ChartDashStyle.NotSet),
2948 SRDescription("DescriptionAttributeBorderDashStyle"),
2949 #if !Microsoft_CONTROL
2950 PersistenceMode(PersistenceMode.Attribute)
2953 public ChartDashStyle BorderDashStyle
2957 return _borderDashStyle;
2961 _borderDashStyle = value;
2966 /// Gets the font cache.
2968 /// <value>The font cache.</value>
2969 internal FontCache FontCache
2971 get { return _fontCache; }
2976 #region Chart areas alignment methods
2979 /// Checks if any of the chart areas are aligned.
2980 /// Also checks if the chart ares name in AlignWithChartArea property is valid.
2982 /// <returns>True if at least one area requires alignment.</returns>
2983 private bool IsAreasAlignmentRequired()
2985 bool alignmentRequired = false;
2987 // Loop through all chart areas
2988 foreach(ChartArea area in this.ChartAreas)
2991 // Check if chart area is visible
2995 // Check if area is aligned
2996 if (area.AlignWithChartArea != Constants.NotSetValue)
2998 alignmentRequired = true;
3000 // Check the chart area used for alignment
3001 if (this._chartAreas.IndexOf(area.AlignWithChartArea)<0)
3003 throw (new InvalidOperationException(SR.ExceptionChartAreaNameReferenceInvalid(area.Name, area.AlignWithChartArea)));
3009 return alignmentRequired;
3013 /// Creates a list of the aligned chart areas.
3015 /// <param name="masterArea">Master chart area.</param>
3016 /// <param name="type">Alignment type.</param>
3017 /// <param name="orientation">Vertical or Horizontal orientation.</param>
3018 /// <returns>List of areas that area aligned to the master area.</returns>
3019 private ArrayList GetAlignedAreasGroup(ChartArea masterArea, AreaAlignmentStyles type, AreaAlignmentOrientations orientation)
3021 ArrayList areaList = new ArrayList();
3023 // Loop throught the chart areas and get the ones aligned with specified master area
3024 foreach(ChartArea area in this.ChartAreas)
3027 // Check if chart area is visible
3031 if(area.Name != masterArea.Name &&
3032 area.AlignWithChartArea == masterArea.Name &&
3033 (area.AlignmentStyle & type) == type &&
3034 (area.AlignmentOrientation & orientation) == orientation )
3036 // Add "slave" area into the list
3042 // If list is not empty insert "master" area in the beginning
3043 if(areaList.Count > 0)
3045 areaList.Insert(0, masterArea);
3052 /// Performs specified type of alignment for the chart areas.
3054 /// <param name="type">Alignment type required.</param>
3055 internal void AlignChartAreas(AreaAlignmentStyles type)
3057 // Check if alignment required
3058 if(IsAreasAlignmentRequired())
3060 // Loop through all chart areas
3061 foreach(ChartArea area in this.ChartAreas)
3064 // Check if chart area is visible
3068 // Get vertical areas alignment group using current area as a master
3069 ArrayList alignGroup = GetAlignedAreasGroup(
3072 AreaAlignmentOrientations.Vertical);
3074 // Align each area in the group
3075 if(alignGroup.Count > 0)
3077 AlignChartAreasPlotPosition(alignGroup, AreaAlignmentOrientations.Vertical);
3080 // Get horizontal areas alignment group using current area as a master
3081 alignGroup = GetAlignedAreasGroup(
3084 AreaAlignmentOrientations.Horizontal);
3086 // Align each area in the group
3087 if(alignGroup.Count > 0)
3089 AlignChartAreasPlotPosition(alignGroup, AreaAlignmentOrientations.Horizontal);
3097 /// Align inner plot position of the chart areas in the group.
3099 /// <param name="areasGroup">List of areas in the group.</param>
3100 /// <param name="orientation">Group orientation.</param>
3101 private void AlignChartAreasPlotPosition(ArrayList areasGroup, AreaAlignmentOrientations orientation)
3103 //****************************************************************
3104 //** Find the smalles size of the inner plot
3105 //****************************************************************
3106 RectangleF areaPlotPosition = ((ChartArea)areasGroup[0]).PlotAreaPosition.ToRectangleF();
3107 foreach(ChartArea area in areasGroup)
3109 if(area.PlotAreaPosition.X > areaPlotPosition.X)
3111 areaPlotPosition.X += area.PlotAreaPosition.X - areaPlotPosition.X;
3112 areaPlotPosition.Width -= area.PlotAreaPosition.X - areaPlotPosition.X;
3114 if(area.PlotAreaPosition.Y > areaPlotPosition.Y)
3116 areaPlotPosition.Y += area.PlotAreaPosition.Y - areaPlotPosition.Y;
3117 areaPlotPosition.Height -= area.PlotAreaPosition.Y - areaPlotPosition.Y;
3119 if(area.PlotAreaPosition.Right < areaPlotPosition.Right)
3121 areaPlotPosition.Width -= areaPlotPosition.Right - area.PlotAreaPosition.Right;
3122 if(areaPlotPosition.Width < 5)
3124 areaPlotPosition.Width = 5;
3127 if(area.PlotAreaPosition.Bottom < areaPlotPosition.Bottom)
3129 areaPlotPosition.Height -= areaPlotPosition.Bottom - area.PlotAreaPosition.Bottom;
3130 if(areaPlotPosition.Height < 5)
3132 areaPlotPosition.Height = 5;
3137 //****************************************************************
3138 //** Align inner plot position for all areas
3139 //****************************************************************
3140 foreach(ChartArea area in areasGroup)
3142 // Get curretn plot position of the area
3143 RectangleF rect = area.PlotAreaPosition.ToRectangleF();
3145 // Adjust area position
3146 if( (orientation & AreaAlignmentOrientations.Vertical) == AreaAlignmentOrientations.Vertical)
3148 rect.X = areaPlotPosition.X;
3149 rect.Width = areaPlotPosition.Width;
3151 if( (orientation & AreaAlignmentOrientations.Horizontal) == AreaAlignmentOrientations.Horizontal)
3153 rect.Y = areaPlotPosition.Y;
3154 rect.Height = areaPlotPosition.Height;
3157 // Set new plot position in coordinates relative to chart picture
3158 area.PlotAreaPosition.SetPositionNoAuto(rect.X, rect.Y, rect.Width, rect.Height);
3160 // Set new plot position in coordinates relative to chart area position
3161 rect.X = (rect.X - area.Position.X) / area.Position.Width * 100f;
3162 rect.Y = (rect.Y - area.Position.Y) / area.Position.Height * 100f;
3163 rect.Width = rect.Width / area.Position.Width * 100f;
3164 rect.Height = rect.Height / area.Position.Height * 100f;
3165 area.InnerPlotPosition.SetPositionNoAuto(rect.X, rect.Y, rect.Width, rect.Height);
3167 if( (orientation & AreaAlignmentOrientations.Vertical) == AreaAlignmentOrientations.Vertical)
3169 area.AxisX2.AdjustLabelFontAtSecondPass(ChartGraph, area.InnerPlotPosition.Auto);
3170 area.AxisX.AdjustLabelFontAtSecondPass(ChartGraph, area.InnerPlotPosition.Auto);
3172 if( (orientation & AreaAlignmentOrientations.Horizontal) == AreaAlignmentOrientations.Horizontal)
3174 area.AxisY2.AdjustLabelFontAtSecondPass(ChartGraph, area.InnerPlotPosition.Auto);
3175 area.AxisY.AdjustLabelFontAtSecondPass(ChartGraph, area.InnerPlotPosition.Auto);
3182 /// Aligns positions of the chart areas.
3184 private void AlignChartAreasPosition()
3186 // Check if alignment required
3187 if(IsAreasAlignmentRequired())
3189 // Loop through all chart areas
3190 foreach(ChartArea area in this.ChartAreas)
3193 // Check if chart area is visible
3197 // Check if area is alignd by Position to any other area
3198 if (area.AlignWithChartArea != Constants.NotSetValue && (area.AlignmentStyle & AreaAlignmentStyles.Position) == AreaAlignmentStyles.Position)
3200 // Get current area position
3201 RectangleF areaPosition = area.Position.ToRectangleF();
3203 // Get master chart area
3204 ChartArea masterArea = this.ChartAreas[area.AlignWithChartArea];
3206 // Vertical alignment
3207 if((area.AlignmentOrientation & AreaAlignmentOrientations.Vertical) == AreaAlignmentOrientations.Vertical)
3209 // Align area position
3210 areaPosition.X = masterArea.Position.X;
3211 areaPosition.Width = masterArea.Position.Width;
3214 // Horizontal alignment
3215 if((area.AlignmentOrientation & AreaAlignmentOrientations.Horizontal) == AreaAlignmentOrientations.Horizontal)
3217 // Align area position
3218 areaPosition.Y = masterArea.Position.Y;
3219 areaPosition.Height = masterArea.Position.Height;
3223 area.Position.SetPositionNoAuto(areaPosition.X, areaPosition.Y, areaPosition.Width, areaPosition.Height);
3230 #if Microsoft_CONTROL
3233 /// Align chart areas cursor.
3235 /// <param name="changedArea">Changed chart area.</param>
3236 /// <param name="orientation">Orientation of the changed cursor.</param>
3237 /// <param name="selectionChanged">AxisName of change cursor or selection.</param>
3238 internal void AlignChartAreasCursor(ChartArea changedArea, AreaAlignmentOrientations orientation, bool selectionChanged)
3240 // Check if alignment required
3241 if(IsAreasAlignmentRequired())
3243 // Loop through all chart areas
3244 foreach(ChartArea area in this.ChartAreas)
3247 // Check if chart area is visible
3251 // Get vertical areas alignment group using current area as a master
3252 ArrayList alignGroup = GetAlignedAreasGroup(
3254 AreaAlignmentStyles.Cursor,
3257 // Align each area in the group if it contains changed area
3258 if(alignGroup.Contains(changedArea))
3260 // Set cursor position for all areas in the group
3261 foreach(ChartArea groupArea in alignGroup)
3263 groupArea.alignmentInProcess = true;
3265 if(orientation == AreaAlignmentOrientations.Vertical)
3267 if(selectionChanged)
3269 groupArea.CursorX.SelectionStart = changedArea.CursorX.SelectionStart;
3270 groupArea.CursorX.SelectionEnd = changedArea.CursorX.SelectionEnd;
3274 groupArea.CursorX.Position = changedArea.CursorX.Position;
3277 if(orientation == AreaAlignmentOrientations.Horizontal)
3279 if(selectionChanged)
3281 groupArea.CursorY.SelectionStart = changedArea.CursorY.SelectionStart;
3282 groupArea.CursorY.SelectionEnd = changedArea.CursorY.SelectionEnd;
3286 groupArea.CursorY.Position = changedArea.CursorY.Position;
3290 groupArea.alignmentInProcess = false;
3299 /// One of the chart areas was zoomed by the user.
3301 /// <param name="changedArea">Changed chart area.</param>
3302 /// <param name="orientation">Orientation of the changed scaleView.</param>
3303 /// <param name="disposeBufferBitmap">Area double fuffer image must be disposed.</param>
3304 internal void AlignChartAreasZoomed(ChartArea changedArea, AreaAlignmentOrientations orientation, bool disposeBufferBitmap)
3306 // Check if alignment required
3307 if(IsAreasAlignmentRequired())
3309 // Loop through all chart areas
3310 foreach(ChartArea area in this.ChartAreas)
3313 // Check if chart area is visible
3317 // Get vertical areas alignment group using current area as a master
3318 ArrayList alignGroup = GetAlignedAreasGroup(
3320 AreaAlignmentStyles.AxesView,
3323 // Align each area in the group if it contains changed area
3324 if(alignGroup.Contains(changedArea))
3326 // Set cursor position for all areas in the group
3327 foreach(ChartArea groupArea in alignGroup)
3329 // Clear image buffer
3330 if(groupArea.areaBufferBitmap != null && disposeBufferBitmap)
3332 groupArea.areaBufferBitmap.Dispose();
3333 groupArea.areaBufferBitmap = null;
3336 if(orientation == AreaAlignmentOrientations.Vertical)
3338 groupArea.CursorX.SelectionStart = double.NaN;
3339 groupArea.CursorX.SelectionEnd = double.NaN;
3341 if(orientation == AreaAlignmentOrientations.Horizontal)
3343 groupArea.CursorY.SelectionStart = double.NaN;
3344 groupArea.CursorY.SelectionEnd = double.NaN;
3353 #endif //Microsoft_CONTROL
3356 /// Align chart areas axes views.
3358 /// <param name="changedArea">Changed chart area.</param>
3359 /// <param name="orientation">Orientation of the changed scaleView.</param>
3360 internal void AlignChartAreasAxesView(ChartArea changedArea, AreaAlignmentOrientations orientation)
3362 // Check if alignment required
3363 if(IsAreasAlignmentRequired())
3365 // Loop through all chart areas
3366 foreach(ChartArea area in this.ChartAreas)
3369 // Check if chart area is visible
3373 // Get vertical areas alignment group using current area as a master
3374 ArrayList alignGroup = GetAlignedAreasGroup(
3376 AreaAlignmentStyles.AxesView,
3379 // Align each area in the group if it contains changed area
3380 if(alignGroup.Contains(changedArea))
3382 // Set cursor position for all areas in the group
3383 foreach(ChartArea groupArea in alignGroup)
3385 groupArea.alignmentInProcess = true;
3387 if(orientation == AreaAlignmentOrientations.Vertical)
3389 groupArea.AxisX.ScaleView.Position = changedArea.AxisX.ScaleView.Position;
3390 groupArea.AxisX.ScaleView.Size = changedArea.AxisX.ScaleView.Size;
3391 groupArea.AxisX.ScaleView.SizeType = changedArea.AxisX.ScaleView.SizeType;
3393 groupArea.AxisX2.ScaleView.Position = changedArea.AxisX2.ScaleView.Position;
3394 groupArea.AxisX2.ScaleView.Size = changedArea.AxisX2.ScaleView.Size;
3395 groupArea.AxisX2.ScaleView.SizeType = changedArea.AxisX2.ScaleView.SizeType;
3397 if(orientation == AreaAlignmentOrientations.Horizontal)
3399 groupArea.AxisY.ScaleView.Position = changedArea.AxisY.ScaleView.Position;
3400 groupArea.AxisY.ScaleView.Size = changedArea.AxisY.ScaleView.Size;
3401 groupArea.AxisY.ScaleView.SizeType = changedArea.AxisY.ScaleView.SizeType;
3403 groupArea.AxisY2.ScaleView.Position = changedArea.AxisY2.ScaleView.Position;
3404 groupArea.AxisY2.ScaleView.Size = changedArea.AxisY2.ScaleView.Size;
3405 groupArea.AxisY2.ScaleView.SizeType = changedArea.AxisY2.ScaleView.SizeType;
3408 groupArea.alignmentInProcess = false;
3418 #region Helper methods
3421 /// Inspects the chart dimensions.
3423 /// <param name="width">The width.</param>
3424 /// <param name="height">The height.</param>
3425 internal void InspectChartDimensions(int width, int height)
3427 if (this.Chart.IsDesignMode() && ((width * height) > (100 * 1024 *1024)))
3429 throw new ArgumentException(SR.ExceptionChartOutOfLimits);
3433 throw new ArgumentException(SR.ExceptionValueMustBeGreaterThan("Width", "0px"));
3437 throw new ArgumentException(SR.ExceptionValueMustBeGreaterThan("Height", "0px"));
3442 /// Loads chart appearance template from file.
3444 /// <param name="name">Template file name to load from.</param>
3445 public void LoadTemplate(string name)
3449 throw new ArgumentNullException("name");
3451 // Load template data into the stream
3452 #if Microsoft_CONTROL
3453 Stream stream = new FileStream(name, FileMode.Open, FileAccess.Read);
3454 #else // Microsoft_CONTROL
3455 Stream stream = LoadTemplateData(name);
3456 #endif // Microsoft_CONTROL
3458 // Load template from stream
3459 LoadTemplate(stream);
3461 // Close tempate stream
3466 /// Loads chart appearance template from stream.
3468 /// <param name="stream">Template stream to load from.</param>
3469 public void LoadTemplate(Stream stream)
3473 throw new ArgumentNullException("stream");
3475 ChartSerializer serializer = (ChartSerializer)this.Common.container.GetService(typeof(ChartSerializer));
3476 if (serializer != null)
3478 // Save previous serializer properties
3479 string oldSerializableContent = serializer.SerializableContent;
3480 string oldNonSerializableContent = serializer.NonSerializableContent;
3481 SerializationFormat oldFormat = serializer.Format;
3482 bool oldIgnoreUnknownXmlAttributes = serializer.IsUnknownAttributeIgnored;
3483 bool oldTemplateMode = serializer.IsTemplateMode;
3485 // Set serializer properties
3486 serializer.Content = SerializationContents.Appearance;
3487 serializer.SerializableContent += ",Chart.Titles,Chart.Annotations," +
3488 "Chart.Legends,Legend.CellColumns,Legend.CustomItems,LegendItem.Cells," +
3489 "Chart.Series,Series.*Style," +
3490 "Chart.ChartAreas,ChartArea.Axis*," +
3491 "Axis.*Grid,Axis.*TickMark, Axis.*Style," +
3492 "Axis.StripLines, Axis.CustomLabels";
3493 serializer.Format = SerializationFormat.Xml;
3494 serializer.IsUnknownAttributeIgnored = true;
3495 serializer.IsTemplateMode = true;
3500 serializer.Load(stream);
3502 catch (Exception ex)
3504 throw (new InvalidOperationException(ex.Message));
3508 // Restore previous serializer properties
3509 serializer.SerializableContent = oldSerializableContent;
3510 serializer.NonSerializableContent = oldNonSerializableContent;
3511 serializer.Format = oldFormat;
3512 serializer.IsUnknownAttributeIgnored = oldIgnoreUnknownXmlAttributes;
3513 serializer.IsTemplateMode = oldTemplateMode;
3518 #if !Microsoft_CONTROL
3521 /// Loads template data from the URL.
3523 /// <param name="url">Template URL.</param>
3524 /// <returns>Stream with template data or null if error.</returns>
3525 private Stream LoadTemplateData(string url)
3527 Debug.Assert(url != null, "LoadTemplateData: handed a null url string");
3529 Stream dataStream = null;
3531 // Try to load as relative URL using the Control object
3532 if(dataStream == null)
3534 if (this.Common != null &&
3535 this.Common.Chart != null &&
3536 this.Common.Chart.Page != null)
3540 dataStream = new FileStream(
3541 this.Common.Chart.Page.MapPath(url),
3544 catch (NotSupportedException)
3548 catch (SecurityException)
3552 catch (FileNotFoundException)
3556 catch (DirectoryNotFoundException)
3560 catch (PathTooLongException)
3567 // Try to load image using the Web Request
3568 if(dataStream == null)
3570 Uri templateUri = null;
3573 // Try to create URI directly from template URL (will work in case of absolute URL)
3574 templateUri = new Uri(url);
3576 catch (UriFormatException)
3581 // Make absolute URL using web form document URL
3582 if(templateUri == null)
3584 if (this.Common != null && this.Common.Chart != null)
3586 string webFormUrl = this.Common.Chart.webFormDocumentURL;
3587 int slashIndex = webFormUrl.LastIndexOf('/');
3588 if(slashIndex != -1)
3590 webFormUrl = webFormUrl.Substring(0, slashIndex + 1);
3595 templateUri = new Uri(new Uri(webFormUrl), url);
3597 catch (UriFormatException)
3604 // Load image from file or web resource
3605 if(templateUri != null)
3609 WebRequest request = WebRequest.Create(templateUri);
3610 dataStream = request.GetResponse().GetResponseStream();
3612 catch (NotSupportedException)
3616 catch (NotImplementedException)
3620 catch (SecurityException)
3627 // Try to load as file
3628 if(dataStream == null)
3630 dataStream = new FileStream(url, FileMode.Open);
3636 #endif // Microsoft_CONTROL
3640 #if !Microsoft_CONTROL
3644 /// Writes chart map tag into the stream.
3646 /// <param name="output">Html writer to output the data to.</param>
3647 /// <param name="mapName">Chart map name.</param>
3648 internal void WriteChartMapTag(HtmlTextWriter output, string mapName)
3651 output.AddAttribute(HtmlTextWriterAttribute.Name, mapName);
3652 output.AddAttribute(HtmlTextWriterAttribute.Id, mapName);
3653 output.RenderBeginTag(HtmlTextWriterTag.Map);
3655 //****************************************************
3656 //** Fire map areas customize event
3657 //****************************************************
3659 // Make sure only non-custom items are passed into the event handler
3660 MapAreasCollection custCollection = new MapAreasCollection();
3662 // Move all non-custom items
3663 for (int index = 0; index < _mapAreas.Count; index++)
3665 if (!_mapAreas[index].IsCustom)
3667 custCollection.Add(_mapAreas[index]);
3668 _mapAreas.RemoveAt(index);
3673 // Call a notification event, so that area items collection can be modified by user
3674 Common.Chart.CallOnCustomizeMapAreas(custCollection);
3676 // Add customized items
3677 foreach(MapArea area in custCollection)
3679 area.IsCustom = false;
3680 _mapAreas.Add(area);
3683 //****************************************************
3684 //** Add all map areas
3685 //****************************************************
3686 foreach (MapArea area in _mapAreas)
3688 area.RenderTag(output, this.Common.Chart);
3690 // if this procedure is enforced to run the image maps have to have at least one map area.
3691 if (_mapAreas.Count == 0)
3693 output.Write("<area shape=\"rect\" coords=\"0,0,0,0\" alt=\"\" />");
3696 //****************************************************
3698 //****************************************************
3699 output.RenderEndTag();
3707 /// Returns the default title from Titles collection.
3709 /// <param name="create">Create title if it doesn't exists.</param>
3710 /// <returns>Default title.</returns>
3711 internal Title GetDefaultTitle(bool create)
3713 // Check if default title exists
3714 Title defaultTitle = null;
3715 foreach(Title title in this.Titles)
3717 if(title.Name == "Default Title")
3719 defaultTitle = title;
3723 // Create new default title
3724 if(defaultTitle == null && create)
3726 defaultTitle = new Title();
3727 defaultTitle.Name = "Default Title";
3728 this.Titles.Insert(0, defaultTitle);
3731 return defaultTitle;
3735 /// Checks if tooltips are enabled
3737 /// <returns>true if tooltips enabled</returns>
3738 private bool IsToolTipsEnabled()
3742 foreach( Series series in Common.DataManager.Series )
3744 // Check series tooltips
3745 if( series.ToolTip.Length > 0)
3751 // Check series tooltips
3752 if( series.LegendToolTip.Length > 0 ||
3753 series.LabelToolTip.Length > 0)
3759 // Check point tooltips only for "non-Fast" chart types
3760 if( !series.IsFastChartType() )
3763 foreach( DataPoint point in series.Points )
3766 if( point.ToolTip.Length > 0)
3772 if( point.LegendToolTip.Length > 0 ||
3773 point.LabelToolTip.Length > 0)
3782 // Legend items loop
3783 foreach( Legend legend in Legends )
3785 foreach( LegendItem legendItem in legend.CustomItems )
3788 if( legendItem.ToolTip.Length > 0 )
3796 foreach( Title title in Titles )
3799 if( title.ToolTip.Length > 0 )
3810 #region IDisposable Members
3812 /// Releases unmanaged and - optionally - managed resources
3814 /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
3815 protected override void Dispose(bool disposing)
3819 // Dispose managed resources
3820 if (ChartGraph != null)
3822 ChartGraph.Dispose();
3825 if (_legends != null)
3830 if (_titles != null)
3835 if (_chartAreas != null)
3837 _chartAreas.Dispose();
3840 if (_annotations != null)
3842 _annotations.Dispose();
3843 _annotations = null;
3845 if (hotRegionsList != null)
3847 hotRegionsList.Dispose();
3848 hotRegionsList = null;
3850 if (_fontCache != null)
3852 _fontCache.Dispose();
3855 if (_borderSkin != null)
3857 _borderSkin.Dispose();
3860 #if ! Microsoft_CONTROL
3861 if (_mapAreas != null)
3863 _mapAreas.Dispose();
3868 #if Microsoft_CONTROL
3869 if (nonTopLevelChartBuffer != null)
3871 nonTopLevelChartBuffer.Dispose();
3872 nonTopLevelChartBuffer = null;
3876 base.Dispose(disposing);
3883 /// Event arguments of Chart paint event.
3886 [AspNetHostingPermission(System.Security.Permissions.SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
3887 [AspNetHostingPermission(System.Security.Permissions.SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
3889 public class ChartPaintEventArgs : EventArgs
3894 private object _chartElement = null;
3895 private ChartGraphics _chartGraph = null;
3896 private CommonElements _common = null;
3897 private Chart _chart = null;
3898 private ElementPosition _position = null;
3906 /// Gets the chart element of the event.
3908 /// <value>The chart element.</value>
3909 public object ChartElement
3913 return _chartElement;
3919 /// Gets the ChartGraphics object of the event.
3921 public ChartGraphics ChartGraphics
3930 /// Chart Common elements.
3932 internal CommonElements CommonElements
3941 /// Chart element position in relative coordinates of the event.
3943 public ElementPosition Position
3952 /// Chart object of the event.
3958 if (_chart == null && _common != null)
3960 _chart = _common.Chart;
3972 /// Default constructor is not accessible
3974 private ChartPaintEventArgs()
3979 /// Paint event arguments constructor.
3981 /// <param name="chartElement">Chart element.</param>
3982 /// <param name="chartGraph">Chart graphics.</param>
3983 /// <param name="common">Common elements.</param>
3984 /// <param name="position">Position.</param>
3985 internal ChartPaintEventArgs(object chartElement, ChartGraphics chartGraph, CommonElements common, ElementPosition position)
3987 this._chartElement = chartElement;
3988 this._chartGraph = chartGraph;
3989 this._common = common;
3990 this._position = position;
3997 /// Event arguments of localized numbers formatting event.
4000 [AspNetHostingPermission(System.Security.Permissions.SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
4001 [AspNetHostingPermission(System.Security.Permissions.SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
4003 public class FormatNumberEventArgs : EventArgs
4008 private double _value;
4009 private string _format;
4010 private string _localizedValue;
4011 private ChartValueType _valueType = ChartValueType.Auto;
4012 private object _senderTag;
4013 private ChartElementType _elementType = ChartElementType.Nothing;
4020 /// Value to be formatted.
4024 get { return this._value; }
4030 public string LocalizedValue
4032 get { return _localizedValue; }
4033 set { _localizedValue = value; }
4039 public string Format
4041 get { return _format; }
4047 public ChartValueType ValueType
4049 get { return _valueType; }
4053 /// The sender object of the event.
4055 public object SenderTag
4057 get { return _senderTag; }
4061 /// Chart element type.
4063 public ChartElementType ElementType
4065 get { return _elementType; }
4073 /// Default constructor is not accessible
4075 private FormatNumberEventArgs()
4080 /// Object constructor.
4082 /// <param name="value">Value to be formatted.</param>
4083 /// <param name="format">Format string.</param>
4084 /// <param name="valueType">Value type..</param>
4085 /// <param name="localizedValue">Localized value.</param>
4086 /// <param name="senderTag">Chart element object tag.</param>
4087 /// <param name="elementType">Chart element type.</param>
4088 internal FormatNumberEventArgs(double value, string format, ChartValueType valueType, string localizedValue, object senderTag, ChartElementType elementType)
4090 this._value = value;
4091 this._format = format;
4092 this._valueType = valueType;
4093 this._localizedValue = localizedValue;
4094 this._senderTag = senderTag;
4095 this._elementType = elementType;
4103 /// Font cache class helps ChartElements to reuse the Font instances
4105 internal class FontCache : IDisposable
4109 // Default font family name
4110 private static string _defaultFamilyName;
4113 /// Gets the default font family name.
4115 /// <value>The default font family name.</value>
4116 public static string DefaultFamilyName
4120 if (_defaultFamilyName == null)
4122 // Find the "Microsoft Sans Serif" font
4123 foreach (FontFamily fontFamily in FontFamily.Families)
4125 if (fontFamily.Name == "Microsoft Sans Serif")
4127 _defaultFamilyName = fontFamily.Name;
4131 // Not found - use the default Sans Serif font
4132 if (_defaultFamilyName == null)
4134 _defaultFamilyName = FontFamily.GenericSansSerif.Name;
4137 return _defaultFamilyName;
4144 // Cached fonts dictionary
4145 private Dictionary<KeyInfo, Font> _fontCache = new Dictionary<KeyInfo, Font>(new KeyInfo.EqualityComparer());
4147 #endregion // Fields
4151 /// Gets the default font.
4153 /// <value>The default font.</value>
4154 public Font DefaultFont
4156 get { return this.GetFont(DefaultFamilyName, 8); }
4160 /// Gets the default font.
4162 /// <value>The default font.</value>
4163 public Font DefaultBoldFont
4165 get { return this.GetFont(DefaultFamilyName, 8, FontStyle.Bold); }
4174 /// <param name="familyName">Name of the family.</param>
4175 /// <param name="size">The size.</param>
4176 /// <returns>Font instance</returns>
4177 public Font GetFont(string familyName, int size)
4179 KeyInfo key = new KeyInfo(familyName, size);
4180 if (!this._fontCache.ContainsKey(key))
4182 this._fontCache.Add(key, new Font(familyName, size));
4184 return this._fontCache[key];
4190 /// <param name="familyName">Name of the family.</param>
4191 /// <param name="size">The size.</param>
4192 /// <param name="style">The style.</param>
4193 /// <returns>Font instance</returns>
4194 public Font GetFont(string familyName, float size, FontStyle style)
4196 KeyInfo key = new KeyInfo(familyName, size, style);
4197 if (!this._fontCache.ContainsKey(key))
4199 this._fontCache.Add(key, new Font(familyName, size, style));
4201 return this._fontCache[key];
4207 /// <param name="family">The family.</param>
4208 /// <param name="size">The size.</param>
4209 /// <param name="style">The style.</param>
4210 /// <returns>Font instance</returns>
4211 public Font GetFont(FontFamily family, float size, FontStyle style)
4213 KeyInfo key = new KeyInfo(family, size, style);
4214 if (!this._fontCache.ContainsKey(key))
4216 this._fontCache.Add(key, new Font(family, size, style));
4218 return this._fontCache[key];
4224 /// <param name="family">The family.</param>
4225 /// <param name="size">The size.</param>
4226 /// <param name="style">The style.</param>
4227 /// <param name="unit">The unit.</param>
4228 /// <returns>Font instance</returns>
4229 public Font GetFont(FontFamily family, float size, FontStyle style, GraphicsUnit unit)
4231 KeyInfo key = new KeyInfo(family, size, style, unit);
4232 if (!this._fontCache.ContainsKey(key))
4234 this._fontCache.Add(key, new Font(family, size, style, unit));
4236 return this._fontCache[key];
4241 #region IDisposable Members
4244 /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
4246 public void Dispose()
4248 foreach (Font font in _fontCache.Values)
4253 GC.SuppressFinalize(this);
4258 #region FontKeyInfo struct
4262 private class KeyInfo
4266 GraphicsUnit _unit = GraphicsUnit.Point;
4267 FontStyle _style = FontStyle.Regular;
4268 int _gdiCharSet = 1;
4271 /// Initializes a new instance of the <see cref="KeyInfo"/> class.
4273 /// <param name="familyName">Name of the family.</param>
4274 /// <param name="size">The size.</param>
4275 public KeyInfo(string familyName, float size)
4277 this._familyName = familyName;
4281 /// Initializes a new instance of the <see cref="KeyInfo"/> class.
4283 /// <param name="familyName">Name of the family.</param>
4284 /// <param name="size">The size.</param>
4285 /// <param name="style">The style.</param>
4286 public KeyInfo(string familyName, float size, FontStyle style)
4288 this._familyName = familyName;
4290 this._style = style;
4293 /// Initializes a new instance of the <see cref="KeyInfo"/> class.
4295 /// <param name="family">The family.</param>
4296 /// <param name="size">The size.</param>
4297 /// <param name="style">The style.</param>
4298 public KeyInfo(FontFamily family, float size, FontStyle style)
4300 this._familyName = family.ToString();
4302 this._style = style;
4305 /// Initializes a new instance of the <see cref="KeyInfo"/> class.
4307 /// <param name="family">The family.</param>
4308 /// <param name="size">The size.</param>
4309 /// <param name="style">The style.</param>
4310 /// <param name="unit">The unit.</param>
4311 public KeyInfo(FontFamily family, float size, FontStyle style, GraphicsUnit unit)
4313 this._familyName = family.ToString();
4315 this._style = style;
4319 #region IEquatable<FontKeyInfo> Members
4321 /// KeyInfo equality comparer
4323 internal class EqualityComparer : IEqualityComparer<KeyInfo>
4326 /// Determines whether the specified objects are equal.
4328 /// <param name="x">The first object of type <paramref name="x"/> to compare.</param>
4329 /// <param name="y">The second object of type <paramref name="y"/> to compare.</param>
4331 /// true if the specified objects are equal; otherwise, false.
4333 public bool Equals(KeyInfo x, KeyInfo y)
4336 x._size == y._size &&
4337 x._familyName == y._familyName &&
4338 x._unit == y._unit &&
4339 x._style == y._style &&
4340 x._gdiCharSet == y._gdiCharSet;
4344 /// Returns a hash code for the specified object.
4346 /// <param name="obj">The <see cref="T:System.Object"/> for which a hash code is to be returned.</param>
4347 /// <returns>A hash code for the specified object.</returns>
4348 /// <exception cref="T:System.ArgumentNullException">The type of <paramref name="obj"/> is a reference type and <paramref name="obj"/> is null.</exception>
4349 public int GetHashCode(KeyInfo obj)
4351 return obj._familyName.GetHashCode() ^ obj._size.GetHashCode();