private void BuildTickMarks()
        {
            double tickSize                   = 10;
            double tickXPos                   = ComputeTickXPos(1, tickSize);
            double tickYPos                   = ComputeTickYPos(1, tickSize);
            double tickRayAngle               = ComputeRayAngle(1) * (180.0 / Math.PI);
            double tickLabelPadding           = 25;
            CircleTickMarkViewModel startTick = new CircleTickMarkViewModel(tickXPos,
                                                                            tickYPos, 1, tickSize);
            CircleTickLabelViewModel startTickLabel = new CircleTickLabelViewModel(tickXPos,
                                                                                   tickYPos, tickLabelPadding, tickRayAngle, 1);

            TickMarks.Add(startTick);
            TickLabels.Add(startTickLabel);
            int tickIncrement    = ComputeTickIncrement();
            int currentTickIndex = tickIncrement;

            do
            {
                tickXPos     = ComputeTickXPos(currentTickIndex, tickSize);
                tickYPos     = ComputeTickYPos(currentTickIndex, tickSize);
                tickRayAngle = ComputeRayAngle(currentTickIndex) * (180.0 / Math.PI);
                CircleTickMarkViewModel tick = new CircleTickMarkViewModel(tickXPos,
                                                                           tickYPos, currentTickIndex, 10);
                CircleTickLabelViewModel label = new CircleTickLabelViewModel(tickXPos,
                                                                              tickYPos, tickLabelPadding, tickRayAngle, currentTickIndex);
                TickMarks.Add(tick);
                TickLabels.Add(label);
                currentTickIndex += tickIncrement;
            } while (currentTickIndex <= _sequence.RawData.Count);
        }
        protected override void OnRender(DrawingContext dc)
        {
            base.OnRender(dc);

            var height    = base.ActualHeight - ReservedSpace;
            var tickRange = Maximum - Minimum;
            var tickCount = (int)(tickRange / TickFrequency) + 1;

            var labels = TickLabels.Split(',');

            if (labels.Length != tickCount)
            {
                return;
            }

            FontFamily fontFamily    = new FontFamily("Segoe UI");
            var        fontPointSize = FontSize;
            var        fontHeight    = Math.Ceiling(fontPointSize * fontFamily.LineSpacing);
            var        typeface      = new Typeface("Segoe");

            var cultureInfo = CultureInfo.GetCultureInfo(Language.IetfLanguageTag);

            var verticalOffset = ReservedSpace / 2 - fontHeight / 2;
            var tickSpacing    = height * this.TickFrequency / tickRange;

            dc.PushOpacity(TickLabelsOpacity);

            for (var i = 0; i < tickCount; ++i)
            {
                var formattedText = new FormattedText(labels[i], cultureInfo, FlowDirection.LeftToRight, typeface, fontPointSize, Fill);
                formattedText.TextAlignment = TextAlignment.Right;
                var y = verticalOffset + height - (i * tickSpacing);
                dc.DrawText(formattedText, new Point(TickLabelsOffset, y));
            }

            dc.Pop();
        }
Ejemplo n.º 3
0
        void DoTickLabels(IChartRenderContext icrc)
        {
            var tc = new TickCalculator(Minimum, Maximum);

            _trace.Verbose($"grid range:{tc.Range} tintv:{tc.TickInterval}");
            // TODO may want to include the LabelStyle's padding if defined
            var padding = AxisLineThickness + 2 * AxisMargin;
            var tbr     = new Recycler <FrameworkElement, ItemState>(TickLabels.Select(tl => tl.tb), (ist) => {
                var fe   = CreateElement(ist);
                fe.Width = icrc.Area.Width - padding;
                if (fe is TextBlock tbb)
                {
                    tbb.Padding = Side == Side.Right ? new Thickness(padding, 0, 0, 0) : new Thickness(0, 0, padding, 0);
                }
                return(fe);
            });
            var itemstate = new List <ItemState>();
            // materialize the ticks
            var lx = tc.GetTicks().ToArray();
            var sc = new ValueAxisSelectorContext(this, icrc.Area, lx, tc.TickInterval);

            for (int ix = 0; ix < lx.Length; ix++)
            {
                //_trace.Verbose($"grid vx:{tick}");
                sc.SetTick(ix);
                var createit = true;
                if (LabelSelector != null)
                {
                    // ask the label selector
                    var ox = LabelSelector.Convert(sc, typeof(bool), null, System.Globalization.CultureInfo.CurrentUICulture.Name);
                    if (ox is bool bx)
                    {
                        createit = bx;
                    }
                    else
                    {
                        createit = ox != null;
                    }
                }
                if (!createit)
                {
                    continue;
                }
                var current = tbr.Next(null);
                var tick    = lx[ix];
                if (!current.Item1)
                {
                    // restore binding if we are using a LabelFormatter
                    if (LabelFormatter != null && LabelStyle != null)
                    {
                        BindTo(this, nameof(LabelStyle), current.Item2, FrameworkElement.StyleProperty);
                    }
                }
                // default text
                var text = tick.Value.ToString(String.IsNullOrEmpty(LabelFormatString) ? "G" : LabelFormatString);
                if (LabelFormatter != null)
                {
                    // call for Style, String override
                    var format = LabelFormatter.Convert(sc, typeof(Tuple <Style, String>), null, System.Globalization.CultureInfo.CurrentUICulture.Name);
                    if (format is Tuple <Style, String> ovx)
                    {
                        if (ovx.Item1 != null)
                        {
                            current.Item2.Style = ovx.Item1;
                        }
                        if (ovx.Item2 != null)
                        {
                            text = ovx.Item2;
                        }
                    }
                }
                var shim = new TextShim()
                {
                    Text = text
                };
                current.Item2.DataContext = shim;
                BindTo(shim, nameof(Visibility), current.Item2, UIElement.VisibilityProperty);
                var state = new ItemState()
                {
                    tb = current.Item2, tick = tick
                };
                state.SetLocation(icrc.Area.Left, tick.Value);
                sc.Generated(tick);
                itemstate.Add(state);
            }
            // VT and internal bookkeeping
            TickLabels = itemstate;
            Layer.Remove(tbr.Unused);
            Layer.Add(tbr.Created);
            foreach (var xx in TickLabels)
            {
                // force it to measure; needed for Transforms
                xx.tb.Measure(icrc.Dimensions);
            }
        }