Commit 55a2c04d authored by PkSM3's avatar PkSM3

Merge branch 'unstable' of ssh://delanoe.org:1979/gargantext into samuel

parents f751059f b4f15d03
...@@ -120,7 +120,7 @@ def do_cooc(corpus=None ...@@ -120,7 +120,7 @@ def do_cooc(corpus=None
if start is not None: if start is not None:
#date_start = datetime.datetime.strptime ("2001-2-3 10:11:12", "%Y-%m-%d %H:%M:%S") #date_start = datetime.datetime.strptime ("2001-2-3 10:11:12", "%Y-%m-%d %H:%M:%S")
# TODO : more complexe date format here. # TODO : more complexe date format here.
date_start = datetime.datetime.strptime (str(start), "%Y") date_start = datetime.datetime.strptime (str(start), "%Y-%m-%d")
date_start_utc = date_start.strftime("%Y-%m-%d %H:%M:%S") date_start_utc = date_start.strftime("%Y-%m-%d %H:%M:%S")
Start=aliased(NodeHyperdata) Start=aliased(NodeHyperdata)
...@@ -134,7 +134,7 @@ def do_cooc(corpus=None ...@@ -134,7 +134,7 @@ def do_cooc(corpus=None
if end is not None: if end is not None:
# TODO : more complexe date format here. # TODO : more complexe date format here.
date_end = datetime.datetime.strptime (str(end), "%Y") date_end = datetime.datetime.strptime (str(end), "%Y-%m-%d")
date_end_utc = date_end.strftime("%Y-%m-%d %H:%M:%S") date_end_utc = date_end.strftime("%Y-%m-%d %H:%M:%S")
End=aliased(NodeHyperdata) End=aliased(NodeHyperdata)
......
from django.http import HttpResponse, Http404
from django.core.exceptions import PermissionDenied, SuspiciousOperation
from django.core.exceptions import ValidationError
from django.core.urlresolvers import reverse
from sqlalchemy import text, distinct, or_,not_
from sqlalchemy.sql import func
from sqlalchemy.orm import aliased
import datetime
import copy
import collections
from gargantext_web.views import move_to_trash
from gargantext_web.db import *
from gargantext_web.validation import validate, ValidationException
from node import models
def DebugHttpResponse(data):
return HttpResponse('<html><body style="background:#000;color:#FFF"><pre>%s</pre></body></html>' % (str(data), ))
import json
class JSONEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime.datetime):
return obj.isoformat()[:19] + 'Z'
else:
return super(self.__class__, self).default(obj)
json_encoder = JSONEncoder(indent=4)
def JsonHttpResponse(data, status=200):
return HttpResponse(
content = json_encoder.encode(data),
content_type = 'application/json; charset=utf-8',
status = status
)
Http400 = SuspiciousOperation
Http403 = PermissionDenied
import csv
def CsvHttpResponse(data, headers=None, status=200):
response = HttpResponse(
content_type = "text/csv",
status = status
)
writer = csv.writer(response, delimiter=',')
if headers:
writer.writerow(headers)
for row in data:
writer.writerow(row)
return response
from rest_framework.authentication import SessionAuthentication, BasicAuthentication
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.exceptions import APIException as _APIException
class APIException(_APIException):
def __init__(self, message, code=500):
self.status_code = code
self.detail = message
class NodeNgramsQueries(APIView):
_resolutions = {
'second': lambda d: d + datetime.timedelta(seconds=1),
'minute': lambda d: d + datetime.timedelta(minutes=1),
'hour': lambda d: d + datetime.timedelta(hours=1),
'day': lambda d: d + datetime.timedelta(days=1),
'week': lambda d: d + datetime.timedelta(days=7),
'month': lambda d: (d + datetime.timedelta(days=32)).replace(day=1),
'year': lambda d: (d + datetime.timedelta(days=367)).replace(day=1, month=1),
'decade': lambda d: (d + datetime.timedelta(days=3660)).replace(day=1, month=1),
'century': lambda d: (d + datetime.timedelta(days=36600)).replace(day=1, month=1),
}
def post(self, request, project_id):
# example only
input = request.data or {
'x': {
'with_empty': True,
'resolution': 'decade',
'value': 'publication_date',
},
'y': {
# 'divided_by': 'total_ngrams_count',
# 'divided_by': 'total_documents_count',
},
'filter': {
# 'ngrams': ['bees', 'bee', 'honeybee', 'honeybees', 'honey bee', 'honey bees'],
# 'ngrams': ['insecticide', 'pesticide'],
# 'corpora': [52633],
# 'date': {'min': '1995-12-31'}
},
# 'format': 'csv',
}
# input validation
input = validate(input, {'type': dict, 'default': {}, 'items': {
'x': {'type': dict, 'default': {}, 'items': {
# which hyperdata to choose for the date
'value': {'type': str, 'default': 'publication_date', 'range': {'publication_date', }},
# time resolution
'resolution': {'type': str, 'range': self._resolutions.keys(), 'default': 'month'},
# should we add zeroes for empty values?
'with_empty': {'type': bool, 'default': False},
}},
'y': {'type': dict, 'default': {}, 'items': {
# mesured value
'value': {'type': str, 'default': 'ngrams_count', 'range': {'ngrams_count', 'documents_count', 'ngrams_tfidf'}},
# value by which we should normalize
'divided_by': {'type': str, 'range': {'total_documents_count', 'documents_count', 'total_ngrams_count'}},
}},
# filtering
'filter': {'type': dict, 'default': {}, 'items': {
# filter by date
'date': {'type': dict, 'items': {
'min': {'type': datetime.datetime},
'max': {'type': datetime.datetime},
}, 'default': {}},
# filter by corpora
'corpora' : {'type': list, 'default': [], 'items': {'type': int}},
# filter by ngrams
'ngrams' : {'type': list, 'default': [], 'items': {'type': str}},
}},
# output format
'format': {'type': str, 'default': 'json', 'range': {'json', 'csv'}},
}})
# build query: prepare columns
column_x = func.date_trunc(input['x']['resolution'], Node_Hyperdata.value_datetime)
column_y = {
'documents_count': func.count(Node.id),
'ngrams_count': func.sum(Node_Ngram.weight),
# 'ngrams_tfidf': func.sum(Node_Node_Ngram.weight),
}[input['y']['value']]
# build query: base
query_base = (session
.query(column_x)
.select_from(Node)
.join(Node_Ngram, Node_Ngram.node_id == Node.id)
.join(Node_Hyperdata, Node_Hyperdata.node_id == Node_Ngram.node_id)
.join(Hyperdata, Hyperdata.id == Node_Hyperdata.hyperdata_id)
.filter(Hyperdata.name == input['x']['value'])
.group_by(column_x)
.order_by(column_x)
)
# build query: base, filter by corpora or project
if 'corpora' in input['filter'] and input['filter']['corpora']:
query_base = (query_base
.filter(Node.parent_id.in_(input['filter']['corpora']))
)
else:
ParentNode = aliased(Node)
query_base = (query_base
.join(ParentNode, ParentNode.id == Node.parent_id)
.filter(ParentNode.parent_id == project_id)
)
# build query: base, filter by date
if 'date' in input['filter']:
if 'min' in input['filter']['date']:
query_base = query_base.filter(Node_Hyperdata.value_datetime >= input['filter']['date']['min'])
if 'max' in input['filter']['date']:
query_base = query_base.filter(Node_Hyperdata.value_datetime <= input['filter']['date']['max'])
# build query: filter by ngrams
query_result = query_base.add_columns(column_y)
if 'ngrams' in input['filter'] and input['filter']['ngrams']:
query_result = (query_result
.join(Ngram, Ngram.id == Node_Ngram.ngram_id)
.filter(Ngram.terms.in_(input['filter']['ngrams']))
)
# build result: prepare data
date_value_list = query_result.all()
if date_value_list:
date_min = date_value_list[0][0].replace(tzinfo=None)
date_max = date_value_list[-1][0].replace(tzinfo=None)
# build result: prepare interval
result = collections.OrderedDict()
if input['x']['with_empty'] and date_value_list:
compute_next_date = self._resolutions[input['x']['resolution']]
date = date_min
while date <= date_max:
result[date] = 0.0
date = compute_next_date(date)
# build result: integrate
for date, value in date_value_list:
result[date.replace(tzinfo=None)] = value
# build result: normalize
query_normalize = None
if date_value_list and 'divided_by' in input['y'] and input['y']['divided_by']:
if input['y']['divided_by'] == 'total_documents_count':
query_normalize = query_base.add_column(func.count(Node.id))
elif input['y']['divided_by'] == 'total_ngrams_count':
query_normalize = query_base.add_column(func.sum(Node_Ngram.weight))
if query_normalize is not None:
for date, value in query_normalize:
date = date.replace(tzinfo=None)
if date in result:
result[date] /= value
# return result with proper formatting
if input['format'] == 'json':
return JsonHttpResponse({
'query': input,
'result': sorted(result.items()),
}, 201)
elif input['format'] == 'csv':
return CsvHttpResponse(sorted(result.items()), ('date', 'value'), 201)
...@@ -240,7 +240,13 @@ def get_or_create_node(nodetype=None,corpus=None,corpus_id=None,name_str=None,hy ...@@ -240,7 +240,13 @@ def get_or_create_node(nodetype=None,corpus=None,corpus_id=None,name_str=None,hy
if nodetype is None: if nodetype is None:
print("Need to give a type node") print("Need to give a type node")
else: else:
ntype=cache.NodeType[nodetype] try:
ntype = cache.NodeType[nodetype]
except KeyError:
ntype = cache.NodeType[nodetype] = NodeType()
ntype.name = nodetype
session.add(ntype)
session.commit()
if corpus_id is not None and corpus is None: if corpus_id is not None and corpus is None:
corpus = session.query(Node).filter(Node.id==corpus_id).first() corpus = session.query(Node).filter(Node.id==corpus_id).first()
......
...@@ -9,6 +9,7 @@ import gargantext_web.corpus_views as corpus_views ...@@ -9,6 +9,7 @@ import gargantext_web.corpus_views as corpus_views
from annotations import urls as annotations_urls from annotations import urls as annotations_urls
from annotations.views import main as annotations_main_view from annotations.views import main as annotations_main_view
import gargantext_web.api2
import tests.ngramstable.views as samtest import tests.ngramstable.views as samtest
...@@ -75,6 +76,7 @@ urlpatterns = patterns('', ...@@ -75,6 +76,7 @@ urlpatterns = patterns('',
url(r'^corpus/(\d+)/node_link.json$', views.node_link), # => api.analysis('type': 'node_link', 'format' : 'json') url(r'^corpus/(\d+)/node_link.json$', views.node_link), # => api.analysis('type': 'node_link', 'format' : 'json')
url(r'^corpus/(\d+)/adjacency.json$', views.adjacency), # => api.analysis('type': 'adjacency', 'format' : 'json') url(r'^corpus/(\d+)/adjacency.json$', views.adjacency), # => api.analysis('type': 'adjacency', 'format' : 'json')
url(r'^api2/nodes/(\d+)/histories$', gargantext_web.api2.NodeNgramsQueries.as_view()),
url(r'^ngrams$', views.ngrams), # to be removed url(r'^ngrams$', views.ngrams), # to be removed
url(r'^nodeinfo/(\d+)$', views.nodeinfo), # to be removed ? url(r'^nodeinfo/(\d+)$', views.nodeinfo), # to be removed ?
url(r'^tfidf/(\d+)/(\w+)$', views_optimized.tfidf), url(r'^tfidf/(\d+)/(\w+)$', views_optimized.tfidf),
......
...@@ -40,7 +40,7 @@ case "$1" in ...@@ -40,7 +40,7 @@ case "$1" in
;; ;;
status) status)
status_of_proc "$DAEMON_NAME" "$DAEMON" && exit 0 || exit $? ps -e | grep "`cat $DAEMON_PID` "
;; ;;
*) *)
echo "Usage: $DAEMON_NAME {start|stop|restart|status}" echo "Usage: $DAEMON_NAME {start|stop|restart|status}"
......
...@@ -103,15 +103,13 @@ class NodesChildrenNgrams(APIView): ...@@ -103,15 +103,13 @@ class NodesChildrenNgrams(APIView):
# query ngrams # query ngrams
ParentNode = aliased(Node) ParentNode = aliased(Node)
ngrams_query = (session ngrams_query = (session
.query(Ngram.terms, func.count().label('count')) .query(Ngram.terms, func.sum(Node_Ngram.weight).label('count'))
# .query(Ngram.id, Ngram.terms, func.count().label('count'))
.join(Node_Ngram, Node_Ngram.ngram_id == Ngram.id) .join(Node_Ngram, Node_Ngram.ngram_id == Ngram.id)
.join(Node, Node.id == Node_Ngram.node_id) .join(Node, Node.id == Node_Ngram.node_id)
.filter(Node.parent_id == node_id) .filter(Node.parent_id == node_id)
.group_by(Ngram.terms) .group_by(Ngram.terms)
# .group_by(Ngram) # .group_by(Ngram)
.order_by(func.count().desc(), Ngram.terms) .order_by(func.sum(Node_Ngram.weight).desc(), Ngram.terms)
# .order_by(func.count().desc(), Ngram.id)
) )
# filters # filters
if 'startwith' in request.GET: if 'startwith' in request.GET:
...@@ -349,8 +347,8 @@ class NodesChildrenDuplicates(APIView): ...@@ -349,8 +347,8 @@ class NodesChildrenDuplicates(APIView):
'deleted': count 'deleted': count
}) })
class NodesChildrenMetatadata(APIView): # retrieve metadata from a given list of parent node
def get(self, request, node_id): def get_metadata(corpus_id_list):
# query hyperdata keys # query hyperdata keys
ParentNode = aliased(Node) ParentNode = aliased(Node)
...@@ -358,7 +356,7 @@ class NodesChildrenMetatadata(APIView): ...@@ -358,7 +356,7 @@ class NodesChildrenMetatadata(APIView):
.query(Hyperdata) .query(Hyperdata)
.join(Node_Hyperdata, Node_Hyperdata.hyperdata_id == Hyperdata.id) .join(Node_Hyperdata, Node_Hyperdata.hyperdata_id == Hyperdata.id)
.join(Node, Node.id == Node_Hyperdata.node_id) .join(Node, Node.id == Node_Hyperdata.node_id)
.filter(Node.parent_id == node_id) .filter(Node.parent_id.in_(corpus_id_list))
.group_by(Hyperdata) .group_by(Hyperdata)
) )
...@@ -377,7 +375,7 @@ class NodesChildrenMetatadata(APIView): ...@@ -377,7 +375,7 @@ class NodesChildrenMetatadata(APIView):
node_hyperdata_query = (session node_hyperdata_query = (session
.query(value_column) .query(value_column)
.join(Node, Node.id == Node_Hyperdata.node_id) .join(Node, Node.id == Node_Hyperdata.node_id)
.filter(Node.parent_id == node_id) .filter(Node.parent_id.in_(corpus_id_list))
.filter(Node_Hyperdata.hyperdata_id == hyperdata.id) .filter(Node_Hyperdata.hyperdata_id == hyperdata.id)
.group_by(value_column) .group_by(value_column)
.order_by(value_column) .order_by(value_column)
...@@ -403,8 +401,68 @@ class NodesChildrenMetatadata(APIView): ...@@ -403,8 +401,68 @@ class NodesChildrenMetatadata(APIView):
'valuesCount': values_count, 'valuesCount': values_count,
}) })
# give the result back
return collection
class ApiHyperdata(APIView):
def get(self, request):
corpus_id_list = list(map(int, request.GET['corpus_id'].split(',')))
return JsonHttpResponse({ return JsonHttpResponse({
'data': collection, 'data': get_metadata(corpus_id_list),
})
# retrieve ngrams from a given list of parent node
def get_ngrams(corpus_id_list):
pass
class ApiNgrams(APIView):
def get(self, request):
# parameters retrieval and validation
startwith = request.GET.get('startwith', '').replace("'", "\\'")
# query ngrams
ParentNode = aliased(Node)
ngrams_query = (session
.query(Ngram.terms, func.sum(Node_Ngram.weight).label('count'))
.join(Node_Ngram, Node_Ngram.ngram_id == Ngram.id)
.join(Node, Node.id == Node_Ngram.node_id)
.group_by(Ngram.terms)
# .group_by(Ngram)
.order_by(func.sum(Node_Ngram.weight).desc(), Ngram.terms)
)
# filters
if 'startwith' in request.GET:
ngrams_query = ngrams_query.filter(Ngram.terms.startswith(request.GET['startwith']))
if 'contain' in request.GET:
ngrams_query = ngrams_query.filter(Ngram.terms.contains(request.GET['contain']))
if 'corpus_id' in request.GET:
corpus_id_list = list(map(int, request.GET.get('corpus_id', '').split(',')))
if corpus_id_list and corpus_id_list[0]:
ngrams_query = ngrams_query.filter(Node.parent_id.in_(corpus_id_list))
# pagination
offset = int(request.GET.get('offset', 0))
limit = int(request.GET.get('limit', 20))
total = ngrams_query.count()
# return formatted result
return JsonHttpResponse({
'pagination': {
'offset': offset,
'limit': limit,
'total': total,
},
'data': [
{
'terms': ngram.terms,
'count': ngram.count,
}
for ngram in ngrams_query[offset : offset+limit]
],
}) })
class NodesChildrenQueries(APIView): class NodesChildrenQueries(APIView):
......
...@@ -22,8 +22,8 @@ urlpatterns = patterns('', ...@@ -22,8 +22,8 @@ urlpatterns = patterns('',
url(r'node/(\d+)/ngrams$', ngrams.Ngrams.as_view()), url(r'node/(\d+)/ngrams$', ngrams.Ngrams.as_view()),
url(r'node/(\d+)/ngrams$', ngrams.Ngrams.as_view()), url(r'node/(\d+)/ngrams$', ngrams.Ngrams.as_view()),
url(r'nodes/(\d+)/children/hyperdata$', api.NodesChildrenMetatadata.as_view()), #url(r'nodes/(\d+)/children/hyperdata$', api.NodesChildrenMetatadata.as_view()),
url(r'nodes/(\d+)/children/hyperdata$', api.NodesChildrenMetatadata.as_view()), #url(r'nodes/(\d+)/children/hyperdata$', api.NodesChildrenMetatadata.as_view()),
url(r'nodes/(\d+)/children/queries$', api.NodesChildrenQueries.as_view()), url(r'nodes/(\d+)/children/queries$', api.NodesChildrenQueries.as_view()),
url(r'nodes/(\d+)/children/queries$', api.NodesChildrenQueries.as_view()), url(r'nodes/(\d+)/children/queries$', api.NodesChildrenQueries.as_view()),
...@@ -37,6 +37,9 @@ urlpatterns = patterns('', ...@@ -37,6 +37,9 @@ urlpatterns = patterns('',
url(r'nodes/(\d+)/graph$', graph.Graph.as_view()), url(r'nodes/(\d+)/graph$', graph.Graph.as_view()),
url(r'corpus/(\d+)/graph$', graph.Graph.as_view()), url(r'corpus/(\d+)/graph$', graph.Graph.as_view()),
url(r'hyperdata$', api.ApiHyperdata.as_view()),
url(r'ngrams$', api.ApiNgrams.as_view()),
url(r'tfidf/(\d+)/(\w+)$', views_optimized.tfidf), url(r'tfidf/(\d+)/(\w+)$', views_optimized.tfidf),
) )
This source diff could not be displayed because it is too large. You can view the blob instead.
// Pre-defined constants
//
// Documentations: // Documentations:
// n3-charts/line-chart // n3-charts/line-chart
// define operators (for hyperdata filtering, according to the considered type)
var operators = { var operators = {
'text': [ 'text': [
{'label': 'contains', 'key': 'contains'}, {'label': 'contains', 'key': 'contains'},
...@@ -33,6 +34,13 @@ var operators = { ...@@ -33,6 +34,13 @@ var operators = {
{'label': 'is after', 'key': '>'} {'label': 'is after', 'key': '>'}
], ],
}; };
$.each(operators, function(type, type_operators) {
type_operators.unshift({});
});
// define available periods of time
var periods = ['second', 'minute', 'hour', 'day', 'week', 'month', 'year', 'decade', 'century'];
var strDate = function(date) { var strDate = function(date) {
return date.getFullYear() + '-' + return date.getFullYear() + '-' +
...@@ -159,154 +167,64 @@ angular.module('Gargantext').run(function($rootScope, $http, $cookies){ ...@@ -159,154 +167,64 @@ angular.module('Gargantext').run(function($rootScope, $http, $cookies){
} }
return output; return output;
}; };
// Pre-defined stuff
$rootScope.operators = operators;
$rootScope.periods = periods;
// For CSRF token compatibility with Django // For CSRF token compatibility with Django
$http.defaults.headers.post['X-CSRFToken'] = $cookies['csrftoken']; $http.defaults.headers.post['X-CSRFToken'] = $cookies['csrftoken'];
}); });
// Controller for queries // Controller for datasets
gargantext.controller("QueryController", function($scope, $http) { gargantext.controller('DatasetController', function($scope, $http) {
// query-specific information
$scope.filters = []; // are we loading data from the server right now?
$scope.pagination = {offset:0, limit: 20}; $scope.is_loading = false;
// results information
$scope.loading = false; // initital parameters for the y-axis of the query
$scope.results = []; $scope.query_y = {
$scope.resultsCount = undefined; 'value': 'ngrams_count',
// corpus retrieval 'is_relative': false,
$scope.corpora = []; 'divided_by': 'total_ngrams_count',
$.get('/api/nodes?type=Corpus').success(function(response){
$scope.corpora = response.data;
$scope.$apply();
});
// filtering informations retrieval
$scope.operators = operators;
// add a filter
$scope.addFilter = function() {
$scope.filters.push({});
};
// remove a filter
$scope.removeFilter = function(filterIndex) {
$scope.filters.splice(filterIndex, 1);
};
// perform a query
$scope.postQuery = function() {
if ($scope.corpusId) {
// change view to loading mode
$scope.loading = true;
// query parameters: columns
var retrieve = {type: 'fields', list: ['id', 'name', 'hyperdata.publication_date']};
// query parameters: pagination
var pagination = $scope.pagination;
// query parameters: sort
var sort = ['name'];
// query parameters: filters
var filters = [];
var keys = ['entity', 'column', 'operator', 'value'];
for (var i=0, m=$scope.filters.length; i<m; i++) {
var filter = $scope.filters[i];
for (var j=0, n=keys.length; j<n; j++) {
if (!filter[keys[j]]) {
continue;
}
}
filters.push({
field: filter.entity + '.' + filter.column,
operator: filter.operator,
value: filter.value
});
}
// query URL & body building
var url = '/api/nodes/' + $scope.corpusId + '/children/queries';
var query = {
retrieve: retrieve,
filters: filters,
sort: sort,
pagination: pagination
};
// send query to the server
$http.post(url, query).success(function(response){
$scope.resultsCount = response.pagination.total;
$scope.results = response.results;
$scope.columns = response.retrieve;
$scope.loading = false;
}).error(function(response){
console.error(response);
});
}
}
// change current page
$scope.decrement = function() {
if ($scope.pagination.offset > 0) {
$scope.pagination.offset--;
}
$scope.postQuery();
};
$scope.increment = function() {
if ($scope.pagination.offset < $scope.resultsCount) {
$scope.pagination.offset += $scope.pagination.limit;
}
$scope.postQuery();
}; };
});
// Controller for datasets // filters: corpora retrieval
gargantext.controller("DatasetController", function($scope, $http) {
// query-specific information
$scope.mesured = 'nodes.count';
$scope.filters = [];
$scope.pagination = {offset:0, limit: 20};
// results information
$scope.loading = false;
$scope.results = [];
$scope.resultsCount = undefined;
// corpus retrieval
$scope.projects = [];
$scope.corpora = []; $scope.corpora = [];
$http.get('/api/nodes?type=Project', {cache: true}).success(function(response){
$scope.projects = response.data;
// Initially set to what is indicated in the URL
if (/^\/project\/\d+\/corpus\/\d+/.test(location.pathname)) { if (/^\/project\/\d+\/corpus\/\d+/.test(location.pathname)) {
$scope.projectId = parseInt(location.pathname.split('/')[2]); $scope.project_id = parseInt(location.pathname.split('/')[2]);
$scope.updateCorpora(); } else {
console.error('The id of the project has to be set.');
} }
});
// update corpora according to the select parent project
$scope.updateCorpora = function() { $scope.updateCorpora = function() {
$http.get('/api/nodes?type=Corpus&parent=' + $scope.projectId, {cache: true}).success(function(response){ $http.get('/api/nodes?type=Corpus&parent=' + $scope.project_id, {cache: true}).success(function(response){
$scope.corpora = response.data; $scope.corpora = response.data;
// Initially set to what is indicated in the URL // Initially set to what is indicated in the URL
if (/^\/project\/\d+\/corpus\/\d+/.test(location.pathname)) { if (/^\/project\/\d+\/corpus\/\d+/.test(location.pathname)) {
$scope.corpusId = parseInt(location.pathname.split('/')[4]); var corpus_id = parseInt(location.pathname.split('/')[4]);
$scope.updateEntities(); $.each($scope.corpora, function(c, corpus) {
corpus.is_selected = (corpus.id == corpus_id);
});
$scope.updateHyperdataList();
$scope.updateDataset();
} }
}); });
}; };
// update entities depending on the selected corpus var getSelectedCorporaIdList = function() {
$scope.updateEntities = function() { var corpus_id_list = [];
var url = '/api/nodes/' + $scope.corpusId + '/children/hyperdata'; $.each($scope.corpora, function(c, corpus) {
$scope.entities = undefined; if (corpus.is_selected) {
$scope.filters = []; corpus_id_list.push(corpus.id);
$http.get(url, {cache: true}).success(function(response){
$scope.entities = [
{
key: 'hyperdata',
columns: response.data
},
{
key: 'ngrams',
columns: [
{key:'terms', type:'string'},
{key:'terms count', type:'integer'}
],
} }
];
}); });
$scope.updateQuery(); return corpus_id_list;
}; }
// query ngrams $scope.updateCorpora();
// filters: ngrams
$scope.getNgrams = function(query) { $scope.getNgrams = function(query) {
var url = '/api/nodes/' + $scope.corpusId + '/children/ngrams?limit=10&contain=' + encodeURI(query); var url = '/api/ngrams?limit=10';
url += '&contain=' + encodeURI(query);
url += '&corpus_id=' + getSelectedCorporaIdList().join(',');
var appendTransform = function(defaults, transform) { var appendTransform = function(defaults, transform) {
defaults = angular.isArray(defaults) ? defaults : [defaults]; defaults = angular.isArray(defaults) ? defaults : [defaults];
return defaults.concat(transform); return defaults.concat(transform);
...@@ -318,68 +236,95 @@ gargantext.controller("DatasetController", function($scope, $http) { ...@@ -318,68 +236,95 @@ gargantext.controller("DatasetController", function($scope, $http) {
}) })
}); });
}; };
// filtering informations retrieval
$scope.operators = operators; // filters: corpora
// add a filter $scope.corporaSelectNone = function() {
$scope.addFilter = function() { $.each($scope.corpora, function(c, corpus){
$scope.filters.push({}); corpus.is_selected = false;
}; });
// remove a filter $scope.updateDataset();
$scope.removeFilter = function(filterIndex) {
$scope.filters.splice(filterIndex, 1);
$scope.updateQuery();
}; };
// transmit query parameters to parent elements $scope.corporaSelectAll = function() {
$scope.updateQuery = function() { $.each($scope.corpora, function(c, corpus){
if ($scope.corpusId) { corpus.is_selected = true;
// query parameters: sort
var url = '/api/nodes/' + $scope.corpusId + '/children/queries';
// filters
var filters = [];
var keys = ['entity', 'column', 'operator', 'value'];
for (var i=0, m=$scope.filters.length; i<m; i++) {
var filter = $scope.filters[i];
for (var j=0, n=keys.length; j<n; j++) {
if (!filter[keys[j]]) {
continue;
}
}
if (filter.entity.key == 'ngrams') {
var termsList = [];
angular.forEach(filter.value, function(ngram) {
termsList.push(ngram.terms);
}); });
if (termsList.length) { $scope.updateDataset();
filters.push({ };
field: 'ngrams.terms',
operator: 'in', // filters: metadata, according to the considered corpora
value: termsList $scope.hyperdataList = [];
$scope.updateHyperdataList = function() {
var corpus_id_list = getSelectedCorporaIdList();
if (corpus_id_list && corpus_id_list.length) {
var url = '/api/hyperdata?corpus_id=';
url += corpus_id_list.join(',');
$scope.is_loading = true;
$http.get(url, {cache: true}).success(function(response){
$scope.is_loading = false;
$scope.hyperdataList = response.data;
}); });
}
} else { } else {
filters.push({ $scope.hyperdataList = [];
field: filter.entity.key + '.' + filter.column.key,
operator: filter.operator,
value: filter.value
});
}
} }
// event firing to parent(s) };
$scope.$emit('updateDataset', {
datasetIndex: $scope.$index, // update the dataset, according to the various filters applied to it
url: url, $scope.updateDataset = function() {
filters: filters, // parameters
mesured: $scope.mesured var parameters = {
'x': {
'with_empty': true,
'resolution': $scope.query_x.resolution,
'value': 'publication_date',
},
'y': {
'value': $scope.query_y.value,
},
'filter': {
},
'format': 'json',
};
// x: normalization
if ($scope.query_y.is_relative) {
parameters.y.divided_by = $scope.query_y.divided_by;
}
// filter: ngrams
if ($scope.query_y.ngrams && $scope.query_y.ngrams.length) {
parameters.filter.ngrams = [];
$.each($scope.query_y.ngrams, function(n, ngram) {
parameters.filter.ngrams.push(ngram.terms)
})
console.log($scope.query_y.ngrams);
}
// retrieve data
var url = '/api2/nodes/' + $scope.project_id + '/histories';
$scope.is_loading = true;
$http.post(url, parameters, {cache: true}).success(function(response){
$scope.is_loading = false;
// event firing to parent
$scope.$emit('updateDatasets', {
response: response,
dataset_index: $scope.$index,
}); });
} });
} };
$scope.$on('updateDataset', function(e, data) {
$scope.updateDataset();
});
}); });
// Controller for graphs // Controller for graphs
gargantext.controller("GraphController", function($scope, $http, $element) { gargantext.controller('GraphController', function($scope, $http, $element) {
// initial values
$scope.query_x = {
'resolution': 'year'
};
// initialization // initialization
$scope.datasets = [{}]; $scope.datasets = [{}];
$scope.groupingKey = 'year';
$scope.options = { $scope.options = {
stacking: false stacking: false
}; };
...@@ -398,125 +343,70 @@ gargantext.controller("GraphController", function($scope, $http, $element) { ...@@ -398,125 +343,70 @@ gargantext.controller("GraphController", function($scope, $http, $element) {
}, },
tension: 1.0, tension: 1.0,
lineMode: 'linear', lineMode: 'linear',
tooltip: {mode: 'scrubber', formatter: function(x, y, series) { // tooltip: {mode: 'scrubber', formatter: function(x, y, series) {
var grouping = groupings.datetime[$scope.groupingKey]; // var grouping = groupings.datetime[$scope.groupingKey];
return grouping.representation(x) + ' → ' + y; // return grouping.representation(x) + ' → ' + y;
}}, // }},
drawLegend: true, drawLegend: true,
drawDots: true, drawDots: true,
columnsHGap: 5 columnsHGap: 5
} }
}; };
// add a dataset // add a dataset
$scope.addDataset = function() { $scope.addDataset = function() {
$scope.datasets.push({}); $scope.datasets.push({});
}; };
// remove a dataset // remove a dataset
$scope.removeDataset = function(datasetIndex) { $scope.removeDataset = function(datasetIndex) {
if ($scope.datasets.length > 1) {
$scope.datasets.shift(datasetIndex); $scope.datasets.shift(datasetIndex);
$scope.query(); $scope.updateDatasets();
}; } else {
// show results on the graph alert('You can not remove the last dataset.')
$scope.showResults = function() {
// Format specifications
var grouping = groupings.datetime[$scope.groupingKey];
var convert = function(x) {return new Date(x);};
// Find extrema for X
var xMin, xMax;
angular.forEach($scope.datasets, function(dataset){
if (!dataset.results) {
return false;
}
var results = dataset.results;
if (results.length) {
var xMinTmp = results[0][0];
var xMaxTmp = results[results.length - 1][0];
if (xMin === undefined || xMinTmp < xMin) {
xMin = xMinTmp;
}
if (xMax === undefined || xMaxTmp < xMax) {
xMax = xMaxTmp;
}
}
});
// Create the dataObject for interpolation
var dataObject = {};
if (xMin != undefined && xMax != undefined) {
xMin = grouping.truncate(xMin);
xMax = grouping.truncate(xMax);
for (var x=xMin; x<=xMax; x=grouping.next(x)) {
var row = [];
angular.forEach($scope.datasets, function(){
row.push(0);
});
dataObject[x] = row;
}
}
// Fill the dataObject with results
angular.forEach($scope.datasets, function(dataset, datasetIndex){
var results = dataset.results;
angular.forEach(results, function(result, r){
var x = grouping.truncate(result[0]);
var y = parseFloat(result[1]);
if (dataObject[x] === undefined) {
var row = [];
angular.forEach($scope.datasets, function(){
row.push(0);
});
dataObject[x] = row;
} }
dataObject[x][datasetIndex] += y; };
// update the datasets (catches the event thrown by children dataset controllers)
$scope.updateDatasets = function(must_refresh) {
// refresh all data
if (must_refresh) {
$scope.$broadcast('updateDataset');
}
// create temporary representation for the result
var values = {}
var n = dataset_results.length;
for (var i=0; i<n; i++) {
var result = dataset_results[i];
var key = 'y' + i;
for (var j=0, m=result.length; j<m; j++) {
var date = result[j][0];
var value = result[j][1];
if (!values[date]) {
values[date] = {};
}
values[date][key] = value;
}
}
// put that in an array
var data = [];
$.each(values, function(date, keys_values) {
var row = {x: new Date(date)};
for (var i=0; i<n; i++) {
var key = 'y' + i;
row[key] = keys_values[key] || 0;
}
data.push(row);
}); });
// sort the array
data.sort(function(a, b) {
return (new Date(a.x)).getTime() - (new Date(b.x)).getTime();
}); });
// show time!
$scope.graph.data = data;
// calculate average for earch dataset // update series names
/*
var sums = [];
for (var i=0; i<$scope.datasets.length;i++){
sums.push(0);
}
var count = 0 ;
for (var x in dataObject) {
count ++ ;
var yList = dataObject[x];
for (var i=0; i<yList.length; i++) {
sums[i] += yList[i];
}
}
for (var i=0; i<$scope.datasets.length;i++){
sums[i] /= count;
}
*/
// Convert this object back to a sorted array
var yMin, yMax;
var linearData = [];
for (var x in dataObject) {
var row = {x: convert(x)};
var yList = dataObject[x];
for (var i=0; i<yList.length; i++) {
y = yList[i];
row['y' + i] = y ; // position vs average
//row['y' + i] = y - sums[i]; // position vs average
if (yMax == undefined || y > yMax) {
yMax = y;
}
if (yMin == undefined || y < yMin) {
yMin = y;
}
}
linearData.push(row);
}
// // Update the axis
// $scope.graph.options.axes.y.min = yMin;
// $scope.graph.options.axes.y.max = yMax;
// $scope.graph.options.axes.y.ticks = Math.pow(10, Math.floor(Math.abs(Math.log10(yMax - yMin))));
// Finally, update the graph
var series = []; var series = [];
for (var i=0, n=$scope.datasets.length; i<n; i++) { for (var i=0; i<n; i++) {
var seriesElement = { var seriesElement = {
id: 'series_'+ i, id: 'series_'+ i,
y: 'y'+ i, y: 'y'+ i,
...@@ -530,103 +420,20 @@ gargantext.controller("GraphController", function($scope, $http, $element) { ...@@ -530,103 +420,20 @@ gargantext.controller("GraphController", function($scope, $http, $element) {
series.push(seriesElement); series.push(seriesElement);
} }
$scope.graph.options.series = series; $scope.graph.options.series = series;
$scope.graph.data = linearData;
// shall we stack?
if ($scope.options.stacking) {
var stack = {
axis: 'y',
series: []
};
angular.forEach(series, function(seriesElement) {
stack.series.push(seriesElement.id);
});
$scope.graph.options.stacks = [stack];
} else {
delete $scope.graph.options.stacks;
}
}; };
// perform a query on the server var dataset_results = [];
$scope.query = function() { $scope.$on('updateDatasets', function(e, data) {
// number of requests made to the server // data extraction
var requestsCount = 0; var dataset_index = data.dataset_index;
// reinitialize graph data var result = data.response.result;
$scope.graph.data = []; // update full results array
// queue all the server requests while (dataset_results.length < $scope.datasets.length) {
angular.forEach($scope.datasets, function(dataset, datasetIndex) { dataset_results.push([]);
// if the results are already present, don't send a query }
if (dataset.results !== undefined) { while (dataset_results.length > $scope.datasets.length) {
return; dataset_results.splice(-1, 1);
} }
// format data to be sent as a query dataset_results[dataset_index] = result;
var query = dataset.query; $scope.updateDatasets();
var data = {
filters: query.filters,
sort: ['hyperdata.publication_date.day'],
retrieve: {
aggregate: true,
fields: ['hyperdata.publication_date.day', query.mesured]
}
};
// request to the server
$http.post(query.url, data, {cache: true}).success(function(response) {
dataset.results = response.results;
for (var i=0, n=$scope.datasets.length; i<n; i++) {
if ($scope.datasets[i].results == undefined) {
return;
}
}
$scope.showResults();
}).error(function(response) {
console.error('An error occurred while retrieving the query response');
});
requestsCount++;
});
// if no request have been made at all, refresh the chart
if (requestsCount == 0) {
$scope.showResults();
}
};
// update the datasets (catches the vent thrown by children dataset controllers)
$scope.$on('updateDataset', function(e, data) {
var dataset = $scope.datasets[data.datasetIndex]
dataset.query = {
url: data.url,
filters: data.filters,
mesured: data.mesured
};
dataset.results = undefined;
$scope.query();
}); });
}); });
// Only for debugging!
/*
setTimeout(function(){
// first dataset
$('div.corpus select').change();
$('button.add').first().click();
setTimeout(function(){
$('div.corpus select').change();
// $('div.filters button').last().click();
// var d = $('li.dataset').last();
// d.find('select').last().val('hyperdata').change();
// d.find('select').last().val('publication_date').change();
// d.find('select').last().val('>').change();
// d.find('input').last().val('2010').change();
// // second dataset
// // $('button.add').first().click();
// // var d = $('li.dataset').last();
// // d.find('select').change();
// // // second dataset's filter
// // d.find('div.filters button').last().click();
// // d.find('select').last().val('hyperdata').change();
// // d.find('select').last().val('abstract').change();
// // d.find('select').last().val('contains').change();
// // d.find('input').last().val('dea').change();
// // refresh
// // $('button.refresh').first().click();
}, 500);
}, 250);
*/
d3.sankey = function() {
var sankey = {},
nodeWidth = 24,
nodePadding = 8,
size = [1, 1],
nodes = [],
links = [];
sankey.nodeWidth = function(_) {
if (!arguments.length) return nodeWidth;
nodeWidth = +_;
return sankey;
};
sankey.nodePadding = function(_) {
if (!arguments.length) return nodePadding;
nodePadding = +_;
return sankey;
};
sankey.nodes = function(_) {
if (!arguments.length) return nodes;
nodes = _;
return sankey;
};
sankey.links = function(_) {
if (!arguments.length) return links;
links = _;
return sankey;
};
sankey.size = function(_) {
if (!arguments.length) return size;
size = _;
return sankey;
};
sankey.layout = function(iterations) {
computeNodeLinks();
computeNodeValues();
computeNodeBreadths();
computeNodeDepths(iterations);
computeLinkDepths();
return sankey;
};
sankey.relayout = function() {
computeLinkDepths();
return sankey;
};
sankey.link = function() {
var curvature = .5;
function link(d) {
var x0 = d.source.x + d.source.dx,
x1 = d.target.x,
xi = d3.interpolateNumber(x0, x1),
x2 = xi(curvature),
x3 = xi(1 - curvature),
y0 = d.source.y + d.sy + d.dy / 2,
y1 = d.target.y + d.ty + d.dy / 2;
return "M" + x0 + "," + y0
+ "C" + x2 + "," + y0
+ " " + x3 + "," + y1
+ " " + x1 + "," + y1;
}
link.curvature = function(_) {
if (!arguments.length) return curvature;
curvature = +_;
return link;
};
return link;
};
// Populate the sourceLinks and targetLinks for each node.
// Also, if the source and target are not objects, assume they are indices.
function computeNodeLinks() {
nodes.forEach(function(node) {
node.sourceLinks = [];
node.targetLinks = [];
});
links.forEach(function(link) {
var source = link.source,
target = link.target;
if (typeof source === "number") source = link.source = nodes[link.source];
if (typeof target === "number") target = link.target = nodes[link.target];
source.sourceLinks.push(link);
target.targetLinks.push(link);
});
}
// Compute the value (size) of each node by summing the associated links.
function computeNodeValues() {
nodes.forEach(function(node) {
node.value = Math.max(
d3.sum(node.sourceLinks, value),
d3.sum(node.targetLinks, value)
);
});
}
// Iteratively assign the breadth (x-position) for each node.
// Nodes are assigned the maximum breadth of incoming neighbors plus one;
// nodes with no incoming links are assigned breadth zero, while
// nodes with no outgoing links are assigned the maximum breadth.
function computeNodeBreadths() {
var remainingNodes = nodes,
nextNodes,
x = 0;
while (remainingNodes.length) {
nextNodes = [];
remainingNodes.forEach(function(node) {
node.x = x;
node.dx = nodeWidth;
node.sourceLinks.forEach(function(link) {
nextNodes.push(link.target);
});
});
remainingNodes = nextNodes;
++x;
}
//
moveSinksRight(x);
scaleNodeBreadths((width - nodeWidth) / (x - 1));
}
function moveSourcesRight() {
nodes.forEach(function(node) {
if (!node.targetLinks.length) {
node.x = d3.min(node.sourceLinks, function(d) { return d.target.x; }) - 1;
}
});
}
function moveSinksRight(x) {
nodes.forEach(function(node) {
if (!node.sourceLinks.length) {
node.x = x - 1;
}
});
}
function scaleNodeBreadths(kx) {
nodes.forEach(function(node) {
node.x *= kx;
});
}
function computeNodeDepths(iterations) {
var nodesByBreadth = d3.nest()
.key(function(d) { return d.x; })
.sortKeys(d3.ascending)
.entries(nodes)
.map(function(d) { return d.values; });
//
initializeNodeDepth();
resolveCollisions();
for (var alpha = 1; iterations > 0; --iterations) {
relaxRightToLeft(alpha *= .99);
resolveCollisions();
relaxLeftToRight(alpha);
resolveCollisions();
}
function initializeNodeDepth() {
var ky = d3.min(nodesByBreadth, function(nodes) {
return (size[1] - (nodes.length - 1) * nodePadding) / d3.sum(nodes, value);
});
nodesByBreadth.forEach(function(nodes) {
nodes.forEach(function(node, i) {
node.y = i;
node.dy = node.value * ky;
});
});
links.forEach(function(link) {
link.dy = link.value * ky;
});
}
function relaxLeftToRight(alpha) {
nodesByBreadth.forEach(function(nodes, breadth) {
nodes.forEach(function(node) {
if (node.targetLinks.length) {
var y = d3.sum(node.targetLinks, weightedSource) / d3.sum(node.targetLinks, value);
node.y += (y - center(node)) * alpha;
}
});
});
function weightedSource(link) {
return center(link.source) * link.value;
}
}
function relaxRightToLeft(alpha) {
nodesByBreadth.slice().reverse().forEach(function(nodes) {
nodes.forEach(function(node) {
if (node.sourceLinks.length) {
var y = d3.sum(node.sourceLinks, weightedTarget) / d3.sum(node.sourceLinks, value);
node.y += (y - center(node)) * alpha;
}
});
});
function weightedTarget(link) {
return center(link.target) * link.value;
}
}
function resolveCollisions() {
nodesByBreadth.forEach(function(nodes) {
var node,
dy,
y0 = 0,
n = nodes.length,
i;
// Push any overlapping nodes down.
nodes.sort(ascendingDepth);
for (i = 0; i < n; ++i) {
node = nodes[i];
dy = y0 - node.y;
if (dy > 0) node.y += dy;
y0 = node.y + node.dy + nodePadding;
}
// If the bottommost node goes outside the bounds, push it back up.
dy = y0 - nodePadding - size[1];
if (dy > 0) {
y0 = node.y -= dy;
// Push any overlapping nodes back up.
for (i = n - 2; i >= 0; --i) {
node = nodes[i];
dy = node.y + node.dy + nodePadding - y0;
if (dy > 0) node.y -= dy;
y0 = node.y;
}
}
});
}
function ascendingDepth(a, b) {
return a.y - b.y;
}
}
function computeLinkDepths() {
nodes.forEach(function(node) {
node.sourceLinks.sort(ascendingTargetDepth);
node.targetLinks.sort(ascendingSourceDepth);
});
nodes.forEach(function(node) {
var sy = 0, ty = 0;
node.sourceLinks.forEach(function(link) {
link.sy = sy;
sy += link.dy;
});
node.targetLinks.forEach(function(link) {
link.ty = ty;
ty += link.dy;
});
});
function ascendingSourceDepth(a, b) {
return a.source.y - b.source.y;
}
function ascendingTargetDepth(a, b) {
return a.target.y - b.target.y;
}
}
function center(node) {
return node.y + node.dy / 2;
}
function value(link) {
return link.value;
}
return sankey;
};
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
<div class="container theme-showcase" role="main"> <div class="container theme-showcase" role="main">
<div class="jumbotron"> <div class="jumbotron">
<h1>Advanced chart</h1> <h1>Advanced chart</h1>
<p>Custom, test, interpret</p> <p>Customize, test, interpret</p>
</div> </div>
</div> </div>
...@@ -24,319 +24,128 @@ ...@@ -24,319 +24,128 @@
<!-- All the templates used by the Javascript framework --> <!-- All the templates used by the Javascript framework -->
{% verbatim %} {% verbatim %}
<!--
<script type="text/template" id="filter-template">
<span>...where</span>
<select name="entity">
<option value="hyperdata">hyperdata</option>
<option value="ngrams">ngrams</option>
</select>
<span class="entity hyperdata">
<select name="key"><% _.each(hyperdataList, function(hyperdata){ %>
<option><%= hyperdata.name %></option>
<% }); %></select>
</span>
<span class="entity ngrams">
<select name="key">
<option value="terms">terms</option>
<option value="n">terms count</option>
</select>
</span>
<select name="operator"></select>
<input name="value" />
<button class="remove">X</button>
</script>
<script type="text/template" id="filterlist-template">
<ul class="filters"></ul>
<button class="add">Add a filter...</button>
</script>
<script type="text/template" id="nodesquery-template">
<div ng-include="'filterlist-template'"></div>
</script>
-->
<!--
<script type="text/ejs" id="FilterView">
<li>
<span>...where the</span>
<select name="entity">
<option>hyperdata</option>
<option>ngrams</option>
</select>
<span class="entity hyperdata">
<select name="key"></select>
</span>
<span class="entity ngrams">
<select name="key">
<option value="terms">terms</option>
<option value="n">terms count</option>
</select>
</span>
</li>
</script>
<script type="text/ejs" id="FilterListView">
<div class="filters">
<ul class="filters"></ul>
<button class="create">Add a filter</button>
</div>
</script>
-->
{% endverbatim %} {% endverbatim %}
<script type="text/javascript" src="{% static "js/jquery/jquery.min.js" %}"></script> <script type="text/javascript" src="{% static "js/jquery/jquery.min.js" %}"></script>
{% verbatim %} {% verbatim %}
<!--
<div ng-app="Gargantext" ng-controller="FilterListController">
<ul>
<li ng-repeat="(filterIndex, filter) in filters">
<span>...where the </span>
<select ng-model="filter.entity">
<option ng-repeat="(entityName, entityColumns) in entities" value="{{entityName}}">{{entityName}}</option>
</select>
<span ng-if="filter.entity">
<select ng-model="filter.column">
<option ng-repeat="column in entities[filter.entity] | orderBy:'key'" value="{{column.key}}">{{column.key}}</option>
</select>
<span ng-if="filter.column">
<span ng-repeat="column in entities[filter.entity] | filter : {'key':filter.column} : strict">
<select ng-model="filter.operator">
<option ng-repeat="operator in operators[column.type]" value="{{operator.key}}">{{operator.label}}</option>
</select>
<input type="text" ng-model="filter.value">
</span>
</span>
</span>
<button ng-click="removeFilter(filterIndex)" title="remove this filter">X</button>
</li>
</ul>
<button ng-click="addFilter()">Add a filter...</button>
</div>
-->
<style type="text/css"> <style type="text/css">
div.corpus button:first-child+select {color:#FFF;} div.controller div.autocomplete {margin: 0; padding: 0; }
div.list-results table {border-collapse: collapse;} div.controller div.autocomplete * {background: transparent; }
div.list-results th, div.list-results td {border: solid 1px #888; padding: 0.5em;} div.controller div.autocomplete ul.suggestion-list {background: rgba(255,255,255,.75); }
div.list-results th {background: #444; color: #FFF} div.controller .tags li.tag-item {background: transparent; border: solid 1px rgba(0,0,0,.1); box-shadow: inset .05em .1em .4em rgba(0,0,0,.4); border-radius: 0; font-weight: bold; }
div.list-results tr:nth-child(even) td {background: #FFF; color: #111} div.controller div.autocomplete li.suggestion-item:hover, div.controller div.autocomplete li.suggestion-item.selected {background: rgba(0,0,0,.5)}
div.list-results tr:nth-child(odd) td {background: #EEE; color: #000} div.controller div.autocomplete li.suggestion-item em {background: transparent; }
div.controller div.tags>input {padding: 0; border: 0; outline: 0; box-shadow: none; background: transparent;}
div.controller div.tags>input::-webkit-input-placeholder,div.controller div.tags input::-webkit-input-placeholder {color: rgba(0,0,0,.25); }
div.controller, div.controller * {color: rgba(0,0,0,.75); }
div.controller button {background: none; border: solid 1px rgba(0,0,0,.25); box-shadow: .05em .1em .4em rgba(0,0,0,.5); opacity: .5; }
div.controller button:hover {opacity: .65; }
div.controller button:active {opacity: .8; }
div.controller>button {width: 100%; }
div.controller input[type=checkbox] {position: relative; top: .125em; }
div.controller input[type=text], div.controller select, div.controller div.tags {font-weight: bold; box-shadow: inset .05em .1em .4em rgba(0,0,0,.2); outline: solid 1px rgba(0,0,0,.125); border: 0; background: rgba(255,255,255,.5); }
div.controller input[type=text] {padding-left: 0.5em;}
ul.datasets {padding: 0; margin: 0; list-style: none; }
li.dataset {padding: 0.3em; border: solid 1px rgba(0,0,0,.125); margin-bottom: 0.5em; box-shadow: inset .1em .2em .8em rgba(0,0,0,.1) }
li.dataset * { }
li.dataset button {float: right; position: relative; top: -.15em; margin-left: .25em; }
li.dataset select {cursor: pointer; border: 0; padding: 0; }
ul.filters {list-style: none; margin: 0; padding: 0; margin-top: .25em;}
ul.filters>li {padding-top: .5em; margin-top: .5em; border-top: solid 1px rgba(0,0,0,.125);}
ul.filters>li>ul {list-style: none; padding-left: 0; }
ul.filters>li>ul label {font-weight: normal; cursor: pointer; }
ul.filters>li input[type=checkbox] {opacity: .8;}
</style> </style>
<!--
TODO: use inclusions
SEE: http://stackoverflow.com/questions/17801988/dynamically-loading-controllers-and-ng-include
-->
<div class="container"> <div class="container">
<!-- <div ng-app="Gargantext" ng-controller="GraphController" class="controller">
<div ng-app="Gargantext" ng-controller="QueryController">
<div class="corpus">
Chosen corpus:
<select ng-model="corpusId" ng-change="updateEntities()">
<option ng-repeat="corpus in corpora" value="{{corpus.id}}">{{corpus.name}}</option>
</select>
</div>
<div class="filters" ng-if="corpusId">
<hr/>
<ul>
<li ng-repeat="(filterIndex, filter) in filters">
<span>...where the </span>
<select ng-model="filter.entity">
<option ng-repeat="(entityName, entityColumns) in entities" value="{{entityName}}">{{entityName}}</option>
</select>
<span ng-if="filter.entity">
<select ng-model="filter.column">
<option ng-repeat="column in entities[filter.entity] | orderBy:'key'" value="{{column.key}}">{{column.key}}</option>
</select>
<span ng-if="filter.column">
<span ng-repeat="column in entities[filter.entity] | filter : {'key':filter.column} : strict">
<select ng-model="filter.operator">
<option ng-repeat="operator in operators[column.type]" value="{{operator.key}}">{{operator.label}}</option>
</select>
<input type="text" ng-model="filter.value">
</span>
</span>
</span>
<button ng-click="removeFilter(filterIndex)" title="remove this filter">X</button>
</li>
</ul>
<button ng-click="addFilter()">Add a filter...</button>
</div>
<div class="results" ng-if="corpusId">
<hr/>
<button ng-if="corpusId" ng-click="postQuery()">Refresh results</button>
<div class="list">
<div class="list-pagination">
<select ng-model="pagination.limit">
<option ng-repeat="limit in [5, 10, 20, 50, 100]" value="{{limit}}">{{limit}}</option>
</select>
results per page
<span ng-if="resultsCount != undefined">
showing page
<select ng-model="pagination.offset">
<option ng-repeat="x in range(0, resultsCount+1, pagination.limit)" value="{{ x }}">{{ Math.round(1+x/pagination.limit) }}</option>
</select>
out of
{{ Math.ceil(resultsCount / pagination.limit) }}
<strong>({{resultsCount}}</strong> results found)
</span>
</div>
<div class="list-wait" ng-if="loading">
<em>Loading, please wait...</em>
</div>
<div class="list-results" ng-if="!loading &amp;&amp; resultsCount != undefined">
<div ng-if="!results.length">
<em>No results were found.</em>
</div>
<table ng-if="results.length">
<thead>
<tr>
<th ng-repeat="(key, value) in results[0]">{{key}}</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="result in results">
<td ng-repeat="(key, value) in result">{{value}}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
-->
<div ng-app="Gargantext" ng-controller="GraphController">
<ul class="datasets"> <ul class="datasets">
<li class="dataset" ng-controller="DatasetController" ng-repeat="dataset in datasets"> <li class="dataset" ng-controller="DatasetController" ng-repeat="dataset in datasets" style="background-color:{{ getColor($index, datasets.length) }}">
<hr/> <!-- main part -->
<div class="corpus"> <div class="main">
<button ng-click="removeDataset($index)" title="Remove this dataset">X</button> <!-- buttons -->
<select ng-model="mesured" style="background-color:{{ getColor($index, datasets.length) }}" ng-options="value as key for (key, value) in {'Documents count': 'nodes.count', 'Documents count (normalized)': 'nodes.countnorm', 'Ngrams count': 'ngrams.count'}" ng-change="updateQuery()"></select> <button ng-click="show_filters = !show_filters">{{ show_filters ? 'Hide' : 'Show' }} filters</button>
in the project <button ng-click="removeDataset($index)">Remove dataset</button>
<select ng-model="projectId" ng-change="updateCorpora()" ng-options="project.id as project.name for project in projects"></select>, <!-- description of Y values -->
corpus Evolution of the
<select ng-model="corpusId" ng-change="updateEntities()" ng-options="corpus.id as corpus.name for corpus in corpora"></select> <select ng-model="query_y.value" ng-options="value as key for (key, value) in {'ngrams count': 'ngrams_count', 'documents count': 'documents_count'}" ng-change="updateDataset()"></select>
</div> <select ng-model="query_y.is_relative" ng-options="value as key for (key, value) in {'in absolute terms': false, 'relative to the': true}" ng-change="updateDataset()"></select>
<div class="filters" ng-if="entities"> <span ng-if="query_y.is_relative">
<select ng-model="query_y.divided_by" ng-options="value as key for (key, value) in {'total ngrams count': 'total_ngrams_count', 'total documents count': 'total_documents_count'}" ng-change="updateDataset()"></select>
</span>
</div>
<!-- filters -->
<ul class="filters" ng-show="show_filters">
<!-- filter corpora -->
<li>
<button ng-click="corporaSelectAll()">select all</button>
<button ng-click="corporaSelectNone()">select none</button>
...restrict to the following corpora:
<ul> <ul>
<li ng-repeat="filter in filters"> <li ng-repeat="corpus in corpora">
<button ng-click="removeFilter($index)" title="Remove this filter">x</button> <label>
<span>...where the </span> <input type="checkbox" ng-model="corpus.is_selected" ng-change="updateHyperdataList();updateDataset()"/>
<select ng-model="filter.entity" ng-options="entity as entity.key for entity in entities"></select> <span style="font-weight: {{ corpus.is_selected ? 'bold' : 'normal' }}">{{ corpus.name }}</span>
<span ng-if="filter.entity.key != 'ngrams'"> </label>
<select ng-if="filter.entity" ng-model="filter.column" ng-options="column as column.key for column in filter.entity.columns | orderBy:'key'"></select> </li>
<select ng-if="filter.column" ng-model="filter.operator" ng-options="operator.key as operator.label for operator in operators[filter.column.type]"></select> </ul>
<input ng-if="filter.operator" type="text" ng-model="filter.value" ng-change="updateQuery()"> </li>
</span> <!-- filter ngrams -->
<span ng-if="filter.entity.key == 'ngrams'"> <li class="ngrams">
are in this list of words or expressions: ...only consider documents containing the following ngrams:
<tags-input ng-model="filter.value" display-property="terms" placeholder="Add an ngram" on-tag-added="updateQuery()" on-tag-removed="updateQuery()" add-from-autocomplete-only="true"> <tags-input ng-model="query_y.ngrams" display-property="terms" placeholder="Add an ngram" on-tag-added="updateDataset()" on-tag-removed="updateDataset()" add-from-autocomplete-only="true">
<auto-complete source="getNgrams($query)"></auto-complete> <auto-complete source="getNgrams($query)"></auto-complete>
</tags-input ng-model="tags"> </tags-input ng-model="tags">
</span> </li>
<!-- filter hyperdata -->
<li>
<ul>
<li ng-repeat="hyperdata in hyperdataList">
...where the value of
<span ng-if="!hyperdata.operator">"{{ hyperdata.key }}"</span>
<strong ng-if="hyperdata.operator">{{ hyperdata.key }}</strong>
<select ng-model="hyperdata.operator" ng-options="operator.key as operator.label for operator in operators[hyperdata.type]"></select>
<input type="text" ng-if="hyperdata.operator" ng-model="hyperdata.value" ng-change="updateDataset()" placeholder="type a value here..." />
</li>
</ul>
</li> </li>
</ul> </ul>
<button ng-click="addFilter()">Add a filter...</button>
</div>
</li> </li>
</ul> </ul>
<hr/>
<button class="add" ng-click="addDataset()">Compare with...</button>
<hr/>
<button style="width:100%" class="refresh" ng-click="query()">Refresh results</button>
<div class="graph">
<linechart data="graph.data" options="graph.options"></linechart>
</div>
<div class="graph-parameters">
X-axis: groups the results by
<select ng-model="groupingKey" ng-options="key for key in ['day', 'month', 'year', 'decade', 'century']" ng-change="query()">
</select>
<br/>
Y-axis: use a <!-- add a new dataset -->
<select ng-model="graph.options.axes.y.type" ng-options="type for type in ['linear', 'log']"></select> <button ng-click="addDataset()">Add a dataset</button>
scale
<br/>
<hr/> <!-- X-axis (time) resolution -->
<p>
(group results by
<select ng-model="query_x.resolution" ng-options="period as period for period in periods" ng-change="updateDatasets(true)"></select>)
</p>
<!-- data representation -->
Represent data with Represent data with
<select ng-model="seriesOptions.type" ng-options="type for type in ['column', 'area', 'line']" ng-change="query()"></select> <select ng-model="seriesOptions.type" ng-options="type for type in ['column', 'area', 'line']" ng-change="updateDatasets()"></select>
<span ng-show="seriesOptions.type == 'area' || seriesOptions.type == 'column'"> <span ng-show="seriesOptions.type == 'area' || seriesOptions.type == 'column'">
(<select ng-model="options.stacking" ng-options="value as key for (key, value) in {'with':true, 'without':false}" ng-change="query()"></select> stacking) (<select ng-model="options.stacking" ng-options="value as key for (key, value) in {'with':true, 'without':false}" ng-change="updateDatasets()"></select> stacking)
</span> </span>
<span ng-show="seriesOptions.type == 'area'"> <div class="graph">
(<select ng-model="seriesOptions.striped" ng-options="value as key for (key, value) in {'with':true, 'without':false}" ng-change="query()"></select> stripes) <linechart data="graph.data" options="graph.options"></linechart>
</span>
<br/>
<span ng-hide="seriesOptions.type == 'column'">
Line thickness:
<input ng-model="seriesOptions.thicknessNumber" type="range" min="1" max="8" ng-change="seriesOptions.thickness = seriesOptions.thicknessNumber + 'px'; query()" />
<br/>
Interpolation:
<select ng-model="graph.options.lineMode">
<option ng-repeat="mode in ['linear', 'bundle']" value="{{ mode }}">{{ mode }}</option>
</select>
<span ng-if="graph.options.lineMode != 'linear'">
with a tension of
<input type="text" disabled="disabled" ng-model="graph.options.tension" />
<input type="range" min="0" max="2" step=".1" ng-model="graph.options.tension" />
</span>
</span>
</div> </div>
</div> </div>
</div> </div>
<!--
<div ng-app="Gargantext" ng-controller="FilterController">
<span>...where the </span>
<select ng-model="entityName">
<option ng-repeat="(entityName, entityColumns) in entities" value="{{entityName}}">{{entityName}}</option>
</select>
<span ng-if="entityName">
<select ng-model="entityColumn.key">
<option ng-repeat="entityColumn in entities[entityName] | orderBy:'key'" value="{{entityColumn.key}}">{{entityColumn.key}}</option>
</select>
<span ng-if="entityColumn.key">
<span ng-repeat="entityColumn in entities[entityName] | filter : entityColumn : strict">
<select ng-model="operator.key">
<option ng-repeat="operator in operators[entityColumn.type]" value="{{operator.key}}">{{operator.label}}</option>
</select>
<input type="text">
</span>
</span>
</span>
</div>
-->
{% endverbatim %} {% endverbatim %}
<script type="text/javascript" src="{% static "js/angular.min.js" %}"></script> <script type="text/javascript" src="{% static "js/angular.min.js" %}"></script>
<script type="text/javascript" src="{% static "js/angular-cookies.min.js" %}"></script> <script type="text/javascript" src="{% static "js/angular-cookies.min.js" %}"></script>
<script type="text/javascript" src="{% static "js/d3/d3.v2.min.js" %}"></script> <!-- <script type="text/javascript" src="{% static "js/d3/d3.v2.min.js" %}"></script> -->
<script type="text/javascript" src="{% static "js/d3/n3.line-chart.min.js" %}"></script> <script type="text/javascript" src="{% static "js/d3/n3.line-chart.min.js" %}"></script>
<script type="text/javascript" src="{% static "js/ng-tags-input.min.js" %}"></script> <script type="text/javascript" src="{% static "js/ng-tags-input.min.js" %}"></script>
...@@ -344,25 +153,6 @@ ...@@ -344,25 +153,6 @@
<script type="text/javascript" src="{% static "js/gargantext.angular.js" %}"></script> <script type="text/javascript" src="{% static "js/gargantext.angular.js" %}"></script>
<!--
<script type="text/javascript" src="{% static "js/underscore-min.js" %}"></script>
<script type="text/javascript" src="{% static "js/backbone.js" %}"></script>
<script type="text/javascript" src="{% static "js/gargantext.backbone.js" %}"></script>
<script type="text/javascript">
var filterList = new FilterListView({nodeId: 39576});
filterList.render().$el.appendTo('#test-container');
</script>
-->
<!--
<script type="text/javascript" src="{% static "js/can.custom.js" %}"></script>
<script type="text/javascript" src="{% static "js/gargantext.can.js" %}"></script>
<script type="text/javascript">
var f = new FilterListController('#test-container', {parent: 39576});
</script>
-->
{% endblock %} {% endblock %}
{% load staticfiles %}
<style>
.node rect {
cursor: move;
fill-opacity: .9;
shape-rendering: crispEdges;
}
.node text {
pointer-events: none;
text-shadow: 0 1px 0 #fff;
}
.link {
fill: none;
stroke: #000;
stroke-opacity: .2;
}
.link:hover {
stroke-opacity: .5;
}
</style>
<body>
<p id="chart">
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="{% static "js/sankey.js" %}"></script>
<script>
var units = "Widgets";
var margin = {top: 10, right: 10, bottom: 10, left: 10},
width = 700 - margin.left - margin.right,
height = 300 - margin.top - margin.bottom;
var formatNumber = d3.format(",.0f"), // zero decimal places
format = function(d) { return formatNumber(d) + " " + units; },
color = d3.scale.category20();
// append the svg canvas to the page
var svg = d3.select("#chart").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
// Set the sankey diagram properties
var sankey = d3.sankey()
.nodeWidth(36)
.nodePadding(40)
.size([width, height]);
var path = sankey.link();
// load the data (using the timelyportfolio csv method)
d3.csv("/corpus/{{corpus.id}}/sankey.csv", function(error, data) {
//set up graph in same style as original example but empty
graph = {"nodes" : [], "links" : []};
data.forEach(function (d) {
graph.nodes.push({ "name": d.source });
graph.nodes.push({ "name": d.target });
graph.links.push({ "source": d.source,
"target": d.target,
"value": +d.value });
});
// return only the distinct / unique nodes
graph.nodes = d3.keys(d3.nest()
.key(function (d) { return d.name; })
.map(graph.nodes));
// loop through each link replacing the text with its index from node
graph.links.forEach(function (d, i) {
graph.links[i].source = graph.nodes.indexOf(graph.links[i].source);
graph.links[i].target = graph.nodes.indexOf(graph.links[i].target);
});
//now loop through each nodes to make nodes an array of objects
// rather than an array of strings
graph.nodes.forEach(function (d, i) {
graph.nodes[i] = { "name": d };
});
sankey
.nodes(graph.nodes)
.links(graph.links)
.layout(32);
// add in the links
var link = svg.append("g").selectAll(".link")
.data(graph.links)
.enter().append("path")
.attr("class", "link")
.attr("d", path)
.style("stroke-width", function(d) { return Math.max(1, d.dy); })
.sort(function(a, b) { return b.dy - a.dy; });
// add the link titles
link.append("title")
.text(function(d) {
return d.source.name + " → " +
d.target.name + "\n" + format(d.value); });
// add in the nodes
var node = svg.append("g").selectAll(".node")
.data(graph.nodes)
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")"; })
.call(d3.behavior.drag()
.origin(function(d) { return d; })
.on("dragstart", function() {
this.parentNode.appendChild(this); })
.on("drag", dragmove));
// add the rectangles for the nodes
node.append("rect")
.attr("height", function(d) { return d.dy; })
.attr("width", sankey.nodeWidth())
.style("fill", function(d) {
return d.color = color(d.name.replace(/ .*/, "")); })
.style("stroke", function(d) {
return d3.rgb(d.color).darker(2); })
.append("title")
.text(function(d) {
return d.name + "\n" + format(d.value); });
// add in the title for the nodes
node.append("text")
.attr("x", -6)
.attr("y", function(d) { return d.dy / 2; })
.attr("dy", ".35em")
.attr("text-anchor", "end")
.attr("transform", null)
.text(function(d) { return d.name; })
.filter(function(d) { return d.x < width / 2; })
.attr("x", 6 + sankey.nodeWidth())
.attr("text-anchor", "start");
// the function for moving the nodes
function dragmove(d) {
d3.select(this).attr("transform",
"translate(" + d.x + "," + (
d.y = Math.max(0, Math.min(height - d.dy, d3.event.y))
) + ")");
sankey.relayout();
link.attr("d", path);
}
});
</script>
</body>
</html>
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