@flourish/legend
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -927,2 +927,4 @@ (function (global, factory) { | ||
order_override: "", | ||
orientation: "horizontal", | ||
@@ -1018,6 +1020,37 @@ | ||
function sortByOrderOverride(items, order_override_string) { | ||
// First build a lookup table from each of the overridden legend | ||
// labels to the index of its new position in the legend. | ||
var new_index_lookup = {}; | ||
var overrides = order_override_string.split(/\s*\n\s*/); | ||
for (var i = 0; i < overrides.length; i++) { | ||
var override = overrides[i]; | ||
new_index_lookup[override] = i; | ||
} | ||
// Now go through each of the original items and if it appears in | ||
// the lookup table, set it at its new position in sorted_items. | ||
var sorted_items = []; | ||
for (var old_index = 0; old_index < items.length; old_index++) { | ||
var item = items[old_index]; | ||
var new_index = new_index_lookup[item.label]; | ||
if (new_index === undefined) continue; | ||
sorted_items[new_index] = item; | ||
} | ||
// If there was any override that isn't a legend label (e.g. it's | ||
// mispelled), there'll be a "hole" in the sorted_items array, so | ||
// compact it before returning. | ||
return sorted_items.filter(function (item) { return item !== undefined; }); | ||
} | ||
DiscreteColorLegend.prototype._updateLegend = function() { | ||
var _this = this; | ||
var legend_items = this._container.selectAll(".fl-legend-item").data(this._legend_items); | ||
var legend_item_data; | ||
if (this._state.order_override.trim()) { | ||
legend_item_data = sortByOrderOverride(this._legend_items, this._state.order_override); | ||
} | ||
else { | ||
legend_item_data = this._legend_items; | ||
} | ||
var legend_items = this._container.selectAll(".fl-legend-item").data(legend_item_data); | ||
var legend_items_enter = legend_items.enter().append("div").attr("class", "fl-legend-item"); | ||
@@ -1024,0 +1057,0 @@ legend_items_enter.append("div").attr("class", "fl-legend-swatch"); |
{ | ||
"name": "@flourish/legend", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Flourish module for making legend", | ||
@@ -5,0 +5,0 @@ "main": "legend.js", |
@@ -0,1 +1,4 @@ | ||
# 1.3.0 | ||
* Add custom order override setting | ||
# 1.2.0 | ||
@@ -2,0 +5,0 @@ * Add format method |
@@ -15,2 +15,4 @@ import { select, event as d3_event } from "d3-selection"; | ||
order_override: "", | ||
orientation: "horizontal", | ||
@@ -106,6 +108,37 @@ | ||
function sortByOrderOverride(items, order_override_string) { | ||
// First build a lookup table from each of the overridden legend | ||
// labels to the index of its new position in the legend. | ||
var new_index_lookup = {}; | ||
var overrides = order_override_string.split(/\s*\n\s*/); | ||
for (var i = 0; i < overrides.length; i++) { | ||
var override = overrides[i]; | ||
new_index_lookup[override] = i; | ||
} | ||
// Now go through each of the original items and if it appears in | ||
// the lookup table, set it at its new position in sorted_items. | ||
var sorted_items = []; | ||
for (var old_index = 0; old_index < items.length; old_index++) { | ||
var item = items[old_index]; | ||
var new_index = new_index_lookup[item.label]; | ||
if (new_index === undefined) continue; | ||
sorted_items[new_index] = item; | ||
} | ||
// If there was any override that isn't a legend label (e.g. it's | ||
// mispelled), there'll be a "hole" in the sorted_items array, so | ||
// compact it before returning. | ||
return sorted_items.filter(function (item) { return item !== undefined; }); | ||
} | ||
DiscreteColorLegend.prototype._updateLegend = function() { | ||
var _this = this; | ||
var legend_items = this._container.selectAll(".fl-legend-item").data(this._legend_items); | ||
var legend_item_data; | ||
if (this._state.order_override.trim()) { | ||
legend_item_data = sortByOrderOverride(this._legend_items, this._state.order_override); | ||
} | ||
else { | ||
legend_item_data = this._legend_items; | ||
} | ||
var legend_items = this._container.selectAll(".fl-legend-item").data(legend_item_data); | ||
var legend_items_enter = legend_items.enter().append("div").attr("class", "fl-legend-item"); | ||
@@ -112,0 +145,0 @@ legend_items_enter.append("div").attr("class", "fl-legend-swatch"); |
Sorry, the diff of this file is not supported yet
70011
1643