v2.15.0

Chart New

Строит данные линиями, областями, столбцами или кольцом; SVG отрисовывается на сервере.

Использование 2.15+

Компонент Chart построен на TanStack Charts и отрисовывает SVG на сервере.

TanStack Charts — необязательная peer-зависимость, сначала установите её:

pnpm add @tanstack/charts

Данные

Пропс data принимает массив объектов, пропс index задаёт ключ для оси X, а пропс categories — ряды: у каждого есть key со значениями и необязательные label и color.

<script setup lang="ts">
import type { ChartData } from '@bxshefby/b24ui-nuxt'

const data = ref<ChartData[]>([
  {
    month: 'Январь',
    desktop: 186,
    mobile: 80
  },
  {
    month: 'Февраль',
    desktop: 305,
    mobile: 200
  },
  {
    month: 'Март',
    desktop: 237,
    mobile: 120
  },
  {
    month: 'Апрель',
    desktop: 73,
    mobile: 190
  },
  {
    month: 'Май',
    desktop: 209,
    mobile: 130
  },
  {
    month: 'Июнь',
    desktop: 214,
    mobile: 140
  }
])
</script>

<template>
  <B24Chart
    :data="data"
    index="month"
    :categories="{
      '0': {
        key: 'desktop',
        label: 'Компьютер'
      },
      '1': {
        key: 'mobile',
        label: 'Телефон'
      }
    }"
  />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ChartData } from '@bxshefby/b24ui-nuxt'

const data = ref<ChartData[]>([
  {
    month: 'Январь',
    desktop: 186,
    mobile: 80
  },
  {
    month: 'Февраль',
    desktop: 305,
    mobile: 200
  },
  {
    month: 'Март',
    desktop: 237,
    mobile: 120
  },
  {
    month: 'Апрель',
    desktop: 73,
    mobile: 190
  },
  {
    month: 'Май',
    desktop: 209,
    mobile: 130
  },
  {
    month: 'Июнь',
    desktop: 214,
    mobile: 140
  }
])
</script>

<template>
  <B24Chart
    :data="data"
    index="month"
    :categories="{
      '0': {
        key: 'desktop',
        label: 'Компьютер'
      },
      '1': {
        key: 'mobile',
        label: 'Телефон'
      }
    }"
  />
</template>
Категории можно передать и строками, например ['desktop', 'mobile'], если они хорошо читаются как названия.
Пропс aria-label задаёт название графика для вспомогательных технологий.

Тип

Пропс type меняет тип графика. По умолчанию — line.

При donut каждая строка data — отдельный сектор. index задаёт его подпись, а первый ключ categories — значение.
<script setup lang="ts">
import type { ChartData } from '@bxshefby/b24ui-nuxt'

const data = ref<ChartData[]>([
  {
    month: 'Январь',
    desktop: 186,
    mobile: 80
  },
  {
    month: 'Февраль',
    desktop: 305,
    mobile: 200
  },
  {
    month: 'Март',
    desktop: 237,
    mobile: 120
  },
  {
    month: 'Апрель',
    desktop: 73,
    mobile: 190
  },
  {
    month: 'Май',
    desktop: 209,
    mobile: 130
  },
  {
    month: 'Июнь',
    desktop: 214,
    mobile: 140
  }
])
</script>

<template>
  <B24Chart
    type="donut"
    :data="data"
    index="month"
    :categories="{
      '0': {
        key: 'desktop',
        label: 'Компьютер'
      },
      '1': {
        key: 'mobile',
        label: 'Телефон'
      }
    }"
  />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ChartData } from '@bxshefby/b24ui-nuxt'

const data = ref<ChartData[]>([
  {
    month: 'Январь',
    desktop: 186,
    mobile: 80
  },
  {
    month: 'Февраль',
    desktop: 305,
    mobile: 200
  },
  {
    month: 'Март',
    desktop: 237,
    mobile: 120
  },
  {
    month: 'Апрель',
    desktop: 73,
    mobile: 190
  },
  {
    month: 'Май',
    desktop: 209,
    mobile: 130
  },
  {
    month: 'Июнь',
    desktop: 214,
    mobile: 140
  }
])
</script>

<template>
  <B24Chart
    type="donut"
    :data="data"
    index="month"
    :categories="{
      '0': {
        key: 'desktop',
        label: 'Компьютер'
      },
      '1': {
        key: 'mobile',
        label: 'Телефон'
      }
    }"
  />
</template>

С накоплением

Пропс stacked складывает ряды друг на друга.

Пропс работает только при type равном area или bar.
<script setup lang="ts">
import type { ChartData } from '@bxshefby/b24ui-nuxt'

