Important Note for the React Kit: In order to leverage this kit, you must install highcharts
and highcharts-react-official
into your project as shown below. To then apply Playbook styles to your Highchart, import BarGraphTheme.ts from playbook-ui and merge it with your Highchart options. Then, pass the merged value to the options prop. Playbook’s styling will be applied automatically. See the examples in the documentation below.
import React from 'react' import { barGraphTheme } from 'playbook-ui' import Highcharts from "highcharts"; import HighchartsReact from "highcharts-react-official"; const chartData = [{ name: 'Installation', data: [1475, 200, 3000, 654, 656], }, { name: 'Manufacturing', data: [4434, 524, 2320, 440, 500], }, { name: 'Sales & Distribution', data: [3387, 743, 1344, 434, 440], }, { name: 'Project Development', data: [3227, 878, 999, 780, 1000], }, { name: 'Other', data: [1111, 677, 3245, 500, 200], }] const chartOptions = { series: chartData, title: { text: 'Solar Employment Growth by Sector, 2010-2016', }, subtitle: { text: 'Source: thesolarfoundation.com', }, xAxis: { categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May'], }, yAxis: { title: { text: 'Number of Employees', }, }, } const BarGraphDefault = () => { const options = Highcharts.merge({}, barGraphTheme, chartOptions) return ( <div> <HighchartsReact highcharts={Highcharts} options={options} /> </div> ) } export default BarGraphDefault
import React from 'react' import { barGraphTheme } from 'playbook-ui' import Highcharts from "highcharts"; import HighchartsReact from "highcharts-react-official"; const chartData = [{ name: 'Number of Installations', data: [1475, 200, 3000, 654, 656], }] const chartOptions = { series: chartData, title: { text: 'Bar Graph with Legend', }, xAxis: { categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May'], }, yAxis: { title: { text: 'Number of Employees', }, }, legend: { enabled: true }, } const BarGraphLegend = () => { const options = Highcharts.merge({}, barGraphTheme, chartOptions) return ( <div> <HighchartsReact highcharts={Highcharts} options={options} /> </div> ) } export default BarGraphLegend
align
Type: String | Values: left | center | right (defaults to center)
verticalAlign
Type: String | Values: top | middle | bottom (defaults middle)
layout
Type: String | Values: horizontal | vertical | proximate (defaults to horizontal)
x
Type: Number (defaults to 0)
y
Type: Number (defaults to 0)
layout
determines the position of the legend items
layout: proximate
will place the legend items as close as possible to the graphs they're representing. It will also determine whether to place the legend above/below or on the side of the plot area, if the legend is in a corner.
x
offsets the legend relative to its horizontal alignmnet. Negative x moves it to the left, positive x moves it to the right
y
offsets the legend relative to its vertical alignmnet. Negative y moves it up, positive y moves it down.
import React from 'react' import { barGraphTheme, Title } from 'playbook-ui' import Highcharts from "highcharts"; import HighchartsReact from "highcharts-react-official"; const chartData = [{ name: 'Installation', data: [1475], }, { name: 'Manufacturing', data: [4434], }, { name: 'Sales & Distribution', data: [3387], }, { name: 'Project Development', data: [3227], }, { name: 'Other', data: [1111], }] const alignChartOptions = { series: chartData, title: { text: 'Alignment of Legend', }, subtitle: { text: 'Source: thesolarfoundation.com', }, xAxis: { categories: ['Jan'], }, yAxis: { title: { text: 'Number of Employees', }, }, legend: { enabled: true, verticalAlign: 'top', align: 'right', }, } const layoutChartOptions = { series: chartData, title: { text: 'Layout of Legend', }, xAxis: { categories: ['Jan'], }, yAxis: { title: { text: 'Number of Employees', }, }, legend: { enabled: true, layout: 'vertical', }, } const offsetChartOptions = { series: chartData, title: { text: 'Offset of Legend', }, xAxis: { categories: ['Jan'], }, yAxis: { title: { text: 'Number of Employees', }, }, legend: { enabled: true, layout: 'vertical', x: 100, y: 10, }, } const BarGraphLegendPosition = () => { const alignOptions = Highcharts.merge({}, barGraphTheme, alignChartOptions) const layoutOptions = Highcharts.merge({}, barGraphTheme, layoutChartOptions) const offsetOptions = Highcharts.merge({}, barGraphTheme, offsetChartOptions) return ( <div> <Title paddingBottom="sm" paddingTop="sm" size={4} tag="h4" text="align | verticalAlign" /> <HighchartsReact highcharts={Highcharts} options={alignOptions} /> <Title paddingBottom="sm" paddingTop="sm" size={4} tag="h4" text="layout" /> <HighchartsReact highcharts={Highcharts} options={layoutOptions} /> <Title paddingBottom="sm" paddingTop="sm" size={4} tag="h4" text="x | y" /> <HighchartsReact highcharts={Highcharts} options={offsetOptions} /> </div> ) } export default BarGraphLegendPosition
import React from 'react' import { barGraphTheme } from 'playbook-ui' import Highcharts from "highcharts"; import HighchartsReact from "highcharts-react-official"; const chartData = [{ name: 'Number of Installations', data: [1475, 200, 3000, 654, 656], }] const chartOptions = { title: { text: 'Bar Graph with Legend Non Clickable', }, xAxis: { categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May'], }, yAxis: { title: { text: 'Number of Employees', }, }, legend: { enabled: true, events: { itemClick: function () { return false; } } }, series: chartData } const BarGraphLegendNonClickable = () => { const options = Highcharts.merge({}, barGraphTheme, chartOptions) return ( <div> <HighchartsReact highcharts={Highcharts} options={options} /> </div> ) } export default BarGraphLegendNonClickable
By default, Highcharts have a height of 400px, but this can be customized. You can override the default by specifying either a percentage or a fixed pixel value.
Using a percentage maintains a consistent aspect ratio across different responsive sizes. For example, setting the height to 50% makes the chart’s height half of its width.
import React from 'react' import { barGraphTheme } from 'playbook-ui' import Highcharts from "highcharts"; import HighchartsReact from "highcharts-react-official"; const chartData = [{ name: 'Number of Installations', data: [1475, 200, 3000, 654, 656], }] const pixelHeightChartOptions = { chart: { height: "300" }, series: chartData, title: { text: "Fixed Height (300px)", }, xAxis: { categories: ["Jan", "Feb", "Mar", "Apr", "May"], }, yAxis: { title: { text: "Number of Employees", }, }, }; const percentageHeightChartOptions = { chart: { height: "50%" }, series: chartData, title: { text: "Percentage Height (50%)", }, xAxis: { categories: ["Jan", "Feb", "Mar", "Apr", "May"], }, yAxis: { title: { text: "Number of Employees", }, }, }; const pixelOptions = Highcharts.merge({}, barGraphTheme, pixelHeightChartOptions) const percentageOptions = Highcharts.merge({}, barGraphTheme, percentageHeightChartOptions) const BarGraphHeight = () => ( <div> <HighchartsReact highcharts={Highcharts} options={pixelOptions} /> <br /> <br /> <HighchartsReact highcharts={Highcharts} options={percentageOptions} /> </div> ) export default BarGraphHeight
A spline can be added by including a separate chart data with the type: 'spline'
attribute.
A color can also be specified for the spline.
import React from 'react' import { barGraphTheme } from 'playbook-ui' import Highcharts from "highcharts"; import HighchartsReact from "highcharts-react-official"; const chartData = [{ name: 'Number of Installations', data: [1475, 200, 3000, 654, 656], }, { type: 'spline', name: 'Trend Line', data: [1975, 600, 2500, 924, 500], color: '#F9BB00', }] const chartOptions = { series: chartData, title: { text: "Bar Graph with Spline", }, xAxis: { categories: ["Jan", "Feb", "Mar", "Apr", "May"], }, yAxis: { title: { text: "Number of Employees", }, }, legend: { enabled: true }, }; const BarGraphSpline = () => { const options = Highcharts.merge({}, barGraphTheme, chartOptions) return ( <div> <HighchartsReact highcharts={Highcharts} options={options} /> </div> ) } export default BarGraphSpline
Custom data colors
allow for color customization to match the needs of business requirements.
Pass the prop colors and use desired values data-1 | data-2 | data-3 | data-4 | data-5 | data-6 | data-7 | data-8
in an array. Hex colors are also available eg: #CA0095
import React from 'react' import { colors, barGraphTheme } from 'playbook-ui' import Highcharts from "highcharts"; import HighchartsReact from "highcharts-react-official"; const chartData = [{ name: 'Installation', data: [1475, 200, 3000, 654, 656], }, { name: 'Manufacturing', data: [4434, 524, 2320, 440, 500], }, { name: 'Sales & Distribution', data: [3387, 743, 1344, 434, 440], }, { name: 'Project Development', data: [3227, 878, 999, 780, 1000], }, { name: 'Other', data: [1111, 677, 3245, 500, 200], }] const chartOptions = { series: chartData, title: { text: "Bar Graph with Custom Data Colors", }, xAxis: { categories: ["Jan", "Feb", "Mar", "Apr", "May"], }, yAxis: { title: { text: "Number of Employees", }, }, colors: [colors.data_4, colors.data_5, colors.data_6, colors.data_7, colors.data_8], } const BarGraphColors = () => { const options = Highcharts.merge({}, barGraphTheme, chartOptions) return ( <div> <HighchartsReact highcharts={Highcharts} options={options} /> </div> ) } export default BarGraphColors
The stacking
prop can be used for a stacked bar graph. The prop allows for normal
or percent
as options.
import React from 'react' import { barGraphTheme } from 'playbook-ui' import Highcharts from "highcharts"; import HighchartsReact from "highcharts-react-official"; const chartData = [{ name: 'Installation', data: [1475, 200, 3000, 654, 656], }, { name: 'Manufacturing', data: [1270, 800, 200, 454, 956], }, { name: 'Sales & Distribution', data: [975, 1600, 1500, 924, 500], }] const chartOptions = { series: chartData, title: { text: "Bar Graph with Stacked Columns", }, xAxis: { categories: ["Jan", "Feb", "Mar", "Apr", "May"], }, yAxis: { title: { text: "Number of Employees", }, }, plotOptions: { column: { stacking: 'normal', borderWidth: 0, } }, legend: { enabled: true }, }; const BarGraphStacked = () => { const options = Highcharts.merge({}, barGraphTheme, chartOptions) return ( <div> <HighchartsReact highcharts={Highcharts} options={options} /> </div> ) } export default BarGraphStacked
import React from 'react' import { barGraphTheme } from 'playbook-ui' import Highcharts from "highcharts"; import HighchartsReact from "highcharts-react-official"; const chartData = [{ name: 'Installation', data: [-475, 400, -1000, 354, -856], threshold: 0 }, { name: 'Manufacturing', data: [1475, 200, 1000, 654, -656], threshold: 0 }, { name: 'Sales & Distribution', data: [1270, 100, -1200, 554, 756], threshold: 0 }] const chartOptions = { series: chartData, title: { text: "Bar Graph with Negative Numbers", }, xAxis: { categories: ["Jan", "Feb", "Mar", "Apr", "May"], }, yAxis: { title: { text: "Number of Employees", }, }, legend: { enabled: true }, }; const BarGraphStacked = () => { const options = Highcharts.merge({}, barGraphTheme, chartOptions) return( <div> <HighchartsReact highcharts={Highcharts} options={options} /> </div> ) } export default BarGraphStacked
Optionally add a second yAxis to support secondary datasets (e.x., a spline) by passing yAxis: 1
to the second node of your chartData
array.
To customize the format and/or title of your secondary yAxis, pass your desired values as arrays through the axisFormat
and axisTitle
props, respectively.
import React from 'react' import { barGraphTheme, colors, typography } from 'playbook-ui' import Highcharts from "highcharts" import HighchartsReact from "highcharts-react-official" const chartData = [{ name: 'Number of Installations', data: [1475, 200, 3000, 654, 656], }, { type: 'spline', name: 'Percentage', data: [48, 70, 25, 55, 72], color: '#F9BB00', yAxis: 1 }] const chartOptions = { series: chartData, title: { text: "Bar Graph with Secondary Y-axis", }, xAxis: { categories: ["Jan", "Feb", "Mar", "Apr", "May"], }, yAxis: [{ labels: { style: { fontFamily: typography.font_family_base, color: colors.text_lt_lighter, fontWeight: typography.bold, fontSize: typography.text_smaller, }, }, title: { text: "Number of Employees", style: { fontFamily: typography.font_family_base, color: colors.text_lt_lighter, fontWeight: typography.bold, fontSize: typography.text_smaller, }, }, }, { labels: { style: { fontFamily: typography.font_family_base, color: colors.text_lt_lighter, fontWeight: typography.bold, fontSize: typography.text_smaller, }, }, title: { text: "Percentage", style: { fontFamily: typography.font_family_base, color: colors.text_lt_lighter, fontWeight: typography.bold, fontSize: typography.text_smaller, }, }, opposite: true, min: 0, max: 100 }], legend: { enabled: true }, } const BarGraphSecondaryYAxis = () => { const options = Highcharts.merge({}, barGraphTheme, chartOptions) return ( <div> <HighchartsReact highcharts={Highcharts} options={options} /> </div> ) } export default BarGraphSecondaryYAxis
import React from 'react' import { barGraphTheme } from 'playbook-ui' import Highcharts from "highcharts"; import HighchartsReact from "highcharts-react-official"; const chartOptions = { chart: { type: 'bar' }, title: { text: 'Historic World Population by Region', align: 'left' }, subtitle: { text: 'Source: <a ' + 'href="https://en.wikipedia.org/wiki/List_of_continents_and_continental_subregions_by_population"' + 'target="_blank">Wikipedia.org</a>', align: 'left' }, xAxis: { categories: ['Africa', 'America', 'Asia', 'Europe'], lineWidth: 0 }, yAxis: { title: { text: '', }, }, tooltip: { valueSuffix: ' millions' }, plotOptions: { bar: { dataLabels: { enabled: true }, groupPadding: 0.1 } }, series: [{ name: 'Year 1990', data: [631, 727, 3202, 721] }, { name: 'Year 2000', data: [814, 841, 3714, 726] }, { name: 'Year 2018', data: [1276, 1007, 4561, 746] }], } const options = Highcharts.merge({}, barGraphTheme, chartOptions) const BarGraphHorizontal = () => ( <div> <HighchartsReact highcharts={Highcharts} options={options} /> </div> ) export default BarGraphHorizontal