UNPKG

@gitlab/ui

Version:
316 lines (302 loc) • 11.6 kB
import merge from 'lodash/merge'; import { symbolSize, lineStyle, getThresholdConfig, generateAnnotationSeries, defaultChartOptions, dataZoomAdjustments, mergeSeriesToOptions, mergeAnnotationAxisToOptions, grid } from '../../../utils/charts/config'; import { LEGEND_AVERAGE_TEXT, LEGEND_MAX_TEXT, LEGEND_MIN_TEXT, LEGEND_CURRENT_TEXT, LEGEND_LAYOUT_INLINE, LEGEND_LAYOUT_TABLE, HEIGHT_AUTO_CLASSES } from '../../../utils/charts/constants'; import { colorFromDefaultPalette } from '../../../utils/charts/theme'; import { seriesHasAnnotations, isDataPointAnnotation } from '../../../utils/charts/utils'; import Chart from '../chart/chart'; import ChartLegend from '../legend/legend'; import ChartTooltip from '../shared/tooltip/tooltip'; import __vue_normalize__ from 'vue-runtime-helpers/dist/normalize-component.js'; // var script = { name: 'GlLineChart', components: { Chart, ChartLegend, ChartTooltip }, inheritAttrs: false, props: { data: { type: Array, required: true }, option: { type: Object, required: false, default: () => ({}) }, thresholds: { type: Array, required: false, default: () => [] }, annotations: { type: Array, required: false, default: () => [] }, includeLegendAvgMax: { type: Boolean, required: false, default: true }, /** * Callback called when showing or refreshing a tooltip. * **Deprecated:** Use slots `#tooltip-title`, `#tooltip-content` or `#tooltip-value`. * * @deprecated Use slots `#tooltip-title`, `#tooltip-content` or `#tooltip-value`. */ formatTooltipText: { type: Function, required: false, default: null }, legendAverageText: { type: String, required: false, default: LEGEND_AVERAGE_TEXT }, legendMaxText: { type: String, required: false, default: LEGEND_MAX_TEXT }, legendMinText: { type: String, required: false, default: LEGEND_MIN_TEXT }, legendCurrentText: { type: String, required: false, default: LEGEND_CURRENT_TEXT }, legendLayout: { type: String, required: false, default: LEGEND_LAYOUT_INLINE, validator(layout) { return [LEGEND_LAYOUT_INLINE, LEGEND_LAYOUT_TABLE].indexOf(layout) !== -1; } }, showLegend: { type: Boolean, required: false, default: true }, /** * 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 } }, data() { // Part of the tooltip related data can be // moved into the tooltip component. // Tracking that progress in // https://gitlab.com/gitlab-org/gitlab-ui/-/issues/618 return { chart: null, compiledOptions: null, showAnnotationsTooltip: false, annotationsTooltipTitle: '', annotationsTooltipContent: '', annotationsTooltipPosition: { left: '0', top: '0' } }; }, computed: { series() { const dataSeries = this.data.map((series, index) => { const defaultColor = colorFromDefaultPalette(index); const getColor = type => { var _series$type$color, _series$type; return (_series$type$color = (_series$type = series[type]) === null || _series$type === void 0 ? void 0 : _series$type.color) !== null && _series$type$color !== void 0 ? _series$type$color : defaultColor; }; return merge({ showSymbol: true, lineStyle: { color: getColor('lineStyle') }, itemStyle: { color: getColor('itemStyle') } }, symbolSize, lineStyle, series, getThresholdConfig(this.thresholds)); }); // if annotation series exists, append it // along with data series if (this.annotationSeries) { return [...dataSeries, this.annotationSeries]; } return dataSeries; }, annotationSeries() { return generateAnnotationSeries(this.annotations); }, options() { const lineChartOptions = { xAxis: { axisTick: { alignWithLabel: true, show: true, lineStyle: { color: 'var(--gl-chart-axis-line-color)' } } } }; // `formatTooltipText` is deprecated, these added options should be // removed when `formatTooltipText` is removed. const deprecatedTooltipFormatterOptions = { xAxis: { axisPointer: { show: true, label: { formatter: this.formatTooltipText } } } }; const mergedOptions = merge({}, defaultChartOptions, lineChartOptions, this.formatTooltipText ? deprecatedTooltipFormatterOptions : {}, this.option, dataZoomAdjustments(this.option.dataZoom)); // All chart options can be merged but series // needs to be handled specially return mergeSeriesToOptions(mergeAnnotationAxisToOptions(mergedOptions, this.hasAnnotations), this.series); }, /** * Annotations currently are passed as series options in monitoring dashboard. * Once https://gitlab.com/gitlab-org/gitlab/-/issues/213390 is closed, * annotations will be passed as props and not as series options. * * For backward compatibility, we're having to check for both. */ hasAnnotations() { return this.annotations.length !== 0 || seriesHasAnnotations(this.option.series); }, shouldShowAnnotationsTooltip() { return this.chart && this.hasAnnotations; }, legendStyle() { return { paddingLeft: `${grid.left}px` }; }, hasLegend() { return this.showLegend && this.compiledOptions; }, seriesInfo() { var _this$compiledOptions; const compiledSeries = ((_this$compiledOptions = this.compiledOptions) === null || _this$compiledOptions === void 0 ? void 0 : _this$compiledOptions.series) || []; return compiledSeries.reduce((acc, series, index) => { if (series.type === 'line') { var _series$data; acc.push({ name: series.name, type: series.lineStyle.type, color: series.lineStyle.color || colorFromDefaultPalette(index), data: this.includeLegendAvgMax ? (_series$data = series.data) === null || _series$data === void 0 ? void 0 : _series$data.map(data => data[1]) : undefined }); } return acc; }, []); }, autoHeight() { return this.height === 'auto'; } }, beforeDestroy() { this.chart.off('mouseout', this.onChartDataPointMouseOut); this.chart.off('mouseover', this.onChartDataPointMouseOver); }, methods: { defaultAnnotationTooltipText(params) { var _params$data$tooltipD; return { title: params.data.xAxis, content: (_params$data$tooltipD = params.data.tooltipData) === null || _params$data$tooltipD === void 0 ? void 0 : _params$data$tooltipD.content }; }, onCreated(chart) { // eCharts inbuild mouse events // https://echarts.apache.org/en/api.html#events.Mouse%20events // is used to attach listeners to markPoints. These listeners // are currently used for annotation arrows at the bottom of the chart. // Because data points and annotations arrows are in different // sections of the charts with their own mouseovers and mouseouts, // there shouldn't be an overlapping situation where both tooltips // are visible. if (this.hasAnnotations) { chart.on('mouseout', this.onChartDataPointMouseOut); chart.on('mouseover', this.onChartDataPointMouseOver); } this.chart = chart; this.$emit('created', chart); }, onUpdated() { this.compiledOptions = this.chart.getOption(); }, onChartDataPointMouseOut() { this.showAnnotationsTooltip = false; }, /** * Check if the hovered data point is an annotation * point to show the annotation tooltip. */ onChartDataPointMouseOver(params) { if (isDataPointAnnotation(params)) { const { event } = params; const toolTipFormatter = this.formatAnnotationsTooltipText || this.defaultAnnotationTooltipText; const { title = '', content = '' } = toolTipFormatter(params); this.showAnnotationsTooltip = true; this.annotationsTooltipTitle = title; this.annotationsTooltipContent = content; this.annotationsTooltipPosition = { left: `${event.event.zrX}px`, top: `${event.event.zrY}px` }; } } }, 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,"updated":_vm.onUpdated}},'chart',_vm.$attrs,false),_vm.$listeners)),_vm._v(" "),(_vm.shouldShowAnnotationsTooltip)?_c('chart-tooltip',{ref:"annotationsTooltip",attrs:{"id":"annotationsTooltip","show":_vm.showAnnotationsTooltip,"chart":_vm.chart,"top":_vm.annotationsTooltipPosition.top,"left":_vm.annotationsTooltipPosition.left,"placement":"bottom"},scopedSlots:_vm._u([{key:"title",fn:function(){return [_c('div',[_vm._v(_vm._s(_vm.annotationsTooltipTitle))])]},proxy:true}],null,false,1889294429)},[_vm._v(" "),_c('div',[_vm._v(_vm._s(_vm.annotationsTooltipContent))])]):_vm._e(),_vm._v(" "),(_vm.chart)?_c('chart-tooltip',{ref:"dataTooltip",attrs:{"chart":_vm.chart,"use-default-tooltip-formatter":!_vm.formatTooltipText},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(),_vm._v(" "),(_vm.hasLegend)?_c('chart-legend',{style:(_vm.legendStyle),attrs:{"chart":_vm.chart,"series-info":_vm.seriesInfo,"text-style":_vm.compiledOptions.textStyle,"min-text":_vm.legendMinText,"max-text":_vm.legendMaxText,"average-text":_vm.legendAverageText,"current-text":_vm.legendCurrentText,"layout":_vm.legendLayout}}):_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 };