const data = ref<ChartData[]>([
  {
    month: 'Январь',
    desktop: 186,
    mobile: 80
  },
  {
    month: 'Февраль',
    desktop: 305,
    mobile: 200
  },
  {
    month: 'Март',
    desktop: 237,
    mobile: 120
  },
  {
    month: 'Апрель',
    desktop: 73,
    mobile: 190
  },
  {
    month: 'Май',
    desktop: 209,
    mobile: 130
  },
  {
    month: 'Июнь',
    desktop: 214,
    mobile: 140
  }
])
</script>

<template>
  <B24Chart
    type="bar"
    stacked
    :data="data"
    index="month"
    :categories="{
      '0': {
        key: 'desktop',
        label: 'Компьютер'
      },
      '1': {
        key: 'mobile',
        label: 'Телефон'
      }
    }"
  />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ChartData } from '@bxshefby/b24ui-nuxt'

const data = ref<ChartData[]>([
  {
    month: 'Январь',
    desktop: 186,
    mobile: 80
  },
  {
    month: 'Февраль',
    desktop: 305,
    mobile: 200
  },
  {
    month: 'Март',
    desktop: 237,
    mobile: 120
  },
  {
    month: 'Апрель',
    desktop: 73,
    mobile: 190
  },
  {
    month: 'Май',
    desktop: 209,
    mobile: 130
  },
  {
    month: 'Июнь',
    desktop: 214,
    mobile: 140
  }
])
</script>

<template>
  <B24Chart
    type="bar"
    stacked
    :data="data"
    index="month"
    :categories="{
      '0': {
        key: 'desktop',
        label: 'Компьютер'
      },
      '1': {
        key: 'mobile',
        label: 'Телефон'
      }
    }"
  />
</template>

Кривая

Пропс curve меняет форму линии между значениями. По умолчанию — linear.

Пропс работает только при type равном line или area.
<script setup lang="ts">
import type { ChartData } from '@bxshefby/b24ui-nuxt'

const data = ref<ChartData[]>([
  {
    month: 'Январь',
    desktop: 186,
    mobile: 80
  },
  {
    month: 'Февраль',
    desktop: 305,
    mobile: 200
  },
  {
    month: 'Март',
    desktop: 237,
    mobile: 120
  },
  {
    month: 'Апрель',
    desktop: 73,
    mobile: 190
  },
  {
    month: 'Май',
    desktop: 209,
    mobile: 130
  },
  {
    month: 'Июнь',
    desktop: 214,
    mobile: 140
  }
])
</script>

<template>
  <B24Chart
    type="line"
    curve="monotone"
    :data="data"
    index="month"
    :categories="{
      '0': {
        key: 'desktop',
        label: 'Компьютер'
      },
      '1': {
        key: 'mobile',
        label: 'Телефон'
      }
    }"
  />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ChartData } from '@bxshefby/b24ui-nuxt'

const data = ref<ChartData[]>([
  {
    month: 'Январь',
    desktop: 186,
    mobile: 80
  },
  {
    month: 'Февраль',
    desktop: 305,
    mobile: 200
  },
  {
    month: 'Март',
    desktop: 237,
    mobile: 120
  },
  {
    month: 'Апрель',
    desktop: 73,
    mobile: 190
  },
  {
    month: 'Май',
    desktop: 209,
    mobile: 130
  },
  {
    month: 'Июнь',
    desktop: 214,
    mobile: 140
  }
])
</script>

<template>
  <B24Chart
    type="line"
    curve="monotone"
    :data="data"
    index="month"
    :categories="{
      '0': {
        key: 'desktop',
        label: 'Компьютер'
      },
      '1': {
        key: 'mobile',
        label: 'Телефон'
      }
    }"
  />
</template>

Цвета

Пропс colors задаёт цвет каждого ряда: цветами air-* (air-primary, air-primary-success, air-primary-alert, air-primary-warning, air-primary-copilot, air-secondary-no-accent) или любым CSS-цветом.

Цвета назначаются в порядке categories, а при donut — в порядке data. color, заданный у категории, приоритетнее. Если рядов больше, чем цветов, цвета повторяются в более светлых оттенках.
<script setup lang="ts">
import type { ChartData } from '@bxshefby/b24ui-nuxt'

const data = ref<ChartData[]>([
  {
    month: 'Январь',
    desktop: 186,
    mobile: 80
  },
  {
    month: 'Февраль',
    desktop: 305,
    mobile: 200
  },
  {
    month: 'Март',
    desktop: 237,
    mobile: 120
  },
  {
    month: 'Апрель',
    desktop: 73,
    mobile: 190
  },
  {
    month: 'Май',
    desktop: 209,
    mobile: 130
  },
  {
    month: 'Июнь',
    desktop: 214,
    mobile: 140
  }
])
</script>

