Skip to content

Commit df88f45

Browse files
committed
Update Pivot Chart Plugin and Demo
1 parent b995243 commit df88f45

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

demo_app/app/adminx.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from xadmin.layout import Main, TabHolder, Tab, Fieldset, Row, Col, AppendedText, Side
55
from xadmin.plugins.inline import Inline
66
from xadmin.plugins.batch import BatchChangeAction
7+
from django.db.models import Avg, Max, Min, Count, Sum
78

89
class MainDashboard(object):
910
widgets = [
@@ -22,8 +23,8 @@ class MainDashboard(object):
2223

2324

2425
class BaseSetting(object):
25-
enable_themes = True
26-
use_bootswatch = True
26+
enable_themes = False
27+
use_bootswatch = False
2728
xadmin.site.register(views.BaseAdminView, BaseSetting)
2829

2930

@@ -172,24 +173,39 @@ def avg_count(self, instance):
172173
avg_count.allow_tags = True
173174
avg_count.is_column = True
174175

175-
list_display = ('date', 'user_count', 'view_count', 'avg_count')
176-
list_display_links = ('date',)
176+
def user_count_sum(self, obj):
177+
return obj
178+
user_count_sum.short_description = "User Count Sum"
177179

178-
list_filter = ['date', 'user_count', 'view_count']
180+
def view_count_sum(self, obj):
181+
return obj
182+
view_count_sum.short_description = "View Count Sum"
183+
184+
list_display = ('date', 'user_count_sum', 'view_count_sum')
185+
186+
list_filter = ['date']
179187
actions = None
180-
aggregate_fields = {"user_count": "sum", 'view_count': "sum"}
181188

182-
refresh_times = (3, 5, 10)
183-
data_charts = {
184-
"user_count": {'title': u"User Report", "x-field": "date", "y-field": ("user_count", "view_count"), "order": ('date',)},
185-
"avg_count": {'title': u"Avg Report", "x-field": "date", "y-field": ('avg_count',), "order": ('date',)},
186-
"per_month": {'title': u"Monthly Users", "x-field": "_chart_month", "y-field": ("user_count", ),
187-
"option": {
188-
"series": {"bars": {"align": "center", "barWidth": 0.8,'show':True}},
189-
"xaxis": {"aggregate": "sum", "mode": "categories"},
190-
},
191-
},
189+
pivot_fields = {
190+
'values' : ['date',],
191+
'annotate': {
192+
'user_count_sum': Sum('user_count'),
193+
'view_count_sum': Sum('view_count'),
194+
}
192195
}
196+
# aggregate_fields = {"user_count": "sum", 'view_count': "sum"}
197+
198+
# refresh_times = (3, 5, 10)
199+
# data_charts = {
200+
# "user_count": {'title': u"User Report", "x-field": "date", "y-field": ("user_count", "view_count"), "order": ('date',)},
201+
# "avg_count": {'title': u"Avg Report", "x-field": "date", "y-field": ('avg_count',), "order": ('date',)},
202+
# "per_month": {'title': u"Monthly Users", "x-field": "_chart_month", "y-field": ("user_count", ),
203+
# "option": {
204+
# "series": {"bars": {"align": "center", "barWidth": 0.8,'show':True}},
205+
# "xaxis": {"aggregate": "sum", "mode": "categories"},
206+
# },
207+
# },
208+
# }
193209

194210
def _chart_month(self,obj):
195211
return obj.date.strftime("%B")

demo_app/demo/urls.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
# from xadmin.plugins import xversion
88
# xversion.register_models()
99

10-
from django.contrib import admin
11-
admin.autodiscover()
10+
# from django.contrib import admin
11+
# admin.autodiscover()
1212

1313
urlpatterns = patterns('',
14-
url(r'^admin/', include(admin.site.urls)),
1514
url(r'^', include(xadmin.site.urls))
1615
)

xadmin/plugins/pivot_chart.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,19 @@ def mock_for_field_label(obj):
1919

2020
class PivotChartPlugin(BaseAdminPlugin):
2121

22-
pivot_fields = []
22+
pivot_fields = None
2323

2424
def result_item(self, __, obj, field_name, row):
25+
print obj
2526
item = ResultItem(field_name, row)
2627
item.text = obj[field_name]
2728
return item
2829

29-
def get_list_display(self, __):
30-
self.admin_view.list_display = ['log_time', 'click_count_sum']
31-
return __()
32-
3330
def init_request(self, *args, **kwargs):
34-
self.admin_view.click_count_sum = mock_for_field_label
35-
self.admin_view.log_time = mock_for_field_label
3631
return bool(self.pivot_fields)
3732

3833
def queryset(self, qs):
39-
return qs.values('log_time').annotate(click_count_sum=Sum('click_count'))
34+
return qs.values(*self.pivot_fields['values']).annotate(**self.pivot_fields['annotate'])
4035

4136
# Media
4237
def get_media(self, media):

0 commit comments

Comments
 (0)