簡體   English   中英

使用鼠標滾輪縮放會破壞 ZedGraph 中實時數據的平移(滾動)

[英]Zooming with mouse wheel breaks panning (scrolling) of real time data in ZedGraph

Windows Forms WPF 應用程序中的 ZedGraph 控件。 數據點每 N 秒自動生成並附加到圖表上。 當將新數據點添加到圖表時,我將圖表向左移動(平移)一個點,因此視口中的可見點始終不超過最后 50 個。 總的來說,它工作得很好,除了兩件事。

問題

  1. 如果用戶嘗試放大或縮小,視口將停止跟隨最后一個數據點,並且圖表會超出屏幕,因此平移停止工作
  2. 我想使用鼠標事件平移或移動圖表,而不滾動,但是當我按下鼠標右鍵並嘗試將其移動到左側時,它會嘗試縮放圖表而不是平移
protected void CreateChart(ZedGraphControl control)
{
  _rand = new Random();

  var curve = control.GraphPane.AddJapaneseCandleStick("Demo", new StockPointList());

  curve.Stick.IsAutoSize = true;
  curve.Stick.Color = Color.Blue;

  control.AutoScroll = true; // Always shift to the last data point when new data comes in
  control.IsEnableHPan = true;  // I assume this should allow me to move chart to the left using mouse
  control.IsEnableVPan = true;
  control.IsEnableHZoom = true;
  control.IsEnableVZoom = true;
  control.IsShowPointValues = true;
  control.IsShowHScrollBar = false;
  control.IsShowVScrollBar = false;
  control.IsAutoScrollRange = true;  // Always shift to the last data point when new data comes in
  control.IsZoomOnMouseCenter = false;
  control.GraphPane.XAxis.Type = AxisType.DateAsOrdinal;
  control.AxisChange();
  control.Invalidate();

  var aTimer = new Timer();

  aTimer.Elapsed += new ElapsedEventHandler(OnTime);
  aTimer.Interval = 100;
  aTimer.Enabled = true;
}

protected XDate _xDate = new XDate(2006, 2, 1);
protected double _open = 50.0;
protected Random _rand = new Random();

// Auto generate data points

protected void OnTime(object source, ElapsedEventArgs e)
{
  var control = FormCharts;

  var x = _xDate.XLDate;
  var close = _open + _rand.NextDouble() * 10.0 - 5.0;
  var hi = Math.Max(_open, close) + _rand.NextDouble() * 5.0;
  var low = Math.Min(_open, close) - _rand.NextDouble() * 5.0;

  var pt = new StockPt(x, hi, low, _open, close, 100000);

  _open = close;
  _xDate.AddDays(1.0);

  if (XDate.XLDateToDayOfWeek(_xDate.XLDate) == 6)
  {
    _xDate.AddDays(2.0);
  }

  (control.GraphPane.CurveList[0].Points as StockPointList).Add(pt);

  control.GraphPane.XAxis.Scale.Min = control.GraphPane.XAxis.Scale.Max - 50; // Hide all points except last 50, after mouse zooming this line stops working
  //control.GraphPane.XAxis.Scale.Max = control.GraphPane.XAxis.Scale.Max + 1;
  control.AxisChange();
  control.Invalidate();
}

有點解決了。

縮放后自動滾動損壞的第一個問題

這是因為縮放將這些參數設置為 FALSE。

area.XAxis.Scale.MinAuto = false;
area.XAxis.Scale.MaxAuto = false;

要修復它,請在每次新數據點出現時將其設置回 TRUE。 修復它的另一種方法是將它們始終保持為 FALSE 並手動移動圖表

protected void MoveChart(GraphPane pane, int pointsToMove, int pointsToShow)
{
  pane.XAxis.Scale.Max = pane.XAxis.Scale.Max - pointsToMove;
  pane.XAxis.Scale.Min = pane.XAxis.Scale.Max - Math.Abs(pointsToShow);
}

...

// Example : shift one point to the left and show only 50 last points

MoveChart(control.MasterPane.PaneList["Quotes"], -1, 50); 

第二個問題,使用鼠標事件實現沒有滾動條的自定義平移。

protected int _mouseX = -1;
protected int _mouseY = -1;

... 

control.MouseUpEvent += OnMouseUp;
control.MouseDownEvent += OnMouseDown;
control.MouseMoveEvent += OnMouseMove;

...

// Example : remember X and Y on mouse down and move chart until mouse up event

protected bool OnMouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
  _mouseX = -1; // unset X on mouse up
  _mouseY = -1;
  return true;
}

protected bool OnMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
  _mouseX = e.X; // remember last X on mouse down
  _mouseY = e.Y;
  return true;
}

protected bool OnMouseMove(ZedGraphControl sender, System.Windows.Forms.MouseEventArgs e)
{
  if (_mouseX >= 0) // if X was saved after mouse down
  {
    foreach (var pane in sender.MasterPane.PaneList) // move synced chart panels
    {
      MoveChart(pane, _mouseX > e.X ? -1 : 1, 50); // if mouse move is increasing X, move chart to the right, and vice versa
    }

    _mouseX = e.X; // update X to new position
    _mouseY = e.Y;
  }

  return true;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM