Highcharts如何使用JavaScript变量作为一系列数据源? [英] Highcharts how to use JavaScript variable as series data source?

查看:326
本文介绍了Highcharts如何使用JavaScript变量作为一系列数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行一个asp.net code-落后创建其中包含用逗号分隔的彩车集合的字符串变量。像这样的事情在C#

I am running an asp.net code-behind that creates a string variable which holds a collection of floats separated by commas. Something like this in C#

string myCString = "4.5, 3.1, 6.5, 7.0, -1.3";

然后,这个变量使得它进入一个asp.net网页,它被分配给一个JavaScript变量:

This variable then makes it into the an asp.net web page where it is assigned to a JavaScript variable:

var myJString = '<%=myCString %>';

现在我们进入Highcharts其中对于普通系列的语法如下:

Now we enter Highcharts where the syntax for a normal series looks like this:

series: [{
     name: 'Series1',
     data: [1.1, 3.8, 5.3, 9.8, 5.0]
       }]

我想要做的就是分配myJString该系列的数据字段。我曾尝试两种方法都和没有工作。该解决方案可能是微不足道的,但是从我竟然被一个热心的程序员很远。它看起来像这个问题的核心是,Highcharts预计数字而不是字符串数组。然而,我的方括号中的JavaScript的理解是,他们无论转换是在为一个字符串?

What I would like to do is assign myJString to the data field of the series. I have tried two approaches and both did not work. The solution is probably trivial but I am far from even being an enthusiast programmer. It looks like the core of the problem is that Highcharts expects an array of numbers rather than a string. However, my understanding of the square brackets in JavaScript was that they convert whatever is within to a string?

下面是做什么的不可以工作

series: [{
     name: 'Series1',
     data: myJString    // does not work
       }]

series: [{
     name: 'Series1',
     data: myJString - 0    // does not work either
       }]

第二次尝试来自<一个href=\"http://stackoverflow.com/questions/3183584/highcharts-variable-data-causes-browser-lockup\">highcharts - 可变数据导致浏览器锁死回想起来这是有道理的,它并没有因为来自一个字符串,不减去0的工作只是一些达不到的目标。

The second try was from highcharts - variable data causes browser lockup in retrospect it makes sense that it didn't work since subtracting 0 from a string that is not just a number falls short of the goal.

这也使得意义上说,第一次尝试没有成功,因为它似乎想数字而不是一个字符串数组。现在我的实际问题(S):

It also makes sense that the first try didn't work since it seems to want an array of numbers rather than a string. Now to my actual question(s):

我可以廉价转换我的逗号分隔字符串在JavaScript中花车这样我就可以在数据字段中使用它,如果是这样,我怎么做呢?它会更好(性能明智),这样做在code-后面传递一个数组JavaScript,然后尝试用一个数组整个事情?这是假设不属myJString彩车的数组的实际问题。

Can I inexpensively convert my string of comma separated floats in JavaScript so I can use it in the data field, and if so, how do I do that? Would it be better (performance wise) to do this in the code-behind and pass an array to JavaScript and then try the whole thing with an array? This is assuming that the myJString not being an array of floats is the actual problem.

先谢谢您的任何见解您可以提供。

Thanks in advance for any insight you may be able to provide.

推荐答案

试试这个:

data: JSON.parse("[" + myJString + "]")

基本上这个格式化字符串的内容作为JSON阵列。在解析函数,那么反序列化字符串转换成JavaScript数组。

Basically this formats the string's contents as a JSON array. The parse function then de-serializes the string into a JavaScript array.

有关它的工作,你将需要包括 json2.js

For it to work, you will need to include json2.js.

这篇关于Highcharts如何使用JavaScript变量作为一系列数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