pigwing

导航

 

我们打开DynamicDataDisplay的源码.内部自带有samples其中v2中带有一个名叫TooltipSample的例子.

如图...坐标尺默认计算的是横坐标的值.若我们的横坐标是以日期计算呢?则我们看源码

我们找到一个名为CursorCoordinateGraph的xaml文件.它负责渲染横纵坐标的值

if (xTextMapping != null)
   text = xTextMapping(xValue);
// doesnot have xTextMapping or it returned null
 if (text == null)
     text = GetRoundedValue(visible.Left, visible.Right, xValue);

if (!String.IsNullOrEmpty(customXFormat))
     text = String.Format(customXFormat, text);
horizTextBlock.Text = text;
上述代码就是计算横坐标的值并显示在页面

 

目前我并没有找到那个类可以帮助显示横坐标为日期的值...若存在,请各位大神知点一下小弟

我目前的解决方案是修改源码.

我们在上述类中添加一个

private HorizontalDateTimeAxis dateAxis = new HorizontalDateTimeAxis();

 

/// <summary>
        /// 水平方向是否显示日期
        /// </summary>
        private bool isHorizontalDateTimeAxis = false;

        /// <summary>
        /// 水平方向是否显示日期
        /// 修改人:pigwing
        /// 修改日期:2010-11-19 9:57
        /// </summary>
        public bool IsHorizontalDateTimeAxis
        {
            get { return isHorizontalDateTimeAxis; }
            set { isHorizontalDateTimeAxis = value; }
        }

		private void UpdateUIRepresentation()
		{
			UpdateUIRepresentation(Mouse.GetPosition(this));
		}

        /// <summary>
        /// 更新水平方向显示值
        /// </summary>
        /// <param name="mousePos"></param>
		private void UpdateUIRepresentation(Point mousePos)
		{
			if (Plotter2D == null) return;

			var transform = Plotter2D.Viewport.Transform;
			Rect visible = Plotter2D.Viewport.Visible;
			Rect output = Plotter2D.Viewport.Output;

			if (!output.Contains(mousePos)) return;

			horizLine.X1 = output.Left;
			horizLine.X2 = output.Right;
			horizLine.Y1 = mousePos.Y;
			horizLine.Y2 = mousePos.Y;

			vertLine.X1 = mousePos.X;
			vertLine.X2 = mousePos.X;
			vertLine.Y1 = output.Top;
			vertLine.Y2 = output.Bottom;

			if (UseDashOffset)
			{
				horizLine.StrokeDashOffset = (output.Right - mousePos.X) / 2;
				vertLine.StrokeDashOffset = (output.Bottom - mousePos.Y) / 2;
			}

			Point mousePosInData = mousePos.ScreenToData(transform);

			string text = null;

			if (showVerticalLine)
			{
				double xValue = mousePosInData.X;
                if (!isHorizontalDateTimeAxis)
                {
                    if (xTextMapping != null)
                        text = xTextMapping(xValue);

                    // doesnot have xTextMapping or it returned null
                    if (text == null)
                        text = GetRoundedValue(visible.Left, visible.Right, xValue);

                    if (!String.IsNullOrEmpty(customXFormat))
                        text = String.Format(customXFormat, text);
                }
                else
                {
                    text = dateAxis.ConvertFromDouble(xValue).ToString();
                }

				horizTextBlock.Text = text;
			}

			double width = horizGrid.ActualWidth;
			double x = mousePos.X + blockShift.X;
			if (x + width > output.Right)
			{
				x = mousePos.X - blockShift.X - width;
			}
			Canvas.SetLeft(horizGrid, x);

			if (showHorizontalLine)
			{
				double yValue = mousePosInData.Y;
				text = null;
				if (yTextMapping != null)
					text = yTextMapping(yValue);

				if (text == null)
					text = GetRoundedValue(visible.Bottom, visible.Top, yValue);

				if (!String.IsNullOrEmpty(customYFormat))
					text = String.Format(customYFormat, text);
				vertTextBlock.Text = text;
			}

			// by default vertGrid is positioned on the top of line.
			double height = vertGrid.ActualHeight;
			double y = mousePos.Y - blockShift.Y - height;
			if (y < output.Top)
			{
				y = mousePos.Y + blockShift.Y;
			}
			Canvas.SetTop(vertGrid, y);

			Position = mousePos;
		}
上述红色的代码便是我修改后的代码.其实原理很简单只要将原来的double转换为datetime就行了.

 

cursorCoordinateGraph.IsHorizontalDateTimeAxis = true;
只要我们添加children时把上述属性设置为true.

日期便可以在下面正常显示了.

posted on 2010-11-22 10:02  pigwing  阅读(6326)  评论(2编辑  收藏  举报