在 .AddJavaScriptFunction(...) 中调用 C# 方法
Call C# method in .AddJavaScriptFunction(...)
我想在我的 ASP.NET MVC 项目中使用 Highcharts,正是这个图表:
http://www.highcharts.com/demo/dynamic-update
如何在 .AddJavaScriptFunction(...) 中调用 C# 方法,可能吗?问题是我代码中的第 24 行。
我的控制器:
public ActionResult SplineUpdateEachSecond()
{
List<object> points = new List<object>(20);
DateTime now = DateTime.Now;
Random rand = new Random();
for (int i = -19; i <= 0; i++)
points.Add(new { X = now.AddSeconds(i), Y = rand.NextDouble() });
Highcharts chart = new Highcharts("chart")
.SetOptions(new GlobalOptions { Global = new Global { UseUTC = true } })
.InitChart(new Chart
{
DefaultSeriesType = ChartTypes.Spline,
MarginRight = 10,
Events = new ChartEvents
{
Load = "ChartEventsLoad"
}
})
.AddJavascripFunction("ChartEventsLoad(var t = test())",
@"// set up the updating of the chart each second
var series = this.series[0];
setInterval(function() {
var x = (new Date()).getTime(), // current time
y = <%:test()%>; //HERE i want call my 'test' method, which is lower in the code
series.addPoint([x, y], true, true);
}, 1000);")
.SetTitle(new Title { Text = "Live random data" })
.SetXAxis(new XAxis
{
Type = AxisTypes.Datetime,
TickPixelInterval = 150
})
.SetYAxis(new YAxis
{
Title = new YAxisTitle { Text = "Value" },
PlotLines = new[]
{
new YAxisPlotLines
{
Value = 0,
Width = 1,
Color = ColorTranslator.FromHtml("#808080")
}
}
})
.SetTooltip(new Tooltip { Formatter = "TooltipFormatter" })
.AddJavascripFunction("TooltipFormatter",
@"return '<b>'+ this.series.name +'</b><br/>'+
Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br/>'+
Highcharts.numberFormat(this.y, 2);")
.SetLegend(new Legend { Enabled = false })
.SetExporting(new Exporting { Enabled = false })
.SetSeries(new Series
{
Name = "Random data",
Data = new Data(points.ToArray())
});
return View(chart);
}
public string test() //HERE is my C# function, which i want call to above
{
WebClient webClient = new WebClient();
webClient.DownloadFile("http://examplelink/example.dat", @"example.dat"); //This file is constantly changing
string exampleString = System.IO.File.ReadAllText(@"example.dat");
string[] words = exampleString.Split('_');
return words[3];
}
我的 C# 方法在同一个 .cs 文件中。这是来自 Highcharts 的标准示例项目,我只是添加方法 test() 并尝试在 .AddJavaScriptFunction 中调用它。
.AddJavascripFunction("ChartEventsLoad(var t = test())",
@"// set up the updating of the chart each second
var series = this.series[0];
setInterval(function() {
var x = (new Date()).getTime(),
y = " + test(); + ";series.addPoint([x, y], true, true);}, 1000);")
我想在我的 ASP.NET MVC 项目中使用 Highcharts,正是这个图表:
http://www.highcharts.com/demo/dynamic-update
如何在 .AddJavaScriptFunction(...) 中调用 C# 方法,可能吗?问题是我代码中的第 24 行。
我的控制器:
public ActionResult SplineUpdateEachSecond()
{
List<object> points = new List<object>(20);
DateTime now = DateTime.Now;
Random rand = new Random();
for (int i = -19; i <= 0; i++)
points.Add(new { X = now.AddSeconds(i), Y = rand.NextDouble() });
Highcharts chart = new Highcharts("chart")
.SetOptions(new GlobalOptions { Global = new Global { UseUTC = true } })
.InitChart(new Chart
{
DefaultSeriesType = ChartTypes.Spline,
MarginRight = 10,
Events = new ChartEvents
{
Load = "ChartEventsLoad"
}
})
.AddJavascripFunction("ChartEventsLoad(var t = test())",
@"// set up the updating of the chart each second
var series = this.series[0];
setInterval(function() {
var x = (new Date()).getTime(), // current time
y = <%:test()%>; //HERE i want call my 'test' method, which is lower in the code
series.addPoint([x, y], true, true);
}, 1000);")
.SetTitle(new Title { Text = "Live random data" })
.SetXAxis(new XAxis
{
Type = AxisTypes.Datetime,
TickPixelInterval = 150
})
.SetYAxis(new YAxis
{
Title = new YAxisTitle { Text = "Value" },
PlotLines = new[]
{
new YAxisPlotLines
{
Value = 0,
Width = 1,
Color = ColorTranslator.FromHtml("#808080")
}
}
})
.SetTooltip(new Tooltip { Formatter = "TooltipFormatter" })
.AddJavascripFunction("TooltipFormatter",
@"return '<b>'+ this.series.name +'</b><br/>'+
Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br/>'+
Highcharts.numberFormat(this.y, 2);")
.SetLegend(new Legend { Enabled = false })
.SetExporting(new Exporting { Enabled = false })
.SetSeries(new Series
{
Name = "Random data",
Data = new Data(points.ToArray())
});
return View(chart);
}
public string test() //HERE is my C# function, which i want call to above
{
WebClient webClient = new WebClient();
webClient.DownloadFile("http://examplelink/example.dat", @"example.dat"); //This file is constantly changing
string exampleString = System.IO.File.ReadAllText(@"example.dat");
string[] words = exampleString.Split('_');
return words[3];
}
我的 C# 方法在同一个 .cs 文件中。这是来自 Highcharts 的标准示例项目,我只是添加方法 test() 并尝试在 .AddJavaScriptFunction 中调用它。
.AddJavascripFunction("ChartEventsLoad(var t = test())",
@"// set up the updating of the chart each second
var series = this.series[0];
setInterval(function() {
var x = (new Date()).getTime(),
y = " + test(); + ";series.addPoint([x, y], true, true);}, 1000);")