From d9c91f9ec6808409ab73b6c5f5073d46771ceeb8 Mon Sep 17 00:00:00 2001 From: fbormann Date: Wed, 22 Feb 2017 05:00:30 -0300 Subject: [PATCH] modified and added usernames, title and pictures but still has a problem is social name has space on it --- amadeus/static/js/charts/home.js | 133 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- users/urls.py | 2 ++ users/views.py | 14 ++++++++++++-- 3 files changed, 144 insertions(+), 5 deletions(-) diff --git a/amadeus/static/js/charts/home.js b/amadeus/static/js/charts/home.js index da8a424..fa610af 100644 --- a/amadeus/static/js/charts/home.js +++ b/amadeus/static/js/charts/home.js @@ -12,8 +12,8 @@ d3.selection.prototype.last = function() { return d3.select(this[last]); }; -var resource_donut_chart = { - build: function(url){ +var charts = { + build_resource: function(url){ $.get(url, function(dataset){ @@ -81,7 +81,134 @@ var resource_donut_chart = { }) // end of the get method + }, + + build_bubble_user: function(url){ + $.get(url, function(dataset){ + var width = 600; + var height = 480; + + + function min(){ + min = 100000000000; + for(var i = 0; i < dataset.length; i++){ + if (dataset[i]['count'] < min){ + min = dataset[i]['count']; + } + } + + return min + } + + function max(){ + max = 0; + for(var i = 0; i < dataset.length; i++){ + if (dataset[i]['count'] > max){ + max = dataset[i]['count']; + } + } + + return max + } + + + + var color = d3.scaleOrdinal(d3.schemeCategory20); + //adding new div to carousel-inner div + var new_div = d3.select(".carousel-inner").append("div").attr("class","item"); + var radiusScale = d3.scaleSqrt().domain([min(), max()]).range([10,50]); + var svg = new_div.append("svg").attr("width", width).attr("height", height) + .style("margin","auto") + .style("display","block") + .append('g') + .attr("transform", "translate(0,0)") + .attr("width", width) + .attr("height", height); + + //adding svg title + + svg.append("text") + .attr("text-anchor", "middle") + .attr("x", width/2 ) + .attr("y", 30) + .style("font-size", "30px") + .text("Usuários mais ativos no Amadeus"); + + var simulation = d3.forceSimulation() + .force("x", d3.forceX(width/2).strength(0.05)) + .force("y", d3.forceY(height/2).strength(0.05)) + .force("collide", d3.forceCollide(function(d){ + return radiusScale(d['count']); + })); + + //First I create as many groups as datapoints so + //it can hold all other objects (circle, texts, images) + var groups = svg.selectAll('.users-item') + .data(dataset) + .enter() + .append("g") + .attr("class",".user-dot"); + + //Create circles to be drawn + var circles = groups + .append('circle') + .attr("r", function(d){ + return radiusScale(d['count']); + }) + + .attr("fill", function(d){ + //return color(d['count']); + return 'url('+'#'+d['user']+')'; + }); + + + + //Add texts to show user names + groups.append("text") + .text(function(d){ + return d['user']; + }).attr("fill", "#FFFFFF"); + + + var defs = groups.append('svg:defs'); + + defs.append("svg:pattern") + .attr("id", function(d){ + return d['user']; + }) + .attr("width", function(d){ + return radiusScale(d['count']); + }) + .attr("height", function(d){ + return radiusScale(d['count']); + }) + .append("svg:image") + .attr("xlink:href", '/uploads/users/estilocabelo.jpg') + .attr("width",function(d){ + return radiusScale(d['count'])*2; + }) + .attr("height", function(d){ + return radiusScale(d['count'])*2; + }) + .attr("x", 0) + .attr("y", 0); + + + + //simulation + simulation.nodes(dataset) + .on('tick', ticked); //so all data points are attached to it + + function ticked(){ + groups.attr("transform", function(d){ + return "translate(" + d.x + "," + d.y + ")"; + }) + } + }); + + } } -resource_donut_chart.build('/topics/count_resources/'); \ No newline at end of file +charts.build_resource('/topics/count_resources/'); +charts.build_bubble_user('/users/get_users_log/'); \ No newline at end of file diff --git a/users/urls.py b/users/urls.py index 180aceb..1f42149 100644 --- a/users/urls.py +++ b/users/urls.py @@ -18,4 +18,6 @@ urlpatterns = [ url(r'^edit_profile/$', views.UpdateProfile.as_view(), name = 'edit_profile'), url(r'^change_pass/$', views.ChangePassView.as_view(), name='change_pass'), url(r'^remove_account/$', views.DeleteView.as_view(), name='remove_acc'), + + url(r'get_users_log/$', views.get_users_log, name="users_log"), ] diff --git a/users/views.py b/users/views.py index b63050c..7d3e471 100644 --- a/users/views.py +++ b/users/views.py @@ -5,7 +5,7 @@ from django.contrib.auth import authenticate, login as login_user, logout as log from django.contrib.auth.mixins import LoginRequiredMixin from django.core.urlresolvers import reverse, reverse_lazy from django.utils.translation import ugettext_lazy as _ -from django.db.models import Q +from django.db.models import Q, Count from braces import views as braces_mixins @@ -13,7 +13,8 @@ from security.models import Security from log.decorators import log_decorator from log.mixins import LogMixin - +from log.models import Log +from django.http import JsonResponse from .models import User from .utils import has_dependencies from .forms import RegisterUserForm, ProfileForm, UserForm, ChangePassForm, PassResetRequest, SetPasswordForm @@ -521,6 +522,15 @@ def logout(request, next_page = None): return redirect(reverse('users:login')) + +def get_users_log(request): + fifty_users = Log.objects.values('user_id').annotate(count = Count('user_id')).order_by('-count')[:50] + + return JsonResponse(list(fifty_users.values('user_id','user','count')), safe=False) + + + + # API VIEWS class UserViewSet(viewsets.ModelViewSet): queryset = User.objects.all() -- libgit2 0.21.2