ECharts是百度商業前端數據可視化團隊的一款開源圖表控件,下面介紹結合SQL Server 2008進行數據綁定顯示圖表。
方法/步驟
打開百度echarts首頁,點擊下載最新的開發包,下載下來的文件如下圖所示:
打開SQL Server 2008新建數據庫表 T_EchartData1, 表字段和表數據如下圖所示:
新建數據庫表 T_EchartData2 ,表字段和表數據如下圖所示:
打開VS2010或VS2012,新建Web頁面 EchartDemo.aspx及EchartDemo.aspx.cs ,核心代碼如下:
var cityClick = '';
function GetUnitEnergy(unitName) {
GetCityEnergy(unitName, 2);
}
require.config({
paths: {
echarts: '../Themes/Scripts/echarts'
}
});
require(
[
'echarts',
'echarts/chart/bar',
'echarts/chart/line',
'echarts/chart/pie',
'echarts/chart/funnel'
],
DrawEChart //異步加載的回調函數繪製圖表
);
var myEnergyChart;
var myReportChart;
//創建ECharts圖表方法
function DrawEChart(ec) {
//--- 折柱 ---
myEnergyChart = ec.init(document.getElementById('divEnergy'), macarons);
//定義圖表options
var options = {
title: {
text: "",
subtext: ""
},
tooltip: {
trigger: 'axis'
},
legend: {
data: [" "]
},
toolbox: {
show: true,
feature: {
mark: { show: false },
dataView: { show: false, readOnly: false },
magicType: { show: true, type: ['line', 'bar'] },
restore: { show: true },
saveAsImage: { show: false }
}
},
grid: {
x: 60,
y: 30,
x2: 5,
y2: 30
},
calculable: true,
xAxis: [
{
type: 'category',
data: [" "]
}
],
yAxis: [
{
type: 'value',
axisLabel: {
fomatter: "."
},
splitArea: { show: true }
}
],
series: [{
name: " ",
type: "line",
data: [0]
}]
};
//選擇一個空圖表
myEnergyChart.setOption(options);
myReportChart = ec.init(document.getElementById('divReport'), macarons);
//選擇一個空圖表
myReportChart.setOption(options);
// 默認加載填報
GetReport("1");
// 默認加載省直
GetUnitEnergy('34');
}
function GetCityEnergy(cityCode, level) {
//獲得圖表的options對象
var options = myEnergyChart.getOption();
//通過Ajax獲取數據
$.ajax({
type: "post",
async: false, //同步執行
url: "GetChartData.aspx?type=energyData",
dataType: "json", //返回數據形式為json
success: function (result) {
if (result) {
options.xAxis[0].data = result.category;
options.series = result.series;
options.legend.data = result.legend;
myEnergyChart.setOption(options);
myEnergyChart.refresh();
}
},
error: function (errorMsg) {
alert("圖表數據加載失敗!");
}
});
}
///點擊按鈕獲取圖表數據採用ajax方式
function GetReport(code) {
//獲得圖表的options對象
var options = myReportChart.getOption();
//通過Ajax獲取數據
$.ajax({
type: "post",
async: false, //同步執行
url: "GetChartData.aspx?type=reportData",
dataType: "json", //返回數據形式為json
success: function (result) {
if (result) {
options.xAxis[0].data = result.category;
options.series = result.series;
options.legend.data = result.legend;
myReportChart.setOption(options);
}
},
error: function (errorMsg) {
alert("圖表數據加載失敗!");
}
});
}
//初始加載圖表數據
$(function () {
GetCityEnergy("", "");
GetUnitEnergy("");
});
新建Web頁面 GetChartData.aspx 及 GetChartData.aspx.cs ,用於從數據庫獲取數據,代碼如下:
protected void Page_Load(object sender, EventArgs e)
{
string type = Request["type"];
if (!string.IsNullOrEmpty(type))
{
switch (type)
{
case "energyData":
GetEnergyDataAjaxData("", "");
break;
case "reportData":
GetReportDataAjaxData("");
break;
}
}
}
private void GetEnergyDataAjaxData(string level, string code)
{
List
List
List
DataTable dt = null;
using (SqlConnection con = new SqlConnection("Data Source=.;User ID=sa;Password=111111;Database=XXXXX;"))
{
string strSQL = "select top 12 * from T_EchartData1";
using (SqlDataAdapter adapter = new SqlDataAdapter(strSQL, con))
{
DataSet ds = new DataSet();
adapter.Fill(ds);
dt = ds.Tables[0];
}
}
legendList.Add("今年能耗");
legendList.Add("去年能耗");
Series thisSeriesObj = new Series();
thisSeriesObj.id = 0;
thisSeriesObj.name = "今年能耗";
thisSeriesObj.type = "line"; //線性圖呈現
thisSeriesObj.data = new List