Commit e7b99ff5 by Dennis Jen

Merge pull request #189 from edx/dsjen/demographic-tooltips

Updated charting tooltips to include header and updated labels.
parents 90cb48ad 8b25011f
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
require(['vendor/domReady!', 'load/init-page'], function (doc, page) { require(['vendor/domReady!', 'load/init-page'], function (doc, page) {
'use strict'; 'use strict';
require(['views/data-table-view', 'views/trends-view'], function (DataTableView, TrendsView) { require(['underscore', 'views/data-table-view', 'views/trends-view'], function (_, DataTableView, TrendsView) {
// shared settings between the chart and table // shared settings between the chart and table
// colors are chosen to be color-blind accessible // colors are chosen to be color-blind accessible
var settings = [ var settings = [
...@@ -72,9 +72,8 @@ require(['vendor/domReady!', 'load/init-page'], function (doc, page) { ...@@ -72,9 +72,8 @@ require(['vendor/domReady!', 'load/init-page'], function (doc, page) {
title: 'Students', title: 'Students',
key: 'count' key: 'count'
}, },
tooltip: gettext('The number of active students, and the number of students who engaged in specific activities, over time.'), // jshint ignore:line // Translators: <%=value%> will be replaced with a date.
// Translators: %(value)s will be replaced with a date. interactiveTooltipHeaderTemplate: _.template(gettext('Week Ending <%=value%>'))
interactiveTooltipHeader: gettext('Week Ending %(value)s')
}); });
// weekly engagement activities table // weekly engagement activities table
......
...@@ -6,8 +6,8 @@ ...@@ -6,8 +6,8 @@
require(['vendor/domReady!', 'load/init-page'], function(doc, page) { require(['vendor/domReady!', 'load/init-page'], function(doc, page) {
'use strict'; 'use strict';
require(['views/data-table-view', 'views/histogram-view'], require(['underscore', 'views/data-table-view', 'views/histogram-view'],
function (DataTableView, HistogramView) { function (_, DataTableView, HistogramView) {
// used in the table to show ages above this are binned--displayed as "100+" // used in the table to show ages above this are binned--displayed as "100+"
var maxNumber = 100; var maxNumber = 100;
...@@ -18,12 +18,14 @@ require(['vendor/domReady!', 'load/init-page'], function(doc, page) { ...@@ -18,12 +18,14 @@ require(['vendor/domReady!', 'load/init-page'], function(doc, page) {
modelAttribute: 'ages', modelAttribute: 'ages',
excludeData: ['Unknown'], excludeData: ['Unknown'],
trends: [{ trends: [{
title: gettext('Students'), title: gettext('Number of Students'),
color: 'rgb(58, 162, 224)', color: 'rgb(58, 162, 224)',
maxNumber: maxNumber maxNumber: maxNumber
}], }],
x: { key: 'age' }, x: { key: 'age' },
y: { key: 'count' } y: { key: 'count' },
// Translators: <%=value%> will be replaced with an age.
interactiveTooltipHeaderTemplate: _.template(gettext('Age: <%=value%>'))
}); });
new DataTableView({ new DataTableView({
......
...@@ -6,9 +6,8 @@ ...@@ -6,9 +6,8 @@
require(['vendor/domReady!', 'load/init-page'], function(doc, page) { require(['vendor/domReady!', 'load/init-page'], function(doc, page) {
'use strict'; 'use strict';
require(['views/data-table-view', require(['underscore', 'views/data-table-view', 'views/discrete-bar-view'],
'views/discrete-bar-view'], function (_, DataTableView, DiscreteBarView) {
function (DataTableView, DiscreteBarView) {
new DiscreteBarView({ new DiscreteBarView({
el: '#enrollment-chart-view', el: '#enrollment-chart-view',
...@@ -17,11 +16,13 @@ require(['vendor/domReady!', 'load/init-page'], function(doc, page) { ...@@ -17,11 +16,13 @@ require(['vendor/domReady!', 'load/init-page'], function(doc, page) {
excludeData: ['Unknown'], excludeData: ['Unknown'],
dataType: 'percent', dataType: 'percent',
trends: [{ trends: [{
title: gettext('Education'), title: gettext('Percentage'),
color: 'rgb(58, 162, 224)' color: 'rgb(58, 162, 224)'
}], }],
x: { key: 'educationLevel' }, x: { key: 'educationLevel' },
y: { key: 'percent' } y: { key: 'percent' },
// Translators: <%=value%> will be replaced with a level of education (e.g. Doctorate).
interactiveTooltipHeaderTemplate: _.template(gettext('Education: <%=value%>'))
}); });
new DataTableView({ new DataTableView({
......
...@@ -6,9 +6,8 @@ ...@@ -6,9 +6,8 @@
require(['vendor/domReady!', 'load/init-page'], function(doc, page) { require(['vendor/domReady!', 'load/init-page'], function(doc, page) {
'use strict'; 'use strict';
require(['views/data-table-view', require(['underscore', 'views/data-table-view', 'views/discrete-bar-view'],
'views/discrete-bar-view'], function (_, DataTableView, DiscreteBarView) {
function (DataTableView, DiscreteBarView) {
new DiscreteBarView({ new DiscreteBarView({
el: '#enrollment-chart-view', el: '#enrollment-chart-view',
...@@ -16,11 +15,13 @@ require(['vendor/domReady!', 'load/init-page'], function(doc, page) { ...@@ -16,11 +15,13 @@ require(['vendor/domReady!', 'load/init-page'], function(doc, page) {
modelAttribute: 'genders', modelAttribute: 'genders',
dataType: 'percent', dataType: 'percent',
trends: [{ trends: [{
title: gettext('Gender'), title: gettext('Percentage'),
color: 'rgb(58, 162, 224)' color: 'rgb(58, 162, 224)'
}], }],
x: { key: 'gender' }, x: { key: 'gender' },
y: { key: 'percent' } y: { key: 'percent' },
// Translators: <%=value%> will be replaced with a level of gender (e.g. Female).
interactiveTooltipHeaderTemplate: _.template(gettext('Gender: <%=value%>'))
}); });
// Daily enrollment table // Daily enrollment table
......
/**
* Abstract class for NVD3 bar charts (includes discrete bar and histogram).
*/
define(['nvd3', 'underscore', 'views/chart-view'],
function (nvd3, _, ChartView) {
'use strict';
var BarView = ChartView.extend({
defaults: _.extend({}, ChartView.prototype.defaults, {
graphShiftSelector: '.nv-barsWrap'
}
),
initChart: function(chart) {
var self = this;
ChartView.prototype.initChart.call(self, chart);
// NVD3's bar views display tooltips differently than for graphs
chart.tooltipContent(function(key, x, y) {
var maxNumber = self.options.trends[0].maxNumber,
xValue = x;
if (!_(maxNumber).isUndefined) {
// e.g. 100+
xValue = x >= maxNumber ? maxNumber + '+' : x;
}
if (_(self.options).has('interactiveTooltipHeaderTemplate')) {
xValue = self.options.interactiveTooltipHeaderTemplate({value: xValue});
}
return self.hoverTooltipTemplate({
xValue: xValue,
label: key,
yValue: y,
swatchColor: self.options.trends[0].color
});
});
}
});
return BarView;
}
);
...@@ -217,13 +217,6 @@ define(['d3', 'jquery', 'nvd3', 'underscore', 'utils/utils', 'views/attribute-li ...@@ -217,13 +217,6 @@ define(['d3', 'jquery', 'nvd3', 'underscore', 'utils/utils', 'views/attribute-li
.tickFormat(Utils.localizeNumber); .tickFormat(Utils.localizeNumber);
self.chart.yAxis.tickFormat(self.getYAxisFormat()); self.chart.yAxis.tickFormat(self.getYAxisFormat());
if (_(self.options).has('interactiveTooltipHeader')) {
self.chart.interactiveLayer.tooltip.headerFormatter(function (d) {
// interpolate is a global django function
return interpolate(self.options.interactiveTooltipHeader, {value: d}, true); // jshint ignore:line
});
}
// Append the svg to an inner container so that it adapts to // Append the svg to an inner container so that it adapts to
// the height of the inner container instead of the outer // the height of the inner container instead of the outer
// container which needs to create height for the title. // container which needs to create height for the title.
......
define(['nvd3', 'underscore', 'views/chart-view'], define(['nvd3', 'underscore', 'views/bar-view'],
function (nvd3, _, ChartView) { function (nvd3, _, BarView) {
'use strict'; 'use strict';
var DiscreteBarView = ChartView.extend({ var DiscreteBarView = BarView.extend({
defaults: _.extend({}, ChartView.prototype.defaults, { defaults: _.extend({}, BarView.prototype.defaults, {
// unsetting because this view will always display all x-ticks // unsetting because this view will always display all x-ticks
displayExplicitTicksThreshold: undefined, displayExplicitTicksThreshold: undefined
graphShiftSelector: '.nv-barsWrap'
} }
), ),
...@@ -17,28 +16,11 @@ define(['nvd3', 'underscore', 'views/chart-view'], ...@@ -17,28 +16,11 @@ define(['nvd3', 'underscore', 'views/chart-view'],
initChart: function(chart) { initChart: function(chart) {
var self = this; var self = this;
ChartView.prototype.initChart.call(self, chart); BarView.prototype.initChart.call(self, chart);
if (_(self.options.trends[0]).has('color')) { if (_(self.options.trends[0]).has('color')) {
chart.color([self.options.trends[0].color]); chart.color([self.options.trends[0].color]);
} }
chart.tooltipContent(function(key, x, y) {
var maxNumber = self.options.trends[0].maxNumber,
xValue = x;
if (!_(maxNumber).isUndefined) {
// e.g. 100+
xValue = x >= maxNumber ? maxNumber + '+' : x;
}
return self.hoverTooltipTemplate({
xValue: xValue,
label: key,
yValue: y,
swatchColor: self.options.trends[0].color
});
});
} }
}); });
......
define(['nvd3', 'underscore', 'views/chart-view'], define(['nvd3', 'underscore', 'views/bar-view'],
function (nvd3, _, ChartView) { function (nvd3, _, BarView) {
'use strict'; 'use strict';
var HistogramView = ChartView.extend({ var HistogramView = BarView.extend({
defaults: _.extend({}, ChartView.prototype.defaults, {
graphShiftSelector: '.nv-barsWrap'
}
),
getChart: function() { getChart: function() {
return nvd3.models.multiBarChart(); return nvd3.models.multiBarChart();
}, },
initChart: function(chart) { initChart: function(chart) {
ChartView.prototype.initChart.call(this, chart); BarView.prototype.initChart.call(this, chart);
var self = this;
chart.showLegend(false); chart.showLegend(false);
chart.multibar.stacked(true); chart.multibar.stacked(true);
chart.showControls(false); chart.showControls(false);
chart.tooltip(function(key, x, y) {
var maxNumber = self.options.trends[0].maxNumber;
return self.hoverTooltipTemplate({
xValue: x >= maxNumber ? maxNumber + '+' : x, // e.g. 100+
label: key,
yValue: y,
swatchColor: self.options.trends[0].color
});
});
} }
}); });
......
...@@ -10,8 +10,16 @@ define(['moment', 'nvd3', 'underscore', 'views/chart-view'], ...@@ -10,8 +10,16 @@ define(['moment', 'nvd3', 'underscore', 'views/chart-view'],
initChart: function (chart) { initChart: function (chart) {
ChartView.prototype.initChart.call(this, chart); ChartView.prototype.initChart.call(this, chart);
var self = this;
chart.showLegend(false) chart.showLegend(false)
.useInteractiveGuideline(true); .useInteractiveGuideline(true);
if (_(self.options).has('interactiveTooltipHeaderTemplate')) {
self.chart.interactiveLayer.tooltip.headerFormatter(function (d) {
return self.options.interactiveTooltipHeaderTemplate({value: d});
});
}
}, },
formatXTick: function (d) { formatXTick: function (d) {
......
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