当前位置: 移动技术网 > IT编程>开发语言>c# > ZedGraph怎样在生成曲线时随机生成不一样的颜色

ZedGraph怎样在生成曲线时随机生成不一样的颜色

2019年12月25日  | 移动技术网IT编程  | 我要评论

场景

在使用zedgraph生成多条曲线时为了能区分曲线颜色,要求随机设置曲线颜色。

首先从system.drawing.color中获取所有颜色的对象的数组,然后将其顺序打乱随机排序,然后在生成曲线时从color数组中取color并赋值。

效果

 

 

注:

博客主页:

关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

单纯生成曲线的代码

lineitem mycurve = mypane.addcurve(ylist[i].title, list, curvecolor, symboltype.none);

第一个参数:图例标题

第二个参数:坐标点的list

第三个参数:颜色color对象

第四个参数:曲线节点符号

所以在生成曲线时就要在第三个参数上进行修改

首先获取color的所有颜色对象

//用于存取取出的颜色对象 
list<color> colorlist = new list<color>(); 
//通过getmember获取所有的公共成员 
foreach (var item in typeof(color).getmembers()) 
      { 
       //只取属性且为属性中的已知color,剔除byte属性以及一些布尔属性等(a b g r isknowncolor name等) 
          if (item.membertype == system.reflection.membertypes.property && system.drawing.color.fromname

(item.name).isknowncolor == true) 
              { 
                  color color = system.drawing.color.fromname(item.name); 
                  colorlist.add(color); 
               } 
        } 
//转成数组 
color[] colors = colorlist.toarray();

 

然后将color数组的顺序进行打乱

colors = getdisrupteditems(colors);

 

调用乱序方法

private static color[] getdisrupteditems(color[] colors) 
        { 
            //生成一个新数组:用于在之上计算和返回 
            color[] temp; 
            temp = new color[colors.length]; 
            for (int i = 0; i < temp.length; i++) 
            { 
                temp[i] = colors[i]; 
            }

            //打乱数组中元素顺序 
            random rand = new random(datetime.now.millisecond); 
            for (int i = 0; i < temp.length; i++) 
            { 
                int x, y; color t; 
                x = rand.next(0, temp.length); 
                do 
                { 
                    y = rand.next(0, temp.length); 
                } while (y == x);

                t = temp[x]; 
                temp[x] = temp[y];
                temp[y] = t; 
            }

            return temp;

        }

 

然后在循环生成曲线时,循环条件为要生成的曲线数量与颜色数组的长度进行对比判断。

int index = 0;
//循环添加曲线
foreach(datatreenode node in global.instance.preparecomparedatainone)
      {
          
           list = setcurvetextinone(ylist[i].titlekey,record);
           //如果要生成的曲线数量 < 颜色数组长度 那么就从颜色数组中去按索引取
           color curvecolor = new color();
           if (global.instance.preparecomparedatainone.count < colors.length)
               {
                    curvecolor = colors[index];
                }
           //否则要生成的曲线数量 > 颜色数组长度
           else
                {
                    //当索引达到颜色数组的长度时 索引置0
                    if (index >= colors.length)
                       {
                           index = 0;
                        }
                    curvecolor = colors[index];
                }
            lineitem mycurve = mypane.addcurve(ylist[i].title, list, curvecolor, symboltype.none);
            index++;
 }

 

其中global.instance.preparecomparedatainone就是要生成的曲线的数据,其count就是要生成的曲线数量。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网