Application of C#chart Chart

Source: Internet
Author: User

In the chart, the x-axis represents the category, and the y-axis represents the values (like the class and their attributes)

This is the data in the database, below we choose the first 5 cars, show their names in the chart, fuel consumption, power, price

To create a class for querying data

 class Carda {public List < cardata> select () {List < cardata > list= null; SqlConnection conn = new SqlConnection ("server=.;            Database=mydb;user=sa;pwd=123 "); Conn.            Open (); SqlCommand cmd = conn.            CreateCommand ();            Cmd.commandtext = "Select top 5* from car"; SqlDataReader dr= cmd.            ExecuteReader (); if (Dr.                HasRows) {list = new list<cardata> (); while (Dr.                Read ()) {Cardata data = new Cardata ();                data = new Cardata (); Data. Name = dr["Name"].                ToString (); Data.                Oil= (decimal) dr["oil"; Data. powers=dr["Powers"].                ToString (); Data.                Price= (decimal) dr["Price"; List.                ADD (data);            }} return list; Cmd.            Dispose (); Conn.        Close (); }    }

Class Cardata    {        private string _name;        public string Name        {            get {return _name;}            set {_name = value;}        }        private decimal _oil;        Public decimal Oil        {            get {return _oil;}            set {_oil = value;}        }        private string  _powers;        public string  Powers        {            get {return _powers;}            set {_powers = value;}        }        private decimal _price;        Public decimal price        {            get {return _price;}            set {_price = value;}        }    }

When a form is loaded, the data source is bound to the chart, note that the binding is in a single column, indicating that the x and Y axes of each row correspond to the property name of the data source (that is, the property name of the object in the generic collection). Note: The specified must be a property name and cannot be a field name, which also requires that the data source object's fields must be encapsulated and must have attributes.

This shows the encapsulation features of C # programming, we give the chart tool a data source, let it help us visualize the data, we do not care about how it is implemented, we only tell it needs to display the data, the rest of it to the chart tool.

private void Form1_Load (object sender, EventArgs e)        {            list<cardata> List = new Carda (). Select ();            if (list! = null)            {                Chart1. DataSource = list;                Chart1. series["Series1"]. xvaluemember= "name";                Chart1. series["Series1"]. Yvaluemembers = "oil";            }        }
Run results

In the Series property, you can set the display name for each column and add a new column

private void Form1_Load (object sender, EventArgs e)        {            list<cardata> List = new Carda (). Select ();            if (list! = null)            {                Chart1. DataSource = list;                Chart1. series["fuel consumption"]. xvaluemember= "name";                Chart1. series["fuel consumption"]. Yvaluemembers = "oil";                Chart1. series["Power"]. Xvaluemember = "name";                Chart1. series["Power"]. Yvaluemembers = "Powers";                Chart1. series["Price"]. Xvaluemember = "name";                Chart1. series["Price"]. Yvaluemembers = "Price";            }        }

Application of C#chart Chart

Related Article

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.