Skip to content

Instantly share code, notes, and snippets.

@thadk
Created May 2, 2013 01:00
Show Gist options
  • Save thadk/5499501 to your computer and use it in GitHub Desktop.
Save thadk/5499501 to your computer and use it in GitHub Desktop.
Tributary inlet

This examples demonstrates how to use D3's brush component to implement focus + context zooming. Click and drag in the small chart below to pan or zoom.

{"description":"Tributary inlet","endpoint":"","display":"div","public":true,"require":[],"fileconfigs":{"README.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"index.html":{"default":true,"vim":false,"emacs":false,"fontSize":12},"sp500.csv":{"default":true,"vim":false,"emacs":false,"fontSize":12},"thumbnail.png":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"javascript.js":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01}
<!DOCTYPE html>
<meta charset="utf-8">
<style>
svg {
font: 10px sans-serif;
}
path {
fill: steelblue;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.brush .extent {
stroke: #fff;
fill-opacity: .125;
shape-rendering: crispEdges;
}
</style>
var margin = {top: 10, right: 10, bottom: 100, left: 40},
margin2 = {top: 430, right: 10, bottom: 20, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom,
height2 = 500 - margin2.top - margin2.bottom;
var parseDate = d3.time.format("%b %Y").parse;
var x = d3.time.scale().range([0, width]),
x2 = d3.time.scale().range([0, width]),
y = d3.scale.linear().range([height, 0]),
y2 = d3.scale.linear().range([height2, 0]);
var xAxis = d3.svg.axis().scale(x).orient("bottom"),
xAxis2 = d3.svg.axis().scale(x2).orient("bottom"),
yAxis = d3.svg.axis().scale(y).orient("left");
var brush = d3.svg.brush()
.x(x2)
.on("brush", brushed);
var area = d3.svg.area()
.interpolate("monotone")
.x(function(d) { return x(d.date); })
.y0(height)
.y1(function(d) { return y(d.price); });
var area2 = d3.svg.area()
.interpolate("monotone")
.x(function(d) { return x2(d.date); })
.y0(height2)
.y1(function(d) { return y2(d.price); });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
svg.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height);
var focus = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var context = svg.append("g")
.attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");
d3.csv("sp500.csv", function(error, data) {
data.forEach(function(d) {
d.date = parseDate(d.date);
d.price = +d.price;
});
x.domain(d3.extent(data.map(function(d) { return d.date; })));
y.domain([0, d3.max(data.map(function(d) { return d.price; }))]);
x2.domain(x.domain());
y2.domain(y.domain());
focus.append("path")
.datum(data)
.attr("clip-path", "url(#clip)")
.attr("d", area);
focus.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
focus.append("g")
.attr("class", "y axis")
.call(yAxis);
context.append("path")
.datum(data)
.attr("d", area2);
context.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height2 + ")")
.call(xAxis2);
context.append("g")
.attr("class", "x brush")
.call(brush)
.selectAll("rect")
.attr("y", -6)
.attr("height", height2 + 7);
});
function brushed() {
x.domain(brush.empty() ? x2.domain() : brush.extent());
focus.select("path").attr("d", area);
focus.select(".x.axis").call(xAxis);
}
date price
Jan 2000 1394.46
Feb 2000 1366.42
Mar 2000 1498.58
Apr 2000 1452.43
May 2000 1420.6
Jun 2000 1454.6
Jul 2000 1430.83
Aug 2000 1517.68
Sep 2000 1436.51
Oct 2000 1429.4
Nov 2000 1314.95
Dec 2000 1320.28
Jan 2001 1366.01
Feb 2001 1239.94
Mar 2001 1160.33
Apr 2001 1249.46
May 2001 1255.82
Jun 2001 1224.38
Jul 2001 1211.23
Aug 2001 1133.58
Sep 2001 1040.94
Oct 2001 1059.78
Nov 2001 1139.45
Dec 2001 1148.08
Jan 2002 1130.2
Feb 2002 1106.73
Mar 2002 1147.39
Apr 2002 1076.92
May 2002 1067.14
Jun 2002 989.82
Jul 2002 911.62
Aug 2002 916.07
Sep 2002 815.28
Oct 2002 885.76
Nov 2002 936.31
Dec 2002 879.82
Jan 2003 855.7
Feb 2003 841.15
Mar 2003 848.18
Apr 2003 916.92
May 2003 963.59
Jun 2003 974.5
Jul 2003 990.31
Aug 2003 1008.01
Sep 2003 995.97
Oct 2003 1050.71
Nov 2003 1058.2
Dec 2003 1111.92
Jan 2004 1131.13
Feb 2004 1144.94
Mar 2004 1126.21
Apr 2004 1107.3
May 2004 1120.68
Jun 2004 1140.84
Jul 2004 1101.72
Aug 2004 1104.24
Sep 2004 1114.58
Oct 2004 1130.2
Nov 2004 1173.82
Dec 2004 1211.92
Jan 2005 1181.27
Feb 2005 1203.6
Mar 2005 1180.59
Apr 2005 1156.85
May 2005 1191.5
Jun 2005 1191.33
Jul 2005 1234.18
Aug 2005 1220.33
Sep 2005 1228.81
Oct 2005 1207.01
Nov 2005 1249.48
Dec 2005 1248.29
Jan 2006 1280.08
Feb 2006 1280.66
Mar 2006 1294.87
Apr 2006 1310.61
May 2006 1270.09
Jun 2006 1270.2
Jul 2006 1276.66
Aug 2006 1303.82
Sep 2006 1335.85
Oct 2006 1377.94
Nov 2006 1400.63
Dec 2006 1418.3
Jan 2007 1438.24
Feb 2007 1406.82
Mar 2007 1420.86
Apr 2007 1482.37
May 2007 1530.62
Jun 2007 1503.35
Jul 2007 1455.27
Aug 2007 1473.99
Sep 2007 1526.75
Oct 2007 1549.38
Nov 2007 1481.14
Dec 2007 1468.36
Jan 2008 1378.55
Feb 2008 1330.63
Mar 2008 1322.7
Apr 2008 1385.59
May 2008 1400.38
Jun 2008 1280
Jul 2008 1267.38
Aug 2008 1282.83
Sep 2008 1166.36
Oct 2008 968.75
Nov 2008 896.24
Dec 2008 903.25
Jan 2009 825.88
Feb 2009 735.09
Mar 2009 797.87
Apr 2009 872.81
May 2009 919.14
Jun 2009 919.32
Jul 2009 987.48
Aug 2009 1020.62
Sep 2009 1057.08
Oct 2009 1036.19
Nov 2009 1095.63
Dec 2009 1115.1
Jan 2010 1073.87
Feb 2010 1104.49
Mar 2010 1140.45
?PNG

IHDR?x???tEXtSoftwareAdobe ImageReadyq?e<!9IDATx??}yt??u?7??}#??H??%R?ek?e?n?ĊS?4NS7u??4????n?????8=u???8??ȑ?8??z???I?;?}@ ?X��?}???̀ ?? ??IC3??7?w?????ϐ?d?4?#
?
?<?f??Z?z=44J ???q??E?????Bz?4J?L?.?a4]?????ܶm???????4h??????i_?????S?\??TN??r_??bI??z?ti??????~?0???????????9,x?R?2b=?$1'"?H?g8x????1?????.??pN?G??{????9?x2=?Z<??K{/?\tX͸??é?Ú??`??)x??i?s?}}?|???2??????P?\,??7^;q}?????V?n5??X3?��߽?!_di?o4?K=?r?k?ף?:??&pm(??L??p?˝#?Y?L??c???)y?????sy?,&\???????????K??$??l-֍֛?\Xb??}??Z?^?E{1k?] ???G?1??&?ʼ??ě_~?(??QXM???*??'1??%E̫??X1?????%?ĕ??????%??9??x?<?V??;?n????8}MTc2?8??????B??s??Gq?kv?y?{)o????(L??????7???Y??V?ҕKX?n+?W?=( cw"
c嚍h\V?T*????#A??F?@W|???????.??+?j5g]
?,??{?Q???;_?(?U9?Dj?Kd??v_?Iq[?U=?a??_?Ӧ??t??XLw?!1?q?SƤ???[? Gp{1?vW?o?QՈ?@e?(/?`xx?V,G?'"nj&S?Ѱ?
????$+c4&
n2W:??%pQ?RM̥?????p????B]?]?????G?۠XEƌ??b2?X????????D*?O|`?? ??G?@?B?3?N3?QV?Y^)??[Y5����ƖʝpX͓??{???֛^<?}???%?+}>|?{?1??e???iR.?Y,ᵡ D?EJEL??nwn?b???I??v????xdU??!??:TV?*?-?X'??j??K??Pd?M??r^wO??????=??
???%O????`t,?r????g??g?}?ɨ-????N??;z
?C݁???+u?mF+Bb&RE.????/Z?wtlҾ??]??nh?]????ʍuXMs???:ry?Ɵ{?s=#A8lfe??,u?LU??A>?+??bJ??mٝ??H\???UՆ??ƾ }x?t\6˼?=&? ??7?iUYZb???HJ?ٹ?i???????????+jøg$?bV?ͬ%x???a<??k\x?Ӎ?y??\s????]?o_~??-??????????hb?��???՞??c?????c??Z-? ????43???]X?Xy?{?|??6??M?y?#s!^ ???0Qy0cX?8\?Y??hT ?_(jܶ^?Ζ:??x??0?^Tn??^:?W?\E?P?;?y??_9܅?x??x???Y!!???5?1ƻ?8???
??CK?bNtkǿ???ܧ???:X��???2?????D ?Ax?Et!;?}?u4Χ?:SK$2???s~n{??/C?o??g???j?s",bA?????I~??4?K?n(Ǘ>??���Fq?~ ?L???]V???m
CN??D?H$?h??J?\?&????{:Qi5[{<??㮭ɠ?Lf??J??swߔ?????w??????7k?̡%?-?c?(A???*?G??bp3?E!T??N?ijTI??k[XVj?g
?X%|?CkK???7??? ?1????t")????2??9WI?ʬl{?W?? f?*V?2?z??:???????B3?.?:?Q?????<????8????mmQ?I????{???Z?U9e?E?v??:rG颗 1??v`?ю֣???AuY"b???x??4.]j?o????ؕ!???U*??Œ-??dd"?*~v?/??3h?qiN&\???????SJ??~6T8??]???? i?????~V?????G??? _??A???Xv??a?Ϟ?!(?x???~Ao
Mk???��4"?(ZQ????G?+???dt???|?*??M?Z????k? ????>???l? ??02Kʪ?o?yQ??????x$[?- 6?"???JB? y01b??}??l?A???.!W?as???BC??k ?+Q?????MU7???
?ן^??\4n?I??O??????2??????V<?\??y?
?K?a??|F?a??cFYM??????8ҥ?שR?<??%?=?=??k??x=(???;?\]%?cbB?T�� ?-???~|??v ??7Ɓ7???R???w??#z?Amf?ئ? ?OU??XJ 3????I??w?6???^:??}??? χ?A??f?T????{????ޏ?]wvyx?t????!?3???v??(?I??u2뭧??m1?M???J&¿?Ew?ɡ?3g<??bxf#????9???ʣX?X??Ov+???碨)????????????T?z?��?????????w?Vs?{???ߡ?@lY^??rgY?g[=8????o?[???^o?D?A??k4??Kf?m9?v??:?؟a??????"??`QG?Bʝ}jS㸫??b?x??&?:??WO Q-?3?i\???G?4??ga?ڞ ??e??;5%?O?L??h0??I???˷[?1????E?U??.��%?Q?Ƀܮ?Kb????}?w???O???????ڏN??5u????(?????܍QU?l?7?]c?'?]7??,(k??F??:)??9??MP46?l??f?:59?8-??'o?ߧ????D?,S??"1?\?<?6
?/댩 ??????[&4!XP?Utg?s??'n@R?>?1?)??S-??^
(w?v?|?????0??f?p???|Υ??ͽ?Y????E)+?+??ʫ/??~???&??aA]msΝ?,A2??8?^???OYoY?a??vˊj???߻ҝ?? ?ȴ?Ki?~?a?;? ?.??R?????E??J?<?~2Ñd*5Y?Ңr????~^ݢ???3iuBrr1??{???ئy?7?<???&e)??L?̞?d???s??.+?#
?D??#??:۰z? t??/X%~???X?f?5T??⑈??x?u??^[?ړ???k?"Ώ[t_Z?bi{d?lciz8?^+????Ϩ$k??k?Wc??(??N?oe????މ??j??z?????D?^?={v?je si K?????A??6??n?????8;????��\ f?t??`1???T?P ?* ???݀??? ???????5????????W,?ڭX?͙??????I??nݺ?tv,??g?ɓ?sA̞?1|w?!f???rS?hR.W? 1`B?gC̿??%??V8?????u???DCc?Ԗ?b]4??ҰX>v?FR3k,?ཋ?*????%??{,?????0duT?LzH':???
???fvyq?Zg?u?&???DZ?b????L?ڧ肛GN?S?PP?<cт?//ibr?cB?C??Ӿ????/??-?I??'?U+
r???XL?1?3׆ ?n?M??xIK2M?@??& ???Wf?&???:???:??Dw?qx?$?Ť%z? _3?????%???|??%??j?,t?7/???6?W?{Lu??/?~?????k̨qߑ?.??u?S[?h??sV?T???)??c?T ?G?4??z?{?p???|?Bf?I??p?n?????S??Ƀ?��??4?ɫ?j?c*??V??."_?yn???ҟ?~???}???ކ???(K?ղsIK?0?|?_?/??'?j(???????{`L??g{??R-^?`|PA???Tq#o?̐h.?????7?b
??#???L?1o{????C?b薵t??Sǖ?_q?Z?M?:??Ⳛ??c????Y/~??&??l?ȾCle?k?????37?w??r?5??|l???[
????\??&??1??sgN??Q]?@$?ʵ?J?Vv?b???����q??޼?#?ʰ??PhIMzkdɀן?b?????3??1??ΉJ|Έy??) ??кUB???ƒ???m?A??0??߂|*?F??K??U?Ί 6&W?L???&j?E????$????k??T?xW*]A?1?;ˢf?ILzM???y??I?lZ?fj???je54
9???O??Fݑ?????c|zNCc`?????G?Z???0OSCCcj???Xv?5??|?&???L?b2?P۠?"?물&???}`6
?u????@??????????????@1)???߃D??ɀh$???T?9?*ih<Hb?l?[???j?J??l??Sp?g}???FI?Ï??/?݆P(???4?W.?ZY
??&???2??t?????B'4441544415441544415441544415441544J
?
"? ??2? b?VVC?4?y???J? ?<????n
M.ttv?h??UC??s???b??R3¡??k?bZP]U? ?z?441?W?R??Q__?LfH????<A??????????????????IC7 ?????FQ?L??jh<pbv\n???.??4 ??ZY
?R ??lǎ?՘?X(?ke54J??O<??)ߤke54?????????????????????????????????????(5??{????Y^????I=444f?x?ȯ)?{p`?;ظ?CX?P#?CCz???H?gg"?E&???h4e??;?š+? ?R???k????njē??f?NL????{???h_?խhllB"?(jr\4?ٌ???|><?y3?????^Ī?$??L&?>}
?eL?͆L*?ts?x|???:l޲?D?T??hPL≜>)sݲ???y?u5a?X??֊??j,k\?T2U??R!?d?X,???v?ع????J???rM-Kn?UhljB2?,j??+?m<G{k+v<????\????U?zI?Z]?8?L&d??q??kll ?H?u???tv\Y?????lT?[s?/'Q??1??b?"!X?mv?LҪ.I!6 i??H$㨩??Q??????? ?IN????F|?٪?P?c?^???B?4?].???B????bd??`eee?;?9?\8???E>??'D7???\ic??\???T2K???V??? rNA-|?Ģ1L?
??"ON?M????^????84⩄???euL"?? ?Ҥ!
)??\eYC`6��??>???Ȩv?E?_R??P?e??=ӎ?????ݻ?a4?d??d?>?M??<?+mgq??UTոD[?Ѳ????~⩂nj?<???7Q%.z"c??U d???????<??C??u3??Z'F=chj?AJ??O?R?c??^Ş?????xT,{5?b^|?S??JG???~?(??F`??`??`?m?g>?|?c??{?????!??mV$?#??>????/???U?Tb??E?S?[બ@Ey??<?둂??~?N?w???\d@?
c??o|?7e??k&????!??F?6;<????߀^x???v??ǁ?X?? ?1 ??
?Q/??g??v?ݓ??w??T??<??hy??nV??(
DͰzE ??(,Ɣ?啖??Ts58Q?T?@0??Q<?A]m]?f???z?
DB?e5b?d?lE????EMM3?V??Bg2f?9????( ?A??2rI???Q???+?????T??H?(??b?҅???x?x+?/?/??iF?̻eݦ??:?EcK ???%?EI?Z?D.
?D2b???*+j???+TYӄ???"e��??E??W!?#? ?wz?zM???]D?pL??G&s?v1>?X??.??H7??\3?Ţ?oʹƳ1W?k???K?^?+??V???ܜ??T|?[?U? ?:}:r1??(??؟?*?F6???"??3?Q??hF?"??V?????l̕?qu??\??x?ʨ????L?o?o???SCC??�� 4J??y2??di?????Ł?~<??J5?vY?&1???eSo�� ?????]??R?MԔ??4h??@B???0M?j6*?!B?s?`@dl?z<?'?h?vbSK?.?T?i?; ?y?|?$6?+?????X?@?ӆm?kQ??,?5??+{Uk??j[???=??2? (?[??G<?BP?a5???/ F?,&?lfS?\W??>?^?/ȅ????ooQ??X ?,?'VQ???B?e??????? ?0??g8??B?AoX?IX?X,?6?-ה?U?? ?1?(B??ː?RiU֢?-k)?Q4_??g???=?1?ז???a??4T:`????ɮa?yB0???3a?ں ?J?f?,?<sm??????|?dM6??M3?9Ux`????1?q?Ŷ?? nT߇cI???U????q?{?8??/?M?.dm?v??܆j????ZQ?N?c??EI?W8?????5bQ*?V%?ޱ?Z[>??ׯ?a??V?"?ȷZc??5?G,?V???G0쏪??:??PT?qy?ׇ?(?t19>????ȹ???$'?c? ??f?ѐMV??/?L??e1g??D? "K??2??lf?zo ?PV?7E?+? ??t_????A?ն??^>??E??-?wH?S?<? ???tO]S?_zlY??v=#c??OY??hN?̯Vq????>??P?o_?\"???Z?<??5?ց?7? ???G}V
0?oBU?????????1䋈P????\͛kBR??
˃??
)R??E?g-?gIȿ?u?GS'??k??p??ͥ??ߌ???_ ^?)+??'??q??U?箉!??i?&??`V7?????g?o??L????!???????? +???"?????N??I^???5 ?_W*w ??~??DŽ_{r???n ?L?U|:?? ???GfE?Ke(܌K??k?Jgr?&??㒨??T??G?4??5?'???E?
ɂ?Ҙ????P7F?ʢ0f"?bmH???j$ ?rI????s???3r??~\äy??G~^$I~???ΗDs??l̖$?WoY?[^M????{????S?1]N?Q&???ăk??{K???LY?{?? z?hP ?}??k?%???t??{.ׅn<??Ř(??n?_?֓?C?????3q? ?v/d?5??????:?m#???Z???Y??L,?R????2l^^?j?
vq??rc]wpV\|?B&?J?dr
$/?y;????O??hЌ(???nY?򾨤???[۱?^Y8?_?X???>~]
*?L???p�� #?Jaf? ?E^??n??N
?Ѐ?? EL?B?l?6͍9?d5?+??3?D?|el??ia????b?g??f??ml???��C'?
x=I??!?*XR?q?G?rˊjx?1\𩐂?S?*?-???n?N???i??y?^j/?=|Vy??ZX??[+b1U?9??m????^?}?rZ?44fZC?J̋??˭?Qz?1?uk
?9t?g?Z?v??l`d@??????\ ??b?Z?,??r???=?Ҟ?ĭ1?Z??>-f?ۋ??|#??[?J&?? ??F?????[Tk?????Z?lQ'??]?????"^l޲ ?>ϔ?\ ???z?]GJ?l̕ק??;v>6kc??"?9???书fE&?Aak?+??k????????;;?```M???c??b՛?`??e(/????@ ?p8????&zn����iV?8.Ĺ?g???DEEŬ??L???߇U+W???_Hsr??t8f ???Ze9^??.???1}?A4??;{߅?j??dE"cT%(?6n??5?O????k?Fee?? ??+`J???C;
?sݳ?=????vQ&N?~?\??Z]??'??G?`?ND? ??X?��}?pa컊#??DUy
,?5?`q?m???N??G?c???p???#k???{?????????਩U?9c??z?+?? *s'B????هp??2"[.;??L?*?z?т??q?.u]C?ȕ?!W4&
5?O~??'u?/d?8zd?? ??<ҍPhuk??S?<׫q?Bk?T��?VwЃg>?,\?=?? ?~???~?h6cd4????3-????K?? ހ?PU?U?Ɗ???{؏??e
?f/?��??>^Ը7z??_??
F?1??{T\?=<0?P8?9?@8".m?16l?Q1?F?E??=????????????[\x?LeN??` ?r?"ႉ??G?=ЏeU????<?Dˊr??[[?\o??@???\?8?LD????k?*J???>?$,>6??????\??d??&ⱌ?iu5U2?(??Ω n?ALi?|m?GbR\?  ???F?8??vF՜?*???e?2?%X?$?4,b?m2???r????v??t[L&?i??W*M_H??2???@??1
?\?x$"oW?Uؘ?~??Oy
Fcf?]?b??&?i?HU??xe
-?v%2C?)?5(?5$D??pج???,"??lBCAK?P??(O{6??I?*??ʟ ??8W?ף?v???J?u/?I??XQ`’???q}"SFXd
?v???U???^X^?ׇ?)m2?v"??r?KIBQ?VW?} ˆ??_?r?t?)??IEND?B`?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment