UNPKG

@gitlab/ui

Version:
212 lines (197 loc) 6.05 kB
import merge from 'lodash/merge'; import truncate from 'lodash/truncate'; import { dataZoomAdjustments, mergeSeriesToOptions, grid } from '../../../utils/charts/config'; import { CHART_DEFAULT_SERIES_STACK, HEIGHT_AUTO_CLASSES } from '../../../utils/charts/constants'; import { colorFromDefaultPalette } from '../../../utils/charts/theme'; import { engineeringNotation } from '../../../utils/number_utils'; import { hexToRgba } from '../../../utils/utils'; import Chart from '../chart/chart'; import ChartTooltip from '../shared/tooltip/tooltip'; import { stackedPresentationOptions } from '../../../utils/constants'; import __vue_normalize__ from 'vue-runtime-helpers/dist/normalize-component.js'; // /** * `nameGap` in charts/config is set to 50 but it is not * used for bar charts as the axes are flipped. That is why * we're explicitly setting it here */ const DEFAULT_NAME_GAP = 50; /** * This is the magic number at which the y-axis name * and y-axis labels don't overlap * @Number */ const AXIS_LABEL_LENGTH = 7; /** * Because the axes are reversed in bar charts defaultChartOptions * xAxis and yAxis needs to be handled specifically. */ const defaultOptions = { grid, xAxis: { nameLocation: 'center', axisLabel: { formatter: num => engineeringNotation(num, 2) } }, yAxis: { nameGap: DEFAULT_NAME_GAP, boundaryGap: true, nameLocation: 'center', splitLine: { show: false }, axisLabel: { interval: 0, formatter: str => truncate(str, { length: AXIS_LABEL_LENGTH, separator: '...' }) } }, legend: { show: false } }; var script = { name: 'GlBarChart', components: { Chart, ChartTooltip }, inheritAttrs: false, props: { data: { type: Object, required: true }, option: { type: Object, required: false, default: () => ({}) }, yAxisTitle: { type: String, required: true }, xAxisTitle: { type: String, required: true }, xAxisType: { type: String, required: false, default: 'value' }, /** * Sets the chart's height in pixels. Set to `"auto"` to use the height of the container. */ height: { type: [Number, String], required: false, default: null }, /** * Controls how multiple series data are displayed in the chart – * `stacked` stacks series horizontally, and * `tiled` displays series as grouped bars. */ presentation: { type: String, required: false, default: stackedPresentationOptions.stacked, validator: value => Object.values(stackedPresentationOptions).includes(value) } }, data() { return { chart: null }; }, computed: { series() { return Object.keys(this.data).map((key, index) => { const barColor = colorFromDefaultPalette(index); return { name: key, data: this.data[key], type: 'bar', stack: this.presentation === stackedPresentationOptions.tiled ? null : CHART_DEFAULT_SERIES_STACK, itemStyle: { color: hexToRgba(barColor, 0.2), borderColor: barColor, borderWidth: 1 }, emphasis: { itemStyle: { color: hexToRgba(barColor, 0.4) } }, barMaxWidth: '50%' }; }); }, options() { const mergedOptions = merge({}, defaultOptions, { xAxis: { axisLine: { show: false }, name: this.xAxisTitle, type: this.xAxisType }, yAxis: { name: this.yAxisTitle, type: 'category', axisTick: { show: true } } }, this.option, dataZoomAdjustments(this.option.dataZoom)); // All chart options can be merged but series // needs to be handled specially return mergeSeriesToOptions(mergedOptions, this.series); }, autoHeight() { return this.height === 'auto'; } }, methods: { onCreated(chart) { this.chart = chart; this.$emit('created', chart); } }, HEIGHT_AUTO_CLASSES }; /* script */ const __vue_script__ = script; /* template */ var __vue_render__ = function () { var _obj; var _vm=this;var _h=_vm.$createElement;var _c=_vm._self._c||_h;return _c('div',{staticClass:"gl-relative",class:( _obj = {}, _obj[_vm.$options.HEIGHT_AUTO_CLASSES] = _vm.autoHeight, _obj )},[_c('chart',_vm._g(_vm._b({class:{ 'gl-grow': _vm.autoHeight },attrs:{"height":_vm.height,"options":_vm.options},on:{"created":_vm.onCreated}},'chart',_vm.$attrs,false),_vm.$listeners)),_vm._v(" "),(_vm.chart)?_c('chart-tooltip',{attrs:{"chart":_vm.chart,"use-default-tooltip-formatter":true,"dimension-axis":"yAxis"},scopedSlots:_vm._u([(_vm.$scopedSlots['tooltip-title'])?{key:"title",fn:function(scope){return [_vm._t("tooltip-title",null,null,scope)]}}:null,(_vm.$scopedSlots['tooltip-content'])?{key:"default",fn:function(scope){return [_vm._t("tooltip-content",null,null,scope)]}}:null,(_vm.$scopedSlots['tooltip-value'])?{key:"tooltip-value",fn:function(scope){return [_vm._t("tooltip-value",null,null,scope)]}}:null],null,true)}):_vm._e()],1)}; var __vue_staticRenderFns__ = []; /* style */ const __vue_inject_styles__ = undefined; /* scoped */ const __vue_scope_id__ = undefined; /* module identifier */ const __vue_module_identifier__ = undefined; /* functional template */ const __vue_is_functional_template__ = false; /* style inject */ /* style inject SSR */ /* style inject shadow dom */ const __vue_component__ = /*#__PURE__*/__vue_normalize__( { render: __vue_render__, staticRenderFns: __vue_staticRenderFns__ }, __vue_inject_styles__, __vue_script__, __vue_scope_id__, __vue_is_functional_template__, __vue_module_identifier__, false, undefined, undefined, undefined ); export { __vue_component__ as default };