<template>
  <B24Chart
    :colors="{
      '0': 'air-primary-success',
      '1': '#8b5cf6'
    }"
    :data="data"
    index="month"
    :categories="{
      '0': {
        key: 'desktop',
        label: 'Компьютер'
      },
      '1': {
        key: 'mobile',
        label: 'Телефон'
      }
    }"
  />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ChartData } from '@bxshefby/b24ui-nuxt'

const data = ref<ChartData[]>([
  {
    month: 'Январь',
    desktop: 186,
    mobile: 80
  },
  {
    month: 'Февраль',
    desktop: 305,
    mobile: 200
  },
  {
    month: 'Март',
    desktop: 237,
    mobile: 120
  },
  {
    month: 'Апрель',
    desktop: 73,
    mobile: 190
  },
  {
    month: 'Май',
    desktop: 209,
    mobile: 130
  },
  {
    month: 'Июнь',
    desktop: 214,
    mobile: 140
  }
])
</script>

<template>
  <B24Chart
    :colors="{
      '0': 'air-primary-success',
      '1': '#8b5cf6'
    }"
    :data="data"
    index="month"
    :categories="{
      '0': {
        key: 'desktop',
        label: 'Компьютер'
      },
      '1': {
        key: 'mobile',
        label: 'Телефон'
      }
    }"
  />
</template>
По умолчанию цвета берутся из CSS-переменных --b24ui-chart-*, которые можно переопределить в файле main.css.

Точки

Пропс points рисует точку у каждого значения.

Пропс работает только при type равном line.
<script setup lang="ts">
import type { ChartData } from '@bxshefby/b24ui-nuxt'

const data = ref<ChartData[]>([
  {
    month: 'Январь',
    desktop: 186,
    mobile: 80
  },
  {
    month: 'Февраль',
    desktop: 305,
    mobile: 200
  },
  {
    month: 'Март',
    desktop: 237,
    mobile: 120
  },
  {
    month: 'Апрель',
    desktop: 73,
    mobile: 190
  },
  {
    month: 'Май',
    desktop: 209,
    mobile: 130
  },
  {
    month: 'Июнь',
    desktop: 214,
    mobile: 140
  }
])
</script>

<template>
  <B24Chart
    points
    :data="data"
    index="month"
    :categories="{
      '0': {
        key: 'desktop',
        label: 'Компьютер'
      },
      '1': {
        key: 'mobile',
        label: 'Телефон'
      }
    }"
  />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ChartData } from '@bxshefby/b24ui-nuxt'

const data = ref<ChartData[]>([
  {
    month: 'Январь',
    desktop: 186,
    mobile: 80
  },
  {
    month: 'Февраль',
    desktop: 305,
    mobile: 200
  },
  {
    month: 'Март',
    desktop: 237,
    mobile: 120
  },
  {
    month: 'Апрель',
    desktop: 73,
    mobile: 190
  },
  {
    month: 'Май',
    desktop: 209,
    mobile: 130
  },
  {
    month: 'Июнь',
    desktop: 214,
    mobile: 140
  }
])
</script>

<template>
  <B24Chart
    points
    :data="data"
    index="month"
    :categories="{
      '0': {
        key: 'desktop',
        label: 'Компьютер'
      },
      '1': {
        key: 'mobile',
        label: 'Телефон'
      }
    }"
  />
</template>

Сетка

Пропс grid включает и выключает горизонтальные линии сетки. По умолчанию — true.

<script setup lang="ts">
import type { ChartData } from '@bxshefby/b24ui-nuxt'

const data = ref<ChartData[]>([
  {
    month: 'Январь',
    desktop: 186,
    mobile: 80
  },
  {
    month: 'Февраль',
    desktop: 305,
    mobile: 200
  },
  {
    month: 'Март',
    desktop: 237,
    mobile: 120
  },
  {
    month: 'Апрель',
    desktop: 73,
    mobile: 190
  },
  {
    month: 'Май',
    desktop: 209,
    mobile: 130
  },
  {
    month: 'Июнь',
    desktop: 214,
    mobile: 140
  }
])
</script>

<template>
  <B24Chart
    :grid="false"
    :data="data"
    index="month"
    :categories="{
      '0': {
        key: 'desktop',
        label: 'Компьютер'
      },
      '1': {
        key: 'mobile',
        label: 'Телефон'
      }
    }"
  />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ChartData } from '@bxshefby/b24ui-nuxt'

