debian-mirror-gitlab/app/assets/javascripts/monitoring/components/charts/column.vue

113 lines
2.6 KiB
Vue
Raw Normal View History

2019-09-30 21:07:59 +05:30
<script>
2020-03-13 15:44:24 +05:30
import { GlResizeObserverDirective } from '@gitlab/ui';
2019-09-30 21:07:59 +05:30
import { GlColumnChart } from '@gitlab/ui/dist/charts';
import { getSvgIconPathContent } from '~/lib/utils/icon_utils';
import { chartHeight } from '../../constants';
import { makeDataSeries } from '~/helpers/monitor_helper';
import { graphDataValidatorForValues } from '../../utils';
2020-04-08 14:13:33 +05:30
import { getYAxisOptions, getChartGrid } from './options';
2019-09-30 21:07:59 +05:30
export default {
components: {
GlColumnChart,
},
2020-03-13 15:44:24 +05:30
directives: {
GlResizeObserverDirective,
},
2019-09-30 21:07:59 +05:30
props: {
graphData: {
type: Object,
required: true,
validator: graphDataValidatorForValues.bind(null, false),
},
},
data() {
return {
width: 0,
height: chartHeight,
svgs: {},
};
},
computed: {
chartData() {
2020-01-01 13:55:28 +05:30
const queryData = this.graphData.metrics.reduce((acc, query) => {
const series = makeDataSeries(query.result || [], {
2019-09-30 21:07:59 +05:30
name: this.formatLegendLabel(query),
});
return acc.concat(series);
}, []);
return {
values: queryData[0].data,
};
},
2020-04-08 14:13:33 +05:30
chartOptions() {
const yAxis = {
...getYAxisOptions(this.graphData.yAxis),
scale: false,
};
return {
grid: getChartGrid(),
yAxis,
dataZoom: this.dataZoomConfig,
};
},
2019-09-30 21:07:59 +05:30
xAxisTitle() {
2020-01-01 13:55:28 +05:30
return this.graphData.metrics[0].result[0].x_label !== undefined
? this.graphData.metrics[0].result[0].x_label
2019-09-30 21:07:59 +05:30
: '';
},
yAxisTitle() {
2020-04-08 14:13:33 +05:30
return this.chartOptions.yAxis.name;
2019-09-30 21:07:59 +05:30
},
xAxisType() {
return this.graphData.x_type !== undefined ? this.graphData.x_type : 'category';
},
dataZoomConfig() {
const handleIcon = this.svgs['scroll-handle'];
return handleIcon ? { handleIcon } : {};
},
},
created() {
this.setSvg('scroll-handle');
},
methods: {
formatLegendLabel(query) {
return `${query.label}`;
},
onResize() {
2020-03-13 15:44:24 +05:30
if (!this.$refs.columnChart) return;
2019-09-30 21:07:59 +05:30
const { width } = this.$refs.columnChart.$el.getBoundingClientRect();
this.width = width;
},
setSvg(name) {
getSvgIconPathContent(name)
.then(path => {
if (path) {
this.$set(this.svgs, name, `path://${path}`);
}
})
.catch(() => {});
},
},
};
</script>
<template>
2020-04-08 14:13:33 +05:30
<div v-gl-resize-observer-directive="onResize">
2019-09-30 21:07:59 +05:30
<gl-column-chart
ref="columnChart"
v-bind="$attrs"
:data="chartData"
:option="chartOptions"
:width="width"
:height="height"
:x-axis-title="xAxisTitle"
:y-axis-title="yAxisTitle"
:x-axis-type="xAxisType"
/>
</div>
</template>