예제 #1
0
        protected override void OnRender(DrawingContext drawingContext)
        {
            Rect rectAll = new Rect(0, 0, ActualWidth, ActualHeight);

            drawingContext.DrawRectangle(Background, null, rectAll);

            TimeDivision scale = GetTimeScale(ViewPort, ActualWidth);

            TimeSpan earliestFull = RoundDown(Offset, scale.MajorScale);

            TimeSpan currentPosition = earliestFull;

            Typeface typeface = new Typeface(new FontFamily("Arial"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);

            if (ShowMarker)
            {
                double marker = TimeSpanToPosition(Marker);

                drawingContext.DrawLine(new Pen(Brushes.Red, 1), new Point(marker, 0), new Point(marker, ActualHeight));
            }

            while (currentPosition <= Offset + ViewPort)
            {
                double x = TimeSpanToPosition(currentPosition);

                drawingContext.DrawLine(new Pen(Brushes.White, 1), new Point(x, 32), new Point(x, 64));



                FormattedText text = new FormattedText(GetText(currentPosition), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, 10, Brushes.White);

                drawingContext.DrawText(text, new Point(x - text.Width / 2, text.Height));

                for (int i = 1; i < scale.Subdivisions; i++)
                {
                    TimeSpan smallTicks = currentPosition + scale.MajorScale.Divide(scale.Subdivisions).Multiply(i);
                    double   x2         = TimeSpanToPosition(smallTicks);
                    drawingContext.DrawLine(new Pen(Brushes.White, 1), new Point(x2, 48), new Point(x2, 64));
                }

                currentPosition += scale.MajorScale;
            }
        }
        private static TimeDivision GetTimeScale(TimeSpan viewPort, double width)
        {
            double   minSizePerMajorUnit = 100.0;
            double   units              = width / minSizePerMajorUnit;
            TimeSpan minTimeSpan        = viewPort.Divide(units);
            TimeSpan actualUnitTimeSpan = RoundUp(minTimeSpan);

            TimeDivision result = new TimeDivision();

            result.MajorScale = actualUnitTimeSpan;
            if (result.MajorScale >= TimeSpan.FromMinutes(1))
            {
                result.Subdivisions = 6;
            }
            else
            {
                result.Subdivisions = 10;
            }

            return(result);
        }