const data = ref<ChartData[]>([
  {
    month: 'Январь',
    desktop: 186,
    mobile: 80
  },
  {
    month: 'Февраль',
    desktop: 305,
    mobile: 200
  },
  {
    month: 'Март',
    desktop: 237,
    mobile: 120
  },
  {
    month: 'Апрель',
    desktop: 73,
    mobile: 190
  },
  {
    month: 'Май',
    desktop: 209,
    mobile: 130
  },
  {
    month: 'Июнь',
    desktop: 214,
    mobile: 140
  }
])
</script>

<template>
  <B24Chart
    :grid="false"
    :data="data"
    index="month"
    :categories="{
      '0': {
        key: 'desktop',
        label: 'Компьютер'
      },
      '1': {
        key: 'mobile',
        label: 'Телефон'
      }
    }"
  />
</template>

Оси

Пропсы x-axis и y-axis включают и выключают каждую ось. По умолчанию оба — true.

<script setup lang="ts">
import type { ChartData } from '@bxshefby/b24ui-nuxt'

const data = ref<ChartData[]>([
  {
    month: 'Январь',
    desktop: 186,
    mobile: 80
  },
  {
    month: 'Февраль',
    desktop: 305,
    mobile: 200
  },
  {
    month: 'Март',
    desktop: 237,
    mobile: 120
  },
  {
    month: 'Апрель',
    desktop: 73,
    mobile: 190
  },
  {
    month: 'Май',
    desktop: 209,
    mobile: 130
  },
  {
    month: 'Июнь',
    desktop: 214,
    mobile: 140
  }
])
</script>

<template>
  <B24Chart
    :x-axis="false"
    :y-axis="false"
    :data="data"
    index="month"
    :categories="{
      '0': {
        key: 'desktop',
        label: 'Компьютер'
      },
      '1': {
        key: 'mobile',
        label: 'Телефон'
      }
    }"
  />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ChartData } from '@bxshefby/b24ui-nuxt'

const data = ref<ChartData[]>([
  {
    month: 'Январь',
    desktop: 186,
    mobile: 80
  },
  {
    month: 'Февраль',
    desktop: 305,
    mobile: 200
  },
  {
    month: 'Март',
    desktop: 237,
    mobile: 120
  },
  {
    month: 'Апрель',
    desktop: 73,
    mobile: 190
  },
  {
    month: 'Май',
    desktop: 209,
    mobile: 130
  },
  {
    month: 'Июнь',
    desktop: 214,
    mobile: 140
  }
])
</script>

<template>
  <B24Chart
    :x-axis="false"
    :y-axis="false"
    :data="data"
    index="month"
    :categories="{
      '0': {
        key: 'desktop',
        label: 'Компьютер'
      },
      '1': {
        key: 'mobile',
        label: 'Телефон'
      }
    }"
  />
</template>

Подписи осей

Пропсы x-label и y-label показывают заголовок вдоль каждой оси.

<script setup lang="ts">
import type { ChartData } from '@bxshefby/b24ui-nuxt'

const data = ref<ChartData[]>([
  {
    month: 'Январь',
    desktop: 186,
    mobile: 80
  },
  {
    month: 'Февраль',
    desktop: 305,
    mobile: 200
  },
  {
    month: 'Март',
    desktop: 237,
    mobile: 120
  },
  {
    month: 'Апрель',
    desktop: 73,
    mobile: 190
  },
  {
    month: 'Май',
    desktop: 209,
    mobile: 130
  },
  {
    month: 'Июнь',
    desktop: 214,
    mobile: 140
  }
])
</script>

<template>
  <B24Chart
    x-label="Месяц"
    y-label="Посетители"
    :data="data"
    index="month"
    :categories="{
      '0': {
        key: 'desktop',
        label: 'Компьютер'
      },
      '1': {
        key: 'mobile',
        label: 'Телефон'
      }
    }"
  />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ChartData } from '@bxshefby/b24ui-nuxt'

const data = ref<ChartData[]>([
  {
    month: 'Январь',
    desktop: 186,
    mobile: 80
  },
  {
    month: 'Февраль',
    desktop: 305,
    mobile: 200
  },
  {
    month: 'Март',
    desktop: 237,
    mobile: 120
  },
  {
    month: 'Апрель',
    desktop: 73,
    mobile: 190
  },
  {
    month: 'Май',
    desktop: 209,
    mobile: 130
  },
  {
    month: 'Июнь',
    desktop: 214,
    mobile: 140
  }
])
</script>

<template>
  <B24Chart
    x-label="Месяц"
    y-label="Посетители"
    :data="data"
    index="month"
    :categories="{
      '0': {
        key: 'desktop',
        label: 'Компьютер'
      },
      '1': {
        key: 'mobile',
        label: 'Телефон'
      }
    }"
  />
</template>

Легенда

