Framework:Javascript Data GridAngular Data GridReact Data GridVue Data Grid

Angular Data Grid: Sparklines - Bar Customisation

This section shows how Bar Sparklines can be customised by overriding the default bar options.

The following Bar Sparkline Options can be used to customise Bar Sparklines:

Also see Additional Customisations for more advanced customisations that are common across all sparklines.

The snippet below shows option overrides for the Bar Sparkline:

sparklineOptions: {
    type: 'bar',
    // Optional customisation properties
    fill: '#5470c6',
    stroke: '#91cc75',
    highlightStyle: {
        fill: '#fac858'
    },
    valueAxisDomain: [0, 1]
},

In the snippet above, the valueAxisDomain property is optional.

valueAxisDomain is used to specify the interval within which the data values lie. These boundaries are used to create a scale which will be used to map data values from 0px to the size of the sparkline.

If the valueAxisDomain property is ommitted, the domain will be set to the minmum and maximum values in the provided data, hence it could be different for different sparklines in the grid depending on the provided data.

The following example demonstrates the results of the Bar Sparkline options above:

  • Note that valueAxisDomain has been set to [0, 1], indicating that the supplied data across all sparklines will contain values from 0 to 1. This allows easy comparisons across the different sparklines in the grid column as all sparklines will have the same domain.

Bar Fill Options

To apply a custom color to the bars, set the fill property in sparklineOptions as shown:

sparklineOptions: {
    type: 'bar',
    fill: '#91cc75', // sets the bar fill
}
Bar fill default
Default
Bar fill customisation
Custom fill

It is possible to set the fill for the highlighted state of the bar by adding fill in highlightStyle options as follows:

sparklineOptions: {
    type: 'bar',
    highlightStyle: {
        fill: 'orange', // sets the highlighted bar fill
    }
}
Highlighted Bar fill default
Default highlighted fill
Highlighted Bar fill customisation
Custom highlighted fill

The given fill string can be in one of the following formats:

  • #rgb - Short Hex Code
  • #rrggbb - Hex Code
  • rgb(r, g, b) - RGB
  • rgba(r, g, b, a) - RGB with an alpha channel
  • CSS color keyword - such as aqua, orange, etc.

Bar Stroke Options

By default, the strokeWidth of each bar is 0, so no outline is visible around the bars.

To add a stroke, modify the strokeWidth and stroke properties as shown below.

sparklineOptions: {
    type: 'bar',
    stroke: '#ec7c7d', // sets the bar stroke
    strokeWidth: 2, // sets the bar stroke width
    highlightStyle: {
        stroke: '#b5ec7c', // sets the highlighted bar stroke
        strokeWidth: 2, // sets the highlighted bar stroke width
    }
}
  • In the snippet above, we have configured the bar stroke to be 2px in the un-highlighted state, and 2px in the highlighted state.
  • Note that the stroke property is also different depending on the highlighted state of the bar.

Here is the result of the configuration shown in the above snippet.

Stroke default
Default
Stroke customisation
Custom stroke
Stroke customisation for highlighted state
Custom highlighted stroke

If strokeWidth is set to a value greater than 1, it is recommended to set the axis line stroke to the same value in order to preserve the alignment of the bars with the x-axis line.

Bar Padding Options

The spacing between bars is adjustable via the paddingInner property. This property takes values between 0 and 1.

It is a proportion of the “step”, which is the interval between the start of a band and the start of the next band.

Here's an example.

sparklineOptions: {
    type: 'bar',
    paddingInner: 0.5, // sets the padding between bars.
}
Bar padding default
Default
PaddingInner customisation
Custom paddingInner

The padding on the outer edges of the first and last bars can also be adjusted. As with paddingInner, this value can be between 0 and 1.

If the value of paddingOuter is increased, the x-axis line will stick out more at both ends of the sparkline.

Here's a snippet where the paddingOuter is set to 0.

sparklineOptions: {
    type: 'bar',
    paddingOuter: 0, // sets the padding on the outer edge of the first and last bars.
}

In this case there will be no gap on either end of the sparkline, i.e. between the axis line start and the first bar and the axis line end and the last bar. This is demonstrated below in the middle sparkline.

bar padding default
Default
PaddingOuter customisation
No paddingOuter
PaddingOuter customisation
Increased paddingOuter

Bar Label Options

To enable bar labels, set the enabled property in label options as shown:

sparklineOptions: {
    type: 'bar',
    label: {
        enabled: true // show bar labels
    }
}
Bar default
Default
Bar labels enabled
Label enabled

It is possible to change the text value displayed as the label of individual bars by adding a formatter callback function to label options as follows:

sparklineOptions: {
    type: 'bar',
    label: {
        enabled: true,
        formatter: labelFormatter
    }
}

function labelFormatter({ value }) {
    return `${value}%`
}
Bar default
Default
Bar label text customisation
Custom label text

To customise the label text style, set the style attributes in label options as follows:

sparklineOptions: {
    type: 'bar',
    label: {
        enabled: true,
        fontWeight: 'bold',
        fontStyle: 'italic',
        fontSize: 9,
        fontFamily: 'Arial, Helvetica, sans-serif',
        color: 'black',
    }
}
Bar default
Default
Bar label text style customisation
Custom label text styles

The position of the labels can be specified by setting the placement property in label options. By default, the labels are positioned at the end of the bars on the inside, i.e. placement is set to insideEnd. The snippet below shows how the positioning of the label can be modified:

sparklineOptions: {
    type: 'bar',
    label: {
        enabled: true,
        placement: 'center', // positions the labels in the center of the bars
    }
}

Label placement options include insideBase, center, insideEnd and outsideEnd. These are shown in the screenshots below.

Bar label insideBase placement
insideBase
Bar label center placement
center
Bar label insideEnd placement
insideEnd
Bar label placement default
outsideEnd

When configuring labels with placement:outsideEnd, it is recommended to add some padding to the sparkline using the padding options in order to prevent the labels from being clipped.

Axis Line Options

By default, an x-axis line is displayed which can be modified using the axis options.

Here is a snippet to demonstrate axis formatting.

sparklineOptions: {
    type: 'bar',
    axis: {
        stroke: '#7cecb3', // sets the x-axis line stroke
        strokeWidth: 3, // sets the x-axis line strokeWidth
    },
}
Axis line default
Default axis line
Axis line customisation
Custom axis line

It's possible to remove the x-axis line entirely by setting the axis strokeWidth to 0.

Sparkline Padding Options

To add extra space around the sparklines, custom padding options can be applied in the following way.

sparklineOptions: {
    type: 'bar',
    // sets the padding around the sparklines
    padding: {
        top: 10,
        right: 5,
        bottom: 10,
        left: 5
    },
}
  • The top, right, bottom and left properties are all optional and can be modified independently.
Padding customisation
Default padding
Padding customisation
Custom padding

Additional Customisations

More advanced customisations are discussed separately in the following sections:

  • Axis - configure the x-axis type via axis options.
  • Tooltips - configure tooltips using tooltip options.
  • Points of Interest - configure individual points of interest using a formatter.

Interfaces

BarSparklineOptions

Properties available on the BarSparklineOptions interface.

type
'bar'
The type of sparklines to create, in this case it would be 'bar'.
fill
string
The CSS colour value for the fill of the bars.
Default: 'rgb(124, 181, 236)'
stroke
string
The CSS colour value for the outline of the bars. Default 'silver'
strokeWidth
number
The thickness in pixels for the stroke of the bars.
Default: 0
paddingInner
number
The size of the gap between the bars as a proportion, between 0 and 1. This value is a fraction of the “step”, which is the interval between the start of a band and the start of the next band.
Default: 0.1
paddingOuter
number
The padding on the outside i.e. left and right of the first and last bars, to leave some room for the axis. In association with paddingInner, this value can be between 0 and 1.
Default: 0.2
valueAxisDomain
[number, number]
User override for the automatically determined domain (based on data min and max values). Only applied to number axes. Used to interpolate the numeric pixel values corresponding to each data value.
formatter
SparklineBarFormatter
A callback function to return format styles of type BarFormat, based on the data represented by individual bars.
formatter: SparklineBarFormatter;

type SparklineBarFormatter = 
      (params: BarFormatterParams) => BarFormat


interface BarFormatterParams {
  // The raw data associated with the specific bar. 
  datum: any;
  // The X value of the data point. 
  xValue: any;
  // The Y value of the data point. 
  yValue: any;
  // The width of the bar in pixels. 
  width: number;
  // The height of the bar in pixels. 
  height: number;
  // Whether or not the bar is a minimum point. 
  min?: boolean;
  // Whether or not the bar is a maximum point. 
  max?: boolean;
  // Whether or not the bar represents the first data point. 
  first?: boolean;
  // Whether or not the bar represents the last data point. 
  last?: boolean;
  // The CSS colour value for the fill of the individual bar. 
  fill?: string;
  // The CSS colour value for the outline of the individual bar. 
  stroke?: string;
  // The thickness in pixels for the stroke of the individual bar. 
  strokeWidth: number;
  // Whether or not the bar is highlighted. 
  highlighted: boolean;
}

interface BarFormat {
  // The CSS colour value for the fill of the individual bar. 
  fill?: string;
  // The CSS colour value for the outline of the individual bar. 
  stroke?: string;
  // The thickness in pixels for the stroke of the individual bar. 
  strokeWidth?: number;
}
label
SparklineLabelOptions
Configuration for the labels. See SparklineLabelOptions.
xKey
string
The key to use to retrieve X values from the data. This will only be used if the data array contains objects with key-value pairs.
Default: 'x'
yKey
string
The key to use to retrieve Y values from the data. This will only be used if the data array contains objects with key-value pairs.
Default: 'y'
padding
PaddingOptions
Configuration for the padding in pixels shown around the sparklines.
padding: PaddingOptions;

interface PaddingOptions {
  // The number of pixels of padding at the top of the sparkline area.
  // Default: `3` 
  top?: number;
  // The number of pixels of padding at the right of the sparkline area.
  // Default: `3` 
  right?: number;
  // The number of pixels of padding at the bottom of the sparkline area.
  // Default: `3` 
  bottom?: number;
  // The number of pixels of padding at the left of the sparkline area.
  // Default" `3` 
  left?: number;
}
axis
SparklineAxisOptions
The options for the horizontal axis line in the sparklines. See SparklineAxisOptions.
highlightStyle
HighlightStyleOptions
The configuration for the highlighting used when the items are hovered over.
highlightStyle: HighlightStyleOptions;

interface HighlightStyleOptions {
  // The width in pixels of the markers when hovered over. This is only for the Line and Area sparklines as Column and Bar sparklines do not have markers.
  // Default: `6` 
  size?: number;
  // The fill colour of the markers, columns or bars when hovered over. Use `undefined` for no highlight fill.
  // Default: `'yellow'` 
  fill?: string;
  // The CSS colour value for the outline of the markers, columns or bars when hovered over. Use `undefined` for no highlight stroke.
  // Default: `'silver'` 
  stroke?: string;
  // The thickness in pixels for the stroke of the markers, columns or bars when hovered over.
  // Default: `1` 
  strokeWidth?: number;
}
tooltip
SparklineTooltipOptions
Configuration for the tooltips.
tooltip: SparklineTooltipOptions;

interface SparklineTooltipOptions {
  // Set to false to disable tooltips. 
  enabled?: boolean;
  // The element to place the tooltip into. This can be used to confine the tooltip to a specific area which may be outside of the sparkline grid cell. 
  container?: HTMLElement;
  // A callback function used to create the content for the tooltips. This function should return an object or a HTML string used to render the tooltip. 
  renderer?: SparklineTooltipRenderer;
}

type SparklineTooltipRenderer = 
      (params: TooltipRendererParams) => TooltipRendererResult


interface TooltipRendererParams {
  // The grid context, includes row data, giving access to data from other columns in the same row. 
  context?: any;
  // The raw datum associated with the point. 
  datum: any;
  // The X value of the data point. 
  xValue: any;
  // The Y value of the data point. 
  yValue: any;
}

interface TooltipRendererResult {
  // Set to false to disable individual tooltip. 
  enabled?: boolean;
  // The content to display in each tooltip. 
  content?: string;
  // The title of the tooltip. 
  title?: string;
  // The CSS color for the title text. 
  color?: string;
  // The CSS color for the background of the tooltip title. 
  backgroundColor?: string;
  // The opacity of the background for the tooltip title. 
  opacity?: number;
}

SparklineAxisOptions

type
string
The type of the x-axis.
Default: 'category'
Options: 'category', 'number', 'time'
stroke
string
The CSS colour value for the outline of the horizontal axis line.
Default: 'rgb(204, 214, 235)'
strokeWidth
number
The thickness in pixels for the stroke of the horizontal axis line.
Default: 1

SparklineLabelOptions

enabled
boolean
Set to true to enable labels.
Default: false
fontSize
number
Set size of the font.
Default: 8
fontFamily
string
Specify the font for the label text.
Default: 'Verdana, sans-serif'
fontStyle
FontStyle
Specify the font style for the label text.
Options: 'normal', 'italic', 'oblique'
fontWeight
FontWeight
Set how thick or thin characters in label text should be displayed.
Options: 'normal', 'bold', 'bolder', 'lighter', '100', '200', '300', '400', '500', '600', '700', '800', '900'
color
string
Set the color of the label text. The color can be specified by a color name, a HEX or an RGB value.
Default: 'rgba(70, 70, 70, 1)'
formatter
Function
A callback function to return the text to be displayed as the label, based on the value represented by the column or bar. By default the values are simply stringified.
formatter = (
    params: FormatterParams
) => string;

interface FormatterParams {
  value: number;
}
placement
string
Where to render labels relative to the segments.
Default: 'insideEnd'
Options: 'insideBase', 'insideEnd', 'center', 'outsideEnd'

Next Up

Continue to the next section to learn about: Column Sparkline Customisation.