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

Added category axis support to xy_ charts #129

Merged
merged 1 commit into from
Jun 10, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
Added category axis support to xy_ charts
  • Loading branch information
texodus committed Jun 10, 2018
commit b5ca880e57dbf6ffaa85ec0b10769cf7c464fb7d
8 changes: 8 additions & 0 deletions packages/perspective-viewer-highcharts/src/js/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ export function set_tick_size(config) {
config.plotOptions.scatter = {marker: {radius: new_radius}};
}

export function set_both_axis(config, axis, name, type, tree_type, top) {
if (type === "string") {
set_category_axis(config, axis, tree_type, top);
} else {
set_axis(config, axis, name, type);
}
}

export function set_axis(config, axis, name, type) {
let opts = {
type: type === "date" ? "datetime" : undefined,
Expand Down
25 changes: 12 additions & 13 deletions packages/perspective-viewer-highcharts/src/js/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import "../less/highcharts.less";
import {COLORS_10, COLORS_20} from "./externals.js";
import {color_axis} from "./color_axis.js";
import {make_tree_data, make_y_data, make_xy_data, make_xyz_data} from "./series.js";
import {set_boost, set_axis, set_category_axis, default_config, set_tick_size} from "./config.js";
import {set_boost, set_axis, set_category_axis, set_both_axis, default_config, set_tick_size} from "./config.js";

export const draw = (mode) => async function(el, view, task) {
const row_pivots = this._view_columns('#row_pivots perspective-row:not(.off)');
Expand Down Expand Up @@ -43,15 +43,14 @@ export const draw = (mode) => async function(el, view, task) {
xaxis_type = schema[xaxis_name],
yaxis_name = aggregates.length > 1 ? aggregates[1].column : undefined,
yaxis_type = schema[yaxis_name],
xtree_name = row_pivots.length > 0 ? row_pivots[row_pivots.length - 1] : undefined,
// xtree_name = row_pivots.length > 0 ? row_pivots[row_pivots.length - 1] : undefined,
xtree_type = tschema[xaxis_name],
ytree_name = col_pivots.length > 1 ? col_pivots[col_pivots.length - 1] : undefined,
//ytree_name = col_pivots.length > 1 ? col_pivots[col_pivots.length - 1] : undefined,
ytree_type = tschema[yaxis_name],
num_aggregates = aggregates.length - hidden.length,
num_cols = Object.keys(js[0]).filter(x => x !== '__ROW_PATH__').length;
num_aggregates = aggregates.length - hidden.length;

if (mode === 'scatter') {
let [series, , colorRange] = make_xy_data(js, row_pivots, col_pivots, hidden);
let [series, xtop, colorRange, ytop] = make_xy_data(js, schema, aggregates.map(x => x.column), row_pivots, col_pivots, hidden);
config.legend.floating = series.length <= 20;
config.legend.enabled = col_pivots.length > 0;
config.series = series;
Expand All @@ -67,8 +66,8 @@ export const draw = (mode) => async function(el, view, task) {
if (num_aggregates < 3) {
set_boost(config, xaxis_type, yaxis_type);
}
set_axis(config, 'xAxis', xaxis_name, xaxis_type);
set_axis(config, 'yAxis', yaxis_name, yaxis_type);
set_both_axis(config, 'xAxis', xaxis_name, xaxis_type, xtree_type, xtop);
set_both_axis(config, 'yAxis', yaxis_name, yaxis_type, ytree_type, ytop);
set_tick_size.call(this, config);
} else if (mode === 'heatmap') {
let [series, top, ytop, colorRange] = make_xyz_data(js, row_pivots, hidden);
Expand All @@ -86,15 +85,15 @@ export const draw = (mode) => async function(el, view, task) {
set_category_axis(config, 'yAxis', ytree_type, ytop);

} else if (mode === "treemap" || mode === "sunburst") {
let [series, top, colorRange] = make_tree_data(js, row_pivots, hidden, aggregates);
let [series, , colorRange] = make_tree_data(js, row_pivots, hidden, aggregates);
config.series = series;
config.plotOptions.series.borderWidth = 1;
config.legend.floating = false;
if (colorRange) {
color_axis.call(this, config, colorRange);
}
} else if (mode === 'line') {
let [series, top, colorRange] = make_xy_data(js, row_pivots, col_pivots, hidden);
let [series, xtop, , ytop] = make_xy_data(js, schema, aggregates.map(x => x.column), row_pivots, col_pivots, hidden);
const colors = series.length <= 10 ? COLORS_10 : COLORS_20;
config.legend.floating = series.length <= 20;
config.legend.enabled = col_pivots.length > 0;
Expand All @@ -104,10 +103,10 @@ export const draw = (mode) => async function(el, view, task) {
if (set_boost(config, xaxis_type, yaxis_type)) {
delete config.chart['type'];
}
set_axis(config, 'xAxis', xaxis_name, xaxis_type);
set_axis(config, 'yAxis', yaxis_name, yaxis_type);
set_both_axis(config, 'xAxis', xaxis_name, xaxis_type, xtree_type, xtop);
set_both_axis(config, 'yAxis', yaxis_name, yaxis_type, ytree_type, ytop);
} else {
let [series, top, colorRange] = make_y_data(js, row_pivots, hidden);
let [series, top, ] = make_y_data(js, row_pivots, hidden);
config.series = series;
config.colors = series.length <= 10 ? COLORS_10 : COLORS_20;
config.legend.enabled = col_pivots.length > 0 || series.length > 1;
Expand Down
165 changes: 110 additions & 55 deletions packages/perspective-viewer-highcharts/src/js/series.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,77 +135,132 @@ export function make_y_data(js, pivots, hidden) {
* XY
*/

function make_tick(row, columns, is_string, colorRange) {
var tick = {};
tick.x = row[columns[0]];
tick.x = (tick.x === undefined || tick.x === "" || isNaN(tick.x) ? null : tick.x);
tick.y = 0;
if (columns.length > 1) {
tick.y = row[columns[1]];
tick.y = (tick.y === undefined || tick.y === "" || isNaN(tick.y) ? null : tick.y);
class TickClean {

constructor(type) {
this.dict = {};
this.names = [];
this.type = type;
}

// Color by
if (columns.length > 2) {
let color = row[columns[2]];
if (typeof color === "string") {
if (!(color in is_string)) {
is_string[color] = COLORS_20[Object.keys(is_string).length];
}
tick.marker = {
lineColor: is_string[color],
fillColor: is_string[color]
};
} else {
if (!isNaN(color)) {
colorRange[0] = Math.min(colorRange[0], color);
colorRange[1] = Math.max(colorRange[1], color);
clean(val) {
if (this.type === "string") {
if (!(val in this.dict)) {
this.dict[val] = Object.keys(this.dict).length;
if (val === null) {
this.names.push('-');
} else {
this.names.push(val);
}
}
tick.colorValue = color;
return this.dict[val];
} else if (val === undefined || val === "" || isNaN(val)) {
return null;
}
return val;
}
// size by
if (columns.length > 3) {
tick.z = isNaN(row[columns[3]]) ? 1 : row[columns[3]];
}

class MakeTick {

constructor(schema, columns) {
this.schema = schema;
this.xaxis_clean = new TickClean(schema[columns[0]]);
this.yaxis_clean = new TickClean(schema[columns[1]]);
this.color_clean = new TickClean(schema[columns[2]]);;
}
if ('__ROW_PATH__' in row) {
tick.name = row['__ROW_PATH__'].join(", ");

make(row, columns, colorRange) {
let tick = {};
tick.x = row[columns[0]];
if (tick.x === null && row[columns[1]] === null) {
return;
}
tick.x = this.xaxis_clean.clean(tick.x);
tick.y = 0;
if (columns.length > 1) {
tick.y = row[columns[1]];
tick.y = this.yaxis_clean.clean(tick.y);
}

// Color by
if (columns.length > 2) {
let color = row[columns[2]];
if (this.schema[columns[2]] === "string") {
let color_index = this.color_clean.clean(color);
tick.marker = {
lineColor: color_index,
fillColor: color_index
};
} else {
if (!isNaN(color)) {
colorRange[0] = Math.min(colorRange[0], color);
colorRange[1] = Math.max(colorRange[1], color);
}
tick.colorValue = color;
}
}
// size by
if (columns.length > 3) {
tick.z = isNaN(row[columns[3]]) ? 1 : row[columns[3]];
}
if ('__ROW_PATH__' in row) {
tick.name = row['__ROW_PATH__'].join(", ");
}
return tick;
}
return tick;
}

export function make_xy_data(js, pivots, col_pivots, hidden) {
export function make_xy_data(js, schema, columns, pivots, col_pivots, hidden) {
let rows = new TreeAxisIterator(pivots.length, js);
let rows2 = new ColumnsIterator(rows, hidden);
let series = [];
let colorRange = [Infinity, -Infinity];
let is_string = {};

for (let row of rows2) {
if (col_pivots.length === 0) {
let sname = ' ';
let s = row_to_series(series, sname);
s.data.push(make_tick(row, rows2.columns, is_string, colorRange));
} else {
let prev, group = [], s;
for (let prop of rows2.columns) {
let sname = prop.split(',');
let gname = sname[sname.length - 1];
sname = sname.slice(0, sname.length - 1).join(",") || " ";
if (prev === undefined) prev = sname;
s = row_to_series(series, prev);
if (prev === sname) {
group.push(prop);
} else {
prev = sname;
s.data.push(make_tick(row, group, is_string, colorRange));
group = [prop];
}
let make_tick = new MakeTick(schema, columns);
if (col_pivots.length === 0) {
let sname = ' ';
let s = row_to_series(series, sname);
for (let row of rows2) {
let tick = make_tick.make(row, columns, colorRange);
if (tick) {
s.data.push(tick);
}
}
} else {
let prev, group = [], s;
let cols = Object.keys(js[0]).filter(prop => {
let cname = prop.split(',');
cname = cname[cname.length - 1];
return prop !== "__ROW_PATH__" && hidden.indexOf(cname) === -1;
});
for (let prop of cols) {
let column_levels = prop.split(',');
let group_name = column_levels.slice(0, column_levels.length - 1).join(",") || " ";
if (prev === undefined) {
prev = group_name;
}
s = row_to_series(series, prev);
if (prev === group_name) {
group.push(prop);
} else {
for (let row of rows2) {
let tick = make_tick.make(row, group, colorRange);
if (tick) {
s.data.push(tick);
}
}
prev = group_name;
group = [prop];
}
}
for (let row of rows2) {
let tick = make_tick.make(row, group, colorRange);
if (tick) {
s.data.push(tick);
}
s.data.push(make_tick(row, group, is_string, colorRange));
}
}
return [series, rows.top, colorRange];
return [series, {categories: make_tick.xaxis_clean.names}, colorRange, {categories: make_tick.yaxis_clean.names}];
}

/******************************************************************************
Expand Down