Skip to content

Instantly share code, notes, and snippets.

@RoxIv
Last active July 29, 2018 15:32
Show Gist options
  • Save RoxIv/742b7ed39a3e2ddfa54f39a874872b9d to your computer and use it in GitHub Desktop.
Save RoxIv/742b7ed39a3e2ddfa54f39a874872b9d to your computer and use it in GitHub Desktop.
Women's rights score by country
license: gpl-3.0

Say your dataset is an array of numbers, and includes both positive and negative values. Use two scales to construct the bar chart: a quantitative scale (such as a linear scale) to compute the bar positions along the x-axis, and an ordinal scale with rangeBands to compute the bar positions along the y-axis.

For the quantitative scale, compute the data domain (the minimum and maximum value) using d3.extent:

var x = d3.scale.linear()
    .domain(d3.extent(data, function(d) { return d.value; }))
    .range([0, width]);

Nicing the scale will extend the extent slightly to the nearest round numbers. If you want the zero-value to be centered in the middle of the canvas, take the greater of the minimum and maximum value by magnitude, or simply hard-code the desired domain.

For the y-axis, use rangeRoundBands to divide the vertical space into bands for each bar and specify the amount of padding between bars. The input (domain) to the ordinal scale is some identifying data—such as a name or a unique id. A simple such identifier is the data’s index:

var y = d3.scale.ordinal()
    .domain(data.map(function(d) { return d.name; }))
    .rangeRoundBands([0, height], .2);

Use both scales to position the bars. This is made slightly tricky in that SVG rects are positioned (the x and y attributes) by their top-left corner and cannot have a negative width or height. So, we must use the x- and y-scales to compute the position of the top-left corner, depending on whether the associated value is positive or negative: if the value is positive, then the data value determines the right edge of the bar, while if it’s negative, it determines the left edge of the bar. Hence the conditionals:

svg.selectAll(".bar")
    .data(data)
  .enter().append("rect")
    .attr("class", "bar")
    .attr("x", function(d) { return x(Math.min(0, d.value)); })
    .attr("y", function(d) { return y(d.name); })
    .attr("width", function(d) { return Math.abs(x(d.value) - x(0)); })
    .attr("height", y.rangeBand());

Lastly, you can add an axis to display tick marks on top. You might also compute a fill style (or even a gradient) to alter the differentiate the appearance of positive and negative values.

forked from mbostock's block: Bar Chart with Negative Values

forked from RoxIv's block: Bar Chart with Negative Values

forked from RoxIv's block: Bar Chart with Negative Values

forked from RoxIv's block: Bar Chart with Negative Values

name value
A -15
B -20
C -22
D -18
E 2
F 6
G 26
H 18
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.bar--positive {
fill: steelblue;
}
.bar--negative {
fill: darkorange;
}
.axis text {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 20, right: 30, bottom: 40, left: 30},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.ordinal()
.rangeRoundBands([0, height], 0.1);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickSize(0)
.tickPadding(6);
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.tsv("data.tsv", type, function(error, data) {
x.domain(d3.extent(data, function(d) { return d.value; })).nice();
y.domain(data.map(function(d) { return d.name; }));
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", function(d) { return "bar bar--" + (d.value < 0 ? "negative" : "positive"); })
.attr("x", function(d) { return x(Math.min(0, d.value)); })
.attr("y", function(d) { return y(d.name); })
.attr("width", function(d) { return Math.abs(x(d.value) - x(0)); })
.attr("height", y.rangeBand());
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + x(0) + ",0)")
.call(yAxis);
});
function type(d) {
d.value = +d.value;
return d;
}
</script>
�PNG

