public override UIElement[] CreateLabels(ITicksInfo <DateTime> ticksInfo) { object info = ticksInfo.Info; var ticks = ticksInfo.Ticks; UIElement[] res = new UIElement[ticks.Length - 1]; int labelsNum = 3; if (info is DifferenceIn) { DifferenceIn diff = (DifferenceIn)info; DateFormat = GetDateFormat(diff); } else if (info is MajorLabelsInfo) { MajorLabelsInfo mInfo = (MajorLabelsInfo)info; DifferenceIn diff = (DifferenceIn)mInfo.Info; DateFormat = GetDateFormat(diff); labelsNum = mInfo.MajorLabelsCount + 1; //DebugVerify.Is(labelsNum < 100); } DebugVerify.Is(ticks.Length < 10); LabelTickInfo <DateTime> tickInfo = new LabelTickInfo <DateTime>(); for (int i = 0; i < ticks.Length - 1; i++) { tickInfo.Info = info; tickInfo.Tick = ticks[i]; string tickText = GetString(tickInfo); Grid grid = new Grid { }; // doing binding as described at http://sdolha.spaces.live.com/blog/cns!4121802308C5AB4E!3724.entry?wa=wsignin1.0&sa=835372863 grid.SetBinding(Grid.BackgroundProperty, new Binding { Path = new PropertyPath("(0)", DateTimeAxis.MajorLabelBackgroundBrushProperty), RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorType = typeof(AxisControlBase) } }); Rectangle rect = new Rectangle { StrokeThickness = 2 }; rect.SetBinding(Rectangle.StrokeProperty, new Binding { Path = new PropertyPath("(0)", DateTimeAxis.MajorLabelRectangleBorderPropertyProperty), RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorType = typeof(AxisControlBase) } }); Grid.SetColumn(rect, 0); Grid.SetColumnSpan(rect, labelsNum); for (int j = 0; j < labelsNum; j++) { grid.ColumnDefinitions.Add(new ColumnDefinition()); } grid.Children.Add(rect); for (int j = 0; j < labelsNum; j++) { var tb = new TextBlock { Text = tickText, HorizontalAlignment = HorizontalAlignment.Center, Margin = new Thickness(0, 3, 0, 3) }; Grid.SetColumn(tb, j); grid.Children.Add(tb); } ApplyCustomView(tickInfo, grid); res[i] = grid; } return(res); }
private void DoDrawMajorLabels() { ITicksProvider <T> majorTicksProvider = ticksProvider.MajorProvider; additionalLabelsCanvas.Children.Clear(); if (majorTicksProvider != null && majorLabelProvider != null) { additionalLabelsCanvas.Visibility = Visibility.Visible; Size renderSize = RenderSize; var majorTicks = majorTicksProvider.GetTicks(range, DefaultTicksProvider.DefaultTicksCount); double[] screenCoords = majorTicks.Ticks.Select(tick => createDataPoint(convertToDouble(tick))). Select(p => p.DataToScreen(transform)).Select(p => getCoordinate(p)).ToArray(); // todo this is not the best decision - when displaying, for example, // milliseconds, it causes to create hundreds and thousands of textBlocks. double rangesRatio = GetRangesRatio(majorTicks.Ticks.GetPairs().ToArray()[0], range); object info = majorTicks.Info; MajorLabelsInfo newInfo = new MajorLabelsInfo { Info = info, MajorLabelsCount = (int)Math.Ceiling(rangesRatio) }; var newMajorTicks = new TicksInfo <T> { Info = newInfo, Ticks = majorTicks.Ticks, TickSizes = majorTicks.TickSizes }; UIElement[] additionalLabels = MajorLabelProvider.CreateLabels(newMajorTicks); for (int i = 0; i < additionalLabels.Length; i++) { if (screenCoords[i].IsNaN()) { continue; } UIElement tickLabel = additionalLabels[i]; tickLabel.Measure(renderSize); StackCanvas.SetCoordinate(tickLabel, screenCoords[i]); StackCanvas.SetEndCoordinate(tickLabel, screenCoords[i + 1]); if (tickLabel is FrameworkElement) { ((FrameworkElement)tickLabel).LayoutTransform = additionalLabelTransform; } additionalLabelsCanvas.Children.Add(tickLabel); } } else { additionalLabelsCanvas.Visibility = Visibility.Collapsed; } }