Skip to main content
Version: 6.5

Bar chart

(quoted from Bar chart definition in Wikipedia)

A bar chart is a chart that presents categorical data with rectangular bars with heights or lengths proportional to the values that they represent. The bars can be plotted vertically or horizontally.

A bar chart shows comparisons among discrete categories. One axis of the chart shows the specific categories being compared, and the other axis represents a measured value. Some bar charts present bars clustered in groups of more than one, showing the values of more than one measured variable.

Programmatically, you could use a bar chart as following:

// creates the chart    
BarChart chart = new BarChart();
// adds to DOM
component.add(chart);
...
// example for Elemental2
// gets the chart instance as DOM element
Element element = chart.getChartElement().as();
// adds to DOM
DomGlobal.document.body.appendChild(element);

By UIBinder (ONLY for GWT), you could use a bar chart as following:

<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
xmlns:g="urn:import:com.google.gwt.user.client.ui"
xmlns:c="urn:import:org.pepstock.charba.client.gwt.widgets">

<g:HTMLPanel width="100%">
....
<c:BarChartWidget ui:field="chart"/>
...
</g:HTMLPanel>
</ui:UiBinder>

Dataset

The bar chart allows to define the data and a number of properties, used to display the data, by a bar dataset.

Every chart has got a method to create a typed dataset accordingly with the chart type. The dataset can be also created instantiating the constructor.

// creates the chart
BarChart chart = new BarChart();
// creates the dataset
BarDataset dataset = chart.newDataset();
// sets the option
dataset.setBackgroundColor(HtmlColor.RED);
...
// creates the dataset
BarDataset datasetNew = new BarDataset();
// sets the option
datasetNew.setBackgroundColor(HtmlColor.RED);
...
// sets the datasets to the chart
chart.getData().setDatasets(dataset, datasetNew);

The following are the attributes that you can set:

NameTypeScriptableDescription
autoInflateAmountboolean-If true, the amount of pixels to inflate the bar rectangles, when drawing, is automatically calculating.
basedouble - double[]YesBase value for the bar in data units along the value axis.
backgroundColorString[] - IsColor[] - Pattern[] - Gradient[]YesThe fill color/pattern of the bar.
barPercentagedouble-The percent (0-1) of the available width each bar should be within the category width. 1.0 will take the whole category width and put the bars right next to each other.
barThicknessint-The width of each bar in pixels.
borderColorString[] - IsColor[] - Gradient[]YesThe color of the bar border.
borderSkippedboolean - BorderSkippedYesWhich edge to skip drawing the border for.
borderRadiusint[] - BarBorderRadius[]YesThe bar border radius (in pixels).
borderWidthint[] - BarBorderWidth[]YesThe stroke width of the bar in pixels.
categoryPercentagedouble-The percent (0-1) of the available width each category should be within the sample width.
clipboolean - double - Clip-How to clip relative to chart area. Positive value allows overflow, negative value clips that many pixels inside chart area. When set to 0 means clip at chart area.
enableBorderRadiusbooleanYesIf true, it only shows the border radius of a bar when the bar is at the end of the stack.
groupedboolean-Should the bars be grouped on index axis.
When true, all the datasets at same index value will be placed next to each other centering on that index value.
When false, each bar is placed on its actual index-axis value.
hoverBackgroundColorString[] - IsColor[] - Pattern[] - Gradient[]YesThe fill color/pattern of the bars when hovered.
hoverBorderColorString[] - IsColor[] - Gradient[]YesThe stroke color of the bars when hovered.
hoverBorderWidthint[]YesThe stroke width of the bars when hovered.
hoverBorderRadiusint[] - BarBorderRadius[]YesThe bar border radius (in pixels) when hovered.
indexAxisIndexAxis-The base axis of the dataset. 'IndexAxis.X' for vertical bars and 'IndexAxis.Y' for horizontal bars.
inflateAmountint[]YesThe amount of pixels to inflate the bar rectangles, when drawing.
labelString-The label for the dataset which appears in the legend and tooltips.
maxBarThicknessint-The maximum bar thickness, to ensure that bars are not sized thicker than this.
minBarLengthint-Set this to ensure that bars have a minimum length in pixels.
normalizedboolean-If true, you provide data with indices that are unique, sorted, and consistent across data sets and provide the normalized.
orderint-The drawing order of dataset. Also affects order for stacking, tooltip, and legend.
pointStylePointStyle - Img - CanvasYesPoint style for legend.
parsingboolean-If false, the data set parsing is disable. If parsing is disabled, data must be sorted and in the formats the associated chart type and scales use internally.
xAxisIDString-The ID of the x axis to plot this dataset on.
yAxisIDString-The ID of the y axis to plot this dataset on.

