Security News
Maven Central Adds Sigstore Signature Validation
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
@flourish/legend
Advanced tools
The legend component adds legends to a Flourish template.
There are currently three types:
Multiple instances of a legend may be added to a template.
npm install --save @flourish/legend
Then import into you project like this:
import { createDiscreteColorLegend } from "@flourish/legend"
for discrete (color) legend
import { createContinuousColorLegend } from "@flourish/legend"
for continuous (color) legend
import { createContinuousSizeLegend } from "@flourish/legend"
for continuous (size) legend
In your template.yml, add:
- property: legend
import: "@flourish/legend/discrete-color"
for discrete (color) legend. (Use @flourish/legend/continuous-color
or @flourish/legend/continuous-size
for the other legend types.)
To your state, add:
state: {
"legend": {}
}
Inside your template init
code, initialize the legend component like this:
var legend = createDiscreteColorLegend(state.legend)
legend.appendTo(target_container)
where target_container
is a node element, for example layout.getSection("legend")
legend
.data(array, color_function(optional)) // See explanation below
.filtered(["Brazil"]) // Array, items that should have low opacity
.on("click", function(d) { // Add event listener to legend items (eg. "click", "mouseover", etc.)
console.log(this, d, i); // Legend item node element, "Brazil", 0
})
.update(); // Update the legend
There are two ways to add data to your legend.
var legend_items = [
{
label: "Brazil",
color: "#ff0000"
},
{
label: "Argentina",
color: "#000000"
}
]
legend.data(legend_items);
var getColor = initializeColors(state.color).getColor; // Using Flourish custom colors module
// var getColor = function(label, i) { return color_list[label]; } // Or create your own function, referencing an object/array with colors
var legend_items = ["Brazil", "Argentina"];
legend.data(legend_items, getColor)
legend
.data(domain, colorScale)
.markerValue(500)
.update();
domain
is a 2-element array (e.g. [0, 1000]
) that specifies the values at the lower and upper end of the color scale. color_scale
is a color scale function (e.g. from the colors component). The markerValue
method adds a black and white marker centred on its argument's value. Pass in a value of null
to remove.
legend
.scale(sizeScale)
.update();
size_scale
is a scale function that maps the domain to a size. For example, if the legend is representing circle size, this would typically be a D3 scaleSqrt
.
It's assumed that the domain and range minima (e.g. size_scale.domain()[0]
) are zero to keep things simple and because this is good practice. (It's the equivalent of starting bar charts at zero.)
If size_scale
depends on the container size (which in turn depends on the size of the legend) then .update()
will need to be called twice. The first call to set the height of the legend and the second call to update the legend content.
For example:
size_by_legend
.visible(true)
.update();
// Compute sizeScale (typically this will require calling chart-layout's update function)
size_by_legend
.scale(sizeScale)
.update();
legend.getContainer()
Gets the legend node element
legend.visible(true|false)
Sets the visibility of the legend.
legend.format(formatFunction)
Adds a formatter function to format the labels
legend.autoTitle(string)
Sets the automatic title for the legend. This could be a column name for example.
A typical implementation of multiple legends might look something like:
template.yml
- Discrete color legend
- property: legend_discrete_color
import: "@flourish/legend/discrete-color"
- Continuous color legend
- property: legend_continuous_color
import: "@flourish/legend/continuous-color"
state.js
// Settings overrides
legend_discrete_color: {
text_size: 0.7
},
legend_continuous_color: {
text_size: 0.7
}
legend.js
var discrete_color_legend, continuous_color_legend;
function initLegends() {
discrete_color_legend = createDiscreteColorLegend(state.legend_discrete_color);
continuous_color_legend = createContinuousColorLegend(state.legend_continuous_color);
var legend_container = layout.getSection("legend");
discrete_color_legend.appendTo(legend_container);
continuous_color_legend.appendTo(legend_container);
}
function updateLegends() {
discrete_color_legend
.data(...)
.update();
continuous_color_legend
.data(...)
.update();
}
If a legend needs to change type, add a legend for each type and use .visible()
to hide/show such that one is showing at a time.
FAQs
Flourish module for making legend
The npm package @flourish/legend receives a total of 0 weekly downloads. As such, @flourish/legend popularity was classified as not popular.
We found that @flourish/legend demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.