[英]Call C# method in .AddJavaScriptFunction(…)
I want use Highcharts in my ASP.NET MVC project, exactly this chart: 我想在我的ASP.NET MVC项目中使用Highcharts,正是此图表:
http://www.highcharts.com/demo/dynamic-update http://www.highcharts.com/demo/dynamic-update
How can i call C# method in .AddJavaScriptFunction(...), is it possible? 我如何在.AddJavaScriptFunction(...)中调用C#方法,这可能吗? The problem is the 24 line in my code.
问题是我的代码中的24行。
My controller: 我的控制器:
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];
}
My C# method is in the same .cs file. 我的C#方法在同一.cs文件中。 This is standard sample project from Highcharts, i just add method test() and i tried call it in .AddJavaScriptFunction.
这是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);")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.