File size: 35,366 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 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 |
//============================================================================
//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.Runtime.Serialization;
using System.Security.Permissions;
namespace ZedGraph
{
/// <summary>
/// This class encapsulates the chart <see cref="Legend"/> that is displayed
/// in the <see cref="GraphPane"/>
/// </summary>
///
/// <author> John Champion </author>
/// <version> $Revision: 3.41 $ $Date: 2007-08-11 19:24:55 $ </version>
[Serializable]
public class Legend : ICloneable, ISerializable
{
#region private Fields
/// <summary> Private field to hold the bounding rectangle around the legend.
/// This bounding rectangle varies with the number of legend entries, font sizes,
/// etc., and is re-calculated by <see cref="Legend.CalcRect"/> at each redraw.
/// Use the public readonly property <see cref="Legend.Rect"/> to access this
/// rectangle.
/// </summary>
private RectangleF _rect;
/// <summary>Private field to hold the legend location setting. This field
/// contains the <see cref="LegendPos"/> enum type to specify the area of
/// the graph where the legend will be positioned. Use the public property
/// <see cref="LegendPos"/> to access this value.
/// </summary>
/// <seealso cref="Default.Position"/>
private LegendPos _position;
/// <summary>
/// Private field to enable/disable horizontal stacking of the legend entries.
/// If this value is false, then the legend entries will always be a single column.
/// Use the public property <see cref="IsHStack"/> to access this value.
/// </summary>
/// <seealso cref="Default.IsHStack"/>
private bool _isHStack;
/// <summary>
/// Private field to enable/disable drawing of the entire legend.
/// If this value is false, then the legend will not be drawn.
/// Use the public property <see cref="IsVisible"/> to access this value.
/// </summary>
private bool _isVisible;
/// <summary>
/// Private field that stores the <see cref="ZedGraph.Fill"/> data for this
/// <see cref="Legend"/>. Use the public property <see cref="Fill"/> to
/// access this value.
/// </summary>
private Fill _fill;
/// <summary>
/// Private field that stores the <see cref="ZedGraph.Border"/> data for this
/// <see cref="Legend"/>. Use the public property <see cref="Border"/> to
/// access this value.
/// </summary>
private Border _border;
/// <summary>
/// Private field to maintain the <see cref="FontSpec"/> class that
/// maintains font attributes for the entries in this legend. Use
/// the <see cref="FontSpec"/> property to access this class.
/// </summary>
private FontSpec _fontSpec;
/// <summary>
/// Private field to maintain the <see cref="Legend"/> location. This object
/// is only applicable if the <see cref="Position"/> property is set to
/// <see cref="LegendPos.Float"/>.
/// </summary>
private Location _location;
/// <summary>
/// Private temporary field to maintain the number of columns (horizontal stacking) to be used
/// for drawing the <see cref="Legend"/>. This value is only valid during a draw operation.
/// </summary>
private int _hStack;
/// <summary>
/// Private temporary field to maintain the width of each column in the
/// <see cref="Legend"/>. This value is only valid during a draw operation.
/// </summary>
private float _legendItemWidth;
/// <summary>
/// Private temporary field to maintain the height of each row in the
/// <see cref="Legend"/>. This value is only valid during a draw operation.
/// </summary>
private float _legendItemHeight;
/// <summary>
/// Private field to store the gap between the legend and the chart rectangle.
/// </summary>
private float _gap;
// CJBL
/// <summary>
/// Private field to select output order of legend entries.
/// </summary>
private bool _isReverse;
/// <summary>
/// Private temporary field to maintain the characteristic "gap" for the legend.
/// This is normal the height of the largest font in the legend.
/// This value is only valid during a draw operation.
/// </summary>
private float _tmpSize;
/// <summary>
/// Private field to enable/diable drawing the line and symbol samples in the
/// legend.
/// </summary>
private bool _isShowLegendSymbols;
#endregion
#region Defaults
/// <summary>
/// A simple struct that defines the
/// default property values for the <see cref="Legend"/> class.
/// </summary>
public struct Default
{
// Default Legend properties
/// <summary>
/// The default pen width for the <see cref="Legend"/> border border.
/// (<see cref="ZedGraph.LineBase.Width"/> property). Units are in pixels.
/// </summary>
public static float BorderWidth = 1;
/// <summary>
/// The default color for the <see cref="Legend"/> border border.
/// (<see cref="ZedGraph.LineBase.Color"/> property).
/// </summary>
public static Color BorderColor = Color.Black;
/// <summary>
/// The default color for the <see cref="Legend"/> background.
/// (<see cref="ZedGraph.Fill.Color"/> property). Use of this
/// color depends on the status of the <see cref="ZedGraph.Fill.Type"/>
/// property.
/// </summary>
public static Color FillColor = Color.White;
/// <summary>
/// The default custom brush for filling in this <see cref="Legend"/>.
/// </summary>
public static Brush FillBrush = null;
/// <summary>
/// The default fill mode for the <see cref="Legend"/> background.
/// </summary>
public static FillType FillType = FillType.Brush;
/// <summary>
/// The default location for the <see cref="Legend"/> on the graph
/// (<see cref="Legend.Location"/> property). This property is
/// defined as a <see cref="LegendPos"/> enumeration.
/// </summary>
public static LegendPos Position = LegendPos.Top;
/// <summary>
/// The default border mode for the <see cref="Legend"/>.
/// (<see cref="ZedGraph.LineBase.IsVisible"/> property). true
/// to draw a border around the <see cref="Legend.Rect"/>,
/// false otherwise.
/// </summary>
public static bool IsBorderVisible = true;
/// <summary>
/// The default display mode for the <see cref="Legend"/>.
/// (<see cref="Legend.IsVisible"/> property). true
/// to show the legend,
/// false to hide it.
/// </summary>
public static bool IsVisible = true;
/// <summary>
/// The default fill mode for the <see cref="Legend"/> background
/// (<see cref="ZedGraph.Fill.Type"/> property).
/// true to fill-in the background with color,
/// false to leave the background transparent.
/// </summary>
public static bool IsFilled = true;
/// <summary>
/// The default horizontal stacking mode for the <see cref="Legend"/>
/// (<see cref="Legend.IsHStack"/> property).
/// true to allow horizontal legend item stacking, false to allow
/// only vertical legend orientation.
/// </summary>
public static bool IsHStack = true;
/// <summary>
/// The default font family for the <see cref="Legend"/> entries
/// (<see cref="ZedGraph.FontSpec.Family"/> property).
/// </summary>
public static string FontFamily = "Segoe UI";
/// <summary>
/// The default font size for the <see cref="Legend"/> entries
/// (<see cref="ZedGraph.FontSpec.Size"/> property). Units are
/// in points (1/72 inch).
/// </summary>
public static float FontSize = 12;
/// <summary>
/// The default font color for the <see cref="Legend"/> entries
/// (<see cref="ZedGraph.FontSpec.FontColor"/> property).
/// </summary>
public static Color FontColor = Color.Black;
/// <summary>
/// The default font bold mode for the <see cref="Legend"/> entries
/// (<see cref="ZedGraph.FontSpec.IsBold"/> property). true
/// for a bold typeface, false otherwise.
/// </summary>
public static bool FontBold = false;
/// <summary>
/// The default font italic mode for the <see cref="Legend"/> entries
/// (<see cref="ZedGraph.FontSpec.IsItalic"/> property). true
/// for an italic typeface, false otherwise.
/// </summary>
public static bool FontItalic = false;
/// <summary>
/// The default font underline mode for the <see cref="Legend"/> entries
/// (<see cref="ZedGraph.FontSpec.IsUnderline"/> property). true
/// for an underlined typeface, false otherwise.
/// </summary>
public static bool FontUnderline = false;
/// <summary>
/// The default color for filling in the scale text background
/// (see <see cref="ZedGraph.Fill.Color"/> property).
/// </summary>
public static Color FontFillColor = Color.White;
/// <summary>
/// The default custom brush for filling in the scale text background
/// (see <see cref="ZedGraph.Fill.Brush"/> property).
/// </summary>
public static Brush FontFillBrush = null;
/// <summary>
/// The default fill mode for filling in the scale text background
/// (see <see cref="ZedGraph.Fill.Type"/> property).
/// </summary>
public static FillType FontFillType = FillType.None;
/// <summary>
/// The default gap size between the legend and the <see cref="Chart.Rect" />.
/// This is the default value of <see cref="Legend.Gap" />.
/// </summary>
public static float Gap = 0.5f;
/// <summary>
/// Default value for the <see cref="Legend.IsReverse" /> property.
/// </summary>
public static bool IsReverse = false;
/// <summary>
/// Default value for the <see cref="Legend.IsShowLegendSymbols" /> property.
/// </summary>
public static bool IsShowLegendSymbols = true;
}
#endregion
#region Properties
/// <summary>
/// Get the bounding rectangle for the <see cref="Legend"/> in screen coordinates
/// </summary>
/// <value>A screen rectangle in pixel units</value>
public RectangleF Rect
{
get { return _rect; }
}
/// <summary>
/// Access to the <see cref="ZedGraph.FontSpec"/> class used to render
/// the <see cref="Legend"/> entries
/// </summary>
/// <value>A reference to a <see cref="Legend"/> object</value>
/// <seealso cref="Default.FontColor"/>
/// <seealso cref="Default.FontBold"/>
/// <seealso cref="Default.FontItalic"/>
/// <seealso cref="Default.FontUnderline"/>
/// <seealso cref="Default.FontFamily"/>
/// <seealso cref="Default.FontSize"/>
public FontSpec FontSpec
{
get { return _fontSpec; }
set
{
if ( value == null )
throw new ArgumentNullException( "Uninitialized FontSpec in Legend" );
_fontSpec = value;
}
}
/// <summary>
/// Gets or sets a property that shows or hides the <see cref="Legend"/> entirely
/// </summary>
/// <value> true to show the <see cref="Legend"/>, false to hide it </value>
/// <seealso cref="Default.IsVisible"/>
public bool IsVisible
{
get { return _isVisible; }
set { _isVisible = value; }
}
/// <summary>
/// The <see cref="Border"/> class used to draw the border border around this <see cref="Legend"/>.
/// </summary>
public Border Border
{
get { return _border; }
set { _border = value; }
}
/// <summary>
/// Gets or sets the <see cref="ZedGraph.Fill"/> data for this
/// <see cref="Legend"/> background.
/// </summary>
public Fill Fill
{
get { return _fill; }
set { _fill = value; }
}
/// <summary>
/// Sets or gets a property that allows the <see cref="Legend"/> items to
/// stack horizontally in addition to the vertical stacking
/// </summary>
/// <value>true to allow horizontal stacking, false otherwise
/// </value>
/// <seealso cref="Default.IsHStack"/>
public bool IsHStack
{
get { return _isHStack; }
set { _isHStack = value; }
}
/// <summary>
/// Sets or gets the location of the <see cref="Legend"/> on the
/// <see cref="GraphPane"/> using the <see cref="LegendPos"/> enum type
/// </summary>
/// <seealso cref="Default.Position"/>
public LegendPos Position
{
get { return _position; }
set { _position = value; }
}
/// <summary>
/// Gets or sets the <see cref="Location"/> data for the <see cref="Legend"/>.
/// This property is only applicable if <see cref="Position"/> is set
/// to <see cref="LegendPos.Float"/>.
/// </summary>
public Location Location
{
get { return _location; }
set { _location = value; }
}
/// <summary>
/// Gets or sets the gap size between the legend and the <see cref="Chart.Rect" />.
/// </summary>
/// <remarks>
/// This is expressed as a fraction of the largest scaled character height for any
/// of the fonts used in the legend. Each <see cref="CurveItem" /> in the legend can
/// optionally have its own <see cref="FontSpec" /> specification.
/// </remarks>
public float Gap
{
get { return _gap; }
set { _gap = value; }
}
/// <summary>
/// Gets or sets a value that determines if the legend entries are displayed in normal order
/// (matching the order in the <see cref="CurveList" />, or in reverse order.
/// </summary>
public bool IsReverse
{
get { return _isReverse; }
set { _isReverse = value; }
}
/// <summary>
/// Gets or sets a value that determines whether the line and symbol keys will be displayed
/// in the legend.
/// </summary>
/// <remarks>
/// Note: If this value is set to false (so that only the curve label text is displayed
/// with no legend keys), then the color of the font for the legend entry of each curve
/// will automatically be set to match the <see cref="CurveItem.Color"/> setting for that curve.
/// You can override this behavior by specifying a specific font to be used for each
/// individual curve with the <see cref="ZedGraph.Label.FontSpec">CurveItem.Label.FontSpec</see>
/// property.
/// </remarks>
public bool IsShowLegendSymbols
{
get { return _isShowLegendSymbols; }
set { _isShowLegendSymbols = value; }
}
#endregion
#region Constructors
/// <summary>
/// Default constructor that sets all <see cref="Legend"/> properties to default
/// values as defined in the <see cref="Default"/> class.
/// </summary>
public Legend()
{
_position = Default.Position;
_isHStack = Default.IsHStack;
_isVisible = Default.IsVisible;
this.Location = new Location( 0, 0, CoordType.PaneFraction );
_fontSpec = new FontSpec( Default.FontFamily, Default.FontSize,
Default.FontColor, Default.FontBold,
Default.FontItalic, Default.FontUnderline,
Default.FontFillColor, Default.FontFillBrush,
Default.FontFillType );
_fontSpec.Border.IsVisible = false;
_border = new Border( Default.IsBorderVisible, Default.BorderColor, Default.BorderWidth );
_fill = new Fill( Default.FillColor, Default.FillBrush, Default.FillType );
_gap = Default.Gap;
_isReverse = Default.IsReverse;
_isShowLegendSymbols = Default.IsShowLegendSymbols;
}
/// <summary>
/// The Copy Constructor
/// </summary>
/// <param name="rhs">The XAxis object from which to copy</param>
public Legend( Legend rhs )
{
_rect = rhs.Rect;
_position = rhs.Position;
_isHStack = rhs.IsHStack;
_isVisible = rhs.IsVisible;
_location = rhs.Location;
_border = rhs.Border.Clone();
_fill = rhs.Fill.Clone();
_fontSpec = rhs.FontSpec.Clone();
_gap = rhs._gap;
_isReverse = rhs._isReverse;
_isShowLegendSymbols = rhs._isShowLegendSymbols;
}
/// <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 Legend Clone()
{
return new Legend( this );
}
#endregion
#region Serialization
/// <summary>
/// Current schema value that defines the version of the serialized file
/// </summary>
public const int schema = 12;
/// <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 Legend( 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" );
_position = (LegendPos)info.GetValue( "position", typeof( LegendPos ) );
_isHStack = info.GetBoolean( "isHStack" );
_isVisible = info.GetBoolean( "isVisible" );
_fill = (Fill)info.GetValue( "fill", typeof( Fill ) );
_border = (Border)info.GetValue( "border", typeof( Border ) );
_fontSpec = (FontSpec)info.GetValue( "fontSpec", typeof( FontSpec ) );
_location = (Location)info.GetValue( "location", typeof( Location ) );
_gap = info.GetSingle( "gap" );
if ( schema >= 11 )
_isReverse = info.GetBoolean( "isReverse" );
if ( schema >= 12 )
_isShowLegendSymbols = info.GetBoolean( "isShowLegendSymbols" );
}
/// <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( "position", _position );
info.AddValue( "isHStack", _isHStack );
info.AddValue( "isVisible", _isVisible );
info.AddValue( "fill", _fill );
info.AddValue( "border", _border );
info.AddValue( "fontSpec", _fontSpec );
info.AddValue( "location", _location );
info.AddValue( "gap", _gap );
info.AddValue( "isReverse", _isReverse );
info.AddValue( "isShowLegendSymbols", _isShowLegendSymbols );
}
#endregion
#region Rendering Methods
/// <summary>
/// Render the <see cref="Legend"/> to the specified <see cref="Graphics"/> device.
/// </summary>
/// <remarks>
/// This method is normally only called by the Draw method
/// of the parent <see cref="GraphPane"/> object.
/// </remarks>
/// <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="PaneBase"/> object that is the parent or
/// owner of this object.
/// </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 Draw( Graphics g, PaneBase pane, float scaleFactor )
{
// if the legend is not visible, do nothing
if ( !_isVisible )
return;
// Fill the background with the specified color if required
_fill.Draw( g, _rect );
PaneList paneList = GetPaneList( pane );
float halfGap = _tmpSize / 2.0F;
// Check for bad data values
if ( _hStack <= 0 )
_hStack = 1;
if ( _legendItemWidth <= 0 )
_legendItemWidth = 100;
if ( _legendItemHeight <= 0 )
_legendItemHeight = _tmpSize;
//float gap = pane.ScaledGap( scaleFactor );
int iEntry = 0;
float x, y;
// Get a brush for the legend label text
using ( SolidBrush brushB = new SolidBrush( Color.Black ) )
{
foreach ( GraphPane tmpPane in paneList )
{
// Loop for each curve in the CurveList collection
//foreach ( CurveItem curve in tmpPane.CurveList )
int count = tmpPane.CurveList.Count;
for ( int i = 0; i < count; i++ )
{
CurveItem curve = tmpPane.CurveList[_isReverse ? count - i - 1 : i];
if ( curve._label._text != "" && curve._label._isVisible )
{
// Calculate the x,y (TopLeft) location of the current
// curve legend label
// assuming:
// charHeight/2 for the left margin, plus legendWidth for each
// horizontal column
// legendHeight is the line spacing, with no extra margin above
x = _rect.Left + halfGap / 2.0F +
( iEntry % _hStack ) * _legendItemWidth;
y = _rect.Top + (int)( iEntry / _hStack ) * _legendItemHeight;
// Draw the legend label for the current curve
FontSpec tmpFont = ( curve._label._fontSpec != null ) ?
curve._label._fontSpec : this.FontSpec;
// This is required because, for long labels, the centering can affect the
// position in GDI+.
tmpFont.StringAlignment = StringAlignment.Near;
if ( _isShowLegendSymbols )
{
tmpFont.Draw( g, pane, curve._label._text,
x + 2.5F * _tmpSize, y + _legendItemHeight / 2.0F,
AlignH.Left, AlignV.Center, scaleFactor );
RectangleF rect = new RectangleF( x, y + _legendItemHeight / 4.0F,
2 * _tmpSize, _legendItemHeight / 2.0F );
curve.DrawLegendKey( g, tmpPane, rect, scaleFactor );
}
else
{
if ( curve._label._fontSpec == null )
tmpFont.FontColor = curve.Color;
tmpFont.Draw(g, pane, curve._label._text,
x + 0.0F * _tmpSize, y + _legendItemHeight / 2.0F,
AlignH.Left, AlignV.Center, scaleFactor);
}
// maintain a curve count for positioning
iEntry++;
}
}
if ( pane is MasterPane && ( (MasterPane)pane ).IsUniformLegendEntries )
break;
}
// Draw a border around the legend if required
if ( iEntry > 0 )
this.Border.Draw( g, pane, scaleFactor, _rect );
}
}
private float GetMaxHeight( PaneList paneList, Graphics g, float scaleFactor )
{
// Set up some scaled dimensions for calculating sizes and locations
float defaultCharHeight = this.FontSpec.GetHeight( scaleFactor );
float maxCharHeight = defaultCharHeight;
// Find the largest charHeight, just in case the curves have individual fonts defined
foreach ( GraphPane tmpPane in paneList )
{
foreach ( CurveItem curve in tmpPane.CurveList )
{
if ( curve._label._text != string.Empty && curve._label._isVisible )
{
float tmpHeight = defaultCharHeight;
if ( curve._label._fontSpec != null )
tmpHeight = curve._label._fontSpec.GetHeight( scaleFactor );
// Account for multiline legend entries
tmpHeight *= curve._label._text.Split( '\n' ).Length;
if ( tmpHeight > maxCharHeight )
maxCharHeight = tmpHeight;
}
}
}
return maxCharHeight;
}
/// <summary>
/// Determine if a mouse point is within the legend, and if so, which legend
/// entry (<see cref="CurveItem"/>) is nearest.
/// </summary>
/// <param name="mousePt">The screen point, in pixel coordinates.</param>
/// <param name="pane">
/// A reference to the <see cref="PaneBase"/> object that is the parent or
/// owner of this object.
/// </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>
/// <param name="index">The index number of the <see cref="CurveItem"/> legend
/// entry that is under the mouse point. The <see cref="CurveItem"/> object is
/// accessible via <see cref="GraphPane.CurveList">CurveList[index]</see>.
/// </param>
/// <returns>true if the mouse point is within the <see cref="Legend"/> bounding
/// box, false otherwise.</returns>
/// <seealso cref="GraphPane.FindNearestObject"/>
public bool FindPoint( PointF mousePt, PaneBase pane, float scaleFactor, out int index )
{
index = -1;
if ( _rect.Contains( mousePt ) )
{
int j = (int)( ( mousePt.Y - _rect.Top ) / _legendItemHeight );
int i = (int)( ( mousePt.X - _rect.Left - _tmpSize / 2.0f ) / _legendItemWidth );
if ( i < 0 )
i = 0;
if ( i >= _hStack )
i = _hStack - 1;
int pos = i + j * _hStack;
index = 0;
PaneList paneList = GetPaneList( pane );
foreach ( GraphPane tmpPane in paneList )
{
foreach ( CurveItem curve in tmpPane.CurveList )
{
if ( curve._label._isVisible && curve._label._text != string.Empty )
{
if ( pos == 0 )
return true;
pos--;
}
index++;
}
}
return true;
}
else
return false;
}
private PaneList GetPaneList( PaneBase pane )
{
// For a single GraphPane, create a PaneList to contain it
// Otherwise, just use the paneList from the MasterPane
PaneList paneList;
if ( pane is GraphPane )
{
paneList = new PaneList();
paneList.Add( (GraphPane)pane );
}
else
paneList = ( (MasterPane)pane ).PaneList;
return paneList;
}
/// <summary>
/// Calculate the <see cref="Legend"/> rectangle (<see cref="Rect"/>),
/// taking into account the number of required legend
/// entries, and the legend drawing preferences.
/// </summary>
/// <remarks>Adjust the size of the
/// <see cref="Chart.Rect"/> for the parent <see cref="GraphPane"/> to accomodate the
/// space required by the legend.
/// </remarks>
/// <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="PaneBase"/> object that is the parent or
/// owner of this object.
/// </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>
/// <param name="tChartRect">
/// The rectangle that contains the area bounded by the axes, in pixel units.
/// <seealso cref="Chart.Rect" />
/// </param>
public void CalcRect( Graphics g, PaneBase pane, float scaleFactor,
ref RectangleF tChartRect )
{
// Start with an empty rectangle
_rect = Rectangle.Empty;
_hStack = 1;
_legendItemWidth = 1;
_legendItemHeight = 0;
RectangleF clientRect = pane.CalcClientRect( g, scaleFactor );
// If the legend is invisible, don't do anything
if ( !_isVisible )
return;
int nCurve = 0;
PaneList paneList = GetPaneList( pane );
_tmpSize = GetMaxHeight( paneList, g, scaleFactor );
float halfGap = _tmpSize / 2.0F,
maxWidth = 0,
tmpWidth,
gapPix = _gap * _tmpSize;
foreach ( GraphPane tmpPane in paneList )
{
// Loop through each curve in the curve list
// Find the maximum width of the legend labels
//foreach ( CurveItem curve in tmpPane.CurveList )
//foreach ( CurveItem curve in GetIterator( tmpPane.CurveList, _isReverse ) )
int count = tmpPane.CurveList.Count;
for ( int i = 0; i < count; i++ )
{
CurveItem curve = tmpPane.CurveList[_isReverse ? count - i - 1 : i];
if ( curve._label._text != string.Empty && curve._label._isVisible )
{
// Calculate the width of the label save the max width
FontSpec tmpFont = ( curve._label._fontSpec != null ) ?
curve._label._fontSpec : this.FontSpec;
tmpWidth = tmpFont.GetWidth( g, curve._label._text, scaleFactor );
if ( tmpWidth > maxWidth )
maxWidth = tmpWidth;
// Save the maximum symbol height for line-type curves
if ( curve is LineItem && ( (LineItem)curve ).Symbol.Size > _legendItemHeight )
_legendItemHeight = ( (LineItem)curve ).Symbol.Size;
nCurve++;
}
}
if ( pane is MasterPane && ( (MasterPane)pane ).IsUniformLegendEntries )
break;
}
float widthAvail;
// Is this legend horizontally stacked?
if ( _isHStack )
{
// Determine the available space for horizontal stacking
switch ( _position )
{
// Never stack if the legend is to the right or left
case LegendPos.Right:
case LegendPos.Left:
widthAvail = 0;
break;
// for the top & bottom, the axis border width is available
case LegendPos.Top:
case LegendPos.TopCenter:
case LegendPos.Bottom:
case LegendPos.BottomCenter:
widthAvail = tChartRect.Width;
break;
// for the top & bottom flush left, the panerect less margins is available
case LegendPos.TopFlushLeft:
case LegendPos.BottomFlushLeft:
widthAvail = clientRect.Width;
break;
// for inside the axis area or Float, use 1/2 of the axis border width
case LegendPos.InsideTopRight:
case LegendPos.InsideTopLeft:
case LegendPos.InsideBotRight:
case LegendPos.InsideBotLeft:
case LegendPos.Float:
widthAvail = tChartRect.Width / 2;
break;
// shouldn't ever happen
default:
widthAvail = 0;
break;
}
// width of one legend entry
if ( _isShowLegendSymbols )
_legendItemWidth = 3.0f * _tmpSize + maxWidth;
else
_legendItemWidth = 0.5f * _tmpSize + maxWidth;
// Calculate the number of columns in the legend
// Normally, the legend is:
// available width / ( max width of any entry + space for line&symbol )
if ( maxWidth > 0 )
_hStack = (int)( ( widthAvail - halfGap ) / _legendItemWidth );
// You can never have more columns than legend entries
if ( _hStack > nCurve )
_hStack = nCurve;
// a saftey check
if ( _hStack == 0 )
_hStack = 1;
}
else
{
if ( _isShowLegendSymbols )
_legendItemWidth = 3.0F * _tmpSize + maxWidth;
else
_legendItemWidth = 0.5F * _tmpSize + maxWidth;
}
// legend is:
// item: space line space text space
// width: wid 4*wid wid maxWid wid
// The symbol is centered on the line
//
// legend begins 3 * wid to the right of the plot rect
//
// The height of the legend is the actual height of the lines of text
// (nCurve * hite) plus wid on top and wid on the bottom
// total legend width
float totLegWidth = _hStack * _legendItemWidth;
// The total legend height
_legendItemHeight = _legendItemHeight * (float)scaleFactor + halfGap;
if ( _tmpSize > _legendItemHeight )
_legendItemHeight = _tmpSize;
float totLegHeight = (float)Math.Ceiling( (double)nCurve / (double)_hStack )
* _legendItemHeight;
RectangleF newRect = new RectangleF();
// Now calculate the legend rect based on the above determined parameters
// Also, adjust the ChartRect to reflect the space for the legend
if ( nCurve > 0 )
{
newRect = new RectangleF( 0, 0, totLegWidth, totLegHeight );
// The switch statement assigns the left and top edges, and adjusts the ChartRect
// as required. The right and bottom edges are calculated at the bottom of the switch.
switch ( _position )
{
case LegendPos.Right:
newRect.X = clientRect.Right - totLegWidth;
newRect.Y = tChartRect.Top;
tChartRect.Width -= totLegWidth + gapPix;
break;
case LegendPos.Top:
newRect.X = tChartRect.Left;
newRect.Y = clientRect.Top;
tChartRect.Y += totLegHeight + gapPix;
tChartRect.Height -= totLegHeight + gapPix;
break;
case LegendPos.TopFlushLeft:
newRect.X = clientRect.Left;
newRect.Y = clientRect.Top;
tChartRect.Y += totLegHeight + gapPix * 1.5f;
tChartRect.Height -= totLegHeight + gapPix * 1.5f;
break;
case LegendPos.TopCenter:
newRect.X = tChartRect.Left + ( tChartRect.Width - totLegWidth ) / 2;
newRect.Y = tChartRect.Top;
tChartRect.Y += totLegHeight + gapPix;
tChartRect.Height -= totLegHeight + gapPix;
break;
case LegendPos.Bottom:
newRect.X = tChartRect.Left;
newRect.Y = clientRect.Bottom - totLegHeight;
tChartRect.Height -= totLegHeight + gapPix;
break;
case LegendPos.BottomFlushLeft:
newRect.X = clientRect.Left;
newRect.Y = clientRect.Bottom - totLegHeight;
tChartRect.Height -= totLegHeight + gapPix;
break;
case LegendPos.BottomCenter:
newRect.X = tChartRect.Left + ( tChartRect.Width - totLegWidth ) / 2;
newRect.Y = clientRect.Bottom - totLegHeight;
tChartRect.Height -= totLegHeight + gapPix;
break;
case LegendPos.Left:
newRect.X = clientRect.Left;
newRect.Y = tChartRect.Top;
tChartRect.X += totLegWidth + halfGap;
tChartRect.Width -= totLegWidth + gapPix;
break;
case LegendPos.InsideTopRight:
newRect.X = tChartRect.Right - totLegWidth;
newRect.Y = tChartRect.Top;
break;
case LegendPos.InsideTopLeft:
newRect.X = tChartRect.Left;
newRect.Y = tChartRect.Top;
break;
case LegendPos.InsideBotRight:
newRect.X = tChartRect.Right - totLegWidth;
newRect.Y = tChartRect.Bottom - totLegHeight;
break;
case LegendPos.InsideBotLeft:
newRect.X = tChartRect.Left;
newRect.Y = tChartRect.Bottom - totLegHeight;
break;
case LegendPos.Float:
newRect.Location = this.Location.TransformTopLeft( pane, totLegWidth, totLegHeight );
break;
}
}
_rect = newRect;
}
// /// <summary>
// /// Private method to the render region that gives the iterator depending on the attribute
// /// </summary>
// /// <param name="c"></param>
// /// <param name="forward"></param>
// /// <returns></returns>
// private IEnumerable<CurveItem> GetIterator(CurveList c, bool forward)
// {
// return forward ? c.Forward : c.Backward;
// }
#endregion
}
}
|