CxJS

Legend

import { Legend, LegendEntry } from 'cx/charts'; Copied

The Legend component displays an index of chart elements. Chart components like LineGraph, ColumnGraph, and PieSlice automatically register with the legend when they have a name property.

Click on legend entries to toggle visibility of the corresponding series. This requires binding the active property on chart components.

<div controller={PageController}>
  <Legend />
  <Svg style="width: 500px; height: 300px; border: 1px dashed #ddd">
    <Chart
      margin="20 20 40 50"
      axes={{
        x: <NumericAxis />,
        y: <NumericAxis vertical />,
      }}
    >
      <Rectangle fill="white" />
      <Gridlines />
      <LineGraph
        name="Sales"
        data={m.data}
        colorIndex={0}
        yField="sales"
        active={m.salesActive}
      />
      <LineGraph
        name="Profit"
        data={m.data}
        colorIndex={4}
        yField="profit"
        active={m.profitActive}
      />
      <LineGraph
        name="Costs"
        data={m.data}
        colorIndex={8}
        yField="costs"
        active={m.costsActive}
      />
    </Chart>
  </Svg>
</div>
Sales
Profit
Costs

When multiple charts are on the same page, use Legend.Scope to prevent legends from interfering with each other.

Configuration

PropertyTypeDescription
verticalbooleanSet to true for vertical layout.
shapestringShape of the symbol in legend entries (circle, square, triangle, etc).
showValuesbooleanSet to true to show values in the legend.
valueFormatstringFormat string for legend entry values. Default is "s".
entryClassstring/objectAdditional CSS classes for legend entry elements.
entryStylestring/objectAdditional CSS styles for legend entry elements.
valueClassstring/objectAdditional CSS classes for legend entry values.
valueStylestring/objectAdditional CSS styles for legend entry values.