How can I add an Annotation in dygraph? - javascript

I have the following code that displays a graph using dygraphs:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>title tbd - jsFiddle demo</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.4.4rc2.js'></script>
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<script type='text/javascript' src="http://dygraphs.com/dygraph-combined.js"></script>
<style type='text/css'>
</style>
<script type='text/javascript'>//<![CDATA[
var csvData = function() {
return "Date,Close Price\n" +
"2015-09-17,5.20\n" +
"2015-09-16,5.31\n" +
"2015-09-15,5.40\n" +
"2015-09-14,5.20\n" +
"2015-09-11,5.17\n" +
"2015-09-10,5.20\n" +
"2015-09-09,5.09\n" +
"2015-09-08,5.05\n" +
"2015-09-07,4.90\n" +
"2015-09-04,4.74\n" +
"2015-09-03,4.68\n" +
"2015-09-02,4.24\n" +
"2015-09-01,4.20\n" +
"2015-08-31,4.06\n" +
"2015-08-28,4.19\n" +
"2015-08-27,4.08\n" +
"2015-08-26,4.20\n" +
"2015-08-25,4.04\n" +
"2015-08-24,3.53\n" +
"2015-08-21,4.30\n" +
"2015-08-20,4.45\n" +
"2015-08-19,4.58\n" +
"2015-08-18,4.54\n" +
"2015-08-17,4.63\n" +
"2015-08-14,4.49\n" +
"2015-08-13,4.56\n" +
"2015-08-12,4.69\n" +
"2015-08-11,4.80\n" +
"2015-08-10,4.65\n" +
"2015-08-07,4.40\n" +
"2015-08-06,4.24\n" +
"2015-08-05,3.98\n" +
"2015-08-04,3.84\n" +
"2015-08-03,3.66\n" +
"2015-07-31,4.71\n" +
"2015-07-30,4.71\n" +
"2015-07-29,4.71\n" +
"2015-07-28,4.71\n" +
"2015-07-27,4.71\n" +
"2015-07-24,4.71\n" +
"2015-07-23,4.71\n" +
"2015-07-22,4.71\n" +
"2015-07-21,4.71\n" +
"2015-07-20,4.71\n" +
"2015-07-17,4.71\n" +
"2015-07-16,4.71\n" +
"2015-07-15,4.71\n" +
"2015-07-14,4.71\n" +
"2015-07-13,4.71\n" +
"2015-07-10,4.71\n" +
"2015-07-09,4.71\n" +
"2015-07-08,4.71\n" +
"2015-07-07,4.71\n" +
"2015-07-06,4.71\n" +
"2015-07-03,4.71\n" +
"2015-07-02,4.71\n" +
"2015-07-01,4.71\n" +
"2015-06-30,4.71\n" +
"2015-06-29,4.71\n" +
"2015-06-26,4.71\n" +
"2015-06-25,4.45\n" +
"2015-06-24,4.43\n" +
"2015-06-23,4.41\n" +
"2015-06-22,4.14\n" +
"2015-06-19,3.84\n" +
"2015-06-18,3.65\n" +
"2015-06-17,3.63\n" +
"2015-06-16,4.23\n" +
"2015-06-15,4.49\n" +
"2015-06-12,4.74\n" +
"2015-06-11,5.04\n" +
"2015-06-10,4.75\n" +
"2015-06-09,4.75\n" +
"2015-06-08,4.61\n" +
"2015-06-05,4.69\n" +
"2015-06-04,4.91\n" +
"2015-06-03,4.94\n" +
"2015-06-02,4.68\n" +
"2015-06-01,4.56\n" +
"2015-05-29,4.56\n" +
"2015-05-28,4.96\n" +
"2015-05-27,5.29\n" +
"2015-05-26,4.75\n" +
"2015-05-25,4.88\n" +
"2015-05-22,5.06\n" +
"2015-05-21,5.18\n" +
"2015-05-20,5.07\n" +
"2015-05-19,5.28\n" +
"2015-05-18,5.26\n" +
"2015-05-15,4.95\n" +
"2015-05-14,5.00\n" +
"2015-05-13,5.18\n" +
"2015-05-12,5.49\n" +
"2015-05-11,5.50\n" +
"2015-05-08,5.74\n" +
"2015-05-07,5.84\n" +
"2015-05-06,5.60\n" +
"2015-05-05,5.28\n" +
"2015-05-04,5.80\n" +
"2015-05-01,5.99\n" +
"2015-04-30,5.99\n" +
"2015-04-29,5.46\n" +
"2015-04-28,5.58\n" +
"2015-04-27,5.45\n" +
"2015-04-24,4.97\n" +
"2015-04-23,4.87\n" +
"2015-04-22,4.57\n" +
"2015-04-21,4.30\n" +
"2015-04-20,4.85\n" +
"2015-04-17,4.86\n" +
"2015-04-16,5.09\n" +
"2015-04-15,4.80\n" +
"2015-04-14,5.00\n" +
"2015-04-13,5.45\n" +
"2015-04-10,5.45\n" +
"2015-04-09,5.45\n" +
"2015-04-08,5.38\n" +
"2015-04-07,5.42\n" +
"2015-04-06,5.50\n" +
"2015-04-03,5.50\n" +
"2015-04-02,5.50\n" +
"2015-04-01,5.49\n" +
"2015-03-31,5.70\n" +
"2015-03-30,5.80\n" +
"2015-03-27,5.68\n" +
"2015-03-26,5.40\n" +
"2015-03-25,5.83\n" +
"2015-03-24,5.83\n" +
"2015-03-23,5.30\n" +
"2015-03-20,5.01\n" +
"2015-03-19,4.84\n" +
"2015-03-18,4.90\n" +
"2015-03-17,5.13\n" +
"2015-03-16,4.91\n" +
"2015-03-13,5.18\n" +
"2015-03-12,5.53\n" +
"2015-03-11,5.58\n" +
"2015-03-10,5.70\n" +
"2015-03-09,5.60\n" +
"2015-03-06,6.15\n" +
"2015-03-05,6.19\n" +
"2015-03-04,6.40\n" +
"2015-03-03,6.32\n" +
"2015-03-02,6.51\n" +
"2015-02-27,7.00\n" +
"2015-02-26,7.30\n" +
"2015-02-25,7.50\n" +
"2015-02-24,6.88\n" +
"2015-02-23,6.05\n" +
"2015-02-20,6.05\n" +
"2015-02-19,6.09\n" +
"2015-02-18,5.90\n" +
"2015-02-17,5.98\n" +
"2015-02-16,6.00\n" +
"2015-02-13,6.20\n" +
"2015-02-12,5.54\n" +
"2015-02-11,5.25\n" +
"2015-02-10,5.64\n" +
"2015-02-09,5.18\n" +
"2015-02-06,5.15\n" +
"2015-02-05,5.27\n" +
"2015-02-04,5.50\n" +
"2015-02-03,5.69\n" +
"2015-02-02,5.12\n" +
"2015-01-30,4.80\n" +
"2015-01-29,4.78\n" +
"2015-01-28,4.45\n" +
"2015-01-27,5.17\n" +
"2015-01-26,5.82\n" +
"2015-01-23,5.63\n" +
"2015-01-22,5.16\n" +
"2015-01-21,4.94\n" +
"2015-01-20,5.18\n" +
"2015-01-19,5.20\n" +
"2015-01-16,4.77\n" +
"2015-01-15,4.90\n" +
"2015-01-14,5.03\n" +
"2015-01-13,5.31\n" +
"2015-01-12,5.20\n" +
"2015-01-09,4.93\n" +
"2015-01-08,4.76\n" +
"2015-01-07,4.97\n" +
"2015-01-06,5.07\n" +
"2015-01-05,5.07\n" +
"2015-01-02,5.55\n" +
"2015-01-01,5.40\n" +
"2015-10-28,10";
};
$(document).ready(function () {
// Get your data from somewhere
var data = csvData();
// Set highlight start and end
var highlight_start = new Date('2015/09/17');
var highlight_end = new Date('2015/10/28');
g = new Dygraph(
document.getElementById("graphdiv"),
data,
{
animatedZooms: true,
underlayCallback: function(canvas, area, g) {
var bottom_left = g.toDomCoords(highlight_start,0);
var top_right = g.toDomCoords(highlight_end,2500);
console.log(bottom_left);
console.log(top_right);
var left = bottom_left[0];
var right = top_right[0];
canvas.fillStyle = "rgba(255, 255, 102, 1.0)";
canvas.fillRect(left, area.y, right - left, area.h);
}
}
);
g.setAnnotations([
{
series: "Temperature",
x: "2015-05-08",
shortText: "L",
text: "Coldest Day"
}
]);
});
</script>
</head>
<body>
<div id="graphdiv" style="width:600px; height:300px;"></div>
<div id="div_g" style="width:600px; height:300px;"></div>
<p>When you zoom and pan, the region remains highlighted.</p>
</body>
</html>
I tried to add an annotation like described in http://dygraphs.com/annotations.html:
g.setAnnotations([
{
series: "Temperature",
x: "2015-05-08",
shortText: "L",
text: "Coldest Day"
}
]);
But the annotation doesn't show up. What am I doing wrong?

