回复 6楼jxlxl的帖子
jxlxl 你好,
请参考如下代码,看能否实现你的功能:
- public MainPage()
- {
- InitializeComponent();
- chart.BeginUpdate();
- // create sample data
- int cnt = 12;
- double[] vals = new double[cnt];
- DateTime[] times = new DateTime[cnt];
- for (int i = 0; i < 12; i++)
- {
- times[i] = DateTime.Now.AddMinutes(5 * i);
- vals[i] = i * i;
- }
- XYDataSeries ds = new XYDataSeries();
- ds.ValuesSource = vals;
- ds.XValuesSource = times;
- ds.Loaded += new EventHandler(ds_Loaded);
- chart.Data.Children.Add(ds);
- chart.View.AxisX.Min = times[0].ToOADate();
- chart.View.AxisX.Max = times[cnt - 1].ToOADate();
- chart.View.AxisX.IsTime = true;
- chart.View.AxisX.AnnoFormat = "t";
- chart.EndUpdate();
- }
- void ds_Loaded(object sender, EventArgs e)
- {
- PlotElement pe = sender as PlotElement;
- if (pe != null && pe.Shape is Path && ((Path)pe.Shape).Data is RectangleGeometry)
- {
- Axis ax = chart.View.AxisX;
- Axis ay = chart.View.AxisY;
- double widthMinutes = 2;
- // convert width from minutes to pixels
- Point p1 = chart.View.PointFromData(
- new Point(ax.Min, ay.Min));
- Point p2 = chart.View.PointFromData(
- new Point(ax.Min + widthMinutes / 60 / 24, ay.Min));
- double w = (p2.X - p1.X) * 20; // in pixels
- RectangleGeometry rg = ((Path)pe.Shape).Data as RectangleGeometry;
- double x = rg.Rect.X + 0.5 * rg.Rect.Width;
- //rg.Rect = new Rect(x - 0.5 * w, rg.Rect.Y, w, rg.Rect.Height);
- rg.Rect = new Rect(0, 0, 1000, rg.Rect.Height);
- }
- }
- }
复制代码 |