@figlinq/react-chart-editor
Version:
plotly.js chart editor react component UI
2 lines • 6.17 kB
JavaScript
function ownKeys(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);r&&(o=o.filter(function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable})),t.push.apply(t,o)}return t}function _objectSpread(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?ownKeys(Object(t),!0).forEach(function(r){_defineProperty(e,r,t[r])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):ownKeys(Object(t)).forEach(function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))})}return e}function _defineProperty(e,r,t){return(r=_toPropertyKey(r))in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e}function _toPropertyKey(t){var i=_toPrimitive(t,"string");return"symbol"==typeof i?i:i+""}function _toPrimitive(t,r){if("object"!=typeof t||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var i=e.call(t,r||"default");if("object"!=typeof i)return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===r?String:Number)(t)}import{Component}from"react";import Field from"./Field";import Dropdown from"../widgets/Dropdown";import NumericInput from"../widgets/NumericInput";import PropTypes from"prop-types";import{connectToContainer}from"../../lib";import{isDateTime}from"plotly.js/src/lib";import{isJSDate}from"plotly.js/src/lib/dates";import{jsx as _jsx,jsxs as _jsxs}from"react/jsx-runtime";const MILLISECONDS_IN_SECOND=1000;const MILLISECONDS_IN_MINUTE=MILLISECONDS_IN_SECOND*60;const MILLISECONDS_IN_DAY=MILLISECONDS_IN_MINUTE*60*24;const DAYS_IN_MONTH=30;const MONTHS_IN_YEAR=12;const twoDecimalsRound=value=>Math.round(value*100)/100;const getSmallestUnit=milliseconds=>{const units={seconds:MILLISECONDS_IN_SECOND,minutes:MILLISECONDS_IN_MINUTE,days:MILLISECONDS_IN_DAY};let smallestUnit="milliseconds";["seconds","minutes","days"].forEach(unit=>{if(milliseconds%units[unit]===0&&(smallestUnit==="milliseconds"||smallestUnit!=="milliseconds"&&milliseconds/units[smallestUnit]>milliseconds/units[unit])){smallestUnit=unit}});return smallestUnit};export class UnconnectedAxisInterval extends Component{constructor(props){super(props);const initialUnit=props.fullValue&&typeof props.fullValue==="string"&&props.fullValue[0]==="M"?parseInt(props.fullValue.substring(1),10)%MONTHS_IN_YEAR===0?"years":"months":getSmallestUnit(props.fullValue);this.state={units:initialUnit}}update(value){let adjustedValue=value<0?0:value;const isValueInteger=adjustedValue%1===0;if(this.state.units==="years"){if(isValueInteger){adjustedValue="M"+adjustedValue*MONTHS_IN_YEAR}else{adjustedValue=adjustedValue*MONTHS_IN_YEAR*DAYS_IN_MONTH*MILLISECONDS_IN_DAY}}if(this.state.units==="months"){if(isValueInteger){adjustedValue="M"+adjustedValue}else{adjustedValue=adjustedValue*DAYS_IN_MONTH*MILLISECONDS_IN_DAY}}if(this.state.units==="days"){adjustedValue=adjustedValue*MILLISECONDS_IN_DAY}if(this.state.units==="minutes"){adjustedValue=adjustedValue*MILLISECONDS_IN_MINUTE}if(this.state.units==="seconds"){adjustedValue=adjustedValue*MILLISECONDS_IN_SECOND}this.props.updatePlot(adjustedValue)}onUnitChange(value){const isFullValueMonthFormat=typeof this.props.fullValue==="string"&&this.props.fullValue[0]==="M";const milliseconds=isFullValueMonthFormat?parseInt(this.props.fullValue.substring(1),10)*DAYS_IN_MONTH*MILLISECONDS_IN_DAY:this.props.fullValue;this.setState({units:value});if(["years","months"].includes(value)){const nbMonths=milliseconds/MILLISECONDS_IN_DAY/DAYS_IN_MONTH;if(nbMonths%1===0){this.props.updatePlot("M"+nbMonths)}else{this.props.updatePlot(milliseconds)}}else{this.props.updatePlot(milliseconds)}}getDisplayValue(value){const numericValue=typeof value==="string"&&value[0]==="M"?parseInt(value.substring(1),10):value;if(this.state.units==="years"){if(typeof value==="string"){return twoDecimalsRound(numericValue/MONTHS_IN_YEAR)}return twoDecimalsRound(numericValue/MILLISECONDS_IN_DAY/DAYS_IN_MONTH/MONTHS_IN_YEAR)}if(this.state.units==="months"){if(typeof value==="string"){return twoDecimalsRound(numericValue)}return twoDecimalsRound(numericValue/MILLISECONDS_IN_DAY/DAYS_IN_MONTH)}if(this.state.units==="days"){return twoDecimalsRound(numericValue/MILLISECONDS_IN_DAY)}if(this.state.units==="minutes"){return twoDecimalsRound(numericValue/MILLISECONDS_IN_MINUTE)}if(this.state.units==="seconds"){return twoDecimalsRound(numericValue/MILLISECONDS_IN_SECOND)}if(this.state.units==="milliseconds"){return numericValue}return null}render(){const _=this.context.localize;const attrHead=this.props.attr.split(".")[0];const binStartValue=this.props.fullContainer[attrHead].start;const BinStartIsDate=typeof binStartValue==="string"&&(isDateTime(binStartValue)||isJSDate(binStartValue));const tick0=this.props.fullContainer.tick0&&(this.props.fullContainer.tick0||this.props.fullContainer.colorbar.tick0);const tick0IsDate=tick0&&(isDateTime(tick0)||isJSDate(tick0));return BinStartIsDate||tick0IsDate?_jsxs(Field,_objectSpread(_objectSpread({},this.props),{},{children:[_jsx(Dropdown,{options:[{value:"years",label:_("Years")},{value:"months",label:_("Months")},{value:"days",label:_("Days")},{value:"minutes",label:_("Minutes")},{value:"seconds",label:_("Seconds")},{value:"milliseconds",label:_("Milliseconds")}],clearable:false,onChange:value=>this.onUnitChange(value),value:this.state.units}),_jsx("div",{style:{width:"100%",display:"block"},children:" "}),_jsx(NumericInput,{value:this.getDisplayValue(this.props.fullValue),onUpdate:value=>this.update(value),editableClassName:"AxisInterval-milliseconds"})]})):_jsx(Field,_objectSpread(_objectSpread({},this.props),{},{children:_jsx(NumericInput,{value:this.props.fullValue,onUpdate:value=>this.props.updatePlot(value)})}))}}UnconnectedAxisInterval.contextTypes={localize:PropTypes.func};UnconnectedAxisInterval.propTypes=_objectSpread({fullValue:PropTypes.oneOfType([PropTypes.number,PropTypes.string]),updatePlot:PropTypes.func,attr:PropTypes.string,fullContainer:PropTypes.object},Field.propTypes);export default connectToContainer(UnconnectedAxisInterval);
//# sourceMappingURL=AxisInterval.js.map