HTMLcopy
1
<button onclick="addPoint()">Add Point</button>
2
<div id="container"></div>
CSScopy
15
1
html, body {
2
width: 100%;
3
height: 100%;
4
margin: 0;
5
padding: 0;
6
}
7
button {
8
margin: 10px 0 0 10px;
9
}
10
#container {
11
position: absolute;
12
width: 100%;
13
top: 35px;
14
bottom: 0;
15
}
JavaScriptcopy
x
1
anychart.onDocumentReady(function () {
2
3
// data
4
data = anychart.data.set([
5
{x: "P1", value: 10},
6
{x: "P2", value: 2},
7
{x: "P3", value: 15},
8
{x: "P4", value: 10}
9
]);
10
11
// set chart type
12
var chart = anychart.column();
13
14
chart.title().text("Click on Button to Add a Point");
15
16
// set data
17
chart.column(data);
18
19
// set container and draw chart
20
chart.container("container").draw();
21
});
22
23
// add point function
24
function addPoint() {
25
var view = data.mapAs();
26
27
// set default color for each point
28
for (var i = 0;i<view.getRowsCount();i++)
29
view.set(i,"fill", undefined);
30
31
// get random position in range of chart length
32
var randomIndex = Math.floor((Math.random() * (view.getRowsCount())) + 1);
33
34
// insert data
35
data.insert(
36
{
37
x: "P" + ((randomIndex)+1), // set name
38
value : Math.floor((Math.random() * 100)+ 1), // set random value in range 1 - 100
39
fill: "red" // set red color to define inserted column from others
40
},
41
randomIndex // position for inserting column
42
);
43
44
// get all columns after inserted one and change x value
45
for(var indexCounter = randomIndex; indexCounter < view.getRowsCount(); indexCounter++){
46
view.set(
47
(indexCounter), // column index
48
"x", // parameter to adjust
49
"P" + (indexCounter+1) // set as new parameter
50
);
51
}
52
}