I'm trying to draw a chart on my website. Here is the js code for the chart with sample inputs
我正在尝试在我的网站上绘制图表。这是带有示例输入的图表的js代码
<script type="text/javascript">
$(function () {
var d2 = [[0, 3], [4, 8], [8, 5], [9, 13]];//these are sample
$.plot("#placeholder", [d2]);
});
</script>
Here is what i wanna do. I want to take some information from the database and put it to the chart. I wrote down the code for taking information. But i couldn't manage how to insert them to the js code for the chart.
这就是我想做的事情。我想从数据库中获取一些信息并将其放入图表中。我写下了获取信息的代码。但我无法管理如何将它们插入到图表的js代码中。
using (var db = new Entities())
{ var q = from u in db.Sites
where u.SiteURL == result.SiteURL
select u;
if (q.Any()) {
List<webpagespeedtest_flat.DataModel.Sites> l = new List<webpagespeedtest_flat.DataModel.Sites>();
l = q.OrderByDescending(s => s.CheckDate).Take(10).ToList();
webpagespeedtest_flat.DataModel.Sites[] z = l.ToArray();
int x = z.Count();
for( int i=0; i < z.Count(); i++){
z[i].Score; // --> this will be the first attribute(x)
z[i].CheckDate;// -->this will be the second attribute(y)
}
2 个解决方案
#1
0
Assuming you're using MVC, there are two ways you could do this. One is to use razor to emit the correct javascript that you need when the page is rendered. The other (neater) way is to make a call that returns JSON which is then used to populate the chart.
假设您正在使用MVC,有两种方法可以做到这一点。一种是使用razor发出页面渲染时所需的正确javascript。另一种(整洁的)方式是进行一次返回JSON的调用,然后用于填充图表。
#2
0
You can use from c# like this.
您可以像这样使用c#。
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(@"<script type='text/javascript'>");
sb.Append(@"$(function () {");
sb.Append(@"var d2 = [[0, 3], [4, 8], [8, 5], [9, 13]];");
sb.Append(@"$.plot(\""#placeholder\"", [d2]);");
sb.Append(@"});");
sb.Append(@"</script>");
ScriptManager.RegisterStartupScript(this, this.GetType(), "ChangeChart", sb.ToString(), false);
Sorry if i can not help you.
对不起,如果我帮不了你。
#1
0
Assuming you're using MVC, there are two ways you could do this. One is to use razor to emit the correct javascript that you need when the page is rendered. The other (neater) way is to make a call that returns JSON which is then used to populate the chart.
假设您正在使用MVC,有两种方法可以做到这一点。一种是使用razor发出页面渲染时所需的正确javascript。另一种(整洁的)方式是进行一次返回JSON的调用,然后用于填充图表。
#2
0
You can use from c# like this.
您可以像这样使用c#。
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(@"<script type='text/javascript'>");
sb.Append(@"$(function () {");
sb.Append(@"var d2 = [[0, 3], [4, 8], [8, 5], [9, 13]];");
sb.Append(@"$.plot(\""#placeholder\"", [d2]);");
sb.Append(@"});");
sb.Append(@"</script>");
ScriptManager.RegisterStartupScript(this, this.GetType(), "ChangeChart", sb.ToString(), false);
Sorry if i can not help you.
对不起,如果我帮不了你。