| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1288 人关注过本帖
标题:从数据库读取数据总和传送给一个变量(附有画图代码),请大家指教一下,先 ...
只看楼主 加入收藏
huanying205
Rank: 1
等 级:新手上路
帖 子:107
专家分:0
注 册:2007-6-14
收藏
 问题点数:0 回复次数:3 
从数据库读取数据总和传送给一个变量(附有画图代码),请大家指教一下,先谢谢了
我想画一个直方图,代码如下:
Graphics g = e.Graphics;
            int MaxNum = 12;
            int start;
            int end;
            int legendindex, heightindex;
            string tempValue = "";

            Pen blackpen = new Pen(Color.Black,2);
            Brush[] colorarray = new Brush[12];
            colorarray[0] = new SolidBrush(Color.Red);
            colorarray[1] = new SolidBrush(Color.Green);
            colorarray[2] = new SolidBrush(Color.Blue);
            colorarray[3] = new SolidBrush(Color.Yellow);
            colorarray[4] = new SolidBrush(Color.Gray);
            colorarray[5] = new SolidBrush(Color.Aquamarine);
            colorarray[6] = new SolidBrush(Color.Turquoise);
            colorarray[7] = new SolidBrush(Color.SandyBrown);
            colorarray[8] = new SolidBrush(Color.NavajoWhite);
            colorarray[9] = new SolidBrush(Color.LavenderBlush);
            colorarray[10] = new SolidBrush(Color.Chartreuse);
            colorarray[11] = new SolidBrush(Color.LightCyan);

            int legendfontsize = 8;
            Font LegendArial = new Font("Arial",legendfontsize);
            int labelfontsize = 12;
            Font LabelArial = new Font("Arial",labelfontsize);
            int titlefontsize = 18;
            Font TitleArial = new Font("Arial",titlefontsize,FontStyle.Bold);

            string legend = initlegend;
            start = 0;
            end = legend.Length;
            legend += ',';
            legendindex = 0;
            string[] legendarray = new string[20];
            while (start < legend.Length)
            {
                end = legend.IndexOf(',');
                tempValue = legend.Substring(start,end);
                legendarray[legendindex] = tempValue;
                legend = legend.Substring(++end,legend.Length-end);
                legendindex++;
            }
            if (legendindex > MaxNum)
                legendindex = MaxNum;

            string height = initheight;
            start = 0;
            end = height.Length;
            height += ',';
            heightindex = 0;
            double[] heightarray = new double[20];
            while (start < height.Length)
            {
                end = height.IndexOf(',');
                tempValue = height.Substring(start,end);
                heightarray[heightindex] = double.Parse(tempValue);
                height = height.Substring(++end,height.Length-end);
                heightindex++;
            }
            if (heightindex > MaxNum)
                heightindex = MaxNum;

            double barMax = heightarray[0];
            for (int i = 1; i < heightindex; i++)
                if (barMax < heightarray[i])
                    barMax = heightarray[i];

            double[]  scaledheightarray = new double[heightindex];
            for (int i = 0; i < heightindex; i++)
                scaledheightarray[i] = heightarray[i] * 175 / barMax;

            int tpos = 200 - ((TitleStr.Length / 3) * titlefontsize);
            g.DrawString(TitleStr,TitleArial,colorarray[0],tpos,15);
            int hpos = 200 - ((HorizontalStr.Length / 3) * labelfontsize);
            g.DrawString(HorizontalStr,LabelArial,colorarray[2],hpos,240);
            int vpos = 120 - ((VerticalStr.Length * 2 / 3) * labelfontsize);
            char[] mychar = VerticalStr.ToCharArray();
            for (int i = 0; i < VerticalStr.Length; i++)
            {
                string s1 = mychar[i].ToString();
                g.DrawString(s1,LabelArial,colorarray[2],20,vpos+(i*20));
            }

            g.DrawLine(blackpen,48,50,48,232);
            g.DrawLine(blackpen,48,232,450,232);

            float x1 = 50;
            float barwidth = 350 / heightindex;
            for (int i = 0; i < heightindex; i++)
            {
                g.FillRectangle(colorarray[i],x1,230-Convert.ToInt32(scaledheightarray[i]),barwidth,Convert.ToInt32(scaledheightarray[i]));
                x1 += barwidth;
            }

            float legendXPos = 525;
            float legendYPos = 40;
            for (int i = 0; i < legendindex; i++)
            {
                g.FillRectangle(colorarray[i],legendXPos,legendYPos,20,20);
                g.DrawString(legendarray[i], LegendArial, colorarray[2], legendXPos + 30, legendYPos);
                legendYPos += 30;
            }

            for (int i = 0; i < 12; i++)
            {
                colorarray[i].Dispose();
            }
            TitleArial.Dispose();
            LabelArial.Dispose();
            LegendArial.Dispose();
            blackpen.Dispose();
这样画法,在窗体运行的时候,直方图就直接出来了,但是他的数据信息我是在代码里这样赋值的:
        public string initheight = "10,20,30,15,60,45,50";
这些值决定了直方图的高度,我想要从数据库里取数据赋值给initheight ,举个例子:
序号   姓名   成绩
1     张三    80
2     李四    77
3     王五    69
4     张六    85
5     张七    80
6     李二    67
我想把姓张、姓李、姓王的成绩的总和做成直方图,比较一下,就是从数据库中取出第三列中各个姓的成绩总和赋给initheight
public string initheight = "245,144,69";
其实说不定也不用这些代码,另有一种更直接的方法,我现在是没头绪,希望大家指教一下,先谢谢了!
搜索更多相关主题的帖子: 数据库 变量 画图 代码 指教 
2007-12-19 15:50
huwei1024
Rank: 1
等 级:禁止发言
帖 子:149
专家分:0
注 册:2007-4-12
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽

www.
2007-12-19 15:51
huanying205
Rank: 1
等 级:新手上路
帖 子:107
专家分:0
注 册:2007-6-14
收藏
得分:0 
其实有需要画图的朋友可以看看,不需要画图的朋友也就懒得看了,只要明白我的问题就行了: 我就是想从数据库中有选择性的取出某些数据的和用来画直方图,希望大家给出点建议
2007-12-19 15:56
andey
Rank: 2
等 级:新手上路
威 望:4
帖 子:938
专家分:0
注 册:2007-7-18
收藏
得分:0 
想学习这方面的只是...
帮你顶一下!!!

msdn == 葵花宝典!!!
QQ:122768959
2007-12-20 11:25
快速回复:从数据库读取数据总和传送给一个变量(附有画图代码),请大家指教一下 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.041200 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved