示例#1
0
        private static void AssertChartViewModel <T>(ChartType chartType, ChartAxisType xAxisType, ChartAxisType yAxisType, List <ChartPoint> expectedChartPoints, ChartPointsAssertionDelegate pointsAssertionMethod)
            where T : Series
        {
            // Init
            var chartAlertProperty = new ChartAlertProperty(
                "propertyName",
                "displayName",
                1,
                chartType,
                xAxisType,
                yAxisType,
                expectedChartPoints);

            // Act
            var chartPropertyControlViewModel = new ChartPropertyControlViewModel(chartAlertProperty);

            // Assert
            Assert.AreEqual("displayName", chartPropertyControlViewModel.Title, "Unexpected chart title");

            Assert.IsNotNull(chartPropertyControlViewModel, "View model is expected to be defined");
            Assert.IsNotNull(chartPropertyControlViewModel.SeriesCollection[0], "Series is expected to be defined");

            T series = chartPropertyControlViewModel.SeriesCollection[0] as T;

            Assert.IsNotNull(series, $"Series is from type {chartPropertyControlViewModel.SeriesCollection[0].GetType()}, but expected to be from type {typeof(T)}");

            List <LiveCharts.ChartPoint> actualDataPoints = series.ChartPoints.ToList();

            pointsAssertionMethod(expectedChartPoints, actualDataPoints);

            FormatterAssertMethod(chartPropertyControlViewModel, xAxisType, yAxisType, expectedChartPoints);
        }
