HTMLcopy
1
<select id="typeSelect" onclick="create()">
2
<option value="default" selected disabled>Annotation Type</option>
3
<option value="andrews-pitchfork">Andrews' Pitchfork</option>
4
<option value="ellipse">Ellipse</option>
5
<option value="fibonacci-arc">Fibonacci Arc</option>
6
<option value="fibonacci-fan">Fibonacci Fan</option>
7
<option value="fibonacci-retracement">Fibonacci Retracement</option>
8
<option value="fibonacci-timezones">Fibonacci Time Zones</option>
9
<option value="horizontal-line">Horizontal Line</option>
10
<option value="infinite-line">Infinite Line</option>
11
<option value="line">Line Segment</option>
12
<option value="marker">Marker</option>
13
<option value="ray">Ray</option>
14
<option value="rectangle">Rectangle</option>
15
<option value="trend-channel">Trend Channel</option>
16
<option value="triangle">Triangle</option>
17
<option value="vertical-line">Vertical Line</option>
18
</select>
19
<button onclick="removeAll()">Remove All</button>
20
<button onclick="save()">Save</button>
21
<button onclick="load()">Load</button>
22
<div id="container"></div>
CSScopy
18
1
html, body {
2
width: 100%;
3
height: 100%;
4
margin: 0;
5
padding: 0;
6
}
7
select {
8
margin: 10px 0 0 10px;
9
}
10
button {
11
margin: 10px 0 0 5px;
12
}
13
#container {
14
position: absolute;
15
width: 100%;
16
top: 35px;
17
bottom: 0;
18
}
JavaScriptcopy
x
1
// the list of annotations in JSON format
2
var annotationsAtServer = {"annotationsList":[{"enabled":true,"type":"ellipse","xAnchor":"2007-04-01","valueAnchor":34.73,"secondXAnchor":"2008-03-02","secondValueAnchor":26.03}]};
3
4
anychart.onDocumentReady(function () {
5
6
// the data used in this sample can be obtained from the CDN
7
// https://cdn.anychart.com/csv-data/csco-daily.js
8
// create a data table using this data
9
var dataTable = anychart.data.table();
10
dataTable.addData(get_csco_daily_short_data());
11
12
// map the data
13
var mapping = dataTable.mapAs({"value": 4});
14
15
// create a stock chart
16
var chart = anychart.stock();
17
18
// create a plot on the chart
19
plot = chart.plot(0);
20
21
// create a line series
22
plot.line(mapping);
23
24
// load an annotation
25
plot.annotations().fromJson(annotationsAtServer);
26
27
// set the chart title
28
chart.title("Serializing and Deserialising Annotations");
29
30
// set the container id
31
chart.container("container");
32
33
// initiate drawing the chart
34
chart.draw();
35
36
// reset the select list to the first option
37
chart.listen("annotationDrawingFinish", function(){
38
document.getElementById("typeSelect").value = "default";
39
});
40
});
41
42
// send annotations to the server
43
function sendAnnotationsToServer(data) {
44
/* here a variable for saving annotations is used,
45
but you can save them to a database, local storage, or server*/
46
annotationsAtServer = data;
47
}
48
49
// load annotations from the server
50
function getAnnotationsFromServer() {
51
/* here a variable for load annotations is used,
52
but you can load them from a database, local storage, or server*/
53
return annotationsAtServer;
54
}
55
56
// create annotations
57
function create(){
58
var select = document.getElementById("typeSelect");
59
plot.annotations().startDrawing(select.value);
60
}
61
62
// remove all annotations
63
function removeAll(){
64
plot.annotations().removeAllAnnotations();
65
}
66
67
// save annotations
68
function save(){
69
sendAnnotationsToServer(plot.annotations().toJson(true));
70
}
71
72
// load all saved annotations
73
function load(){
74
var annotations = getAnnotationsFromServer();
75
plot.annotations().fromJson(annotations);
76
}