Toolforge Docs
DocsBlock OutputsbarChart

barChart

Create bar charts to compare categorical data with horizontal or vertical bars.

Usage

Bar chart

const chart = block.barChart({
  title: 'Sales by Region',
  data: [
    { region: 'North', sales: 4000 },
    { region: 'South', sales: 3000 },
    { region: 'East', sales: 2000 },
    { region: 'West', sales: 2780 },
  ],
  index: 'region',
  categories: ['sales'],
})

Props

PropDescriptionTypeRequiredDefault
titleChart title displayed above the chartstringNoundefined
descriptionDescription text below the titlestringNoundefined
dataArray of data objects to visualizeT[]Yes
indexKey of the data object for the x-axiskeyof TYes
categoriesKeys of the data object for the bar values(keyof T)[]Yes
colorsColors for each category seriesChartColor[]No['blue']
xAxisLabelLabel for the x-axisstringNoundefined
yAxisLabelLabel for the y-axisstringNoundefined
layoutBar orientation'horizontal' | 'vertical'No'horizontal'
stackTypeHow bars are stacked'default' | 'stacked' | 'percent'No'default'
intervalTypeX-axis tick placement logic'equidistantPreserveStart' | 'preserveStartEnd'No'equidistantPreserveStart'
startEndOnlyShow only start and end labels on x-axisbooleanNofalse
showXAxisWhether to display the x-axisbooleanNotrue
showYAxisWhether to display the y-axisbooleanNotrue
showGridLinesWhether to display grid linesbooleanNotrue
showTooltipWhether to display tooltips on hoverbooleanNotrue
showLegendWhether to display the legendbooleanNotrue
valueFormatFormat configuration for valuesValueFormatNoundefined

Returns

Returns a BarChartOutput object that can be returned from a handler or passed to io.message().

Examples

Grouped bar chart

Grouped bar chart

return block.barChart({
  title: 'Quarterly Performance',
  data: quarterlyData,
  index: 'quarter',
  categories: ['revenue', 'expenses', 'profit'],
  colors: ['blue', 'red', 'emerald'],
})

Horizontal bar chart

Horizontal bar chart

return block.barChart({
  title: 'Top Products',
  data: productData,
  index: 'product',
  categories: ['sales'],
  layout: 'vertical', // Flips to horizontal bars
})

Stacked bar chart

Stacked bar chart

return block.barChart({
  title: 'Revenue by Source',
  data: revenueData,
  index: 'month',
  categories: ['online', 'retail', 'wholesale'],
  colors: ['blue', 'emerald', 'amber'],
  stackType: 'stacked',
})

Percentage stacked chart

Percentage stacked chart

return block.barChart({
  title: 'Market Share by Quarter',
  data: marketData,
  index: 'quarter',
  categories: ['companyA', 'companyB', 'companyC'],
  stackType: 'percent',
  valueFormat: { type: 'percentage' },
})

With currency formatting

With currency formatting

return block.barChart({
  title: 'Budget vs Actual',
  data: budgetData,
  index: 'department',
  categories: ['budget', 'actual'],
  colors: ['gray', 'blue'],
  valueFormat: {
    type: 'currency',
    currency: 'USD',
    notation: 'compact',
  },
})

On this page