Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve arranged bar handling #268

Open
wants to merge 5 commits into
base: dev-v1.11.7
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 68 additions & 32 deletions build/webcharts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3402,7 +3402,7 @@
);
})
) {
this.data.filtered = this.data.raw;
this.data.filtered = this.data.raw.slice();
this.filters
.filter(function(filter) {
return (
Expand All @@ -3418,7 +3418,7 @@
: filter.val === d[filter.col];
});
});
} else this.data.filtered = this.data.raw;
} else this.data.filtered = this.data.raw.slice();
}

function updateDataObject() {
Expand Down Expand Up @@ -3922,7 +3922,8 @@
key: col
})
.classed('wc-button sort-box', true)
.text(header)
.text(header),
type: this.config.types[col]
};
sortItem.wrap
.append('span')
Expand Down Expand Up @@ -3968,27 +3969,50 @@
this.draw();
}

function _typeof(obj) {
if (typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol') {
_typeof = function(obj) {
return typeof obj;
};
} else {
_typeof = function(obj) {
return obj &&
typeof Symbol === 'function' &&
obj.constructor === Symbol &&
obj !== Symbol.prototype
? 'symbol'
: typeof obj;
};
}

return _typeof(obj);
}

function sortData(data) {
var _this = this;

data = data.sort(function(a, b) {
var order = 0;

_this.sortable.order.forEach(function(item) {
var aCell = a[item.col],
bCell = b[item.col];
var aCell = a[item.col];
var bCell = b[item.col];

if (order === 0) {
if (
(item.direction === 'ascending' && aCell < bCell) ||
(item.direction === 'descending' && aCell > bCell)
)
order = -1;
else if (
(item.direction === 'ascending' && aCell > bCell) ||
(item.direction === 'descending' && aCell < bCell)
)
order = 1;
if (item.type === 'number') {
order = item.direction === 'ascending' ? +aCell - +bCell : +bCell - +aCell;
} else {
if (order === 0) {
if (
(item.direction === 'ascending' && aCell < bCell) ||
(item.direction === 'descending' && aCell > bCell)
)
order = -1;
else if (
(item.direction === 'ascending' && aCell > bCell) ||
(item.direction === 'descending' && aCell < bCell)
)
order = 1;
}
}
});

Expand Down Expand Up @@ -4347,30 +4371,39 @@
}

function setDefaults$1(firstItem) {
//Set data-driven defaults.
if (this.config.cols instanceof Array && this.config.headers instanceof Array) {
if (this.config.cols.length === 0) delete this.config.cols;
if (
this.config.headers.length === 0 ||
this.config.headers.length !== this.config.cols.length
)
delete this.config.headers;
}
var _this = this;

this.config.cols = this.config.cols || d3.keys(firstItem);
this.config.headers = this.config.headers || this.config.cols;
this.config.layout = 'horizontal'; // placeholder setting to align table components vertically or horizontally
//Set all other defaults.
// cols
if (
!Array.isArray(this.config.cols) ||
(Array.isArray(this.config.cols) && this.config.cols.length === 0)
)
this.config.cols = d3.keys(firstItem); // headers

if (
!Array.isArray(this.config.headers) ||
(Array.isArray(this.config.headers) && this.config.headers.length === 0) ||
(Array.isArray(this.config.headers) &&
this.config.headers.length !== this.config.cols.length)
)
this.config.headers = this.config.cols.slice(); // types

if (_typeof(this.config.types) !== 'object') this.config.types = {};
this.config.cols.forEach(function(col) {
if (!['string', 'number'].includes(_this.config.types[col]))
_this.config.types[col] = 'string';
}); // Set all other defaults.

setDefault.call(this, 'searchable');
setDefault.call(this, 'exportable');
setDefault.call(this, 'exports', ['csv']);
setDefault.call(this, 'sortable');
setDefault.call(this, 'pagination');
setDefault.call(this, 'exportable');
setDefault.call(this, 'exports', ['csv']);
setDefault.call(this, 'nRowsPerPage', 10);
setDefault.call(this, 'nPageLinksDisplayed', 5);
setDefault.call(this, 'applyCSS');
setDefault.call(this, 'dynamicPositioning');
setDefault.call(this, 'layout', 'horizontal');
}

function transformData$1(processed_data) {
Expand Down Expand Up @@ -4508,6 +4541,7 @@
}
});

var tableCount = 0;
function createTable() {
var element = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'body';
var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
Expand Down Expand Up @@ -4540,8 +4574,10 @@
if (callback) {
thisTable.events['on' + event.charAt(0).toUpperCase() + event.slice(1)] = callback;
}
};
}; //increment thisChart count to get unique thisChart id

