ASP.NET+Sql Server 2008 +highchart控件从数据库中获取数据生成饼状图柱状图

首先下载highchart控件
下载地址:(https://www.highcharts.com.cn/download)
ASP.NET+Sql Server 2008 +highchart控件从数据库中获取数据生成饼状图柱状图_第1张图片
下载解压后复制到网站的根目录下,然后引入控件中的js文件

        
       
    

前台代码完整代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Stafftubiao.aspx.cs" Inherits="Stafftubiao" %>






        
       
    
    
    
     
    


    

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.Configuration;

public partial class Stafftubiao : System.Web.UI.Page
{
    public string returnzhuzhangValue = "";
    public string returnbingValue = "";
    public string returnbingxueliValue = "";
    public string xAxisCategories = "";
    public string xAxisCategories1 = "";
    public string containerHeight = "400px";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Loadzhuzhang(); 
            Loadbingtu();
            Loadbingtu1();
        }
    }

    protected void Loadzhuzhang()
    {
        SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
        sqlcon.Open();
       
        string strsqls = "select top 5 LEFT(date,7) as month,SUM(CONVERT(INT,[yugujine])) as 销售额 from yejibiao group by LEFT(date,7)  order by LEFT(date,7) desc";
        SqlDataAdapter adsa = new SqlDataAdapter(strsqls, sqlcon);
        DataSet adds = new DataSet();
        adsa.Fill(adds);
        DataTable dt = adds.Tables[0];
        if (dt.Rows.Count > 0)
        {
            if (dt.Rows.Count > 4)
            {
                containerHeight = (dt.Rows.Count * 50).ToString() + "px";
            }
            else
            {
                containerHeight = "200px";
            }
            string dataY = "[{name: '销售业绩',data: [";
            string dataX = "[";
            foreach (DataRow dr in dt.Rows)
            {
                dataX += "'" + dr["month"].ToString() + "',";
                dataY += dr["销售额"].ToString() + ",";
            }
            xAxisCategories = dataX.Substring(0, dataX.Length - 1) + "]";
            returnzhuzhangValue = dataY.Substring(0, dataY.Length - 1) + "]}]";
        }
    }
    protected void Loadbingtu()
    {
        SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
        sqlcon.Open();
       
        string strsqls = "SELECT emp_bumen, COUNT(emp_bumen) AS 人数 FROM staff where emp_bumen is not null GROUP BY emp_bumen";
        SqlDataAdapter adsa = new SqlDataAdapter(strsqls, sqlcon);
        DataSet adds = new DataSet();
        adsa.Fill(adds);
        DataTable dt = adds.Tables[0];
        if (dt.Rows.Count > 0)
        {
            if (dt.Rows.Count > 4)
            {
                containerHeight = (dt.Rows.Count * 50).ToString() + "px";
            }
            else
            {
                containerHeight = "200px";
            }
            string dataY = "[{name: '人员分布情况',data: [";
            string dataX = "[";
            foreach (DataRow dr in dt.Rows)
            {
                dataX += "'" + dr["emp_bumen"].ToString() + "',";
                //   dataX += "'妖怪',";
                //   dataY += dr["人数"].ToString() + ",";
                dataY += "['" + dr["emp_bumen"].ToString() + "'," + dr["人数"].ToString() + "],";
            }
            xAxisCategories1 = dataX.Substring(0, dataX.Length - 1) + "]";
            returnbingValue = dataY.Substring(0, dataY.Length - 1) + "]}]";

        }
    }

    protected void Loadbingtu1() //学历分布
    {
        SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
        sqlcon.Open();

        string strsqls = "SELECT emp_xueli, COUNT(emp_xueli) AS 人数 FROM staff where emp_xueli is not null GROUP BY emp_xueli";
        SqlDataAdapter adsa = new SqlDataAdapter(strsqls, sqlcon);
        DataSet adds = new DataSet();
        adsa.Fill(adds);
        DataTable dt = adds.Tables[0];
        if (dt.Rows.Count > 0)
        {
            if (dt.Rows.Count > 4)
            {
                containerHeight = (dt.Rows.Count * 50).ToString() + "px";
            }
            else
            {
                containerHeight = "200px";
            }
            string dataY = "[{name: '学历分布情况',data: [";
            string dataX = "[";
            foreach (DataRow dr in dt.Rows)
            {
                dataX += "'" + dr["emp_xueli"].ToString() + "',";
                //   dataX += "'妖怪',";
                //   dataY += dr["人数"].ToString() + ",";
                dataY += "['" + dr["emp_xueli"].ToString() + "'," + dr["人数"].ToString() + "],";
            }
            xAxisCategories1 = dataX.Substring(0, dataX.Length - 1) + "]";
            returnbingxueliValue = dataY.Substring(0, dataY.Length - 1) + "]}]";

        }
    }
}

最终效果图
ASP.NET+Sql Server 2008 +highchart控件从数据库中获取数据生成饼状图柱状图_第2张图片

你可能感兴趣的:(ASP.NET+Sql Server 2008 +highchart控件从数据库中获取数据生成饼状图柱状图)