debian-mirror-gitlab/app/assets/javascripts/pages/projects/pipelines/charts/index.js

84 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2018-03-27 19:54:05 +05:30
import Chart from 'chart.js';
2019-05-18 00:54:41 +05:30
import { barChartOptions, lineChartOptions } from '~/lib/utils/chart_utils';
const SUCCESS_LINE_COLOR = '#1aaa55';
const TOTAL_LINE_COLOR = '#707070';
2018-03-27 19:54:05 +05:30
2019-05-18 00:54:41 +05:30
const buildChart = (chartScope, shouldAdjustFontSize) => {
2018-03-27 19:54:05 +05:30
const data = {
labels: chartScope.labels,
2018-12-13 13:39:08 +05:30
datasets: [
{
2019-05-18 00:54:41 +05:30
backgroundColor: SUCCESS_LINE_COLOR,
borderColor: SUCCESS_LINE_COLOR,
pointBackgroundColor: SUCCESS_LINE_COLOR,
pointBorderColor: '#fff',
data: chartScope.successValues,
fill: 'origin',
2018-12-13 13:39:08 +05:30
},
{
2019-05-18 00:54:41 +05:30
backgroundColor: TOTAL_LINE_COLOR,
borderColor: TOTAL_LINE_COLOR,
pointBackgroundColor: TOTAL_LINE_COLOR,
pointBorderColor: '#EEE',
data: chartScope.totalValues,
fill: '-1',
2018-12-13 13:39:08 +05:30
},
2018-03-27 19:54:05 +05:30
],
};
2018-12-13 13:39:08 +05:30
const ctx = $(`#${chartScope.scope}Chart`)
.get(0)
.getContext('2d');
2018-03-27 19:54:05 +05:30
2019-05-18 00:54:41 +05:30
return new Chart(ctx, {
type: 'line',
data,
options: lineChartOptions({
width: ctx.canvas.width,
numberOfPoints: chartScope.totalValues.length,
shouldAdjustFontSize,
}),
});
2018-03-27 19:54:05 +05:30
};
2019-05-18 00:54:41 +05:30
const buildBarChart = (chartTimesData, shouldAdjustFontSize) => {
2018-03-27 19:54:05 +05:30
const data = {
labels: chartTimesData.labels,
2018-12-13 13:39:08 +05:30
datasets: [
{
2019-05-18 00:54:41 +05:30
backgroundColor: 'rgba(220,220,220,0.5)',
borderColor: 'rgba(220,220,220,1)',
borderWidth: 1,
2018-12-13 13:39:08 +05:30
barValueSpacing: 1,
barDatasetSpacing: 1,
data: chartTimesData.values,
},
],
2018-03-27 19:54:05 +05:30
};
2019-05-18 00:54:41 +05:30
return new Chart(
2018-12-13 13:39:08 +05:30
$('#build_timesChart')
.get(0)
.getContext('2d'),
2019-05-18 00:54:41 +05:30
{
type: 'bar',
data,
options: barChartOptions(shouldAdjustFontSize),
},
);
};
document.addEventListener('DOMContentLoaded', () => {
const chartTimesData = JSON.parse(document.getElementById('pipelinesTimesChartsData').innerHTML);
const chartsData = JSON.parse(document.getElementById('pipelinesChartsData').innerHTML);
// Scale fonts if window width lower than 768px (iPad portrait)
const shouldAdjustFontSize = window.innerWidth < 768;
buildBarChart(chartTimesData, shouldAdjustFontSize);
2018-03-27 19:54:05 +05:30
2019-05-18 00:54:41 +05:30
chartsData.forEach(scope => buildChart(scope, shouldAdjustFontSize));
2018-03-27 19:54:05 +05:30
});