Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort category names alphabetically in legends #3218

Merged
merged 4 commits into from
Apr 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
3.10.1 ()
---------
* Sort category names alphabetically in legends [3218](https://github.com/CartoDB/cartodb/pull/3218)
* Editable descriptions and tags in the maps and datasets view [3129](https://github.com/CartoDB/cartodb/pull/3129)
* fixed interaction when there are hidden layers #3090
* Add caching of geometry types [#3157](https://github.com/CartoDB/cartodb/pull/3157)
Expand Down
53 changes: 18 additions & 35 deletions lib/assets/javascripts/cartodb/models/carto/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,64 +37,47 @@ cdb.admin.carto.category = {
// Close defaults
css += "}\n";

if (props.colors && !props.categories) {
colors_to_categories();
} else if (changed.property || !props.categories || props.categories.length === 0) {
if (changed.property || !props.categories || props.categories.length === 0) {
this.get_categories(props.property, table, function(colors) {
callback(css + self.generate_categories(props, table, colors), colors);
});
} else {
callback(css + this.generate_categories(props, table, props.categories), props.categories);
}

// Transform colors values to categories array
function colors_to_categories() {
var colors = [];

// Generate categories metadata
for (var i in props.colors) {
var obj = {};
var color = props.colors[i];

obj.title = (i < this.max_values) ? color[0] : this.others_value;
obj.title_type = color[2];
obj.color = color[1];
obj.value_type = 'color';

if (i >= this.max_values) {
obj["default"] = true;
}

colors.push(obj);
}

callback(css + self.generate_categories(props, table, colors), colors);
}

},

// Get values with default colors from the sql
get_categories: function(property, table, callback) {

var self = this;
table.data().categoriesForColumn(this.max_values, property, function(cat) {

// We request an extra category to determine if we need to display the "Others" legend
table.data().categoriesForColumn(this.max_values + 1, property, function(cat) {
var column_type = cat.type;
var categories = cat.categories;
var showOthers = cat.categories.length > self.max_values;

// Limit the categories we display and sort them alphabetically
var categories = cat.categories.slice(0, self.max_values).sort();

var colors = [];

// Generate categories metadata
for (var i in categories) {
var obj = {};
obj.title = (i < self.max_values) ? categories[i]: self.others_value;
obj.title = categories[i];
obj.title_type = column_type;
obj.color = cdb.admin.color_brewer[i];
obj.value_type = 'color';

if (i >= self.max_values) {
obj["default"] = true;
}
colors.push(obj);
}
if (showOthers) {
colors.push({
title: self.others_value,
value_type: 'color',
color: cdb.admin.color_brewer[categories.length],
default: true
})
}
callback(colors);
});
},
Expand Down