如何在谷歌图表选项(颜色)中使用js变量

时间:2016-06-14 08:05:31

标签: javascript google-visualization

我正在构建一个Google图表,并在此选项代码中添加:

options: {
            'colors': ['#e0440e', '#e6693e', '#ec8f6e', '#f3b49f', '#f6c7b6'],
         'chartArea':  {width: '60%', left: 45},
         'legend' :'none',
         'title':'some chart title'
     }

以上工作正常,但我需要使用变量作为颜色。所以我想做的是:

var newcolors = "'#e0440e', '#e6693e', '#ec8f6e', '#f3b49f', '#f6c7b6'";

这里有更多代码

options: {
     'colors': [newcolors],
     'chartArea':  {width: '60%', left: 45},
     'legend' :'none',
     'title':'some chart title'
 }

但我在图表上遇到“'不是有效的颜色字符串”错误。我希望这只是一个语法错误,我是愚蠢的。感谢您提前提供任何帮助。

2 个答案:

答案 0 :(得分:2)

定义新颜色时,它是一个长字符串。 当你执行var newcolors = ['#e0440e', '#e6693e', '#ec8f6e', '#f3b49f', '#f6c7b6'];时,它变成一个包含一个元素的数组,这是一个带逗号的长字符串。 这些选项似乎期望的是一个包含多个元素的数组,每个元素都有一种颜色。

所以当你定义新颜色时

colors: newcolorsvar newcolors = "'#e0440e', '#e6693e', '#ec8f6e', '#f3b49f', '#f6c7b6'";

而不是

using Microsoft.Practices.Unity; class Program { static void Main(string[] args) { IUnityContainer container = new UnityContainer(); } } ;

答案 1 :(得分:1)

颜色应该是一个数组

  

How to Customize Charts

var newcolors = ['#e0440e', '#e6693e', '#ec8f6e', '#f3b49f', '#f6c7b6']

options: {
     'colors': newcolors,
     ...
}