<script src="http://echarts.baidu.com/build/dist/echarts.js"></script>
function onLoadRate(list) {
//路径配置
require.config({
paths: {
echarts: 'http://echarts.baidu.com/build/dist'
}
});
// 使用
require(
[
'echarts',
'echarts/chart/line' // 使用折现图就加载line模块,按需加载
],
function (qq) {
// 基于准备好的dom,初始化echarts图表
var myChart = qq.init(document.getElementById('main'));
var option = {
tooltip: {
show: true
},
legend: {
data: getChannelName()
},
xAxis: [
{
type: 'category',
data: getDate() //动态取X轴坐标
}
],
yAxis: [
{
type: 'value',
}
],
series: list //动态加载的数据源
};
myChart.setOption(option);
});
}
function getSuccessRate() {
$.ajax({
url: "/AjaxHandler/LoadSuccessRate.ashx",
type: "POST",
dataType: "text",//返回text
async: true,//同步
success: function (msg) {
result = JSON.parse(msg);
onLoadRate(result);
},
error: function (msg) { }
});
}
public class LoadSuccessRate : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
var date = DateTime.Now;
var list = Luobo.BLL.channel.Channel.GetChannelSuccessRateList(date);
var newList = list.GroupBy(it => new { it.ChannelName }).Select(cit => new SuccessRateResult
{
ChannelName = cit.Key.ChannelName
}).ToList();
var seriesList = new List<SeriesData>();
var dataList = new List<string>();
for (int i = 11; i >= 0; i--)
{
var str = date.AddMinutes(-5 * i).ToString("HH:mm");
dataList.Add(str);
}
foreach (var item in newList)
{
foreach (var i in dataList)
{
var num = list.Count(it => it.ChannelName == item.ChannelName && it.SearchDate == i);
if (num <= 0)
{
list.Add(new SuccessRateResult()
{
ChannelName = item.ChannelName,
SearchDate = i,
SuccessRate = "0"
});
}
}
}
foreach (var item in newList.OrderBy(it=>it.ChannelName).ThenByDescending(it=>it.SearchDate))
{
var rateList = list.Where(it => it.ChannelName == item.ChannelName).OrderBy(it => it.SearchDate).Select(it =>decimal.Round(decimal.Parse(it.SuccessRate), 2));
decimal[] dataValue = rateList.ToArray();
seriesList.Add(new SeriesData()
{
type = "line",
name = item.ChannelName,
data = dataValue
});
}
var json = "[]";
json = JsonConvert.SerializeObject(seriesList);
context.Response.ContentType = "text/plain";
context.Response.Write(json);
}
public bool IsReusable
{
get
{
return false;
}
}
}