Last active
April 29, 2017 11:13
-
-
Save netsi1964/523a837385f0488498678a6fb5ea3ba2 to your computer and use it in GitHub Desktop.
A SKETCH basic graph user plug-in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sketch = context.api(); | |
var document = sketch.selectedDocument; | |
var selection = document.selectedLayers; | |
var page = document.selectedPage; | |
var values = sketch.getStringFromUser("Enter values", "10,30,10,22,50,10,30"); | |
var canvasWidth = parseFloat(sketch.getStringFromUser("Canvas width", "300")); | |
var canvasHeight = parseFloat(sketch.getStringFromUser("Canvas height", "300")); | |
var graphValues = values.split(","); | |
var min = 1000000, max = -100000, width = canvasWidth / graphValues.length; | |
function getX(i) { | |
return i * width; | |
} | |
function getY(value) { | |
return canvasHeight - getHeight(value); | |
} | |
function getHeight(value) { | |
var height = value / max * canvasHeight; | |
return height; | |
} | |
graphValues.map(function(val, i) { | |
val = parseFloat(val); | |
min = min > val ? val : min; | |
max = max < val ? val : max; | |
}); | |
var groupWidth = graphValues.length * width; | |
var group = page.newGroup({ | |
frame: new sketch.Rectangle(0, 0, canvasWidth, canvasHeight), | |
name: "Graph (" + | |
canvasWidth + | |
"x" + | |
canvasHeight + | |
" - " + | |
graphValues.length + | |
" values from " + | |
min + | |
" to " + | |
max + | |
")" | |
}); | |
var reats = []; | |
graphValues.map(function(val, i) { | |
var x = i * width; | |
var rect = group.newShape({ | |
frame: new sketch.Rectangle(getX(i), getY(val), width, getHeight(val)) | |
}); | |
}); | |
group.select(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example of generated graph.