Skip to content

Commit

Permalink
Move scales to a separate directory
Browse files Browse the repository at this point in the history
Signed-off-by: martinRenou <martin.renou@gmail.com>
  • Loading branch information
JohanMabille authored and martinRenou committed Dec 17, 2019
1 parent 543b62a commit 4f3744d
Show file tree
Hide file tree
Showing 77 changed files with 2,026 additions and 1,697 deletions.
12 changes: 8 additions & 4 deletions examples/Marks/Object Model/GridHeatMap.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"metadata": {},
"outputs": [],
"source": [
"grid_map.selected"
Expand All @@ -326,6 +324,12 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
}
},
"outputs": [],
"source": [
"import numpy as np\n",
Expand Down Expand Up @@ -372,7 +376,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
"version": "3.6.2"
}
},
"nbformat": 4,
Expand Down
37 changes: 25 additions & 12 deletions js/src/Axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,24 @@
* limitations under the License.
*/

import { WidgetView } from '@jupyter-widgets/base';
import * as _ from 'underscore';

import * as d3 from 'd3';

import {
WidgetView
} from '@jupyter-widgets/base';

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

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

import 'd3-selection-multi';
// const d3 =Object.assign({}, require("d3-axis"), require("d3-format"), require("d3-selection"), require("d3-selection-multi"), require("d3-time"), require("d3-time-format"));
import * as utils from './utils';
import * as _ from 'underscore';