Пропс legend включает и выключает легенду.

По умолчанию легенда видна, если категорий больше одной (или секторов больше одного при donut).
<script setup lang="ts">
import type { ChartData } from '@bxshefby/b24ui-nuxt'

const data = ref<ChartData[]>([
  {
    month: 'Январь',
    desktop: 186,
    mobile: 80
  },
  {
    month: 'Февраль',
    desktop: 305,
    mobile: 200
  },
  {
    month: 'Март',
    desktop: 237,
    mobile: 120
  },
  {
    month: 'Апрель',
    desktop: 73,
    mobile: 190
  },
  {
    month: 'Май',
    desktop: 209,
    mobile: 130
  },
  {
    month: 'Июнь',
    desktop: 214,
    mobile: 140
  }
])
</script>

<template>
  <B24Chart
    :data="data"
    index="month"
    :categories="{
      '0': {
        key: 'desktop',
        label: 'Компьютер'
      },
      '1': {
        key: 'mobile',
        label: 'Телефон'
      }
    }"
  />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ChartData } from '@bxshefby/b24ui-nuxt'

const data = ref<ChartData[]>([
  {
    month: 'Январь',
    desktop: 186,
    mobile: 80
  },
  {
    month: 'Февраль',
    desktop: 305,
    mobile: 200
  },
  {
    month: 'Март',
    desktop: 237,
    mobile: 120
  },
  {
    month: 'Апрель',
    desktop: 73,
    mobile: 190
  },
  {
    month: 'Май',
    desktop: 209,
    mobile: 130
  },
  {
    month: 'Июнь',
    desktop: 214,
    mobile: 140
  }
])
</script>

<template>
  <B24Chart
    :data="data"
    index="month"
    :categories="{
      '0': {
        key: 'desktop',
        label: 'Компьютер'
      },
      '1': {
        key: 'mobile',
        label: 'Телефон'
      }
    }"
  />
</template>

Подсказка

Пропс tooltip настраивает или скрывает подсказку, которая показывается у выбранных значений. По умолчанию — true.

Содержимое подсказки заменяет слот #tooltip.
<script setup lang="ts">
import type { ChartData } from '@bxshefby/b24ui-nuxt'

const data = ref<ChartData[]>([
  {
    month: 'Январь',
    desktop: 186,
    mobile: 80
  },
  {
    month: 'Февраль',
    desktop: 305,
    mobile: 200
  },
  {
    month: 'Март',
    desktop: 237,
    mobile: 120
  },
  {
    month: 'Апрель',
    desktop: 73,
    mobile: 190
  },
  {
    month: 'Май',
    desktop: 209,
    mobile: 130
  },
  {
    month: 'Июнь',
    desktop: 214,
    mobile: 140
  }
])
</script>

<template>
  <B24Chart
    :tooltip="false"
    :data="data"
    index="month"
    :categories="{
      '0': {
        key: 'desktop',
        label: 'Компьютер'
      },
      '1': {
        key: 'mobile',
        label: 'Телефон'
      }
    }"
  />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ChartData } from '@bxshefby/b24ui-nuxt'

const data = ref<ChartData[]>([
  {
    month: 'Январь',
    desktop: 186,
    mobile: 80
  },
  {
    month: 'Февраль',
    desktop: 305,
    mobile: 200
  },
  {
    month: 'Март',
    desktop: 237,
    mobile: 120
  },
  {
    month: 'Апрель',
    desktop: 73,
    mobile: 190
  },
  {
    month: 'Май',
    desktop: 209,
    mobile: 130
  },
  {
    month: 'Июнь',
    desktop: 214,
    mobile: 140
  }
])
</script>

<template>
  <B24Chart
    :tooltip="false"
    :data="data"
    index="month"
    :categories="{
      '0': {
        key: 'desktop',
        label: 'Компьютер'
      },
      '1': {
        key: 'mobile',
        label: 'Телефон'
      }
    }"
  />
</template>

Толщина

Пропс thickness задаёт толщину кольца в пикселях. По умолчанию — 24.

Пропс работает только при type равном donut.
<script setup lang="ts">
import type { ChartData } from '@bxshefby/b24ui-nuxt'

const data = ref<ChartData[]>([
  {
    month: 'Январь',
    desktop: 186,
    mobile: 80
  },
  {
    month: 'Февраль',
    desktop: 305,
    mobile: 200
  },
  {
    month: 'Март',
    desktop: 237,
    mobile: 120
  },
  {
    month: 'Апрель',
    desktop: 73,
    mobile: 190
  },
  {
    month: 'Май',
    desktop: 209,
    mobile: 130
  },
  {
    month: 'Июнь',
    desktop: 214,
    mobile: 140
  }
])
</script>

