CxJS

ColorMap

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

ColorMap automatically assigns colors from the palette to chart elements. Elements register themselves by name and receive consistent color indices. This is useful when the number of series is dynamic.

<div controller={PageController}>
  <Legend />
  <Svg style="width: 100%; height: 280px">
    <Chart
      margin="20 20 40 50"
      axes={{
        x: { type: NumericAxis },
        y: { type: NumericAxis, vertical: true },
      }}
    >
      <Gridlines />
      <ColorMap />
      <Repeater records={m.series} recordAlias="$record">
        <LineGraph
          name={m.$record.name}
          active={m.$record.active}
          data={m.$record.points}
          colorMap="lines"
        />
      </Repeater>
    </Chart>
  </Svg>
</div>
Series 1
Series 2
Series 3
Series 4
Series 5

How It Works

  1. Place ColorMap above chart elements that need automatic colors
  2. Elements use colorMap="mapName" to register with a specific color map
  3. ColorMap assigns colors based on element names, distributing them evenly across the palette
  4. Use ColorMap.Scope to isolate multiple color maps in nested charts

Configuration

PropertyTypeDefaultDescription
offsetnumber0Starting offset in the color palette.
stepnumberStep between colors. Auto-calculated if not specified.
sizenumber16Size of the color palette.
namesstring[]Pre-register names to ensure consistent color assignment.