Related to:
- Is it possible to create/use a custom marker?
- Documentation or example on adding custom MarkerType on Scatter model
I too want to be able to able add custom markers types (without using images), and started poking around. I got things working, but there are some key things I still need to figure out, and I am not sure if these are dead ends or just tricky to figure out.
Below is an example drawing little suns (I also created a number of others just for the sake of messing around with the canvas context).
Using bokeh==3.6.0
sun.py
from bokeh.models import Marker
from bokeh.core.properties import Override
from bokeh.util.compiler import TypeScript
CODE = """
import { Marker, MarkerView } from "models/glyphs/marker";
export class SunView extends MarkerView {
declare model: Sun
_render_one = (ctx: any, i: number, r: number, visuals: any): void => {
const x = this.x[i];
const y = this.y[i];
const angle = Math.PI / 4.0;
ctx.beginPath();
ctx.arc(x, y, r, 0.0, 2.0 * Math.PI);
ctx.closePath();
visuals.line.apply(ctx, i)
visuals.fill.apply(ctx, i)
visuals.hatch.apply(ctx, i)
ctx.beginPath();
for (let j = 0; j < 8; j++) {
const cos = Math.cos(j * angle);
const sin = Math.sin(j * angle);
const ray_radius = r * (j % 2 == 0 ? 1.25 : 1.5);
const lx1 = x + r * cos;
const ly1 = y + r * sin;
const lx2 = x + ray_radius * cos;
const ly2 = y + ray_radius * sin;
ctx.moveTo(lx1, ly1);
ctx.lineTo(lx2, ly2);
}
ctx.closePath();
visuals.line.apply(ctx, i)
}
}
export class Sun extends Marker {
declare __view_type__: SunView
static {
this.prototype.default_view = SunView;
}
}
"""
class Sun(Marker):
__implementation__ = TypeScript(CODE) # Link to the compiled JavaScript file
"""
main.py
from bokeh.io import output_file
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show
from sun import Sun
p = figure(width=400, height=400)
source = ColumnDataSource(data=dict(x=[1, 2, 3], y=[1, 2, 3]))
p.add_glyph(source, Sun(x="x", y="y", size=20, fill_color='yellow', line_color='red'))
output_file("example.html")
show(p)
This works great:
However, this requires me to add_glyph
- I would love to be able to use scatter
and specify the marker type as sun
. I ultimately an using holoviews/hvplot, but obviously need to figure it out here before I tackle that side.
Can anyone provide any guidance on getting this to work? I know that there are no guarantees on the stability of the underlying API’s, but that is OK. Aside, if there are some odd things in the code provided and you have suggestions on how to do some of this better, I am very open to ideas! I am not familiar with lower level Bokeh code at all.