From 420aa027b305c91380d96d05e39db2767ec1333a Mon Sep 17 00:00:00 2001 From: Jukka Kurkela Date: Tue, 16 Mar 2021 14:49:19 +0200 Subject: [PATCH] Exclude base-line from bar size (#8648) * Exclude base-line from bar size * lint * add to types --- src/controllers/controller.bar.js | 12 +++++ src/core/core.scale.js | 15 +++++++ .../controller.bar/baseLine/bottom.js | 40 +++++++++++++++++ .../controller.bar/baseLine/bottom.png | Bin 0 -> 901 bytes test/fixtures/controller.bar/baseLine/left.js | 41 +++++++++++++++++ .../fixtures/controller.bar/baseLine/left.png | Bin 0 -> 647 bytes .../fixtures/controller.bar/baseLine/mid-x.js | 41 +++++++++++++++++ .../controller.bar/baseLine/mid-x.png | Bin 0 -> 758 bytes .../fixtures/controller.bar/baseLine/mid-y.js | 40 +++++++++++++++++ .../controller.bar/baseLine/mid-y.png | Bin 0 -> 930 bytes .../fixtures/controller.bar/baseLine/right.js | 42 ++++++++++++++++++ .../controller.bar/baseLine/right.png | Bin 0 -> 611 bytes test/fixtures/controller.bar/baseLine/top.js | 41 +++++++++++++++++ test/fixtures/controller.bar/baseLine/top.png | Bin 0 -> 895 bytes .../controller.bar/baseLine/value-x.js | 42 ++++++++++++++++++ .../controller.bar/baseLine/value-x.png | Bin 0 -> 943 bytes .../controller.bar/baseLine/value-y.js | 41 +++++++++++++++++ .../controller.bar/baseLine/value-y.png | Bin 0 -> 951 bytes test/specs/controller.bar.tests.js | 10 +++-- types/index.esm.d.ts | 6 +++ 20 files changed, 367 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/controller.bar/baseLine/bottom.js create mode 100644 test/fixtures/controller.bar/baseLine/bottom.png create mode 100644 test/fixtures/controller.bar/baseLine/left.js create mode 100644 test/fixtures/controller.bar/baseLine/left.png create mode 100644 test/fixtures/controller.bar/baseLine/mid-x.js create mode 100644 test/fixtures/controller.bar/baseLine/mid-x.png create mode 100644 test/fixtures/controller.bar/baseLine/mid-y.js create mode 100644 test/fixtures/controller.bar/baseLine/mid-y.png create mode 100644 test/fixtures/controller.bar/baseLine/right.js create mode 100644 test/fixtures/controller.bar/baseLine/right.png create mode 100644 test/fixtures/controller.bar/baseLine/top.js create mode 100644 test/fixtures/controller.bar/baseLine/top.png create mode 100644 test/fixtures/controller.bar/baseLine/value-x.js create mode 100644 test/fixtures/controller.bar/baseLine/value-x.png create mode 100644 test/fixtures/controller.bar/baseLine/value-y.js create mode 100644 test/fixtures/controller.bar/baseLine/value-y.png diff --git a/src/controllers/controller.bar.js b/src/controllers/controller.bar.js index 0d4176f27..cbac98e51 100644 --- a/src/controllers/controller.bar.js +++ b/src/controllers/controller.bar.js @@ -447,6 +447,18 @@ export default class BarController extends DatasetController { head = base + size; } + const actualBase = baseValue || 0; + if (base === vScale.getPixelForValue(actualBase)) { + const halfGrid = vScale.getLineWidthForValue(actualBase) / 2; + if (size > 0) { + base += halfGrid; + size -= halfGrid; + } else if (size < 0) { + base -= halfGrid; + size += halfGrid; + } + } + return { size, base, diff --git a/src/core/core.scale.js b/src/core/core.scale.js index f62ec10e9..2da297290 100644 --- a/src/core/core.scale.js +++ b/src/core/core.scale.js @@ -1606,6 +1606,21 @@ export default class Scale extends Element { } } + getLineWidthForValue(value) { + const me = this; + const grid = me.options.grid; + if (!me._isVisible() || !grid.display) { + return 0; + } + const ticks = me.ticks; + const index = ticks.findIndex(t => t.value === value); + if (index >= 0) { + const opts = grid.setContext(me.getContext(index)); + return opts.lineWidth; + } + return 0; + } + /** * @protected */ diff --git a/test/fixtures/controller.bar/baseLine/bottom.js b/test/fixtures/controller.bar/baseLine/bottom.js new file mode 100644 index 000000000..272b8e99b --- /dev/null +++ b/test/fixtures/controller.bar/baseLine/bottom.js @@ -0,0 +1,40 @@ +module.exports = { + config: { + type: 'bar', + data: { + labels: ['a', 'b'], + datasets: [{ + backgroundColor: '#AAFFCC', + borderColor: '#0000FF', + borderWidth: 1, + data: [1, 2] + }] + }, + options: { + scales: { + x: { + display: false + }, + y: { + ticks: { + display: false + }, + grid: { + color: function(context) { + return context.tick.value === 0 ? 'red' : 'transparent'; + }, + lineWidth: 5, + tickLength: 0 + }, + } + }, + maintainAspectRatio: false + } + }, + options: { + canvas: { + width: 128, + height: 128 + } + } +}; diff --git a/test/fixtures/controller.bar/baseLine/bottom.png b/test/fixtures/controller.bar/baseLine/bottom.png new file mode 100644 index 0000000000000000000000000000000000000000..d7107153bb098da80a029fc2f58084bb8c2415f4 GIT binary patch literal 901 zc-rd>@N?(olHy`uVBq!ia0vp^4Is?H1|$#LC7xzrVD|BJaSW-5dpp-KPufv{<@Bb`HCDG6V9)c^=0Kbv8(7++ktoM=H9iPbZOf!b|DKsg(`=W z+kEx2M11TY@cjRu`+wKIc>8bZpMGuIf5$F=`bSfCh73~%2ahyQh8ee6?=dm7q)cOJ z*ctmkcT|vg!8MO7&NXe{{ipQHW#$CFhKAg4{b3!Jz_{dOIdJ{!-(MS>SID`2f(5xb zqeJR5RsjY^1NDSB#?8OCp7}SMp`n4{#@=>?3-!VW_AoIqs0c9JTaW@eALXo<;t& z=mN8Tm41Bk{pn=WY&+&sZUz>Xh97|%FXI1d?BQhK$Y3&I+;aVanuBnH-vJw@^zXHD z4z(OyU=Mv~+S0CYV1E_UBIgHD$6ud+{Jpj1tHvz(+u!Z8bN9ddT(#%lf93}fuRZ^J o{iw~dd+g8IV7%>L|MUHiw_iEYp~`z4n7tT0UHx3vIVCg!0NOWjp#T5? literal 0 Hc-jL100001 diff --git a/test/fixtures/controller.bar/baseLine/left.js b/test/fixtures/controller.bar/baseLine/left.js new file mode 100644 index 000000000..56a68ce54 --- /dev/null +++ b/test/fixtures/controller.bar/baseLine/left.js @@ -0,0 +1,41 @@ +module.exports = { + config: { + type: 'bar', + data: { + labels: ['a', 'b'], + datasets: [{ + backgroundColor: '#AAFFCC', + borderColor: '#0000FF', + borderWidth: 1, + data: [1, 2] + }] + }, + options: { + indexAxis: 'y', + scales: { + y: { + display: false + }, + x: { + ticks: { + display: false + }, + grid: { + color: function(context) { + return context.tick.value === 0 ? 'red' : 'transparent'; + }, + lineWidth: 5, + tickLength: 0 + }, + } + }, + maintainAspectRatio: false + } + }, + options: { + canvas: { + width: 128, + height: 128 + } + } +}; diff --git a/test/fixtures/controller.bar/baseLine/left.png b/test/fixtures/controller.bar/baseLine/left.png new file mode 100644 index 0000000000000000000000000000000000000000..ca5e1227b5cad1b5f4e8b172859e23fd53a1d834 GIT binary patch literal 647 zc-rd>@N?(olHy`uVBq!ia0vp^4Is?H1|$#LC7xzrVDj^HaSW-5dwcg_;UNbB){8f1 zcrZ*V$m?@^@>rpO^X8_)smd>ov#wix-`ZB$VYi%d|IezmGr#`5S^bzxhry)jU;b2v zjOX@1L**8nY2XU*t8Zg^^plx^;T`i5NsH=+-!ae4qCX_9`Equ>a6|v=)OWY5xf-^Y z-n^~A@Ma~m!qI;_BSaloE%I1aGH5l(DV$(n4PanpG2f?Fu%GGJf%yBZ8BDWF9rzcp zUtl)*{%>OzP;mfb2tzs3W*(rz0Ee>Q*-U@s8kjVI=zz$CR4{AP?fW7LTniXL$bdCP z62xkF^Ua)f23TDKlMiDg8;GT_`>!wKG6sm!V25dpAl8Yz`rrmlgpmghFVZ|P(eUQm z_eUlaeW{-(_@yRcLbGzQi-n1n_|Ml(j0_CE2{Rc*7tGnGsW8uufx&^X5EQ_{4iG^# z12jQA&gV0XR0vZ3M^ nP$laT$%v0%pHJs+uwa^RWSQa|^x<_SP@KWj)z4*}Q$iB}WjpQQ literal 0 Hc-jL100001 diff --git a/test/fixtures/controller.bar/baseLine/mid-x.js b/test/fixtures/controller.bar/baseLine/mid-x.js new file mode 100644 index 000000000..ef6beb5c9 --- /dev/null +++ b/test/fixtures/controller.bar/baseLine/mid-x.js @@ -0,0 +1,41 @@ +module.exports = { + config: { + type: 'bar', + data: { + labels: ['a', 'b'], + datasets: [{ + backgroundColor: '#AAFFCC', + borderColor: '#0000FF', + borderWidth: 1, + data: [1, -1] + }] + }, + options: { + indexAxis: 'y', + scales: { + y: { + display: false + }, + x: { + ticks: { + display: false + }, + grid: { + color: function(context) { + return context.tick.value === 0 ? 'red' : 'transparent'; + }, + lineWidth: 5, + tickLength: 0 + }, + } + }, + maintainAspectRatio: false + } + }, + options: { + canvas: { + width: 128, + height: 128 + } + } +}; diff --git a/test/fixtures/controller.bar/baseLine/mid-x.png b/test/fixtures/controller.bar/baseLine/mid-x.png new file mode 100644 index 0000000000000000000000000000000000000000..1f4feb2e36b0b554ca6279bec8fab61b1bc2a599 GIT binary patch literal 758 zc-rd>@N?(olHy`uVBq!ia0vp^4Is?H1|$#LC7xzrVA|{H;uum9_xA4X!ovnUE`cl5 z{Qtd`GkU43yM%LhW5^_>7Z;laRn#bah$3*YT#*Lj5uKUbR&b9z;TyC#??%5o8m5Ply_onRQSNq*uU%A+INM^ z8(DLr{}u1O^7x;mYD53aMS@q#=P)eVvY&^EPt1XVhMoF#MP9VCB;NA(zM4RJag+NXTRXR{-k@rej(P z53kHm`m*swEJNe%_|SibDR;kHym(op&cO6xx}nj5i~pQ{Ge{`>@l#+FZ~#V5f&~j( l*1z);l21D@MT9@#T&+35Ig{ZA4=_D5c)I$ztaD0e0s!p{6HovE literal 0 Hc-jL100001 diff --git a/test/fixtures/controller.bar/baseLine/mid-y.js b/test/fixtures/controller.bar/baseLine/mid-y.js new file mode 100644 index 000000000..d01c00a12 --- /dev/null +++ b/test/fixtures/controller.bar/baseLine/mid-y.js @@ -0,0 +1,40 @@ +module.exports = { + config: { + type: 'bar', + data: { + labels: ['a', 'b'], + datasets: [{ + backgroundColor: '#AAFFCC', + borderColor: '#0000FF', + borderWidth: 1, + data: [1, -1] + }] + }, + options: { + scales: { + x: { + display: false + }, + y: { + ticks: { + display: false + }, + grid: { + color: function(context) { + return context.tick.value === 0 ? 'red' : 'transparent'; + }, + lineWidth: 5, + tickLength: 0 + }, + } + }, + maintainAspectRatio: false + } + }, + options: { + canvas: { + width: 128, + height: 128 + } + } +}; diff --git a/test/fixtures/controller.bar/baseLine/mid-y.png b/test/fixtures/controller.bar/baseLine/mid-y.png new file mode 100644 index 0000000000000000000000000000000000000000..88c21a153742db4bcb547760cd33dd99dbc2cedb GIT binary patch literal 930 zc-rd>@N?(olHy`uVBq!ia0vp^4Is?H1|$#LC7xzrV9xY(aSW-5dplRxk2z7IWxucM z(Kr_Grq%*ZmF3PwoJ`YLPP)$W)jGAv@J&nCOg{gwIrn~69tnGEvgH0QuT2&gO^!x# z8LNnHneX4e{P1y^XMe-3`#*mziYXI+dh?<_gGChoE=~r=WQ#Qn4U4}nn9U$y(sr0@ zRG55WA%^5N+t0uLt#{)4vZ?JapJn&FEDio~p4FZq;`ojW>s$VR7Cqm=&yYUl`UedL zlQk7?Yz!8Q_q4e)Iz)WTz0@N?(olHy`uVBq!ia0vp^4Is?H1|$#LC7xzrVAAq*aSW-5dwZ9=m&sAU^?`LJU!>+g5x!ykyYRzQo4wBVm)IA)VQ47&Rb2Ns zv|$q?!>O0689shtWN_f_kY;$mn=t=!!JFbYk@j^QR>AjwZ+pM;&-FKtcj+KT!=@J& z4bMLA)!R4o_wOqV3=ght*u*HhV9q}8#gASxGcvXM$jrcy zz^x%2z)=0I%|U#3&X0D|L01B8~`@r z-x**$hy|D}V48CIIui+2P*35HRSZwt1#N#nKfP#izd~J(xEsTRQ!^lHwUbeu)ELhC ZgVj=C|NQyJq5FYC44$rjF6*2UngIVB<30cY literal 0 Hc-jL100001 diff --git a/test/fixtures/controller.bar/baseLine/top.js b/test/fixtures/controller.bar/baseLine/top.js new file mode 100644 index 000000000..86a7a378d --- /dev/null +++ b/test/fixtures/controller.bar/baseLine/top.js @@ -0,0 +1,41 @@ +module.exports = { + config: { + type: 'bar', + data: { + labels: ['a', 'b'], + datasets: [{ + backgroundColor: '#AAFFCC', + borderColor: '#0000FF', + borderWidth: 1, + data: [-1, -2] + }] + }, + options: { + scales: { + x: { + display: false + }, + y: { + ticks: { + display: false + }, + grid: { + color: function(context) { + return context.tick.value === 0 ? 'red' : 'transparent'; + }, + borderWidth: 0, + lineWidth: 5, + tickLength: 0 + }, + } + }, + maintainAspectRatio: false + } + }, + options: { + canvas: { + width: 128, + height: 128 + } + } +}; diff --git a/test/fixtures/controller.bar/baseLine/top.png b/test/fixtures/controller.bar/baseLine/top.png new file mode 100644 index 0000000000000000000000000000000000000000..8472c0fbf0db6915ff137f0e74781229679ddaa9 GIT binary patch literal 895 zc-rd>@N?(olHy`uVBq!ia0vp^4Is?H1|$#LC7xzrV0QC#aSW-5dwcg_u85;Z%R|Y+ z2OByIR2c+11!OxFI1acAZPY1r=yqYc8?^Swwb|#xpWQSKtuNpIZrk+Fbu!-%Ur&{_ zU@W-#=KtOwVhr~E{>R((^F#gq{;IRKled>!RQc)md~3eF`~Oz{{r0QqcICIoc|WUu z|M>R%?OkUR|GIjH906_y;{!J&FYqL=MOYhDa>eZbTj*0)Z+w7_k>LXK3&tGg^t@(W z#%T>(nI~UA@bz{!lY99=h8qP3cnrR4#PAn^)h6$>o87Za6jn$ z-{_RkhItI``OSR&8tC)>x;SgU7DECRx_?&rdzCJQTR0zx)2L9W}lWjxCsbhnW$iMy+Rj# z1>z6%65iR}z-~x6<2$B1tZTrA#5U}1IDVH^=J?((`)Am1pLOTz4xm}KM;1(NeEXlT z;TwzdUj`1|9DR0&>ZXNt3@vQirZX?FJs4om;KY3UG~o5Bve~6+!E&b%B*0rVXq9sTh6v$i~>?O;zqWy$9}#4{v-O&^!$Q( ctL1--pZjvFVdQ&MBb@0IpbhJOBUy literal 0 Hc-jL100001 diff --git a/test/fixtures/controller.bar/baseLine/value-x.js b/test/fixtures/controller.bar/baseLine/value-x.js new file mode 100644 index 000000000..557a89863 --- /dev/null +++ b/test/fixtures/controller.bar/baseLine/value-x.js @@ -0,0 +1,42 @@ +module.exports = { + config: { + type: 'bar', + data: { + labels: ['a', 'b'], + datasets: [{ + backgroundColor: '#AAFFCC', + borderColor: '#0000FF', + borderWidth: 1, + data: [1, 3] + }] + }, + options: { + base: 2, + indexAxis: 'y', + scales: { + y: { + display: false + }, + x: { + ticks: { + display: false + }, + grid: { + color: function(context) { + return context.tick.value === 2 ? 'red' : 'transparent'; + }, + lineWidth: 5, + tickLength: 0 + }, + } + }, + maintainAspectRatio: false + } + }, + options: { + canvas: { + width: 128, + height: 128 + } + } +}; diff --git a/test/fixtures/controller.bar/baseLine/value-x.png b/test/fixtures/controller.bar/baseLine/value-x.png new file mode 100644 index 0000000000000000000000000000000000000000..1fd0161a55f50e2850408d52b9af838f45f45e3f GIT binary patch literal 943 zc-rd>@N?(olHy`uVBq!ia0vp^4Is?H1|$#LC7xzrU@r4?aSW-5dppNa&oxlQ^?kMh z=i&v1EK|GgarhiH5#gF_AYi4u;A4y1r%#_}Z!aq<-{kW9`|bLaSxAG%y`P6g6aGoM zN^3cYy#Mo%OJMHBD!XG9-F8XwKTcaslm0KTh5ymS@b%BH^UM2x?|&b@(fti~-OgA& z(FFO!$?L-!SZ*(}XVq?C`k~BFet`FY;#al@5prIi+#Qb;$Ql&XrMB42KTGH2R43TJF)2tRHlkKWn~p}K@CuocYcEo z5VORaJ2b5_cbK*cXvp=!qCD#=?}K*N&qgAg2liK5UF`2(-Z|~5ro+txFLrS`2 zZrId#u~f7{Eo$0whE(RoueBI_x9IpYo?>&m9?CE)BXS8)pF|eqB$ULmE`S>x&A9K_OAG1wUy?7_{8!~UvQzZI?=LAf z_4}Iq6BfUl*1eEHA()jR(acTL;qjFN=eQUS<@5wK99!8qSCpaUro>W)-e6{HZ3d3b zhF*+P%h}4p7zEOir!blL@xEEZpm64x25ZI~sT+}u4l{ZKxh`0kZrIG!U?9C%WIj;BIPCbxIRd4{Zo7Xw`u9l%8jyl%NTNnn}2ID@NCibW4xut_C1tA yA|rYZ(-vQzcdHo;0v>;2`D1EuzT%boABI=v6P%}ITM7a*B7>)^pUXO@geCx~`hw~J literal 0 Hc-jL100001 diff --git a/test/fixtures/controller.bar/baseLine/value-y.js b/test/fixtures/controller.bar/baseLine/value-y.js new file mode 100644 index 000000000..caee084df --- /dev/null +++ b/test/fixtures/controller.bar/baseLine/value-y.js @@ -0,0 +1,41 @@ +module.exports = { + config: { + type: 'bar', + data: { + labels: ['a', 'b'], + datasets: [{ + backgroundColor: '#AAFFCC', + borderColor: '#0000FF', + borderWidth: 1, + data: [1, 3] + }] + }, + options: { + base: 2, + scales: { + x: { + display: false + }, + y: { + ticks: { + display: false + }, + grid: { + color: function(context) { + return context.tick.value === 2 ? 'red' : 'transparent'; + }, + lineWidth: 5, + tickLength: 0 + }, + } + }, + maintainAspectRatio: false + } + }, + options: { + canvas: { + width: 128, + height: 128 + } + } +}; diff --git a/test/fixtures/controller.bar/baseLine/value-y.png b/test/fixtures/controller.bar/baseLine/value-y.png new file mode 100644 index 0000000000000000000000000000000000000000..15fe7871013270ca6760e0af47d0f139d1f37105 GIT binary patch literal 951 zc-rd>@N?(olHy`uVBq!ia0vp^4Is?H1|$#LC7xzrV6O9YaSW-5dppN5Dfa63wclUA^Sal=;I^70!68e|vEf-+Z_ZF> zR+=%*xwh-V|HHGdzAwAGX8LdY&GMBG-j=tEzfQY*YVH5OzA{W6aiz>00pTwz8wDQN z#Fbu#u-EMG+I#PA{jZuob*1-SEuUMrfA7z|*S>wsTz}&KE^UXm^HraRn^Y?@o%z&z zzmMUg=vlcAhRrpPw<|l;xNc@wI-qm^$6F48_=|5EISfvl*I6?i0V-D*I?AUpWY6`> zzb{*NZ28aU2lC^;ufKNdOwIHuZxZuO8RvfE|9$iLt!=V$uf3=Lk_~R~?md3Di-AeQ z>FIlpfVd4^?F?N3KdqUjh_891*q{{huaDs=>*@-Q1sqfND?3yk)RG%=O6wWZxZ9_u TSf@_}W=IB4S3j3^P6 extends El * @return {string} */ getLabelForValue(value: number): string; + + /** + * Returns the grid line width at given value + */ + getLineWidthForValue(value: number): number; + /** * Returns the location of the given data point. Value can either be an index or a numerical value * The coordinate (0, 0) is at the upper-left corner of the canvas -- 2.47.3