/// <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); }
private void Read() { var r = new Random(); Thread.Sleep(150); var now = DateTime.Now; //_trend += r.Next( 0 , 50 ); ChartValues.AddRange(Enumerable.Range(counter, 20).Select(x => ( double )(x + r.Next(0, 50)))); //lets only use the last 150 values if (ChartValues.Count > 150) { ChartValues.Skip(ChartValues.Count - 150).Take(150); } AxisMin = 0; AxisMax = AxisMax == 100 ? 70 : 100; counter += 20; }
public void PaintX2(int j) { ChartValues <double> valueChart = new ChartValues <double>(); ChartValues <double> test = new ChartValues <double>(); for (int i = 0; i < 20; i++) { valueChart.Add(Math.Sin(i * i * j)); test.Add(Math.Cos(i)); } ValueAsset = new SeriesCollection { new LineSeries { Title = "Serie 1", Values = valueChart, ScalesYAt = 0 } }; if (GeneralSetting.MovingAverageList.Count != 0) // If there is some Moving average ... { for (int i = 0; i < GeneralSetting.MovingAverageList.Count; i++) { int period = GeneralSetting.MovingAverageList[i].Period; if (period < valueChart.Count) { ChartValues <double> valueChartMovingAverageTemp = new ChartValues <double>(); // because for the beginning sma is equal to value chart until we can compute the average for (int k = 0; k < period; k++) { valueChartMovingAverageTemp.Add(valueChart[k]); } // compute average for the rest of data for (int k = period; k < valueChart.Count; k++) { valueChartMovingAverageTemp.Add(valueChart.Skip(k).Take(period).Sum() / period); } // adding sma ValueAsset.Add(new LineSeries { Title = "SMA" + period, Values = valueChartMovingAverageTemp, Fill = System.Windows.Media.Brushes.Transparent }); } } } if (GeneralSetting.isBollingerBands) { ChartValues <double> bollingerAverage = new ChartValues <double>(); int period = GeneralSetting.BollingerBand.Period; for (int k = 0; k < period; k++) { bollingerAverage.Add(valueChart[k]); } // compute average for the rest of data for (int k = period; k < valueChart.Count; k++) { bollingerAverage.Add(valueChart.Skip(k).Take(period).Sum() / period); } ChartValues <double> bollingerTop = new ChartValues <double>(); for (int i = 1; i < bollingerAverage.Count; i++) { double result = bollingerAverage[i] + StandardDeviation(bollingerAverage.Take(i).ToList(), bollingerAverage.Take(i).Count()); bollingerTop.Add(result); } ChartValues <double> bollingerDown = new ChartValues <double>(); for (int i = 1; i < bollingerAverage.Count; i++) { double result = bollingerAverage[i] - StandardDeviation(bollingerAverage.Take(i).ToList(), bollingerAverage.Take(i).Count()); bollingerDown.Add(result); } ValueAsset.Add(new LineSeries { Title = "BollingerAverage", Values = bollingerAverage, Stroke = Brushes.Green, Fill = System.Windows.Media.Brushes.Transparent }); ValueAsset.Add(new LineSeries { Title = "BollingerTop", Values = bollingerTop, Stroke = Brushes.Green, Fill = System.Windows.Media.Brushes.Transparent }); ValueAsset.Add(new LineSeries { Title = "BollingerDown", Values = bollingerDown, Stroke = Brushes.Green, Fill = System.Windows.Media.Brushes.Transparent }); } }