Skip to content

Commit 2909acf

Browse files
committed
highcharts integration working
1 parent c71f892 commit 2909acf

File tree

3 files changed

+99
-175
lines changed

3 files changed

+99
-175
lines changed

app/assets/javascripts/scout.js

Lines changed: 95 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@ function render(method) {
7575
}
7676

7777
function renderAll() {
78-
// $('#static').scope().render();
7978
$('.filterchart').each(function() { this.render() });
80-
chart.each(render);
8179
list.each(render);
8280
d3.select("#active").text(formatNumber(gr_all.value()));
8381
numWins = _.find(winGrp.all(), function(grp) {return grp.key == "true"}).value.value
@@ -86,17 +84,6 @@ function renderAll() {
8684
d3.select("#winrate").text(pctWins);
8785
}
8886

89-
// not currently used, was from the original crossfilter demo
90-
window.filter = function(filters) {
91-
filters.forEach(function(d, i) { charts[i].filter(d); });
92-
renderAll();
93-
};
94-
95-
window.reset = function(i) {
96-
charts[i].filter(null);
97-
renderAll();
98-
};
99-
10087
function matchList(elem) {
10188
var gamerecordsByDate = dateDim.top(80);
10289

@@ -351,7 +338,7 @@ function data_host() {
351338
}
352339

353340
function debug_suffix() {
354-
debug = true;
341+
debug = false;
355342

356343
if (debug) {
357344
return "_debug";
@@ -369,6 +356,53 @@ function entities_url() {
369356
}
370357

371358
function filter_chart(element, dimension, group, domain) {
359+
element.group = group;
360+
element.dimension = dimension;
361+
element.djchart = $(element).parents('.djchart');
362+
var dateAxis = (domain[0] > Date.UTC(2010, 1, 1));
363+
364+
element.render = function () {
365+
var grp = this.group.all();
366+
if (typeof grp !== 'undefined') {
367+
var xyValues = _.map(grp, function (g) { return [g.key, g.value.value]; })
368+
this.chart.series[0].setData(xyValues);
369+
}
370+
}
371+
372+
element.reset = function () {
373+
console.log('element reset!');
374+
this.chart.xAxis[0].removePlotBand('plot-band-1');
375+
this.dimension.filterAll();
376+
377+
this.djchart.children(".filtered").css('visibility', 'hidden');
378+
renderAll();
379+
}
380+
381+
element.range_select = function (event) {
382+
console.log("Selected", event.xAxis[0].min, event.xAxis[0].max, event);
383+
384+
event.xAxis[0].axis.removePlotBand('plot-band-1');
385+
386+
event.xAxis[0].axis.addPlotBand({
387+
from: event.xAxis[0].min,
388+
to: event.xAxis[0].max,
389+
color: 'rgba(150, 50, 50, 0.1)',
390+
zIndex: 5,
391+
id: 'plot-band-1'
392+
});
393+
394+
element.djchart.find(".lower").text(Math.floor(event.xAxis[0].min));
395+
element.djchart.find(".upper").text(Math.floor(event.xAxis[0].max));
396+
element.djchart.children(".filtered").css('visibility', 'visible');
397+
398+
dimension.filterRange([event.xAxis[0].min, event.xAxis[0].max]);
399+
renderAll();
400+
401+
// TODO if not too hard, make selection area draggable
402+
403+
event.preventDefault();
404+
};
405+
372406
var options = {
373407
chart: {
374408
type: "column",
@@ -377,47 +411,21 @@ function filter_chart(element, dimension, group, domain) {
377411
color: "#00f",
378412
animation: false,
379413
zoomType: 'x',
380-
width: 170,
414+
width: 185,
381415
height: 130,
382-
events: {
383-
selection: function(event) {
384-
console.log("Selected", event.xAxis[0].min, event.xAxis[0].max, event);
385-
386-
event.xAxis[0].axis.removePlotBand('plot-band-1');
387-
388-
event.xAxis[0].axis.addPlotBand({
389-
from: event.xAxis[0].min,
390-
to: event.xAxis[0].max,
391-
color: 'rgba(150, 50, 50, 0.1)',
392-
zIndex: 5,
393-
id: 'plot-band-1'
394-
});
395-
396-
filterchart = event.currentTarget.container.parentElement.parentElement
397-
$(filterchart).children(".filtered").show();
398-
$(filterchart).find(".lower").text(Math.floor(event.xAxis[0].min));
399-
$(filterchart).find(".upper").text(Math.floor(event.xAxis[0].max));
400-
401-
dimension.filterRange([event.xAxis[0].min, event.xAxis[0].max]);
402-
renderAll();
403-
404-
// TODO make the RESET link actually work
405-
// TODO when filter is cleared, hide the filter text and RESET link
406-
// TODO switch all charts over to highcharts, in a nice DRY way
407-
// TODO if not too hard, make selection area draggable
408-
409-
event.preventDefault();
410-
}
411-
}
416+
events: { selection: element.range_select }
412417
},
413418
title: {text: ""},
414419
legend: {enabled: false},
415420
xAxis: {
416421
labels: {
417422
enabled: true,
418423
style: {"fontFamily":"'Open Sans', verdana, arial, helvetica, sans-serif"},
419-
formatter: function () { return this.value; }
420-
}
424+
formatter: dateAxis ? null : function () { return this.value; }
425+
},
426+
type: dateAxis ? 'datetime' : 'linear',
427+
min: domain[0],
428+
max: domain[1]
421429
},
422430
yAxis: {
423431
lineWidth: 0,
@@ -459,17 +467,36 @@ function filter_chart(element, dimension, group, domain) {
459467
element.chart.addSeries({
460468
data: xyValues
461469
});
462-
element.group = group;
463-
element.dimension = dimension;
464-
element.render = function () {
465-
var grp = this.group.all();
466-
if (typeof grp !== 'undefined') {
467-
var xyValues = _.map(grp, function (g) { return [g.key, g.value.value]; })
468-
this.chart.series[0].setData(xyValues);
469-
}
470-
}
471470
}
472471

472+
function reset_click(e) {
473+
console.log('reset_click', e);
474+
e.preventDefault();
475+
var filterchart = $(e.target).parents('.djchart').find('.filterchart')[0];
476+
filterchart.reset();
477+
}
478+
479+
function add_filterchart(element, dimension, group, title, domain) {
480+
481+
var chartContainer = $(document.createElement('div')).addClass('djchart');
482+
483+
var chartTitle = $('<div class="title">' + title + '</div>');
484+
485+
var chartFilterText = $('<div class="filtered title" style="visibility:hidden">from <span class="lower">-</span> to <span class="upper">-</span></div>');
486+
var filterResetLink = $('<a href="#" class="reset">reset</a>');
487+
488+
filterResetLink.click(reset_click);
489+
chartFilterText.append(filterResetLink);
490+
chartContainer.append(chartTitle);
491+
chartContainer.append(chartFilterText);
492+
493+
var thechart = $(document.createElement('div')).addClass('filterchart');
494+
chartContainer.append(thechart);
495+
496+
filter_chart(thechart[0], dimension, group, domain);
497+
498+
element.append(chartContainer);
499+
}
473500

474501
function scout_init() {
475502
var start = Date.now();
@@ -532,7 +559,9 @@ function scout_init() {
532559
durDim = gr_cf.dimension(function(gr) { return Math.min(40, gr.match.duration_minutes) });
533560
durGrp = groupWinPct(durDim.group(function(d) { return d }));
534561

535-
dateDim = gr_cf.dimension(function(gr) { return gr.match.play_date });
562+
dateDim = gr_cf.dimension(function(gr) {
563+
return gr.match.play_date.getTime();
564+
});
536565
dateGrp = groupDJ(dateDim.group());
537566

538567
asDim = gr_cf.dimension(function(gr) { return gr.player.as8 });
@@ -560,76 +589,6 @@ function scout_init() {
560589

561590
var dims_built = Date.now();
562591

563-
charts = [
564-
barChart()
565-
.dimension(lgDim)
566-
.group(lgGrp)
567-
.x(d3.scale.linear()
568-
.domain([0, 6])
569-
.rangeRound([0, 10 * 15])),
570-
571-
barChart()
572-
.dimension(asDim)
573-
.group(asGrp)
574-
.x(d3.scale.linear()
575-
.domain([0, 2500])
576-
.rangeRound([0, 10 * 15])),
577-
578-
barChart()
579-
.dimension(oasDim)
580-
.group(oasGrp)
581-
.x(d3.scale.linear()
582-
.domain([0, 2500])
583-
.rangeRound([0, 10 * 15])),
584-
585-
barChart()
586-
.dimension(wsDim)
587-
.group(wsGrp)
588-
.x(d3.scale.linear()
589-
.domain([0, 60])
590-
.rangeRound([0, 10 * 15])),
591-
592-
barChart()
593-
.dimension(owsDim)
594-
.group(owsGrp)
595-
.x(d3.scale.linear()
596-
.domain([0, 60])
597-
.rangeRound([0, 10 * 15])),
598-
599-
barChart()
600-
.dimension(mb2Dim)
601-
.group(mb2Grp)
602-
.x(d3.scale.linear()
603-
.domain([0, 15])
604-
.rangeRound([0, 10 * 15])),
605-
606-
barChart()
607-
.dimension(omb2Dim)
608-
.group(omb2Grp)
609-
.x(d3.scale.linear()
610-
.domain([0, 15])
611-
.rangeRound([0, 10 * 15])),
612-
613-
barChart()
614-
.dimension(durDim)
615-
.group(durGrp)
616-
.x(d3.scale.linear()
617-
.domain([0, 40])
618-
.rangeRound([0, 20 * 8])),
619-
620-
barChart()
621-
.dimension(dateDim)
622-
.group(dateGrp)
623-
.round(d3.time.day.round)
624-
.x(d3.time.scale()
625-
.domain([new Date(2014, 7, 1), new Date(2014, 8, 3)])
626-
.rangeRound([0, 10 * 40])),
627-
];
628-
629-
chart = d3.selectAll(".chart")
630-
.data(charts)
631-
.each(function(chart) { chart.on("brush", renderAll).on("brushend", renderAll); });
632-
633592
// Render the initial lists.
634593
list = d3.selectAll(".list tbody")
635594
.data([matchList]);
@@ -649,26 +608,16 @@ function scout_init() {
649608
})
650609
});
651610

611+
add_filterchart($('#filtercharts'), lgDim, lgGrp, 'Game league', [0, 6]);
612+
add_filterchart($('#filtercharts'), asDim, asGrp, 'Player\'s Army Strength @ X minutes', [0, 2500]);
613+
add_filterchart($('#filtercharts'), oasDim, oasGrp, 'Opponent\'s Army Strength @ X minutes', [0, 2500]);
614+
add_filterchart($('#filtercharts'), wsDim, wsGrp, 'Player\'s Workers @ X minutes', [0, 60]);
615+
add_filterchart($('#filtercharts'), owsDim, owsGrp, 'Opponent\'s Workers @ X minutes', [0, 60]);
616+
add_filterchart($('#filtercharts'), mb2Dim, mb2Grp, 'Player\'s 2nd Mining Base Timing', [0, 15]);
617+
add_filterchart($('#filtercharts'), omb2Dim, omb2Grp, 'Opponent\'s 2nd Mining Base Timing', [0, 15]);
618+
add_filterchart($('#filtercharts'), durDim, durGrp, 'Game Length, minutes', [0, 40]);
619+
add_filterchart($('#filtercharts'), dateDim, dateGrp, 'Game Date', [Date.UTC(2014, 7, 1), Date.UTC(2014, 8, 3)]);
652620

653-
chartContainer = $(document.createElement('div')).addClass('djchart');
654-
655-
chartTitle = $('<div class="title">Player\'s Army Strength @ X minutes</div>');
656-
657-
chartFilterText = $('<div class="filtered title" style="display:none">from <span class="lower">-</span> to <span class="upper">-</span></div>');
658-
filterResetLink = $('<a href="#" class="reset">reset</a>');
659-
filterResetLink.click(function(e) { e.preventDefault(); console.log('reset!'); });
660-
chartFilterText.append(filterResetLink);
661-
662-
chartContainer.append(chartTitle);
663-
chartContainer.append(chartFilterText);
664-
665-
666-
asChart = $(document.createElement('div')).addClass('filterchart');
667-
chartContainer.append(asChart);
668-
669-
filter_chart(asChart[0], asDim, asGrp, [0, 2500]);
670-
$('#filtercharts').append(chartContainer);
671-
672621
renderAll();
673622

674623
var end = Date.now();

app/assets/stylesheets/scout.css.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
display: inline-block;
1313
height: 151px;
1414
margin-bottom: 20px;
15+
.title {
16+
margin-bottom: 0px;
17+
max-width: 175px;
18+
}
1519
}
1620

1721
.reset {

app/views/home/scout.html

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,6 @@
2222
<div><span id="winrate">-</span>% matches won.</div>
2323
<div><span id="active">-</span> of <span id="total">-</span> matches selected.</div>
2424
<div id="filtercharts"></div>
25-
<div id="charts">
26-
<div id="league-chart" class="chart">
27-
<div class="title">Game League</div>
28-
</div>
29-
<div id="as-chart" class="chart">
30-
<div class="title">Player's Army Strength @ X minutes</div>
31-
</div>
32-
<div id="oas-chart" class="chart">
33-
<div class="title">Opponent's Army Strength @ X minutes</div>
34-
</div>
35-
<div id="ws-chart" class="chart">
36-
<div class="title">Player's Workers @ X minutes</div>
37-
</div>
38-
<div id="ows-chart" class="chart">
39-
<div class="title">Opponent's Workers @ X minutes</div>
40-
</div>
41-
<div id="mb2-chart" class="chart">
42-
<div class="title">Player's 2nd Mining Base Timing</div>
43-
</div>
44-
<div id="omb2-chart" class="chart">
45-
<div class="title">Opponent's 2nd Mining Base Timing</div>
46-
</div>
47-
<div id="duration-chart" class="chart">
48-
<div class="title">Game Length, minutes</div>
49-
</div>
50-
<div id="date-chart" class="chart">
51-
<div class="title">Game Date</div>
52-
</div>
53-
</div>
5425
<div id="lists">
5526
<table id="match-list" class="list">
5627
<tbody>

0 commit comments

Comments
 (0)