天天看點

幾個重要的屬性實作思路代碼實作

幾個重要的屬性

在介紹實作方法前需要先了解chart控件的幾個屬性

  1. ChartAreas :繪圖區域,當資料量大時隻要一個繪圖區域。
  2. AxisX:X軸。(Y軸一緻,此文進介紹X軸)。
  3. AxisX.ScrollBar:X軸滾動條。
  4. AxisX.ScaleView.Position:X軸顯示的起始值。
  5. AxisX.ScaleView.Size:X軸顯示資料的數量
幾個重要的屬性實作思路代碼實作

圖中

AxisX.ScaleView.Position = 941

AxisX.ScaleView.Size = 1941-941+1

實作思路

  1. 将資料分段每段10000到50000之間(以50000為例)。
  2. 給chart控件添加滾動條,
  3. 将第一個資料段的資料綁定到chart資料源上,給chart添加滑鼠滾動事件。
  4. 滾動滾輪可以更改AxisX.ScaleView.Position的值,當顯示完最後一個數值時,更改chart的資料源将第二段資料綁定在chart資料源上。
  5. 依次循環就可以實作所有資料的顯示

代碼實作

  1. 資料分段
double[] data = {...};//需要顯示的資料 長度為200萬。
public List<double[]> DataPanel = new List<double[]>();
for(int m =0;m<40;m++)
{
  double [] smallArray = new double [50000]; 
  for(int n = 0;n<50000;n++)
  {
  	smallArray [n] = data [i*50000+n];
  }
  DataPanel .add(smallArray);
}           

複制

  1. 給chart控件添加滾動條
private void ChartScrollbarStyle()
 {        
  chartAmend.ChartAreas[0].AxisX.ScrollBar.Enabled = true;
  chartAmend.ChartAreas[0].AxisX.ScaleView.Position = 1;
  chartAmend.ChartAreas[0].AxisX.ScaleView.Size = 300;
  chartAmend.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
  chartAmend.ChartAreas[0].AxisX.ScrollBar.ButtonColor = Color.Silver;
  chartAmend.ChartAreas[0].AxisX.ScrollBar.LineColor = Color.Black;
  chartAmend.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = false;
}           

複制

  1. 将資料綁定在chart資料源上,更改DataCount,就更改了資料源。
int DataCount = 0;
chart1.Series[0].Points.DataBindY(DataPanel[DataCount]);           

複制

  1. 給chart控件添加滑鼠滾輪事件
chart1.MouseWheel += Chart1_MouseWheel;
private void Chart1_MouseWheel(object sender, MouseEventArgs e)
{
  int position = Convert.ToInt32(chart1.ChartAreas[0].AxisX.ScaleView.Position);
  int WindowSize = chart1.ChartAreas[0].AxisX.ScaleView.Size;
  if (e.Delta < 0)
  {
     position += 200;//滾輪動一下,移動多少資料
     if (position >= chartAmend.ChartAreas[0].AxisX.Maximum - WindowSize)//一段資料顯示完畢
      {
          DataCount++;
          if (DataCount >= DataPanel.Count)
          {
            MessageBox.Show("所有資料已經全部顯示完畢","提示");
           	DataCount = DataPanel.Count-1;
           	return;
      	  }  
		chart1.Series[0].Points.DataBindY(Overall.DataPanel[DataCount]);
		position = 1;//新的一段資料開始時 滾動條移動到最左側
      }
  }
  else
  {
     position -= 200;
     if (position < 1)
     {
		    if (DataCount == 0)
		     {
		        position = 1;
		        MessageBox.Show("已經是第一個資料", "提示");
		     }
		     else
		       {             
		         DataCount--;
		       } 
          chart1.Series[0].Points.DataBindY(Overall.DataPanel[DataCount]);
		  position = Convert.ToInt32(chart1.ChartAreas[0].AxisX.Maximum - WindowSize);
      } 
	}
	chart1.ChartAreas[0].AxisX.ScaleView.Position = position;
}           

複制

  1. 到此處,基本上就已經完成了。該方法本人經過驗證是可行的,但是因為本文的代碼是經過删減整理的,可能會有一些問題。取用時要自行驗證。