private void Draw(bool updateLegends = true) { cnvMain.Children.Clear(); double verticalLineHorizontalMargin = 40; double horisontalLineVerticalMargin = 25; double legendsHorizontalMargin = 10; // Drawing Main lines var vLine = new Line { X1 = verticalLineHorizontalMargin, X2 = verticalLineHorizontalMargin, Y1 = 10, Y2 = cnvMain.ActualHeight - 10 }; var hLine = new Line { X1 = 10, X2 = cnvMain.ActualWidth - 10, Y1 = cnvMain.ActualHeight - horisontalLineVerticalMargin, Y2 = cnvMain.ActualHeight - horisontalLineVerticalMargin }; cnvMain.Children.Add(vLine); cnvMain.Children.Add(hLine); //------------------------------------------------------------------------------- var tmpItems = new ArrayList(); if (Items != null) { foreach (object item in Items) { tmpItems.Add(item); } } if (tmpItems.Count == 0 || String.IsNullOrEmpty(VerticalPropertyName) || String.IsNullOrEmpty(HorizontalPropertyName) || String.IsNullOrEmpty(LegendPropertyName)) { return; } if (tmpItems[0].GetType().GetProperty(VerticalPropertyName) == null) { throw new ArgumentException("VerticalPropertyName is not correct."); } if (tmpItems[0].GetType().GetProperty(HorizontalPropertyName) == null) { throw new ArgumentException("HorizontalPropertyName is not correct."); } if (tmpItems[0].GetType().GetProperty(LegendPropertyName) == null) { throw new ArgumentException("LegendPropertyName is not correct."); } tmpItems.Sort(new ItemsComparer(HorizontalPropertyName)); //------------------------------------------------------------------------------- var horizontalValues = new List <double>(); double maxValue = 0; foreach (var item in tmpItems) { var verticalValue = item.GetType().GetProperty(VerticalPropertyName).GetValue(item, null); var horizontalValue = item.GetType().GetProperty(HorizontalPropertyName).GetValue(item, null); if (!horizontalValues.Exists(i => i == Convert.ToDouble(horizontalValue))) { horizontalValues.Add(Convert.ToDouble(horizontalValue)); } if (Convert.ToDouble(verticalValue) > maxValue) { maxValue = Convert.ToDouble(verticalValue); } } if (cnvMain.ActualWidth == 0) { return; } //------------------------------------------------------------------------------- double drawingAreaWidth = (cnvMain.ActualWidth - verticalLineHorizontalMargin); double maxValueTopMargin = 10 + 20; var lMax = new Line { StrokeDashArray = new DoubleCollection() { 2 }, X1 = verticalLineHorizontalMargin - 5, X2 = hLine.X2, Y1 = maxValueTopMargin, Y2 = maxValueTopMargin }; cnvMain.Children.Add(lMax); var lAvg = new Line { StrokeDashArray = new DoubleCollection() { 2 }, X1 = lMax.X1, X2 = lMax.X2, Y1 = (hLine.Y1 - lMax.Y1) / 2 + maxValueTopMargin, Y2 = (hLine.Y1 - lMax.Y1) / 2 + maxValueTopMargin }; cnvMain.Children.Add(lAvg); var tbMax = new TextBlock { Text = maxValue.ToString() }; var formattedMaxText = new FormattedText(tbMax.Text, CultureInfo.CurrentUICulture, FlowDirection.LeftToRight, new Typeface(tbMax.FontFamily, tbMax.FontStyle, tbMax.FontWeight, tbMax.FontStretch), tbMax.FontSize, Brushes.Black); Canvas.SetLeft(tbMax, verticalLineHorizontalMargin - formattedMaxText.Width - 10); Canvas.SetTop(tbMax, lMax.Y1 - formattedMaxText.Height / 2.0); cnvMain.Children.Add(tbMax); var tbAvg = new TextBlock { Text = (maxValue / 2.0).ToString() }; var formattedAvgText = new FormattedText(tbAvg.Text, CultureInfo.CurrentUICulture, FlowDirection.LeftToRight, new Typeface(tbAvg.FontFamily, tbAvg.FontStyle, tbAvg.FontWeight, tbAvg.FontStretch), tbAvg.FontSize, Brushes.Black); Canvas.SetLeft(tbAvg, verticalLineHorizontalMargin - formattedAvgText.Width - 10); Canvas.SetTop(tbAvg, lAvg.Y1 - formattedAvgText.Height / 2.0); cnvMain.Children.Add(tbAvg); int legendsCount = Legends.Count(f => f.IsVisible || !CanChangeLegendVisibility); double barsWidth = (drawingAreaWidth - (horizontalValues.Count * legendsHorizontalMargin)) / horizontalValues.Count / legendsCount - legendsHorizontalMargin / 2.0; if (Double.IsInfinity(barsWidth) || Double.IsNaN(barsWidth)) { barsWidth = 0; } double HorItemWidth = Math.Ceiling((drawingAreaWidth - (horizontalValues.Count * legendsHorizontalMargin)) / horizontalValues.Count); for (int i = 0; i < horizontalValues.Count; i++) { Line l = new Line(); l.X1 = (HorItemWidth * i) + verticalLineHorizontalMargin + ((legendsCount * barsWidth) / 2.0) + legendsHorizontalMargin; l.X2 = l.X1; l.Y1 = hLine.Y1; l.Y2 = l.Y1 + 5; cnvMain.Children.Add(l); var tb = new TextBlock { Text = horizontalValues[i].ToString() }; var formattedText = new FormattedText(tb.Text, CultureInfo.CurrentUICulture, FlowDirection.LeftToRight, new Typeface(tb.FontFamily, tb.FontStyle, tb.FontWeight, tb.FontStretch), tb.FontSize, Brushes.Black); Canvas.SetLeft(tb, l.X1 - (formattedText.Width / 2)); Canvas.SetTop(tb, l.Y2 + 5); cnvMain.Children.Add(tb); } foreach (double horizontalIndex in horizontalValues) { var items = from object item in tmpItems where Convert.ToDouble(item.GetType().GetProperty(HorizontalPropertyName).GetValue(item, null)) == horizontalIndex orderby item.GetType().GetProperty(LegendPropertyName).GetValue(item, null) select item; int legendValueIndex = 0; foreach (object item in items) { var verticalValue = item.GetType().GetProperty(VerticalPropertyName).GetValue(item, null); var horizontalValue = item.GetType().GetProperty(HorizontalPropertyName).GetValue(item, null); var legendValue = item.GetType().GetProperty(LegendPropertyName).GetValue(item, null); object currentLegend = null; try { currentLegend = Legends.Where(i => i.LegendType.Equals(legendValue)).First(); } catch { } if (currentLegend == null || (CanChangeLegendVisibility && !(currentLegend as Legend).IsVisible)) { continue; } int horizontalValueIndex = horizontalValues.IndexOf(Convert.ToDouble(horizontalValue)); double barLeft = (HorItemWidth * horizontalValueIndex) + legendsHorizontalMargin + verticalLineHorizontalMargin + (legendValueIndex * barsWidth); var b = new Border { Style = (Style)cnvMain.FindResource("BarStyle"), Width = barsWidth, Height = Convert.ToDouble(verticalValue) * (hLine.Y1 - lMax.Y1) / maxValue, Background = (currentLegend as Legend).Brush }; Canvas.SetLeft(b, barLeft); Canvas.SetTop(b, hLine.Y1 - b.Height); cnvMain.Children.Add(b); var tbValue = new TextBlock(); Panel.SetZIndex(tbValue, 100); tbValue.Text = verticalValue.ToString(); var binding = new Binding("ValueVisibility") { Source = this }; tbValue.SetBinding(TextBlock.VisibilityProperty, binding); var formattedText = new FormattedText(tbValue.Text, CultureInfo.CurrentUICulture, FlowDirection.LeftToRight, new Typeface(tbValue.FontFamily, tbValue.FontStyle, tbValue.FontWeight, tbValue.FontStretch), tbValue.FontSize, Brushes.Black); Canvas.SetLeft(tbValue, barLeft + (((barLeft + barsWidth) - barLeft) / 2 - formattedText.Width / 2)); Canvas.SetTop(tbValue, hLine.Y1 - b.Height - formattedText.Height - 5); cnvMain.Children.Add(tbValue); legendValueIndex++; } } if (updateLegends) { DrawLegends(); } else { cnvMain.Children.Add(brdLegends); } }