//The MIT License(MIT) //Copyright(c) 2016 Alberto Rodriguez & LiveCharts Contributors //Permission is hereby granted, free of charge, to any person obtaining a copy //of this software and associated documentation files (the "Software"), to deal //in the Software without restriction, including without limitation the rights //to use, copy, modify, merge, publish, distribute, sublicense, and/or sell //copies of the Software, and to permit persons to whom the Software is //furnished to do so, subject to the following conditions: //The above copyright notice and this permission notice shall be included in all //copies or substantial portions of the Software. //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, //FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE //AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE //SOFTWARE. using System; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Shapes; using LiveCharts.Definitions.Points; using LiveCharts.Definitions.Series; using LiveCharts.Dtos; using LiveCharts.SeriesAlgorithms; using LiveCharts.Uwp.Charts.Base; using LiveCharts.Uwp.Points; using LiveCharts.Uwp.Components; namespace LiveCharts.Uwp { /// /// The stacked column series compares the proportion of every series in a point /// public class StackedColumnSeries : Series, IStackedColumnSeriesView { #region Constructors /// /// Initializes a new instance of StackedColumnSeries class /// public StackedColumnSeries() { Model = new StackedColumnAlgorithm(this); InitializeDefuaults(); } /// /// Initializes a new instance of StackedColumnSeries class, with a given mapper /// public StackedColumnSeries(object configuration) { Model = new StackedColumnAlgorithm(this); Configuration = configuration; InitializeDefuaults(); } #endregion #region Private Properties #endregion #region Properties /// /// The maximum column width property /// public static readonly DependencyProperty MaxColumnWidthProperty = DependencyProperty.Register( "MaxColumnWidth", typeof (double), typeof (StackedColumnSeries), new PropertyMetadata(35d)); /// /// Gets or sets the maximum width of a column, any column will be capped at this value /// public double MaxColumnWidth { get { return (double) GetValue(MaxColumnWidthProperty); } set { SetValue(MaxColumnWidthProperty, value); } } /// /// The column padding property /// public static readonly DependencyProperty ColumnPaddingProperty = DependencyProperty.Register( "ColumnPadding", typeof (double), typeof (StackedColumnSeries), new PropertyMetadata(2d)); /// /// Gets or sets the padding between every column in this series /// public double ColumnPadding { get { return (double) GetValue(ColumnPaddingProperty); } set { SetValue(ColumnPaddingProperty, value); } } /// /// The stack mode property /// public static readonly DependencyProperty StackModeProperty = DependencyProperty.Register( "StackMode", typeof (StackMode), typeof (StackedColumnSeries), new PropertyMetadata(default(StackMode))); /// /// Gets or sets stacked mode, values or percentage /// public StackMode StackMode { get { return (StackMode) GetValue(StackModeProperty); } set { SetValue(StackModeProperty, value); } } /// /// The labels position property /// public static readonly DependencyProperty LabelsPositionProperty = DependencyProperty.Register( "LabelsPosition", typeof(BarLabelPosition), typeof(StackedColumnSeries), new PropertyMetadata(BarLabelPosition.Parallel, CallChartUpdater())); /// /// Gets or sets where the label is placed /// public BarLabelPosition LabelsPosition { get { return (BarLabelPosition)GetValue(LabelsPositionProperty); } set { SetValue(LabelsPositionProperty, value); } } #endregion #region Overridden Methods /// /// Gets the view of a given point /// /// /// /// public override IChartPointView GetPointView(ChartPoint point, string label) { var pbv = (ColumnPointView) point.View; if (pbv == null) { pbv = new ColumnPointView { IsNew = true, Rectangle = new Rectangle(), Data = new CoreRectangle() }; Model.Chart.View.AddToDrawMargin(pbv.Rectangle); } else { pbv.IsNew = false; point.SeriesView.Model.Chart.View .EnsureElementBelongsToCurrentDrawMargin(pbv.Rectangle); point.SeriesView.Model.Chart.View .EnsureElementBelongsToCurrentDrawMargin(pbv.HoverShape); } pbv.Rectangle.Fill = Fill; pbv.Rectangle.StrokeThickness = StrokeThickness; pbv.Rectangle.Stroke = Stroke; pbv.Rectangle.StrokeDashArray = StrokeDashArray; pbv.Rectangle.Visibility = Visibility; Canvas.SetZIndex(pbv.Rectangle, Canvas.GetZIndex(this)); if (Model.Chart.RequiresHoverShape && pbv.HoverShape == null) { pbv.HoverShape = new Rectangle { Fill = new SolidColorBrush(Windows.UI.Colors.Transparent), StrokeThickness = 0 }; Canvas.SetZIndex(pbv.HoverShape, short.MaxValue); var uwpfChart = (Chart)Model.Chart.View; uwpfChart.AttachHoverableEventTo(pbv.HoverShape); Model.Chart.View.AddToDrawMargin(pbv.HoverShape); } if (pbv.HoverShape != null) pbv.HoverShape.Visibility = Visibility; if (DataLabels) { pbv.DataLabel = UpdateLabelContent(new DataLabelViewModel { FormattedText = label, Instance = point.Instance }, pbv.DataLabel); } pbv.LabelPosition = LabelsPosition; return pbv; } #endregion #region Private Methods private void InitializeDefuaults() { //this.SetIfNotSet(StrokeThicknessProperty, 0d); //this.SetIfNotSet(ForegroundProperty, new SolidColorBrush(Windows.UI.Colors.White)); Func defaultLabel = x => Model.CurrentYAxis.GetFormatter()(x.Y); this.SetIfNotSet(LabelPointProperty, defaultLabel); DefaultFillOpacity = 1; } #endregion } }