diff --git a/lib/timeline/Graph2d.js b/lib/timeline/Graph2d.js index c4354df2..b94b9522 100644 --- a/lib/timeline/Graph2d.js +++ b/lib/timeline/Graph2d.js @@ -27,7 +27,7 @@ var configureOptions = require('./optionsGraph2d').configureOptions; */ function Graph2d (container, items, groups, options) { // if the third element is options, the forth is groups (optionally); - if (!(Array.isArray(groups) || groups instanceof DataSet) && groups instanceof Object) { + if (!(Array.isArray(groups) || groups instanceof DataSet || groups instanceof DataView) && groups instanceof Object) { var forthArgument = options; options = groups; groups = forthArgument; diff --git a/lib/timeline/component/LineGraph.js b/lib/timeline/component/LineGraph.js index 23e82e9e..452d3385 100644 --- a/lib/timeline/component/LineGraph.js +++ b/lib/timeline/component/LineGraph.js @@ -307,8 +307,6 @@ LineGraph.prototype.setItems = function(items) { ids = this.itemsData.getIds(); this._onAdd(ids); } - this._updateUngrouped(); - //this._updateGraph(); this.redraw(true); }; @@ -365,9 +363,7 @@ LineGraph.prototype.setGroups = function(groups) { * @private */ LineGraph.prototype._onUpdate = function(ids) { - this._updateUngrouped(); this._updateAllGroupData(); - //this._updateGraph(); this.redraw(true); }; LineGraph.prototype._onAdd = function (ids) {this._onUpdate(ids);}; @@ -377,8 +373,6 @@ LineGraph.prototype._onUpdateGroups = function (groupIds) { var group = this.groupsData.get(groupIds[i]); this._updateGroup(group, groupIds[i]); } - - //this._updateGraph(); this.redraw(true); }; LineGraph.prototype._onAddGroups = function (groupIds) {this._onUpdateGroups(groupIds);}; @@ -405,8 +399,6 @@ LineGraph.prototype._onRemoveGroups = function (groupIds) { delete this.groups[groupIds[i]]; } } - this._updateUngrouped(); - //this._updateGraph(); this.redraw(true); }; @@ -454,81 +446,40 @@ LineGraph.prototype._updateGroup = function (group, groupId) { LineGraph.prototype._updateAllGroupData = function () { if (this.itemsData != null) { var groupsContent = {}; - var groupId; - for (groupId in this.groups) { - if (this.groups.hasOwnProperty(groupId)) { - groupsContent[groupId] = []; - } - } - for (var itemId in this.itemsData._data) { - if (this.itemsData._data.hasOwnProperty(itemId)) { - var item = this.itemsData._data[itemId]; - if (groupsContent[item.group] === undefined) { - throw new Error('Cannot find referenced group ' + item.group + '. Possible reason: items added before groups? Groups need to be added before items, as items refer to groups.') - } - item.x = util.convert(item.x,'Date'); - groupsContent[item.group].push(item); + this.itemsData.get().forEach(function(item){ + var groupId = item.group; + if (groupId === null || groupId === undefined) { + groupId = UNGROUPED; } - } - for (groupId in this.groups) { - if (this.groups.hasOwnProperty(groupId)) { - this.groups[groupId].setItems(groupsContent[groupId]); + if (groupsContent[groupId] === undefined) { + groupsContent[groupId] = []; } - } - } -}; - - -/** - * Create or delete the group holding all ungrouped items. This group is used when - * there are no groups specified. This anonymous group is called 'graph'. - * @protected - */ -LineGraph.prototype._updateUngrouped = function() { - if (this.itemsData && this.itemsData != null) { - var ungroupedCounter = 0; - for (var itemId in this.itemsData._data) { - if (this.itemsData._data.hasOwnProperty(itemId)) { - var item = this.itemsData._data[itemId]; - if (item != undefined) { - if (item.hasOwnProperty('group')) { - if (item.group === undefined) { - item.group = UNGROUPED; - } + var extended = Object.create(item); + extended.x = util.convert(item.x, 'Date'); + groupsContent[groupId].push(extended); + }); + //Update legendas and axis + for (var groupId in groupsContent) { + if (groupsContent.hasOwnProperty(groupId)) { + if (groupsContent[groupId].length == 0) { + if (this.groups.hasOwnProperty(groupId)) { + this._onRemoveGroups([groupId]); } - else { - item.group = UNGROUPED; + } else { + if (!this.groups.hasOwnProperty(groupId)) { + var group = {id: groupId, content: this.options.defaultGroup}; + if (this.groupsData && this.groupsData.hasOwnProperty(groupId)) { + group = this.groupsData[groupId]; + } + this._updateGroup(group, groupId); } - ungroupedCounter = item.group == UNGROUPED ? ungroupedCounter + 1 : ungroupedCounter; + this.groups[groupId].setItems(groupsContent[groupId]); } } } - - if (ungroupedCounter == 0) { - delete this.groups[UNGROUPED]; - this.legendLeft.removeGroup(UNGROUPED); - this.legendRight.removeGroup(UNGROUPED); - this.yAxisLeft.removeGroup(UNGROUPED); - this.yAxisRight.removeGroup(UNGROUPED); - } - else { - var group = {id: UNGROUPED, content: this.options.defaultGroup}; - this._updateGroup(group, UNGROUPED); - } - } - else { - delete this.groups[UNGROUPED]; - this.legendLeft.removeGroup(UNGROUPED); - this.legendRight.removeGroup(UNGROUPED); - this.yAxisLeft.removeGroup(UNGROUPED); - this.yAxisRight.removeGroup(UNGROUPED); } - - this.legendLeft.redraw(); - this.legendRight.redraw(); }; - /** * Redraw the component, mandatory function * @return {boolean} Returns true if the component is resized