用Dundas制作盒须图BoxPlot(盒式图,箱形图)

前段时间有个需求,要求做个"盒式图"或叫"盒须图""箱形图",此图是统计学中用于统计分析的一类图表,开始看到这个词有些奇怪,说实话之前从来没听说过这样的图!因为是用户需求,还是要尝试实现一下!尝试过用水晶报表,Excel等,但都没找到实现方法或找到方法但不是很理想,最终Dundas Chart(Enterprise)控件中终于找到了相应的控件(当然其它的控件也提供此图支持)
现把学习过程记录下来以备后用,或给有用的朋友一点提示..
 
  
一.名词解释

    在网上找了N久,关于盒须图的解释很少,下面是搜索的几个解释,可以综合起来理解:
    1.盒须图又称为箱形图,其绘制须使用常用的统计量,最适宜提供有关数据的位置和分散的参考,尤其在不同的母体数据时更可表现其差异
常用的统计量 :
 。 平均数
 。 中位数
 。 百分位数
 。 四分位数
 。 全距
 。 四分位距
 。 变异数和标准差
 。
盒须图的绘制
详情参见:http://web.thu.edu.tw/wenwei/www/cgi/stat/box/index.html#a4
 2.盒须图提供了一种只用5个点对数据集做简单的总结的方式。这5个点包括中点、Q1、Q3、分部状态的高位和低位。盒须图很形象的分为中心、延伸以及分部状态的全部范围
