Draw curves from 0 self-taught c#08--chart control

Source: Internet
Author: User

Use of the chart control

This article describes how to use the chart control in the toolbox to draw multiple curves. As follows:

1.InitializeChart

Add the chart control to the form and then empty the Chartareas, Legends, and series collections in the properties, which are dynamically implemented by the following code. In the form constructor, implement the InitializeChart and Drawseries methods.

The InitializeChart code is as follows.

Public partial class Mainformbert:form {public Mainformbert () {InitializeComponent ();            InitializeChart ();        Drawseries (); The public void InitializeChart () {#region Sets the chart's properties//Chart's background color Chart1.            BackColor = Color.FromArgb (211, 223, 240); Chart background color gradient mode Chart1.            Backgradientstyle = Gradientstyle.none; The border color of the chart, Chart1.            Borderlinecolor = Color.FromArgb (26, 59, 105); The border line style of the chart is Chart1.            Borderlinedashstyle = Chartdashstyle.solid; The width of the chart border line Chart1.            Borderlinewidth = 2; The skin of the chart border is Chart1.            Borderskin.skinstyle = Borderskinstyle.none;            #endregion #region Set the title title of the chart title = new title (); Title content is title.            Text = "BER"; Title of the font title.          Font = new System.Drawing.Font ("Microsoft Sans Serif", fontstyle.regular);  Title Font Color//title.            ForeColor = Color.FromArgb (26, 59, 105); Title Shadow color//title.            Shadowcolor = Color.FromArgb (32, 0, 0, 0); Title Shadow offset//title.            Shadowoffset = 3; Chart1.            Titles.add (title);            #endregion #region Set the chart area properties//Chart Area name ChartArea ChartArea = new ChartArea ("Default");            Background color Chartarea.backcolor = color.white;//Color.FromArgb (64, 165, 191, 228);            Background gradient mode chartarea.backgradientstyle = Gradientstyle.none;            Secondary background color for gradients and shadows chartarea.backsecondarycolor = Color.White;            Border color chartarea.bordercolor = Color.Blue;            Border line width chartarea.borderwidth = 2;            Border line Style Chartarea.borderdashstyle = Chartdashstyle.solid;            Shadow color//chartarea.shadowcolor = color.transparent; Sets the color and width of the x-axis and y-axis bars ChartArea.AxisX.LineColor = Color.FromArgb (64, 64, 64, 64);            ChartArea.AxisX.LineWidth = 1;            ChartArea.AxisY.LineColor = Color.FromArgb (64, 64, 64, 64);            ChartArea.AxisY.LineWidth = 1;            Set the x-axis and y-axis title//chartarea.axisx.title = "Time";            ChartArea.AxisY.Title = "Count";            ChartArea.AxisX.TitleFont = new System.Drawing.Font ("Microsoft Sans Serif", ten, Fontstyle.regular);            ChartArea.AxisY.TitleFont = new System.Drawing.Font ("Microsoft Sans Serif", ten, Fontstyle.regular);            Sets the color and width of the horizontal vertical line of the chart area grid ChartArea.AxisX.MajorGrid.LineColor = Color.FromArgb (64, 64, 64, 64);            ChartArea.AxisX.MajorGrid.LineWidth = 1;            ChartArea.AxisY.MajorGrid.LineColor = Color.FromArgb (64, 64, 64, 64);                      ChartArea.AxisY.MajorGrid.LineWidth = 1; Chart1.            Chartareas.add (ChartArea);            #endregion #region The location of the legend and the legend Legend Legend = new Legend (); Legend. Alignment = stringalignment.Center; Legend.            Docking = Docking.bottom; Legend.            BackColor = color.transparent;            THIS.CHART1.LEGENDS.ADD (legend); #endregion}}

2. Set the curve style

Type, color and width, etc.

Private Series Setseriesstyle (int i) {Series series = new Series (string.            Format ("ch{0}", I + 1)); The type series of the series.            ChartType = Seriescharttype.line; The series's border color series.            BorderColor = Color.FromArgb (180, 26, 59, 105); Line width series.            BorderWidth = 3; Line Shadow color//series.            Shadowcolor = Color.Black; Shadow width//series.            Shadowoffset = 2; Whether the data Description series is displayed.            Isvisibleinlegend = true; Whether there is a data display series on the data points on the line.            Isvalueshownaslabel = false; The data point marker type series on the line.            MarkerStyle = Markerstyle.none; The size of the line data point is//series.            Markersize = 8; Line Color switch (i) {case 0:series.                    Color = Color.FromArgb (220, 65, 140, 240);                Break Case 1:series.       Color = Color.FromArgb (220, 224, 64, 10);             Break Case 2:series.                    Color = Color.FromArgb (220, 120, 150, 20);                Break Case 3:series.                    Color = Color.FromArgb (220, 12, 128, 232);            Break        } return series; }

3. Drawing Curves

Get the data from the DataTable and draw four curves.

Draw curve     private void drawseries ()        {            dt = new testdatatable ();            Dt. CreateTable ();            for (int i = 0; i < 4; i++)            {                Series series = this. Setseriesstyle (i);                Datarow[] FoundRows;                string expression = "Ch =" + i;                foundrows = dt. Select (expression);                foreach (DataRow row in foundrows)                {                    series. POINTS.ADDXY (Row[0], row[2]);                }                THIS.CHART1.SERIES.ADD (Series);            }        }

4. Show or hide curves

Displays and hides the four curves according to the status of the checkbox.

Show hidden curves     private void disorplayseries (int i)        {            if (checkbox[i]. Checked)            {                datarow[] foundrows;                string expression = "Ch =" + i;                foundrows = dt. Select (expression);                foreach (DataRow row in foundrows)                {                    this.chart1.series[i]. POINTS.ADDXY (Row[0], row[2]);                }            }            else            {                this.chart1.series[i]. Points.clear ();            }        }

The above is the content of the chart control from the 0 self-taught c#08--drawing curve, please pay attention to topic.alibabacloud.com (www.php.cn) for more relevant content.

  • 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.