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

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;
上述代码就是计算横坐标的值并显示在页面

 

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

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

我们在上述类中添加一个

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
///
         /// 水平方向是否显示日期
         ///
         "color: #ff0000;" > private  bool  isHorizontalDateTimeAxis = false ;
 
         ///
         /// 水平方向是否显示日期
         /// 修改人:pigwing
         /// 修改日期:2010-11-19 9:57
         ///
         public  bool  IsHorizontalDateTimeAxis
         {
             get  { return  isHorizontalDateTimeAxis; }
             set  { isHorizontalDateTimeAxis = value; }
         }
 
         private  void  UpdateUIRepresentation()
         {
             UpdateUIRepresentation(Mouse.GetPosition( this ));
         }
 
         ///
         /// 更新水平方向显示值
         ///
         ///
         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;
                 "color: #ff0000;" > 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);
                 "color: #ff0000;" >}
                 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就行了.

 

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

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

原文转自:

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

你可能感兴趣的:(.net(C#,winform,WPF))