如何为图表创建动态颜色列表 [英] How to create dynamic color list for charts

查看:29
本文介绍了如何为图表创建动态颜色列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为图表创建动态颜色列表

How to create dynamic color list for charts

基本上我的数据库中有一个不同的值,或者每个值都包含每个百分比,我需要在饼图中用不同的颜色显示这个百分比......

Basically i have a different value in my database or each values contain each percentage and i need to show this percentages in pie chart with different colors ....

这是静态方式的饼图示例代码

This is the example code of pie chart in static way

<color value="#99CDFB"/>
<color value="#3366FB"/>
<color value="#0000FA"/>
<color value="#F8CC00"/>
<color value="#F89900"/>
<color value="#F76600"/>

但是我需要像这样使用 PHP(for 循环/foreach 循环)的动态方式

but i need dynamic way using PHP ( for loop / foreach loop) like this

$color = "";
foreach($data as $data){
   echo '<color value=".$color."/>';
}

我不知道创建动态颜色列表也看截图验证

and i don't know to create dynamic color list also see the screenshot for verification

推荐答案

对于随机颜色的字符串,这里:

For random string of colors, here:

function randColor( $numColors ) {
    $chars = "ABCDEF0123456789";   
    $size = strlen( $chars );
    $str = array();
    for( $i = 0; $i < $numColors; $i++ ) {
        for( $j = 0; $j < 6; $j++ ) {
            $str[$i] .= $chars[ rand( 0, $size - 1 ) ];
        }
    }
    return $str;
}

然后,在您的函数中,使用 $colors = randColor( 6 ); 创建总共 6 种不同的颜色.之后,这是您的输出颜色方法.

Then, in your function, use $colors = randColor( 6 ); to create a total of 6 different colors. After that, here's your output color method.

foreach( $colors as $codeColor ) {
    echo "<color value=\"#{$codeColor}\" />\n";
}

这篇关于如何为图表创建动态颜色列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