All Downloads are FREE. Search and download functionalities are using the official Maven repository.

js.models.charting.context.Model.js Maven / Gradle / Ivy

The newest version!
define(function(require){
	
	var View = require("text!./View.html"),
		$ = require("jquery"),
		MV = require("mv"),
		
		Moment = require("moment");
		
	return function(cfg){
		
		this.html = View;
		
		var self = this;
		
		cfg = $.extend({
			setFromDate: function(date){},
			setToDate: function(date){}
		}, cfg);
		
		this.selections = MV.observableArray();
		this.time = MV.observable();
		this.value = MV.observable();
		this.enabled = MV.observable(false);
		
		this.hover = function(x, y, series){
			self.time( new Date(x) );
			self.value(y + (series.unit ? " " + series.unit : ""));
		};
		
		this.select = function(x, y, series){
			if (self.enabled())
				self.selections.push({
					value: y,
					unit: series.unit,
					time: new Date(x),
					deltaValue: MV.observable(),
					deltaTime: MV.observable(),
					unitMismatch: MV.observable(false),
					setFrom: function(){
						cfg.setFromDate(new Date(x));
					},
					setTo: function(){
						cfg.setToDate(new Date(x));
					}
				});
		};
		
		/**
		 * Selection add/remove event
		 */
		self.selections.subscribe(function(){
			//Create delta infos
			var previous;
			self.selections().forEach(function(selection){
				if (previous){
					selection.deltaValue( selection.value - previous.value );
					selection.deltaTime( Moment.duration(selection.time - previous.time).humanize() );
					selection.unitMismatch( selection.unit != previous.unit );
				}
				
				previous = selection;
			});
		});
		
		/**
		 * Reset button
		 */
		this.reset = function(){
			self.selections.removeAll();
		};
		
		/**
		 * Remove button
		 */
		this.remove = function(selection){
			self.selections.remove(selection);
		};

	};
	
});




© 2015 - 2025 Weber Informatics LLC | Privacy Policy