<%@ Page Language=C# AutoEventWireup=true CodeFile=chart.aspx.cs Inherits=chart %><%@ Register Assembly=System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 Namespace=System.Web.UI.DataVisualization.Charting TagPrefix=asp %>
Using System; using System. collections. generic; using System. linq; using System. web; using System. data; using System. web. UI; using System. web. UI. webControls; // addusing System. drawing; public partial class chart: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {DataTable dt = CreatData (); GetStyle1 (dt); GetStyle2 (dt); GetStyle3 (dt ); getStyle4 (dt);} protected void GetStyle1 (DataTable dt) {# region line chart Chart1.DataSource = dt; // bind data Chart1.Series [Series1]. chartType = System. web. UI. dataVisualization. charting. seriesChartType. line; // set the chart type Chart1.Series [0]. XValueMember = Country; // X axis data member column Chart1.Series [0]. YValueMembers = Score; // Chart1.ChartAreas [ChartArea1]. axisX. title = country; // the Title of the X axis Chart1.ChartAreas [ChartArea1]. axisX. titleAlignment = StringAlignment. far; // set the position of the title of the Y axis to Chart1.ChartAreas [ChartArea1]. axisY. title = Gold Medal; // X axis Title Chart1.ChartAreas [ChartArea1]. axisY. titleAlignment = StringAlignment. far; // set the position of the title of the Y axis to Chart1.ChartAreas [ChartArea1]. axisX. interval = 1; // The data spacing between the X axis and Chart1.ChartAreas [ChartArea1]. axisX. majorGrid. enabled = false; // The vertical split line Chart1.Series [0] is not displayed. isValueShownAsLabel = true; // display coordinate value # endregion} protected void GetStyle2 (DataTable dt) {# region spline (smoothing curve) Chart2.DataSource = dt; // bind data Chart2.Series [Series1]. chartType = System. web. UI. dataVisualization. charting. seriesChartType. spline; // set the chart type Chart2.Series [Series1]. markerStyle = System. web. UI. dataVisualization. charting. markerStyle. cross; // set the vertex style, Cross Chart2.Series [0]. XValueMember = Country; // X axis data member column Chart2.Series [0]. YValueMembers = Score; // Chart2.ChartAreas [ChartArea1]. axisX. title = country; // the Title of the X axis Chart2.ChartAreas [ChartArea1]. axisX. titleAlignment = StringAlignment. far; // set the position of the title of the Y axis to Chart2.ChartAreas [ChartArea1]. axisY. title = Gold Medal; // Title of the X axis Chart2.ChartAreas [ChartArea1]. axisY. titleAlignment = StringAlignment. far; // set the position of the title of the Y axis to Chart2.ChartAreas [ChartArea1]. axisX. interval = 1; // The data spacing between the X axis and Chart2.ChartAreas [ChartArea1]. axisX. majorGrid. enabled = false; // The vertical split line Chart2.Series [0] is not displayed. isValueShownAsLabel = true; // display coordinate value # endregion} protected void GetStyle3 (DataTable dt) {# region Bar Chart Chart3.DataSource = dt; // bind data Chart3.Series [Series1]. chartType = System. web. UI. dataVisualization. charting. seriesChartType. bar; // set the chart type Chart3.Series [0]. XValueMember = Country; // X axis data member column Chart3.Series [0]. YValueMembers = Score; // Chart3.ChartAreas [ChartArea1]. axisX. title = country; // the Title of the X axis Chart3.ChartAreas [ChartArea1]. axisX. titleAlignment = StringAlignment. far; // set the position of the title of the Y axis to Chart3.ChartAreas [ChartArea1]. axisY. title = Gold Medal; // X axis Title Chart3.ChartAreas [ChartArea1]. axisY. titleAlignment = StringAlignment. far; // set the position of the title of the Y axis to Chart3.ChartAreas [ChartArea1]. axisX. interval = 1; // The data spacing between the X axis and Chart3.ChartAreas [ChartArea1]. axisX. majorGrid. enabled = false; // do not display the vertical split line Chart3.Series [0]. isValueShownAsLabel = true; // display coordinate value # endregion} protected void GetStyle4 (DataTable dt) {# region pie chart Chart4.DataSource = dt; // bind data Chart4.Series [Series1]. chartType = System. web. UI. dataVisualization. charting. seriesChartType. pie; // set the chart type Chart4.Series [0]. XValueMember = Country; // X axis data member column Chart4.Series [0]. YValueMembers = Score; // Chart4.Series [0] In the y-axis data member column. legendText = Country; Chart4.Series [0]. isValueShownAsLabel = true; // display coordinate value # endregion }////// Create a two-dimensional data table //////
Data Table of the Datatable type
Protected DataTable CreatData () {DataTable dt = new DataTable (); dt. columns. add (Country, System. type. getType (System. string); dt. columns. add (Score, System. type. getType (System. string); string [] n = new string [] {China, USA, Russia, UK}; string [] c = new string [] {, 80, 50, 35 }; for (int I = 0; I <4; I ++) {DataRow dr = dt. newRow (); dr [Country] = n [I]; dr [Score] = c [I]; dt. rows. add (dr) ;}return dt ;}}