Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/479.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何为图表生成随机颜色_Javascript_Jquery_Arrays_Function_Jscharts - Fatal编程技术网

Javascript 如何为图表生成随机颜色

Javascript 如何为图表生成随机颜色,javascript,jquery,arrays,function,jscharts,Javascript,Jquery,Arrays,Function,Jscharts,你的功能看起来不错!要使用它在数组中生成颜色,只需使用循环: $.getJSON("http://...", function (data) { var array = $.map(data, function (data) { return [[data.Name, parseInt(data.Id)]]; }); var colors = ['#FA5E1F', '#FDCB3F', '#71D743', '#D2

你的功能看起来不错!要使用它在数组中生成颜色,只需使用循环:

$.getJSON("http://...", function (data) {

        var array = $.map(data, function (data) {
            return [[data.Name, parseInt(data.Id)]];
        }); 

        var colors = ['#FA5E1F', '#FDCB3F', '#71D743', '#D23333', '#BAE73F', '#B381C9'];
        var myChart = new JSChart('graph', 'bar');
        myChart.setDataArray(array);
        myChart.colorizeBars(colors);
        myChart.setTitle('Title');
        myChart.setTitleColor('#8E8E8E');
        myChart.setAxisNameX('Orgs');
        myChart.setAxisNameY('%');
        myChart.setAxisColor('#c6c6c6');
        myChart.setAxisWidth(1);
        myChart.setAxisNameColor('#9a9a9a');
        myChart.setAxisValuesColor('#939393');
        myChart.setAxisPaddingTop(60);
        myChart.setAxisPaddingLeft(50);
        myChart.setAxisPaddingBottom(60);
        myChart.setTextPaddingBottom(20);
        myChart.setTextPaddingLeft(15);
        myChart.setTitleFontSize(11);
        myChart.setBarBorderWidth(0);
        myChart.setBarSpacingRatio(50);
        myChart.setBarValuesColor('#737373');
        myChart.setGrid(false);
        myChart.setSize(616, 321);
        //myChart.setBackgroundImage('chart_bg.jpg');
        myChart.draw();
    });
与:


您可以使用位运算符生成随机十六进制颜色:

var myChart = new JSChart('graph', 'bar');
myChart.setDataArray(array);
myChart.colorizeBars(getColorArray(array.length));
现在,您可以创建一个循环并生成颜色,并将其推送到
颜色
数组中:

// Example data Array.
var data = [ 1, 2, 4, 5, 6];

// The amount of random colors you want to generate.
var amount = data.length;
getRandomColor()中发生了什么事

“最高”十六进制值为
ffffff
,“最低”十六进制值为
000000
,将其转换为十进制数时,您会得到:

// Will contain the random hex colors.
var colors = [];

// Generate colors.
while(amount--) {
    colors.push(getRandomColor());
}

事实证明,
2^24
等于
16777216
,这可以用,也就是说,
1来计算,你有没有尝试过生成颜色的函数?想想你所知道的。您需要生成6个十六进制值或3个小于256的整数。我很想帮你,但你似乎没有亲自解决。好的,更新了我的问题有两个问题,所以我写的函数1)我必须通过计数2)我没有返回数组Tanks,
getColorArray
没有返回数组集,它只返回最后一个,当我在cosole.debug中调试时,我得到了最后一种没有数组的颜色colors
[“#9BB08E”]
谢谢您的帮助1+
var myChart = new JSChart('graph', 'bar');
myChart.setDataArray(array);
myChart.colorizeBars(getColorArray(array.length));
var getRandomColor = function () {
    var random = Math.random();
    var exponent = --random.toExponential().split('-')[1];

    random *= Math.pow(10, exponent);

    return '#' + ( ~~(random * (1 << 24) )).toString(16);
};
// Example data Array.
var data = [ 1, 2, 4, 5, 6];

// The amount of random colors you want to generate.
var amount = data.length;
// Will contain the random hex colors.
var colors = [];

// Generate colors.
while(amount--) {
    colors.push(getRandomColor());
}
parseInt('ffffff', 16); // 16777215
parseInt('000000', 16); // 0