首先下载highchart控件
下载地址:(https://www.highcharts.com.cn/download)
下载解压后复制到网站的根目录下,然后引入控件中的js文件
<script src="Highcharts-7.1.1/code/themes/jquery-3.2.1.min.js" type="text/javascript"></script>
<script src="Highcharts-7.1.1/code/highcharts.js" type="text/javascript"></script>
<script src="Highcharts-7.1.1/code/modules/exporting.js" type="text/javascript"></script>
前台代码完整代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Stafftubiao.aspx.cs" Inherits="Stafftubiao" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script src="Highcharts-7.1.1/code/themes/jquery-3.2.1.min.js" type="text/javascript"></script>
<script src="Highcharts-7.1.1/code/highcharts.js" type="text/javascript"></script>
<script src="Highcharts-7.1.1/code/modules/exporting.js" type="text/javascript"></script>
<title></title>
<style>
body {
margin:40px auto;
width:1215px;
display:block
}
</style>
<script type="text/javascript">
var chart; //柱状图
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container1',
defaultSeriesType: 'column'
},
title: {
text: '近5个月销售业绩'
},
subtitle: {
// text: 'Source: 下级人员' //图标的副标题
},
xAxis: {
categories: <%= xAxisCategories %>,//从后台获取数据
tickPixelInterval:0.2,
title: {
text: null
}
},
yAxis: {
min: 0,
title: {
text: '销售额 (元)',
align: 'high'
}
},
tooltip: {
formatter: function() {
return ''+
this.series.name +': '+ this.y +' 元';
}
},
plotOptions: {
column: {
pointPadding: 0.3,
borderWidth: 0
},
bar: {
dataLabels: {
enabled: true
}
}
},
legend: {
layout: 'horizontal',
align: 'center',
verticalAlign: 'top',
y: 50,
borderWidth: 1,
backgroundColor: '#FFFFFF',
borderColor: '#CCC',
shadow: true
},
credits: {
enabled: false
},
series:<%=returnzhuzhangValue %>//此处数据从后台获取
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div style="display:inline-block">
<div id="container" style="display:inline-block; width:400px;height:400px;border:1px solid #ccc"></div>
<script type="text/javascript">
Highcharts.chart('container', {
chart: { //饼图
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie'
},
title: {
text: '各部门人员分布占比'
},
tooltip: {
/* pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'*/
formatter: function() {
return '<b>'+ this.point.name +'</b>: '+ Highcharts.numberFormat(this.percentage, 1) +'% ('+
Highcharts.numberFormat(this.y, 0, ',') +' 人)';
}
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: false,
},
showInLegend: true
}
},
credits: {
enabled: false
},
series:<%=returnbingValue %>//此处数据从后台获取
});
</script>
<div id="container1" style="display:inline-block; width:400px;height:400px;border:1px solid #ccc"></div>
<div id="container2" style="display:inline-block; width:400px;height:400px;border:1px solid #ccc"></div>
<script type="text/javascript">
Highcharts.chart('container2', {
chart: { //饼图
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie'
},
title: {
text: '员工学历分布占比'
},
tooltip: {
/* pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'*/
formatter: function() {
return '<b>'+ this.point.name +'</b>: '+ Highcharts.numberFormat(this.percentage, 1) +'% ('+
Highcharts.numberFormat(this.y, 0, ',') +' 人)';
}
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: false,
},
showInLegend: true
}
},
credits: {
enabled: false
},
series:<%=returnbingxueliValue %>//此处数据从后台获取
});
</script>
</div>
</form>
</body>
</html>
后台代码:
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) + "]}]";
}
}
}
最终效果图