//-------------------------------------------------------------
//
// Copyright © Microsoft Corporation. All Rights Reserved.
//
//-------------------------------------------------------------
// @owner=alexgor, deliant
//=================================================================
// File: ChartRenderingEngine.cs
//
// Namespace: System.Web.UI.WebControls[Windows.Forms].Charting
//
// Classes: ChartRenderingEngine, ValueA, PointA, RectangleA,
// ColorA
//
// Purpose: ChartRenderingEngine class provides a common interface
// to the graphics rendering and animation engines.
// Internally it uses SvgChartGraphics, FlashGraphics or
// GdiGraphics classes depending on the ActiveRenderingType
// property settings.
//
// ValueA, PointA, RectangleA and ColorA classes are
// used to store data about animated values like colors
// position or rectangles. They store starting value/time,
// end value/time, repeat flags and other settings. These
// clases are used with animation engines.
//
// Reviwed: AG - Jul 15, 2003
// AG - Microsoft 16, 2007
//
//===================================================================
#region Used namespaces
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Drawing.Imaging;
using System.ComponentModel;
using System.Collections;
using System.Xml;
using System.IO;
using System.Diagnostics.CodeAnalysis;
#if Microsoft_CONTROL
using System.Windows.Forms.DataVisualization.Charting.Utilities;
using System.Windows.Forms.DataVisualization.Charting.Borders3D;
#else
using System.Web.UI.DataVisualization.Charting.Utilities;
using System.Web.UI.DataVisualization.Charting.Borders3D;
#endif
#endregion
#if Microsoft_CONTROL
namespace System.Windows.Forms.DataVisualization.Charting
#else
namespace System.Web.UI.DataVisualization.Charting
#endif
{
#region Enumerations
///
/// Specify Rendering AxisName
///
internal enum RenderingType
{
///
/// GDI+ AxisName
///
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Gdi")]
Gdi,
///
/// SVG AxisName
///
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Svg")]
Svg,
}
#endregion // Enumerations
///
/// The ChartGraphics class provides a common interface to the
/// graphics rendering.
///
#if ASPPERM_35
[AspNetHostingPermission(System.Security.Permissions.SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(System.Security.Permissions.SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
#endif
public partial class ChartGraphics
{
#region Fields
// Current rendering type
private RenderingType _activeRenderingType = RenderingType.Gdi;
// GDI+ rendering engine
private GdiGraphics _gdiGraphics = new GdiGraphics();
// Document title used for SVG rendering
//private string documentTitle = string.Empty;
// True if text should be clipped
internal bool IsTextClipped = false;
#endregion // Fields
#region Drawing Methods
///
/// Draws a line connecting two PointF structures.
///
/// Pen object that determines the color, width, and style of the line.
/// PointF structure that represents the first point to connect.
/// PointF structure that represents the second point to connect.
internal void DrawLine(
Pen pen,
PointF pt1,
PointF pt2
)
{
RenderingObject.DrawLine( pen, pt1, pt2 );
}
///
/// Draws a line connecting the two points specified by coordinate pairs.
///
/// Pen object that determines the color, width, and style of the line.
/// x-coordinate of the first point.
/// y-coordinate of the first point.
/// x-coordinate of the second point.
/// y-coordinate of the second point.
internal void DrawLine(
Pen pen,
float x1,
float y1,
float x2,
float y2
)
{
RenderingObject.DrawLine( pen, x1, y1, x2, y2 );
}
///
/// Draws the specified portion of the specified Image object at the specified location and with the specified size.
///
/// Image object to draw.
/// Rectangle structure that specifies the location and size of the drawn image. The image is scaled to fit the rectangle.
/// x-coordinate of the upper-left corner of the portion of the source image to draw.
/// y-coordinate of the upper-left corner of the portion of the source image to draw.
/// Width of the portion of the source image to draw.
/// Height of the portion of the source image to draw.
/// Member of the GraphicsUnit enumeration that specifies the units of measure used to determine the source rectangle.
/// ImageAttributes object that specifies recoloring and gamma information for the image object.
internal void DrawImage(
System.Drawing.Image image,
Rectangle destRect,
int srcX,
int srcY,
int srcWidth,
int srcHeight,
GraphicsUnit srcUnit,
ImageAttributes imageAttr
)
{
RenderingObject.DrawImage(
image,
destRect,
srcX,
srcY,
srcWidth,
srcHeight,
srcUnit,
imageAttr
);
}
///
/// Draws an ellipse defined by a bounding rectangle specified by
/// a pair of coordinates, a height, and a width.
///
/// Pen object that determines the color, width, and style of the ellipse.
/// x-coordinate of the upper-left corner of the bounding rectangle that defines the ellipse.
/// y-coordinate of the upper-left corner of the bounding rectangle that defines the ellipse.
/// Width of the bounding rectangle that defines the ellipse.
/// Height of the bounding rectangle that defines the ellipse.
internal void DrawEllipse(
Pen pen,
float x,
float y,
float width,
float height
)
{
RenderingObject.DrawEllipse( pen, x, y, width, height );
}
///
/// Draws a cardinal spline through a specified array of PointF structures
/// using a specified tension. The drawing begins offset from
/// the beginning of the array.
///
/// Pen object that determines the color, width, and height of the curve.
/// Array of PointF structures that define the spline.
/// Offset from the first element in the array of the points parameter to the starting point in the curve.
/// Number of segments after the starting point to include in the curve.
/// Value greater than or equal to 0.0F that specifies the tension of the curve.
internal void DrawCurve(
Pen pen,
PointF[] points,
int offset,
int numberOfSegments,
float tension
)
{
ChartGraphics chartGraphics = this as ChartGraphics;
if (chartGraphics == null || !chartGraphics.IsMetafile)
{
RenderingObject.DrawCurve(pen, points, offset, numberOfSegments, tension);
}
else
{
// Special handling required for the metafiles. We cannot pass large array of
// points because they will be persisted inside EMF file and cause exponential
// increase in emf file size. Draw curve method uses additional 2, 3 or 4 points
// depending on which segement is drawn.
PointF[] pointsExact = null;
if (offset == 0 && numberOfSegments == points.Length - 1)
{
// In case the array contains the minimum required number of points
// to draw segments - just call the curve drawing method
RenderingObject.DrawCurve(pen, points, offset, numberOfSegments, tension);
}
else
{
if (offset == 0 && numberOfSegments < points.Length - 1)
{
// Segment is at the beginning of the array with more points following
pointsExact = new PointF[numberOfSegments + 2];
for (int index = 0; index < numberOfSegments + 2; index++)
{
pointsExact[index] = points[index];
}
}
else if (offset > 0 && (offset + numberOfSegments) == points.Length - 1)
{
// Segment is at the end of the array with more points prior to it
pointsExact = new PointF[numberOfSegments + 2];
for (int index = 0; index < numberOfSegments + 2; index++)
{
pointsExact[index] = points[offset + index - 1];
}
offset = 1;
}
else if (offset > 0 && (offset + numberOfSegments) < points.Length - 1)
{
// Segment in the middle of the array with points prior and following it
pointsExact = new PointF[numberOfSegments + 3];
for (int index = 0; index < numberOfSegments + 3; index++)
{
pointsExact[index] = points[offset + index - 1];
}
offset = 1;
}
// Render the curve using minimum number of required points in the array
RenderingObject.DrawCurve(pen, pointsExact, offset, numberOfSegments, tension);
}
}
}
///
/// Draws a rectangle specified by a coordinate pair, a width, and a height.
///
/// Pen object that determines the color, width, and style of the rectangle.
/// x-coordinate of the upper-left corner of the rectangle to draw.
/// y-coordinate of the upper-left corner of the rectangle to draw.
/// Width of the rectangle to draw.
/// Height of the rectangle to draw.
internal void DrawRectangle(
Pen pen,
int x,
int y,
int width,
int height
)
{
RenderingObject.DrawRectangle( pen, x, y, width, height );
}
///
/// Draws a polygon defined by an array of PointF structures.
///
/// Pen object that determines the color, width, and style of the polygon.
/// Array of PointF structures that represent the vertices of the polygon.
internal void DrawPolygon(
Pen pen,
PointF[] points
)
{
RenderingObject.DrawPolygon( pen, points );
}
///
/// Draws the specified text string in the specified rectangle with the specified Brush and Font objects using the formatting properties of the specified StringFormat object.
///
/// String to draw.
/// Font object that defines the text format of the string.
/// Brush object that determines the color and texture of the drawn text.
/// RectangleF structure that specifies the location of the drawn text.
/// StringFormat object that specifies formatting properties, such as line spacing and alignment, that are applied to the drawn text.
internal void DrawString(
string s,
Font font,
Brush brush,
RectangleF layoutRectangle,
StringFormat format
)
{
using (StringFormat fmt = (StringFormat)format.Clone())
{
if ( IsRightToLeft )
fmt.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
if (!IsTextClipped && (fmt.FormatFlags & StringFormatFlags.NoClip) != StringFormatFlags.NoClip)
fmt.FormatFlags |= StringFormatFlags.NoClip;
RenderingObject.DrawString(s, font, brush, layoutRectangle, fmt);
}
}
///
/// Draws the specified text string at the specified location with the specified Brush and Font objects using the formatting properties of the specified StringFormat object.
///
/// String to draw.
/// Font object that defines the text format of the string.
/// Brush object that determines the color and texture of the drawn text.
/// PointF structure that specifies the upper-left corner of the drawn text.
/// StringFormat object that specifies formatting properties, such as line spacing and alignment, that are applied to the drawn text.
internal void DrawString(
string s,
Font font,
Brush brush,
PointF point,
StringFormat format
)
{
if (IsRightToLeft)
{
using (StringFormat fmt = (StringFormat)format.Clone())
{
fmt.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
if (fmt.Alignment == StringAlignment.Far)
{
fmt.Alignment = StringAlignment.Near;
}
else if (fmt.Alignment == StringAlignment.Near)
{
fmt.Alignment = StringAlignment.Far;
}
RenderingObject.DrawString(s, font, brush, point, fmt);
}
}
else
RenderingObject.DrawString(s, font, brush, point, format);
}
///
/// Draws the specified portion of the specified Image object at the specified location and with the specified size.
///
/// Image object to draw.
/// Rectangle structure that specifies the location and size of the drawn image. The image is scaled to fit the rectangle.
/// x-coordinate of the upper-left corner of the portion of the source image to draw.
/// y-coordinate of the upper-left corner of the portion of the source image to draw.
/// Width of the portion of the source image to draw.
/// Height of the portion of the source image to draw.
/// Member of the GraphicsUnit enumeration that specifies the units of measure used to determine the source rectangle.
/// ImageAttributes object that specifies recoloring and gamma information for the image object.
internal void DrawImage(
System.Drawing.Image image,
Rectangle destRect,
float srcX,
float srcY,
float srcWidth,
float srcHeight,
GraphicsUnit srcUnit,
ImageAttributes imageAttrs
)
{
RenderingObject.DrawImage( image, destRect, srcX, srcY, srcWidth, srcHeight, srcUnit, imageAttrs );
}
///
/// Draws a rectangle specified by a coordinate pair, a width, and a height.
///
/// A Pen object that determines the color, width, and style of the rectangle.
/// The x-coordinate of the upper-left corner of the rectangle to draw.
/// The y-coordinate of the upper-left corner of the rectangle to draw.
/// The width of the rectangle to draw.
/// The height of the rectangle to draw.
internal void DrawRectangle(
Pen pen,
float x,
float y,
float width,
float height
)
{
RenderingObject.DrawRectangle( pen, x, y, width, height );
}
///
/// Draws a GraphicsPath object.
///
/// Pen object that determines the color, width, and style of the path.
/// GraphicsPath object to draw.
internal void DrawPath(
Pen pen,
GraphicsPath path
)
{
// Check if path is empty
if(path == null ||
path.PointCount == 0)
{
return;
}
RenderingObject.DrawPath( pen, path );
}
///
/// Draws a pie shape defined by an ellipse specified by a coordinate pair, a width, and a height and two radial lines.
///
/// Pen object that determines the color, width, and style of the pie shape.
/// x-coordinate of the upper-left corner of the bounding rectangle that defines the ellipse from which the pie shape comes.
/// y-coordinate of the upper-left corner of the bounding rectangle that defines the ellipse from which the pie shape comes.
/// Width of the bounding rectangle that defines the ellipse from which the pie shape comes.
/// Height of the bounding rectangle that defines the ellipse from which the pie shape comes.
/// Angle measured in degrees clockwise from the x-axis to the first side of the pie shape.
/// Angle measured in degrees clockwise from the startAngle parameter to the second side of the pie shape.
internal void DrawPie(
Pen pen,
float x,
float y,
float width,
float height,
float startAngle,
float sweepAngle
)
{
RenderingObject.DrawPie( pen, x, y, width, height, startAngle, sweepAngle );
}
///
/// Draws an ellipse defined by a bounding RectangleF.
///
/// Pen object that determines the color, width, and style of the ellipse.
/// RectangleF structure that defines the boundaries of the ellipse.
internal void DrawEllipse(
Pen pen,
RectangleF rect
)
{
RenderingObject.DrawEllipse( pen, rect );
}
///
/// Draws a series of line segments that connect an array of PointF structures.
///
/// Pen object that determines the color, width, and style of the line segments.
/// Array of PointF structures that represent the points to connect.
internal void DrawLines(
Pen pen,
PointF[] points
)
{
RenderingObject.DrawLines( pen, points );
}
#endregion // Drawing Methods
#region Filling Methods
///
/// Fills the interior of an ellipse defined by a bounding rectangle
/// specified by a RectangleF structure.
///
/// Brush object that determines the characteristics of the fill.
/// RectangleF structure that represents the bounding rectangle that defines the ellipse.
internal void FillEllipse(
Brush brush,
RectangleF rect
)
{
RenderingObject.FillEllipse( brush, rect );
}
///
/// Fills the interior of a GraphicsPath object.
///
/// Brush object that determines the characteristics of the fill.
/// GraphicsPath object that represents the path to fill.
internal void FillPath(
Brush brush,
GraphicsPath path
)
{
// Check if path is empty
if(path == null ||
path.PointCount == 0)
{
return;
}
RenderingObject.FillPath( brush, path );
}
///
/// Fills the interior of a Region object.
///
/// Brush object that determines the characteristics of the fill.
/// Region object that represents the area to fill.
internal void FillRegion(
Brush brush,
Region region
)
{
RenderingObject.FillRegion( brush, region );
}
///
/// Fills the interior of a rectangle specified by a RectangleF structure.
///
/// Brush object that determines the characteristics of the fill.
/// RectangleF structure that represents the rectangle to fill.
internal void FillRectangle(
Brush brush,
RectangleF rect
)
{
RenderingObject.FillRectangle( brush, rect );
}
///
/// Fills the interior of a rectangle specified by a pair of coordinates, a width, and a height.
///
/// Brush object that determines the characteristics of the fill.
/// x-coordinate of the upper-left corner of the rectangle to fill.
/// y-coordinate of the upper-left corner of the rectangle to fill.
/// Width of the rectangle to fill.
/// Height of the rectangle to fill.
internal void FillRectangle(
Brush brush,
float x,
float y,
float width,
float height
)
{
RenderingObject.FillRectangle( brush, x, y, width, height );
}
///
/// Fills the interior of a polygon defined by an array of points specified by PointF structures .
///
/// Brush object that determines the characteristics of the fill.
/// Array of PointF structures that represent the vertices of the polygon to fill.
internal void FillPolygon(
Brush brush,
PointF[] points
)
{
RenderingObject.FillPolygon( brush, points );
}
///
/// Fills the interior of a pie section defined by an ellipse
/// specified by a pair of coordinates, a width, and a height
/// and two radial lines.
///
/// Brush object that determines the characteristics of the fill.
/// x-coordinate of the upper-left corner of the bounding rectangle that defines the ellipse from which the pie section comes.
/// y-coordinate of the upper-left corner of the bounding rectangle that defines the ellipse from which the pie section comes.
/// Width of the bounding rectangle that defines the ellipse from which the pie section comes.
/// Height of the bounding rectangle that defines the ellipse from which the pie section comes.
/// Angle in degrees measured clockwise from the x-axis to the first side of the pie section.
/// Angle in degrees measured clockwise from the startAngle parameter to the second side of the pie section.
internal void FillPie(
Brush brush,
float x,
float y,
float width,
float height,
float startAngle,
float sweepAngle
)
{
RenderingObject.FillPie( brush, x, y, width, height, startAngle, sweepAngle );
}
#endregion // Filling Methods
#region Other Methods
///
/// This method starts SVG Selection mode
///
/// The location of the referenced object, expressed as a URI reference.
/// Title which could be used for tooltips.
internal void StartHotRegion( string url, string title )
{
RenderingObject.BeginSelection( url, title );
}
///
/// This method starts SVG Selection mode
///
/// Data Point which properties are used for SVG selection
internal void StartHotRegion(DataPoint point)
{
StartHotRegion( point, false );
}
///
/// This method starts SVG Selection mode
///
/// Data Point which properties are used for SVG selection
/// Indicates if point label region is processed.
internal void StartHotRegion(DataPoint point, bool labelRegion)
{
string hRef = string.Empty;
string tooltip = (labelRegion) ? point.LabelToolTip : point.ToolTip;
#if !Microsoft_CONTROL
hRef = (labelRegion) ? point.LabelUrl : point.Url;
#endif
if(hRef.Length > 0 || tooltip.Length > 0)
{
RenderingObject.BeginSelection(
point.ReplaceKeywords( hRef ),
point.ReplaceKeywords( tooltip ) );
}
}
///
/// This method stops SVG Selection mode
///
internal void EndHotRegion()
{
RenderingObject.EndSelection();
}
///
/// Measures the specified string when drawn with the specified
/// Font object and formatted with the specified StringFormat object.
///
/// String to measure.
/// Font object defines the text format of the string.
/// SizeF structure that specifies the maximum layout area for the text.
/// StringFormat object that represents formatting information, such as line spacing, for the string.
/// This method returns a SizeF structure that represents the size, in pixels, of the string specified in the text parameter as drawn with the font parameter and the stringFormat parameter.
internal SizeF MeasureString(
string text,
Font font,
SizeF layoutArea,
StringFormat stringFormat
)
{
return RenderingObject.MeasureString( text, font, layoutArea, stringFormat );
}
///
/// Measures the specified string when drawn with the specified
/// Font object and formatted with the specified StringFormat object.
///
/// String to measure.
/// Font object defines the text format of the string.
/// This method returns a SizeF structure that represents the size, in pixels, of the string specified in the text parameter as drawn with the font parameter and the stringFormat parameter.
internal SizeF MeasureString(
string text,
Font font
)
{
return RenderingObject.MeasureString( text, font );
}
///
/// Saves the current state of this Graphics object and identifies the saved state with a GraphicsState object.
///
/// This method returns a GraphicsState object that represents the saved state of this Graphics object.
internal GraphicsState Save()
{
return RenderingObject.Save();
}
///
/// Restores the state of this Graphics object to the state represented by a GraphicsState object.
///
/// GraphicsState object that represents the state to which to restore this Graphics object.
internal void Restore(
GraphicsState gstate
)
{
RenderingObject.Restore( gstate );
}
///
/// Resets the clip region of this Graphics object to an infinite region.
///
internal void ResetClip()
{
RenderingObject.ResetClip();
}
///
/// Sets the clipping region of this Graphics object to the rectangle specified by a RectangleF structure.
///
/// RectangleF structure that represents the new clip region.
internal void SetClipAbs(RectangleF rect)
{
RenderingObject.SetClip( rect );
}
///
/// Prepends the specified translation to the transformation matrix of this Graphics object.
///
/// x component of the translation.
/// y component of the translation.
internal void TranslateTransform(
float dx,
float dy
)
{
RenderingObject.TranslateTransform( dx, dy );
}
#endregion // Other Methods
#region Properties
///
/// Gets current rendering object.
///
internal IChartRenderingEngine RenderingObject
{
get
{
return _gdiGraphics;
}
}
///
/// Gets the active rendering type.
///
internal RenderingType ActiveRenderingType
{
get
{
return _activeRenderingType;
}
}
///
/// Gets or sets the rendering mode for text associated with this Graphics object.
///
internal TextRenderingHint TextRenderingHint
{
get
{
return RenderingObject.TextRenderingHint;
}
set
{
RenderingObject.TextRenderingHint = value;
}
}
///
/// Gets or sets the world transformation for this Graphics object.
///
internal Matrix Transform
{
get
{
return RenderingObject.Transform;
}
set
{
RenderingObject.Transform = value;
}
}
///
/// Gets or sets the rendering quality for this Graphics object.
///
internal SmoothingMode SmoothingMode
{
get
{
return RenderingObject.SmoothingMode;
}
set
{
RenderingObject.SmoothingMode = value;
}
}
///
/// Gets or sets a Region object that limits the drawing region of this Graphics object.
///
internal Region Clip
{
get
{
return RenderingObject.Clip;
}
set
{
RenderingObject.Clip = value;
}
}
///
/// Gets a value indicating whether the clipping region of this Graphics object is empty.
///
internal bool IsClipEmpty {
get
{
return RenderingObject.IsClipEmpty;
}
}
///
/// Gets or sets the reference to the Graphics object.
///
public Graphics Graphics
{
get
{
return RenderingObject.Graphics;
}
set
{
RenderingObject.Graphics = value;
}
}
#endregion // Properties
}
}