In Observable/d3/javascript, how do you apply a filter from based on a action in one plot to another plot?

I have 2 functions in Observable which each create a plot. I designed it so that when I click the squares in the legend of the first plot, it filters which data is displayed (there are 7 lines for each. of 7 object_types). That works! But I want to also filter the second plot based on the selections made in that first plot. The 2 functions use different datasets for their plots.

I combine the 2 functions with this code:

{
 
  const { svg , selectedObjectsMap } = objects_over_time()
  const vis_right = plot_objects(selectedObjectsMap);

  return html`<div style="display: flex">${svg}${vis_right}</div>`;
} 

The first function returns an svg element and a map which maps each of 7 object_types to either true or false.

The plot_objects() function is called with this true/false map as an argument whenever a square is clicked in the first plot.

  function onclick(event, d) {
    const selectedObjectType = d3.select(event.target).datum();
    const isSelected = selected.get(selectedObjectType);
    const square = d3.select(this);
    square.attr('fill', isSelected ? 'white' : color(selectedObjectType));
    selected.set(selectedObjectType, !isSelected);
    
    const filteredData = new Map();
    objects_grouped.forEach((group, object_type) => {

        const includeObject = selected.get(object_type);
        const filteredGroup = includeObject ? group.filter(obj => obj.location === selectedLocation) : [];
      
        filteredData.set(object_type, filteredGroup);
    });

    svg.selectAll('#plot-line').remove();
    drawLines(filteredData);
    plot_objects(selected);
  }

The plot_objects() function has a function updateVisualization() which draws new datapoints based on the filtering. This works for the location filter which is done in a separate drop-down menu. I’ve printed console.log statements and I can see it has the right data and filters it properly, but it just doesn’t update the data points.

  function updateVisualization(location) {

    svg.selectAll('#data-points').remove()
    const filteredData = all_data.filter(d => d.location === location &&
                                         selectedObjects.has(d.object_type) &&
                                         selectedObjects.get(d.object_type));
    console.log("plot_objects:", filteredData)
    drawPoints(filteredData)
    updateBackground(location)
  }

  updateVisualization(selectedLocation)

  function drawPoints(data) {
    console.log("in drawPoints function:", data)
    svg.selectAll('rect:not(#tooltip-rect):not(#image)')
      .data(data)
      .join('rect')
        .attr('id', 'data-points')
        .attr('x', d => x_rv(d.x))
        .attr('y', d => y_rv(d.y))
        .attr('fill', d =>  color(d.object_type))
        .attr('opacity', 0.5)
        .attr('width', d => sizeScaleW(d.object_type))
        .attr('height', d => sizeScaleH(d.object_type))
        .on('mouseenter', handleMouseEnter)
        .on('mouseleave', handleMouseLeave)
        .raise();
  }

Changing svg.selectAll('#data-points').remove() to d3.selectAll('#data-points').remove() in the updateVisualization function did finally remove all of the data points, but none are added back in. So it seems like all of the functions are being called as expected, but I must be missing a step to make the changes actually show up in the plot.

New contributor

rytwin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật