C#端程序,只查询数据库前五行的数据,可以实现。但是如果改成程序里那种将Response.Write语句放入循环里就不能显示出来了。就是想把Response.write放在循环里,每次查询多加1,用来实现行数越来越多,前端显示也就是相当于是动态的数据了。请问是什么原因呢。把Response.end删掉也不行
9private void GetEnergyDataAjaxData(string level, string code)
{
//考虑到图表的category是字符串数组 这里定义一个string的List
List<string> categoryList = new List<string>();
//考虑到图表的series数据为一个对象数组 这里额外定义一个series的类
List<Series> seriesList = new List<Series>();
//考虑到Echarts图表需要设置legend内的data数组为series的name集合这里需要定义一个legend数组
List<string> legendList = new List<string>();
//想设计一个循环导致一直加下一条数据
int xxxxx = 5;
// CC:
//while (true)
//{
DataTable dt = null;
using (SqlConnection con = new SqlConnection("Data Source=127.0.0.1;UID=diamond;Password=88888888;Database=test_VE2;"))
{
// while (true)
//{
//string strSQL = "select top 12 * from T_EchartData1 where TID not in (select top " + xxxxx.ToString() + " TID from T_EchartData1)";
string strSQL = "select top " + xxxxx.ToString() + " * from T_EchartData1";
using (SqlDataAdapter adapter = new SqlDataAdapter(strSQL, con))
{
DataSet ds = new DataSet();
adapter.Fill(ds);
dt = ds.Tables[0];
}
}
//Response.Write(dt.Rows.Count+"条数据!");
//设置legend数组
legendList.Add("今年能耗"); //这里的名称必须和series的每一组series的name保持一致
legendList.Add("去年能耗"); //这里的名称必须和series的每一组series的name保持一致
Series thisSeriesObj = new Series();
thisSeriesObj.id = 0;
thisSeriesObj.name = "今年能耗";
thisSeriesObj.type = "line"; //线性图呈现
thisSeriesObj.data = new List<object>();
thisSeriesObj.itemStyle = new itemStyle { normal = new normal { color = "#0099FF" } };
Series lastSeriesObj = new Series();
lastSeriesObj.id = 1;
lastSeriesObj.name = "去年能耗";
lastSeriesObj.type = "line"; //线性图呈现
lastSeriesObj.data = new List<object>();
lastSeriesObj.itemStyle = new itemStyle { normal = new normal { color = "#00CC00" } };
foreach (DataRow dr in dt.Rows)
{
categoryList.Add(dr[0].ToString() + "月");
thisSeriesObj.data.Add(dr[1]);
lastSeriesObj.data.Add(dr[2]);
}
//将sereis对象压入sereis数组列表内
seriesList.Add(thisSeriesObj);
seriesList.Add(lastSeriesObj);
//最后调用相关函数将List转换为Json
//因为我们需要返回category和series、legend多个对象 这里我们自己在new一个新的对象来封装这两个对象
var newObj = new
{
category = categoryList,
series = seriesList,
legend = legendList
};
//Response返回新对象的json数据
Response.Write(JsonConvert.SerializeObject(newObj, Formatting.None));
xxxxx++;
//Thread.Sleep(1900);
//Response.End();
//goto CC;
//continue;
//Response.End();
// }
}
网页端代码:用ECHARTS进行动态展示
///点击按钮获取图表数据采用ajax方式
setInterval(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) {
//将返回的category和series对象赋值给options对象内的category和series
//因为xAxis是一个数组 这里需要是xAxis[i]的形式
options.xAxis[0].data = result.category;
options.series = result.series;
options.legend.data = result.legend;
myEnergyChart.setOption(options);
myEnergyChart.refresh();
console.info(result);
}
},
error: function (errorMsg) {
alert("图表数据加载失败!");
}
});
}, 2000);