c# web Column chart(包含美观的样式)

来源:互联网 发布:淘宝提前收款不能用了 编辑:程序博客网 时间:2024/05/29 17:21

1)前台代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication51.Default" %><%@ Register Assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"    Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title></head><body>    <form id="form1" runat="server">    <div>        <asp:Chart ID="Chart1" runat="server" Palette="BrightPastel" BackColor="#D3DFF0"            Width="412px" Height="296px" BorderlineDashStyle="Solid" BackGradientStyle="TopBottom"            BorderWidth="2" BorderColor="181, 64, 1">            <Titles>                <asp:Title ShadowColor="32, 0, 0, 0" Font="Trebuchet MS, 14.25pt, style=Bold" ShadowOffset="3"                    Text="Money of student" Name="Title1" ForeColor="26, 59, 105">                </asp:Title>            </Titles>            <Legends>                <asp:Legend TitleFont="Microsoft Sans Serif, 8pt, style=Bold" BackColor="Transparent"                    Font="Trebuchet MS, 8.25pt, style=Bold" IsTextAutoFit="False" Enabled="False"                    Name="Default">                </asp:Legend>            </Legends>            <BorderSkin SkinStyle="Emboss"></BorderSkin>            <Series>                <asp:Series Name="Series1" BorderColor="180, 26, 59, 105" ChartType="Column" IsValueShownAsLabel="true">                </asp:Series>            </Series>            <ChartAreas>                <asp:ChartArea Name="ChartArea1" BorderColor="64, 64, 64, 64" BackSecondaryColor="White"                    BackColor="Transparent" ShadowColor="Transparent" BackGradientStyle="TopBottom">                    <Area3DStyle Rotation="10" Perspective="10" Inclination="15" IsRightAngleAxes="False"                        WallWidth="0" IsClustered="False" />                    <AxisY LineColor="64, 64, 64, 64" LabelAutoFitMaxFontSize="8" TitleFont="Trebuchet MS, 10.25pt">                        <LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" />                        <MajorGrid LineColor="64, 64, 64, 64" />                    </AxisY>                    <AxisX LineColor="64, 64, 64, 64" LabelAutoFitMaxFontSize="8" TitleFont="Trebuchet MS, 10.25pt">                        <LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" IsEndLabelVisible="False" Format="MM-dd" />                        <MajorGrid LineColor="64, 64, 64, 64" />                    </AxisX>                </asp:ChartArea>            </ChartAreas>        </asp:Chart>    </div>    </form></body></html>

2)后台代码

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.DataVisualization.Charting;namespace WebApplication51{    public partial class Default : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            if (!IsPostBack)            {                BindStudent();            }        }        /// <summary>        /// 后台绑定        /// </summary>        private void BindStudent()        {            List<Student> items = GetStudents();            Series series = Chart1.Series[0];            foreach (Student item in items)            {                DataPoint dataPoint = new DataPoint();                dataPoint.SetValueXY(item.Name, item.Money);                series.Points.Add(dataPoint);            }            Chart1.ChartAreas[0].AxisX.Title = "Student Name";            Chart1.ChartAreas[0].AxisY.Title = "Money($)";        }        private List<Student> GetStudents()        {            List<Student> items = new List<Student>();            items.Add(new Student("Chad", 1));            items.Add(new Student("Catherine", 4));            items.Add(new Student("Amamda", 3));            items.Add(new Student("Emily", 2));            return items;        }    }    public class Student    {        public Student() { }        public Student(string _name, decimal _money)        {            this.Name = _name;            this.Money = _money;        }        public string Name { set; get; }        public decimal Money { set; get; }    }}

3)错误解决方法

错误:Error executing child request for ChartImg.axd.

解决方法:在web.config 的<system.web>节点小添加如下代码

<httpHandlers>
      <add path="ChartImg.axd" verb="GET,HEAD,POST" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
       validate="false" />
    </httpHandlers>



原创粉丝点击