Skip to content

Legend

Configure the chart legend to help users understand your data series.

Basic Configuration

typescript
import type { LegendConfig } from '@nstudio/ncharts';

const legend: LegendConfig = {
  enabled: true,
  horizontalAlignment: 'CENTER',
  verticalAlignment: 'BOTTOM',
  orientation: 'HORIZONTAL',
  form: 'CIRCLE',
  textSize: 11,
};

Legend Properties

Visibility

PropertyTypeDefaultDescription
enabledbooleantrueShow/hide legend

Positioning

PropertyTypeDefaultDescription
horizontalAlignmentLegendHorizontalAlignment'LEFT'Horizontal position
verticalAlignmentLegendVerticalAlignment'BOTTOM'Vertical position
drawInsidebooleanfalseDraw inside chart area
typescript
type LegendHorizontalAlignment = 'LEFT' | 'CENTER' | 'RIGHT';
type LegendVerticalAlignment = 'TOP' | 'CENTER' | 'BOTTOM';

Layout

PropertyTypeDefaultDescription
orientationLegendOrientation'HORIZONTAL'Entry layout direction
directionLegendDirection'LEFT_TO_RIGHT'Text direction
wordWrapEnabledbooleanfalseWrap text to multiple lines
maxSizePercentnumber0.95Max size as % of chart
typescript
type LegendOrientation = 'HORIZONTAL' | 'VERTICAL';
type LegendDirection = 'LEFT_TO_RIGHT' | 'RIGHT_TO_LEFT';

Form (Symbol) Shape

PropertyTypeDefaultDescription
formLegendForm'DEFAULT'Symbol shape
formSizenumber-Symbol size in dp
typescript
type LegendForm = 
  | 'NONE'     // No symbol
  | 'EMPTY'    // Empty/hollow
  | 'DEFAULT'  // Default for chart type
  | 'SQUARE'   // Square shape
  | 'CIRCLE'   // Circle shape
  | 'LINE';    // Line segment

Text Styling

PropertyTypeDefaultDescription
textColorChartColor-Text color
textSizenumber10Text size in dp
fontFamilystring-Font family name
fontStylenumber00=normal, 1=bold, 2=italic
fontWeightnumber-Font weight

Spacing

PropertyTypeDefaultDescription
xEntrySpacenumber-Horizontal space between entries
yEntrySpacenumber-Vertical space between entries
formToTextSpacenumber-Space between form and text

Common Configurations

typescript
const legend: LegendConfig = {
  enabled: true,
  horizontalAlignment: 'CENTER',
  verticalAlignment: 'BOTTOM',
  orientation: 'HORIZONTAL',
  form: 'CIRCLE',
  textSize: 11,
  xEntrySpace: 20,
};

Top Right

typescript
const legend: LegendConfig = {
  enabled: true,
  horizontalAlignment: 'RIGHT',
  verticalAlignment: 'TOP',
  orientation: 'VERTICAL',
  form: 'SQUARE',
  textSize: 10,
};

Inside Chart (Top Left)

typescript
const legend: LegendConfig = {
  enabled: true,
  horizontalAlignment: 'LEFT',
  verticalAlignment: 'TOP',
  drawInside: true,
  orientation: 'VERTICAL',
  form: 'LINE',
  textSize: 10,
};

Custom Legend Entries

Override the automatic legend with custom entries:

typescript
const legend: LegendConfig = {
  enabled: true,
  custom: {
    colors: ['#3B82F6', '#10B981', '#F59E0B'],
    labels: ['Revenue', 'Expenses', 'Profit'],
  },
  horizontalAlignment: 'CENTER',
  verticalAlignment: 'BOTTOM',
  form: 'CIRCLE',
};

Template Usage

html
<LineChart
  [data]="chartData"
  [legend]="{
    enabled: true,
    horizontalAlignment: 'CENTER',
    verticalAlignment: 'BOTTOM',
    orientation: 'HORIZONTAL',
    form: 'CIRCLE',
    textSize: 11,
    textColor: '#374151'
  }">
</LineChart>

Or with a component property:

typescript
legend: LegendConfig = {
  enabled: true,
  horizontalAlignment: 'CENTER',
  verticalAlignment: 'BOTTOM',
  orientation: 'HORIZONTAL',
  form: 'CIRCLE',
};
html
<LineChart [data]="chartData" [legend]="legend"></LineChart>

Legend Forms by Chart Type

Chart TypeRecommended Form
Line Chart'LINE' or 'CIRCLE'
Bar Chart'SQUARE'
Pie Chart'CIRCLE'
Scatter'CIRCLE'
Radar'CIRCLE'

Disable Legend

typescript
const legend: LegendConfig = {
  enabled: false,
};

Or simply omit the legend configuration.

Responsive Considerations

For smaller screens:

typescript
const mobileLegend: LegendConfig = {
  enabled: true,
  horizontalAlignment: 'CENTER',
  verticalAlignment: 'BOTTOM',
  orientation: 'HORIZONTAL',
  form: 'CIRCLE',
  formSize: 8,
  textSize: 9,
  xEntrySpace: 10,
  wordWrapEnabled: true,
};

See Also