Change series to one of your column names, e.g. Close Price:
g.setAnnotations([
{
series: "Close Price",
x: "2015-05-08",
shortText: "L",
text: "Coldest Day"
}
]);
See example here:
http://codepen.io/Dragory/pen/QjEpEK

Related

In jquery run time dynamic value not able to get it

I am creating the complete syntax of jquery and assigning it in the PHP field and on the button click I get the field value but the problem is in jquery the syntax I used to replace their value is not replaced. It shows the same variable.
<input type="hidden" name="ajaxUrl" id="ajaxUrl" value="'index.php?mode=ajax&action=modHistory&process=save&account={$account}&date=' + uenc(readNextdate('dateField')) + '&response=' + uenc(fieldVal('response')) + '&contact=' + uenc(fieldVal('contact')) + '&salesman=' + uenc(fieldVal('salesman')) + '&activity=' + uenc(fieldVal('activity')) + '&project=' + uenc(fieldVal('project')) + '&owner=' + uenc(fieldVal('owner')) + '&status=' + uenc(fieldVal('modstatus')) + '&product=' + uenc(fieldVal('product')) + '&duration=' + uenc(fieldVal('duration')) + '&username=' + uenc(fieldVal('username')) + '&oldProduct=' + uenc(fieldVal('oldProduct')) + '&oldDuration=' + uenc(fieldVal('oldDuration')) + '&oldActivity=' + uenc(fieldVal('oldActivity')) + '&oldDate={$pointer}{$save}&extraId={$extraId}&time=' + new Date().getTime() + '&token={$XSRFToken|escape:'url'}'">
At the jquery end I suppose it will replace the jquery syntax with there value but it shows the same value -
'index.php?mode=ajax&action=modHistory&process=save&account=10002&date=' + uenc(readNextdate('dateField')) + '&response=' + uenc(fieldVal('response')) + '&contact=' + uenc(fieldVal('contact')) + '&salesman=' + uenc(fieldVal('salesman')) + '&activity=' + uenc(fieldVal('activity')) + '&project=' + uenc(fieldVal('project')) + '&owner=' + uenc(fieldVal('owner')) + '&status=' + uenc(fieldVal('modstatus')) + '&product=' + uenc(fieldVal('product')) + '&duration=' + uenc(fieldVal('duration')) + '&username=' + uenc(fieldVal('username')) + '&oldProduct=' + uenc(fieldVal('oldProduct')) + '&oldDuration=' + uenc(fieldVal('oldDuration')) + '&oldActivity=' + uenc(fieldVal('oldActivity')) + '&oldDate=1638172792&FIELD1='+uenc(readNextdate('mod-RCMANL-ANAL01'))+'&ADDN-FIELD1=RCMANL-ANAL01&FIELD2='+uenc(jqfieldVal('.RCMANL-ANAL15 select'))+'&ADDN-FIELD2=RCMANL-ANAL15&FIELD3='+uenc(jqfieldVal('.RCMANL-ANAL13 input'))+'&ADDN-FIELD3=RCMANL-ANAL13&FIELD4='+uenc(readNextdate('mod-RCMANL-ANAL51'))+'&ADDN-FIELD4=RCMANL-ANAL51&FIELD5='+uenc(readNextdate('mod-RCMANL-ANAL80'))+'&ADDN-FIELD5=RCMANL-ANAL80&extraId=&time=' + new Date().getTime() + '&token=2d62814f617129a0a611dd505ec239a2'

Merge every two elements into 1 element in javascript array

Suppose I have the following array in JavaScript:
var dataArray= [
'name1\n' +
'\n' +
'name2\n' +
'name3',
' \n' +
'name4\n' +
'name5\n' +
'\n' +
'name6\n' +
'name7\n' +
'\n' +
'name8',
'name9\n' +
'\n' +
'name10\n' +
'name11',
' \n' +
'name12\n' +
'name13\n' +
'\n' +
'name14\n' +
'name15',
'name16\n' +
'\n' +
'name17\n' +
'name18',
' \n' +
'name19\n' +
'name20\n' +
'\n' +
'name21\n' +
'name22\n' +
'\n' +
'name23',
'name24\n' +
'\n' +
'name25\n' +
'name26',
' \n' +
'name27\n' +
'name28\n' +
'\n' +
'name29\n' +
'name30',
]
How can I write code to merge every two elements into one element? So that each pair, is only separated by 1 comma?
I'm looking to get the following output:
var dataArray= [
'name1\n' +
'\n' +
'name2\n' +
'name3'
' \n' +
'name4\n' +
'name5\n' +
'\n' +
'name6\n' +
'name7\n' +
'\n' +
'name8',
'name9\n' +
'\n' +
'name10\n' +
'name11'
' \n' +
'name12\n' +
'name13\n' +
'\n' +
'name14\n' +
'name15',
'name16\n' +
'\n' +
'name17\n' +
'name18'
' \n' +
'name19\n' +
'name20\n' +
'\n' +
'name21\n' +
'name22\n' +
'\n' +
'name23',
'name24\n' +
'\n' +
'name25\n' +
'name26',
' \n' +
'name27\n' +
'name28\n' +
'\n' +
'name29\n' +
'name30',
]
I want it to be like the above, whereby every 2 elements are amalgamated.
You can use Array.from, assuming that the length of the array is even.
const res = Array.from({length:dataArray.length/2}, (_,i)=>dataArray[2*i]+dataArray[2*i+1]);
var dataArray= [
'name1\n' +
'\n' +
'name2\n' +
'name3',
' \n' +
'name4\n' +
'name5\n' +
'\n' +
'name6\n' +
'name7\n' +
'\n' +
'name8',
'name9\n' +
'\n' +
'name10\n' +
'name11',
' \n' +
'name12\n' +
'name13\n' +
'\n' +
'name14\n' +
'name15',
'name16\n' +
'\n' +
'name17\n' +
'name18',
' \n' +
'name19\n' +
'name20\n' +
'\n' +
'name21\n' +
'name22\n' +
'\n' +
'name23',
'name24\n' +
'\n' +
'name25\n' +
'name26',
' \n' +
'name27\n' +
'name28\n' +
'\n' +
'name29\n' +
'name30',
];
const res = Array.from({length:dataArray.length/2}, (_,i)=>dataArray[2*i]+dataArray[2*i+1]);
console.log(res);

