在C#中绘制动态图表






2.67/5 (50投票s)
2005 年 1 月 16 日
3分钟阅读

330728

15596
本文介绍了如何在 C# 中设计动态图表。
引言
我们将使用 .NET Framework 内置的 Graphics
类来设计动态图表。它将是一个动态图表,因为数据将通过外部.ocx文件传递给图表。图表将读取此外部文件中的数据,然后根据提供的数据绘制图表。
数据页
数据在此页面上输入。 我已经输入了默认数据,因此您无需在文本框中输入任何内容,但是,如果您希望输入数据,则可以看到这对图表产生的动态影响。
图表
我将图表设计为“自动缩放”。 这意味着图表将确定发送给它的最大整数,然后相应地调整其“X”轴上的比例。 我们这样做是因为我们希望图表落在刻度的范围内。 换句话说,我们不希望图表在底部显得太小而无法正确解释,也不希望图表溢出刻度的范围。 此图表接受的最大整数为 1,000。 我们的刻度为“0 - 10”、“0 - 50”、“0 - 100”、“0 - 250”、“0 - 500”和“0 - 1000”。 图表的“Y”轴设置为读取一年中的月份。 因此,我们需要 12 个矩形,以便每个月都有一个“条”。
我推导了用于计算每个刻度的 Y 位置的公式。 图表的 Y 值底部为 600,因此矩形的顶部将是由从 600 中减去的比例确定的比率。 例如,当我们的比例为“0 - 1000”时,矩形顶部的 Y 位置由以下公式确定
Chart1 = 600 - (5 * Qso1)/ 10;
矩形的 X 位置是根据图表的宽度、一年中的月份数(对于此图表)以及每个矩形的宽度来设置的
X Position =
Bar Chart No * ((Chart Width)/(No of Rectangles) - (Rectangle Width)/2)
我将传递给图表的数据按升序排序,以确定传递给图表的最大数据值是多少。 在我调用此排序方法后,这些值将重新分配给另一个数组(int [] d
)。 此数组中的最后一个整数是最大的整数。 确定此最大整数是此数组的唯一目的,并且它不会影响数据的绘制方式。 此数组仅影响要使用的刻度。
已传递的值被分配到屏幕上的矩形位置,并且每月数据在图表中表示。 该整数绘制在条的顶部,以阐明其值。
示例代码
private void bttnStart_Click(object sender, System.EventArgs e)
{
// Instantiate new StreamReader
StreamReader streamQso = new StreamReader( riteFileName );
readQso = streamQso.ReadToEnd();
streamQso.Close();
// define trimchars[] and split string
char[] trimChars = {'\u002c'};
passQso = Regex.Split( readQso, @",\s*");
Graphics graph = this.CreateGraphics();
SolidBrush brush = new SolidBrush(Color.Red);
FontStyle style3 = FontStyle.Regular;
Font arial3 = new Font( new FontFamily( "Arial" ), 12, style3 );
FontStyle style4 = FontStyle.Bold;
Font arial4 = new Font( new FontFamily( "Arial" ), 40, style4 );
// pass integer values to private integer
this.Qso1 = Int32.Parse(passQso[0]);
this.Qso2 = Int32.Parse(passQso[1]);
this.Qso3 = Int32.Parse(passQso[2]);
this.Qso4 = Int32.Parse(passQso[3]);
this.Qso5 = Int32.Parse(passQso[4]);
this.Qso6 = Int32.Parse(passQso[5]);
this.Qso7 = Int32.Parse(passQso[6]);
this.Qso8 = Int32.Parse(passQso[7]);
this.Qso9 = Int32.Parse(passQso[8]);
this.Qso10 = Int32.Parse(passQso[9]);
this.Qso11 = Int32.Parse(passQso[10]);
this.Qso12 = Int32.Parse(passQso[11]);
// create array for bubblesort
int[] d = new int[]{Qso1,
Qso2,
Qso3,
Qso4,
Qso5,
Qso6,
Qso7,
Qso8,
Qso9,
Qso10,
Qso11,
Qso12};
// call bubblesort
BubbleSort( d );
// pass largest value
hold = d[11];
// set scales in chart
if ( hold <= 10 )
{
// When the Chart Scale is 0 to 10
Chart1 = 600 - (50 * Qso1);
Chart2 = 600 - (50* Qso2);
Chart3 = 600 - (50 * Qso3);
Chart4 = 600 - (50 * Qso4);
Chart5 = 600 - (50 * Qso5);
Chart6 = 600 - (50 * Qso6);
Chart7 = 600 - (50 * Qso7);
Chart8 = 600 - (50 * Qso8);
Chart9 = 600 - (50 * Qso9);
Chart10 = 600 - (50 * Qso10);
Chart11 = 600 - (50 * Qso11);
Chart12 = 600 - (50 * Qso12);
brush.Color = Color.Black;
graph.DrawString( "10", arial3, brush, 70, 90 );
graph.DrawString( "9", arial3, brush, 70, 140 );
graph.DrawString( "8", arial3, brush, 70, 190 );
graph.DrawString( "7", arial3, brush, 70, 240 );
graph.DrawString( "6", arial3, brush, 70, 290 );
graph.DrawString( "5", arial3, brush, 70, 340 );
graph.DrawString( "4", arial3, brush, 70, 390 );
graph.DrawString( "3", arial3, brush, 70, 440 );
graph.DrawString( "2", arial3, brush, 70, 490 );
graph.DrawString( "1", arial3, brush, 70, 540 );
graph.DrawString( "0", arial3, brush, 70, 590 );
DrawGraph(graph);
}
if( hold > 10 )
{
if(hold <= 50)
{
// When the Chart Scale is 100 to 500
Chart1 = 600 - 10 * Qso1;
Chart2 = 600 - 10 * Qso2;
Chart3 = 600 - 10 * Qso3;
Chart4 = 600 - 10 * Qso4;
Chart5 = 600 - 10 * Qso5;
Chart6 = 600 - 10 * Qso6;
Chart7 = 600 - 10 * Qso7;
Chart8 = 600 - 10 * Qso8;
Chart9 = 600 - 10 * Qso9;
Chart10 = 600 - 10 * Qso10;
Chart11 = 600 - 10 * Qso11;
Chart12 = 600 - 10 * Qso12;
brush.Color = Color.Black;
graph.DrawString( "50", arial3, brush, 50, 90 );
graph.DrawString( "45", arial3, brush, 50, 140 );
graph.DrawString( "40", arial3, brush, 50, 190 );
graph.DrawString( "35", arial3, brush, 50, 240 );
graph.DrawString( "30", arial3, brush, 50, 290 );
graph.DrawString( "25", arial3, brush, 50, 340 );
graph.DrawString( "20", arial3, brush, 50, 390 );
graph.DrawString( "15", arial3, brush, 50, 440 );
graph.DrawString( "10", arial3, brush, 50, 490 );
graph.DrawString( "5", arial3, brush, 50, 540 );
graph.DrawString( "0", arial3, brush, 50, 590 );
DrawGraph(graph);
}
}
}
private void DrawGraph(Graphics graph)
{
// Draw BarGraph
graph = this.CreateGraphics();
SolidBrush brush = new SolidBrush( Color.Blue );
Rectangle rect1 = new Rectangle(100, Chart1, 50, (600 - Chart1));
graph.FillRectangle( brush, rect1);
// Write Qso Value
FontStyle style3 = FontStyle.Regular;
Font arial3 = new Font( new FontFamily( "Arial" ), 12, style3 );
brush.Color = Color.Black;
graph.DrawString( Qso1.ToString(), arial3, brush, 112, (Chart1 - 20 ));
// Draw BarGraph
SolidBrush brush2 = new SolidBrush(Color.BlanchedAlmond);
Rectangle rect2 = new Rectangle( 170, Chart2, 50, (600 - Chart2));
graph.FillRectangle( brush2, rect2 );
// Write Qso Value
brush.Color = Color.Black;
graph.DrawString( Qso2.ToString(), arial3, brush, 182, (Chart2 - 20 ));
// Draw BarGraph
SolidBrush brush3 = new SolidBrush(Color.ForestGreen);
Rectangle rect3 = new Rectangle( 240, Chart3, 50, (600 - Chart3));
graph.FillRectangle( brush3, rect3 );
// Write Qso Value
brush.Color = Color.Black;
graph.DrawString( Qso3.ToString(), arial3, brush, 252, (Chart3 - 20 ));
// Draw BarGraph
SolidBrush brush4 = new SolidBrush(Color.Brown);
Rectangle rect4 = new Rectangle( 310, Chart4, 50, (600 - Chart4));
graph.FillRectangle( brush4, rect4 );
// Write Qso Value
brush.Color = Color.Black;
graph.DrawString( Qso4.ToString(), arial3, brush, 322, (Chart4 - 20 ));
// Draw BarGraph
SolidBrush brush5 = new SolidBrush(Color.DarkMagenta);
Rectangle rect5 = new Rectangle( 380, Chart5, 50, (600 - Chart5));
graph.FillRectangle( brush5, rect5 );
// Write Qso Value
brush.Color = Color.Black;
graph.DrawString( Qso5.ToString(), arial3, brush, 392, (Chart5 - 20 ));
// Draw BarGraph
SolidBrush brush6 = new SolidBrush(Color.BlanchedAlmond);
Rectangle rect6 = new Rectangle( 450, Chart6, 50, (600 - Chart6));
graph.FillRectangle( brush6, rect6 );
// Write Qso Value
brush.Color = Color.Black;
graph.DrawString( Qso6.ToString(), arial3, brush, 462, (Chart6 - 20 ));
// Draw BarGraph
SolidBrush brush7 = new SolidBrush(Color.DarkGreen);
Rectangle rect7 = new Rectangle ( 520, Chart7, 50, (600 - Chart7));
graph.FillRectangle( brush7, rect7 );
// Write Qso Value
brush.Color = Color.Black;
graph.DrawString( Qso7.ToString(), arial3, brush, 532, (Chart7 - 20 ));
// Draw BarGraph
SolidBrush brush8 = new SolidBrush(Color.Gold);
Rectangle rect8 = new Rectangle ( 590, Chart8, 50, (600 - Chart8));
graph.FillRectangle( brush8, rect8 );
// Write Qso Value
brush.Color = Color.Black;
graph.DrawString( Qso8.ToString(), arial3, brush, 602, (Chart8 - 20 ));
// Draw BarGraph
SolidBrush brush9 = new SolidBrush(Color.BlueViolet);
Rectangle rect9 = new Rectangle( 660, Chart9, 50, (600 - Chart9));
graph.FillRectangle( brush9, rect9 );
// Write Qso Value
brush.Color = Color.Black;
graph.DrawString( Qso9.ToString(), arial3, brush, 672, (Chart9 - 20 ));
// Draw BarGraph
SolidBrush brush10 = new SolidBrush(Color.Firebrick);
Rectangle rect10 = new Rectangle( 730, Chart10, 50, (600 - Chart10 ));
graph.FillRectangle( brush10, rect10 );
// Write Qso Value
brush.Color = Color.Black;
graph.DrawString( Qso10.ToString(), arial3, brush, 742, (Chart10 - 20 ));
// Draw BarGraph
SolidBrush brush11 = new SolidBrush(Color.BlanchedAlmond);
Rectangle rect11 = new Rectangle( 800, Chart11, 50, (600 - Chart11));
graph.FillRectangle( brush11, rect11 );
// Write Qso Value
brush.Color = Color.Black;
graph.DrawString( Qso11.ToString(), arial3, brush, 812, (Chart11 - 20 ));
// Draw BarGraph
SolidBrush brush12 = new SolidBrush(Color.DarkOrange);
Rectangle rect12 = new Rectangle( 870, Chart12, 50, (600 - Chart12));
graph.FillRectangle( brush12, rect12 );
// Write Qso Value
brush.Color = Color.Black;
graph.DrawString( Qso12.ToString(), arial3, brush, 882, (Chart12 - 20 ));
}
打印此图表
我喜欢使用 Print Scrn 打印此图表。 请按照计算机制造商提供的 Print Scrn 功能的说明进行操作,并使用该制造商提供给您的成像工具。 可以相应地调整大小和裁剪此图像(.jpg、.png)。 您可以通过电子邮件发送这些文件或打印它们,并将它们包含在您的报告中。 这是您演示文稿层的绝佳补充。
摘要
这个简单的自动缩放图表是您项目中的一个有用的工具。 它添加了颜色,并使您的程序具有一定的视觉吸引力。 享受 C# 中的 Graphics
乐趣。 构建您自己的图表以满足您自己的需求和品味。