本文介绍了由于使用DataMap接收到值,Google图表会针对无效颜色抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于使用DataMap接收到值,Google图表会针对无效颜色抛出错误。

Google chart throws error for invalid color due to values received using DataMap.

您可以在选项中看到颜色:[颜色]。

You can see colors: [colors], in options.

如果我直接使用下面的颜色值,则可以正常工作。

If i direct use color values like below then it works fine.

colors: ['#006400', '#3cb371', 'red', '#f5fffa'],

我通过数据映射获取值,并且它具有相同的输出,然后它不起作用并引发错误:

but if i get values through data map and it has same output then it doesn't work and throws error:

"'#006400' is not a valid color string".

是否存在任何数据格式化问题?

Is there any kind of data formatting issue?

{"cols":[{"label":"status","type":"string"},{"label":"count","type":"string"}],"rows":[{"c":[{"v":"CLOSED"},{"v":3}]},{"c":[{"v":"VERIFIED"},{"v":35}]},{"c":[{"v":"RESOLVED"},{"v":15}]},{"c":[{"v":"IN_PROGRESS"},{"v":92}]},{"c":[{"v":"ASSIGNED"},{"v":63}]},{"c":[{"v":"NEW"},{"v":16}]},{"c":[{"v":""},{"v":0}]}]}



自定义颜色(有问题):



Custom color (problematic):

const DataMap = {
    CLOSED: '#006400',
    VERIFIED: '#006400',
    RESOLVED: '#3cb371',
    REOPENED: 'red',
    IN_PROGRESS: '#f5fffa',
    ASSIGNED: 'brown',
    NEW: 'brown',
    UNCONFIRMED: 'brown'
};

let myColors = [];
Object.keys(DataMap).forEach((key, index) => {
    if (jsonData.search(key) !== -1) {
        myColors.push("'" + DataMap[key] + "'");
    }
});

options = {
    title: chartTitle,
    width: '410',
    height: '320',
    backgroundColor: '#f5fffa',
    is3D: true,
    colors: [myColors],
    chartArea: {
        left: "20%",
        top: "20%",
        height: "100%",
        width: "100%"
    }
    };
    chart.draw(data, options);



直接彩色(效果很好):



Direct color (works fine):

 options = {
    title: chartTitle,
    width: '410',
    height: '320',
    backgroundColor: '#f5fffa',
    is3D: true,
    colors: ['#006400', '#3cb371', 'red', '#f5fffa'],
    chartArea: {
        left: "20%",
        top: "20%",
        height: "100%",
        width: "100%"
        }
     };
    chart.draw(data, options);

问题可能是什么,我该如何解决?

What can be the issue and how can i resolve it?

推荐答案

我现在能说的是:

myColors.push("'" + DataMap[key] + "'");

应更改为

myColors.push(DataMap[key]);

因为 DataMap 中每个键的值都是已经是字符串,即 DataMap [key] 是字符串。

because values for each key from DataMap are already strings, i.e. DataMap[key] is a string.

其次,而不是

colors: [myColors],

应该只是

colors: myColors,

因为 myColors 已经是一个数组。

这篇关于由于使用DataMap接收到值,Google图表会针对无效颜色抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 19:36