tableCount++;
thisTable.id = tableCount;
return thisTable;
}

Expand Down
2 changes: 1 addition & 1 deletion build/webcharts.min.js

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/createTable.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import table from './table/index';
import { select } from 'd3';

export var tableCount = 0;

export default function createTable(element = 'body', config = {}, controls = null) {
let thisTable = Object.create(table);

Expand Down Expand Up @@ -36,5 +38,10 @@ export default function createTable(element = 'body', config = {}, controls = nu
}
};

//increment thisChart count to get unique thisChart id
tableCount++;

thisTable.id = tableCount;

return thisTable;
}
4 changes: 2 additions & 2 deletions src/table/draw/applyFilters.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default function applyFilters() {
(Array.isArray(filter.val) && filter.val.length < filter.choices.length)
)
) {
this.data.filtered = this.data.raw;
this.data.filtered = this.data.raw.slice();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call

this.filters
.filter(
filter =>
Expand All @@ -24,5 +24,5 @@ export default function applyFilters() {
: filter.val === d[filter.col]
);
});
} else this.data.filtered = this.data.raw;
} else this.data.filtered = this.data.raw.slice();
}
41 changes: 26 additions & 15 deletions src/table/setDefaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,39 @@ import { keys } from 'd3';
import setDefault from '../util/setDefault';