A Box-And-Whisker Plot 
(http://wme.lzu.edu.cn/cn/stat_boxwhisker.html?index=/cn/stat_index.html&indexname=%E8%BF%94%E5%9B%9E%E7%9B%AE%E5%BD%95)



二. 盒须图百分位数的计算方式
   盒式图中最重要的是对相关统计点的计算,相关统计点都可以通过百分位计算方法进行实现,强大的教育统计分析软件SPSS,提供了五种计算方式,用于计算百分位的计算方案可参见(http://www.broadtarget.com/Blog/?action/viewspace/itemid/44).本文的百分位数的计算方式采用最基本的计算方案.
 

百分位数:
p个样本百分位数是某一个数值,使得样本中有 p 部分的观察值小于或等于
求第p 个百分位数的程序:
1.
将数据由小到大排序。
2.
计算第个位置
3.
假如 i 不为整数,则取下一个比i 还要大的值,即为第 p 个百分位数。
 假如 i 为整数,则第p 个百分位数为第 i i+1的平均。
注意:第50个百分位数 = 中位数

三.样本数据结构,数据,计算方法
  
1.成绩表
    班级  学号      分数    其它字段
       3    01     85.50
       3    02     78.00
       4    03     76.50
       4    04     71.00
       5    05     67.00
       5    06     76.00
       ......

   2.盒须图数据结构表
    (Q1=lower_whisker,Q2=upper_whisker,Q3=low_box,Q4=upper_box,Q5=med)
   分组 分组名称 Q1    Q2    Q3    Q4    avg   Q5    
    1 高一1班 46.0 79.5 63.0 73.0 66.0 71.0
    2 高一2班 48.6 82.2 63.3 76.5 68.1 71.5
    3 高一3班 48.9 82.5 63.4 74.0 67.1 68.5    
    4 高一4班 65.7 87.3 74.8 81.4 77.5 79.3
    5 高一5班 53.0 88.5 73.1 83.5 76.6 78.5
    6 高一6班 62.5 88.3 72.5 82.3 76.0 76.5

    3.分析存储过程
     执行分析存储过程,将成绩表(1)中的数据分析为盒须图指定格式盒须图数据结构表(2)

分析存储过程
  1set ANSI_NULLS ON
  2set QUOTED_IDENTIFIER ON
  3go
  4
  5
  6
  7-- =============================================
  8-- Author:      林付国
  9-- Create date: 2007.8.14
 10-- Description:    盒须图数据生成
 11/**//*
 12    Name:盒须图(箱型图)统计数据
 13    Date:2007.8.14
 14    描述:完成盒须图5%,25%,50%,75%,95%,平均值的统计数据计算
 15    规则:
 16        第N個百分位數的計算方法
 17        1. 将数据由小到大排序。 
 18        2. 计算第i=(p/100)*n個位置
 19        3. 假如i不为整数,则取下一个比i还要大的值,即为第P个百分位数。
 20            假如i为整数,则第P个百分位数为第i和i+1的平均。
 21           注意:第50个百分位数 = 中位数
 22*/

 23
 24-- =============================================
 25ALTER PROCEDURE [dbo].[BoxPlot]
 26    @ExamID numeric(20)
 27AS
 28BEGIN
 29    
 30    
 31    
 32
 33    declare @p5 float;
 34    declare @p25 float;
 35    declare @p50 float;
 36    declare @p75 float;
 37    declare @p95 float;
 38    set @p5 = .95;
 39    set @p25 = .75;
 40    set @p50 = .5;
 41    set @p75 = .25;
 42    set @p95 = .05;
 43
 44    with 
 45    e1 as (
 46    select classid, score, row_number() over(partition by classid order by score descas PRN, 
 47    count(*over(partition by classid) as N
 48    from TB_DM_TotalScore
 49    ), 
 50    e2 as (
 51        select 
 52            classid, score, PRN, N, 
 53            1 + (@p5 * (N-1)) as RN5,
 54            (@p25 * N) as RN25,
 55            (@p50 * N) as RN50,
 56            (@p75 * N) as RN75,
 57            (@p95 * N) as RN95
 58        from e1
 59    ), 
 60    e3 as 
 61    (
 62        select classid, score, N, PRN, RN5, RN25,RN50,RN75,RN95,
 63            CEILING(RN5) as CRN5, FLOOR(RN5) as FRN5,
 64            CEILING(RN25) as CRN25, FLOOR(RN25) as FRN25,
 65            CEILING(RN50) as CRN50, FLOOR(RN50) as FRN50,
 66            CEILING(RN75) as CRN75, FLOOR(RN75) as FRN75,
 67            CEILING(RN95) as CRN95, FLOOR(RN95) as FRN95
 68        from e2
 69    ), 
 70
 71    er as (
 72        select 
 73            classid, score, N, PRN, 
 74            RN5, CRN5, FRN5,
 75            RN25, CRN25, FRN25,
 76            RN50, CRN50, FRN50, 
 77            RN75, CRN75, FRN75,
 78            RN95, CRN95, FRN95, 
 79            case when e3.CRN5 = e3.FRN5 and e3.CRN5 = e3.RN5 then 1 else 0 end as grp5,
 80            case when e3.CRN25 = e3.FRN25 and e3.CRN25 = e3.RN25 then 1 else 0 end as grp25,
 81            case when e3.CRN50 = e3.FRN50 and e3.CRN50 = e3.RN50 then 1 else 0 end as grp50,
 82            case when e3.CRN75 = e3.FRN75 and e3.CRN75 = e3.RN75 then 1 else 0 end as grp75,
 83            case when e3.CRN95 = e3.FRN95 and e3.CRN95 = e3.RN95 then 1 else 0 end as grp95
 84        from e3
 85    )
 86
 87    --select 
 88    --    classid, score, N, PRN, 
 89    --    RN5, CRN5, FRN5,grp5,
 90    --    RN25, CRN25, FRN25,grp25,
 91    --    RN50, CRN50, FRN50,grp50,
 92    --    RN75, CRN75, FRN75,grp75,
 93    --    RN95, CRN95, FRN95,grp95
 94    -- from  er
 95    --
 96    insert into TB_DM_BoxPlot([ExamID]
 97      ,[groupid]
 98      ,[GroupName]
 99      ,[avg]
100      ,[lower_whisker]
101      ,[low_box]
102      ,[med]
103      ,[upper_box]
104      ,[upper_whisker])
105    select @ExamID as ExamID ,er.classid, tc.classname,avg(score) [avg],
106        sum(case grp5
107            when 1 then (case PRN when RN5 then score end)
108            else (case PRN
109                 when FRN5 then (CRN5 - RN5) * score
110                 when CRN5 then (RN5 - FRN5) * score end)
111            endas percentile_score5,
112        sum(case grp25
113            when 1 then (case PRN when RN25 then score end)
114            else (case PRN
115                 when FRN25 then (CRN25 - RN25) * score
116                 when CRN25 then (RN25 - FRN25) * score end)
117            endas percentile_score25,
118        sum(case grp50
119            when 1 then (case PRN when RN50 then score end)
120            else (case PRN
121                 when FRN50 then (CRN50 - RN50) * score
122                 when CRN50 then (RN50 - FRN50) * score end)
123            endas percentile_score50,
124        sum(case grp75
125            when 1 then (case PRN when RN75 then score end)
126            else (case PRN
127                 when FRN75 then (CRN75 - RN75) * score
128                 when CRN75 then (RN75 - FRN75) * score end)
129            endas percentile_score75,
130        sum(case grp95
131            when 1 then (case PRN when RN95 then score end)
132            else (case PRN
133                 when FRN95 then (CRN95 - RN95) * score
134                 when CRN95 then (RN95 - FRN95) * score end)
135            endas percentile_score95
136    from er left join tb_class tc on er.classid = tc.classid
137    group by er.classid,tc.classname;
138
139END
140
141
142
143
144
145

四.实现源码(C#)
   实现数据绑定的源码非常简单,在对应DundasChart控制上,直接将数据源绑定即可
五.结果图像
      盒须图的显示结果如下所示:



 

附: 

统计学名词

population 母体
sample 样本
census 普查
sampling 抽样
quantitative 量的
qualitative/categorical 质的
discrete 离散的
continuous 连续的
population parameters 母体参数
sample statistics 样本统计量
descriptive statistics 叙述统计学
inferential/inductive statistics 推论/归纳统计学
levels of measurement 衡量尺度
nominal scale 名目尺度
ordinal scale 顺序尺度
interval scale 区间尺度
ratio scale 比例尺度
frequency distribution 次数分配
relative frequency 相对次数
range 全距
class midpoint 组中点
class limits 组限
class boundaries 组界
class width 组距
cumulative frequency (以下) 累加次数
decumulative frequency 以上累加次数
histogram 直方图
pie chart 饼图
ogive 肩形图
frequency polygon 多边形图
cumulative frequency polygon 累加次数多边形图
box plot 盒须图
stem and leaf plot 枝叶图
measures of central tendency 中央趋势量数
mean 平均数
median 中位数
mode 众数
location measures 位置量数
percentile 百分位数
quartile 四分位数
decile 十分位数
dispersion measures 分散量数
range 全距
interquartile-range IQR 四分位距
mean absolute deviation 平均绝对离差
variance 变异数
standard deviation 标准差
coefficient of variation 变异系数
left-skewed 左偏
negative-skewed 负偏
right-skewed 右偏
positive-skewed 正偏
contingency table 列联表
sampling distribution (of a statistic)(某个统计量的) 抽样分布
point estimate 点估计值
point estimator 点估计式
unbiased estimator 不偏点估计式
efficient estimator 有效点估计式
consistent estimator 一致点估计式
confidence level 信赖水准
confidence interval 信赖区间
null hypothesis 虚无假设
alternative hypothesis 对立假设
left-tailed test 左尾检定
right-tailed test 右尾检定
two-tailed test 双尾检定
test statistic 检定统计量
critical value 临界值

转载于:https://www.cnblogs.com/linfuguo/archive/2007/09/04/878345.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值