2016-08-30 104 views
1

下面是我的Google图表代码,颜色设置对于数据集部分中的两组数据都是相同的。谷歌图表 - 如何从数据中移动颜色设置

如何从数据集数组中分离样式,所以样式来自一个对象或变量?

var data = { 
labels: [ 
    new CustomLabel("January", "January 11"), 
    new CustomLabel("February", "February 12"), 
    new CustomLabel("March", "March 13"), 
    new CustomLabel("April", "April 14"), 
    new CustomLabel("May", "May 15"), 
    new CustomLabel("June", "June 16"), 
    new CustomLabel("July", "July 17")], 
datasets: [ 
    { 
     label: "My First dataset", 
     fillColor: "rgba(220,220,220,0.2)", 
     strokeColor: "rgba(220,220,220,1)", 
     pointColor: "rgba(220,220,220,1)", 
     pointStrokeColor: "#fff", 
     pointHighlightFill: "#fff", 
     pointHighlightStroke: "rgba(220,220,220,1)", 
     data: [65, 59, 80, 81, 56, 55, 40] 
    }, 
    { 
     label: "My Second dataset", 
     fillColor: "rgba(220,220,220,0.2)", 
     strokeColor: "rgba(220,220,220,1)", 
     pointColor: "rgba(220,220,220,1)", 
     pointStrokeColor: "#fff", 
     pointHighlightFill: "#fff", 
     pointHighlightStroke: "rgba(220,220,220,1)", 
     data: [28, 48, 40, 19, 86, 27, 90] 
    } 
] 

};

回答

1

如果你使用jQuery,您可以使用jQuery.extend()

var style = { 
    fillColor: "rgba(220,220,220,0.2)", 
    strokeColor: "rgba(220,220,220,1)", 
    pointColor: "rgba(220,220,220,1)", 
    pointStrokeColor: "#fff", 
    pointHighlightFill: "#fff", 
    pointHighlightStroke: "rgba(220,220,220,1)" 
}; 

var data = { 
    labels: [ 
     new CustomLabel("January", "January 11"), 
     new CustomLabel("February", "February 12"), 
     new CustomLabel("March", "March 13"), 
     new CustomLabel("April", "April 14"), 
     new CustomLabel("May", "May 15"), 
     new CustomLabel("June", "June 16"), 
     new CustomLabel("July", "July 17")], 
    datasets: [] 
}; 

data.datasets.push($.extend({}, style, { 
    label: "My First dataset", 
    data: [65, 59, 80, 81, 56, 55, 40] 
    }) 
); 

data.datasets.push($.extend({}, style, { 
     label: "My Second dataset", 
     data: [28, 48, 40, 19, 86, 27, 90] 
    }) 
);