General

The general options for a bar dataset can control behaviors not related to styling or interactions and they are the following:

NameDefaultsDescription
clipUndefined.DOUBLEHow to clip relative to chart area.
indexAxisIndexAxis.XThe base axis of the dataset.
labelnullThe label for the dataset which appears in the legend and tooltips.
normalizedfalseIf true, you provide data with indices that are unique, sorted, and consistent across data sets and provide the normalized.
order0The drawing order of dataset. Also affects order for stacking, tooltips, and legend.
parsingtrueIf false, the data set parsing is disable. If parsing is disabled, data must be sorted and in the formats the associated chart type and scales use internally.
xAxisIDDefaultScaleId.XThe ID of the x axis to plot this dataset on.
yAxisIDDefaultScaleId.YThe ID of the y axis to plot this dataset on.

Styling

The style of each bar of the dataset can be configured by the following properties:

NameDescription
backgroundColorThe fill color of the bar.
baseBase value for the bar in data units along the value axis.
borderColorThe color of the bar border.
borderSkippedWhich edge to skip drawing the border for.
borderRadiusThe bar border radius (in pixels).
borderWidthThe stroke width of the bar in pixels.
enableBorderRadiusIf true, it only shows the border radius of a bar when the bar is at the end of the stack.
inflateAmountThis option can be used to inflate the rects that are used to draw the bars. This can be used to hide artifacts between bars when (barPercentage * categoryPercentage) is equals to 1.
pointStylePoint style for legend.

All above options have got the fallback to the associated Bar element, retrievable by the following statements:

// from chart instance
Bar bar = chart.getOptions().getElements().getBar();
// sets options for all bar datasets of the chart
bar.setBorderRadius(6);
...
// from defaults
Bar defaultBar = Defaults.get().getGlobal().getElements().getBar();
// sets options for all bar datasets of all charts
defaultBar.setBorderRadius(6);

Interactions

The interactions can change the styling of each bar of the dataset which can set by the following properties:

NameDescription
hoverBackgroundColorThe fill color of the bars when hovered.
hoverBorderColorThe stroke color of the bars when hovered.
hoverBorderRadiusThe bar border radius (in pixels) when hovered.
hoverBorderWidthThe stroke width of the bars when hovered.

All above options have got the fallback to the associated Bar elements, retrievable by the following statements:

// from chart instance
Bar bar = chart.getOptions().getElements().getBar();
// sets options for all bar datasets of the chart
bar.setHoverBorderRadius(6);
...
// from defaults
Bar defaultBar = Defaults.get().getGlobal().getElements().getBar();
// sets options for all bar datasets of all charts
defaultBar.setHoverBorderRadius(6);

Scriptable

Scriptable options at dataset level accept a callback which is called for each of the underlying data values. See more details in Configuring charts section.

// creates chart
BarChart chart = new BarChart();
// creates dataset
BarDataset dataset = chart.newDataset();
// sets the option by a callback
dataset.setBackgroundColor(new ColorCallback<DatasetContext>(){

@Override
public IsColor invoke(DatasetContext context){
// logic
return color;
}
});

The following options can be set by a callback:

