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)
5 // Copyright (c) 2012 Oystein Bjorke
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:
15 // The above copyright notice and this permission notice shall be included
16 // in all copies or substantial portions of the Software.
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.
27 // Represents a series for clustered or stacked bar charts.
29 // --------------------------------------------------------------------------------------------------------------------
30 namespace OxyPlot.Series
37 /// Represents a series for clustered or stacked bar charts.
39 public class BarSeries : BarSeriesBase<BarItem>
42 /// Initializes a new instance of the <see cref="BarSeries"/> class.
50 /// Gets or sets the width (height) of the bars.
53 /// The width of the bars.
55 public double BarWidth { get; set; }
58 /// Gets or sets the width of the columns/bars (as a fraction of the available space).
61 /// The fractional width.
64 /// The width of the bars.
67 /// The available space will be determined by the GapWidth of the CategoryAxis used by this series.
69 internal override double GetBarWidth()
75 /// Gets the actual width/height of the items of this series.
78 /// The width or height.
81 /// The actual width is also influenced by the GapWidth of the CategoryAxis used by this series.
83 protected override double GetActualBarWidth()
85 var categoryAxis = this.GetCategoryAxis();
86 return this.BarWidth / (1 + categoryAxis.GapWidth) / categoryAxis.MaxWidth;
90 /// Gets the category axis.
93 /// The category axis.
95 protected override CategoryAxis GetCategoryAxis()
97 if (!(this.YAxis is CategoryAxis))
100 "A BarSeries requires a CategoryAxis on the y-axis. Use a ColumnSeries if you want vertical bars.");
103 return this.YAxis as CategoryAxis;
107 /// Gets the rectangle for the specified values.
109 /// <param name="baseValue">
110 /// The base value of the bar
112 /// <param name="topValue">
113 /// The top value of the bar
115 /// <param name="beginValue">
116 /// The begin value of the bar
118 /// <param name="endValue">
119 /// The end value of the bar
124 protected override OxyRect GetRectangle(double baseValue, double topValue, double beginValue, double endValue)
126 return OxyRect.Create(this.Transform(baseValue, beginValue), this.Transform(topValue, endValue));
130 /// Gets the value axis.
135 protected override Axis GetValueAxis()
143 /// <param name="rc">
144 /// The render context.
146 /// <param name="clippingRect">
147 /// The clipping rect.
149 /// <param name="rect">
152 /// <param name="value">
158 protected override void RenderLabel(IRenderContext rc, OxyRect clippingRect, OxyRect rect, double value, int i)
160 var s = StringHelper.Format(
161 this.ActualCulture, this.LabelFormatString, this.GetItem(this.ValidItemsIndexInversion[i]), value);
162 HorizontalAlignment ha;
164 switch (this.LabelPlacement)
166 case LabelPlacement.Inside:
167 pt = new ScreenPoint(rect.Right - this.LabelMargin, (rect.Top + rect.Bottom) / 2);
168 ha = HorizontalAlignment.Right;
170 case LabelPlacement.Middle:
171 pt = new ScreenPoint((rect.Left + rect.Right) / 2, (rect.Top + rect.Bottom) / 2);
172 ha = HorizontalAlignment.Center;
174 case LabelPlacement.Base:
175 pt = new ScreenPoint(rect.Left + this.LabelMargin, (rect.Top + rect.Bottom) / 2);
176 ha = HorizontalAlignment.Left;
179 pt = new ScreenPoint(rect.Right + this.LabelMargin, (rect.Top + rect.Bottom) / 2);
180 ha = HorizontalAlignment.Left;
188 this.ActualTextColor,
191 this.ActualFontWeight,
194 VerticalAlignment.Middle);