CxJS

Calculated Chart Height

Charts with a CategoryAxis can dynamically adjust their height based on the number of categories. This is done by binding the categoryCount output property and using computable to calculate the Svg height.

Use the slider to add more items and observe how the chart grows to accommodate them.

<div controller={PageController}>
  <Slider
    value={{ bind: m.itemCount.toString(), debounce: 100 }}
    minValue={1}
    maxValue={50}
    step={1}
    style="width: 200px; margin-bottom: 16px"
    help={<Text value={m.itemCount} />}
  />
  <Svg
    style={{
      width: "500px",
      height: computable(m.categoryCount, (count) => (count ?? 1) * 40 + 60),
      border: "1px dashed #ddd",
    }}
  >
    <Chart
      margin="20 20 40 100"
      axes={{
        y: <CategoryAxis vertical inverted categoryCount={m.categoryCount} />,
        x: <NumericAxis minLabelTickSize={1} />,
      }}
    >
      <Rectangle fill="white" />
      <Gridlines />
      <Repeater
        records={m.points}
        recordAlias="$point"
        sortField="count"
        sortDirection="DESC"
      >
        <Bar
          colorIndex={6}
          x={{ bind: "$point.count" }}
          y={{ bind: "$point.name" }}
          size={0.5}
        />
      </Repeater>
    </Chart>
  </Svg>
</div>
 
5

How It Works

  1. The CategoryAxis outputs the number of categories via the categoryCount binding
  2. A computable calculates the Svg height: (count) => count * 40 + 60
  3. The chart automatically resizes as categories are added or removed

Configuration

PropertyTypeDescription
categoryCountbindingOutput binding for the number of categories on the axis.