private void Chart_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
int clientWidth = ClientRectangle.Width;
int clientHeight = ClientRectangle.Height;
// fill with white background
Brush backgroundBrush = new SolidBrush(BackColor);
g.FillRectangle(backgroundBrush, 0, 0, clientWidth - 1, clientHeight - 1);
backgroundBrush.Dispose();
// draw a black rectangle
g.DrawRectangle(blackPen, 0, 0, clientWidth - 1, clientHeight - 1);
// set clipping rectangle
g.SetClip(new Rectangle(2, 2, clientWidth - 4, clientHeight - 4));
// check if there are any data series
if (rangeX.Length != 0)
{
double xFactor = (double)(clientWidth - 10) / (rangeX.Length);
double yFactor = (double)(clientHeight - 10) / ((rangeY.Length != 0) ? rangeY.Length : 1);
// walk through all data series
foreach (KeyValuePair<string, DataSeries> kvp in seriesTable)
{
DataSeries series = kvp.Value;
// get data of the series
double[,] data = series.data;
// check for available data
if (data == null)
continue;
// check series type
if (series.type == SeriesType.Dots)
{
// draw dots
Brush brush = new SolidBrush(series.color);
int width = series.width;
int r = width >> 1;
// draw all points
for (int i = 0, n = data.GetLength(0); i < n; i++)
{
int x = (int)((data[i, 0] - rangeX.Min) * xFactor);
int y = (int)((data[i, 1] - rangeY.Min) * yFactor);
x += 5;
y = clientHeight - 6 - y;
g.FillRectangle(brush, x - r, y - r, width, width);
}
brush.Dispose();
}
else if (series.type == SeriesType.ConnectedDots)
{
// draw dots connected with 1-pixel width line
Brush brush = new SolidBrush(series.color);
Pen pen = new Pen(series.color, 1);
int width = series.width;
int r = width >> 1;
int x1 = (int)((data[0, 0] - rangeX.Min) * xFactor);
int y1 = (int)((data[0, 1] - rangeY.Min) * yFactor);
x1 += 5;
y1 = clientHeight - 6 - y1;
g.FillRectangle(brush, x1 - r, y1 - r, width, width);
// draw all lines
for (int i = 1, n = data.GetLength(0); i < n; i++)
{
int x2 = (int)((data[i, 0] - rangeX.Min) * xFactor);
int y2 = (int)((data[i, 1] - rangeY.Min) * yFactor);
x2 += 5;
y2 = clientHeight - 6 - y2;
g.FillRectangle(brush, x2 - r, y2 - r, width, width);
g.DrawLine(pen, x1, y1, x2, y2);
x1 = x2;
y1 = y2;
}
pen.Dispose();
brush.Dispose();
}
else if (series.type == SeriesType.Line)
{
// draw line
Pen pen = new Pen(series.color, series.width);
int x1 = (int)((data[0, 0] - rangeX.Min) * xFactor);
int y1 = (int)((data[0, 1] - rangeY.Min) * yFactor);
x1 += 5;
y1 = clientHeight - 6 - y1;
// draw all lines
for (int i = 1, n = data.GetLength(0); i < n; i++)
{
int x2 = (int)((data[i, 0] - rangeX.Min) * xFactor);
int y2 = (int)((data[i, 1] - rangeY.Min) * yFactor);
x2 += 5;
y2 = clientHeight - 6 - y2;
g.DrawLine(pen, x1, y1, x2, y2);
x1 = x2;
y1 = y2;
}
pen.Dispose();
}
}
}
}