using C1.Xaml.Chart;
using System.Linq;
using Windows.UI.Xaml.Controls;
using C1.Chart;

namespace FloatingBarChart
{
    /// <summary>
    /// Interaction logic for FloatingBar.xaml
    /// </summary>
    public partial class FloatingBar : Page
    {
        Series seriesA, seriesB;
        float columnWidthPercentage = 0.6f;
        public FloatingBar()
        {
            InitializeComponent();
            seriesA = new Series();
            seriesA.Binding = "ClassAHigh";
            seriesA.SeriesName = "Class A";
            seriesA.SymbolRendering += SunflowerSeries_SymbolRendering;

            seriesB = new Series();
            seriesB.Binding = "ClassBHigh";
            seriesB.SeriesName = "Class B";
            seriesB.SymbolRendering += RoseSeries_SymbolRendering;

            flexChart.Series.Add(seriesA);
            flexChart.Series.Add(seriesB);
            flexChart.LabelRendering += FlexChart_LabelRendering;
            flexChart.AxisY.MajorUnit = 10;
            flexChart.ToolTip.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            flexChart.DataLabel.Position = LabelPosition.Bottom;
            flexChart.DataLabel.Content = "{seriesName}";
            flexChart.Options.ClusterSize = new ElementSize() { SizeType = ElementSizeType.Percentage, Value = columnWidthPercentage * 100 };
        }

        private void FlexChart_LabelRendering(object sender, RenderDataLabelEventArgs e)
        {
            SubjectScoreRange range = (SubjectScoreRange)e.Item;
            e.Text = e.Text.Equals("Class A") ? range.ClassALow + " - " + range.ClassAHigh : range.ClassBLow + " - " + range.ClassBHigh;
        }

        private void RoseSeries_SymbolRendering(object sender, RenderSymbolEventArgs e)
        {
            e.Cancel = true;
            SubjectScoreRange range = (SubjectScoreRange)e.Item;
            double width = flexChart.PlotRect.Width / seriesB.GetValues(0).Count() * columnWidthPercentage / flexChart.Series.Count;
            double bottom = flexChart.AxisY.Convert(range.ClassBLow);
            e.Engine.DrawRect(e.Point.X - width / 2, e.Point.Y, width, bottom - e.Point.Y);
        }

        private void SunflowerSeries_SymbolRendering(object sender, RenderSymbolEventArgs e)
        {
            e.Cancel = true;
            SubjectScoreRange range = (SubjectScoreRange)e.Item;
            double width = flexChart.PlotRect.Width / seriesA.GetValues(0).Count() * columnWidthPercentage / flexChart.Series.Count;
            double bottom = flexChart.AxisY.Convert(range.ClassALow);
            e.Engine.DrawRect(e.Point.X - width / 2, e.Point.Y, width, bottom - e.Point.Y);
        }
    }
}
