Skip to content

Commit a359b30

Browse files
authored
allow setting label font (#174)
<!-- ELLIPSIS_HIDDEN --> > [!IMPORTANT] > Add support for setting label font in Deepscatter via `font` option in `LabelMaker`. > > - **Behavior**: > - Allow setting label font via `font` option in `LabelMaker` in `label_rendering.ts`. > - Default font is 'verdana' if not specified. > - **Functions**: > - Update `measure_text()` in `label_rendering.ts` to use specified `font`. > - Update `DepthTree` constructor in `label_rendering.ts` to accept `font` parameter. > - **Types**: > - Add `font` option to `LabelOptions` in `types.ts`. > - **Misc**: > - Fix duplicate import in `wrap_arrow.ts`. > > <sup>This description was created by </sup>[<img alt="Ellipsis" src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=nomic-ai%2Fdeepscatter&utm_source=github&utm_medium=referral)<sup> for bdea4f6. You can [customize](https://app.ellipsis.dev/nomic-ai/settings/summaries) this summary. It will automatically update as commits are pushed.</sup> <!-- ELLIPSIS_HIDDEN -->
1 parent f49f0a5 commit a359b30

File tree

5 files changed

+18
-8
lines changed

5 files changed

+18
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "deepscatter",
33
"type": "module",
4-
"version": "3.0.0-next.50",
4+
"version": "3.0.0-next.47",
55
"description": "Fast, animated zoomable scatterplots scaling to billions of points",
66
"files": [
77
"dist"

src/interaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export class Zoom {
173173
if (event.sourceEvent) {
174174
(event.sourceEvent as Event).stopPropagation();
175175
}
176-
});
176+
})
177177

178178
canvas.call(zoomer);
179179

src/label_rendering.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export class LabelMaker extends Renderer {
3232
public label_key?: string;
3333
public labelgroup: SVGGElement;
3434
private hovered: undefined | string;
35+
private font: string = 'verdana';
3536
public options: DS.LabelOptions = {};
3637
/**
3738
*
@@ -46,6 +47,11 @@ export class LabelMaker extends Renderer {
4647
) {
4748
super(scatterplot.div!.node() as HTMLDivElement, scatterplot);
4849
this.options = options;
50+
if (options.font) {
51+
this.font = options.font;
52+
// else verdana
53+
}
54+
4955
this.canvas = scatterplot
5056
.elements![2].selectAll('canvas')
5157
.node() as HTMLCanvasElement;
@@ -74,6 +80,7 @@ export class LabelMaker extends Renderer {
7480
0.5,
7581
[0.5, 1e6],
7682
options.margin === undefined ? 30 : options.margin,
83+
this.font
7784
);
7885

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

269276
context.shadowBlur = 12 + emphasize * 3;
270277
context.lineWidth = 3 + emphasize;
@@ -380,13 +387,13 @@ function getContext(): CanvasRenderingContext2D {
380387
return context;
381388
}
382389

383-
function measure_text(d: RawPoint, pixel_ratio: number, margin: number) {
390+
function measure_text(d: RawPoint, pixel_ratio: number, margin: number, font: string) {
384391
// Uses a global context that it calls into existence for measuring;
385392
// using the deepscatter
386393
// canvas gets too confused with state information.
387394
const context = getContext();
388395
// Called for the side-effect of setting `d.aspect_ratio` on the passed item.
389-
context.font = `${d.height}pt verdana`;
396+
context.font = `${d.height}pt ${font}`;
390397
if (d.text === '') {
391398
return null;
392399
}
@@ -427,6 +434,7 @@ class DepthTree extends RBush3D {
427434
public pixel_ratio: number;
428435
public rectangle_buffer: number;
429436
public margin: number;
437+
public font: string;
430438
// public insertion_log = [];
431439
private _accessor: (p: Point) => [number, number] = (p) => [p.x, p.y];
432440

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

448457
super();
458+
this.font = font;
449459
this.scale_factor = scale_factor;
450460
this.mindepth = zoom[0];
451461
this.maxdepth = zoom[1];
@@ -520,7 +530,7 @@ class DepthTree extends RBush3D {
520530
if (point['pixel_width'] === undefined) {
521531
measured = {
522532
...point,
523-
...measure_text(point, this.pixel_ratio, this.margin),
533+
...measure_text(point, this.pixel_ratio, this.margin, this.font),
524534
};
525535
} else {
526536
measured = point as Point;

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import type {
1111
Timestamp,
1212
Utf8,
1313
Vector,
14-
TypeMap,
1514
} from 'apache-arrow';
1615
import type { Renderer } from './rendering';
1716
import type { Deeptable } from './Deeptable';
@@ -520,6 +519,7 @@ export type LabelOptions = {
520519
useColorScale?: boolean; // Whether the colors of text should inherit from the active color scale.
521520
margin?: number; // The number of pixels around each box. Default 30.
522521
draggable_labels?: boolean; // Should labels be draggable in place?
522+
font?: string; // Font. Default verdana.
523523
};
524524

525525
export type Labelset = {

src/wrap_arrow.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
import { Deeptable } from './Deeptable';
1515
import { add_or_delete_column } from './Deeptable';
1616
import type * as DS from './types';
17-
import { extent, extent, range } from 'd3-array';
17+
import { extent, range } from 'd3-array';
1818
import { Rectangle } from './tile';
1919
import { tixToZxy } from './tixrixqid';
2020

0 commit comments

Comments
 (0)