<template>
  <B24Chart
    type="donut"
    :thickness="12"
    :data="data"
    index="month"
    :categories="{
      '0': {
        key: 'desktop',
        label: 'Компьютер'
      },
      '1': {
        key: 'mobile',
        label: 'Телефон'
      }
    }"
  />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ChartData } from '@bxshefby/b24ui-nuxt'

const data = ref<ChartData[]>([
  {
    month: 'Январь',
    desktop: 186,
    mobile: 80
  },
  {
    month: 'Февраль',
    desktop: 305,
    mobile: 200
  },
  {
    month: 'Март',
    desktop: 237,
    mobile: 120
  },
  {
    month: 'Апрель',
    desktop: 73,
    mobile: 190
  },
  {
    month: 'Май',
    desktop: 209,
    mobile: 130
  },
  {
    month: 'Июнь',
    desktop: 214,
    mobile: 140
  }
])
</script>

<template>
  <B24Chart
    type="donut"
    :thickness="12"
    :data="data"
    index="month"
    :categories="{
      '0': {
        key: 'desktop',
        label: 'Компьютер'
      },
      '1': {
        key: 'mobile',
        label: 'Телефон'
      }
    }"
  />
</template>

Размер

Пропс height задаёт высоту графика в пикселях. По умолчанию — 300.

График занимает всю ширину контейнера. Пропс aspect-ratio выводит высоту из ширины, а пропс width задаёт фиксированную ширину.
При фиксированной высоте график плавно появляется после измерения контейнера. При aspect-ratio сразу показывается график, отрисованный на сервере.
<script setup lang="ts">
import type { ChartData } from '@bxshefby/b24ui-nuxt'

const data = ref<ChartData[]>([
  {
    month: 'Январь',
    desktop: 186,
    mobile: 80
  },
  {
    month: 'Февраль',
    desktop: 305,
    mobile: 200
  },
  {
    month: 'Март',
    desktop: 237,
    mobile: 120
  },
  {
    month: 'Апрель',
    desktop: 73,
    mobile: 190
  },
  {
    month: 'Май',
    desktop: 209,
    mobile: 130
  },
  {
    month: 'Июнь',
    desktop: 214,
    mobile: 140
  }
])
</script>

<template>
  <B24Chart
    :height="200"
    :data="data"
    index="month"
    :categories="{
      '0': {
        key: 'desktop',
        label: 'Компьютер'
      },
      '1': {
        key: 'mobile',
        label: 'Телефон'
      }
    }"
  />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ChartData } from '@bxshefby/b24ui-nuxt'

const data = ref<ChartData[]>([
  {
    month: 'Январь',
    desktop: 186,
    mobile: 80
  },
  {
    month: 'Февраль',
    desktop: 305,
    mobile: 200
  },
  {
    month: 'Март',
    desktop: 237,
    mobile: 120
  },
  {
    month: 'Апрель',
    desktop: 73,
    mobile: 190
  },
  {
    month: 'Май',
    desktop: 209,
    mobile: 130
  },
  {
    month: 'Июнь',
    desktop: 214,
    mobile: 140
  }
])
</script>

<template>
  <B24Chart
    :height="200"
    :data="data"
    index="month"
    :categories="{
      '0': {
        key: 'desktop',
        label: 'Компьютер'
      },
      '1': {
        key: 'mobile',
        label: 'Телефон'
      }
    }"
  />
</template>

Примеры

С форматированием значений

Пропс format форматирует значения на оси Y и в подсказке.

<script setup lang="ts">
const data = [
  { month: 'Январь', revenue: 18600 },
  { month: 'Февраль', revenue: 30500 },
  { month: 'Март', revenue: 23700 },
  { month: 'Апрель', revenue: 27300 },
  { month: 'Май', revenue: 20900 },
  { month: 'Июнь', revenue: 31400 }
]

const currency = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', notation: 'compact' })
</script>

<template>
  <B24Chart
    type="bar"
    :data="data"
    index="month"
    :categories="[{ key: 'revenue', label: 'Выручка' }]"
    :format="value => currency.format(value)"
    aria-label="Выручка по месяцам"
  />
</template>

С готовым описанием графика

Если остальных пропсов не хватает, пропс definition принимает готовое описание графика TanStack Charts.

Описание из примеров TanStack Charts отрисовывается как есть, с подсказкой и адаптивным размером.
<script setup lang="ts">
import { defineChart, lineY, ruleY } from '@tanstack/charts'
import { scaleLinear } from '@tanstack/charts/scales/linear'
import { scalePoint } from '@tanstack/charts/scales/point'

