External/OxyPlot/OxyPlot/Series/BarSeries/BarSeries.cs
author moel.mich
Sat, 08 Jun 2013 16:53:22 +0000
changeset 391 5be8f2773237
permissions -rw-r--r--
Added the source code of OxyPlot as of commit d190d7748a73 (6.5.2013).
     1 // --------------------------------------------------------------------------------------------------------------------
     2 // <copyright file="BarSeries.cs" company="OxyPlot">
     3 //   The MIT License (MIT)
     4 //
     5 //   Copyright (c) 2012 Oystein Bjorke
     6 //
     7 //   Permission is hereby granted, free of charge, to any person obtaining a
     8 //   copy of this software and associated documentation files (the
     9 //   "Software"), to deal in the Software without restriction, including
    10 //   without limitation the rights to use, copy, modify, merge, publish,
    11 //   distribute, sublicense, and/or sell copies of the Software, and to
    12 //   permit persons to whom the Software is furnished to do so, subject to
    13 //   the following conditions:
    14 //
    15 //   The above copyright notice and this permission notice shall be included
    16 //   in all copies or substantial portions of the Software.
    17 //
    18 //   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    19 //   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    20 //   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    21 //   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    22 //   CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    23 //   TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    24 //   SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    25 // </copyright>
    26 // <summary>
    27 //   Represents a series for clustered or stacked bar charts.
    28 // </summary>
    29 // --------------------------------------------------------------------------------------------------------------------
    30 namespace OxyPlot.Series
    31 {
    32     using System;
    33 
    34     using OxyPlot.Axes;
    35 
    36     /// <summary>
    37     /// Represents a series for clustered or stacked bar charts.
    38     /// </summary>
    39     public class BarSeries : BarSeriesBase<BarItem>
    40     {
    41         /// <summary>
    42         /// Initializes a new instance of the <see cref="BarSeries"/> class.
    43         /// </summary>
    44         public BarSeries()
    45         {
    46             this.BarWidth = 1;
    47         }
    48 
    49         /// <summary>
    50         /// Gets or sets the width (height) of the bars.
    51         /// </summary>
    52         /// <value>
    53         /// The width of the bars.
    54         /// </value>
    55         public double BarWidth { get; set; }
    56 
    57         /// <summary>
    58         /// Gets or sets the width of the columns/bars (as a fraction of the available space).
    59         /// </summary>
    60         /// <returns>
    61         /// The fractional width.
    62         /// </returns>
    63         /// <value>
    64         /// The width of the bars.
    65         /// </value>
    66         /// <remarks>
    67         /// The available space will be determined by the GapWidth of the CategoryAxis used by this series.
    68         /// </remarks>
    69         internal override double GetBarWidth()
    70         {
    71             return this.BarWidth;
    72         }
    73 
    74         /// <summary>
    75         /// Gets the actual width/height of the items of this series.
    76         /// </summary>
    77         /// <returns>
    78         /// The width or height.
    79         /// </returns>
    80         /// <remarks>
    81         /// The actual width is also influenced by the GapWidth of the CategoryAxis used by this series.
    82         /// </remarks>
    83         protected override double GetActualBarWidth()
    84         {
    85             var categoryAxis = this.GetCategoryAxis();
    86             return this.BarWidth / (1 + categoryAxis.GapWidth) / categoryAxis.MaxWidth;
    87         }
    88 
    89         /// <summary>
    90         /// Gets the category axis.
    91         /// </summary>
    92         /// <returns>
    93         /// The category axis.
    94         /// </returns>
    95         protected override CategoryAxis GetCategoryAxis()
    96         {
    97             if (!(this.YAxis is CategoryAxis))
    98             {
    99                 throw new Exception(
   100                     "A BarSeries requires a CategoryAxis on the y-axis. Use a ColumnSeries if you want vertical bars.");
   101             }
   102 
   103             return this.YAxis as CategoryAxis;
   104         }
   105 
   106         /// <summary>
   107         /// Gets the rectangle for the specified values.
   108         /// </summary>
   109         /// <param name="baseValue">
   110         /// The base value of the bar
   111         /// </param>
   112         /// <param name="topValue">
   113         /// The top value of the bar
   114         /// </param>
   115         /// <param name="beginValue">
   116         /// The begin value of the bar
   117         /// </param>
   118         /// <param name="endValue">
   119         /// The end value of the bar
   120         /// </param>
   121         /// <returns>
   122         /// The rectangle.
   123         /// </returns>
   124         protected override OxyRect GetRectangle(double baseValue, double topValue, double beginValue, double endValue)
   125         {
   126             return OxyRect.Create(this.Transform(baseValue, beginValue), this.Transform(topValue, endValue));
   127         }
   128 
   129         /// <summary>
   130         /// Gets the value axis.
   131         /// </summary>
   132         /// <returns>
   133         /// The value axis.
   134         /// </returns>
   135         protected override Axis GetValueAxis()
   136         {
   137             return this.XAxis;
   138         }
   139 
   140         /// <summary>
   141         /// Draws the label.
   142         /// </summary>
   143         /// <param name="rc">
   144         /// The render context.
   145         /// </param>
   146         /// <param name="clippingRect">
   147         /// The clipping rect.
   148         /// </param>
   149         /// <param name="rect">
   150         /// The rect.
   151         /// </param>
   152         /// <param name="value">
   153         /// The value.
   154         /// </param>
   155         /// <param name="i">
   156         /// The i.
   157         /// </param>
   158         protected override void RenderLabel(IRenderContext rc, OxyRect clippingRect, OxyRect rect, double value, int i)
   159         {
   160             var s = StringHelper.Format(
   161                 this.ActualCulture, this.LabelFormatString, this.GetItem(this.ValidItemsIndexInversion[i]), value);
   162             HorizontalAlignment ha;
   163             ScreenPoint pt;
   164             switch (this.LabelPlacement)
   165             {
   166                 case LabelPlacement.Inside:
   167                     pt = new ScreenPoint(rect.Right - this.LabelMargin, (rect.Top + rect.Bottom) / 2);
   168                     ha = HorizontalAlignment.Right;
   169                     break;
   170                 case LabelPlacement.Middle:
   171                     pt = new ScreenPoint((rect.Left + rect.Right) / 2, (rect.Top + rect.Bottom) / 2);
   172                     ha = HorizontalAlignment.Center;
   173                     break;
   174                 case LabelPlacement.Base:
   175                     pt = new ScreenPoint(rect.Left + this.LabelMargin, (rect.Top + rect.Bottom) / 2);
   176                     ha = HorizontalAlignment.Left;
   177                     break;
   178                 default: // Outside
   179                     pt = new ScreenPoint(rect.Right + this.LabelMargin, (rect.Top + rect.Bottom) / 2);
   180                     ha = HorizontalAlignment.Left;
   181                     break;
   182             }
   183 
   184             rc.DrawClippedText(
   185                 clippingRect,
   186                 pt,
   187                 s,
   188                 this.ActualTextColor,
   189                 this.ActualFont,
   190                 this.ActualFontSize,
   191                 this.ActualFontWeight,
   192                 0,
   193                 ha,
   194                 VerticalAlignment.Middle);
   195         }
   196 
   197     }
   198 }