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

118 lines
2.8 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';
2021-03-11 19:13:27 +05:30
import { makeDataSeries } from '~/helpers/monitor_helper';
2019-09-30 21:07:59 +05:30
import { getSvgIconPathContent } from '~/lib/utils/icon_utils';
import { chartHeight } from '../../constants';
2021-03-11 19:13:27 +05:30
import { timezones } from '../../format_date';
2019-09-30 21:07:59 +05:30
import { graphDataValidatorForValues } from '../../utils';
2020-06-23 00:09:42 +05:30
import { getTimeAxisOptions, 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),
},
2020-06-23 00:09:42 +05:30
timezone: {
type: String,
required: false,
default: timezones.LOCAL,
},
2019-09-30 21:07:59 +05:30
},
data() {
return {
width: 0,
height: chartHeight,
svgs: {},
};
},
computed: {
2021-01-29 00:20:46 +05:30
barChartData() {
return this.graphData.metrics.reduce((acc, query) => {
2020-01-01 13:55:28 +05:30
const series = makeDataSeries(query.result || [], {
2019-09-30 21:07:59 +05:30
name: this.formatLegendLabel(query),
});
return acc.concat(series);
}, []);
},
2020-04-08 14:13:33 +05:30
chartOptions() {
2020-06-23 00:09:42 +05:30
const xAxis = getTimeAxisOptions({ timezone: this.timezone });
2020-04-08 14:13:33 +05:30
const yAxis = {
...getYAxisOptions(this.graphData.yAxis),
scale: false,
};
return {
grid: getChartGrid(),
2020-06-23 00:09:42 +05:30
xAxis,
2020-04-08 14:13:33 +05:30
yAxis,
2020-06-23 00:09:42 +05:30
dataZoom: [this.dataZoomConfig],
2020-04-08 14:13:33 +05:30
};
},
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) {
2020-04-22 19:07:51 +05:30
return query.label;
2019-09-30 21:07:59 +05:30
},
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)
2021-03-08 18:12:59 +05:30
.then((path) => {
2019-09-30 21:07:59 +05:30
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"
2021-01-29 00:20:46 +05:30
:bars="barChartData"
2019-09-30 21:07:59 +05:30
:option="chartOptions"
:width="width"
:height="height"
:x-axis-title="xAxisTitle"
:y-axis-title="yAxisTitle"
:x-axis-type="xAxisType"
/>
</div>
</template>