const data = [
  { month: 'Январь', revenue: 186 },
  { month: 'Февраль', revenue: 305 },
  { month: 'Март', revenue: 237 },
  { month: 'Апрель', revenue: 273 },
  { month: 'Май', revenue: 209 },
  { month: 'Июнь', revenue: 314 }
]

const definition = defineChart({
  marks: [
    ruleY([250], { stroke: 'var(--ui-color-accent-main-alert)', strokeDasharray: '4 4' }),
    lineY(data, { x: 'month', y: 'revenue', stroke: 'var(--ui-color-accent-main-primary)', strokeWidth: 2, points: true })
  ],
  scales: {
    x: { scale: () => scalePoint<string>().padding(0.2) },
    y: { scale: scaleLinear, nice: true, grid: true, axis: { label: 'Выручка' } }
  }
})
</script>

<template>
  <B24Chart :definition="definition" aria-label="Выручка и целевой показатель" />
</template>
Ряды без явного цвета берут цвет из CSS-переменных --b24ui-chart-*. Цвета, заданные в описании, сохраняются.
Если в описании есть собственный tooltip, он показывается вместо пропса tooltip.
Примерам, которые импортируют пакеты d3-*, нужны эти пакеты в вашем проекте.

Со слотом center

Слот #center показывает содержимое в центре кольцевого графика donut.

835Посетителей
<script setup lang="ts">
const data = [
  { browser: 'Chrome', visitors: 275 },
  { browser: 'Safari', visitors: 200 },
  { browser: 'Firefox', visitors: 187 },
  { browser: 'Edge', visitors: 173 }
]
</script>

<template>
  <B24Chart
    type="donut"
    :data="data"
    index="browser"
    :categories="['visitors']"
    legend
    aria-label="Посетители по браузерам"
  >
    <template #center>
      <span class="text-(length:--ui-font-size-4xl) font-(--ui-font-weight-semi-bold) text-legend">835</span>
      <span class="text-(length:--ui-font-size-xs) text-description">Посетителей</span>
    </template>
  </B24Chart>
</template>

Со слотом tooltip

Слот #tooltip заменяет содержимое подсказки. Он получает title, rows с их label, value и color, а также выбранные points.

<script setup lang="ts">
const data = [
  { month: 'Январь', revenue: 18600, expenses: 12400 },
  { month: 'Февраль', revenue: 30500, expenses: 21800 },
  { month: 'Март', revenue: 23700, expenses: 25100 },
  { month: 'Апрель', revenue: 27300, expenses: 19600 },
  { month: 'Май', revenue: 20900, expenses: 22300 },
  { month: 'Июнь', revenue: 31400, expenses: 20700 }
]

const currency = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0 })

function profit(month?: string) {
  const row = data.find(row => row.month === month)

  return row ? row.revenue - row.expenses : 0
}
</script>

<template>
  <B24Chart
    type="bar"
    :data="data"
    index="month"
    :categories="[{ key: 'revenue', label: 'Выручка', color: 'air-primary-success' }, { key: 'expenses', label: 'Расходы', color: 'air-primary-alert' }]"
    :format="value => currency.format(value)"
    aria-label="Выручка и расходы"
  >
    <template #tooltip="{ title, rows }">
      <p class="font-(--ui-font-weight-medium)">
        {{ title }}
      </p>

      <div v-for="row in rows" :key="row.label" class="flex items-center gap-2">
        <span class="size-2 rounded-full" :style="{ backgroundColor: row.color }" />
        <span class="opacity-75">{{ row.label }}</span>
        <span class="ms-auto ps-4 tabular-nums">{{ row.value }}</span>
      </div>

      <B24Separator class="my-1" />

      <div class="flex items-center gap-2">
        <span class="opacity-75">Прибыль</span>
        <B24Badge
          :color="profit(title) >= 0 ? 'air-primary-success' : 'air-primary-alert'"
          :label="currency.format(profit(title))"
          size="sm"
          class="ms-auto tabular-nums"
        />
      </div>
    </template>
  </B24Chart>
</template>

API

Пропсы

Prop Default Type
type'line' "line" | "area" | "bar" | "donut"

The type of chart to render.

data T[]

The rows to plot, one object per x value, or per slice with donut.

index keyof T & string

The key of each row used for the x axis, or for the slice labels with donut.

categories ((keyof T & string) | ChartCategory<T>)[]

The keys of each row plotted as series on the y axis, or objects to set their label and color. With donut, the first one sets the slice values.

colors string[]

The series colors, in categories order, or in data order with donut. Accepts air-* colors or any CSS color. When there are more series than colors, the colors repeat as lighter tints. Defaults to the --b24ui-chart-1 to --b24ui-chart-6 CSS variables.

