vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

26 lines
71 KiB

11 years ago
11 years ago
11 years ago
  1. /**
  2. * vis.js
  3. * https://github.com/almende/vis
  4. *
  5. * A dynamic, browser-based visualization library.
  6. *
  7. * @version 0.0.5
  8. * @date 2013-04-23
  9. *
  10. * @license
  11. * Copyright (C) 2011-2013 Almende B.V, http://almende.com
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  14. * use this file except in compliance with the License. You may obtain a copy
  15. * of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  22. * License for the specific language governing permissions and limitations under
  23. * the License.
  24. */
  25. function DataSet(t){var e=this;this.options=t||{},this.data={},this.fieldId=this.options.fieldId||"id",this.fieldTypes={},this.options.fieldTypes&&util.forEach(this.options.fieldTypes,function(t,i){e.fieldTypes[i]="Date"==t||"ISODate"==t||"ASPDate"==t?"Date":t}),this.subscribers={},this.internalIds={}}function Stack(t,e){this.parent=t,this.options={order:function(t,e){return e.width-t.width||t.left-e.left}},this.ordered=[],this.setOptions(e)}function Range(t){this.id=util.randomUUID(),this.start=0,this.end=0,this.options={min:null,max:null,zoomMin:null,zoomMax:null},this.setOptions(t),this.listeners=[]}function Controller(){this.id=util.randomUUID(),this.components={},this.repaintTimer=void 0,this.reflowTimer=void 0}function Component(){this.id=null,this.parent=null,this.depends=null,this.controller=null,this.options=null,this.frame=null,this.top=0,this.left=0,this.width=0,this.height=0}function Panel(t,e,i){this.id=util.randomUUID(),this.parent=t,this.depends=e,this.options={},this.setOptions(i)}function RootPanel(t,e){this.id=util.randomUUID(),this.container=t,this.options={autoResize:!0},this.listeners={},this.setOptions(e)}function TimeAxis(t,e,i){this.id=util.randomUUID(),this.parent=t,this.depends=e,this.dom={majorLines:[],majorTexts:[],minorLines:[],minorTexts:[],redundant:{majorLines:[],majorTexts:[],minorLines:[],minorTexts:[]}},this.props={range:{start:0,end:0,minimumStep:0},lineTop:0},this.options={orientation:"bottom",showMinorLabels:!0,showMajorLabels:!0},this.conversion=null,this.range=null,this.setOptions(i)}function ItemSet(t,e,i){this.id=util.randomUUID(),this.parent=t,this.depends=e,this.options={style:"box",align:"center",orientation:"bottom",margin:{axis:20,item:10},padding:5};var n=this;this.data=null,this.range=null,this.listeners={add:function(t,e){n._onAdd(e.items)},update:function(t,e){n._onUpdate(e.items)},remove:function(t,e){n._onRemove(e.items)}},this.items={},this.queue={},this.stack=new Stack(this),this.conversion=null,this.setOptions(i)}function Item(t,e,i){this.parent=t,this.data=e,this.selected=!1,this.visible=!0,this.dom=null,this.options=i}function ItemBox(t,e,i){this.props={dot:{left:0,top:0,width:0,height:0},line:{top:0,left:0,width:0,height:0}},Item.call(this,t,e,i)}function ItemPoint(t,e,i){this.props={dot:{top:0,width:0,height:0},content:{height:0,marginLeft:0}},Item.call(this,t,e,i)}function ItemRange(t,e,i){this.props={content:{left:0,width:0}},Item.call(this,t,e,i)}function Timeline(t,e,i){var n=this;if(this.options={orientation:"bottom",zoomMin:10,zoomMax:31536e10,moveable:!0,zoomable:!0},this.controller=new Controller,!t)throw Error("No container element provided");this.main=new RootPanel(t,{autoResize:!1,height:function(){return n.timeaxis.height+n.itemset.height}}),this.controller.add(this.main);var o=moment().hours(0).minutes(0).seconds(0).milliseconds(0);this.range=new Range({start:o.clone().add("days",-3).valueOf(),end:o.clone().add("days",4).valueOf()}),this.range.subscribe(this.main,"move","horizontal"),this.range.subscribe(this.main,"zoom","horizontal"),this.range.on("rangechange",function(){n.controller.requestReflow()}),this.range.on("rangechanged",function(){n.controller.requestReflow()}),this.timeaxis=new TimeAxis(this.main,null,{orientation:this.options.orientation,range:this.range}),this.timeaxis.setRange(this.range),this.controller.add(this.timeaxis),this.itemset=new ItemSet(this.main,[this.timeaxis],{orientation:this.options.orientation}),this.itemset.setRange(this.range),e&&this.setData(e),this.controller.add(this.itemset),this.setOptions(i)}var util={};util.isNumber=function(t){return t instanceof Number||"number"==typeof t},util.isString=function(t){return t instanceof String||"string"==typeof t},util.isDate=function(t){if(t instanceof Date)return!0;if(util.isString(t)){var e=ASPDateRegex.exec(t);if(e)return!0;if(!isNaN(Date.parse(t)))return!0}return!1},util.isDataTable=function(t){return"undefined"!=typeof google&&google.visualization&&google.visualization.DataTable&&t instanceof google.visualization.DataTable},util.randomUUID=function(){var t=function(){
  26. },Component.prototype.getContainer=function(){return null},Component.prototype.getFrame=function(){return this.frame},Component.prototype.repaint=function(){return!1},Component.prototype.reflow=function(){return!1},Component.prototype.requestRepaint=function(){if(!this.controller)throw Error("Cannot request a repaint: no controller configured");this.controller.requestRepaint()},Component.prototype.requestReflow=function(){if(!this.controller)throw Error("Cannot request a reflow: no controller configured");this.controller.requestReflow()},Component.prototype.on=function(t,e){if(!this.parent)throw Error("Cannot attach event: no root panel found");this.parent.on(t,e)},Panel.prototype=new Component,Panel.prototype.getContainer=function(){return this.frame},Panel.prototype.repaint=function(){var t=0,e=util.updateProperty,i=util.option.asSize,n=this.options,o=this.frame;if(o||(o=document.createElement("div"),o.className="panel",n.className&&("function"==typeof n.className?util.addClassName(o,n.className()+""):util.addClassName(o,n.className+"")),this.frame=o,t+=1),!o.parentNode){if(!this.parent)throw Error("Cannot repaint panel: no parent attached");var r=this.parent.getContainer();if(!r)throw Error("Cannot repaint panel: parent has no container element");r.appendChild(o),t+=1}return t+=e(o.style,"top",i(n.top,"0px")),t+=e(o.style,"left",i(n.left,"0px")),t+=e(o.style,"width",i(n.width,"100%")),t+=e(o.style,"height",i(n.height,"100%")),t>0},Panel.prototype.reflow=function(){var t=0,e=util.updateProperty,i=this.frame;return i?(t+=e(this,"top",i.offsetTop),t+=e(this,"left",i.offsetLeft),t+=e(this,"width",i.offsetWidth),t+=e(this,"height",i.offsetHeight)):t+=1,t>0},RootPanel.prototype=new Panel,RootPanel.prototype.setOptions=function(t){util.extend(this.options,t),this.options.autoResize?this._watch():this._unwatch()},RootPanel.prototype.repaint=function(){var t=0,e=util.updateProperty,i=util.option.asSize,n=this.options,o=this.frame;if(o||(o=document.createElement("div"),o.className="graph panel",n.className&&util.addClassName(o,util.option.asString(n.className)),this.frame=o,t+=1),!o.parentNode){if(!this.container)throw Error("Cannot repaint root panel: no container attached");this.container.appendChild(o),t+=1}return t+=e(o.style,"top",i(n.top,"0px")),t+=e(o.style,"left",i(n.left,"0px")),t+=e(o.style,"width",i(n.width,"100%")),t+=e(o.style,"height",i(n.height,"100%")),this._updateEventEmitters(),t>0},RootPanel.prototype.reflow=function(){var t=0,e=util.updateProperty,i=this.frame;return i?(t+=e(this,"top",i.offsetTop),t+=e(this,"left",i.offsetLeft),t+=e(this,"width",i.offsetWidth),t+=e(this,"height",i.offsetHeight)):t+=1,t>0},RootPanel.prototype._watch=function(){var t=this;this._unwatch();var e=function(){return t.options.autoResize?(t.frame&&(t.frame.clientWidth!=t.width||t.frame.clientHeight!=t.height)&&t.requestReflow(),void 0):(t._unwatch(),void 0)};util.addEventListener(window,"resize",e),this.watchTimer=setInterval(e,1e3)},RootPanel.prototype._unwatch=function(){this.watchTimer&&(clearInterval(this.watchTimer),this.watchTimer=void 0)},RootPanel.prototype.on=function(t,e){var i=this.listeners[t];i||(i=[],this.listeners[t]=i),i.push(e),this._updateEventEmitters()},RootPanel.prototype._updateEventEmitters=function(){if(this.listeners){var t=this;util.forEach(this.listeners,function(e,i){if(t.emitters||(t.emitters={}),!(i in t.emitters)){var n=t.frame;if(n){var o=function(t){e.forEach(function(e){e(t)})};t.emitters[i]=o,util.addEventListener(n,i,o)}}})}},TimeAxis.prototype=new Component,TimeAxis.prototype.setOptions=function(t){util.extend(this.options,t)},TimeAxis.prototype.setRange=function(t){if(!(t instanceof Range||t&&t.start&&t.end))throw new TypeError("Range must be an instance of Range, or an object containing start and end.");this.range=t},TimeAxis.prototype.toTime=function(t){var e=this.conversion;return new Date(t/e.factor+e.offset)},TimeAxis.prototype.toScreen=function(t){var e=this.conversion;return(t.valueOf()-e.offset)*e.factor},TimeAxis.prototype.repaint=function(){var t=0,e=util.updateProperty,i=util.option
  27. },_ordinal:"%d",preparse:function(t){return t},postformat:function(t){return t},week:function(t){return _(t,this._week.dow,this._week.doy)},_week:{dow:0,doy:6}},O=function(t,e,i){return C({_i:t,_f:e,_l:i,_isUTC:!1})},O.utc=function(t,e,i){return C({_useUTC:!0,_isUTC:!0,_l:i,_i:t,_f:e})},O.unix=function(t){return O(1e3*t)},O.duration=function(t,e){var i,n=O.isDuration(t),o="number"==typeof t,s=n?t._data:o?{}:t;return o&&(e?s[e]=t:s.milliseconds=t),i=new r(s),n&&t.hasOwnProperty("_lang")&&(i._lang=t._lang),i},O.version=Y,O.defaultFormat=J,O.lang=function(e,i){return e?(i?p(e,i):k[e]||d(e),O.duration.fn._lang=O.fn._lang=d(e),t):O.fn._lang._abbr},O.langData=function(t){return t&&t._lang&&t._lang._abbr&&(t=t._lang._abbr),d(t)},O.isMoment=function(t){return t instanceof o},O.isDuration=function(t){return t instanceof r},O.fn=o.prototype={clone:function(){return O(this)},valueOf:function(){return+this._d},unix:function(){return Math.floor(+this._d/1e3)},toString:function(){return this.format("ddd MMM DD YYYY HH:mm:ss [GMT]ZZ")},toDate:function(){return this._d},toJSON:function(){return O.utc(this).format("YYYY-MM-DD[T]HH:mm:ss.SSS[Z]")},toArray:function(){var t=this;return[t.year(),t.month(),t.date(),t.hours(),t.minutes(),t.seconds(),t.milliseconds()]},isValid:function(){return null==this._isValid&&(this._isValid=this._a?!c(this._a,(this._isUTC?O.utc(this._a):O(this._a)).toArray()):!isNaN(this._d.getTime())),!!this._isValid},utc:function(){return this._isUTC=!0,this},local:function(){return this._isUTC=!1,this},format:function(t){var e=g(this,t||O.defaultFormat);return this.lang().postformat(e)},add:function(t,e){var i;return i="string"==typeof t?O.duration(+e,t):O.duration(t,e),u(this,i,1),this},subtract:function(t,e){var i;return i="string"==typeof t?O.duration(+e,t):O.duration(t,e),u(this,i,-1),this},diff:function(t,e,i){var n,o,r=this._isUTC?O(t).utc():O(t).local(),s=6e4*(this.zone()-r.zone());return e&&(e=e.replace(/s$/,"")),"year"===e||"month"===e?(n=432e5*(this.daysInMonth()+r.daysInMonth()),o=12*(this.year()-r.year())+(this.month()-r.month()),o+=(this-O(this).startOf("month")-(r-O(r).startOf("month")))/n,"year"===e&&(o/=12)):(n=this-r-s,o="second"===e?n/1e3:"minute"===e?n/6e4:"hour"===e?n/36e5:"day"===e?n/864e5:"week"===e?n/6048e5:n),i?o:a(o)},from:function(t,e){return O.duration(this.diff(t)).lang(this.lang()._abbr).humanize(!e)},fromNow:function(t){return this.from(O(),t)},calendar:function(){var t=this.diff(O().startOf("day"),"days",!0),e=-6>t?"sameElse":-1>t?"lastWeek":0>t?"lastDay":1>t?"sameDay":2>t?"nextDay":7>t?"nextWeek":"sameElse";return this.format(this.lang().calendar(e,this))},isLeapYear:function(){var t=this.year();return 0===t%4&&0!==t%100||0===t%400},isDST:function(){return this.zone()<O([this.year()]).zone()||this.zone()<O([this.year(),5]).zone()},day:function(t){var e=this._isUTC?this._d.getUTCDay():this._d.getDay();return null==t?e:this.add({d:t-e})},startOf:function(t){switch(t=t.replace(/s$/,"")){case"year":this.month(0);case"month":this.date(1);case"week":case"day":this.hours(0);case"hour":this.minutes(0);case"minute":this.seconds(0);case"second":this.milliseconds(0)}return"week"===t&&this.day(0),this},endOf:function(t){return this.startOf(t).add(t.replace(/s?$/,"s"),1).subtract("ms",1)},isAfter:function(e,i){return i=i!==t?i:"millisecond",+this.clone().startOf(i)>+O(e).startOf(i)},isBefore:function(e,i){return i=i!==t?i:"millisecond",+this.clone().startOf(i)<+O(e).startOf(i)},isSame:function(e,i){return i=i!==t?i:"millisecond",+this.clone().startOf(i)===+O(e).startOf(i)},zone:function(){return this._isUTC?0:this._d.getTimezoneOffset()},daysInMonth:function(){return O.utc([this.year(),this.month()+1,0]).date()},dayOfYear:function(t){var e=I((O(this).startOf("day")-O(this).startOf("year"))/864e5)+1;return null==t?e:this.add("d",t-e)},isoWeek:function(t){var e=_(this,1,4);return null==t?e:this.add("d",7*(t-e))},week:function(t){var e=this.lang().week(this);return null==t?e:this.add("d",7*(t-e))},lang:function(e){return e===t?this._lang:(this._lang=d(e),this)}},N=0;G.length>N;N++)L(G[N].toLowerCase().