.NET Framework 4.5 中chart(图表)控件的使用

优秀博客:

.net chart(图表)控件的使用-System.Windows.Forms.DataVisualization.dll


http://www.cnblogs.com/mooncher/p/3769619.html



http://blog.csdn.net/u010701641/article/details/50836628



功能强大的图表控件(chart 控件)包含于:Microsoft Chart Controls for Microsoft .NET Framework 4.5中


一、需在工程中加需要的引用.DLL文件


1. WinForm应用程序中要想使用该图表控件,需引用如下DLL:


System.Windows.Forms.DataVisualization.Design.dll


System.Windows.Forms.DataVisualization.dll


System.Windows.Forms.DataVisualization.xml


2. Web应用程序中要想使用该图表控件,需引用如下DLL:


System.Web.DataVisualization.dll


System.Web.DataVisualization.Design.dll


System.Web.DataVisualization.xml

文件一般保存于 C:\Windows\Microsoft.NET\Framework64\v4.0.30319目录下面。


为了画柱状图,添加以下程序




using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace WindowsFormsApplication2
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend();
System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();
System.Windows.Forms.DataVisualization.Charting.Series series2 = new System.Windows.Forms.DataVisualization.Charting.Series();
this.chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
((System.ComponentModel.ISupportInitialize)(this.chart1)).BeginInit();
this.SuspendLayout();
//chart1.Series["Series1"].ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), chartTypeName, true);
//
// chart1
//
this.chart1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(211)))), ((int)(((byte)(223)))), ((int)(((byte)(240)))));
this.chart1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
this.chart1.BorderlineColor = System.Drawing.Color.FromArgb(((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
this.chart1.BorderlineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
this.chart1.BorderlineWidth = 2;
this.chart1.BorderSkin.SkinStyle = System.Windows.Forms.DataVisualization.Charting.BorderSkinStyle.Emboss;
chartArea1.Area3DStyle.Inclination = 15;
chartArea1.Area3DStyle.Rotation = 10;
chartArea1.Area3DStyle.WallWidth = 0;
chartArea1.AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
chartArea1.AxisX.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
chartArea1.AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
chartArea1.AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
chartArea1.AxisY.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
chartArea1.AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
chartArea1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(165)))), ((int)(((byte)(191)))), ((int)(((byte)(228)))));
chartArea1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
chartArea1.BackSecondaryColor = System.Drawing.Color.Transparent;
chartArea1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
chartArea1.BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
chartArea1.Name = "Default";
chartArea1.Position.Auto = false;
chartArea1.Position.Height = 92F;
chartArea1.Position.Width = 92F;
chartArea1.Position.X = 2F;
chartArea1.Position.Y = 3F;
chartArea1.ShadowColor = System.Drawing.Color.Transparent;
chartArea1.Name = "ChartArea1";
this.chart1.ChartAreas.Add(chartArea1);
legend1.Name = "Legend1";
this.chart1.Legends.Add(legend1);
this.chart1.Location = new System.Drawing.Point(51, 58);
this.chart1.Name = "chart1";
series1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
series1.ChartArea = "ChartArea1";
series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedArea100;
series1.Color = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(65)))), ((int)(((byte)(140)))), ((int)(((byte)(240)))));
series1.Legend = "Legend1";
series1.Name = "Series1";
series2.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
series2.ChartArea = "ChartArea1";
series2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedArea100;
series2.Color = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(252)))), ((int)(((byte)(180)))), ((int)(((byte)(65)))));
series2.Legend = "Legend1";
series2.Name = "Series2";
this.chart1.Series.Add(series1);
this.chart1.Series.Add(series2);
chart1.Series["Series1"].ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
chart1.Series["Series2"].ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
/* series1.ChartArea = "ChartArea1";
series1.Legend = "Legend1";
series1.Name = "Series1";
//series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedArea100;
Random rd = new Random();
double[] num = new double[20];
for (int i = 0; i < 20; i++)
{
// chart1.Series["Series1"].Points.AddY(rd.Next(75, 170));
int valuey = rd.Next(25, 110);
DataPoint point = new DataPoint((i + 1), valuey);
series1.Points.Add(point);
}
this.chart1.Series.Add(series1);
Random rd2 = new Random();
//double[] num2 = new double[20];
for (int i = 0; i < 20; i++)
{
int valuey = rd2.Next(45, 140);
DataPoint point = new DataPoint((i + 1), valuey);
series2.Points.Add(point);
//chart1.Series["Series2"].Points.AddY(rd2.Next(75, 170));
}
series2.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
series2.ChartArea = "ChartArea1";
//series2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedArea100;
series2.Color = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(252)))), ((int)(((byte)(180)))), ((int)(((byte)(65)))));
series2.Legend = "Legend1";
series2.Name = "Series2";
this.chart1.Series.Add(series2);*/
/*Random random = new Random();
for (int pointIndex = 0; pointIndex < 10; pointIndex++)
{
chart1.Series["Series1"].Points.AddY(random.Next(75, 170));
chart1.Series["Series2"].Points.AddY(random.Next(35, 125));
// chart1.Series["Series3"].Points.AddY(random.Next(45, 140));
// chart1.Series["Series4"].Points.AddY(random.Next(25, 110));
}*/
//this.chart1.Series.Add(series1);
//this.chart1.Series.Add(series2);
this.Load += new System.EventHandler(this.StackedChartType_Load);
this.chart1.Size = new System.Drawing.Size(396, 308);
this.chart1.TabIndex = 0;
this.chart1.Text = "chart1";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(635, 424);
this.Controls.Add(this.chart1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit();
this.ResumeLayout(false);

}

#endregion
private void StackedChartType_Load(object sender, System.EventArgs e)
{
// Populate series data
Random random = new Random();
for (int pointIndex = 0; pointIndex < 10; pointIndex++)
{
chart1.Series["Series1"].Points.AddY(random.Next(75, 170));
chart1.Series["Series2"].Points.AddY(random.Next(35, 125));
// chart1.Series["Series3"].Points.AddY(random.Next(45, 140));
// chart1.Series["Series4"].Points.AddY(random.Next(25, 110));
}

// Set selection
//comboBoxChartType.SelectedIndex = 2;
}
private System.Windows.Forms.DataVisualization.Charting.Chart chart1;
}
}









阅读更多
文章标签: 控件 .net
个人分类: C#学习
上一篇功率谱
下一篇使用matlab工具箱设计IIR滤波器(巴特沃斯)
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