stackedboolean

Stack the series on top of each other. Only applies to area and bar.

curve'linear' "linear" | "monotone" | "step"

The shape of the line between values. Only applies to line and area.

pointsfalseboolean

Draw a dot at each value. Only applies to line.

gridtrueboolean

Display horizontal grid lines.

xAxistrueboolean

Display the x axis.

yAxistrueboolean

Display the y axis.

xLabel string

A title displayed along the x axis.

yLabel string

A title displayed along the y axis.

legendboolean

Display a legend. Defaults to true when there is more than one category, or more than one slice with donut.

format (value: number): string

Format the values on the y axis and in the tooltip.

tooltiptrueboolean | TooltipProps

Display a tooltip on the focused values. { content: { side: 'top', sideOffset: 12 } }

transitiontrueboolean

Animate the chart when its data changes. Does not apply to definition, set svgAnimation on it instead.

thickness24 number

The width of the ring in pixels. Only applies to donut.

height number

The height of the chart in pixels. Defaults to 300 unless aspectRatio is set.

aspectRatio number

The width over height ratio of the chart, used when height is not set.

width number

A fixed width in pixels. The chart follows its container by default.

initialWidth640 number

The width used on the server and before the container is measured.

ariaLabel string

The accessible name of the chart. Defaults to the categories, set it when using definition.

ariaDescription string

A longer description of the chart for assistive technologies.

definition Omit<StaticChartDefinition<any, any, any, "dom">, "tooltip"> & { tooltip?: false | ChartTooltipInput<any, any, any, "dom"> | undefined; } | Omit<ResponsiveChartDefinition<any, any, any, "dom">, "tooltip"> & { tooltip?: false | ChartTooltipInput<any, any, any, "dom"> | undefined; }

A complete TanStack Charts definition. Takes precedence over data, index and categories. When it has its own tooltip, that one is used in place of the tooltip prop.

b24ui { root?: SlotClass; base?: SlotClass; tooltip?: SlotClass; tooltipTitle?: SlotClass; tooltipRow?: SlotClass; tooltipIndicator?: SlotClass; tooltipLabel?: SlotClass; tooltipValue?: SlotClass; center?: SlotClass; }

Слоты

Slot Type
tooltip{ title?: string | undefined; rows: ChartTooltipRow[]; points: ChartPoint<any, ChartValue, ChartValue>[]; primaryPoint: ChartPoint<any, ChartValue, ChartValue>; }
center{}

События

Event Type
select[point: ChartPoint<any, ChartValue, ChartValue> | null]
focusChange[point: ChartPoint<any, ChartValue, ChartValue> | null]
focusGroupChange[points: readonly ChartPoint<any, ChartValue, ChartValue>[]]

Тема

https://github.com/bx-shef/b24-ui/tree/main/src/theme/chart.ts
export default {
  slots: {
    root: 'relative w-full text-(length:--ui-font-size-xs) text-muted **:data-reka-popper-content-wrapper:pointer-events-none [--ts-chart-tooltip-background:var(--ui-color-bg-content-inapp)] [--ts-chart-tooltip-color:var(--ui-color-base-8)] [--ts-chart-tooltip-border:1px_solid_var(--ui-color-bg-content-inapp)] [--ts-chart-tooltip-border-radius:var(--ui-border-radius-md)] [--ts-chart-tooltip-shadow:var(--popup-window-box-shadow)] [--ts-chart-tooltip-padding:6px_10px] [--ts-chart-tooltip-font:inherit]',
    base: 'opacity-0 in-data-measured:opacity-100 transition-opacity duration-150 [&_.ts-chart]:rounded-(--ui-border-radius-xs) [&_.ts-chart]:outline-0 [&_.ts-chart]:outline-(--ui-color-design-outline-focused-stroke) [&_.ts-chart:focus-visible]:outline-2',
    tooltip: 'flex-col items-stretch justify-start gap-[4px] h-auto min-h-0 min-w-32 py-[6px] px-[10px] pointer-events-none',
    tooltipTitle: 'font-(--ui-font-weight-medium)',
    tooltipRow: 'flex items-center gap-[6px] opacity-75 data-[active=true]:opacity-100',
    tooltipIndicator: 'size-2 shrink-0 rounded-full',
    tooltipLabel: 'truncate',
    tooltipValue: 'ms-auto ps-4 font-(--ui-font-weight-medium) tabular-nums',
    center: 'absolute inset-0 flex flex-col items-center justify-center pointer-events-none opacity-0 in-data-measured:opacity-100 transition-opacity duration-150'
  },
  variants: {
    type: {
      line: '',
      area: '',
      bar: '',
      donut: ''
    }
  }
}

История изменений