Browse Source

DataSet constructor now accepts initial data and options

css_transitions
jos 10 years ago
parent
commit
c64cdb9166
7 changed files with 45 additions and 13 deletions
  1. +6
    -1
      HISTORY.md
  2. +6
    -1
      docs/dataset.html
  3. +1
    -2
      examples/timeline/08_edit_items.html
  4. +2
    -4
      examples/timeline/09_order_groups.html
  5. +14
    -4
      src/DataSet.js
  6. +1
    -1
      src/timeline/component/Panel.js
  7. +15
    -0
      test/dataset.js

+ 6
- 1
HISTORY.md View File

@ -1,4 +1,4 @@
vis.js history
# vis.js history
http://visjs.org
@ -15,6 +15,11 @@ http://visjs.org
- Minor bug fixes.
- More examples added.
### DataSet
- A DataSet can now be constructed with initial data, like
`new DataSet(data, options)`.
## 2014-04-18, version 0.7.4

+ 6
- 1
docs/dataset.html View File

@ -107,7 +107,7 @@ console.log('formatted items', items);
</p>
<pre class="prettyprint lang-js">
var data = new vis.DataSet(options)
var data = new vis.DataSet([data] [, options])
</pre>
<p>
@ -116,6 +116,11 @@ var data = new vis.DataSet(options)
<a href="#Data_Manipulation">Data Manipulation</a>.
</p>
<p>
The parameter <code>data</code>code> is optional and can be an Array or
Google DataTable with items.
</p>
<p>
The parameter <code>options</code> is optional and is an object which can
contain the following properties:

+ 1
- 2
examples/timeline/08_edit_items.html View File

@ -18,8 +18,7 @@
<div id="log"></div>
<script type="text/javascript">
var items = new vis.DataSet();
items.add([
var items = new vis.DataSet([
{id: 1, content: 'item 1', start: new Date(2013, 3, 20)},
{id: 2, content: 'item 2', start: new Date(2013, 3, 14)},
{id: 3, content: 'item 3', start: new Date(2013, 3, 18)},

+ 2
- 4
examples/timeline/09_order_groups.html View File

@ -26,16 +26,14 @@
<div id="visualization"></div>
<script>
var groups = new vis.DataSet();
groups.add([
var groups = new vis.DataSet([
{id: 0, content: 'First', value: 1},
{id: 1, content: 'Third', value: 3},
{id: 2, content: 'Second', value: 2}
]);
// create a dataset with items
var items = new vis.DataSet();
items.add([
var items = new vis.DataSet([
{id: 0, group: 0, content: 'item 0', start: new Date(2014, 3, 17)},
{id: 1, group: 0, content: 'item 1', start: new Date(2014, 3, 19)},
{id: 2, group: 1, content: 'item 2', start: new Date(2014, 3, 16)},

+ 14
- 4
src/DataSet.js View File

@ -26,6 +26,7 @@
* - gives triggers upon changes in the data
* - can import/export data in various data formats
*
* @param {Array | DataTable} [data] Optional array with initial data
* @param {Object} [options] Available options:
* {String} fieldId Field name of the id in the
* items, 'id' by default.
@ -35,9 +36,15 @@
* @constructor DataSet
*/
// TODO: add a DataSet constructor DataSet(data, options)
function DataSet (options) {
function DataSet (data, options) {
this.id = util.randomUUID();
// correctly read optional arguments
if (data && !Array.isArray(data) && !util.isDataTable(data)) {
options = data;
data = null;
}
this.options = options || {};
this.data = {}; // map with data indexed by id
this.fieldId = this.options.fieldId || 'id'; // name of the field containing id
@ -58,10 +65,13 @@ function DataSet (options) {
}
}
// event subscribers
this.subscribers = {};
this.subscribers = {}; // event subscribers
this.internalIds = {}; // internally generated id's
this.internalIds = {}; // internally generated id's
// add initial data when provided
if (data) {
this.add(data);
}
}
/**

+ 1
- 1
src/timeline/component/Panel.js View File

@ -17,7 +17,7 @@ function Panel(options) {
this.options = options || {};
// create frame
this.frame = document.createElement('div');
this.frame = (typeof document !== 'undefined') ? document.createElement('div') : null;
}
Panel.prototype = new Component();

+ 15
- 0
test/dataset.js View File

@ -162,5 +162,20 @@ assert.deepEqual((data.get()[0].id == undefined), false);
assert.deepEqual(data.isInternalId(data.get()[0].id), true);
assert.deepEqual((data.get({"showInternalIds": false})[0].id == undefined),true);
// create a dataset with initial data
var data = new DataSet([
{id: 1, content: 'Item 1', start: new Date(now.valueOf())},
{id: 2, content: 'Item 2', start: now.toISOString()}
]);
assert.deepEqual(data.getIds(), [1, 2]);
// create a dataset with initial data and options
var data = new DataSet([
{_id: 1, content: 'Item 1', start: new Date(now.valueOf())},
{_id: 2, content: 'Item 2', start: now.toISOString()}
], {fieldId: '_id'});
assert.deepEqual(data.getIds(), [1, 2]);
// TODO: extensively test DataSet
// TODO: test subscribing to events

Loading…
Cancel
Save