WPF Chart DynamicDataDisplay的横坐标显示日期的解决方案

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

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

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

1
2
3
4
5
6
7
8
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);
1
horizTextBlock.Text = text;<br>
上述代码就是计算横坐标的值并显示在页面

 

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

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

我们在上述类中添加一个

1
private  HorizontalDateTimeAxis dateAxis = new  HorizontalDateTimeAxis();

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/// <summary>
         /// 水平方向是否显示日期
         /// </summary>
         <span style= "color: #ff0000;" > private  bool  isHorizontalDateTimeAxis = false ;</span>
 
         /// <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;
                 <span style= "color: #ff0000;" > if  (!isHorizontalDateTimeAxis)
                 {</span>
                     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);
                 <span style= "color: #ff0000;" >}
                 else
                 {
                     text = dateAxis.ConvertFromDouble(xValue).ToString();
                 }</span>
 
                 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就行了.

 

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

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

原文转自:

http://www.cnblogs.com/pigwing/archive/2010/11/22/1883832.html

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值