示例#2
0
        public void WhenCreatingContractsAlertThenChartPropertiesAreConvertedCorrectly()
        {
            ContractsAlert contractsAlert = CreateContractsAlert <TestAlert>();

            Assert.AreEqual(2, contractsAlert.AlertProperties.Count);

            int propertyIndex = 0;
            ChartReferenceAlertProperty chartReferenceAlertProperty = (ChartReferenceAlertProperty)contractsAlert.AlertProperties[propertyIndex];

            Assert.AreEqual("ChartReference", chartReferenceAlertProperty.PropertyName);
            Assert.AreEqual(AlertPropertyType.Chart, chartReferenceAlertProperty.Type);
            Assert.AreEqual("ChartReferenceDisplayName", chartReferenceAlertProperty.DisplayName);
            Assert.AreEqual(0, chartReferenceAlertProperty.Order);
            Assert.AreEqual(ContractsChartType.BarChart, chartReferenceAlertProperty.ChartType);
            Assert.AreEqual(ContractsChartAxisType.String, chartReferenceAlertProperty.XAxisType);
            Assert.AreEqual(ContractsChartAxisType.Percentage, chartReferenceAlertProperty.YAxisType);
            Assert.AreEqual("chartReferencePath", chartReferenceAlertProperty.ReferencePath);

            propertyIndex++;
            ChartAlertProperty chartAlertProperty = (ChartAlertProperty)contractsAlert.AlertProperties[propertyIndex];

            Assert.AreEqual("DataPoints", chartAlertProperty.PropertyName);
            Assert.AreEqual(AlertPropertyType.Chart, chartAlertProperty.Type);
            Assert.AreEqual("ChartDisplayName", chartAlertProperty.DisplayName);
            Assert.AreEqual(1, chartAlertProperty.Order);
            Assert.AreEqual(ContractsChartType.LineChart, chartAlertProperty.ChartType);
            Assert.AreEqual(ContractsChartAxisType.Date, chartAlertProperty.XAxisType);
            Assert.AreEqual(ContractsChartAxisType.Number, chartAlertProperty.YAxisType);
            Assert.AreEqual(1, chartAlertProperty.DataPoints.Count);
            Assert.AreEqual(new DateTime(2018, 7, 9, 14, 31, 0, DateTimeKind.Utc), chartAlertProperty.DataPoints[0].X);
            Assert.AreEqual(5, chartAlertProperty.DataPoints[0].Y);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ChartPropertyControlViewModel"/> class.
        /// </summary>
        /// <param name="chartAlertProperty">The chart alert property that should be displayed.</param>
        public ChartPropertyControlViewModel(ChartAlertProperty chartAlertProperty)
        {
            this.Title = chartAlertProperty.DisplayName;

            if (chartAlertProperty.YAxisType != ChartAxisType.Number && chartAlertProperty.YAxisType != ChartAxisType.Percentage)
            {
                throw new InvalidOperationException($"Charts with Y axis type other than {ChartAxisType.Number} and {ChartAxisType.Percentage} are not supported.");
            }

            switch (chartAlertProperty.XAxisType)
            {
            case ChartAxisType.Date:
                this.InitializeDateTimeXAxisChart(chartAlertProperty);
                break;

            case ChartAxisType.Number:
                this.InitializeNumberXAxisChart(chartAlertProperty);
                break;

            case ChartAxisType.String:
                throw new NotImplementedException($"Charts with {ChartAxisType.String} X axis type will be supported soon.");

            default:
                throw new InvalidOperationException($"Charts with X axis type {chartAlertProperty.XAxisType} are not supported.");
            }
        }
        /// <summary>
        /// Initializes a chart with X axis from type <see cref="AlertPresentation.ChartAxisType.NumberAxis"/>.
        /// </summary>
        /// <param name="chartAlertProperty">The chart alert property that should be displayed.</param>
        private void InitializeNumberXAxisChart(ChartAlertProperty chartAlertProperty)
        {
            var sortedNumericPoints = chartAlertProperty.DataPoints.OrderBy(point => point.X).ToList();

            var chartPoints = sortedNumericPoints.Select(dataPoint =>
            {
                double x = ConvertCoordinateValueToDouble(dataPoint.X, "X");
                double y = ConvertCoordinateValueToDouble(dataPoint.Y, "Y");

                return(new ChartDataPoint <double>(x, y));
            });

            var chartValues = new ChartValues <ChartDataPoint <double> >(chartPoints);

            CartesianMapper <ChartDataPoint <double> > pointMapperConfig = Mappers.Xy <ChartDataPoint <double> >()
                                                                           .X(dataPoint => dataPoint.X)
                                                                           .Y(dataPoint => dataPoint.Y);

            this.SeriesCollection = new SeriesCollection(pointMapperConfig);

            this.AddSeries(chartAlertProperty.ChartType, chartValues);

            this.XAxisFormatter = value => value.ToString(CultureInfo.InvariantCulture);

            this.SetYAxisFormat(chartAlertProperty);
        }
        /// <summary>
        /// Initializes a chart with X axis from type <see cref="AlertPresentation.ChartAxisType.DateAxis"/>.
        /// </summary>
        /// <param name="chartAlertProperty">The chart alert property that should be displayed.</param>
        private void InitializeDateTimeXAxisChart(ChartAlertProperty chartAlertProperty)
        {
            var sortedDateTimePoints = chartAlertProperty.DataPoints.OrderBy(point => point.X).ToList();

            var chartDatePoints = sortedDateTimePoints.Select(dataPoint =>
            {
                DateTime x = ConvertCoordinateValueToDateTime(dataPoint.X, "X");
                double y   = ConvertCoordinateValueToDouble(dataPoint.Y, "Y");

                return(new ChartDataPoint <DateTime>(x, y));
            });

            var chartValues = new ChartValues <ChartDataPoint <DateTime> >(chartDatePoints);

            // In order to support Bar Chart, Since we are using DateTime.Ticks as X, the width of the bar is 1 tick and 1 tick is 1 millisecond.
            // In order to make our bars visible we need to change the unit of the chart. Hence, we are taking the difference between 2 first points (assuming difference between every 2 points is equal)
            double xAxisFactor = chartValues.Count > 1 ?
                                 chartValues.Skip(1).First().X.Ticks - chartValues.First().X.Ticks :
                                 chartValues.First().X.Ticks;

            CartesianMapper <ChartDataPoint <DateTime> > pointMapperConfig = Mappers.Xy <ChartDataPoint <DateTime> >()
                                                                             .X(dateTimeDataPoint => dateTimeDataPoint.X.Ticks / xAxisFactor)
                                                                             .Y(dateTimeDataPoint => dateTimeDataPoint.Y);

            this.SeriesCollection = new SeriesCollection(pointMapperConfig);

            this.AddSeries(chartAlertProperty.ChartType, chartValues);

            this.XAxisFormatter = value => new DateTime((long)(value * xAxisFactor)).ToString(CultureInfo.InvariantCulture);

            this.SetYAxisFormat(chartAlertProperty);
        }
 /// <summary>
 /// Sets the Y axis format.
 /// </summary>
 /// <param name="chartAlertProperty">The chart alert property that should be displayed.</param>
 private void SetYAxisFormat(ChartAlertProperty chartAlertProperty)
 {
     if (chartAlertProperty.YAxisType == ChartAxisType.Percentage)
     {
         this.YAxisFormatter = value => $"{value.ToString(CultureInfo.InvariantCulture)}%";
     }
     else
     {
         this.YAxisFormatter = value => value.ToString(CultureInfo.InvariantCulture);
     }
 }
示例#7
0
        public void WhenConvertingChartAlertPropertyToChartPropertyControlViewModelThenTheResultIsAsExpected()
        {
            var chartPoints = new List <ChartPoint> {
                new ChartPoint(DateTime.Now, 8), new ChartPoint(DateTime.Now.AddDays(1), 6), new ChartPoint(DateTime.Now.AddDays(2), 4), new ChartPoint(DateTime.Now.AddDays(3), 14), new ChartPoint(DateTime.Now.AddDays(5), 10)
            };
            var chartAlertProperty = new ChartAlertProperty("propertyName", "displayName", 5, ChartType.LineChart, ChartAxisType.Date, ChartAxisType.Number, chartPoints);

            var    converter = new ChartPropertyToChartPropertyControlViewModelConverter();
            object result    = converter.Convert(chartAlertProperty, typeof(ChartPropertyControlViewModel), null, new CultureInfo("en-us"));

            Assert.IsInstanceOfType(result, typeof(ChartPropertyControlViewModel));
        }
        private static void VerifyPresentationTestAlertDisplayedProperty(List <AlertProperty> properties, string propertyName, string displayName, byte order)
        {
            var property = properties.SingleOrDefault(p => p.PropertyName == propertyName);

            Assert.IsNotNull(property, $"Property {propertyName} not found");

            Assert.IsInstanceOfType(property, typeof(DisplayableAlertProperty));
            Assert.AreEqual(order, ((DisplayableAlertProperty)property).Order);
            Assert.AreEqual(displayName, ((DisplayableAlertProperty)property).DisplayName);

            if (propertyName == "LongTextPropertyName")
            {
                Assert.AreEqual("LongTextValue", ((LongTextAlertProprety)property).Value);
            }
            else if (propertyName == "UrlValue")
            {
                Assert.AreEqual("<a href=\"https://www.bing.com/\">LinkText1</a>", ((TextAlertProperty)property).Value);
            }
            else if (propertyName == "TextValue")
            {
                Assert.AreEqual("TextValue", ((TextAlertProperty)property).Value);
            }
            else if (propertyName == "KeyValue")
            {
                KeyValueAlertProperty alertProperty = (KeyValueAlertProperty)property;
                Assert.AreEqual("value1", alertProperty.Value["key1"]);
                Assert.AreEqual(false, alertProperty.ShowHeaders);
            }
            else if (propertyName == "KeyValueWithHeaders")
            {
                KeyValueAlertProperty alertProperty = (KeyValueAlertProperty)property;
                Assert.AreEqual("value1", alertProperty.Value["key1"]);
                Assert.AreEqual(true, alertProperty.ShowHeaders);
                Assert.AreEqual("Keys", alertProperty.KeyHeaderName);
                Assert.AreEqual("Values1", alertProperty.ValueHeaderName);
            }
            else if (propertyName == "DataPoints")
            {
                ChartAlertProperty alertProperty = (ChartAlertProperty)property;

                Assert.AreEqual(1, alertProperty.DataPoints.Count);
                Assert.AreEqual(new DateTime(2018, 7, 9, 14, 31, 0, DateTimeKind.Utc), alertProperty.DataPoints[0].X);
                Assert.AreEqual(5, alertProperty.DataPoints[0].Y);

                Assert.AreEqual(ContractsChartType.LineChart, alertProperty.ChartType);
                Assert.AreEqual(ContractsChartAxisType.Date, alertProperty.XAxisType);
                Assert.AreEqual(ContractsChartAxisType.Number, alertProperty.YAxisType);
            }
            else if (propertyName == "Table")
            {
                TableAlertProperty alertProperty = (TableAlertProperty)property;
                Assert.AreEqual(true, alertProperty.ShowHeaders);

                Assert.AreEqual(2, alertProperty.Values.Count);
                Assert.IsInstanceOfType(alertProperty.Values[0], typeof(TableData));
                Assert.AreEqual("p11", ((TableData)alertProperty.Values[0]).Prop1);
                Assert.AreEqual("p21", ((TableData)alertProperty.Values[0]).Prop2);
                Assert.AreEqual("NDP1", ((TableData)alertProperty.Values[0]).NonDisplayProp);

                Assert.IsInstanceOfType(alertProperty.Values[1], typeof(TableData));
                Assert.AreEqual("p12", ((TableData)alertProperty.Values[1]).Prop1);
                Assert.AreEqual("p22", ((TableData)alertProperty.Values[1]).Prop2);
                Assert.AreEqual("NDP2", ((TableData)alertProperty.Values[1]).NonDisplayProp);

                Assert.AreEqual(2, alertProperty.Columns.Count);
                Assert.AreEqual("prop1", alertProperty.Columns[0].PropertyName);
                Assert.AreEqual("First Prop", alertProperty.Columns[0].DisplayName);
                Assert.AreEqual("Prop2", alertProperty.Columns[1].PropertyName);
                Assert.AreEqual("Second Prop", alertProperty.Columns[1].DisplayName);
            }
            else if (propertyName == "SingleColumnTable")
            {
                TableAlertProperty alertProperty = (TableAlertProperty)property;
                Assert.AreEqual(false, alertProperty.ShowHeaders);

                Assert.AreEqual(3, alertProperty.Values.Count);
                Assert.AreEqual("value1", alertProperty.Values[0]);
                Assert.AreEqual("value2", alertProperty.Values[1]);
                Assert.AreEqual("value3", alertProperty.Values[2]);

                Assert.AreEqual(0, alertProperty.Columns.Count);
            }
            else
            {
                Assert.Fail($"Unknown property '{propertyName}'");
            }
        }