HTMLcopy
1
<div id="container"></div>
CSScopy
6
1
html, body, #container {
2
width: 100%;
3
height: 100%;
4
margin: 0;
5
padding: 0;
6
}
JavaScriptcopy
x
1
anychart.onDocumentReady(function() {
2
3
// create a stage
4
stage = anychart.graphics.create("container");
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
chart = anychart.stock();
17
18
// create a line series
19
var series1 = chart.plot(0).line(mapping);
20
series1.name("Series 1")
21
22
// create an area series
23
var series2 = chart.plot(1).area(mapping);
24
series2.name("Series 2")
25
26
// an auxiliary variable for working with annotations on the first plot
27
var controller1 = chart.plot(0).annotations();
28
29
// an auxiliary variable for working with annotations on the second plot
30
var controller2 = chart.plot(1).annotations();
31
32
// create an Ellipse annotation
33
controller1.ellipse({
34
xAnchor: "2006-11-20",
35
valueAnchor: 35.41,
36
secondXAnchor: "2007-02-24",
37
secondValueAnchor: 18.05,
38
});
39
40
// create a Rectangle annotation
41
controller1.rectangle({
42
xAnchor: "2007-06-01",
43
valueAnchor: 35.41,
44
secondXAnchor: "2007-12-09",
45
secondValueAnchor: 18.05
46
});
47
48
// set the chart position and title
49
chart.top(15);
50
chart.title("Serializing and Deserializing (JSON)");
51
52
// set the chart container and initiate drawing the chart
53
chart.container(stage);
54
chart.draw();
55
56
// create a label for the Ellipse annotation
57
createLabel("Ellipse", 150, function() {
58
// start drawing the annotation on the first plot
59
controller1.startDrawing("ellipse");
60
});
61
62
// create a label for the Rectangle annotation
63
createLabel("Rectangle", 208, function() {
64
// start drawing the annotation on the first plot
65
controller1.startDrawing("rectangle");
66
});
67
68
// create a label for removing annotations
69
createLabel("Remove All", 312, function() {
70
// remove annotations from all plots
71
chart.annotations().removeAllAnnotations();
72
});
73
74
// create a label for serializing annotations from the first plot and deserializing them to the second one
75
createLabel("Copy to the Second Plot", 423, function() {
76
// serialize annotations from the first plot
77
var json = controller1.toJson();
78
// deserialize the annotations to the second plot
79
controller2.fromJson(json);
80
});
81
});
82
83
// a helper function for creating buttons
84
function createLabel(text, offset, action){
85
var label = anychart.standalones.label();
86
label.background({fill: "#1976d2"});
87
label.text(text);
88
label.fontColor("#fff");
89
label.padding(5);
90
label.parentBounds(offset, 5, 130, 100);
91
label.listen("click", action);
92
label.container(stage);
93
label.draw();
94
label.listen("mouseOver", function(){
95
label.background().fill("#1976d2 0.5");
96
document.body.style.cursor = "pointer";
97
label.draw();
98
});
99
label.listen("mouseOut", function(){
100
label.background().fill("#1976d2");
101
document.body.style.cursor = "auto";
102
label.draw();
103
});
104
return label;
105
};