function Legend(){
var data = [];
var rows = 1;
var itemCount = 5;
var multiline = false;
var justify = "flex-start";
function createLegend(){}
function chunkArray(myArray, chunk_size){
var index = 0;
var arrayLength = myArray.length;
var tempArray = [];
for (index = 0; index < arrayLength; index += chunk_size) {
myChunk = myArray.slice(index, index+chunk_size);
// Do something if you want with the group
tempArray.push(myChunk);
}
return tempArray;
}
createLegend.data = function(val){
data = chunkArray(val,itemCount);
return createLegend;
}
createLegend.renderTo = function(selector){
var container = $("<div></div>").appendTo(selector);
container.addClass("legend-container");
data.forEach((rowdata,index)=>{
var row = $("<div></div>").appendTo(container);
row.addClass("legend-row-"+(index+1));
row.css("justify-content",justify);
createLegenditems(row,rowdata);
})
function createLegenditems(row,rowdata){
var widths = [];
var labelLengths = [];
var labelRatios = [];
var maxWidth = 1180/rowdata.length;
rowdata.forEach((value)=>{
labelLengths.push(value.label.length)
})
var labelMaxLength = Math.max(...labelLengths);
var labelMinLength = Math.min(...labelLengths);
labelLengths.forEach((value)=>{
labelRatios.push(value/labelMinLength);
})
rowdata.forEach(function(value,index){
var legendGroup = $("<div></div>").appendTo(row);
legendGroup.addClass("legend-group");
var legendDot = $("<div></div>").appendTo(legendGroup);
legendDot.addClass("legend-dot");
legendDot.css("background",value.color) ;
var legendText = $("<div></div>").appendTo(legendGroup);
legendText.addClass("legend-text");
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.