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
// data
4
var dataSet = anychart.data.set([
5
{x: "P1", value: 100},
6
{x: "P2", value: 200},
7
{x: "P3", value: 15},
8
{x: "P4", value: 130},
9
{x: "P5", value: 153},
10
{x: "P6", value: 120},
11
{x: "P7", value: 151},
12
{x: "P8", value: 58},
13
{x: "P9", value: 19},
14
{x: "P10", value: 135},
15
{x: "P11", value: 170},
16
{x: "P12", value: 195},
17
{x: "P13", value: 22},
18
{x: "P14", value: 175},
19
{x: "P15", value: 120}
20
]);
21
22
// set chart type
23
var chart = anychart.line();
24
25
chart.title().text("Click on Chart to Add a Point ");
26
27
// set data
28
chart.spline(dataSet).markers(null);
29
30
chart.padding().right(80);
31
32
// disable stagger mode. Only one line for x axis labels
33
chart.xAxis().staggerMode(false);
34
35
// set container and draw chart
36
chart.container("container").draw();
37
38
// create new button
39
var streamButton = document.createElement("div");
40
41
// set button style
42
streamButton.style.top = 0;
43
streamButton.style.right = 0;
44
streamButton.style.width = "auto";
45
streamButton.style.height = "auto";
46
streamButton.style.backgroundColor = "#444444";
47
streamButton.style.color = "white";
48
streamButton.style.position = "absolute";
49
streamButton.style.zIndex = 2;
50
streamButton.style.transition = "0.5s";
51
streamButton.style.textAlign = "center";
52
streamButton.style.border = "3px solid #444444";
53
streamButton.style.borderRadius = "7px 7px 7px 7px";
54
streamButton.style.fontSize = "18px";
55
streamButton.style.MozUserSelect= "none";
56
streamButton.style.KhtmlUserSelect= "none";
57
streamButton.style.WebkitUserSelect= "none";
58
streamButton.style.userSelect= "none";
59
streamButton.innerHTML = "Start" + "\nstream";
60
streamButton.style.cursor = "pointer";
61
streamButton.onclick = function() {
62
startStream();
63
};
64
65
// append button to container
66
container.appendChild(streamButton);
67
68
// first index for new point
69
var indexSetter = (dataSet.mapAs().getRowsCount())+1;
70
71
function startStream() {
72
73
// adjust button content
74
streamButton.innerHTML = "Stop" + "\nstream";
75
76
// set interval of data stream
77
var myVar = setInterval(
78
79
// data streaming itself
80
function(e) {
81
82
// append data
83
dataSet.append({
84
85
// x value
86
x: "P" + indexSetter,
87
88
// random value from 1 to 500
89
value : Math.floor((Math.random() * 500)+ 1)
90
});
91
92
// removes first point
93
dataSet.remove(0);
94
indexSetter++;
95
}, 200 // interval
96
);
97
98
streamButton.onclick = function(){
99
100
// clears interval which stops streaming
101
clearInterval(myVar);
102
streamButton.onclick = function() {
103
startStream();
104
};
105
streamButton.innerHTML = "Start" + "\nstream";
106
};
107
}
108
});