How do I replace a string in a parent page from a JS created childpage?

I am creating a dynamic list with JS
var obj = JSON.parse(Agent212);
var i;
var serie = "Agent212";
for (i = 0; i < obj.strips.length; i++) {
if (obj.strips[i].Collectie == "0") {
document.write("<li><a onclick='newPage(" + obj.strips[i].Nummer + "," + serie + ")' style = 'color:orange'>" + obj.strips[i].Nummer + " - " + obj.strips[i].Titel + "</a></li>");
}
else if (obj.strips[i].Collectie == "2") {
document.write("<li><a onclick='newPage(" + obj.strips[i].Nummer + "," + serie + ")' style = 'color:red'>" + obj.strips[i].Nummer + " - " + obj.strips[i].Titel + "</a></li>");
}
else {
document.write("<li><a class='ToHide' onclick='newPage(" + obj.strips[i].Nummer + "," + serie + ")'><strike>" + obj.strips[i].Nummer + " - " + obj.strips[i].Titel + "</strike></a></li>");
}
}
when I click on an item in the list, I create a childpage
function newPage(StripNum, SerieNaam) {
var obj = JSON.parse(SerieNaam);
var i;
var x = StripNum;
for (i = 0; i < obj.strips.length; i++) {
if (obj.strips[i].Nummer == x) {
if (obj.strips[i].Collectie == 0) {
var myWindow = window.open();
myWindow.document.writeln('<html>');
myWindow.document.writeln('<head>');
myWindow.document.writeln('<meta name="viewport" content="width=device-width, initial-scale=1">');
myWindow.document.writeln('<link rel="stylesheet" href="./css/W3CSS.css">');
myWindow.document.writeln('<scr' + 'ipt>');
myWindow.document.writeln('function windowClose(){');
myWindow.document.writeln('window.close();}'); **
myWindow.document.writeln('function ToevoegenVerzameling(){'); **
myWindow.document.writeln('window.alert("toegevoegd aan verzameling");');
myWindow.document.writeln('window.close();');
myWindow.document.writeln('}');
myWindow.document.writeln('</scr' + 'ipt>');
myWindow.document.writeln('</head>');
myWindow.document.writeln('<body>');
myWindow.document.writeln('<div class="w3-container w3-display-topmiddle">');
myWindow.document.writeln('<div class="w3-card-16 w3-round-xlarge" style="width:100%">');
myWindow.document.writeln('<a onclick="windowClose();"><center><img src="' + obj.strips[i].Link + obj.strips[i].Nummer + '.jpg"></center></a>');
myWindow.document.writeln('<div class="w3-container w3-margin-top">');
myWindow.document.writeln('<center><B>Nummer: </B><I>' + obj.strips[i].Nummer + '</I></center>');
myWindow.document.writeln('<center><B>Titel: </B><I>' + obj.strips[i].Titel + '</I></center>');
myWindow.document.writeln('<center><button onclick="ToevoegenVerzameling()" class="w3-btn w3-light-grey w3-border w3-border-black w3-round-xlarge w3-margin-top w3-margin-bottom" style="width:100%"><h2><b>Toevoegen</b></h2></button></center>');
myWindow.document.writeln('</div>');
myWindow.document.writeln('</div>');
myWindow.document.writeln('</div>');
myWindow.document.writeln('</body>');
myWindow.document.writeln('</html>');
}
all of the above does work, but know I want to ad a replace function to ToevoegenVerzameling().
I've tried with adding this piece of code.
myWindow.document.writeln('if (window.opener != null && !window.opener.closed){');
myWindow.document.writeln('var str = "<li><a onclick='
newPage(" + obj.strips[i].Nummer + ", " + serie + ")
' style = '
color: orange '>" + obj.strips[i].Nummer + " - " + obj.strips[i].Titel +"</a></li>";');
myWindow.document.writeln('str.replace("<a onclick='
newPage(", " < a class = 'ToHide'
onclick = 'newPage(");'); myWindow.document.writeln('str.replace("style = '
color: orange '>", "><\strike>");'); myWindow.document.writeln('str.replace("</a>", "</strike></a>");}');
But I can't get it to work, Were am I making a mistake in this?

TypeError: $source.offset is not a function

I have a tree structure where the elements can be dragged and dropped into another element. My objective is when a element or node from a tree is dragged and dropped to the external element I want a line to be drawn from the element of the tree to the external location.
I have the code separate for dragging and dropping. I tried to integrate the code into my tree structure and getting this error. I did see similar errors but could not get an exact idea about what exactly the error is. Here is the code that is giving me the error.
end: function(item, hover, placeholder, helper) {
if (placeholder.parent().length) {
var tree = $('#tree1').aciTree('api');
if (tree.isItem(item)) {
var id = tree.getId(item);
var label = tree.getLabel(item);
if (this._instance.jQuery.hasClass('any')) {
this._instance.jQuery.find('li:not(.aciSortablePlaceholder)').remove();
}
var item = $('<li id="item-' + id + '">' + label + '</li>');
var sourceId = id;
var destinationId = sourceId;
drawLine(sourceId, destinationId);
//This is how the function is called
placeholder.after(item).detach();
}
else {
placeholder.detach();
}
}
helper.detach();
}});
function drawLine(eTarget, eSource) {
setTimeout(function () {
var $source = eSource;
var $target = eTarget;
var originX = $source.offset().left + $source.width() + 20 + 4;
// In the $source.offset() I am getting the error.
var originY = $source.offset().top + (($source.height() + 20 + 4) / 2);
var endingX = $target.offset().left;
var endingY = $target.offset().top + (($target.height() + 20 + 4) / 2);
var space = 20;
var color = "black";
var a = "M" + originX + " " + originY + " L" + (originX + space) + " " + originY; // beginning
var b = "M" + (originX + space) + " " + originY + " L" + (endingX - space) + " " + endingY; // diagonal line
var c = "M" + (endingX - space) + " " + endingY + " L" + endingX + " " + endingY; // ending
var all = a + " " + b + " " + c;
console.log("New Line ----------------------------");
console.log("originX: " + originX + " | originY: " + originY + " | endingX: " + endingX + " | endingY: " + endingY + " | space: " + space + " | color: " + color );
console.log(all);
myLines[myLines.length] = svg
.path(all)
.attr({
"stroke": color,
"stroke-width": 1,
"stroke-dasharray": "-"
});
}, 1000);
PS: I am using firefox. The eTarget and eSource and getting the values as supplied.

JS styling 3 per line

I'm trying to make this so there is 3 on a line then a break then 3 more then a break etc..
Any help?
Code so far http://jsfiddle.net/82wNq/69/
$.getJSON("https://api.twitch.tv/kraken/search/streams?q=dayz&limit=15&type=suggest&callback=?", function (e) {
var t = "";
$.each(e.streams, function (e, n) {
t = t + "<div class='panel-heading'><h3 class='panel-title'></h3><div class='content1'><center><img src='" + n.channel.logo + "' width='33' height='30'/><b> <a href='http://twitch.tv/" + n.channel.name + "'>" + n.channel.display_name + " - " + n.viewers + "</b></a></center></div></div><div class='panel-body'><div class='content3'><center><a href='http://twitch.tv/" + n.channel.display_name + "'><img src='" + n.preview.medium + "' width='400' height='222'/></a></center></div></div>"
});
$("#content").html(t)
})
Example:
http://i.stack.imgur.com/h0xvX.gif
DEMO
Create 3 different content divs instead of 1
<div id="content1" class="panel panel-default"></div>
<div id="content2" class="panel panel-default"></div>
<div id="content3" class="panel panel-default"></div>
Style these divs
.panel.panel-default {
width:430px;
display:inline-block;
}
Distribute the results accordingly
$.getJSON("https://api.twitch.tv/kraken/search/streams?q=dayz&limit=15&type=suggest&callback=?", function (e) {
var t = u = v = "";
$.each(e.streams, function (e, n) {
switch ((e + 1) % 3) {
case 1:
t = t + "<div class='panel-heading'><h3 class='panel-title'></h3><div class='content1'><center><img src='" + n.channel.logo + "' width='33' height='30'/><b> <a href='http://twitch.tv/" + n.channel.name + "'>" + n.channel.display_name + " - " + n.viewers + "</b></a></center></div></div><div class='panel-body'><div class='content3'><center><a href='http://twitch.tv/" + n.channel.display_name + "'><img src='" + n.preview.medium + "' width='400' height='222'/></a></center></div></div>";
break;
case 2:
u = u + "<div class='panel-heading'><h3 class='panel-title'></h3><div class='content1'><center><img src='" + n.channel.logo + "' width='33' height='30'/><b> <a href='http://twitch.tv/" + n.channel.name + "'>" + n.channel.display_name + " - " + n.viewers + "</b></a></center></div></div><div class='panel-body'><div class='content3'><center><a href='http://twitch.tv/" + n.channel.display_name + "'><img src='" + n.preview.medium + "' width='400' height='222'/></a></center></div></div>";
break;
case 3:
default:
v = v + "<div class='panel-heading'><h3 class='panel-title'></h3><div class='content1'><center><img src='" + n.channel.logo + "' width='33' height='30'/><b> <a href='http://twitch.tv/" + n.channel.name + "'>" + n.channel.display_name + " - " + n.viewers + "</b></a></center></div></div><div class='panel-body'><div class='content3'><center><a href='http://twitch.tv/" + n.channel.display_name + "'><img src='" + n.preview.medium + "' width='400' height='222'/></a></center></div></div>";
break;
}
});
$("#content1").html(t);
$("#content2").html(u);
$("#content3").html(v);
})

Categories

Resources