📄 chartcontrol.cs
字号:
using System;
using System.Collections;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace NiceTracker
{
/// <summary>
/// Summary description for LineGraph.
/// </summary>
public class ChartControl : PictureBox
{
private int xMargin = 4;
private int yMargin = 4;
private int xMin = 0;
private int yMin = 0;
private int xMax = 100;
private int yMax = 100;
private int xNumTicks = 20;
private int yNumTicks = 10;
private int bufferLimit = 20;
private bool wrapX = false;
ArrayList data = new ArrayList();
public ChartControl()
{
}
public void AddReading( int value )
{
data.Add( value );
// Pad list if still not big enough
if ( wrapX )
{
while ( data.Count > bufferLimit )
data.RemoveAt( 0 );
}
base.Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
// Prepare graphics
Graphics g = e.Graphics;
Render( g );
}
protected override void OnResize(EventArgs e)
{
base.OnResize (e);
this.Invalidate();
this.Refresh();
}
private void Render( Graphics g )
{
Pen axisPen = new Pen( System.Drawing.Color.Black );
Pen gridPen = new Pen( System.Drawing.Color.LightGray );
Pen graphPen = new Pen( System.Drawing.Color.Red );
#region Grid background
double xGridStep = ( this.Size.Width - ( xMargin * 2.0 ) ) / xNumTicks;
double yGridStep = ( this.Size.Height - ( yMargin * 2.0 ) ) / yNumTicks;
int lastXPos = this.Size.Width - ( xMargin * 2 ) + 2;
for ( int xStep = 0; xStep <= xNumTicks; xStep++ )
{
int xPos = Convert.ToInt32( xMargin + ( xGridStep * xStep ) );
g.DrawLine( gridPen, xPos, YMargin, xPos, this.Size.Height - ( yMargin * 2 ) + 2);
lastXPos = xPos;
}
for ( int yStep = 0; yStep <= yNumTicks; yStep++ )
{
int yPos = Convert.ToInt32( yMargin + ( yGridStep * yStep ) );
g.DrawLine( gridPen, xMargin, yPos, lastXPos, yPos);
}
#endregion
#region Draw axes
// Draw x-axis
g.DrawLine( axisPen, xMargin, this.Size.Height - yMargin, this.Size.Width - xMargin, this.Size.Height - yMargin );
// Draw y-axis
g.DrawLine( axisPen, xMargin, yMargin, xMargin, this.Size.Height - yMargin );
#endregion
if ( data != null )
{
int max = maxArray( data );
if ( max > yMax )
yMax = max;
int xValues = Math.Max( bufferLimit, data.Count );
int count = 0;
double xScale = Convert.ToDouble( this.Size.Width - ( xMargin * 2.0 ) ) / Convert.ToDouble( xValues );
double yScale = Convert.ToDouble( this.Size.Height - ( yMargin * 2.0 ) ) / Convert.ToDouble( yMax );
int lastX = 0;
int lastY = 0;
if ( data.Count > 0 )
{
// If we have an initial value to work with then use it
lastX = Convert.ToInt32( ( count ) * xScale ) + xMargin;
lastY = Convert.ToInt32( ( this.Size.Height - ( yMargin * 2.0 ) ) - ( (int)data[ count ] * yScale ) );
}
else
{
// Else use the origin
lastX = xMargin;
lastY = this.Size.Height - yMargin;
}
count++;
while ( count < data.Count )
{
int val = (int)data[ count ];
int newX = Convert.ToInt32( ( count + 1 ) * xScale );
int newY = Convert.ToInt32( ( this.Size.Height - ( yMargin * 1.0 ) ) - ( val * yScale ) );
g.DrawLine( graphPen, lastX, lastY, newX, newY );
lastX = newX;
lastY = newY;
count++;
}
}
}
private int maxArray( ArrayList al )
{
int maxValue = 0;
foreach( int arrayValue in al )
{
if ( arrayValue > maxValue )
maxValue = arrayValue;
}
return maxValue;
}
private int minArray( ArrayList al )
{
int minValue = int.MaxValue;
foreach( int arrayValue in al )
{
if ( arrayValue < minValue )
minValue = arrayValue;
}
return minValue;
}
public ArrayList Data
{
get
{
return data;
}
set
{
data = value;
}
}
public int XMargin
{
get
{
return xMargin;
}
set
{
xMargin = value;
}
}
public int YMargin
{
get
{
return yMargin;
}
set
{
yMargin = value;
}
}
public int YMin
{
get
{
return yMin;
}
set
{
yMin = value;
}
}
public int YMax
{
get
{
return yMax;
}
set
{
yMax = value;
}
}
public int XMin
{
get
{
return xMin;
}
set
{
xMin = value;
}
}
public int XMax
{
get
{
return xMax;
}
set
{
xMax = value;
}
}
public int BufferLimit
{
get
{
return bufferLimit;
}
set
{
bufferLimit = value;
}
}
public bool WrapX
{
get
{
return wrapX;
}
set
{
wrapX = value;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -