Skip to content

Instantly share code, notes, and snippets.

@smileham
Last active May 14, 2024 20:37
Show Gist options
  • Save smileham/4bbca832d8fe629b72beb4e2b9a4b7ea to your computer and use it in GitHub Desktop.
Save smileham/4bbca832d8fe629b72beb4e2b9a4b7ea to your computer and use it in GitHub Desktop.
jArchi script to create Heatmap (set Red, Amber or Green background to element) based on the value of a given property. #jarchi
/*
* HeatMap by Property Value
*
* Requires jArchi - https://www.archimatetool.com/blog/2018/07/02/jarchi/
*
* Version 1: Set colour to red, amber or green based on value of given property.
* Version 2: Pull out all Properties used in Elements in view, present all possible values of that Property to assign RAG colours
* Version 3: Error handling and key generation
* Version 4: Updated for dynamic colour selection and support for more than 3 colours
* Version 5: If there are less than 5 values, buttons are used instead of the menu.
*
* (c) 2022 Steven Mileham
*
*/
var debug = false;
var buttonLimit=5;
console.show();
console.clear();
console.log("> Starting Heat Map By Property Value");
// Borrowed from https://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb
function componentToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
function rgbToHex(r, g, b) {
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
function colorDialog() {
dialog = new org.eclipse.swt.widgets.ColorDialog(shell);
theColourRGB = dialog.open();
return !theColourRGB?null:rgbToHex(theColourRGB.red,theColourRGB.green,theColourRGB.blue);
}
function buttonDialog(message,options) {
var dialog = new org.eclipse.jface.dialogs.MessageDialog(shell,"Archi",null,message,3,options.concat(["Cancel"]),0);
var result=dialog.open();
return result==options.length?null:(result+1).toString();
}
var theView = $(selection).filter("archimate-diagram-model").first();
var propertyHash = [];
debug ? console.log(theView) : true;
if (theView) {
$(theView).find().not("relationship").each(function(e) {
theProps = e.prop();
for (var i=0; i<theProps.length; i++) {
if (propertyHash[theProps[i]]) {
propertyHash[theProps[i]][[e.prop(theProps[i])]]=1;
}
else
{
newArray = [];
newArray[e.prop(theProps[i])]=1;
propertyHash[theProps[i]]=newArray;
}
}
})
}
debug ? console.log(propertyHash):true;
var propertyList ="";
var i=1;
var menuArray=[];
for (var property in propertyHash){
propertyList +="("+i+") "+property+"\n";
debug ? console.log(propertyHash[property]):true;
menuArray.push(property);
i++;
}
debug ? console.log(propertyList):true;
if (theView) {
var theProperty = "-1";
while (true) {
if (menuArray.length<=buttonLimit) {
theProperty = buttonDialog("Please select a Property",menuArray);
}
else {
theProperty = window.prompt("Please select a Property by number:\n"+propertyList, "");
}
if (theProperty===null || theProperty.trim()=="" || (parseInt(theProperty.trim())>0 && parseInt(theProperty.trim())<=menuArray.length)) {
break;
}
}
if (theProperty!=null && theProperty!="") {
var valueList ="";
var valueArray = [];
var i=1;
for (var value in propertyHash[menuArray[theProperty-1]]) {
valueList += "("+i+") "+value+"\n";
valueArray.push(value);
i++;
}
var colourArray = [];
while (true) {
numberOfColours = window.prompt("Please enter number of colors required:\n(The following values are present):\n"+valueList, valueArray.length);
if (numberOfColours===null || numberOfColours.trim()=="" || (parseInt(numberOfColours.trim())>0 && parseInt(numberOfColours.trim())<=valueArray.length)) {
break;
}
}
for (i=0; i<numberOfColours;i++){
while (true) {
var colourPair = {};
if (valueArray.length<=buttonLimit) {
colourPair.value = buttonDialog("Please select a Property value for Color number "+(i+1), valueArray);
}
else {
colourPair.value = window.prompt("Please select a Property value for Color number "+(i+1)+":\n"+valueList, "");
}
colourPair.colour = colorDialog();
if (colourPair.value===null || colourPair.value.trim()=="" || (parseInt(colourPair.value.trim())>0 && parseInt(colourPair.value.trim())<=valueArray.length)) {
break;
}
}
colourArray[i]=colourPair;
}
var defaultColours = window.confirm("Do you wish to reset non-matching components to default colors?");
$(theView).find().not("relationship").not("diagram-model-group").not("diagram-model-note").each(function(e) {
var found = false;
debug ? console.log(e.name) : true;
var isProperty = e.prop(menuArray[theProperty-1]);
debug ? console.log(isProperty) : true;
for (var pair in colourArray) {
debug ? console.log(colourArray[pair]) : true;
if (isProperty == valueArray[colourArray[pair].value-1]) {
debug ? console.log("Found") : true;
e.fillColor = colourArray[pair].colour;
found = true;
}
}
if (!found && defaultColours) {
e.fillColor = null;
}
})
var oldKey = $(theView).find(".Key").filter("diagram-model-group").delete();
var theKey = theView.createObject("diagram-model-group", 12,-36-(36*colourArray.length), 240, 24+(36*colourArray.length));
theKey.borderType="1";
theKey.name = "Key";
for (var pair in colourArray) {
var aKey = theKey.createObject("diagram-model-note", 12, 24+(pair*36), 216, 24);
debug ? console.log(pair) : true;
debug ? console.log(colourArray[pair].colour) : true;
aKey.fillColor = colourArray[pair].colour;
aKey.borderType="1";
aKey.text = menuArray[theProperty-1] + "=" +valueArray[colourArray[pair].value-1];
}
}
} else {
console.error("> Please select a view.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment