Back to Woocommerce

README

packages/js/components/src/chart/README.md

10.8.0-dev4.7 KB
Original Source

Chart

A chart container using d3, to display timeseries data with an interactive legend.

Usage

jsx
const data = [
	{
		date: '2018-05-30T00:00:00',
		Hoodie: {
			label: 'Hoodie',
			value: 21599,
		},
		Sunglasses: {
			label: 'Sunglasses',
			value: 38537,
		},
		Cap: {
			label: 'Cap',
			value: 106010,
		},
	},
	{
		date: '2018-05-31T00:00:00',
		Hoodie: {
			label: 'Hoodie',
			value: 14205,
		},
		Sunglasses: {
			label: 'Sunglasses',
			value: 24721,
		},
		Cap: {
			label: 'Cap',
			value: 70131,
		},
	},
	{
		date: '2018-06-01T00:00:00',
		Hoodie: {
			label: 'Hoodie',
			value: 10581,
		},
		Sunglasses: {
			label: 'Sunglasses',
			value: 19991,
		},
		Cap: {
			label: 'Cap',
			value: 53552,
		},
	},
	{
		date: '2018-06-02T00:00:00',
		Hoodie: {
			label: 'Hoodie',
			value: 9250,
		},
		Sunglasses: {
			label: 'Sunglasses',
			value: 16072,
		},
		Cap: {
			label: 'Cap',
			value: 47821,
		},
	},
];

<Chart data={ data } title="Example Chart" layout="item-comparison" />

Props

NameTypeDefaultDescription
allowedIntervalsArraynullAllowed intervals to show in a dropdown
baseValueNumber0Base chart value. If no data value is different than the baseValue, the emptyMessage will be displayed if provided
chartTypeOne of: 'bar', 'line''line'Chart type of either line or bar
dataArray[]An array of data
dateParserString'%Y-%m-%dT%H:%M:%S'Format to parse dates into d3 time format
emptyMessageStringnullThe message to be displayed if there is no data to render. If no message is provided, nothing will be displayed
filterParamStringnullName of the param used to filter items. If specified, it will be used, in combination with query, to detect which elements are being used by the current filter and must be displayed even if their value is 0
itemsLabelStringnullLabel describing the legend items
modeOne of: 'item-comparison', 'time-comparison''time-comparison'item-comparison (default) or time-comparison, this is used to generate correct ARIA properties
pathStringnullCurrent path
queryObjectnullThe query string represented in object form
interactiveLegendBooleantrueWhether the legend items can be activated/deactivated
intervalOne of: 'hour', 'day', 'week', 'month', 'quarter', 'year''day'Interval specification (hourly, daily, weekly etc)
intervalDataObjectnullInformation about the currently selected interval, and set of allowed intervals for the chart. See getIntervalsForQuery
isRequestingBooleanfalseRender a chart placeholder to signify an in-flight data request
legendPositionOne of: 'bottom', 'side', 'top', 'hidden'nullPosition the legend must be displayed in. If it's not defined, it's calculated depending on the viewport width and the mode
legendTotalsObjectnullValues to overwrite the legend totals. If not defined, the sum of all line values will be used
screenReaderFormatOne of type: string, func'%B %-d, %Y'A datetime formatting string or overriding function to format the screen reader labels
showHeaderControlsBooleantrueWhether header UI controls must be displayed
titleStringnullA title describing this chart
tooltipLabelFormatOne of type: string, func'%B %-d, %Y'A datetime formatting string or overriding function to format the tooltip label
tooltipValueFormatOne of type: string, func','A number formatting string or function to format the value displayed in the tooltips
tooltipTitleStringnullA string to use as a title for the tooltip. Takes preference over tooltipLabelFormat
valueTypeStringnullWhat type of data is to be displayed? Number, Average, String?
xFormatString'%d'A datetime formatting string, passed to d3TimeFormat
x2FormatString'%b %Y'A datetime formatting string, passed to d3TimeFormat
yBelow1FormatStringnullA number formatting string, passed to d3Format
yFormatStringnullA number formatting string, passed to d3Format
currencyObject{}An object with currency properties for usage in the chart. The following properties are expected: decimal, symbol, symbolPosition, thousands. This is passed to d3Format.

Overriding chart colors

Char colors can be overridden by hooking into the filter woocommerce_admin_chart_item_color. For example:

js
const colorScales = [
  "#0A2F51",
  "#0E4D64",
  "#137177",
  "#188977",
];
addFilter( 'woocommerce_admin_chart_item_color', 'example', ( index, key, orderedKeys ) => colorScales[index] );