@gitlab/ui
Version:
GitLab UI Components
276 lines (241 loc) • 8.14 kB
JavaScript
import merge from 'lodash/merge';
import defaultChartOptions, { dataZoomAdjustments, mergeSeriesToOptions } from '../../../utils/charts/config';
import { colorFromDefaultPalette } from '../../../utils/charts/theme';
import { debounceByAnimationFrame } from '../../../utils/utils';
import ToolboxMixin from '../../mixins/toolbox_mixin';
import TooltipDefaultFormat from '../../shared_components/charts/tooltip_default_format';
import Chart from '../chart/chart';
import ChartTooltip from '../tooltip/tooltip';
import __vue_normalize__ from 'vue-runtime-helpers/dist/normalize-component.js';
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _slicedToArray(arr, i) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
}
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
}
function _iterableToArrayLimit(arr, i) {
if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return;
var _arr = [];
var _n = true;
var _d = false;
var _e = undefined;
try {
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally {
try {
if (!_n && _i["return"] != null) _i["return"]();
} finally {
if (_d) throw _e;
}
}
return _arr;
}
function _unsupportedIterableToArray(o, minLen) {
if (!o) return;
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(o);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}
function _arrayLikeToArray(arr, len) {
if (len == null || len > arr.length) len = arr.length;
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
return arr2;
}
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
var gray200 = '#bfbfbf';
var script = {
components: {
Chart: Chart,
ChartTooltip: ChartTooltip,
TooltipDefaultFormat: TooltipDefaultFormat
},
mixins: [ToolboxMixin],
inheritAttrs: false,
props: {
data: {
type: Array,
required: true
},
option: {
type: Object,
required: false,
default: function _default() {
return {};
}
},
yAxisTitle: {
type: String,
required: true
},
xAxisTitle: {
type: String,
required: true
},
symbolSize: {
type: Number,
required: false,
default: 8
},
formatTooltipText: {
type: Function,
required: false,
default: null
}
},
data: function data() {
return {
chart: null,
showTooltip: false,
tooltipTitle: '',
tooltipContent: {},
tooltipPosition: {
left: '0',
top: '0'
},
debouncedShowHideTooltip: debounceByAnimationFrame(this.showHideTooltip),
selectedFormatTooltipText: this.formatTooltipText || this.defaultFormatTooltipText
};
},
computed: {
series: function series() {
var _this = this;
return this.data.map(function (series, index) {
var defaultColor = colorFromDefaultPalette(index);
var getColor = function getColor(type) {
return series[type] && series[type].color ? series[type].color : defaultColor;
};
return merge({
symbolSize: _this.symbolSize,
lineStyle: {
color: getColor('lineStyle')
},
itemStyle: {
color: getColor('itemStyle')
}
}, series);
});
},
options: function options() {
var mergedOptions = merge({}, defaultChartOptions, {
tooltip: {
formatter: this.onLabelChange
},
xAxis: {
type: 'category',
name: this.xAxisTitle,
axisTick: {
alignWithLabel: true,
show: true,
lineStyle: {
color: gray200
}
},
axisLabel: {
margin: 20,
verticalAlign: 'bottom'
}
},
yAxis: {
type: 'value',
name: this.yAxisTitle
},
legend: {
show: false
}
}, this.option, dataZoomAdjustments(this.option.dataZoom), this.toolboxAdjustments); // All chart options can be merged but series
// needs to be handled specially
return mergeSeriesToOptions(mergedOptions, this.series);
}
},
methods: {
defaultFormatTooltipText: function defaultFormatTooltipText(params) {
var data = params.data;
var _data = _slicedToArray(data, 2),
title = _data[0],
content = _data[1];
this.tooltipTitle = title;
var seriesName = this.yAxisTitle;
var tooltipContent = _defineProperty({}, seriesName, {
value: content,
color: ''
});
this.$set(this, 'tooltipContent', tooltipContent);
},
showHideTooltip: function showHideTooltip(mouseEvent) {
this.showTooltip = this.chart.containPixel('grid', [mouseEvent.zrX, mouseEvent.zrY]);
},
onCreated: function onCreated(chart) {
chart.getDom().addEventListener('mousemove', this.debouncedShowHideTooltip);
chart.getDom().addEventListener('mouseout', this.debouncedShowHideTooltip);
this.chart = chart;
this.$emit('created', chart);
},
onLabelChange: function onLabelChange(params) {
this.selectedFormatTooltipText(params);
var _params$data = params.data,
data = _params$data === void 0 ? [] : _params$data;
if (data.length) {
var _this$chart$convertTo = this.chart.convertToPixel('grid', data),
_this$chart$convertTo2 = _slicedToArray(_this$chart$convertTo, 2),
left = _this$chart$convertTo2[0],
top = _this$chart$convertTo2[1];
this.tooltipPosition = {
left: "".concat(left, "px"),
top: "".concat(top, "px")
};
}
}
}
};
/* script */
const __vue_script__ = script;
/* template */
var __vue_render__ = function () {var _vm=this;var _h=_vm.$createElement;var _c=_vm._self._c||_h;return _c('div',{staticClass:"position-relative"},[_c('chart',_vm._g(_vm._b({attrs:{"options":_vm.options},on:{"created":_vm.onCreated}},'chart',_vm.$attrs,false),_vm.$listeners)),_vm._v(" "),(_vm.chart)?_c('chart-tooltip',{attrs:{"show":_vm.showTooltip,"chart":_vm.chart,"top":_vm.tooltipPosition.top,"left":_vm.tooltipPosition.left}},[(_vm.formatTooltipText)?[_vm._t("tooltip-title",null,{"slot":"title"}),_vm._v(" "),_vm._t("tooltip-content")]:[_c('div',{attrs:{"slot":"title"},slot:"title"},[_vm._v(_vm._s(_vm.tooltipTitle)+" ("+_vm._s(_vm.xAxisTitle)+")")]),_vm._v(" "),_c('tooltip-default-format',{attrs:{"tooltip-content":_vm.tooltipContent}})]],2):_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__ = __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 default __vue_component__;