Files
growstuff/app/assets/javascripts/crops.js.erb
Miles Gould 7fa0d22583 Graph real crop sunninesses from server
Works for me in superficial manual testing, but needs automated tests.
Also currently does two server round trips (one for planting locations
and one for sunninesses); it would be better to combine these into one,
or alternatively to create a "crop stats" API endpoint and calculate
sunniness stats in SQL.
2017-01-09 15:13:55 +00:00

92 lines
3.2 KiB
Plaintext

//= require graphs/horizontal_bar_graph
if (document.getElementById("cropmap") !== null) {
mapbox_map_id = "<%= Rails.env == 'test' ? 0 : Growstuff::Application.config.mapbox_map_id %>";
mapbox_access_token = "<%= Rails.env == 'test' ? 0 : Growstuff::Application.config.mapbox_access_token %>";
mapbox_base_url = "http://a.tiles.mapbox.com/v4/" + mapbox_map_id + "/{z}/{x}/{y}.png?access_token=" + mapbox_access_token;
L.Icon.Default.imagePath = '/assets'
cropmap = L.map('cropmap').setView([0.0, -0.0], 2);
showCropMap(cropmap);
}
function showCropMap(cropmap) {
L.tileLayer(mapbox_base_url, {
attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors under <a href="http://www.openstreetmap.org/copyright">ODbL</a> | Map imagery &copy; <a href="http://mapbox.com">Mapbox</a>',
maxZoom: 18
}).addTo(cropmap);
markers = new L.MarkerClusterGroup({showCoverageOnHover: false, maxClusterRadius: 20 });
things_to_map = location.pathname + '.json';
$.getJSON(things_to_map, function(crop) {
$.each(crop.plantings, function(i, planting) {
owner = planting.owner;
if (owner.latitude && owner.longitude) {
marker = new L.Marker(new L.LatLng(owner.latitude, owner.longitude));
planting_url = "/plantings/" + planting.id;
planting_link = "<a href='" + planting_url + "'>" + owner.login_name + "'s " + crop.name + "</a>";
where = "<p><i>" + owner.location + "</i></p>";
details = "<p>";
if (planting.quantity) {
details = details + "Quantity: " + planting.quantity + "<br/>";
}
if (planting.planted_from) {
details = details + "Planted from: " + planting.planted_from + "<br/>";
}
if (planting.sunniness) {
details = details + "Planted in: " + planting.sunniness+ "<br/>";
}
details = details + "</p>";
marker.bindPopup(planting_link + where + details).openPopup();
markers.addLayer(marker);
}
});
});
cropmap.addLayer(markers);
}
function plantingStats(crop) {
var sunniness_counts = { 'empty': 0, 'sun': 0, 'semi-shade': 0, 'shade': 0 };
$.each(crop.plantings, function(i, planting) {
if (planting.sunniness) {
sunniness_counts[planting.sunniness]++;
} else {
sunniness_counts['Empty']++;
}
});
return [
{name: 'Empty', value: sunniness_counts['empty']},
{name: 'Sun', value: sunniness_counts['sun']},
{name: 'Semi-shade', value: sunniness_counts['semi-shade']},
{name: 'Shade', value: sunniness_counts['shade']}
];
}
if ($("#sunchart")[0] !== null) {
var HorizontalBarGraph = growstuff.HorizontalBarGraph;
$.getJSON(location.pathname + '.json', function (crop) {
data = {
bars: plantingStats(crop),
bar_color: 'steelblue',
width: {size: 300, scale: 'linear'},
height: {size: 100, scale: 'ordinal'},
//left is used to shift the bars over so that there is
//room for the labels
margin: {top: 0, right: 0, bottom: 0, left: 100}
};
var graph = new HorizontalBarGraph(data);
graph.render(d3.select($('#sunchart')[0]));
});
}
$('.btn.toggle.crop-hierarchy').click(function () {
$('.toggle.crop-hierarchy').toggleClass('hide');
});