export default function setDefaults(firstItem) {
//Set data-driven defaults.
if (this.config.cols instanceof Array && this.config.headers instanceof Array) {
if (this.config.cols.length === 0) delete this.config.cols;
if (
this.config.headers.length === 0 ||
this.config.headers.length !== this.config.cols.length
)
delete this.config.headers;
}
// cols
if (
!Array.isArray(this.config.cols) ||
(Array.isArray(this.config.cols) && this.config.cols.length === 0)
)
this.config.cols = keys(firstItem);

this.config.cols = this.config.cols || keys(firstItem);
this.config.headers = this.config.headers || this.config.cols;
this.config.layout = 'horizontal'; // placeholder setting to align table components vertically or horizontally
// headers
if (
!Array.isArray(this.config.headers) ||
(Array.isArray(this.config.headers) && this.config.headers.length === 0) ||
(Array.isArray(this.config.headers) &&
this.config.headers.length !== this.config.cols.length)
)
this.config.headers = this.config.cols.slice();

//Set all other defaults.
// types
if (typeof this.config.types !== 'object') this.config.types = {};

this.config.cols.forEach(col => {
if (!['string', 'number'].includes(this.config.types[col]))
this.config.types[col] = 'string';
});

// Set all other defaults.
setDefault.call(this, 'searchable');
setDefault.call(this, 'exportable');
setDefault.call(this, 'exports', ['csv']);
setDefault.call(this, 'sortable');
setDefault.call(this, 'pagination');
setDefault.call(this, 'exportable');
setDefault.call(this, 'exports', ['csv']);
setDefault.call(this, 'nRowsPerPage', 10);
setDefault.call(this, 'nPageLinksDisplayed', 5);
setDefault.call(this, 'applyCSS');
setDefault.call(this, 'dynamicPositioning');
setDefault.call(this, 'layout', 'horizontal');
}
3 changes: 2 additions & 1 deletion src/table/sortable/onClick.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export default function onClick(th, header) {
.append('div')
.datum({ key: col })
.classed('wc-button sort-box', true)
.text(header)
.text(header),
type: this.config.types[col]
};
sortItem.wrap
.append('span')
Expand Down
30 changes: 17 additions & 13 deletions src/table/sortable/sortData.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ export default function sortData(data) {
let order = 0;

this.sortable.order.forEach(item => {
const aCell = a[item.col],
bCell = b[item.col];
const aCell = a[item.col];
const bCell = b[item.col];

if (order === 0) {
if (
(item.direction === 'ascending' && aCell < bCell) ||
(item.direction === 'descending' && aCell > bCell)
)
order = -1;
else if (
(item.direction === 'ascending' && aCell > bCell) ||
(item.direction === 'descending' && aCell < bCell)
)
order = 1;
if (item.type === 'number') {
Copy link
Contributor

@pburnsdata pburnsdata Feb 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this gets fixed later

order = item.direction === 'ascending' ? +aCell - +bCell : +bCell - +aCell;
} else {
if (order === 0) {
if (
(item.direction === 'ascending' && aCell < bCell) ||
(item.direction === 'descending' && aCell > bCell)
)
order = -1;
else if (
(item.direction === 'ascending' && aCell > bCell) ||
(item.direction === 'descending' && aCell < bCell)
)
order = 1;
}
}
});

Expand Down
35 changes: 27 additions & 8 deletions test-page/createTable/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
function createTable(element, settings) {
var controls = webCharts.createControls(
const controls = webCharts.createControls(
element,
{
inputs: [
{type: 'subsetter', value_col: 'Period', label: 'Filter by Period'},
{type: 'subsetter', value_col: 'Group', label: 'Filter by Group'}
{type: 'subsetter', value_col: 'Group', label: 'Filter by Group'},
]
}
);

return webCharts.createTable(element, settings, controls);
}

var table = createTable(
const table = createTable(
'.table',
{
'sortable': true,
Expand All @@ -27,17 +27,36 @@ var table = createTable(
);

d3.csv(
'https://cdn.jsdelivr.net/gh/RhoInc/data-library/data/miscellaneous/elements.csv',
'https://raw.githubusercontent.com/RhoInc/data-library/master/data/miscellaneous/elements.csv',
function(d) {
return d;
},
function(data) {
table.config.types = Object.keys(data[0])
.map(col => {
let type = 'string';

const vector = data
.map(d => d[col]).filter(d => d !== '');

if (vector.length > 0 && vector.every(d => !isNaN(parseFloat(d))))
type = 'number';

return [col, type];
})
.reduce(
(acc,cur) => {
acc[cur[0]] = cur[1];
return acc;
},
{}
);
table.init(data);

//Update settings.
d3.selectAll('.controls input')
.on('change',function(){
var settings = {
const settings = {
sortable: d3.select('input.sortable').property('checked'),
searchable: d3.select('input.searchable').property('checked'),
nRowsPerPage: +d3.select('input.items').node().value,
Expand All @@ -50,7 +69,7 @@ d3.csv(
console.log(settings);

d3.select('.table').selectAll('*').remove()
var table = createTable(
const table = createTable(
'.table',
settings
);
Expand All @@ -62,7 +81,7 @@ d3.csv(
//Randomize columns.
d3.select('button.randomize-columns')
.on('click', function() {
var table = d3.select('.wc-table').datum();
const table = d3.select('.wc-table').datum();
table.config.cols = Object.keys(table.data.raw[0])
.reverse()
.filter(function(d) {
Expand All @@ -76,7 +95,7 @@ d3.select('button.randomize-columns')
//Randomize headers.
d3.select('button.randomize-headers')
.on('click', function() {
var table = d3.select('.wc-table').datum();
const table = d3.select('.wc-table').datum();
table.config.headers = table.config.cols
.map(function(key) {
const strArr = [];
Expand Down