Skip to content

Commit

Permalink
allow setting label font
Browse files Browse the repository at this point in the history
  • Loading branch information
bmschmidt committed Dec 16, 2024
1 parent 0491be5 commit 81981a0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export class Zoom {
if (event.sourceEvent) {
(event.sourceEvent as Event).stopPropagation();
}
});
})

canvas.call(zoomer);

Expand Down
18 changes: 14 additions & 4 deletions src/label_rendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class LabelMaker extends Renderer {
public label_key?: string;
public labelgroup: SVGGElement;
private hovered: undefined | string;
private font: string = 'verdana';
public options: DS.LabelOptions = {};
/**
*
Expand All @@ -46,6 +47,11 @@ export class LabelMaker extends Renderer {
) {
super(scatterplot.div!.node() as HTMLDivElement, scatterplot);
this.options = options;
if (options.font) {
this.font = options.font;
// else verdana
}

this.canvas = scatterplot
.elements![2].selectAll('canvas')
.node() as HTMLCanvasElement;
Expand Down Expand Up @@ -74,6 +80,7 @@ export class LabelMaker extends Renderer {
0.5,
[0.5, 1e6],
options.margin === undefined ? 30 : options.margin,
this.font
);

/* this.tree.accessor = (x, y) => {
Expand Down Expand Up @@ -264,7 +271,7 @@ export class LabelMaker extends Renderer {
if (this.hovered === '' + d.minZ + d.minX) {
emphasize += 2;
}
context.font = `${datum.height * size_adjust + emphasize}pt verdana`;
context.font = `${datum.height * size_adjust + emphasize}pt ${this.font}`;

context.shadowBlur = 12 + emphasize * 3;
context.lineWidth = 3 + emphasize;
Expand Down Expand Up @@ -380,13 +387,13 @@ function getContext(): CanvasRenderingContext2D {
return context;
}

function measure_text(d: RawPoint, pixel_ratio: number, margin: number) {
function measure_text(d: RawPoint, pixel_ratio: number, margin: number, font: string) {
// Uses a global context that it calls into existence for measuring;
// using the deepscatter
// canvas gets too confused with state information.
const context = getContext();
// Called for the side-effect of setting `d.aspect_ratio` on the passed item.
context.font = `${d.height}pt verdana`;
context.font = `${d.height}pt ${font}`;
if (d.text === '') {
return null;
}
Expand Down Expand Up @@ -427,6 +434,7 @@ class DepthTree extends RBush3D {
public pixel_ratio: number;
public rectangle_buffer: number;
public margin: number;
public font: string;
// public insertion_log = [];
private _accessor: (p: Point) => [number, number] = (p) => [p.x, p.y];

Expand All @@ -440,12 +448,14 @@ class DepthTree extends RBush3D {
scale_factor = 0.5,
zoom = [0.1, 1000],
margin = 10, // in screen pixels
font = 'verdana',
) {
// scale factor used to determine how quickly points scale.
// Not implemented.
// size = exp(log(k) * scale_factor);

super();
this.font = font;
this.scale_factor = scale_factor;
this.mindepth = zoom[0];
this.maxdepth = zoom[1];
Expand Down Expand Up @@ -520,7 +530,7 @@ class DepthTree extends RBush3D {
if (point['pixel_width'] === undefined) {
measured = {
...point,
...measure_text(point, this.pixel_ratio, this.margin),
...measure_text(point, this.pixel_ratio, this.margin, this.font),
};
} else {
measured = point as Point;
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ export type LabelOptions = {
useColorScale?: boolean; // Whether the colors of text should inherit from the active color scale.
margin?: number; // The number of pixels around each box. Default 30.
draggable_labels?: boolean; // Should labels be draggable in place?
font?: string // Font. Default verdana.
};

export type Labelset = {
Expand Down

0 comments on commit 81981a0

Please sign in to comment.