UNPKG

victory-chart

Version:
155 lines (129 loc) 5.14 kB
Object.defineProperty(exports,"__esModule",{value:true});var _omit2=require("lodash/omit");var _omit3=_interopRequireDefault(_omit2);var _defaults2=require("lodash/defaults");var _defaults3=_interopRequireDefault(_defaults2);var _assign2=require("lodash/assign");var _assign3=_interopRequireDefault(_assign2); var _victoryCore=require("victory-core");function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj};}exports.default= { getScale:function getScale(props,fallbackProps){ props=_victoryCore.Helpers.modifyProps(props,fallbackProps,"bar");var _props= props,horizontal=_props.horizontal; var range={ x:_victoryCore.Helpers.getRange(props,"x"), y:_victoryCore.Helpers.getRange(props,"y")}; var domain={ x:_victoryCore.Domain.getDomainWithZero(props,"x"), y:_victoryCore.Domain.getDomainWithZero(props,"y")}; var xScale=_victoryCore.Scale.getBaseScale(props,"x").domain(domain.x).range(range.x); var yScale=_victoryCore.Scale.getBaseScale(props,"y").domain(domain.y).range(range.y); return{ x:horizontal?yScale:xScale, y:horizontal?xScale:yScale}; }, getBarWidth:function getBarWidth(props){var style=props.style,width=props.width,data=props.data; var padding=props.padding.left||props.padding; var defaultWidth=data.length===0?8:(width-2*padding)/data.length; return style&&style.width?style.width:defaultWidth; }, getBarPosition:function getBarPosition(props,datum,scale){ var defaultMin=_victoryCore.Scale.getType(scale.y)==="log"? 1/Number.MAX_SAFE_INTEGER:0; var y0=datum.y0||defaultMin; var formatValue=function formatValue(value,axis){ return datum[axis]instanceof Date?new Date(value):value; }; return{ x:scale.x(formatValue(datum.x1||datum.x,"x")), y0:scale.y(formatValue(y0,"y")), y:scale.y(formatValue(datum.y1||datum.y,"y"))}; }, getBarStyle:function getBarStyle(datum,baseStyle){ var styleData=(0,_omit3.default)(datum,[ "xName","yName","x","y","label","errorX","errorY","eventKey"]); return _victoryCore.Helpers.evaluateStyle((0,_defaults3.default)({},styleData,baseStyle),datum); }, getLabelStyle:function getLabelStyle(style,datum){ var labelStyle=(0,_defaults3.default)({},{ angle:datum.angle, textAnchor:datum.textAnchor, verticalAnchor:datum.verticalAnchor}, style); return _victoryCore.Helpers.evaluateStyle(labelStyle,datum); }, getLabel:function getLabel(props,datum,index){ return datum.label||(Array.isArray(props.labels)? props.labels[index]:_victoryCore.Helpers.evaluateProp(props.labels,datum)); }, getLabelAnchors:function getLabelAnchors(datum,horizontal){ var sign=datum.y>=0?1:-1; if(!horizontal){ return{ vertical:sign>=0?"end":"start", text:"middle"}; }else{ return{ vertical:"middle", text:sign>=0?"start":"end"}; } }, getlabelPadding:function getlabelPadding(style,datum,horizontal){ var defaultPadding=style.padding||0; var sign=datum.y<0?-1:1; return{ x:horizontal?sign*defaultPadding:0, y:horizontal?0:sign*defaultPadding}; }, getCalculatedValues:function getCalculatedValues(props){var theme=props.theme; var defaultStyles=theme&&theme.bar&&theme.bar.style?theme.bar.style:{}; var style=_victoryCore.Helpers.getStyles(props.style,defaultStyles,"auto","100%"); var data=_victoryCore.Data.getData(props); var scale=this.getScale(props); return{style:style,data:data,scale:scale}; }, getBaseProps:function getBaseProps(props,fallbackProps){ props=_victoryCore.Helpers.modifyProps(props,fallbackProps,"bar");var _getCalculatedValues= this.getCalculatedValues(props),style=_getCalculatedValues.style,data=_getCalculatedValues.data,scale=_getCalculatedValues.scale;var _props2= props,horizontal=_props2.horizontal,width=_props2.width,height=_props2.height,padding=_props2.padding; var childProps={parent:{scale:scale,width:width,height:height,data:data,style:style.parent}}; for(var index=0,len=data.length;index<len;index++){ var datum=data[index]; var eventKey=datum.eventKey||index; var position=this.getBarPosition(props,datum,scale); var dataProps=(0,_assign3.default)( { style:this.getBarStyle(datum,style.data), index:index, datum:datum, scale:scale, horizontal:horizontal, padding:padding, width:width, data:data}, position); childProps[eventKey]={ data:dataProps}; var text=this.getLabel(props,datum,index); if(text!==undefined&&text!==null||props.events||props.sharedEvents){ childProps[eventKey].labels=this.getLabelProps(dataProps,text,style); } } return childProps; }, getLabelProps:function getLabelProps(dataProps,text,calculatedStyle){var datum=dataProps.datum,data=dataProps.data,horizontal=dataProps.horizontal,x=dataProps.x,y=dataProps.y,y0=dataProps.y0,index=dataProps.index,scale=dataProps.scale; var labelStyle=this.getLabelStyle(calculatedStyle.labels,datum); var labelPadding=this.getlabelPadding(labelStyle,datum,horizontal); var anchors=this.getLabelAnchors(datum,horizontal); return{ style:labelStyle, x:horizontal?y+labelPadding.x:x+labelPadding.x, y:horizontal?x+labelPadding.y:y-labelPadding.y, y0:y0, text:text, index:index, scale:scale, datum:datum, data:data, horizontal:horizontal, textAnchor:labelStyle.textAnchor||anchors.text, verticalAnchor:labelStyle.verticalAnchor||anchors.vertical, angle:labelStyle.angle}; }};