IHDR��}��^ iCCPicmH��WXS��[R -)�7Az�ދ�t�����T�袂kQ]Qt-�� {]QQ�ł �7I]����;Μs�?�����h��P%��y¨@fBb���l ���"�wdd�2��]�����f)�����*���  q
G�Ά��&[ �������bU!$�,�i2�%�)2l-�����2���� ���g��8
����{��Y�� �����"bӔ���-f�XL+m �r�
ُ'd������ߒ�%�C6j�0(J�3�[ufN�C��q~Jx�*_�q��|7];b���š�� P�a��B k�2ę��#ؖ%��B{4��3�S�9Q#��|~Vx�H�����Q\��G�ڤ��!�+ =R�/㉞��ŅC�q�(3:t��aA�o���P%�l��Ta@��S���Y�Yҹ�!��K� ��b \QB�(��_��p��#�0��|�F|�Y�#�X7+0JVg�(?zԷ3.0Y�G��H� /2F� �A�~� İ���xm� ��l$���.�ьz�KG��
��q�h��G:��P�eL+{Z�T�h��#<�8��=p7< >�`�ŝq�Q?���D�1�@4�����`޿хž ��p���-� ����p��E���ci����B�̙`2��F�K�1�Fmpc�����!�g������x�07����x�۷Z�8��������FX����1���~W#�C�Ėc��� v ;�5&v
k�Z�<�KW��lQRn�0o�ƺֺ���?fg�0J�7���Γl��!/-=�� ��\f0�m5�ikm����.�|�aH����7]�i\��2훎e��'��}����k '��ba�L�K���w����d ��� D��f����l�z��"Pր�` �v�j� �88.�+������/�x�!!4��h ��b��"Έ⏄!QH"���!|D��C� %�:d ��A~E�!g�KHr�F����'C��*���Qg� Ec��h���K�UhZ��C��3��څ�@1��c L�Ĝ1_,K�R1!�+�J�J�k���օ�cq"NǙ�%\�Ax,��s��J| ^�����kx7>�%�Z �+!��@H#�"J {G ����%�#� � � ��Dbq.q%q��4���C$�H$ �;)��"告H�I�H�H��^��<Y�lK '���Br)y/�$����<$�$g$�*!Ǒ�#�Zn�\��U�^�!�2ń�N��dPS�(u����7�����.�S�y�����_���HU��S}�Өb�*j�4�� �F3�yђhy�U��Y�C����B�Ga�B�B�B��KE9E#Eo�������*�+�)+�*��(�+S��4�LW�Q�P�V^��W���3�����
Ge��.��*=t�n@����K���齪DU�`� ����m�j*j�jqj����N�u10�1#���X�8ĸ��4N{��8����u�{�>^�K��^�~@���' ���F��Z��������Y���5�ǫ�w�_<����Z���V��\�]Z�Z��:ځ����g��u:^::tN����u=ty�tO�>g�1��Y�2�9怞�^��Xo�^�ސ��~�~���g�T� ����� ���5�3r6J7�d�b����8�x�q��3u�`��Z���4SO�\�J��fD3g�L�mf�樹�y�y��U ��тg�͢ca����� �,��ޖ�����V �0�B��� '&M\;�e�Wk�,����lTlBl
m�l^ۚ۲m�m������5ڽ�����W��v�;LvX�������Q�X���d���閳�s��J�.��.�]>�:��r����-�m�۳I&���vO�q�wg��t��`z${������dyVz>�2��x��z�m�����������{_W�����0�@�b�6�X�-���j��"�� ���� q
�r.��%�Q�y�0�i2:9d����Í���  "8b}ăH����ߦ�DN)��$�&j^TK4=zf���w1>1�c�ŚƊc�����Ľ���_ߕ01a~•D�D^bc)).iO��T����Ns�V4��t�鳧_��9#kƉ��3Y3'���&fE�*Y�)�)[Sؾ�M�/�Nם���4�=u]�4���i}����<_�ޫ�����3#2�2���d������U���s9:9�s:�"AW�k���a�p�M5�£N��T���;�#�<�ì�Y�g+���n�c>gŜ������6�ӛ�x^�|��; R4/4X�ta��EՋ)�3�^h]�����%MK��.Z��S�O�E
E¢[�ܖm_�/�-o[a�b�Ŝ��%�%�%�W�W^���粟�W��j[���b q �͵�k��)�+X׳~��� � ��n���R�}��M�M�M]eae�� 7���yK���>��jm]���6ζ�
������K����q{g���J���]�]��������/5{4����Růꪎ�>W�TS�Wk��Z�V\۷oھ��~��,�v`(9�>�5�כ�B5v>\w���֣����H��������Ǝc!ǚ�ܚ��f�[�q���'�N�>I9����S����Ϥ��i��|�l���禜k;z�⅀ g[�[N]t�x���c��/7\q�R���z�w�ߏ�9��_u�������1��d�g�k~�.\�~�F�����7oߚv��6���;Yw^�Ϳ;to�}���JJj=����]�]'���[E?����y�X��s��'�'�Ou��<�}v�/������//����T�s�KӗG����u a�������o4�T���<9��]�����4>Tt���)��ӡY�I�˾�}i����p��%dI�lhj*���%³��Qd�/� �;����ew4�8P�@�"���6#�����c�jg7�FD�jg+�E��‡��7����"�6<�e7${�ӹ�{�D�����\��N�X~�@�lH��w� pHYs%%IR$�bKGD�������!�IDATx��� �^e}����N&��2!{0�� Ȓ H��UH� �Q�
j��#B�(��F�*H9�R�j�T��)��)B�` d'��,���f^H1��t����3��z�y���w�}'��<�N��� 4hЦ4h� �@@��6 �@@�� �)���n� �\��<�ReS���x9W�@@�� �������7�ֵ���x�˩mC0�L �١�1��w� ��.K�N3�5hv������ҙ\��`J)m�Y���XY��̠x붵�x�4h� �@@4h� �@@4h�:y/�@@� Ky�Z�S[g�k ��؎�6��~��}� = �wY���4����Ӛͦ ���;�m��9oM���o�r Л�+��X���K'���n]�*����MP��/6/x<�~]�����|]Z�1�jj�W����%��D:q��߄���ү~�.��h�&eiъ��%�>3������t�#i�i@��@@l#O���#��S������=KSG��@@���Oz�E���L�����n�@@������������x�4h� �@@4h� �@@4h� ���ʦ���<Oy��E[���h�€�iȄ�w� ��.K �E4�m��҄��
�����*�f͎��7���4`�+�l�C� ��o��cG�;ZS��,�_(��rjې 3�>���t�S���2��^]�\�f�k&�}�sKz~Ej]���J)mI·�},{�P�����@@�� 4 �@@�� 4 �@@�� 𧐿�7��MP��T�Sk{�̻�9K�R]��k���ܖ>w[Z�!5��J�-�ӇH�L �'����t�^i�i���_��Ʀ�vw#��=`)��v���qCݻ���]����җ�H��G�M-�i�H���>��Ԩ<O���)oI>0�=*M������� Ԯ�Rڭ9M��u���i��T��� h���Rڰ%]�} GK{Z�T:�T�s�'�Ӊ{�߭�
�� �#�AS���GA ���w}����I+�� Л�%�����w� 4h��� 4h��� 4h(��)����?�����5���Ҁ��2����R�j ���Kc�M'ݚr�[(��rzd^�t� t_�������c�PW�؆�@@@ߨ�[8�Jgjy.��y�xU�˩mC0�LP����M�ujZ�P�\w^��,=�:�8�LP�]�L��I�vxU�\{K�3+j8�ӋBGC�U�0�.x73�� 4h@@�� 4h@@�� �O$�E��w�weS�h=g]_�n��3�>5I�� ����CY���NW�:mh�J�#wOg�� ��h��z��t��t��t�����������M �������NNJY7"�=*�mIy%e.? ���Q����cS��R������wnT���v��&�}Y�����di�������3�e��R�0<ݹ$��ܵ#����t�C� У<����|[:�����ӧ�.O2;E4@�&�HW��5�E���r��i�q~�
��!B�&u��<ed��^�䍮��=A���Q�BFǿ�_����;�p5�� �@@�� � �@@�� � �@@�� EU����[�l�aw@Mt�.5�ZW��uw�U��R�j @ tÈt�wR�-�̂������*�f� ��
��x  u�m��4�)�_�i˺�m��:�/�� i�03�����.�=����.K�N3�5���mc��[o��ږ�s{��_���@�nk64^ƕL� �@@�� h� �@@�� h� �@@�)��?R��(K흩������PN ��U�w�O �� �;��k�I�����r)��t��TR�l�[8���4�.}�m�c��F�Oݔ=��3��
4P���SS�sl�+�=vN�_�������v� Ԯ<��r����lN�����~J� �]wB� Ы,�� �Ҫ��'�^cSf�����Q�����������t�4� ���Q����zc�GAG@7=�{�d^����1颣^�N�oQ@@�&{1�_�&�"���� 4h@@�� 4h@@�� 4T��?yJY�W���5��0$ �2?:��W����"��M?-����K�@�/�G�J��@@�cـ!]���ǀ��@� w������^e)m�ұnsG{G{�_�ו�6(el��_���lѲg��3��G +y�����ޫi`����^�e����Zߒ��gqT*������4@���ey��b�g�����@@�� 4 �@@�� 4 �@@�� �I��)����֎�������8�� �z���~�W.Z����1����={����.3���A��?���3=�Ш�xg�ر� ���\�e�]��sώ��,����� �W�7o��׿>����Φ��3�<s��ɦ г�����>��C���(�ˣF�2'h���y�<mڴ}���l ��#˲͛7��X�`A�R��>��#;�03�+�T�U.��=��iӦEI���ŷ��]"��b
���||7S�k�r h� �@@��4h� �@@��4h� �@@��ʦ��y^ɻ��<O]� �
R�M ���坝}�ߏ4�Ԑef@@B����ċ?pp{G{�_�ו5�h�B�Sjl(o,�[���@@@��CI�^e)m�ұnsG��]�Yʆ �o��3���u�R��E˞���>���S�P���Q�g��l6 P�,۴�}����"�{-���-�����Х,�}Vs���=7��%" 4h��� 4h��� 4�'eSԬ��Ζ��<ϫߖJ�A��W3�����e�.��(溺�J�2a„s�9gذaf Ѓ�7FC����F���4hPcc�i@@�j�СӦM=zt���r���Μ �zV*�/^|�EU/<�p� ��iZ�=�󼹹���N1bD��<y�9@@�R@G:Ϙ1c��v2�J>� �i���[?�4�+inn~�{����d*x���Ԯ1cƜ|������h� �@@�� h� �@@�� h� �@@�� �M�+��w1�LP��h����P�<lP��i6z��r]C}��4�Rɏ�g��8����l�&KِA���tݘ��P�X��`+����ᇒ��Rڴ�c���@5�q@�i�g4�kW*e?[���k��)@��T��9���f*4�k�e٦-��ַ� �L@w�Ϸv� ��7t)��,3@���>�,�"�� 4h@@�� 4h@@�� ���)jVGGǪU��l�2r�ȡC�fYfN�=[�v��W_}�M7E@O�0��}�q�W*���e:;;���{��_��1c,Xp�e�����ӧO79h�m�X��;�;w�̙3��SN9e��� 23h��[����i�]w�T*7�p�<���0a�3�v���EY������������aÆ�}�݋-23h��=����W��U�T:���̙ ���-@-jnn>��/���˗�?���o��?�p3���A�e��v�رc��g�yf�}�9�3� �g�˳g�>��c�<Ϻ�4�vHg^+�� 4h@@�� 4h@@�� 4T���<�+yS��~/�� h�?���ꛇ �;;�P#:+��%����T*���L���w�� �v4������Q��<��lo �V" }�%z���iKǺ��������,3h�~�T�~�hٳ���S8�u�Y��k���T'���T�e����ZߒW4����ݧ�#��}C��,w= �^��Y�?�!h� �@@��4h� �@@��4h� �@@��kkkkmm��<^��x�4@�,Xp�u�U*�����wn����v�MP�V�\�裏n�v���� гR���޾~�����<���ڲ,3-h�^z��g�}v��-Z4}�t����Y�R�o���:�j@_~��[� ��<�G���{To�=zt�9����v����㵀��p�Q{���ѣ�>8x�G�9Ҵ �z�g������?ܜ�j��4h� �@@4h� �@@4h� �@@eS�
�<��]L��J�lih�~_�M ���坝f�Zg%2�>���ߪT���x�n�h7�:X_WR�h��+O���<��lo �V" ��>�l��o���[o��٬��������~�e1�rM}ȗQ�ȐK������ŬY�*��]��ۨ��Fݿq��zh� ��o<�[~jg!�`o�V;�����������9�f5�дQg��+�z�Qۨ��G]m�>����s�q�3k֬�|�35u�^���Q/]���{�=��s'M�d�Fm�6j��Q����g�}w��=� �@@��\_�=lذ��u]]]sss��^�6j�Q[��ڨmԵ0����������Qwtt�Y�f�ȑ�r٨��Fm�Vo�6ju-�:���A�����aÆ%K�ċ7�� C� ټy�U��#��N;��'NBb�˗/4h�ԩSb�K�.ݸq�I��^�h������C��I��ճ�xg�ر�C�T*�>�l,�X����M��|����ɓ��Uu��?�|����".��@[ZZb�Ƌ)S���] ��\ʭ��q�=zt�u]QZ�r�3�<3f̘�w޹��;�X��\=j?��s1�R�������T���n!��K�\nkk��;���=x��B.�H�e˖��u�ĉ�N-lݱEǒ���Ǐ�5<��u��E��R�$ہ?v���^�e˖�}��_�`�?��?DIǖ���|��c�-��z衇>��O�rzꩧ�>��~��?�������*��y��7�x����#��_C>������/������qԨQ��c�,ިc�~�G��⊋.�(v����ӟ���q衇�y���`�v?��O��x������������0������ ���{�c���������~��%K�đ+bbѢEs��9rdq�?���3g2%�����oEX�u�Y����_��׎1"V���B�����e�""#�N=�����=�\s�M7�4�[,�~�F�����ꫯ��:�������oݑ"_��W�<�L)��_�b�~����>��'�p�{���ub��������6�:q����?;�+V�~�����7��.�^��nx��x������F|����o�6gΜC9$[읣��7���� .�!q���n��_v�eq�ǡ�to���8-\�fM�ާ;�#vL_��W��1N��<��7��ME��H��C�^y�ի���)��������c����>;V����m��Z�---�_~y�d���ё3f̈�������{�[�K�`��z��K~��~�C���ʕ+�=��X�c
�S�X����q�a�������7���o;v�qh���.��ey�����u�؍7�x�i���]��������ͱ|�;�o�O?��]w�k�����w�wq�kx�cK�"����X v�ս�S�L���?�8ʼnM1�ޑ���'��7���w�N,�(�q��566f�bg��tP��p������<��gN�4)Δ�����9qn�x����-��q�P�������}-���[�y������ɣG�� ���_���ӆ�z(�=��s�1x`�S2N�O�>mڴo�!?��õ�5���!�̙�����իq&|ꩧ655͚5k޼yњ՛�d������'bԛ7oN�?iY�m�…���'�\�[���w��<p������z�ȕ���;�u|�u�x]WWw� '�&\]���ڵk �u�1"�y̘1���X���;"�F���>��TDa切�U��O��o|��t�̙���?�s�N���7�9���w�=:�[n�ꪫ��Qы���������{�]mt�����gc}2dH��U7�8a��WW�Q�N�[ݢch�[�4�j���+�ĉ/��һᄏ0d�|�߼��kW�Z�QWnu59rdu�T���xWnn���غ�󟷴����o��b �袋���)�i,�8��)qLE�������c����[�mmm��X�ll�y�⍺\.��z�wu�e'��������믿��C>ꨣb��կ~�k_��)��oƨ�?^�cY!t�w��ٱJ����O:�X�'L�P�;�gD˗/��w��裏�A9�i����=�dV��]�K����3�8��4�)V�w��q�9�3b/b ��b���X�����|��g�,6���/�w.���8�y��֬Ykm��Gs�ѷ0O��p�x����Q�����XA���P�k�>x�Ac�6l��;��X���O�2Dz��mmmŻrS}�*h�w�8�/}�K��<&dɒ%�ܞ�?�(����6�cǵ]ox�jj)�Tl�QTqV\�"���GǞ��ώ��c��u�z�؛o���\rɈ#���/ĩ�m������GZ��#K����… ?����F���|%6�իW~�~�g�~��h�8S�����ϟ�V=L�v����8{�����x˟��'?���/���]w�5�"�����g?���瞑�MMM�YM��z��V_�7m��<|��Xr�vFv�w�}���K���o-��S�N�;w��+��y睿��/�;��2cf͚U� r�n� �:�iӦ]��q�q���]�yw�غ���o.]�4��1��IU/���������/��舯1 ����ӆ�Y�[7�0dȐ�w����o�E�-�L�2����o݉�F=o޼O~�'N�z�C[��9B��+����c�X� �\�r„ +V���o;v��|�������� ��.�c�=����GzE����?.�><���n���dx����Ǐ���{�?��{�'����; �_zy�����J%�ޟ��g�<b$є�ׯ��.L@ou[[[������9s�!�Aq����' �G�={vaV͗_J�L��\����?���?����C=����oc�S]�9�8m��r,������<yr�vF/��Uq�Yg�]��/��/b���b�p@�E��>��=�ܳ:N�o����Yʩ�'���6�t�}�{_�.\���:w���N����Έ=�����8W,�#�---�V�z�����cdɥ�^z�1��"4h�n��x�F��
����n���8B���8���u���8a�>}�y�W��K.�$�3�9V옐�O?}���;���co�ٰaC�r��Y*�����OD~�MWE�X7m���I�����\�dI�9eʔ��Xбd P�'8�~���r��,�ϻc��J�FՃk�@,�� Dz.���Z�fM�$744�`����[���?�x��v�e��7�^�-���C��>��bŊȎ���W��E��͛#���3���:q��B~�[�t���1�귱VG�DI����X� ���1��{����z��1�5*r��[w�^���1���@˓O>9|��I�&����M���Lh� �@@��m
@@�� 4hS4h� �@@�� �@@@?��Z9 �4�IEND�B`�
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment