Thisismoreofanaestheticchoice, asopposedto a functionalchoice, butitendsupbeing a nicewaytodothisondhewhenyou'reusinganythingwithinthecontainer, thenextclassthatyouhavetohavehisrow.
Butformypurposes, thisiswhat I what I wantedtodioandwhat I reallywanttopointoutisthat I havemadeveryjudicioususeofideashere, wherewithin d three, we'regoingtohavethecapabilitytosimilartonativeJavaScriptactuallyselectcomponentsoutoftheDomoutofthedocumentobjectmodel.
I said, okay, I'm gonnahave a header, andthen I havethisotherdivthat I havesaidisgoingtobealbums.
Theotherreallyimportantthingherethatisnowactually D threespecificisthat I needtobesuretoincludethe D threeJavascriptlibraryif I don't includethis.
Weliterallyaren't workingwith D threeatthispoint.
Inordertofindthisfile, youcanliterallyjustGoogle d threeJavaScriptandyouwillfind a linkinordertoincludeitin a scripttag.
I alwayshighlyrecommendwheneveryoucan, followingthatlinkthatyouwouldotherwisejustinclude, andactuallycopyingandpastingallofthatsourcecodeinto a filebecausethatway.
SohavinglookednowattheJavascriptitself, I'vealsoincludedonemoreJavaScriptfile, whichisthefilethat I haveactuallywrittenandwhereanyofthelogicthat I havedoneformyprojectwillactuallyexistforyourprojects.
Theymightbe a littlemorecomplicatedwhereinfact, youmighthaveseveralJavaScriptfilesthatareallinteractingwitheachother.
Lecture s sosomeofitshouldlookvaguelyfamiliarbecauseitcameoutofthesequellecture.
I actuallyendeduphavingtotreatmydata a littlebitdifferentlytomakethingslineupproperly.
You'llrememberthatinthesequellecture, Davidwastalkingaboutwaystomakeyourdatabasemoreefficienton, andas a result, ratherthanstoreanartistname, hewouldinsteadyoustoreininterviewrepresentinganartist I d whichis a perfectlyvalidwaytodothison.
Itwasactually a verygoodopportunityformetoshowyouguyshowtousemultipledatasetsthatactuallyperhapsinteractwitheachother.
Foreachoneofthesedatasets, I wantyoutocall d threedot c s V onit.
What d threedot c s v actuallydoesisitcalls a methodthatliterallyreadsinyour C S V andsavesitasHonoreofJavaScriptobjectswhereeachoneoftheobjectsAnd I'llshowthistoyouinthedevelopertoolsinchromein a second.
Buteachoneoftheobjectsisgoingtohave a keythatisartist I D.
Sothefirstthing I saidisOkay, I, um I'm immediately.
I'm goingtohave a problembecausemydatasetsdon't make a lotofsenseon, infact, what I'm goingtodohereas I'm goingtogoaheadandcommentinthisconsullogwhichisgoingtoshowuseverythingthat's inalbumdata, and I'm goingtogoaheadandputinanotherconsullog, whichisgoingtoshowusherdistdataartist.
Sothefirstthing I alwaysliketodioisactuallyfigureout.
Whatdoesmydatalooklike?
So I alwaysConsullogmydataassoonas I'veloadeditin.
So, infact, if I savethisandmakesurethatmyserverisstillrunning, I gobackoverhere, refreshthispageandthen I'm goingtogoaheadonhopethattheInternetisworkingandinfact, youseerightawaythat I haveanerror.
You'llseeherethelinewheretheerroroccurred, So I I know I shouldbelookingatmainJslying 22 whereithassaidtome, Okay, you'vereferredtosomethingcalledalbumdataandit's notdefined.
Soinfact, if I gobacktoLine 22 I canimmediatelysayOhyeah, I didn't callitalbumdata.
I calledit, helpeddata, right?
I couldgoback, saveit, refreshthepageagainandyou'llnoticethatifyouhavenoerrors, thingswilllookgood.
Ifyouhaveerrors, thingswillnotload.
Sothisisalways a goodthingtodo.
Sothesetwothingshereareactuallycomingimmediatelyoutofthoseconsullogstatementsthat I did, andinfact, letmemakethis a littlebitbiggerforus.
Soalbumdataweseethatithas 347 items.
Remember, I mentionedthatitwasanarrayofJavaScriptobjects.
Onewayyoucanimmediatelyseethatisthatthisfirstbrackethereisinfact, a squarebracketrate.
Thistellsmethatit's anarraysimilartoanarrayinpythonorlistingpythonorin a rain.
Andsowhat I decidedtovisualizewasactuallyjustthenumberofalbumsthatwereproducedby a particularartist.
Soyounoticedhereonmy Y axis, I'vegotjust a numberofalbumsononmy X axis.
I actuallyhavethenameoftheartistthatthiscorrespondsto.
I'veadded a littlebitofextrafunctionalitythatifyouclickonanyofthebars, youcanactuallysee a listofthesealbums.
We'lltalkaboutthatin a second.
Butkeepingthatinmind, I saidtomyself, Okay, I'm goingtoneedsomewaytocountthenumberoftimesanartist I d occurs, giventhenumberofalbums, right?
Sothefirstthing I saidtomyselfis, Okay, I'm goingtoneedtomakesomekindofmappingbetweenanartist I d andtheactualnameoftheartist.
Sowhat I didwas I didthefollowing.
I said, Okay, I'm goingtomakeanartistmap, whichisitselfgoingtobejust a javascriptobject.
Oryoucanthinkofthismorelike a dictionary.
Whichiskindofhow I wasenvisioningitas I wrotethiscodeandtheideahereisthatwe'regoingtoiteratedthroughalloftheartistdataandforeachoneoftheJavaScriptobjectsintheartistdatarate.
Soforeachobjectthathasanartist, I d.
Andithasanartistname, we'regoingtosay, Okay, here's thatindividualobjectwe'regoingtosayOK, withinartistmapattheindexthatcorrespondstothat I d.
Pleasesavethenameofthatartist.
Andthiswasjusttomake a quick, easymappingsothat I couldrefertotheseobjectslater.
Themoreinterestingstuffactuallyhappensinthis d threedotnest.
Sowhat d threedotnestdoesisthatitsaystoyouOkay, I'm goingtoliterateallthroughallofthisdataforyouonwhat I'm goingtodois I'm goingtobuild a brandnewobjectwherethekeysaregorgeous, aregoingtocorrespondtosomethingthatalreadycameoutofyourdataandthevaluesortheleavesastheyliketocallit, aregoingtocorrespondtosomethingelseoutofyourdata.
Sowhat I saidhereisOK, I wantaccountbyartist I D.
Andthereason I wantedaccountbyartist I d.
Wasbecause I hadalreadymadethismappingbyartist I d.
Sothat I couldeasilyindexintoit.
So I saidOKforeachoneofthepiecesofdatawithinmyalbumdata.
Itmaybe a littlebitunclearifyou'rejustusingitforthefirsttime.
Thatbeingsaid D threeactuallyhasverygooddocumentation, AndsoifonyourcomputernowyouweretoGoogle d threedotnestexampleifyouseeanythingfrombl 3.0.0 c k.
Somethinglikethat.
I lookatitand I readblocks.
Soifyouseeanythingfromlikeblocksdotaward, that's greatexamplesofvisualizationsthatpeoplehavedonethatusedifferentfunctionalityoutof d three.
I highlyrecommend, uh, taking a lookatit.
Sonow I havethiscountbyartist I d.
And I saidtomyself, Great, I haveallofthisinformationand I havesaidAllright, I probablywanttomake a bargraphonbecause I wanttomake a bargraph.
What's importanttomeistomakesurethatmydataisarrangedinsuch a waythatit's goingtobeeasytodisplayit.
So, infact, thenextthingthat I doisaninplacesort, um, inplacehere, meaningthat I actuallycallthisfunctiononthis, um, thisarraythat I'vebeengivenanditwillsortitinplaceforme.
A isgreaterthanobjecttobeButinordertosortthingsinascendingorder, whatyoureallywantisthedifferencebetweenobject a anobjectBe.
Butkeepinmindthatobject a reallyisanobjectandas a result, ithasmultiplekeys.
Thethingthat I wasinterestedinherewasvalue.
Andthereason I knewitwasvalue.
Uh, so, forexample, like, let's say I didn't knowwhat I wasgoingtogetoutofthiscountbyartist.
I d I couldgoaheadandsay, Okay, I'm gonnaConsul, don't logitright.
Andthisisjust a cutetrick I liketouseforconsuldotlogwhereyoucouldactuallylabelthethingthatyou'reabouttologsothatyoucanfinditeasilyintheconsoleso I cangoaheadandsave.
Goaheadandrefresh, and I cansayOkay, like, whatdid I getback?
Computer.
Comeon, youcandoit.
Allright, sohereitis.
Thisartistideacount, I canseeimmediatelyagainthatthisfirstcharacterherewas a bracket.
So I knowit's gonnabe, youknow, right.
And I canseethatwhat I endupwithis a bunchofobjectswheretheyhavethekeykeyandtheotherkeyvaluewherevalueisactuallythenumberofobjectsthatwerecountedforthisparticularartist, I d.
Thiswastheonlywayformetoknowthatwhen I wassorting, I shouldbelookingatthevaluefieldandnotsomeotherfield.
I literallyhadtopullupthedataandlookatitinordertofigurethisout.
Thereisnothingwrongwithdoingthat.
Andinfact, I encourageyoutodothat.
Soonce I wentaheadandsorted, I said, Okay, justfor, like, displaypurposeshere, I'm actuallygoingtocutdownsomeofthedatawehadbecausewehave, like, 204 itemsofdataandthatjust, like, gets a littlebituglytodisplay.
Therearewaystogetaroundthis, butjustliketomakethis a tractableexample, I saidOkay, I'm gonnajustgetridofeverythingfromindexzerotoindex 1 80 thiswasperfectlyarbitrary.
Youabsolutelydon't havetodothis, butitjustsimplified a littlebitofthisprocess.
Andthenfinally, what I saidwasOkay, nowthat I haveloadedallofmydataandnowthat I havecleanedmydatain a waythat I amhappywith, I cangoaheadandcall a newfunctionthatisactuallygoingtocreatevisualization.
Soinfact, you'llnoticeherethat I nevertriedtosay, Okay, createthevisualizationuntil I'vemadesurethateverythinghasbeenloadedin.
Thisissuperimportant.
Um, easierwaystodothisincludeactually, justcalling d threedot C s V andwithinthat, passitananonymousfunctionandthendoallofyourdatacreationwithin d threedot C s V.
Soifthisartist I d matchedthecurrentkeythat I washolding, thiswouldbeconsidered a match.
Once I havethatarray, I candowhatever I wantwithoutarray, butbecausewhat I aminterestedinisactuallythecount.
I knowthatbytakingthelengthofthearray, thatisreallythesameassayinghowmanyalbumscorrespondedtooneartistandthisfinalthingheretheorder, I alwaysthink, is a littlebitweird.
Butthere's actually a littlebitofspacewhereinyoucanactuallyseethatmyvisualizationexistswithinthisnice, confinedrectangle.
Theway I'veconfiguredthatisthroughthismarginobject, where I cannow, likecalculatethewithinthesizeofmyactualvisualizationitselfbyjustchangingvaluesinoneplace, asopposedtochangingvaluesin a bunchofdifferentplaces.
Sothiswasreallymoreof a conveniencethingmorethananything, but I highlyrecommendusingit.
Sothisis a littlebitofJakewearythat I said I wouldpointouttoyouguyswhere, inessence, I'vesaidOkay, I don't wanttohardcodethewidthandtheheightofmyvisualization.
Infact, I wantmyvisualizationtoconformtowhateverthewidthandtheheightoftheelementthatitexistsinisSothat's a fancywayofsayingif I'vealreadysaidthat I'm goingtoputmyvisualizationin a boxthatis 500 by 600 pixels.
I'm onceinessence, what I wanttohappenhereIsthiswithThioequal 500 pixelsandthisheighttoequal 600 pixelsortheotherwayaround.
Sothat's worthnotingbutnoticethat I saidOkay, thisvisualizationisgoingtoexistinthechartdisplaycolumn, whichyou'llrememberwastheideaofthisparticulardefhere.
Right.
SothisiswhyideasWe'regonnabesuperimportant.
Allright, Sopleasegetmethewidthofthatandthengoaheadandsubtractthismarginthat I'vegottenyoubothfortheleftontherightwherewesaid, OK, I knowthatthisfullwithisgonnabethiswholebox.
Infact, I wanttoconstrainthings a littlebitincasemyvisualizationhaslike, strugglingitemsbecause I don't wantthosetoendupeitheroffofthescreenoroutoftheparentelement.
Ifyoueverstartreadingabout t three, you'regoingtoseethisallovertheplace.
SPGumstandsforscalablevectorgraphics, whichisreallyjust a fancywayofsayingthat a bunchofpeoplegottogetherandsaid, Yeah, graphicsontheInternetarereallyhardbecausetheminuteyoutrytorescalethem, theylosealloftheirresolution.
Soifwelookatthis, this d threedotselectisreallyanalogoustoourdocumentdotqueryselectorwherewe'vesaidOkaywith D three, goaheadandfindmetheparentelement.
Andthefirstthingthat I wantyoutodoisactuallyappendtoit.
AnSPG, andwe'lllookatthewhattheHTMLlookslikeforthisin a second, wherenow D threereallyjustlikeschainingsandtaxwherewesaid, OK, getmetheparentelementand a pendantspgtoit.
Pleasegiveittheattributesofwidthand I'vegiven a calculationforthewidth.
Pleasegiveittheattributesofheightand I'vegiven a calculationforheightandthenappendedtoit.
Thisthingcalled a g group a G groupisreallyjust a nicerway, Andythreetostartbuildinglayersontopoflayerssothatifyouwantto, forexample, applystylingto a bunchofthings.
Andfinally, what I'vesaidisokay, onceyou'veupendedthisSPG, goaheadandtransformit, whichisreallythesameassayingokay, I'm goingtoactuallyshiftitsomewhereonthepage.
Andtheshiftthat I wanttodoisahhorizontalandverticaltranslatewhere I'vesaid, OK, translateit.
Ah, a littlebittotheleft.
Thismoneyareactuallyrathertranslatedtotheright, thismanypixelssothat I getmymarginandtranslateitdownthismanypixels.
Okay, Allright, QuestiononSPG.
Youdon't havetounderstandwhatitisaslongasyouunderstandthatit's a canvasattheendoftheday, that's all.
Thisiswhereifyouthinkaboutit, um, thiswhiteblockhereyoucan't actuallyseebutisbasicallymySPGandeverythingthat I'veappendedtoitarejustotherelementsthat I haveputontothatcanvas.
Ifthatcanvaswasn't there, I couldn't haveputanyoftheseelementsonhere.
Thenextthingyoushouldnoticeisthatreally a bargraphisjust a bunchofrectangles, right?
Andthatiskindofexactlyhow D threeisgoingtotreatvisualizationswhere d threeisgoingtosay, Well, youknow, like a linegraphisreallyjust a coupleofcircleswith a linerunningthroughitor a bargraphisreallyjust a bunchofrectanglesofvaryingheightsandwits.
Andsowhenwe'regoingtobemakingthisvisualizationweregoingtosay, OK, I knowthat I need a bunchofrectanglesand I knowthat I needthemultifitrightAndthisiswherethisideaofscalingstartstocomeintoplay.
Soyou'llnoticeherethat I'vesaidOkay, therangeofthevaluesthat I needyoutooutputaregoingtobefromzerotomywidth.
Andthatmakessenserightwhere I said, Thisisgoingtobethescaleformy X axis.
Andso I knowthatmy X axisisgoingtorunfromthezerowithpixeltothepixel, representingmywit.
Ontheotherhand, I'vesaidokaybetweeneachoftheblocks, goaheadandgivemehalf a pixelworthofpaddingjusttomakeitlook a littlebitprettier.
I couldmakethisfigure.
I couldmakethissmaller.
Thisisreallyjustuptotaste.
Andfinally, it's thedomainthatkindoflooks a littlebitugly.
Rangesarethevaluesthatthescaleisgoingtooutput, Given a valueonthedomainareallofthevaluesthatyourscaleshouldexpecttogetWhenitcomestoscales, thedomainisalwaysgoingtocomefromyourdataitself.
Sowhatweendupdoingisthefollowing.
I'vesaidOkay, D three.
Pleasedome a favorandapply a functiontoeverysingleoneoftheitemsinmycountdatawherethefunctionthat I wantyoutoapplytoanindividualitemisgoingtodothefollowing.
Thelinearscaleis a littlebiteasierwherewe'vesaidOkay, alloftheitemsthatyou'regoingtooutputaregoingtobeintherangefromtheheightminusthemarginandallthewaytothemarginofthetop.
What's reallyimportanttonoteintermsofgraphicsisthatinfact, whileyourheight, orratherwhileyou'rewith, actuallymovesinthedirectionthatyouexpectedtosewlikezerostartingonthelefthandsideandthegreaternumbersmovingtotherightheightis a littlebitdifferent.
Infact, the Y axisisinverted, sozeroforthe Y axisstartsinthetoplefthandcornerandmovesdown, sothegreatervaluesareactuallyfurtherdownwithinthegraphic.
Whydidtheydothis?
I'm nottotallysure, butitcanbe a pain.
As a result, you'lloftenfindyourselfreversingheightssothatthevaluesthatyou'regettingoutareactuallycorresponding, nottotheheightitself.
Butthedifferencebetweentheheightthatyou'vegottenfor a barandthetotalheightofthevisualization, whichyoucankindofseeiswouldcorrespondtotheheightof a barthatwewouldbethinkingoff.
That's theonlything I wanttoknow.
I wanttonotethere.
Theotherthing I'llnotehereisthat I neededthe y scaletoknowwhatitsmaximumvalueisgoingtobe.
Andso, as a result, D threegivesmeonmaxfunction, whichliterallyiteratedthrougheverysingleiteminthecountydata, anditreturnslikeanarray, basicallyofallofthevaluesanditpicksthehighestvalue.
Could I havedonethisanotherway?
Absolutely.
Butsince D threegivesmethesetools, youshouldabsolutelyusethem.
Andwhen I appendonthatthing, tellmetheattributesthatithastohaveagain.
Byconvention, wetendtoupendaxesto G groups.
Thereasonwedothisisthatifweeverwanttoaddcolortoouraxes, makethingssmallerontheaxis, rotatetheaxis, it's mucheasiertodothatwithin a G groupthanitistoactuallytrytofindthepointertotheaccessitselfonceit's alreadyonthecanvas.
Um, andthere's thisimportantmethodhere, specificallyforaxescalled, uhnamedcall, where, inessence, you'vesaid, okay, I wantyoutoappendedto a G group.
I wantyoutogiveittheseclassessothat I canstyleitlater.
Andnow I actuallywantyoutocallthismethodandmakemyaccess.
So D threeisalso a lotabout, like, playingaroundandfiguringouthowyoucanrotatethingsandshiftthingstomakethemlookpretty.
So, forexample, if I wantedallofthoseartistsnamestoberotated 90 degreesfromthehorizontalsothattheywouldlookperpendiculartotheaccess, I cangoaheadandsetthisto 90 andsowecanseethatthatlooks a littlebitlikethat I thoughtthatthislookedsuperuglyandwas, like, hardtoread.
And I washopingthatthiswouldbeshortenoughtofitif I rotatedat 75 degrees.
Surprise, surprise.
Itwasnot, um, butaesthetically, I thoughtthat 75 looked a littlebitnicer.
Thisistheonlyreason I'vepicked 75.
Thisispurelyanaestheticchoice.
Okay, sowe'vetalkedabouttheaccess.
We'vetalkedaboutthescale.
Onceyou'veconfiguredthoseagain, therestisrelativelyeasybefore I moveontothebarsthemselvesquestionsaboutaxesorscales, because I thinkthosearetheharderthings.
Butifyouhavenoquestions, that's alsofine.
Awesome.
Okay, Bars.
Itissorememberthat I saidthe D threekindoflikestobreakdownwhatyourvisualizationactuallyis.
Ifyourdatacallsforthatandbecause I havenorectangles, mydataisobviouslygoingtocallfornewrectanglestobemade.
So I'm goingtosay, OK, we'regoingtoupendtheserectanglesandwe'regonnagivethemsomeattributeslike a classofPhil.
Andthenthingsstarttoget a littlebitfunnywhere a lotofthethingsthatwehavetothinkaboutourhowwhydomyrectangleshavetobeHowtalldomyrectangleshavetobeAndwheredo I startdrawingmyrectanglesagain d three.
Buthowfardown I havetodrawRememberthatallofthesevaluesairactuallyinverted.
Sowhen I talkaboutwith, infact, I couldjustusethisnicelittlemethodthatcomesrightoutofmyscale, wheremyscalehasalreadytakeninto a factittakenintoaccounthowmanybars I'm goingtoneedandwhatthespacingbetweenthebarsisgoingtobeonwhatthetotalwithithastoworkwithisandsoas a resultitcanveryeasilygiveme a calculationoffhowwideoneparticularbarhastobe.
Andso I didn't evenhavetothinkaboutthis.
I couldliterallyjustsayHey, justtellmewhat?
ThebandwithISSfor a particularbar.
Greatheightistheonethat's a littlebitfunnyagainbecausewehavetoinvert.
So I said, OK, foreveryoneofthepiecesofdatathatyou'regoingtogetfromthisdatathat I'vegivenyou, Pleaselookatthatdata.
Rememberthatthis Y positionisstillgoingtohavetocorrespondtowhateverthescalehastoldme, right?
Thescaleisgoingtotellmerelativetomyaccess.
Wheredoessomethingwith a countofthreehavetostart?
Wheredoessomethingwithaccountof 21 havetostart?
Andsoagain, I letmyscalehandleallofthis.
Samethinggoesforthe X, where I'vesaidOkay, theamounttotherightthat I havetoshiftwithinmycanvasisreallygoingtodependoneveryotherpieceofdatathat I alreadyhave.
Andso, as a result, I againleavethistomyscaletofigureoutthismathwouldbereallyHarrytofigureoutyourself.
Andso I cannotextolenoughthevirtuesofthe D threescales.
Um, noticehereagainthatwhat I havesaidisactuallyokay, actually, thisintaxes a littlebitfunny.
You'llnoticethat I'm usingthisartistmapagain, whichseems a littlebitweirdbecausewekindofalreadyhavethisartistkey.
Thereason I hadtodoitthiswaywasthefollowing.
I saidtomyscalethatthedomainmeaninganyoftheinputsitwasgoingtoget, we'reactuallygoingtobeartistsnames.
Andso, as a result, for a particularpieceofdata, ratherthanhandthescaleanartist I d whichwouldcomebackundefinedbecausethisdomainhasnoidea.
We'resorry.
Wrongone.
Thisdomainhasnoideawhat a onecorrespondstoorwhatarethreecorrespondsto.
Butitdoesknowwhatan a c d c correspondstoorSantanacorrespondsto.
Andso, as a result, as I'm iteratingthroughallofmydataandcountdatamakingthesebarswhen I actuallyneedtousemyexorginalscaletofigureouttheexpositionofmybars, I needtopassit a nameandnotjustan i d.
Forexample, listenersInthiscase, we'regoingtosay, OK, let's actuallyat a clicklistener.
Thereareotherlisteners, like, mouseovermouthsoff.
Um, thesearethingsthat I highlyrecommendGoogling, buttheyworkandkindofjusttheintuitivewaythatyoumightexpectbuttonstoworkonwithjustregularJavaScripton a regularHTMLpagewhere I said, okay, ifsomeoneeverclickson a particularbar, pleasenotethatforthatparticularbar.
I wantyoutogetwhateverisgoingtogetreturnedfromthisfunctioncall.
I couldhavewrittenthisentirefunctioninhere, butforthesakeofclarityandsegmenting, I madethedecisiontoactuallysegmentitawayintoanotherfunction.
Incase, forexample, myapplicationevergrewand I wantedtouseitonotherplacesonthepage.
Butnoticewhat I'vepassedistheArtemartistname, theartist I d.