Browse Source

Fix DataView for Graph2d, cleanup group initialisation.

fixDataView
Ludo Stellingwerff 9 years ago
parent
commit
22a964daea
3 changed files with 27 additions and 75 deletions
  1. +1
    -1
      lib/timeline/Graph2d.js
  2. +24
    -73
      lib/timeline/component/LineGraph.js
  3. +2
    -1
      test/DataView.test.js

+ 1
- 1
lib/timeline/Graph2d.js View File

@ -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;

+ 24
- 73
lib/timeline/component/LineGraph.js View File

@ -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

+ 2
- 1
test/DataView.test.js View File

@ -159,7 +159,8 @@ describe('DataView', function () {
var view = new DataView(data, {
filter: function (item) {
return item.group === 2;
}
},
fields:["id","title"]
});
var dataUpdates = [];

Loading…
Cancel
Save