CxJS

PointReducer

import { PointReducer } from 'cx/charts'; Copied

PointReducer calculates aggregate values from points displayed on the chart. It provides three callback hooks for initializing an accumulator, processing each point, and computing final results.

<div controller={PageController}>
  <Svg style="width: 100%; height: 320px">
    <Chart
      margin="30 30 40 50"
      axes={{
        x: { type: NumericAxis, min: 0, max: 300 },
        y: { type: NumericAxis, min: 0, max: 300, vertical: true },
      }}
    >
      <Gridlines />
      <PointReducer
        onInitAccumulator={(acc) => {
          acc.sumX = 0
          acc.sumY = 0
          acc.sumSize = 0
        }}
        onMap={(acc, x, y, name, p) => {
          acc.sumX += x * p.size
          acc.sumY += y * p.size
          acc.sumSize += p.size
        }}
        onReduce={(acc, { store }) => {
          if (acc.sumSize > 0) {
            store.set(m.avgX, acc.sumX / acc.sumSize)
            store.set(m.avgY, acc.sumY / acc.sumSize)
          }
        }}
      >
        <Repeater records={m.points} recordAlias="$point">
          <Marker
            colorIndex={m.$point.color}
            size={m.$point.size}
            x={m.$point.x}
            y={m.$point.y}
            style={{ fillOpacity: 0.6 }}
            draggableX
            draggableY
          />
        </Repeater>
        <MarkerLine x={m.avgX} colorIndex={9} />
        <MarkerLine y={m.avgY} colorIndex={9} />
      </PointReducer>
    </Chart>
  </Svg>
  <div style="display: flex; justify-content: space-between; align-items: center; margin-top: 8px">
    <Button onClick="generate">Generate</Button>
    <span style="color: #666">Drag markers to see weighted average update</span>
  </div>
</div>
Drag markers to see weighted average update

How It Works

  1. onInitAccumulator - Initialize the accumulator object before processing
  2. onMap - Called for each point with (accumulator, x, y, name, data, array, index)
  3. onReduce - Calculate final values and write them to the store

Predefined Variants

CxJS provides specialized point reducers for common use cases:

Configuration

PropertyTypeDescription
onInitAccumulatorfunctionInitialize accumulator. Args: (accumulator, instance)
onMapfunctionProcess each point. Args: (accumulator, x, y, name, data, array, index)
onReducefunctionCalculate final values. Args: (accumulator, instance)
filterParamsobjectParameters passed to onCreatePointFilter.
onCreatePointFilterfunctionCreate a predicate for filtering points. Args: (filterParams, instance)