CxJS

Combined Chart

This example demonstrates how to connect a Grid and a Chart with shared selection and sorting. Clicking a row in the grid or a bar in the chart highlights both. Sorting the grid reorders the chart accordingly.

<div
  controller={PageController}
  style="display: flex; flex-wrap: wrap; gap: 16px"
>
  <Svg style="height: 350px; flex: 1; min-width: 400px">
    <Chart
      offset="20 -20 -40 120"
      axes={{
        x: <NumericAxis snapToTicks={1} />,
        y: <CategoryAxis vertical inverted />,
      }}
    >
      <Rectangle fill="white" />
      <Gridlines />
      <Repeater records={m.data} recordAlias="$point" sorters={m.sorters}>
        <Bar
          colorIndex={0}
          height={0.2}
          offset={-0.2}
          x={m.$point.y2022}
          y={m.$point.city}
          selection={barSelection}
          tooltip={format(m.$point.y2022, "n;0")}
        />
        <Bar
          colorIndex={2}
          height={0.2}
          offset={0}
          x={m.$point.y2023}
          y={m.$point.city}
          selection={barSelection}
          tooltip={format(m.$point.y2023, "n;0")}
        />
        <Bar
          colorIndex={4}
          height={0.2}
          offset={0.2}
          x={m.$point.y2024}
          y={m.$point.city}
          selection={barSelection}
          tooltip={format(m.$point.y2024, "n;0")}
        />
      </Repeater>
    </Chart>
  </Svg>
  <Grid
    style="flex: 1; min-width: 350px"
    records={m.data}
    sorters={m.sorters}
    columns={[
      { header: "City", field: "city", sortable: true },
      {
        field: "y2022",
        format: "n;0",
        align: "right",
        sortable: true,
        header: {
          items: (
            <div preserveWhitespace>
              2022 <div className="cxs-color-0" style={legendStyle} />
            </div>
          ),
        },
      },
      {
        field: "y2023",
        format: "n;0",
        align: "right",
        sortable: true,
        header: {
          items: (
            <div preserveWhitespace>
              2023 <div className="cxs-color-2" style={legendStyle} />
            </div>
          ),
        },
      },
      {
        field: "y2024",
        format: "n;0",
        align: "right",
        sortable: true,
        header: {
          items: (
            <div preserveWhitespace>
              2024 <div className="cxs-color-4" style={legendStyle} />
            </div>
          ),
        },
      },
    ]}
    selection={{ type: KeySelection, keyField: "id", bind: "selection" }}
  />
</div>
City
2022
2023
2024
Tokyo109190172
New York122191206
London109161246
Paris99242153
Sydney98239160
Berlin86195205
Toronto94224181

How It Works

  1. Shared sorters - Both Grid and Repeater bind to the same sorters array
  2. PropertySelection - A shared selection instance connects grid rows and chart bars
  3. Color-coded headers - Grid headers include colored boxes matching bar colors using cxs-color-* classes

Configuration

PropertyTypeDescription
sortersSorter[]Array of sort definitions shared between Grid and chart.
selectionPropertySelectionSelection instance to synchronize highlighting.
keyFieldstringField used to identify records in selection.