Highcharts 如何使用 JavaScript 变量作为系列数据源?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5751552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Highcharts how to use JavaScript variable as series data source?
提问by Reality Extractor
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#
我正在运行一个 asp.net 代码隐藏,它创建一个字符串变量,该变量保存由逗号分隔的浮点数集合。在 C# 中是这样的
string myCString = "4.5, 3.1, 6.5, 7.0, -1.3";
This variable then makes it into the an asp.net web page where it is assigned to a JavaScript variable:
这个变量然后使它进入一个 asp.net 网页,在那里它被分配给一个 JavaScript 变量:
var myJString = '<%=myCString %>';
Now we enter Highcharts where the syntax for a normal series looks like this:
现在我们进入 Highcharts,其中普通系列的语法如下所示:
series: [{
name: 'Series1',
data: [1.1, 3.8, 5.3, 9.8, 5.0]
}]
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?
我想做的是将 myJString 分配给系列的数据字段。我尝试了两种方法,但都不起作用。解决方案可能是微不足道的,但我什至不是一个狂热的程序员。看起来问题的核心是 Highcharts 需要一个数字数组而不是一个字符串。但是,我对 JavaScript 中方括号的理解是,它们将其中的任何内容转换为字符串?
Here is what did NOTwork:
这是无效的:
series: [{
name: 'Series1',
data: myJString // does not work
}]
series: [{
name: 'Series1',
data: myJString - 0 // does not work either
}]
The second try was from highcharts - variable data causes browser lockupin 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.
第二次尝试来自highcharts -回想起来,可变数据导致浏览器锁定,这是有道理的,因为从不仅仅是数字的字符串中减去 0 达不到目标。
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):
第一次尝试没有成功也是有道理的,因为它似乎想要一个数字数组而不是一个字符串。现在我的实际问题:
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.
我可以在 JavaScript 中廉价地转换我的逗号分隔的浮点数字符串,以便我可以在数据字段中使用它,如果是这样,我该怎么做?在代码隐藏中执行此操作并将数组传递给 JavaScript 然后使用数组尝试整个过程会更好吗(性能方面)?这是假设 myJString 不是浮点数组是实际问题。
Thanks in advance for any insight you may be able to provide.
预先感谢您提供的任何见解。
回答by Justin Ethier
Try this:
试试这个:
data: JSON.parse("[" + myJString + "]")
Basically this formats the string's contents as a JSON array. The parse
function then de-serializes the string into a JavaScript array.
基本上,这将字符串的内容格式化为 JSON 数组。parse
然后该函数将字符串反序列化为 JavaScript 数组。
For it to work, you will need to include json2.js.
为了让它工作,你需要包含json2.js。
回答by Rupam Datta
Since data is a array type, you can use arrays for the values like
由于数据是数组类型,您可以使用数组作为值,如
myData = [4.5, 3.1, 6.5, 7.0, -1.3];
回答by Reality Extractor
another easy approach is using the JavaScript String.split() function, thus.
另一个简单的方法是使用 JavaScript String.split() 函数,因此。
series: [{ name: 'Series1', data: myJString.split(', ') }]
系列:[{ 名称:'Series1',数据:myJString.split(', ') }]
If you're not sure whether you'll have a space after the comma or not, use RegEx.
如果您不确定逗号后是否有空格,请使用 RegEx。
series: [{ name: 'Series1', data: myJString.split(/,[\d]*/) }]
系列:[{名称:'Series1',数据:myJString.split(/,[\d]*/)}]