mosaic icon indicating copy to clipboard operation
mosaic copied to clipboard

vgplot: programatically enable/disable

Open declann opened this issue 10 months ago • 3 comments

Mosaic clients can be programatically enabled/disabled via #721 (example code using IntersectionObserver).

This same control in vgplot would be helpful for dashboard optimisation - so that out -of-view or collapsed charts can be prevented from updating.

declann avatar Apr 30 '25 20:04 declann

I'm not sure if this is a suitable workaround if you wanted to avoid creating custom clients, but I tried this:

  1. define a custom API context with separate coordinator for plots or groups of plots you want to hide:
const qcAPI = vg.createAPIContext({
  coordinator: new vg.Coordinator(vg.socketConnector())
});
  1. use the API context for your disable-able plot:
const percentMT = qcAPI.plot(
  qcAPI.densityY(qcAPI.from('cells', { filterBy: $densityFilter }), {
    x: 'percent_mt',
    fill: 'sample',
    opacity: 0.25,
    tip: true,
    title: 'sample',
    normalize: 'max',
  }),
  qcAPI.name('percentMT'),
  qcAPI.colorRange(tableau20),
  qcAPI.colorDomain(samples),
  qcAPI.intervalX({ as: $percentMTBrush }),
  qcAPI.xLabel(umapCategories.percent_mt.title),
  qcAPI.yAxis(null),
  qcAPI.width(containerWidth),
  qcAPI.height(88),
);
if (percentMTRef.current) percentMTRef.current.replaceChildren(percentMT);
  1. clear the API context whenever you hide the plot and/or on effect cleanup:
useEffect(() => { // plot qc if any dependences change
  if (!showQCDist) return;
  plotQC();
  return () => {
    if (percentMTRef.current) percentMTRef.current.replaceChildren(qcAPI.plot(qcAPI.name('percentMT')));
    qcAPI.context.coordinator.clear({ clients: true, cache: true });
  }
}, [samples, showQCDist]);

...

onClick={() => {
  if (showQCDist) { // reset brushes on hide only
    handleReset();
    if (percentMTRef.current) percentMTRef.current.replaceChildren(qcAPI.plot(qcAPI.name('percentMT')));
    qcAPI.context.coordinator.clear({ clients: true, cache: true });
  }
  setShowQCDist(!showQCDist);
}}

sanghoonio avatar Aug 04 '25 21:08 sanghoonio

That's an interesting workaround Sam - thanks for sharing!

declann avatar Aug 04 '25 23:08 declann

It's pretty brute force because it clears the client of the plot you are hiding (which resets the plot if you want to reenable), but it at least improves selection/query speed of my visible plots by a lot..

sanghoonio avatar Aug 04 '25 23:08 sanghoonio