chrt-dotplot
Version:
Dot Plot component for Chrt
3 lines (2 loc) • 5.86 kB
JavaScript
// chrt.io v0.0.8 Copyright 2022 chrt
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("chrt-object")):"function"==typeof define&&define.amd?define(["exports","chrt-object"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).chrt=t.chrt||{},t.chrt)}(this,(function(t,e){"use strict";function r(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var i=r(e);function a(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function s(t){return function(t){if(Array.isArray(t))return n(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||function(t,e){if(!t)return;if("string"==typeof t)return n(t,e);var r=Object.prototype.toString.call(t).slice(8,-1);"Object"===r&&t.constructor&&(r=t.constructor.name);if("Map"===r||"Set"===r)return Array.from(t);if("Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r))return n(t,e)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function n(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,i=new Array(e);r<e;r++)i[r]=t[r];return i}function o(t){return this.attr("radius",t)}function l(t){return this.attr("strokeWidth",t)}function c(t){return this.attr("orient",t)}function d(){return c.call(this,"horizontal")}var u=e.utils.isNull,f=e.utils.createSVG,h="#000";function p(){var t=this;i.default.call(this),this.type="series",this.attr("radius",3),this.attr("stroke",h),this.attr("fill",h),this.attr("strokeWidth",0),this.attr("strokeOpacity",1),this.attr("fillOpacity",1),this.attr("range",!1),this.attr("rangeWidth",3),this.attr("rangeColor",h),d.call(this),this._classNames=["chrt-dotplot"],this.getXScale=function(){return u(t.fields.x)&&(t.fields.x=t.parentNode.scales.x[t.scales.x].field),t.parentNode.scales.x[t.scales.x]},this.draw=function(){var r,i;if(e.cssDisplay.call(t,t.attr("display")()),(r=t.g.classList).remove.apply(r,s(t.g.classList)),(i=t.g.classList).add.apply(i,s(t._classNames)),!u(t._data)){if(u(t.fields.x)&&(t.fields.x=t.parentNode.scales.x[t.scales.x].field),u(t.fields.y)&&(t.fields.y=t.parentNode.scales.y[t.scales.y].field),t.attr("range")()){var n=t.attr("rangeWidth")(),o=t.attr("rangeColor")(),l="vertical"===t.attr("orient")()?"x":"y",c=t._data.reduce((function(e,r){var i,n="x"===l?t.fields.y:t.fields.x;e[r[t.fields[l]]]=e[r[t.fields[l]]]||(a(i={},l,r[t.fields[l]]),a(i,"data",[]),a(i,"field",l),a(i,"otherField",n),i),e[r[t.fields[l]]].data.push(r);var o=e[r[t.fields[l]]].data.map((function(t){return t[n]}));return e[r[t.fields[l]]].max=Math.max.apply(Math,s(o)),e[r[t.fields[l]]].min=Math.min.apply(Math,s(o)),e}),{});Object.values(c).forEach((function(e,r){var i=escape("range-".concat(e[l],"-").concat(r)),s=t.g.querySelector("[data-id='".concat(i,"']"));if(s||((s=f("line")).setAttribute("data-id",i),t.g.appendChild(s)),!u(t.parentNode.scales.x[t.scales.x])&&!u(t.parentNode.scales.y[t.scales.y])){var c,d=(a(c={},e.field,t.parentNode.scales[e.field][t.scales[e.field]](e[e.field])),a(c,"".concat(e.otherField,"Min"),t.parentNode.scales[e.otherField][t.scales[e.otherField]](e.min)),a(c,"".concat(e.otherField,"Max"),t.parentNode.scales[e.otherField][t.scales[e.otherField]](e.max)),c);"y"===l?(s.setAttribute("x1",isNaN(d["".concat(e.otherField,"Min")])?0:d["".concat(e.otherField,"Min")]),s.setAttribute("x2",isNaN(d["".concat(e.otherField,"Max")])?0:d["".concat(e.otherField,"Max")]),s.setAttribute("y1",isNaN(d[e.field])?0:d[e.field]),s.setAttribute("y2",isNaN(d[e.field])?0:d[e.field])):(s.setAttribute("y1",isNaN(d["".concat(e.otherField,"Min")])?0:d["".concat(e.otherField,"Min")]),s.setAttribute("y2",isNaN(d["".concat(e.otherField,"Max")])?0:d["".concat(e.otherField,"Max")]),s.setAttribute("x1",isNaN(d[e.field])?0:d[e.field]),s.setAttribute("x2",isNaN(d[e.field])?0:d[e.field])),s.setAttribute("stroke-width",n),s.setAttribute("stroke",o)}}))}t._data.forEach((function(e,r,i){var a=escape("circle-".concat(name,"-").concat(r)),s=t.g.querySelector("[data-id='".concat(a,"']"));if(s||((s=f("circle")).setAttribute("data-id",a),t.g.appendChild(s)),!u(t.parentNode.scales.x[t.scales.x])&&!u(t.parentNode.scales.y[t.scales.y])){var n=t.parentNode.scales.x[t.scales.x](e[t.fields.x]),o=t.parentNode.scales.y[t.scales.y](e[t.fields.y]),l=isNaN(n)?0:n,c=isNaN(o)?0:o,d=t.attr("radius")(e,r,i);e.anchorPoints={x:l,width:d,y:c,height:d,relativePosition:[0,0],directions:{x:1,y:1},alignment:{horizontal:"middle",vertical:"top"}},s.setAttribute("cx",l),s.setAttribute("cy",c),s.setAttribute("r",d),s.setAttribute("fill",t.attr("fill")(e,r,i)),s.setAttribute("fill-opacity",t.attr("fillOpacity")(e,r,i)||1),s.setAttribute("stroke",t.attr("stroke")(e,r,i)),s.setAttribute("stroke-width",t.attr("strokeWidth")(e,r,i)),s.setAttribute("stroke-opacity",t.attr("strokeOpacity")(e,r,i))}}))}return t.objects.forEach((function(t){return t.draw()})),t.parentNode}}p.prototype=Object.create(i.default.prototype),p.prototype.constructor=p,p.parent=i.default.prototype,p.prototype=Object.assign(p.prototype,{pointSize:o,size:o,radius:o,color:function(t){return this.attr("fill",t)},stroke:function(t){return this.attr("stroke",t)},width:l,strokeWidth:l,opacity:function(t){return this.attr("fillOpacity",t)},strokeOpacity:function(t){return this.attr("strokeOpacity",t)},range:function(){var t=!(arguments.length>0&&void 0!==arguments[0])||arguments[0];return this.attr("range",t)},rangeColor:function(t){return this.attr("rangeColor",t)},rangeWidth:function(t){return this.attr("rangeWidth",t)},orient:c,horizontal:d,vertical:function(){return c.call(this,"vertical")}}),t.chrtDotPlot=function(){return new p},Object.defineProperty(t,"__esModule",{value:!0})}));