EFDbContext类:
public class EFDbContext : DbContext
{
public
DbSet<A> Aa { set; get; }
}
Models类:
public class A
{
[Key]
public int Id { set;
get; }
public string ipAddress { set; get; }
public int
Port { set; get; }
public DateTime Time { set; get; }
public string madAddr { set; get; }
}
Highchasts类:
EFDbContext db = new EFDbContext();
public string
Highchartsdata1()
{
List<A> list =
db.Aa.ToList();
List<Person> listA = new
List<Person>();
foreach (var item in list)
{
Person p = new Person();
p.Name =
item.Id.ToString();//横坐标
p.Score =
item.Port;//折线
listA.Add(p);
}
string xdata = "[";
string ydata
= "[";
for (int i = 0; i < listA.Count; i++)
{
if
(!string.IsNullOrEmpty(listA[i].Name))
{
xdata += listA[i].Name == "" ? "0," : "'" +
listA[i].Name + "'" + ",";//横坐标
ydata += listA[i].Score
== -1 ? "0," : listA[i].Score + ",";//折线
}
}
xdata = xdata.Length > 1 ? xdata.Substring(0, xdata.Length -
1) + "]" : "";//横坐标 集合
ydata = ydata.Length > 1 ?
ydata.Substring(0, ydata.Length - 1) + "]" : "";//折线 集合
if
(xdata == "" && ydata == "")
{
return
"";
}
else
{
return "[" + xdata + "," + ydata + "]";
}
}
index:
@{
ViewBag.Title = "Index";
}
<h2>
Index</h2>
<script
src="~/jquery.min.js"></script>
<script
src="~/highcharts.js"></script>
<script
src="~/JavaScript2.js"></script>
<div id="panel12" title="Twins"
class="paneltitle">
<div style="width: 75.5%; height: 550px;"
id="GraveContainer">
</div>
</div>
<script type="text/javascript">
$(function () {
FaultRankLineChart("", "Twins", "次");
})
//故障严重级别统计
function FaultRankLineChart(sn, tipName, tipUnit) {
var xdata =
"";
var ydata = "";
$.ajax({
type:
"POST",
url: "/Home/FaultRankInit",
async:
false,
dataType: "json",
beforeSend: function () {
$("#chartLoading").show(); },
error: function () {
alert("请求超时,请重新加载该页面!"); },
success: function (data)
{
$("#chartLoading").hide();
if (data !=
"") {
xdata = eval(data)[0];
ydata
= eval(data)[1];
showFaultRankLineChart(xdata, ydata,
tipName, tipUnit);
}
else
{
$("#Container").html("Twins!");
}
}
})
}
function showFaultRankLineChart(xdata, ydata, tipName, tipUnit)
{
$("#GraveContainer").highcharts({
chart:
{
type: 'spline'
},
title:
{
text: "Twins",
x: -20
//center
},
subtitle: {
text:
"Twins分析",
x: -20
},
xAxis:
{
type: "string",
categories:
eval(xdata),
gridLineWidth: 0,
labels:
{
rotation: 0, //旋转度数,显示
align:
'center',
style: {
fontSize:
'13px'
//fontFamily: 'Verdana,
sans-serif'
}
}
},
yAxis: {
title:
{
text: tipName + " (" + tipUnit + ")"
},
gridLineWidth: 0,
allowDecimals:
false,
plotLines: [{
value:
0,
width: 1,
color:
'#808080'
}],
min: 0
},
plotOptions: {
spline:
{
marker: {
radius:
1,
lineColor: '#666666',
lineWidth: 1
}
}
},
credits: {
href: '',
text: ''
},
tooltip: {
formatter: function () {
var s = 'Twins: <b>' +
this.x + '</b><br>';
s += 'love: <b>' +
this.y + ' 次</b><br>';
return
s;
}
},
legend:
{
layout: 'vertical',
align:
'right',
verticalAlign: 'middle',
borderWidth: 0
},
exporting: {
// 是否允许导出
enabled: true,
//
按钮配置
buttons: {
//
导出按钮配置
exportButton: {
menuItems: null,
onclick: function ()
{
this.exportChart();
}
},
//
打印按钮配置
printButton: {
enabled:
false
}
},
//
文件名
filename: "StationAnalysisChart"
//
导出文件默认类型
//type: 'application/pdf'
},
series: [{
name: "次数",
data: eval(ydata)
}]
});
}
</script>
链接数据库:
<connectionStrings>
<add name="EFDbContext"
connectionString="Data Source=192.168.1.101;Initial Catalog=KFKZ;Persist
Security Info=True;User ID=11;Password=1111111"
providerName="System.Data.SqlClient"/>
<add name="DefaultConnection" providerName="System.Data.SqlClient"
connectionString="Data Source=(LocalDb)\v11.0;Initial
Catalog=aspnet-MvcApp-20141017145526;Integrated
Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-MvcApp-20141017145526.mdf"
/>
</connectionStrings>
HomeController控制器:
[HttpPost]
public JsonResult FaultRankInit()
{
Highchasts cm = new Highchasts();
string str =
cm.Highchartsdata1();
return Json(str,
JsonRequestBehavior.AllowGet);
}