// Polyfill for Math.log10 in IE11
Math.log10 = Math.log10 || function(x) {
Expand Down Expand Up @@ -155,7 +167,7 @@ export class Axis extends WidgetView {
}
if(this.model.get("tick_format") === null ||
this.model.get("tick_format") === undefined) {
if(this.axis_scale.type !== "ordinal") {
if(this.axis_scale.model.type !== "ordinal") {
this.tick_format = this.guess_tick_format(this.axis.tickValues());
}
}
Expand Down Expand Up @@ -217,7 +229,7 @@ export class Axis extends WidgetView {
const target_range = (is_vertical) ?
this.parent.range("y") : this.parent.range("x");

this.axis_scale.expand_domain(initial_range, target_range);
this.axis_scale.expandDomain(initial_range, target_range);
this.axis.scale(this.axis_scale.scale);
}

Expand All @@ -235,7 +247,7 @@ export class Axis extends WidgetView {
this.parent.range("y") :
this.parent.range("x");

this.offset_scale.expand_domain(initial_range, target_range);
this.offset_scale.expandDomain(initial_range, target_range);
}
}

Expand Down Expand Up @@ -272,10 +284,10 @@ export class Axis extends WidgetView {
set_scales_range() {
const is_vertical = this.model.get("orientation") === "vertical";

this.axis_scale.set_range((is_vertical) ?
this.axis_scale.setRange((is_vertical) ?
[this.height, 0] : [0, this.width]);
if(this.offset_scale) {
this.offset_scale.set_range((is_vertical) ?
this.offset_scale.setRange((is_vertical) ?
[0, this.width] : [this.height, 0]);
}
}
Expand Down Expand Up @@ -626,7 +638,7 @@ export class Axis extends WidgetView {
this.parent.padded_range("y", this.axis_scale.model) : this.parent.padded_range("x", this.axis_scale.model);
const target_range = (this.vertical) ?
this.parent.range("y") : this.parent.range("x");
this.axis_scale.expand_domain(initial_range, target_range);
this.axis_scale.expandDomain(initial_range, target_range);
this.axis.scale(this.axis_scale.scale);
}

Expand Down Expand Up @@ -686,10 +698,11 @@ export class Axis extends WidgetView {
}
}

set_scale_promise(model) {
set_scale_promise(model: ScaleModel) {
// Sets the child scale
if (this.axis_scale) { this.axis_scale.remove(); }
return this.create_child_view(model).then((view) => {
// @ts-ignore
return this.create_child_view(model).then((view: Scale) => {
// Trigger the displayed event of the child view.
this.displayed.then(() => {
view.trigger("displayed");
Expand Down Expand Up @@ -904,7 +917,7 @@ export class Axis extends WidgetView {
return this.parent.margin;
}

axis_scale: any;
axis_scale: Scale;
axis: any;
d3el: any;
g_axisline: any;
Expand Down
106 changes: 55 additions & 51 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 @@ -157,10 +161,10 @@ export class Bars extends Mark {
this.event_dispatcher("parent_clicked");
});
this.model.on_some_change([
"label_display_format",
"label_font_style",
"label_display",
"label_display_vertical_offset",
"label_display_format",
"label_font_style",
"label_display",
"label_display_vertical_offset",
"label_display_horizontal_offset"],
this.draw, this);
}
Expand Down Expand Up @@ -512,7 +516,7 @@ export class Bars extends Mark {
const baseLine = this.baseLine;
const barOrientation = this.barOrientation;
const barLabels = barGroups.selectAll(".bar_label");

barLabels
.attr(dom, d => 0)
.attr(rang, d => {
Expand All @@ -525,23 +529,23 @@ export class Bars extends Mark {
.style("font-weight", "400")
.style("text-anchor", (d, i) => {
return this.styleBarLabelTextAnchor(
d,
barOrientation,
d,
barOrientation,
baseLine);
})
.style("dominant-baseline", (d, i) => {
return this.styleBarLabelDominantBaseline(
d,
baseLine,
d,
baseLine,
barOrientation);
})
.attr("transform", (d, i) => {
return this.transformBarLabel(
d,
baseLine,
this.offsetHorizontal,
this.offsetVertical,
bandWidth,
d,
baseLine,
this.offsetHorizontal,
this.offsetVertical,
bandWidth,
barOrientation);
})
}
Expand All @@ -555,7 +559,7 @@ export class Bars extends Mark {
const baseLine = this.baseLine;
const barOrientation = this.barOrientation;
const barLabels = barGroups.selectAll(".bar_label")

barLabels
.attr("x", (d, i) => {
if (barOrientation === "horizontal") {
Expand All @@ -574,23 +578,23 @@ export class Bars extends Mark {
.style("font-weight", "400")
.style("text-anchor", (d, i) => {
return this.styleBarLabelTextAnchor(
d,
barOrientation,
d,
barOrientation,
baseLine);
})
.style("dominant-baseline", (d, i) => {
return this.styleBarLabelDominantBaseline(
d,
baseLine,
d,
baseLine,
barOrientation);
})
.attr("transform", (d, i) => {
return this.transformBarLabel(
d,
baseLine,
this.offsetHorizontal,
this.offsetVertical,
bandWidth,
d,
baseLine,
this.offsetHorizontal,
this.offsetVertical,
bandWidth,
barOrientation);
})
}
Expand All @@ -606,29 +610,29 @@ export class Bars extends Mark {
* @param barOrientation - Orientation of the bar chart (horizontal/vertical)
*/
transformBarLabel(
d: any,
baseLine: number,
offsetHorizontal: number,
offsetVertical: number,
bandWidth: number,
d: any,
baseLine: number,
offsetHorizontal: number,
offsetVertical: number,
bandWidth: number,
barOrientation: string): string {
if (barOrientation === "horizontal") {
return (d.y <= baseLine)
? `translate(${(d.y0 <= baseLine)
? (0 - offsetVertical)
: (0 + offsetVertical)}, ${bandWidth / 2 + offsetHorizontal})`
: `translate(${(d.y1 <= baseLine)
? (0 - offsetVertical)
return (d.y <= baseLine)
? `translate(${(d.y0 <= baseLine)
? (0 - offsetVertical)
: (0 + offsetVertical)}, ${bandWidth / 2 + offsetHorizontal})`
: `translate(${(d.y1 <= baseLine)
? (0 - offsetVertical)
: (0 + offsetVertical)}, ${bandWidth / 2 + offsetHorizontal})`;
} else {
return (d.y <= baseLine)
? `translate(${bandWidth / 2 + offsetHorizontal},
${(d.y0 <= baseLine)
? (0 - offsetVertical)
: (0 + offsetVertical)})`
: `translate(${bandWidth / 2 + offsetHorizontal},
${(d.y1 <= baseLine)
? (0 - offsetVertical)
return (d.y <= baseLine)
? `translate(${bandWidth / 2 + offsetHorizontal},
${(d.y0 <= baseLine)
? (0 - offsetVertical)
: (0 + offsetVertical)})`
: `translate(${bandWidth / 2 + offsetHorizontal},
${(d.y1 <= baseLine)
? (0 - offsetVertical)
: (0 + offsetVertical)})`;
}
}
Expand Down Expand Up @@ -669,8 +673,8 @@ export class Bars extends Mark {
*/
updateBarLabelsStyle(): void {
const displayFormatStr = this.model.get("label_display_format");
const displayFormat = displayFormatStr
? d3.format(displayFormatStr)
const displayFormat = displayFormatStr
? d3.format(displayFormatStr)
: null;

let fonts = this.d3el.selectAll(".bar_label")
Expand Down Expand Up @@ -976,15 +980,15 @@ 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;

// Overriding super class
model: BarsModel;
}
}
18 changes: 9 additions & 9 deletions js/src/BarsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class BarsModel extends markmodel.MarkModel {
return;
}
const color = this.get("color") || [];
const color_scale = this.get("scales").color;
const color_scale = this.getScales().color;
const color_mode = this.get("color_mode");
const apply_color_to_groups = ((color_mode === "group") ||
(color_mode === "auto" && !(this.is_y_2d)));
Expand All @@ -166,9 +166,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,22 +177,22 @@ export class BarsModel extends markmodel.MarkModel {
if(!this.mark_data) {
return;
}
const scales = this.get("scales");
const scales = this.getScales();
const dom_scale = scales.x;
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 @@ -207,10 +207,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

0 comments on commit 4f3744d

Please sign in to comment.