C # WinForm dynamically add MSChart controls

Source: Internet
Author: User


Add Mschart.dll dynamic link library Add Reference System.Windows.Forms.DataVisualization

MSChart control as a convenient user data display control, you can easily use the control to provide the shape and presentation of data, early Web applications used more, these days have been doing a WinForm based on the CS structure of the demonstration program, using the MSChart, Since has been not too familiar with MSChart, but also dynamic custom add, so 1.1 point of groping to do, dynamically add custom MSChart to WinForm program, on the code:

1. Create a chart in a curved form

System.Windows.Forms.DataVisualization.Charting.Chart Chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart ();

Define a chart
System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea ();

Define a drawing area
System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series ();

Define a data column
Chartarea1.name = "CHARTAREA1";

In fact, there is no need, you can use Chart1. Chartareas[0] It's OK.
Chart1. Chartareas.add (CHARTAREA1);

Completing the Association of chart and ChartArea
System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend ();
Legend1. Name = "icon";
Chart1. Legends.add (LEGEND1);
Chart1. Name = "Chart1";
Series1. ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;

Set linear
Random rd = new random ();
double[] num = new DOUBLE[20];

for (int i = 0; i <; i++)
{
int valuey = Rd. Next (20, 100);
Datapoint point = new Datapoint ((i + 1), valuey);
Series1. Points.Add (point);
}

The coordinates of the resulting point
Chart1. Titles[0]. Text = "";

Series1. ChartArea = "CHARTAREA1";
ChartArea1.AxisX.Title = "date";
ChartArea1.AxisY.Title = "value";
ChartArea1.AxisX.Interval = 1;
ChartArea1.AxisY.Interval = 5;
ChartArea1.AxisY.Minimum = 20;
Series1. Legend = "icon";
Series1. Name = "Series1";
 chart1. Text = "Test";
 chart1. Size = new System.Drawing.Size (700, 500);
 //chart1. Location = new System.Drawing.Point (50,120);
 series1. Color = Color.Blue;
 chart1. Text = "Ceshi";
 //chart1. Titles[0]. Text = "FFF";
 chart1. Series.add (SERIES1);
 //This sentence is important, missing the drawing area will show blank
 //chart1. AllowDrop = true;
 chart1. BackColor = Color.FromArgb (243, 223, 193);

Set chart background color
Chartarea1.backcolor = Color.FromArgb (243, 223, 193);

Setting the C drawing area background color
Series1. BorderWidth = 2;
Series1. Isvalueshownaslabel = true;

Whether to display the value of Y

THIS.GROUPBOX9.CONTROLS.ADD (Chart1);
THIS.PANEL21.CONTROLS.ADD (Chart1);
Chart1. Visible = true;
This.label10.Visible = true;
This.label10.Text = "" "+ TN. Name + "" figure ";
Chart1. Titles.add ("" "+ TN. Name + "" figure ");
Add a title to Chart1
ChartArea1.AxisX.IsMarginVisible = true;

Whether to display white space at both ends of the x-axis

2. Add a line based on the chart above

Control[] controls = This.groupBox9.Controls.Find ("Chart1", true);

//Find existing Chart1
  System.Windows.Forms.DataVisualization.Charting.Chart ch = (System.Windows.Forms.DataVisualization.Charting.Chart ) Controls[0];
 system.windows.forms.datavisualization.charting.series Series2 = New System.Windows.Forms.DataVisualization.Charting.Series ();

//Define a new data item
 random rd=new Random ();
 for (int i = 0; i < Ch. Series[0]. Points.count; i++)
 {
 int valuey=rd. Next (20,100);
 datapoint point = new Datapoint ((i + 1), valuey);
 series2. Points.Add (point);
&NBSP;}
 series2. Color = Color.FromArgb (rd. Next (255), RD. Next (0, $), RD. Next (0, 255));
 series2. BorderWidth = 2;
 series2. ChartType = ((System.Windows.Forms.DataVisualization.Charting.Chart) this.panel21.controls[0]). Series[0]. ChartType;

Define linear and original line shapes to match
Series2. Isvalueshownaslabel = true;
Ch. Series.add (Series2);

Adding Data columns

3. Reduce a curve

Control[] controls = This.groupBox9.Controls.Find ("Chart1", true);
 system.windows.forms.datavisualization.charting.chart ch = ( System.Windows.Forms.DataVisualization.Charting.Chart) controls[0];
 if (Ch. series.count>1)
 {
 //messagebox.show (This, "Remove a line!") "," information tip ");
 int i = ch. series.count-1;
 while (i>1)
 {
 if (Ch. Series[i]. points.count>0)
 {
 break;
&NBSP;}
 i-=1;
&NBSP;}

Ch. Series[i]. Points.clear ();
This.toolStripStatusLabel2.Text = "Reduce the contrast curve to complete!" ";
}
Else
{
MessageBox.Show (This, "the drawing area must have at least one line!") "," information tip ");

}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.