How to add figure at particular location in draw2d? - javascript

I want to add label at start or end position of connection. But here I did not found locator except ManhattanMidpointLocator. So how can I do this? How can I place label at where on connection?
Please find my code as below,
draw2d.LabelConnection = function() {
draw2d.Connection.call(this);
this.sourcePort = null;
this.targetPort = null;
this.lineSegments = [];
this.setColor(new draw2d.Color(0, 255, 0));
this.setLineWidth(2);
var label = new draw2d.Label("Message");
label.setBackgroundColor(new draw2d.Color(230, 230, 250));
label.setBorder(new draw2d.LineBorder(1));
this.addFigure(label, new draw2d.Locator());
};
draw2d.LabelConnection.prototype = new draw2d.Connection();
draw2d.LabelConnection.prototype.type = "draw2d.LabelConnection";
The above code shows label at (0,0) position.
plz help me out.

Use:
Connection.getStartPoint() to determine the start poit of the connection instead
to retrieve all segments of the connection.
Greetings

I'm not sure about what you mean by "any location" but if you implement your own locator it's pretty straightforward.
Look at the code of the ManhattanMidpointLocator. In the relocate function you know everything about the connection on the canvas. From that you just invent a calculation for the position of the label.

Now working with Graphiti in place of Draw2D but the code for your locator should look as follow (didn't tested) :
draw2d.StartConnectionLocator=function(/*:draw2d.Connection*/ connection)
{
draw2d.ConnectionLocator.call(this,connection);
};
draw2d.StartConnectionLocator.prototype.relocate=function(/*:draw2d.Figure*/ target)
{
var conn = this.getConnection();
var points = conn.getPoints();
var index = Math.floor((points.getSize() -2) / 2);
if (points.getSize() <= index+1)
return;
var startPoint = points.get(0);
var myPosition = new draw2d.Point();
myPosition.x = startPoint.x +5;
myPosition.y = startPoint.y +5;
target.setPosition(myPosition.x,myPosition.y);
};

Related

Paint certain words in google docs - Google Apps Script

I have this code below:
var textToHighlight = 'Normal';
var highLightStyle = {};
highLightStyle[DocumentApp.Attribute.FOREGROUND_COLOR] = '#FFC0CB';
var paras = doc.getParagraphs();
var textLocation = {};
for (i=0; i<paras.lenght; i++) {
textLocation = paras[i].findText(textToHighlight);
if (textLocation != null && textLocation.getStartOffset() != -1) {
textLocation.getElement().setAttributes(textLocation.getStartOffset(), textLocation.getEndOffsetInclusive(), highLightStyle);
}
}
With it, I want to color all the words 'normal' that appear in my document, but when I run the code, nothing happens and it doesn't accuse any error, it compiles normally.
I tried this another code:
let pinkColor = "#FFC0CB"
let pinkElements = body.findText("Normal")
let elem = pinkElements.getElement().asText();
let t = elem.getText();
elem.setForegroundColor(t.indexOf('Normal'), t.indexOf('High')+3, pinkColor)
But with the code above it paints only the first word 'Normal' that it finds, the rest remains neutral.
Does anyone know what may be happening to both codes?
Does anyone know what may be happening to both codes?
Code 1:
You made a typo, lenght should be length.
Code 2:
See my answer below.
Explanation:
You need to iterate over all elements with the particular keyword.
To achieve that you need to follow these steps:
get the first found element:
pinkElement = body.findText(searchWord);
check if an element with searchWord exists
do some code for this element
assign a new element which is the next one you found before:
pinkElement = body.findText(searchWord, pinkElement);
repeat steps 1-4 until there is no other element:
while (pinkElement != null)
Solution:
function myFunction() {
let doc = DocumentApp.getActiveDocument();
let body = doc.getBody();
let pinkColor = "#FFC0CB";
let searchWord = "Normal";
let pinkElement = body.findText(searchWord);
while (pinkElement != null) {
let elem = pinkElement.getElement().asText();
let t = elem.getText();
elem.setForegroundColor(t.indexOf(searchWord), t.indexOf('High')+3, pinkColor);
pinkElement = body.findText(searchWord, pinkElement);
}
}
I'm surprised it is returning the first one. "length" is spelled wrong on this line:
for (i=0; i<paras.**lenght**; i++) {
See if changing it to ".length" fixes it. If not, there is a similar example in the Docs at Class Range you could use.

Condensing repetitive code with google scripts

Pretty new to all this but have a simple script to pull API info and put into google sheets. I want to pull the top 20 coins but I'm unsure of how to do it as a ??'function'?? to limit the amount of code required presently especially since only 'XXX' is basically changing. Thanks in advance
var urlBTC='https://api.binance.com/api/v3/klines?symbol=BTCUSDT&interval=1d&limit=2';
var responseBTC = UrlFetchApp.fetch(urlBTC,{'muteHttpExceptions': true});
var jsonBTC = responseBTC.getContentText();
var parseBTC = JSON.parse(jsonBTC);
sheetBTC.getRange(3,3).setValue(parseBTC[0][6]);
var sheetETH = sh.getSheetByName("ETH");
var urlETH='https://api.binance.com/api/v3/klines?symbol=ETHUSDT&interval=1d&limit=2';
var responseETH = UrlFetchApp.fetch(urlETH,{'muteHttpExceptions': true});
var jsonETH = responseETH.getContentText();
var parseETH = JSON.parse(jsonETH);
sheetETH.getRange(3,3).setValue(parseETH[0][6]);
}```
var coins = ['ETHUSDT','BTCUSDT']
function getCoin(){
coins.forEach(coin => {
let url = 'https://api.binance.com/api/v3/klines?symbol='+ coin
+ '&interval=1d&limit=2'
//do the other stuff
})
}
Hope this helps set you off in the right direction.
Basically we store the symbols as an array loop through and create a url for that, in the 'do something' put in your other code for handling the req. watch out for rate limits

amcharts - how to set lat/lon in mappolygon?

Basically, I just want to plot a circle, at a user defined lat/lon with a radius of X km.
So I realize looking through documentation, I can't change the lat/lons of the mappolygons that are already loaded through the geodata, but I guess I'm not quite sure how to create a new map polygon with a specified lat/lon or if I can even do that!
My code is set up so that everytime the user clicks somewhere a marker is plotted or replotted accordingly, the location of the marker is where I would like to plot the center of the circle. I get that using the getCircle function I need to do the whole km2deg for the third variable, but I'm more concerned with the location right now. Any help would be great!
https://codepen.io/kbreezy/pen/wLRrPq
below is my onclick method of the code.
chart.seriesContainer.events.on("hit", function(ev) {
var lp = $("#chartdiv").data("point");
var coords = chart.svgPointToGeo(ev.svgPoint);
if (lp) {
lp.latitude = coords.latitude;
lp.longitude = coords.longitude;
} else {
var marker = imageSeries.mapImages.create();
marker.latitude = coords.latitude;
marker.longitude = coords.longitude;
}
$("#chartdiv").data("point", marker);
var circleSeries = chart.series.push(new am4maps.MapPolygonSeries())
var circleTemplate = circleSeries.mapPolygons.template;
circleTemplate.fill = am4core.color("#bf7569");
circleTemplate.strokeOpacity = 1;
circleTemplate.fillOpacity = 0.75;
var polygon = circleSeries.mapPolygons.create();
var mapPolygon = polygonSeries.getPolygonById("DE");
mapPolygon.setPropertyValue('latitude', coords.latitude);
mapPolygon.setPropertyValue('longitude', coords.longitude);
polygon.latitude = coords.latitude;
polygon.longitude = coords.longitude;
polygon.multiPolygon = am4maps.getCircle(mapPolygon.visualLongitude, mapPolygon.visualLatitude, 5);
//polygon.multiPolygon = am4maps.getCircle(coords.latitude, coords.longitude, 5);
});
MapPolygon does not have some latitude/longitude where you could "move" it. It's multipolygon must contain coordinates of the area you want to show. So when you pass longitude/latitude to the getCircle method, it returns multiPolygon around that point.
I modified your pen where to show the concept: https://codepen.io/team/amcharts/pen/ydrLZw
polygon.multiPolygon = am4maps.getCircle(coords.longitude, coords.latitude, 5);

.remove() is not a function error in Photoshop CC 2017 ExtendScript Tool Kit

I am trying to modify original PSD and then delete the original one and only want to save as a new jpg. My code is working fine with this line:
activeDocument.close(SaveOptions.DONOTSAVECHANGES); // Close Original Image
But when I replace above line with this line:
psd.remove(); // I want to delete Original file
It giving me remove() is not a function error.
Here is the complete script. I have tired to read Photoshop JS Guide 2015 and also google this issue but I didn't find any answer.
var defaultRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;
if (documents.length >= 1){
var hres = 0;
var vres = 0;
var OldName = activeDocument.name.substring(0, activeDocument.name.indexOf('.'));
var CurrentFolder = activeDocument.path;
var psd = app.activeDocument;
hres = activeDocument.width;
vres = activeDocument.height;
activeDocument.selection.selectAll();
if (activeDocument.layers.length >1) {
activeDocument.selection.copy(true);
}
else{
if (activeDocument.layers.length =1) {
activeDocument.selection.copy(false);
}
}
psd.remove(); // I want to delete Original file
var newDoc = documents.add(hres, vres, 72, OldName, NewDocumentMode.RGB, DocumentFill.WHITE);
newDoc.paste();
jpgFile = new File(CurrentFolder + "/" + OldName+ ".jpg" );
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = 12;
newDoc.saveAs(jpgFile, jpgSaveOptions, true, Extension.LOWERCASE);
}
A little late to the thread, but hope this helps someone else.
The remove method expects a file location. You can accomplish removing the original file like this:
var activeDoc = app.activeDocument;
var docPath = new Folder(activeDoc.path);
var psd = new File(docPath + "/" + activeDoc.name);
...
psd.remove();
Edit: Meant to also include a link to this handy ESTK reference doc where I learned about working with the File object: http://estk.aenhancers.com/3%20-%20File%20System%20Access/file-object.html?highlight=delete
srcDoc.activeLayer.remove();
Will delete the active layer. There's no .remove() method to delete a file.

AmCharts: How do i check if graph is hidden?

I got ONE balloon only in my chart, and I want to check if not only the graph it belongs to is hidden, but if ANY graph is hidden. How do I check this attribute?
function adjustBalloonText(graphDataItem, graph){
var data = graphDataItem.dataContext;
var date = ""; //some kind of getDateInScrollbar here
console.log(graphDataItem);
var distance = data.distance; // some kind of getGraphsValue here
var duration = ""; // some kind of getGraphsValue here as well
return distance;
}
Please help me out.

Categories