Skip to content

Commit

Permalink
Update scales usage in bqplot
Browse files Browse the repository at this point in the history
Add typing everywhere scales are used in bqplot
  • Loading branch information
martinRenou committed Sep 24, 2019
1 parent 52fa482 commit f862fa6
Show file tree
Hide file tree
Showing 38 changed files with 263 additions and 170 deletions.
14 changes: 9 additions & 5 deletions js/src/Bars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import * as _ from 'underscore';
import { Mark } from './Mark'
import { BarsModel } from './BarsModel'

import {
Scale
} from './scales/Scale';

export class Bars extends Mark {

render() {
Expand Down Expand Up @@ -65,11 +69,11 @@ export class Bars extends Mark {
const dom = (orient === "vertical") ? "x" : "y";
const rang = (orient === "vertical") ? "y" : "x";
if(dom_scale.model.type !== "ordinal") {
dom_scale.set_range(this.parent.padded_range(dom, dom_scale.model));
dom_scale.setRange(this.parent.padded_range(dom, dom_scale.model));
} else {
dom_scale.set_range(this.parent.padded_range(dom, dom_scale.model), this.model.get("padding"));
dom_scale.setRange(this.parent.padded_range(dom, dom_scale.model), this.model.get("padding"));
}
range_scale.set_range(this.parent.padded_range(rang, range_scale.model));
range_scale.setRange(this.parent.padded_range(rang, range_scale.model));
// x_offset is set later by the adjust_offset method
// This differs because it is not constant for a scale.
// Changes based on the data.
Expand Down Expand Up @@ -695,11 +699,11 @@ export class Bars extends Mark {
}

dom_offset: any;
dom_scale: any;
dom_scale: Scale;
legend_el: any;
pixel_coords: any;
range_offset: any;
range_scale: any;
range_scale: Scale;
x_pixels: any;
x: any;
x1: any;
Expand Down
14 changes: 7 additions & 7 deletions js/src/BarsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ export class BarsModel extends markmodel.MarkModel {
});
if(color_scale && color.length > 0) {
if(!this.get("preserve_domain").color) {
color_scale.compute_and_set_domain(color, this.model_id + "_color");
color_scale.computeAndSetDomain(color, this.model_id + "_color");
} else {
color_scale.del_domain([], this.model_id + "_color");
color_scale.delDomain([], this.model_id + "_color");
}
}
}
Expand All @@ -177,17 +177,17 @@ export class BarsModel extends markmodel.MarkModel {
const range_scale = scales.y;

if(!this.get("preserve_domain").x) {
dom_scale.compute_and_set_domain(this.mark_data.map(function(elem) {
dom_scale.computeAndSetDomain(this.mark_data.map(function(elem) {
return elem.key;
}), this.model_id + "_x");
}
else {
dom_scale.del_domain([], this.model_id + "_x");
dom_scale.delDomain([], this.model_id + "_x");
}

if(!this.get("preserve_domain").y) {
if(this.get("type") === "stacked") {
range_scale.compute_and_set_domain([d3.min(this.mark_data, function(c: any) { return c.neg_max; }),
range_scale.computeAndSetDomain([d3.min(this.mark_data, function(c: any) { return c.neg_max; }),
d3.max(this.mark_data, function(c: any) { return c.pos_max; }), this.base_value],
this.model_id + "_y");
} else {
Expand All @@ -202,10 +202,10 @@ export class BarsModel extends markmodel.MarkModel {
return val.y_ref;
});
});
range_scale.compute_and_set_domain([min, max, this.base_value], this.model_id + "_y");
range_scale.computeAndSetDomain([min, max, this.base_value], this.model_id + "_y");
}
} else {
range_scale.del_domain([], this.model_id + "_y");
range_scale.delDomain([], this.model_id + "_y");
}
}

Expand Down
4 changes: 2 additions & 2 deletions js/src/Boxplot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ export class Boxplot extends Mark {
set_ranges() {
const x_scale = this.scales.x;
if(x_scale) {
x_scale.set_range(this.parent.padded_range("x", x_scale.model));
x_scale.setRange(this.parent.padded_range("x", x_scale.model));
}
const y_scale = this.scales.y;
if(y_scale) {
y_scale.set_range(this.parent.padded_range("y", y_scale.model));
y_scale.setRange(this.parent.padded_range("y", y_scale.model));
}
}

Expand Down
8 changes: 4 additions & 4 deletions js/src/BoxplotModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ export class BoxplotModel extends MarkModel {
const y_scale = scales.y;

if(!this.get("preserve_domain").x && this.mark_data) {
x_scale.compute_and_set_domain(this.mark_data.map(function(elem) {
x_scale.computeAndSetDomain(this.mark_data.map(function(elem) {
return elem[0];
}), this.model_id + "_x");
} else {
x_scale.del_domain([], this.model_id + "_x");
x_scale.delDomain([], this.model_id + "_x");
}
if(!this.get("preserve_domain").y && this.mark_data) {
//The values are sorted, so we are using that to calculate the min/max
Expand All @@ -96,10 +96,10 @@ export class BoxplotModel extends MarkModel {
return values[values.length-1];
}));

y_scale.set_domain([min,max], this.model_id + "_y");
y_scale.setDomain([min,max], this.model_id + "_y");

} else {
y_scale.del_domain([], this.model_id + "_y");
y_scale.delDomain([], this.model_id + "_y");
}
}

Expand Down
11 changes: 10 additions & 1 deletion js/src/FastIntervalSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ import * as d3 from 'd3';
import { BaseXSelector } from './Selector';
import * as sel_utils from './selector_utils';

import {
LinearScale
} from './scales/LinearScale';

import {
OrdinalScale
} from './scales/OrdinalScale';

export class FastIntervalSelector extends BaseXSelector {

render() {
Expand Down Expand Up @@ -112,7 +120,7 @@ export class FastIntervalSelector extends BaseXSelector {
this.rect.attr("width", interval_size);
const pixel_extent = [start, start + interval_size];
this.set_selected("selected",
this.scale.invert_range(pixel_extent));
this.scale.invertRange(pixel_extent));
this.update_mark_selected(pixel_extent, undefined);
this.touch();
this.dirty = false;
Expand Down Expand Up @@ -222,4 +230,5 @@ export class FastIntervalSelector extends BaseXSelector {
size: any;
background: any;
rect: any;
scale: LinearScale | OrdinalScale;
}
14 changes: 8 additions & 6 deletions js/src/Figure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,17 +356,19 @@ export class Figure extends widgets.DOMWidgetView {
// See the scale_x and scale_y attributes of the python Figure
const that = this;
const x_scale_promise = this.create_child_view(this.model.get("scale_x"))
.then(function(view) {
// @ts-ignore
.then(function(view: Scale) {
that.scale_x = view;
that.scale_x.scale.clamp(true);
that.scale_x.set_range([0, that.plotarea_width]);
that.scale_x.setRange([0, that.plotarea_width]);
});

const y_scale_promise = this.create_child_view(this.model.get("scale_y"))
.then(function(view) {
// @ts-ignore
.then(function(view: Scale) {
that.scale_y = view;
that.scale_y.scale.clamp(true);
that.scale_y.set_range([that.plotarea_height, 0]);
that.scale_y.setRange([that.plotarea_height, 0]);
});
return Promise.all([x_scale_promise, y_scale_promise]);
}
Expand Down Expand Up @@ -588,12 +590,12 @@ export class Figure extends widgets.DOMWidgetView {
that.update_plotarea_dimensions();

if (that.scale_x !== undefined && that.scale_x !== null) {
that.scale_x.set_range([0, that.plotarea_width]);
that.scale_x.setRange([0, that.plotarea_width]);
}


if (that.scale_y !== undefined && that.scale_y !== null) {
that.scale_y.set_range([that.plotarea_height, 0]);
that.scale_y.setRange([that.plotarea_height, 0]);
}

// transform figure
Expand Down
2 changes: 1 addition & 1 deletion js/src/FlexLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class FlexLine extends Lines {
super.set_ranges();
const width_scale = this.scales.width;
if(width_scale) {
width_scale.set_range([0.5, this.model.get("stroke_width")]);
width_scale.setRange([0.5, this.model.get("stroke_width")]);
}
}

Expand Down
4 changes: 2 additions & 2 deletions js/src/Graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ export class Graph extends Mark {
const x_scale = this.scales.x,
y_scale = this.scales.y;
if (x_scale) {
x_scale.set_range(this.parent.padded_range("x", x_scale.model));
x_scale.setRange(this.parent.padded_range("x", x_scale.model));
}
if (y_scale) {
y_scale.set_range(this.parent.padded_range("y", y_scale.model));
y_scale.setRange(this.parent.padded_range("y", y_scale.model));
}
}

Expand Down
8 changes: 4 additions & 4 deletions js/src/GraphModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ export class GraphModel extends MarkModel {
if (x.length !== 0 && y.length !== 0) {
if (color_scale) {
if (!this.get("preserve_domain").color) {
color_scale.compute_and_set_domain(color,
color_scale.computeAndSetDomain(color,
this.model_id + "_color");
} else {
color_scale.del_domain([], this.model_id + "_color");
color_scale.delDomain([], this.model_id + "_color");
}
}

Expand Down Expand Up @@ -161,11 +161,11 @@ export class GraphModel extends MarkModel {
if (scales.hasOwnProperty(key)) {
const scale = scales[key];
if (!this.get("preserve_domain")[key]) {
scale.compute_and_set_domain(this.mark_data.map(function(d) {
scale.computeAndSetDomain(this.mark_data.map(function(d) {
return d[key] || d[data_scale_key_map[key]];
}), this.model_id + key);
} else {
scale.del_domain([], this.model_id + key);
scale.delDomain([], this.model_id + key);
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions js/src/GridHeatMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ export class GridHeatMap extends Mark {
// The y_range is reversed because we want the first row
// to start at the top of the plotarea and not the bottom.
const row_range = this.parent.padded_range("y", row_scale.model);
row_scale.set_range(row_range);
// row_scale.set_range([row_range[1], row_range[0]]);
row_scale.setRange(row_range);
// row_scale.setRange([row_range[1], row_range[0]]);
}
const col_scale = this.scales.column;
if(col_scale) {
col_scale.set_range(this.parent.padded_range("x", col_scale.model));
col_scale.setRange(this.parent.padded_range("x", col_scale.model));
}
}

Expand Down Expand Up @@ -413,15 +413,15 @@ export class GridHeatMap extends Mark {
new_domain = this.expand_scale_domain(row_scale, this.model.rows, this.model.modes.row, (row_start_aligned));
if(d3.min(new_domain) < d3.min(row_scale.model.domain) || d3.max(new_domain) > d3.max(row_scale.model.domain)) {
// Update domain if domain has changed
row_scale.model.compute_and_set_domain(new_domain, row_scale.model.model_id);
row_scale.model.computeAndSetDomain(new_domain, row_scale.model.model_id);
}
}

if(this.model.modes.column !== "middle" && this.model.modes.column !== "boundaries") {
new_domain = this.expand_scale_domain(column_scale, this.model.columns, this.model.modes.column, col_start_aligned);
if(d3.min(new_domain) < d3.min(column_scale.model.domain) || d3.max(new_domain) > d3.max(column_scale.model.domain)) {
// Update domain if domain has changed
column_scale.model.compute_and_set_domain(new_domain, column_scale.model.model_id);
column_scale.model.computeAndSetDomain(new_domain, column_scale.model.model_id);
}
}

Expand Down
12 changes: 6 additions & 6 deletions js/src/GridHeatMapModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,23 @@ export class GridHeatMapModel extends MarkModel {
const color_scale = scales.color;

if(!this.get("preserve_domain").row) {
y_scale.compute_and_set_domain(this.rows, this.model_id + "_row");
y_scale.computeAndSetDomain(this.rows, this.model_id + "_row");
} else {
y_scale.del_domain([], this.model_id + "_row");
y_scale.delDomain([], this.model_id + "_row");
}

if(!this.get("preserve_domain").column) {
x_scale.compute_and_set_domain(this.columns, this.model_id + "_column");
x_scale.computeAndSetDomain(this.columns, this.model_id + "_column");
} else {
x_scale.del_domain([], this.model_id + "_column");
x_scale.delDomain([], this.model_id + "_column");
}
if(color_scale !== null && color_scale !== undefined) {
if(!this.get("preserve_domain").color) {
color_scale.compute_and_set_domain(this.mark_data.map(function(elem) {
color_scale.computeAndSetDomain(this.mark_data.map(function(elem) {
return elem.color;
}), this.model_id + "_color");
} else {
color_scale.del_domain([], this.model_id + "_color");
color_scale.delDomain([], this.model_id + "_color");
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions js/src/HeatMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ export class HeatMap extends Mark {
const x_scale = this.scales.x;
if(x_scale) {
const x_range = this.parent.padded_range("x", x_scale.model);
x_scale.set_range(x_range);
x_scale.setRange(x_range);
}
const y_scale = this.scales.y;
if(y_scale) {
y_scale.set_range(this.parent.padded_range("y", y_scale.model));
y_scale.setRange(this.parent.padded_range("y", y_scale.model));
}
}

Expand Down
12 changes: 6 additions & 6 deletions js/src/HeatMapModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,21 @@ export class HeatMapModel extends MarkModel {
const flat_colors = [].concat.apply([], this.mark_data.color.map((x) => Array.prototype.slice.call(x, 0)));

if(!this.get("preserve_domain").x) {
x_scale.compute_and_set_domain(this.mark_data.x, this.model_id + "_x");
x_scale.computeAndSetDomain(this.mark_data.x, this.model_id + "_x");
} else {
x_scale.del_domain([], this.model_id + "_x");
x_scale.delDomain([], this.model_id + "_x");
}

if(!this.get("preserve_domain").y) {
y_scale.compute_and_set_domain(this.mark_data.y, this.model_id + "_y");
y_scale.computeAndSetDomain(this.mark_data.y, this.model_id + "_y");
} else {
y_scale.del_domain([], this.model_id + "_y");
y_scale.delDomain([], this.model_id + "_y");
}
if(color_scale !== null && color_scale !== undefined) {
if(!this.get("preserve_domain").color) {
color_scale.compute_and_set_domain(flat_colors, this.model_id + "_color");
color_scale.computeAndSetDomain(flat_colors, this.model_id + "_color");
} else {
color_scale.del_domain([], this.model_id + "_color");
color_scale.delDomain([], this.model_id + "_color");
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions js/src/Hist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ export class Hist extends Mark {
set_ranges() {
const x_scale = this.scales.sample;
if(x_scale) {
x_scale.set_range(this.parent.padded_range("x", x_scale.model));
x_scale.setRange(this.parent.padded_range("x", x_scale.model));
}
const y_scale = this.scales.count;
if(y_scale) {
y_scale.set_range(this.parent.padded_range("y", y_scale.model));
y_scale.setRange(this.parent.padded_range("y", y_scale.model));
}
}

Expand Down
6 changes: 3 additions & 3 deletions js/src/HistModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ export class HistModel extends MarkModel {
this.x_bins = [];
} else {
if(!this.get("preserve_domain").sample) {
x_scale.compute_and_set_domain(x_data, this.model_id + "_sample");
x_scale.computeAndSetDomain(x_data, this.model_id + "_sample");
} else {
x_scale.del_domain([], this.model_id + "_sample");
x_scale.delDomain([], this.model_id + "_sample");
}

this.min_x = x_scale.domain[0];
Expand Down Expand Up @@ -152,7 +152,7 @@ export class HistModel extends MarkModel {
// y-domain change is handled by this function.
const y_scale = this.getScales().count;
if(!this.get("preserve_domain").count) {
y_scale.set_domain([0, d3.max(this.mark_data, function(d: any): number {
y_scale.setDomain([0, d3.max(this.mark_data, function(d: any): number {
return d.y;
}) * 1.05], this.model_id + "_count");
}
Expand Down

0 comments on commit f862fa6

Please sign in to comment.