Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

227
Views
Bokeh CustomJS Piechart

I am trying to implement js_on_change on Pie graph but am unable to do so. I am new to bokeh and JavaScript. Below is my code. So basically I want to implement a functionality that whenever I deselect any fruit, then my Pie chart should redraw itself by excluding that fruit from the calculation.

My code is below.

from bokeh.layouts import column
from bokeh.models import ColumnDataSource,CustomJS,Slider
from bokeh.models import CheckboxGroup
from math import pi
import pandas as pd
from bokeh.io import show
from bokeh.plotting import figure
from bokeh.transform import cumsum 

from bokeh.palettes import Category10
fruits = {'Apple': 10,
          'Mango': 24,
          'Graphes': 9,
          'Banana': 15,
          'Peach': 6,
          'Pinapple' : 7,
    }

data = pd.Series(fruits).reset_index(name='value').rename(columns={'index':'Fruits'})
data['angle']=data['value']/data['value'].sum() * 2 *pi
data['color'] = Category10[len(fruits)]
data['percent']= data['value']/sum(fruits.values()) *100
p=figure(plot_height= 350 , title='Pie chart',tools = "hover",
         tooltips="@Fruits: @value  : @percent{0.2f} %", x_range=(-0.5,1.0))
p.wedge(x=0, y=1, radius=0.4,
        start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'),
        line_color="white", fill_color='color', legend='Fruits', source=data)
p.axis.axis_label=None
p.axis.visible=False
p.grid.grid_line_color=None

callback =CustomJS(args = {'source':fruits},
code="""
var active=cb_obj.active;
const data = source;
const data1 = source;

var key1 = Object.keys(data)
var values1 = Object.values(data)
var new_fruit={}

var i=0
for (const [key,value] of Object.entries(data)) {
console.log(key,value);
console.log(active.includes(i));
var bool = active.includes(i);
if (bool==true)
  { console.log(active.includes(i));
    new_fruit[key]=value;

}
i=i+1
}


console.log("Callback completed");
console.log(active);
console.log(new_fruit);
"""
                  )
L = ["Apple","Mango","Graphes","Banana","Peach","Pinapple"]

checkbox_group = CheckboxGroup(labels=L, active =[0,1,2,3,4,5])
checkbox_group.js_on_change('active',callback)
layout = column(checkbox_group,p)

show(layout)

This is the image of PieChart I'm getting

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!