NameCallbackReturned types
baseBaseCallbackdouble
backgroundColorColorCallback<DatasetContext>String - IsColor - Pattern - Gradient
borderColorColorCallback<DatasetContext>String - IsColor - Gradient
borderSkippedBorderSkippedCallbackBorderSkipped
borderRadiusBorderRadiusCallback<DatasetContext>int - BarBorderRadius
borderWidthBarBorderWidthCallbackint - BarBorderWidth
enableBorderRadiusEnableBorderRadiusCallbackboolean
hoverBackgroundColorColorCallback<DatasetContext>String - IsColor - Pattern - Gradient
hoverBorderColorColorCallback<DatasetContext>String - IsColor - Gradient
hoverBorderWidthBarBorderWidthCallbackint - BarBorderWidth
hoverBorderRadiusBorderRadiusCallback<DatasetContext>int - BarBorderRadius
inflateAmountInflateAmountCallbackint(1)
pointStylePointStyleCallback<DatasetContext>boolean - PointStyle - Img - Canvas

(1)To enable autoInflateAmount by the inflate amount callback, the value to return must be Undefined.INTEGER.

Configuration

The bar chart accepts specific configuration not related to the styling or interactions which can be manage by the following options:

NameDescription
barPercentageThe percent (0-1) of the available width each bar should be within the category width. 1.0 will take the whole category width and put the bars right next to each other.
barThicknessThe width of each bar in pixels.
categoryPercentageThe percent (0-1) of the available width each category should be within the sample width.
groupedShould the bars be grouped on index axis.
When true, all the datasets at same index value will be placed next to each other centering on that index value.
When false, each bar is placed on its actual index-axis value.
maxBarThicknessThe maximum bar thickness, to ensure that bars are not sized thicker than this.
minBarLengthSet this to ensure that bars have a minimum length in pixels.

All above options have got the fallback to the associated Datasets elements which enables to define common values of the options for all bar datasets of the chart of globally, as following:

// from chart instance
Datasets datasets = chart.getOptions().getDatasets();
// sets options for all bar datasets of the chart
datasets.setBarPercentage(0.7);
...
// from defaults
Datasets defaultDatasets = Defaults.get().getGlobal().getDatasets();
// sets options for all bar datasets of all charts
defaultDatasets.setBarPercentage(0.7);

Data structure

The data of a dataset for a bar chart can be passed in two formats.

Data as doubles

When the data is an array or list of doubles, the x axis is generally a category. The points are placed onto the axis using their position in the array. When a bar chart is created with a category axis, the labels property of the data object must be specified.

// sets data as an array of doubles
dataset.setData(1,2,3,4,6);

// sets data as a list of double
List<Double> list = new LinkedList<>();
list.add(1);
list.add(2);
list.add(3);
dataset.setData(list);

Data as DataPoint

You can also specify the dataset when using the time series charts. Each data point is specified using an object containing y and x (as time type) properties for time series.

// creates a datapoint
DataPoint dp1 = new DataPoint();
dp1.setY(10);
dp1.setX(new Date());
// creates a datapoint
DataPoint dp2 = new DataPoint();
dp2.setY(30);
dp2.setX(new Date());
// sets data by an array of datapoints
dataset.setDataPoint(dp1, dp2);

Floating bars

You can also specify the dataset for a bar chart as list of floating bar data or by an array of arrays of doubles. This will force rendering of bars with the start and the end point of a bar respectively.

// sets in the constructor
FloatingData fd1 = new FloatingData(10, 30);
// sets by method to update values
FloatingData fd2 = new FloatingData();
fd2.setValues(-10, 60)
// stores the data to dataset
dataset.setFloatingData(fd1, fd2);

Options

The bar chart defines specific options implementation to be configured. These options are merged with the global chart configuration options to form the options passed to the chart.

// creates the chart
BarChart chart = new BarChart();
// gets the chart options
BarOptions options = chart.getOptions();
// sets options
options.setResponsive(true);
options.setSkipNull(true);

These are the options specific to bar charts:

NameTypeDefaultDescription
skipNullbooleantrueIf true, NaN values will not be drawn.