Bất kỳ ai biết tìm mẫu đơn nào bằng MSChart trong C#?Mẫu sử dụng MSCHART trong C#
Tôi đã làm ví dụ.
//name program.cs using System; using System.Collections.Generic; using System.Windows.Forms; namespace MSChart_Sample { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
//name Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; //for Visual Studio 2005 include this lines in "project_name.csproj" /* <Reference Include="System.Core"> <RequiredTargetFramework>3.5</RequiredTargetFramework> </Reference> <Reference Include="System.Windows.Forms.DataVisualization"> <Name>System.Windows.Forms.DataVisualization</Name> </Reference> */ namespace MSChart_Sample { class SimplerDialogBox : Form { private System.Windows.Forms.DataVisualization.Charting.Chart chart1; private System.Windows.Forms.Label labelSampleComment; private System.Windows.Forms.Panel panel1; private System.Windows.Forms.Label label1; private System.Windows.Forms.ComboBox comboBoxChartType; private System.Windows.Forms.Label label2; private System.Windows.Forms.ComboBox comboBoxPointLabels; private System.Windows.Forms.CheckBox checkBoxShowMargin; private System.Windows.Forms.CheckBox checkBoxShow3D; private System.Windows.Forms.Button buttonClose; private System.Windows.Forms.Label label3; public SimplerDialogBox() { Text = "Sample graph using MSChart"; FormBorderStyle = FormBorderStyle.FixedDialog; ControlBox = false; MaximizeBox = false; MinimizeBox = false; ShowInTaskbar = false; Button btn = new Button(); InitializeComponent(); this.chart1.Select(); } private void LineCurvesChartType_Load(object sender, System.EventArgs e) { comboBoxChartType.SelectedIndex = 0; comboBoxPointLabels.SelectedIndex = 0; checkBoxShow3D.Checked = false; // Populate series data Random random = new Random(); for (int pointIndex = 0; pointIndex < 10; pointIndex++) { chart1.Series["Series1"].Points.AddY(random.Next(45, 95)); chart1.Series["Series2"].Points.AddY(random.Next(5, 65)); } UpdateChartSettings(); } private void UpdateChartSettings() { // Set series chart type chart1.Series["Series1"].ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), comboBoxChartType.Text, true); chart1.Series["Series2"].ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), comboBoxChartType.Text, true); // Set point labels if (comboBoxPointLabels.Text != "None") { chart1.Series["Series1"].IsValueShownAsLabel = true; chart1.Series["Series2"].IsValueShownAsLabel = true; if (comboBoxPointLabels.Text != "Auto") { chart1.Series["Series1"]["LabelStyle"] = comboBoxPointLabels.Text; chart1.Series["Series2"]["LabelStyle"] = comboBoxPointLabels.Text; } } else { chart1.Series["Series1"].IsValueShownAsLabel = false; chart1.Series["Series2"].IsValueShownAsLabel = false; } // Set X axis margin chart1.ChartAreas["Default"].AxisX.IsMarginVisible = checkBoxShowMargin.Checked; } private void comboBoxChartType_SelectedIndexChanged(object sender, System.EventArgs e) { UpdateChartSettings(); } private void checkBoxShowMargin_CheckedChanged(object sender, System.EventArgs e) { UpdateChartSettings(); } private void checkButtonClose(object sender, System.EventArgs e) { Close(); } private void checkBoxShow3D_CheckedChanged(object sender, System.EventArgs e) { chart1.ChartAreas[0].Area3DStyle.Enable3D = checkBoxShow3D.Checked; if (checkBoxShow3D.Checked) { chart1.Series["Series1"].MarkerStyle = MarkerStyle.None; chart1.Series["Series2"].MarkerStyle = MarkerStyle.None; chart1.Series["Series1"].BorderWidth = 1; chart1.Series["Series2"].BorderWidth = 1; } else { chart1.Series["Series1"].MarkerStyle = MarkerStyle.Circle; chart1.Series["Series2"].MarkerStyle = MarkerStyle.Diamond; chart1.Series["Series1"].BorderWidth = 3; chart1.Series["Series2"].BorderWidth = 3; } } 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(); this.labelSampleComment = new System.Windows.Forms.Label(); this.panel1 = new System.Windows.Forms.Panel(); this.checkBoxShow3D = new System.Windows.Forms.CheckBox(); this.checkBoxShowMargin = new System.Windows.Forms.CheckBox(); this.comboBoxPointLabels = new System.Windows.Forms.ComboBox(); this.label2 = new System.Windows.Forms.Label(); this.comboBoxChartType = new System.Windows.Forms.ComboBox(); this.label1 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.buttonClose = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.chart1)).BeginInit(); this.panel1.SuspendLayout(); this.SuspendLayout(); // // chart1 // this.chart1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(243)))), ((int)(((byte)(223)))), ((int)(((byte)(193))))); this.chart1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom; this.chart1.BorderlineColor = System.Drawing.Color.FromArgb(((int)(((byte)(181)))), ((int)(((byte)(64)))), ((int)(((byte)(1))))); 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 = 40; chartArea1.Area3DStyle.IsClustered = true; chartArea1.Area3DStyle.IsRightAngleAxes = false; chartArea1.Area3DStyle.LightStyle = System.Windows.Forms.DataVisualization.Charting.LightStyle.Realistic; chartArea1.Area3DStyle.Perspective = 9; chartArea1.Area3DStyle.Rotation = 25; chartArea1.Area3DStyle.WallWidth = 3; 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.OldLace; chartArea1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom; chartArea1.BackSecondaryColor = System.Drawing.Color.White; 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.ShadowColor = System.Drawing.Color.Transparent; this.chart1.ChartAreas.Add(chartArea1); legend1.BackColor = System.Drawing.Color.Transparent; legend1.Enabled = false; legend1.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold); legend1.IsTextAutoFit = false; legend1.Name = "Default"; this.chart1.Legends.Add(legend1); this.chart1.Location = new System.Drawing.Point(16, 32); this.chart1.Name = "chart1"; series1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105))))); series1.BorderWidth = 3; series1.ChartArea = "Default"; series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; series1.Color = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(65)))), ((int)(((byte)(140)))), ((int)(((byte)(240))))); series1.Legend = "Default"; series1.MarkerSize = 8; series1.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle; series1.Name = "Series1"; series1.ShadowColor = System.Drawing.Color.Black; series1.ShadowOffset = 2; series1.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double; series1.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double; series2.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105))))); series2.BorderWidth = 3; series2.ChartArea = "Default"; series2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; series2.Color = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(224)))), ((int)(((byte)(64)))), ((int)(((byte)(10))))); series2.Legend = "Default"; series2.MarkerSize = 9; series2.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Diamond; series2.Name = "Series2"; series2.ShadowColor = System.Drawing.Color.Black; series2.ShadowOffset = 2; series2.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double; series2.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double; this.chart1.Series.Add(series1); this.chart1.Series.Add(series2); this.chart1.Size = new System.Drawing.Size(412, 296); this.chart1.TabIndex = 1; // // labelSampleComment // this.labelSampleComment.Font = new System.Drawing.Font("Verdana", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.labelSampleComment.Location = new System.Drawing.Point(16, 0); this.labelSampleComment.Name = "labelSampleComment"; this.labelSampleComment.Size = new System.Drawing.Size(702, 24); this.labelSampleComment.TabIndex = 0; this.labelSampleComment.Text = "This sample demonstrates the Line, Spline and, StepLine chart types. "; this.labelSampleComment.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // // panel1 // this.panel1.Controls.Add(this.buttonClose); this.panel1.Controls.Add(this.checkBoxShow3D); this.panel1.Controls.Add(this.checkBoxShowMargin); this.panel1.Controls.Add(this.comboBoxPointLabels); this.panel1.Controls.Add(this.label2); this.panel1.Controls.Add(this.comboBoxChartType); this.panel1.Controls.Add(this.label1); this.panel1.Location = new System.Drawing.Point(432, 40); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(292, 280); this.panel1.TabIndex = 2; // // checkBoxShow3D // this.checkBoxShow3D.CheckAlign = System.Drawing.ContentAlignment.MiddleRight; this.checkBoxShow3D.Location = new System.Drawing.Point(14, 104); this.checkBoxShow3D.Name = "checkBoxShow3D"; this.checkBoxShow3D.Size = new System.Drawing.Size(168, 24); this.checkBoxShow3D.TabIndex = 5; this.checkBoxShow3D.Text = "Display chart as 3&D:"; this.checkBoxShow3D.TextAlign = System.Drawing.ContentAlignment.MiddleRight; this.checkBoxShow3D.CheckedChanged += new System.EventHandler(this.checkBoxShow3D_CheckedChanged); // // checkBoxShowMargin // this.checkBoxShowMargin.CheckAlign = System.Drawing.ContentAlignment.MiddleRight; this.checkBoxShowMargin.Location = new System.Drawing.Point(14, 72); this.checkBoxShowMargin.Name = "checkBoxShowMargin"; this.checkBoxShowMargin.Size = new System.Drawing.Size(168, 24); this.checkBoxShowMargin.TabIndex = 4; this.checkBoxShowMargin.Text = "Show X Axis &Margin:"; this.checkBoxShowMargin.TextAlign = System.Drawing.ContentAlignment.MiddleRight; this.checkBoxShowMargin.CheckedChanged += new System.EventHandler(this.checkBoxShowMargin_CheckedChanged); // // close // this.buttonClose.Location = new System.Drawing.Point(180, 180); this.buttonClose.Name = "buttonClose"; this.buttonClose.Size = new System.Drawing.Size(60, 30); this.buttonClose.TabIndex = 6; this.buttonClose.Text = "Close"; this.buttonClose.TextAlign = System.Drawing.ContentAlignment.MiddleRight; this.buttonClose.Click += new System.EventHandler(this.checkButtonClose); // // comboBoxPointLabels // this.comboBoxPointLabels.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBoxPointLabels.Items.AddRange(new object[] { "None", "Auto", "TopLeft", "Top", "TopRight", "Right", "BottomRight", "Bottom", "BottomLeft", "Left", "Center"}); this.comboBoxPointLabels.Location = new System.Drawing.Point(168, 40); this.comboBoxPointLabels.Name = "comboBoxPointLabels"; this.comboBoxPointLabels.Size = new System.Drawing.Size(104, 22); this.comboBoxPointLabels.TabIndex = 3; this.comboBoxPointLabels.SelectedIndexChanged += new System.EventHandler(this.comboBoxChartType_SelectedIndexChanged); // // label2 // this.label2.Location = new System.Drawing.Point(3, 40); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(160, 23); this.label2.TabIndex = 2; this.label2.Text = "Point &Labels:"; this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight; // // comboBoxChartType // this.comboBoxChartType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBoxChartType.Items.AddRange(new object[] { "Line", "Spline", "StepLine"}); this.comboBoxChartType.Location = new System.Drawing.Point(168, 8); this.comboBoxChartType.Name = "comboBoxChartType"; this.comboBoxChartType.Size = new System.Drawing.Size(104, 22); this.comboBoxChartType.TabIndex = 1; this.comboBoxChartType.SelectedIndexChanged += new System.EventHandler(this.comboBoxChartType_SelectedIndexChanged); // // label1 // this.label1.Location = new System.Drawing.Point(3, 8); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(160, 23); this.label1.TabIndex = 0; this.label1.Text = "Chart &Type:"; this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight; // // label3 // this.label3.Font = new System.Drawing.Font("Verdana", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.label3.Location = new System.Drawing.Point(13, 336); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(702, 40); this.label3.TabIndex = 4; this.label3.Text = "The label style can be set using the LabelStyle custom attribute, and the ShowMar" + "kers custom attribute is used to display data point markers when the chart area " + "is set to 3D."; this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // // LineCurvesChartType // this.BackColor = System.Drawing.Color.White; this.Controls.Add(this.label3); this.Controls.Add(this.panel1); this.Controls.Add(this.labelSampleComment); this.Controls.Add(this.chart1); this.Font = new System.Drawing.Font("Verdana", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.Name = "LineCurvesChartType"; this.Size = new System.Drawing.Size(728, 384); this.Load += new System.EventHandler(this.LineCurvesChartType_Load); ((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit(); this.panel1.ResumeLayout(false); this.ResumeLayout(false); } } public partial class Form1 : Form { public Form1() { InitializeComponent(); FirstMainMenu(); } public void FirstMainMenu() { MenuItem miGraph = new MenuItem("&Graph", new EventHandler(MenuSampleGraphOnClick)); MenuItem miExit = new MenuItem("E&xit", new EventHandler(MenuFileExitOnClick)); MenuItem miSample = new MenuItem("&Sample", new MenuItem[] { miGraph, miExit }); //-------------------------------------------------------- MenuItem miAbout = new MenuItem("&About " + Text, new EventHandler(MenuHelpAboutOnClick)); MenuItem miHelp = new MenuItem("&Help", new MenuItem[] { miAbout }); //------------------------------------------------------------- Menu = new MainMenu(new MenuItem[] { miSample, miHelp }); //------------------------------------------------------------- } void MenuHelpAboutOnClick(object obj, EventArgs ea) { MessageBox.Show(Text + " - Sample graph using MSChart API"); } void MenuFileExitOnClick(object obj, EventArgs ea) { Close(); } //----------------------------------------------------- void MenuSampleGraphOnClick(object obj, EventArgs ea) { SimplerDialogBox dlg = new SimplerDialogBox(); DialogResult dr = dlg.ShowDialog(); Console.WriteLine(dr); } } }
Ngoài ra còn có liên kết đến một phiên bản dành cho các cửa sổ dạng và đầy đủ mẫu ứng dụng – Rad