Documente Academic
Documente Profesional
Documente Cultură
Introduction
A pivot table provides a 3 dimensional view of data. Microsoft Excel has the ability to create pivot tables, however, it's not always convenient to use Excel. You may want to create a pivot report in your web application, for example. Creating even a simple pivot table can be a complex task. So it is my intention to not only provide you with an excellent tool for creating simple and advanced pivot reports, but also to remove some of the mystery that shrouds them.
Objective
We want the ability to transform 2 dimensional data in a data table into a 3 dimensional (pivot) report.
www.codeproject.com/Articles/25167/Simple-Advanced-Pivots-with-C-and-ASP-NET?display=Print
1/10
06/02/13
Sales Person Product Quantity Sale Amount John John John John John John Jane Jane Jane Jane Jane Jane Sally Sally Sally Sally Sally Sally Sarah Pens Pencils 200 400 350 500 300 100 1200 1500 393.75 418.75 600 150 800 1940 353.5 378.75 600 594 800 2020 196
www.codeproject.com/Articles/25167/Simple-Advanced-Pivots-with-C-and-ASP-NET?display=Print
2/10
06/02/13
Pencils
245
As you can see, this is a 2 dimensional table, and its not very useful as a report. So weve got to transform this data table into something more readable.
The x-axis makes up the main headings at the top of the table. The y-axis makes up the left column of the table, and the z-axis makes up the values corresponding to both the x-axis and y-axis. A simple pivot will have one z-axis column for every x-axis value, while an advanced pivot will have multiple z-axis columns per x-axis value. A very important point is that the z-axis values can only be numeric. This is because the z-axis values are totaled horizontally and vertically. Using a non-numeric field as a z-axis field will throw an exception. So if you look at the data table above, youll notice that the Sales Person and Product fields can be assigned to either the x-axis or y-axis, but not the z-axis. The Quantity and Sale Amount fields can be assigned to the z-axis.
06/02/13
piaesrn _sTbe rvt tig Csal; #nrgo Vrals edein aibe #einCntutr rgo osrcos pbi PvtDtTbedtTbe ulc io(aaal aaal) { Ii(; nt) _aaal =dtTbe DtTbe aaal; } #nrgo Cntutr edein osrcos
This section of the code is pretty self explanatory. Youll construct a Pivot object by passing a data table as an argument. The Init() method simply assigns an empty string value to the CSS variable. If the CSS variable is an empty string, the styling method will use the default style. Each of the CSS variables has a corresponding property.
piaesrn FnVlesrn xxsil,srn xxsau,srn yxsil,srn rvt tig idau(tig AiFed tig AiVle tig AiFed tig yxsau,srn zxsil) AiVle tig AiFed { srn zxsau ="; tig AiVle " ty r { frah(aao rwi _aaal.os oec DtRw o n DtTbeRw) { i (ovr.otigrwxxsil] = xxsau & f CnetTSrn(o[AiFed) = AiVle & CnetTSrn(o[AiFed)= yxsau) ovr.otigrwyxsil] = AiVle { zxsau =CnetTSrn(o[AiFed) AiVle ovr.otigrwzxsil]; bek ra; } } } cth ac { trw ho; } rtr zxsau; eun AiVle }
The FindValue() method searches the data table for a z-axis value corresponding to the x-axis and y-axis values. The xAxisField is the column name of the x-axis field (example Product), and the xAxisValue is a value in that column. The yAxisField is the column name of the y-axis field (example Sales Person), and the yAxisValue is a value in that column. The zAxisField is the name of the column in which the z-axis value that youre looking for resides (example Sale Amount).
piaesrn[ FnVle(tigxxsil,srn xxsau,srn yxsil,srn rvt tig] idaussrn AiFed tig AiVle tig AiFed tig yxsau,srn[ zxsils AiVle tig] AiFed) { itzxs=zxsilsLnt; n Ai AiFed.egh i (Ai <1 f zxs ) zxs+ Ai+; srn[ zxsaus=nwsrn[Ai] tig] AiVle e tigzxs; /stdfutvle /e eal aus fr(n i=0 i< zxsausGtpeBud0;i+ o it ; = AiVle.eUpron() +) { zxsausi ="" AiVle[] 0; } ty r { frah(aao rwi _aaal.os oec DtRw o n DtTbeRw) { i (ovr.otigrwxxsil] = xxsau & f CnetTSrn(o[AiFed) = AiVle &
www.codeproject.com/Articles/25167/Simple-Advanced-Pivots-with-C-and-ASP-NET?display=Print 4/10
06/02/13
CnetTSrn(o[AiFed)= yxsau) ovr.otigrwyxsil] = AiVle { fr(n z=0 z<zxs z+ o it ; Ai; +) { zxsausz =CnetTSrn(o[AiFed[]) AiVle[] ovr.otigrwzxsilsz]; } bek ra; } } } cth ac { trw ho; } rtr zxsaus eun AiVle; }
The FindValues() method is similar to the FindValue() method, however it will return multiple z-axis values. This is used for the advanced pivot table where you have multiple z-axis fields corresponding to an x-axis value.
piaevi MiHaeTpeltl(tlalCl cl) rvt od anedroClSyeHmTbeel el { i (CsoHaig= ") f _sTpedn = " { cl.tl.d(fn-aiy,"aoa) elSyeAd"otfml" thm"; cl.tl.d(fn-ie,"0t) elSyeAd"otsz" 1p"; cl.tl.d(fn-egt,"oml) elSyeAd"otwih" nra"; cl.tl.d(bcgon-oo" "lc"; elSyeAd"akrudclr, bak) cl.tl.d(clr,"ht"; elSyeAd"oo" wie) cl.tl.d(tx-lg" "etr) elSyeAd"etain, cne"; } es le cl.trbtsAd"ls" _sTpedn) elAtiue.d(Cas, CsoHaig; }
This is one of the CSS styling methods. This method in particular styles the x-axis (the row at the top of the table). If you dont assign a CSS class name to the property, the method will use the default style. The CSS class will be picked up from which ever page you add the HTML table to.
//<umr> / smay //Cetsa avne 3 Pvttbe / rae n dacd D io al. //<smay / /umr> //<aa nm=xxsil"Temi haiga tetpo terpr./aa> / prm ae"AiFed>h an edn t h o f h eot<prm //<aa nm=yxsil"Tehaigo telf o terpr./aa> / prm ae"AiFed>h edn n h et f h eot<prm //<aa nm=zxsils>h sbhaiga tetpo terpr./aa> / prm ae"AiFed"Te u edn t h o f h eot<prm //<eun>tlal Cnrl<rtrs / rtrsHmTbe oto./eun> pbi HmTbePvtal(tigxxsil,srn yxsil,srn[ zxsils ulc tlal ioTbesrn AiFed tig AiFed tig] AiFed) { HmTbetbe=nwHmTbe) tlal al e tlal(; /syetbe /tl al Tbetl(al) alSyetbe; / * *Texai i temi hrzna rw h -xs s h an oiotl o. *Tezai i tesbhrzna rw h -xs s h u oiotl o. *Teyai i telf vria clm. h -xs s h et etcl oun * / ty r { /gtdsic xxsils /e itnt AiFed Aryitxxs=nwAryit) raLs Ai e raLs(; frah(aao rwi _aaal.os oec DtRw o n DtTbeRw)
www.codeproject.com/Articles/25167/Simple-Advanced-Pivots-with-C-and-ASP-NET?display=Print 5/10
06/02/13
} /gtdsic yxsils /e itnt AiFed Aryityxs=nwAryit) raLs Ai e raLs(; frah(aao rwi _aaal.os oec DtRw o n DtTbeRw) { i (yxsCnan(o[AiFed) f !Ai.otisrwyxsil]) yxsAdrwyxsil]; Ai.d(o[AiFed) } /cet a2 aryfrteyai/-xsfed /rae D ra o h -xszai ils itzxs=zxsilsLnt; n Ai AiFed.egh i (Ai <1 f zxs ) zxs=1 Ai ; srn[]mti =nwsrn[xxsCut*zxs,yxsCut; tig, arx e tig(Ai.on Ai) Ai.on] srn[ zxsaus=nwsrn[Ai] tig] AiVle e tigzxs; fr(n y=0 y<yxsCut y+ /lo tr yai fed o it ; Ai.on; +) /op hu -xs ils { /rw /os fr(n x=0 x<xxsCut x+ /lo tr xai fed o it ; Ai.on; +) /op hu -xs ils { /mi clms /an oun /gttezai vle /e h -xs aus zxsaus=FnVle(AiFed CnetTSrn(Ai[] AiVle idausxxsil, ovr.otigxxsx) ,yxsil,CnetTSrn(Ai[],zxsils; AiFed ovr.otigyxsy) AiFed) fr(n z=0 z<zxs z+ /lo tr zai fed o it ; Ai; +) /op hu -xs ils { /sbclms /u oun mti[(x+1 *zxs-zxs +z,y =zxsausz; arx(( ) Ai Ai) ) ] AiVle[] } } } /cluaettl frteyai /aclt oas o h -xs dcml]yoas=nwdcml(Ai.on *zxs] eia[ Ttl e eia[xxsCut Ai); fr(n cl=0 cl<(Ai.on *zxs;cl+ o it o ; o xxsCut Ai) o+) { yoascl =0 Ttl[o] ; fr(n rw=0 rw<yxsCut rw+ o it o ; o Ai.on; o+) { yoascl + CnetTDcmlmti[o,rw) Ttl[o] = ovr.oeia(arxcl o]; } } /cluaettl frtexai /aclt oas o h -xs dcml, xoas=nwdcmlzxs (Ai.on +1] eia[] Ttl e eia[Ai, yxsCut ); fr(n y=0 y<yxsCut y+ /lo tr teyai o it ; Ai.on; +) /op hu h -xs { itzon =0 n Cut ; fr(n z=0 z<(Ai *xxsCut;z+ /lo tr tezai o it ; zxs Ai.on) +) /op hu h -xs { xoaszon,y + CnetTDcmlmti[,y) Ttl[Cut ] = ovr.oeia(arxz ]; i (Cut= (Ai -1) f zon = zxs ) zon =0 Cut ; es le zon+; Cut+ } } fr(n x =0 x <zxs x+)/GadTtl o it x ; x Ai; x+ /rn oa { fr(n x =0 x <yxsCut x+) o it y ; y Ai.on; y+ { xoasx,yxsCut + xoasx,x] Ttl[x Ai.on] = Ttl[x y; } } /BidHM Tbe /ul TL al /Apn mi rw(-xs /ped an o xai) HmTbeo miRw=nwHmTbeo(; tlalRw ano e tlalRw) miRwClsAdnwHmTbeel); ano.el.d(e tlalCl() fr(n x=0 x< xxsCut x+ /lo tr xai +1 o it ; = Ai.on; +) /op hu -xs {
www.codeproject.com/Articles/25167/Simple-Advanced-Pivots-with-C-and-ASP-NET?display=Print 6/10
06/02/13
} tbeRw.d(ano) al.osAdmiRw; /Apn sbrw(-xs /ped u o zai) HmTbeo sbo =nwHmTbeo(; tlalRw uRw e tlalRw) sbo.el.d(e HmTbeel); uRwClsAdnw tlalCl() sbo.el[]Inret=yxsil; uRwCls0.neTx AiFed /syecl /tl el Sbedreltl(uRwCls0) uHaeClSyesbo.el[]; fr(n x=0 x< xxsCut x+ /lo tr xai +1 o it ; = Ai.on; +) /op hu -xs { fr(n z=0 z<zxs z+ o it ; Ai; +) { HmTbeelcl =nwHmTbeel) tlalCl el e tlalCl(; cl.neTx =zxsilsz; elInret AiFed[] /syecl /tl el Sbedreltl(el; uHaeClSyecl) sbo.el.d(el; uRwClsAdcl) } } tbeRw.d(uRw; al.osAdsbo) /Apn tbeiesfo mti /ped al tm rm arx fr(n y=0 y<yxsCut y+ /lo tr yai o it ; Ai.on; +) /op hu -xs { HmTbeo ieRw=nwHmTbeo(; tlalRw tmo e tlalRw) fr(n z=0;z< (Ai *xxsCut;z+ /lo tr zai +1 o it = zxs Ai.on) +) /op hu -xs { HmTbeelcl =nwHmTbeel) tlalCl el e tlalCl(; i ( = 0 f z = ) { cl.neTx =CnetTSrn(Ai[]; elInret ovr.otigyxsy) /syecl /tl el MiHaeLfClSyecl) anedreteltl(el; } es le { cl.neTx =CnetTSrn(arx(-) y) elInret ovr.otigmti[z1, ]; /syecl /tl el IeClSyecl) tmeltl(el; } ieRwClsAdcl) tmo.el.d(el; } /apn xai gadttl /ped -xs rn oas fr(n z=0 z<zxs z+ o it ; Ai; +) { HmTbeelcl =nwHmTbeel) tlalCl el e tlalCl(; cl.neTx =CnetTSrn(Ttl[,y) elInret ovr.otigxoasz ]; /syecl /tl el Ttleltl(el; oaClSyecl) ieRwClsAdcl) tmo.el.d(el; } tbeRw.d(tmo) al.osAdieRw; } /apn yai ttl /ped -xs oas HmTbeo ttlo =nwHmTbeo(; tlalRw oaRw e tlalRw) fr(n x=0 x< (Ai *xxsCut;x+ o it ; = zxs Ai.on) +) { HmTbeelcl =nwHmTbeel) tlalCl el e tlalCl(; i ( = 0 f x = ) cl.neTx ="oas; elInret Ttl" es le cl.neTx =CnetTSrn(Ttl[-]; elInret ovr.otigyoasx1)
www.codeproject.com/Articles/25167/Simple-Advanced-Pivots-with-C-and-ASP-NET?display=Print 7/10
HmTbeelcl =nwHmTbeel) tlalCl el e tlalCl(; cl.oSa =zxs elClpn Ai; i ( <xxsCut f x Ai.on) cl.neTx =CnetTSrn(Ai[]; elInret ovr.otigxxsx) es le cl.neTx ="rn Ttl" elInret Gad oas; /syecl /tl el MiHaeTpeltl(el; anedroClSyecl) miRwClsAdcl) ano.el.d(el;
06/02/13
} /apn xai/-xsttl /ped -xsyai oas fr(n z=0 z<zxs z+ o it ; Ai; +) { HmTbeelcl =nwHmTbeel) tlalCl el e tlalCl(; cl.neTx =CnetTSrn(Ttl[,xoasGtpeBud1]; elInret ovr.otigxoasz Ttl.eUpron()) /syecl /tl el Ttleltl(el; oaClSyecl) ttlo.el.d(el; oaRwClsAdcl) } tbeRw.d(oaRw; al.osAdttlo)
The PivotTable() method is where all the magic happens. There are two overloads of this method; one creates a simple pivot, and the other (the one above) creates an advanced pivot. The only difference is that the simple pivot has one z-axis field while the advanced pivot has more than one.
06/02/13
d.osAdnwojc[ {"aa" "acltr" 6,600 }; tRw.d(e bet] Srh, Cluaos, 6 6.0 ) d.osAdnwojc[ {"aa" "akPcs,5,22.0}; tRw.d(e bet] Srh, Bc ak" 0 000 ) rtr d; eun t
Ive created this data table property, which builds the data table in the example above. This is just for demonstration purposes.
poetdvi Pg_odojc sne,Eetrse rtce od aeLa(bet edr vnAg ) { /Avne Pvt /dacd io Pvtavio =nwPvtDtTbeoTsig; io dPvt e io(aaalFretn) HmTbeavnePvt=avio.ioTbe"ae Pro" "rdc" nwsrn[ { tlal dacdio dPvtPvtal(Sls esn, Pout, e tig] "aeAon" "uniy }; Sl mut, Qatt" ) dv.otosAdavnePvt; i1Cnrl.d(dacdio) /Sml Pvt /ipe io Pvtpvt=nwPvtDtTbeoTsig; io io e io(aaalFretn) /oerd dfutsyewt cs /vrie eal tl ih s pvtCsoHaig="edn" io.sTpedn Haig; pvtCsetoun="etoun; io.sLfClm LfClm" pvtCstm ="tm" io.sIes Ies; pvtCsoas="oas; io.sTtl Ttl" pvtCsal ="al" io.sTbe Tbe; HmTbesmlPvt=pvtPvtal(Pout,"ae Pro" "aeAon"; tlal ipeio io.ioTbe"rdc" Sls esn, Sl mut) dv.otosAdsmlPvt; i2Cnrl.d(ipeio) }
The code above instantiates two pivot objects. The first is for an advanced pivot and the second is for a simple pivot. As you can see I've added the HtmlTable controls to divs. I created the divs with the runat="server" attribute so that I can access it in the code. The divs simply helps with the placement of the HtmlTable. The advanced pivot with default styling.
John Product Sale Amount Quantity
200 400 100 50 120 75 945
Quantity
225 335 200 75 80 97 1012
Quantity
202 303 198 98 80 101 982
350 Pens 500 Pencils Notebooks 300 100 Rulers Calculators 1200 Back Packs 1500 Totals 3950
The simple pivot with custom styling using css. Sales Person Pens John Jane 350 393.75 Pencils 500 418.75 Notebooks Rulers Calculators Back Packs Grand Totals 300 600 100 150 1200 800 1500 1940 3950 4302.50
www.codeproject.com/Articles/25167/Simple-Advanced-Pivots-with-C-and-ASP-NET?display=Print
9/10
06/02/13
353.5 196
378.75 306.25
600 594
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
Nitesh Maharaj
Software Developer South Africa Member
Nitesh Maharaj is currently working as a software developer for a company in South Africa. He is extensively involved in enterprise software development. His preferred technologies are Microsoft's SQL Server, Visual Studio, and C# is his language of choice.
www.codeproject.com/Articles/25167/Simple-Advanced-Pivots-with-C-and-ASP-NET?display=Print
10/10