File size: 21,092 Bytes
b1b3bae |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 |
//============================================================================
//ZedGraph Class Library - A Flexible Line Graph/Bar Graph Library in C#
//Copyright � 2004 John Champion
//
//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.
//
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//General Public License for more details.
//
//You should have received a copy of the GNU General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//=============================================================================
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.Runtime.Serialization;
using System.Security.Permissions;
namespace ZedGraph
{
/// <summary>
/// A class representing all the characteristics of the bar
/// segments that make up a curve on the graph.
/// </summary>
///
/// <author> John Champion </author>
/// <version> $Revision: 3.30 $ $Date: 2007-11-03 04:41:28 $ </version>
[Serializable]
public class Bar : ICloneable, ISerializable
{
#region Fields
/// <summary>
/// Private field that stores the <see cref="ZedGraph.Fill"/> data for this
/// <see cref="Bar"/>. Use the public property <see cref="Fill"/> to
/// access this value.
/// </summary>
private Fill _fill;
/// <summary>
/// Private field that stores the <see cref="Border"/> class that defines the
/// properties of the border around this <see cref="BarItem"/>. Use the public
/// property <see cref="Border"/> to access this value.
/// </summary>
private Border _border;
#endregion
#region Defaults
/// <summary>
/// A simple struct that defines the
/// default property values for the <see cref="Bar"/> class.
/// </summary>
public struct Default
{
// Default Bar properties
/// <summary>
/// The default pen width to be used for drawing the border around the bars
/// (<see cref="ZedGraph.LineBase.Width"/> property). Units are points.
/// </summary>
public static float BorderWidth = 1.0F;
/// <summary>
/// The default fill mode for bars (<see cref="ZedGraph.Fill.Type"/> property).
/// </summary>
public static FillType FillType = FillType.Brush;
/// <summary>
/// The default border mode for bars (<see cref="ZedGraph.LineBase.IsVisible"/> property).
/// true to display frames around bars, false otherwise
/// </summary>
public static bool IsBorderVisible = true;
/// <summary>
/// The default color for drawing frames around bars
/// (<see cref="ZedGraph.LineBase.Color"/> property).
/// </summary>
public static Color BorderColor = Color.Black;
/// <summary>
/// The default color for filling in the bars
/// (<see cref="ZedGraph.Fill.Color"/> property).
/// </summary>
public static Color FillColor = Color.Red;
/// <summary>
/// The default custom brush for filling in the bars
/// (<see cref="ZedGraph.Fill.Brush"/> property).
/// </summary>
public static Brush FillBrush = null; //new LinearGradientBrush( new Rectangle(0,0,100,100),
// Color.White, Color.Red, 0F );
}
#endregion
#region Constructors
/// <summary>
/// Default constructor that sets all <see cref="Bar"/> properties to default
/// values as defined in the <see cref="Default"/> class.
/// </summary>
public Bar() : this( Color.Empty )
{
}
/// <summary>
/// Default constructor that sets the
/// <see cref="Color"/> as specified, and the remaining
/// <see cref="Bar"/> properties to default
/// values as defined in the <see cref="Default"/> class.
/// The specified color is only applied to the
/// <see cref="ZedGraph.Fill.Color"/>, and the <see cref="ZedGraph.LineBase.Color"/>
/// will be defaulted.
/// </summary>
/// <param name="color">A <see cref="Color"/> value indicating
/// the <see cref="ZedGraph.Fill.Color"/>
/// of the Bar.
/// </param>
public Bar( Color color )
{
_border = new Border( Default.IsBorderVisible, Default.BorderColor, Default.BorderWidth );
_fill = new Fill( color.IsEmpty ? Default.FillColor : color,
Default.FillBrush, Default.FillType );
}
/// <summary>
/// The Copy Constructor
/// </summary>
/// <param name="rhs">The Bar object from which to copy</param>
public Bar( Bar rhs )
{
_border = (Border) rhs.Border.Clone();
_fill = (Fill) rhs.Fill.Clone();
}
/// <summary>
/// Implement the <see cref="ICloneable" /> interface in a typesafe manner by just
/// calling the typed version of <see cref="Clone" />
/// </summary>
/// <returns>A deep copy of this object</returns>
object ICloneable.Clone()
{
return this.Clone();
}
/// <summary>
/// Typesafe, deep-copy clone method.
/// </summary>
/// <returns>A new, independent copy of this class</returns>
public Bar Clone()
{
return new Bar( this );
}
#endregion
#region Serialization
/// <summary>
/// Current schema value that defines the version of the serialized file
/// </summary>
public const int schema = 10;
/// <summary>
/// Constructor for deserializing objects
/// </summary>
/// <param name="info">A <see cref="SerializationInfo"/> instance that defines the serialized data
/// </param>
/// <param name="context">A <see cref="StreamingContext"/> instance that contains the serialized data
/// </param>
protected Bar( SerializationInfo info, StreamingContext context )
{
// The schema value is just a file version parameter. You can use it to make future versions
// backwards compatible as new member variables are added to classes
int sch = info.GetInt32( "schema" );
_fill = (Fill) info.GetValue( "fill", typeof(Fill) );
_border = (Border) info.GetValue( "border", typeof(Border) );
}
/// <summary>
/// Populates a <see cref="SerializationInfo"/> instance with the data needed to serialize the target object
/// </summary>
/// <param name="info">A <see cref="SerializationInfo"/> instance that defines the serialized data</param>
/// <param name="context">A <see cref="StreamingContext"/> instance that contains the serialized data</param>
[SecurityPermissionAttribute(SecurityAction.Demand,SerializationFormatter=true)]
public virtual void GetObjectData( SerializationInfo info, StreamingContext context )
{
info.AddValue( "schema", schema );
info.AddValue( "fill", _fill );
info.AddValue( "border", _border );
}
#endregion
#region Properties
/// <summary>
/// The <see cref="Border"/> object used to draw the border around the <see cref="Bar"/>.
/// </summary>
/// <seealso cref="Default.IsBorderVisible"/>
/// <seealso cref="Default.BorderWidth"/>
/// <seealso cref="Default.BorderColor"/>
public Border Border
{
get { return _border; }
set { _border = value; }
}
/// <summary>
/// Gets or sets the <see cref="ZedGraph.Fill"/> data for this
/// <see cref="Bar"/>.
/// </summary>
public Fill Fill
{
get { return _fill; }
set { _fill = value; }
}
#endregion
#region Rendering Methods
/// <summary>
/// Draw the <see cref="Bar"/> to the specified <see cref="Graphics"/> device
/// at the specified location. This routine draws a single bar.
/// </summary>
/// <param name="g">
/// A graphic device object to be drawn into. This is normally e.Graphics from the
/// PaintEventArgs argument to the Paint() method.
/// </param>
/// <param name="pane">
/// A reference to the <see cref="ZedGraph.GraphPane"/> object that is the parent or
/// owner of this object.
/// </param>
/// <param name="left">The x position of the left side of the bar in
/// pixel units</param>
/// <param name="right">The x position of the right side of the bar in
/// pixel units</param>
/// <param name="top">The y position of the top of the bar in
/// pixel units</param>
/// <param name="bottom">The y position of the bottom of the bar in
/// pixel units</param>
/// <param name="scaleFactor">
/// The scaling factor for the features of the graph based on the <see cref="PaneBase.BaseDimension"/>. This
/// scaling factor is calculated by the <see cref="PaneBase.CalcScaleFactor"/> method. The scale factor
/// represents a linear multiple to be applied to font sizes, symbol sizes, etc.
/// </param>
/// <param name="fullFrame">true to draw the bottom portion of the border around the
/// bar (this is for legend entries)</param>
/// <param name="dataValue">The data value to be used for a value-based
/// color gradient. This is only applicable for <see cref="FillType.GradientByX"/>,
/// <see cref="FillType.GradientByY"/> or <see cref="FillType.GradientByZ"/>.</param>
/// <param name="isSelected">Indicates that the <see cref="Bar" /> should be drawn
/// with attributes from the <see cref="Selection" /> class.
/// </param>
public void Draw( Graphics g, GraphPane pane, float left, float right, float top,
float bottom, float scaleFactor, bool fullFrame, bool isSelected,
PointPair dataValue )
{
// Do a sanity check to make sure the top < bottom. If not, reverse them
if ( top > bottom )
{
float junk = top;
top = bottom;
bottom = junk;
}
// Do a sanity check to make sure the left < right. If not, reverse them
if ( left > right )
{
float junk = right;
right = left;
left = junk;
}
if ( top < -10000 )
top = -10000;
else if ( top > 10000 )
top = 10000;
if ( left < -10000 )
left = -10000;
else if ( left > 10000 )
left = 10000;
if ( right < -10000 )
right = -10000;
else if ( right > 10000 )
right = 10000;
if ( bottom < -10000 )
bottom = -10000;
else if ( bottom > 10000 )
bottom = 10000;
// Make a rectangle for the bar and draw it
RectangleF rect = new RectangleF( left, top, right - left, bottom - top );
Draw( g, pane, rect, scaleFactor, fullFrame, isSelected, dataValue );
}
/// <summary>
/// Draw the <see cref="Bar"/> to the specified <see cref="Graphics"/> device
/// at the specified location. This routine draws a single bar.
/// </summary>
/// <param name="g">
/// A graphic device object to be drawn into. This is normally e.Graphics from the
/// PaintEventArgs argument to the Paint() method.
/// </param>
/// <param name="pane">
/// A reference to the <see cref="ZedGraph.GraphPane"/> object that is the parent or
/// owner of this object.
/// </param>
/// <param name="rect">The rectangle (pixels) to contain the bar</param>
/// <param name="scaleFactor">
/// The scaling factor for the features of the graph based on the <see cref="PaneBase.BaseDimension"/>. This
/// scaling factor is calculated by the <see cref="PaneBase.CalcScaleFactor"/> method. The scale factor
/// represents a linear multiple to be applied to font sizes, symbol sizes, etc.
/// </param>
/// <param name="fullFrame">true to draw the bottom portion of the border around the
/// bar (this is for legend entries)</param>
/// <param name="dataValue">The data value to be used for a value-based
/// color gradient. This is only applicable for <see cref="FillType.GradientByX"/>,
/// <see cref="FillType.GradientByY"/> or <see cref="FillType.GradientByZ"/>.</param>
/// <param name="isSelected">Indicates that the <see cref="Bar" /> should be drawn
/// with attributes from the <see cref="Selection" /> class.
/// </param>
public void Draw( Graphics g, GraphPane pane, RectangleF rect, float scaleFactor,
bool fullFrame, bool isSelected, PointPair dataValue )
{
if ( isSelected )
{
Selection.Fill.Draw( g, rect, dataValue );
Selection.Border.Draw( g, pane, scaleFactor, rect );
}
else
{
_fill.Draw( g, rect, dataValue );
_border.Draw( g, pane, scaleFactor, rect );
}
}
/// <summary>
/// Draw the this <see cref="Bar"/> to the specified <see cref="Graphics"/>
/// device as a bar at each defined point. This method
/// is normally only called by the <see cref="BarItem.Draw"/> method of the
/// <see cref="BarItem"/> object
/// </summary>
/// <param name="g">
/// A graphic device object to be drawn into. This is normally e.Graphics from the
/// PaintEventArgs argument to the Paint() method.
/// </param>
/// <param name="pane">
/// A reference to the <see cref="GraphPane"/> object that is the parent or
/// owner of this object.
/// </param>
/// <param name="curve">A <see cref="CurveItem"/> object representing the
/// <see cref="Bar"/>'s to be drawn.</param>
/// <param name="baseAxis">The <see cref="Axis"/> class instance that defines the base (independent)
/// axis for the <see cref="Bar"/></param>
/// <param name="valueAxis">The <see cref="Axis"/> class instance that defines the value (dependent)
/// axis for the <see cref="Bar"/></param>
/// <param name="barWidth">
/// The width of each bar, in pixels.
/// </param>
/// <param name="pos">
/// The ordinal position of the this bar series (0=first bar, 1=second bar, etc.)
/// in the cluster of bars.
/// </param>
/// <param name="scaleFactor">
/// The scaling factor to be used for rendering objects. This is calculated and
/// passed down by the parent <see cref="GraphPane"/> object using the
/// <see cref="PaneBase.CalcScaleFactor"/> method, and is used to proportionally adjust
/// font sizes, etc. according to the actual size of the graph.
/// </param>
public void DrawBars( Graphics g, GraphPane pane, CurveItem curve,
Axis baseAxis, Axis valueAxis,
float barWidth, int pos, float scaleFactor )
{
// For non-cluster bar types, the position is always zero since the bars are on top
// of eachother
BarType barType = pane._barSettings.Type;
if ( barType == BarType.Overlay || barType == BarType.Stack || barType == BarType.PercentStack ||
barType == BarType.SortedOverlay )
pos = 0;
// Loop over each defined point and draw the corresponding bar
for ( int i=0; i<curve.Points.Count; i++ )
DrawSingleBar( g, pane, curve, i, pos, baseAxis, valueAxis, barWidth, scaleFactor );
}
/// <summary>
/// Draw the specified single bar (an individual "point") of this series to the specified
/// <see cref="Graphics"/> device. This method is not as efficient as
/// <see cref="DrawBars"/>, which draws the bars for all points. It is intended to be used
/// only for <see cref="BarType.SortedOverlay"/>, which requires special handling of each bar.
/// </summary>
/// <param name="g">
/// A graphic device object to be drawn into. This is normally e.Graphics from the
/// PaintEventArgs argument to the Paint() method.
/// </param>
/// <param name="pane">
/// A reference to the <see cref="GraphPane"/> object that is the parent or
/// owner of this object.
/// </param>
/// <param name="curve">A <see cref="CurveItem"/> object representing the
/// <see cref="Bar"/>'s to be drawn.</param>
/// <param name="baseAxis">The <see cref="Axis"/> class instance that defines the base (independent)
/// axis for the <see cref="Bar"/></param>
/// <param name="valueAxis">The <see cref="Axis"/> class instance that defines the value (dependent)
/// axis for the <see cref="Bar"/></param>
/// <param name="pos">
/// The ordinal position of the this bar series (0=first bar, 1=second bar, etc.)
/// in the cluster of bars.
/// </param>
/// <param name="index">
/// The zero-based index number for the single bar to be drawn.
/// </param>
/// <param name="barWidth">
/// The width of each bar, in pixels.
/// </param>
/// <param name="scaleFactor">
/// The scaling factor to be used for rendering objects. This is calculated and
/// passed down by the parent <see cref="GraphPane"/> object using the
/// <see cref="PaneBase.CalcScaleFactor"/> method, and is used to proportionally adjust
/// font sizes, etc. according to the actual size of the graph.
/// </param>
public void DrawSingleBar( Graphics g, GraphPane pane, CurveItem curve,
Axis baseAxis, Axis valueAxis,
int pos, int index, float barWidth, float scaleFactor )
{
// Make sure that a bar value exists for the current curve and current ordinal position
if ( index >= curve.Points.Count )
return;
// For Overlay and Stack bars, the position is always zero since the bars are on top
// of eachother
if ( pane._barSettings.Type == BarType.Overlay || pane._barSettings.Type == BarType.Stack ||
pane._barSettings.Type == BarType.PercentStack )
pos = 0;
// Draw the specified bar
DrawSingleBar( g, pane, curve, index, pos, baseAxis, valueAxis, barWidth, scaleFactor );
}
/// <summary>
/// Protected internal routine that draws the specified single bar (an individual "point")
/// of this series to the specified <see cref="Graphics"/> device.
/// </summary>
/// <param name="g">
/// A graphic device object to be drawn into. This is normally e.Graphics from the
/// PaintEventArgs argument to the Paint() method.
/// </param>
/// <param name="pane">
/// A reference to the <see cref="GraphPane"/> object that is the parent or
/// owner of this object.
/// </param>
/// <param name="curve">A <see cref="CurveItem"/> object representing the
/// <see cref="Bar"/>'s to be drawn.</param>
/// <param name="index">
/// The zero-based index number for the single bar to be drawn.
/// </param>
/// <param name="pos">
/// The ordinal position of the this bar series (0=first bar, 1=second bar, etc.)
/// in the cluster of bars.
/// </param>
/// <param name="baseAxis">The <see cref="Axis"/> class instance that defines the base (independent)
/// axis for the <see cref="Bar"/></param>
/// <param name="valueAxis">The <see cref="Axis"/> class instance that defines the value (dependent)
/// axis for the <see cref="Bar"/></param>
/// <param name="barWidth">
/// The width of each bar, in pixels.
/// </param>
/// <param name="scaleFactor">
/// The scaling factor to be used for rendering objects. This is calculated and
/// passed down by the parent <see cref="GraphPane"/> object using the
/// <see cref="PaneBase.CalcScaleFactor"/> method, and is used to proportionally adjust
/// font sizes, etc. according to the actual size of the graph.
/// </param>
virtual protected void DrawSingleBar( Graphics g, GraphPane pane,
CurveItem curve,
int index, int pos, Axis baseAxis, Axis valueAxis,
float barWidth, float scaleFactor )
{
// pixBase = pixel value for the bar center on the base axis
// pixHiVal = pixel value for the bar top on the value axis
// pixLowVal = pixel value for the bar bottom on the value axis
float pixBase, pixHiVal, pixLowVal;
float clusterWidth = pane.BarSettings.GetClusterWidth();
//float barWidth = curve.GetBarWidth( pane );
float clusterGap = pane._barSettings.MinClusterGap * barWidth;
float barGap = barWidth * pane._barSettings.MinBarGap;
// curBase = the scale value on the base axis of the current bar
// curHiVal = the scale value on the value axis of the current bar
// curLowVal = the scale value of the bottom of the bar
double curBase, curLowVal, curHiVal;
ValueHandler valueHandler = new ValueHandler( pane, false );
valueHandler.GetValues( curve, index, out curBase, out curLowVal, out curHiVal );
// Any value set to double max is invalid and should be skipped
// This is used for calculated values that are out of range, divide
// by zero, etc.
// Also, any value <= zero on a log scale is invalid
if ( !curve.Points[index].IsInvalid )
{
// calculate a pixel value for the top of the bar on value axis
pixLowVal = valueAxis.Scale.Transform( curve.IsOverrideOrdinal, index, curLowVal );
pixHiVal = valueAxis.Scale.Transform( curve.IsOverrideOrdinal, index, curHiVal );
// calculate a pixel value for the center of the bar on the base axis
pixBase = baseAxis.Scale.Transform( curve.IsOverrideOrdinal, index, curBase );
// Calculate the pixel location for the side of the bar (on the base axis)
float pixSide = pixBase - clusterWidth / 2.0F + clusterGap / 2.0F +
pos * ( barWidth + barGap );
// Draw the bar
if ( pane._barSettings.Base == BarBase.X )
this.Draw( g, pane, pixSide, pixSide + barWidth, pixLowVal,
pixHiVal, scaleFactor, true, curve.IsSelected,
curve.Points[index] );
else
this.Draw( g, pane, pixLowVal, pixHiVal, pixSide, pixSide + barWidth,
scaleFactor, true, curve.IsSelected,
curve.Points[index] );
}
}
#endregion
}
}
|