UNPKG

@domoinc/multiline-chart

Version:

MultiLineChart - Domo Widget

75 lines (62 loc) 2.99 kB
//---------------------------------------------------------------------------------- //---------------------------------------------------------------------------------- // ClipPath Generator: // Example: // usmChart._clip = d3.DomoClipPath(); // usmChart._clip.appendNewDefsAndDomoClipPath(usmChart._height,usmChart._width,usmChart.base); // usmChart.base.attr("clip-path", "url(#" + usmChart._clip.getTheRecentlyAppendedDomoClipPathsId() + ")"); // //---------------------------------------------------------------------------------- //---------------------------------------------------------------------------------- d3.DomoClipPath = function() { var CLIP_ID = "DomoClipPath"; //********************************************************************************** // Searches Dom for a clipId. Return the clipId + (number of id's found plus one) // Thus generating a unique clipId. //********************************************************************************** function generateNewUniqueDomoClipPathId() { var numClipIds = d3.selectAll("[id^="+CLIP_ID+"]").size(); return CLIP_ID + "_" + numClipIds; } //---------------------------------------------------------------------------------- //---------------------------------------------------------------------------------- var clipIt = { clipsId: null, recentRect: null, //********************************************************************************** // Appends a <defs> and <clippath> to the container. //********************************************************************************** appendNewDefsAndDomoClipPath: function (height, width, container) { clipIt.clipsId = generateNewUniqueDomoClipPathId(); clipIt.recentRect = container.append("defs") .append("clipPath") .attr("id", clipIt.clipsId) .append("rect") .attr("x", 0) .attr("y", 0); clipIt.updateHeightAndWidthOfClippingRect(height, width); return clipIt; }, //********************************************************************************** // Return the id that was generated during the last call to // appendNewDefsAndDomoClipPath //********************************************************************************** getTheRecentlyAppendedDomoClipPathsId: function () { return clipIt.clipsId; }, //********************************************************************************** // This function will update the cliping rectangle height and width. //********************************************************************************** updateHeightAndWidthOfClippingRect: function (height, width) { clipIt.height = (height < 0 ? 0 : height); clipIt.width = (width < 0 ? 0 : width); if (clipIt.recentRect == null) {console.log("clipIt recentRect not set."); return;} clipIt.recentRect.attr("height", clipIt.height) .attr("width", clipIt.width); }, }; return clipIt; };