Skip to content

Commit

Permalink
Clean up factor range and add factor method (#14037)
Browse files Browse the repository at this point in the history
* Clean up factor range and add factor method

* remove spurious comment

* formatting

* any -> unknown

* docs comments

* remove unnecessary internal properties

* fix levels property cruft

* use switch for range.levels

* remove axis offsets cruft

* split off typed FactorMapper

* misc formatting

* fix test

* better error type

* don't use .at
  • Loading branch information
bryevdv authored Aug 27, 2024
1 parent d0f895a commit c7368dc
Show file tree
Hide file tree
Showing 6 changed files with 1,315 additions and 1,129 deletions.
15 changes: 4 additions & 11 deletions bokehjs/src/lib/models/axes/axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,9 @@ export class AxisView extends GuideRendererView {

const [sxs, sys] = this.scoords(coords)
const [nx, ny] = this.normals
const [xoff, yoff] = this.offsets

const [nxin, nyin] = [nx * (xoff-tin), ny * (yoff-tin)]
const [nxout, nyout] = [nx * (xoff+tout), ny * (yoff+tout)]
const [nxin, nyin] = [nx * -tin, ny * -tin]
const [nxout, nyout] = [nx * tout, ny * tout]

visuals.set_value(ctx)

Expand All @@ -360,12 +359,11 @@ export class AxisView extends GuideRendererView {
}

const [sxs, sys] = this.scoords(coords)
const [xoff, yoff] = this.offsets

const [nx, ny] = this.normals

const nxd = nx*(xoff + standoff)
const nyd = ny*(yoff + standoff)
const nxd = nx*standoff
const nyd = ny*standoff

const {vertical_align, align} = this.panel.get_label_text_heuristics(orient)
const angle = this.panel.get_label_angle_heuristic(orient)
Expand Down Expand Up @@ -557,11 +555,6 @@ export class AxisView extends GuideRendererView {
}
}

// TODO Remove this.
get offsets(): [number, number] {
return [0, 0]
}

get ranges(): [Range, Range] {
const i = this.dimension
const j = 1 - i
Expand Down
56 changes: 33 additions & 23 deletions bokehjs/src/lib/models/axes/categorical_axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export class CategoricalAxisView extends AxisView {
const [range] = this.ranges as [FactorRange, FactorRange]
const [start, end] = this.computed_bounds

if (range.tops == null || range.tops.length < 2 || !this.visuals.separator_line.doit) {
const {factors} = range
const {tops} = range.mapper
if (tops == null || tops.length < 2 || !this.visuals.separator_line.doit) {
return
}

Expand All @@ -46,12 +48,12 @@ export class CategoricalAxisView extends AxisView {
const coords: Coords = [[], []]

let ind = 0
for (let i = 0; i < range.tops.length - 1; i++) {
for (let i = 0; i < tops.length - 1; i++) {
let first: Factor, last: Factor

for (let j = ind; j < range.factors.length; j++) {
if (range.factors[j][0] == range.tops[i+1]) {
[first, last] = [range.factors[j-1], range.factors[j]]
for (let j = ind; j < factors.length; j++) {
if (factors[j][0] == tops[i+1]) {
[first, last] = [factors[j-1], factors[j]]
ind = j
break
}
Expand Down Expand Up @@ -109,22 +111,29 @@ export class CategoricalAxisView extends AxisView {
return map(this.model.formatter.doFormat(ticks, this))
}

if (range.levels == 1) {
const major = ticks.major as L1Factor[]
const labels = format(major)
info.push([labels, coords.major, this.model.major_label_orientation, this.visuals.major_label_text])
} else if (range.levels == 2) {
const major = (ticks.major as L2Factor[]).map((x) => x[1])
const labels = format(major)
info.push([labels, coords.major, this.model.major_label_orientation, this.visuals.major_label_text])
info.push([map(ticks.tops as string[]), coords.tops, this.model.group_label_orientation, this.visuals.group_text])
} else if (range.levels == 3) {
const major = (ticks.major as L3Factor[]).map((x) => x[2])
const labels = format(major)
const mid_labels = ticks.mids.map((x) => x[1])
info.push([labels, coords.major, this.model.major_label_orientation, this.visuals.major_label_text])
info.push([map(mid_labels), coords.mids, this.model.subgroup_label_orientation, this.visuals.subgroup_text])
info.push([map(ticks.tops as string[]), coords.tops, this.model.group_label_orientation, this.visuals.group_text])
switch (range.mapper.levels) {
case 1: {
const major = ticks.major as L1Factor[]
const labels = format(major)
info.push([labels, coords.major, this.model.major_label_orientation, this.visuals.major_label_text])
break
}
case 2: {
const major = (ticks.major as L2Factor[]).map((x) => x[1])
const labels = format(major)
info.push([labels, coords.major, this.model.major_label_orientation, this.visuals.major_label_text])
info.push([map(ticks.tops as L1Factor[]), coords.tops, this.model.group_label_orientation, this.visuals.group_text])
break
}
case 3: {
const major = (ticks.major as L3Factor[]).map((x) => x[2])
const labels = format(major)
const mid_labels = ticks.mids.map((x) => x[1])
info.push([labels, coords.major, this.model.major_label_orientation, this.visuals.major_label_text])
info.push([map(mid_labels), coords.mids, this.model.subgroup_label_orientation, this.visuals.subgroup_text])
info.push([map(ticks.tops as L1Factor[]), coords.tops, this.model.group_label_orientation, this.visuals.group_text])
break
}
}

return info
Expand All @@ -148,12 +157,13 @@ export class CategoricalAxisView extends AxisView {
coords.major[i] = ticks.major as any
coords.major[j] = ticks.major.map(() => this.loc)

if (range.levels == 3) {
const {levels} = range.mapper
if (levels == 3) {
coords.mids[i] = ticks.mids as any
coords.mids[j] = ticks.mids.map(() => this.loc)
}

if (range.levels > 1) {
if (levels > 1) {
coords.tops[i] = ticks.tops as any
coords.tops[j] = ticks.tops.map(() => this.loc)
}
Expand Down
Loading

0 comments on commit c7368dc

Please sign in to comment.