Commit 4b556b3f authored by Miguel Rincon's avatar Miguel Rincon

Add date format for columns

Refactor the X axis to keep the options in a same place and add
a dates formatter for the column chart
parent 18596bcd
...@@ -5,7 +5,8 @@ import { getSvgIconPathContent } from '~/lib/utils/icon_utils'; ...@@ -5,7 +5,8 @@ import { getSvgIconPathContent } from '~/lib/utils/icon_utils';
import { chartHeight } from '../../constants'; import { chartHeight } from '../../constants';
import { makeDataSeries } from '~/helpers/monitor_helper'; import { makeDataSeries } from '~/helpers/monitor_helper';
import { graphDataValidatorForValues } from '../../utils'; import { graphDataValidatorForValues } from '../../utils';
import { getYAxisOptions, getChartGrid } from './options'; import { getTimeAxisOptions, getYAxisOptions, getChartGrid } from './options';
import { timezones } from '../../format_date';
export default { export default {
components: { components: {
...@@ -20,6 +21,11 @@ export default { ...@@ -20,6 +21,11 @@ export default {
required: true, required: true,
validator: graphDataValidatorForValues.bind(null, false), validator: graphDataValidatorForValues.bind(null, false),
}, },
timezone: {
type: String,
required: false,
default: timezones.LOCAL,
},
}, },
data() { data() {
return { return {
...@@ -43,6 +49,8 @@ export default { ...@@ -43,6 +49,8 @@ export default {
}; };
}, },
chartOptions() { chartOptions() {
const xAxis = getTimeAxisOptions({ timezone: this.timezone });
const yAxis = { const yAxis = {
...getYAxisOptions(this.graphData.yAxis), ...getYAxisOptions(this.graphData.yAxis),
scale: false, scale: false,
...@@ -50,8 +58,9 @@ export default { ...@@ -50,8 +58,9 @@ export default {
return { return {
grid: getChartGrid(), grid: getChartGrid(),
xAxis,
yAxis, yAxis,
dataZoom: this.dataZoomConfig, dataZoom: [this.dataZoomConfig],
}; };
}, },
xAxisTitle() { xAxisTitle() {
......
import { SUPPORTED_FORMATS, getFormatter } from '~/lib/utils/unit_format'; import { SUPPORTED_FORMATS, getFormatter } from '~/lib/utils/unit_format';
import { s__ } from '~/locale'; import { __, s__ } from '~/locale';
import { formatDate, timezones, formats } from '../../format_date';
const yAxisBoundaryGap = [0.1, 0.1]; const yAxisBoundaryGap = [0.1, 0.1];
/** /**
...@@ -58,6 +59,17 @@ export const getYAxisOptions = ({ ...@@ -58,6 +59,17 @@ export const getYAxisOptions = ({
}; };
}; };
export const getTimeAxisOptions = ({ timezone = timezones.LOCAL } = {}) => ({
name: __('Time'),
type: 'time',
axisLabel: {
formatter: date => formatDate(date, { format: formats.shortTime, timezone }),
},
axisPointer: {
snap: true,
},
});
// Chart grid // Chart grid
/** /**
......
...@@ -2,15 +2,15 @@ ...@@ -2,15 +2,15 @@
import { omit, throttle } from 'lodash'; import { omit, throttle } from 'lodash';
import { GlLink, GlDeprecatedButton, GlTooltip, GlResizeObserverDirective } from '@gitlab/ui'; import { GlLink, GlDeprecatedButton, GlTooltip, GlResizeObserverDirective } from '@gitlab/ui';
import { GlAreaChart, GlLineChart, GlChartSeriesLabel } from '@gitlab/ui/dist/charts'; import { GlAreaChart, GlLineChart, GlChartSeriesLabel } from '@gitlab/ui/dist/charts';
import { s__, __ } from '~/locale'; import { s__ } from '~/locale';
import { getSvgIconPathContent } from '~/lib/utils/icon_utils'; import { getSvgIconPathContent } from '~/lib/utils/icon_utils';
import Icon from '~/vue_shared/components/icon.vue'; import Icon from '~/vue_shared/components/icon.vue';
import { panelTypes, chartHeight, lineTypes, lineWidths } from '../../constants'; import { panelTypes, chartHeight, lineTypes, lineWidths } from '../../constants';
import { getYAxisOptions, getChartGrid, getTooltipFormatter } from './options'; import { getYAxisOptions, getTimeAxisOptions, getChartGrid, getTooltipFormatter } from './options';
import { annotationsYAxis, generateAnnotationsSeries } from './annotations'; import { annotationsYAxis, generateAnnotationsSeries } from './annotations';
import { makeDataSeries } from '~/helpers/monitor_helper'; import { makeDataSeries } from '~/helpers/monitor_helper';
import { graphDataValidatorForValues } from '../../utils'; import { graphDataValidatorForValues } from '../../utils';
import { formatDate, timezones, formats } from '../../format_date'; import { formatDate, timezones } from '../../format_date';
export const timestampToISODate = timestamp => new Date(timestamp).toISOString(); export const timestampToISODate = timestamp => new Date(timestamp).toISOString();
...@@ -160,24 +160,16 @@ export default { ...@@ -160,24 +160,16 @@ export default {
const { yAxis, xAxis } = this.option; const { yAxis, xAxis } = this.option;
const option = omit(this.option, ['series', 'yAxis', 'xAxis']); const option = omit(this.option, ['series', 'yAxis', 'xAxis']);
const timeXAxis = {
...getTimeAxisOptions({ timezone: this.timezone }),
...xAxis,
};
const dataYAxis = { const dataYAxis = {
...getYAxisOptions(this.graphData.yAxis), ...getYAxisOptions(this.graphData.yAxis),
...yAxis, ...yAxis,
}; };
const timeXAxis = {
name: __('Time'),
type: 'time',
axisLabel: {
formatter: date =>
formatDate(date, { format: formats.shortTime, timezone: this.timezone }),
},
axisPointer: {
snap: true,
},
...xAxis,
};
return { return {
series: this.chartOptionSeries, series: this.chartOptionSeries,
xAxis: timeXAxis, xAxis: timeXAxis,
......
---
title: Format metrics column chart x axis dates
merge_request: 33681
author:
type: changed
import { shallowMount } from '@vue/test-utils'; import { shallowMount } from '@vue/test-utils';
import timezoneMock from 'timezone-mock';
import { GlColumnChart } from '@gitlab/ui/dist/charts'; import { GlColumnChart } from '@gitlab/ui/dist/charts';
import ColumnChart from '~/monitoring/components/charts/column.vue'; import ColumnChart from '~/monitoring/components/charts/column.vue';
...@@ -18,10 +19,7 @@ const dataValues = [ ...@@ -18,10 +19,7 @@ const dataValues = [
describe('Column component', () => { describe('Column component', () => {
let wrapper; let wrapper;
const findChart = () => wrapper.find(GlColumnChart); const createWrapper = (props = {}) => {
const chartProps = prop => findChart().props(prop);
beforeEach(() => {
wrapper = shallowMount(ColumnChart, { wrapper = shallowMount(ColumnChart, {
propsData: { propsData: {
graphData: { graphData: {
...@@ -41,14 +39,60 @@ describe('Column component', () => { ...@@ -41,14 +39,60 @@ describe('Column component', () => {
}, },
], ],
}, },
...props,
}, },
}); });
};
const findChart = () => wrapper.find(GlColumnChart);
const chartProps = prop => findChart().props(prop);
beforeEach(() => {
createWrapper();
}); });
afterEach(() => { afterEach(() => {
wrapper.destroy(); wrapper.destroy();
}); });
describe('xAxisLabel', () => {
const mockDate = Date.UTC(2020, 4, 26, 20); // 8:00 PM in GMT
const useXAxisFormatter = date => {
const { xAxis } = chartProps('option');
const { formatter } = xAxis.axisLabel;
return formatter(date);
};
it('x-axis is formatted correctly in AM/PM format', () => {
expect(useXAxisFormatter(mockDate)).toEqual('8:00 PM');
});
describe('when in PT timezone', () => {
beforeAll(() => {
timezoneMock.register('US/Pacific');
});
afterAll(() => {
timezoneMock.unregister();
});
it('by default, values are formatted in PT', () => {
createWrapper();
expect(useXAxisFormatter(mockDate)).toEqual('1:00 PM');
});
it('when the chart uses local timezone, y-axis is formatted in PT', () => {
createWrapper({ timezone: 'LOCAL' });
expect(useXAxisFormatter(mockDate)).toEqual('1:00 PM');
});
it('when the chart uses UTC, y-axis is formatted in UTC', () => {
createWrapper({ timezone: 'UTC' });
expect(useXAxisFormatter(mockDate)).toEqual('8:00 PM');
});
});
});
describe('wrapped components', () => { describe('wrapped components', () => {
describe('GitLab UI column chart', () => { describe('GitLab UI column chart', () => {
it('is a Vue instance', () => { it('is a Vue instance', () => {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment