Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Charts #646

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 111 additions & 1 deletion app/scripts/controllers/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,31 @@ const crate_console = angular.module('console', ['sql', 'datatypechecks', 'stats

return service;
})
.directive('console', function(SQLQuery, ColumnTypeCheck, ConsoleFormatting, ClusterState){
.service('ChartService', function () {
var service = {
colors: ["#e6194B", "#3cb44b", "#911eb4", "#4363d8", "#f58231", "#f032e6"]
};
var state = {
lastColorIndex: -1
};

service.getNextColor = function () {
if (state.lastColorIndex === service.colors.length - 1) { state.lastColorIndex = -1; }
state.lastColorIndex++;
return service.colors[state.lastColorIndex];
};
service.formatValue = function (value, type) {
switch (type) {
case 'timestamp':
return new Date(value).toISOString();
default:
return value
}
};

return service;
})
.directive('console', function(SQLQuery, ColumnTypeCheck, ConsoleFormatting, ClusterState, ChartService){
return {
restrict: 'A',
controller: ['$scope', '$translate', '$location', 'Clipboard', '$timeout' , function($scope, $translate, $location, Clipboard, $timeout){
Expand Down Expand Up @@ -74,6 +98,58 @@ const crate_console = angular.module('console', ['sql', 'datatypechecks', 'stats
message: ''
};

$scope.chartVisible = false;
$scope.chartOptions = {
chart: {
type: 'lineChart',
height: 350,
margin: {
'top': 20,
'right': 20,
'bottom': 30,
'left': 40
},
x: function(d) { if (d) { return d.x; } },
y: function(d) { if (d) { return d.y; } },
useInteractiveGuideline: true,
transitionDuration: 0,
showXAxis: false,
yAxis: {
ticks: 8,
axisLabelDistance: 0,
showMaxMin: false
},
showLegend: true,
interactiveLayer:{
tooltip:{
contentGenerator: function(e){
if (e == null) { return ''; }

var data = $scope.formatted_rows[e.index];
if (data == null) { return ''; }

var rows = '';
$scope.resultHeaders.forEach(function (column, index) {
var series = e.series.find(function (series) { return series.key === column; });
rows += '<tr>' +
'<td class="legend-color-guide">' + (series != null ? ('<div style="background-color:' + series.color + '"></div>') : '') + '</td>' +
'<td class="key">' + column + '</td>' +
'<td class="value">' +
($scope.formatResults ? ChartService.formatValue(data[index].value, $scope.resultHeaderDataTypes[index]) : data[index].value) +
'</td>' +
'</tr>';
});

return '<table>' +
'<tbody>' + rows + '</tbody>' +
'</table>';
}
}
}
}
};
$scope.chartData = []

$scope.pageSize = 50;
$scope.startIndex = 0;
$scope.page = 1;
Expand Down Expand Up @@ -209,6 +285,9 @@ const crate_console = angular.module('console', ['sql', 'datatypechecks', 'stats
$scope.toggleOptions = function toggleOptions(){
$scope.showOptions = !$scope.showOptions;
};
$scope.toggleChart = function toggleChart(){
$scope.chartVisible = !$scope.chartVisible;
};

$scope.clearLocalStorage = function() {
$translate(['CONSOLE.CLEAR_HISTORY_MSG', 'CONSOLE.CLEAR_HISTORY_MSG_PLURAL']).then(function(i18n) {
Expand Down Expand Up @@ -298,6 +377,30 @@ const crate_console = angular.module('console', ['sql', 'datatypechecks', 'stats
$scope.paginated_formatted_rows = $scope.formatted_rows.slice($scope.startIndex, $scope.endIndex);
}

function buildChart(){
$scope.chartData.length = 0;

$scope.resultHeaders.forEach(function(column, i) {
var type = $scope.resultHeaderDataTypes[i]

if (type === 'number') {
var seriesData = {
key: column,
color: ChartService.getNextColor(),
values: $scope.formatted_rows.map(function(row, j){
return {
x: j + 1,
y: row[i].value
};
}),
disabled: false
};

$scope.chartData.push(seriesData);
}
})
}

self.execute = function(sql) {
$scope.startIndex = 0;
$scope.page = 1;
Expand Down Expand Up @@ -339,12 +442,18 @@ const crate_console = angular.module('console', ['sql', 'datatypechecks', 'stats
$scope.resultHeaderTypes.push(response.col_types[i]);
}

$scope.resultHeaderDataTypes = [];
for (i = 0; i < response.cols.length; i++) {
$scope.resultHeaderDataTypes.push(getDataType(response.cols[i], response.col_types[i]));
}

$scope.rows = response.rows;
$scope.limitToAmount = 0;
$scope.status = response.status();
$scope.statement = stmt + ';';
inputScope.updateInput($scope.statement);
formatData();
buildChart();
$scope.paginated_rows = $scope.rows.slice($scope.startIndex, $scope.endIndex);
$scope.numberOfPages = Math.ceil($scope.rows.length / $scope.pageSize);
//refresh cluster state
Expand All @@ -353,6 +462,7 @@ const crate_console = angular.module('console', ['sql', 'datatypechecks', 'stats
$scope.loading = false;
$scope.error.hide = false;
$scope.renderTable = false;
$scope.chartVisible = false;
$scope.error = response.error;

if (!$scope.showErrorTrace && !displayedErrorTraceHint) {
Expand Down
7 changes: 6 additions & 1 deletion app/styles/views/_console.scss
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@

}
.query-result-container__header--next,
.query-result-container__header--previous {
.query-result-container__header--previous,
.query-result-container__header--chart {
max-width: 70px !important;
}
.query-result-container__header--page {
Expand Down Expand Up @@ -383,3 +384,7 @@ tr > .cr-table-cell {
#share-btn {
max-width: 70px !important;
}

.cr-chart {
margin-bottom: 15px;
}
3 changes: 2 additions & 1 deletion app/views/console.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ <h4> {{:: 'CONSOLE.ERROR_TRACE' | translate }} </h4>
<div class="indeterminate"></div>
</div>


<div class="cr-panel-block__chart cr-chart" ng-if="chartVisible"><nvd3 id="chart" config="{refreshDataOnly: true, deepWatchData: true}" options="chartOptions" data="chartData"></nvd3></div>

<div ng-if="renderTable" class="query-result-container" ng-class="{colourised: formatResults}">

<div class="query-result-container__header">

<div class="query-result-container__header--title">{{:: 'CONSOLE.RESULT_FROM_QUERY' | translate }}</div>
<div class="cr-console-header__options__item--btn cr-button cr-button--console query-result-container__header--chart" ng-click="toggleChart()"><i class="fa fa-bar-chart" aria-hidden="true"></i></div>
<div id="previous" class="cr-console-header__options__item--btn cr-button cr-button--console query-result-container__header--previous" ng-click="previous()" ng-class="page === 1 ? 'cr-button--disabled':'' "><i class="fa fa-angle-left" aria-hidden="true"></i></div>
<div class="query-result-container__header--page" ng-click="previous()">{{ page }} / {{ numberOfPages }}</div>
<div id="next" class="cr-console-header__options__item--btn cr-button cr-button--console query-result-container__header--next" ng-class="page === numberOfPages ? 'cr-button--disabled':'' " ng-click="next()"><i class="fa fa-angle-right" aria-hidden="true"></i></div>
Expand Down