HTMLcopy
1
<div id="tablePlaceholder"></div>
2
<div id="container"></div>
CSScopy
27
1
html, body {
2
width: 100%;
3
height: 100%;
4
margin: 0;
5
padding: 0;
6
}
7
table {
8
9
}
10
caption {
11
font-weight: 600;
12
padding-bottom: 10px;
13
}
14
#container {
15
position: absolute;
16
width: 60%;
17
top: 0;
18
bottom: 0;
19
}
20
#tableInfo {
21
position: absolute;
22
width: 40%;
23
top: 22px;
24
right: 0;
25
bottom: 0;
26
color: #990000;
27
}
JavaScriptcopy
x
1
anychart.onDocumentReady(function () {
2
3
// create a data table
4
var dataTable = anychart.data.table(0);
5
6
// add data
7
dataTable.addData([
8
["2015-12-25", 505.69, 514.88],
9
["2015-12-26", 505.59, 514.98],
10
["2015-12-27", 505.49, 515.30],
11
["2015-12-28", 505.23, 515.72],
12
["2015-12-29", 505.38, 515.86],
13
["2015-12-30", 505.66, 515.98],
14
["2015-12-31", 505.99, 515.33],
15
["2016-01-01", 505.99, 514.29],
16
["2016-01-02", 506.18, 514.87]
17
]);
18
19
// map the data
20
mapping = dataTable.mapAs({low: 1, high: 2});
21
22
// create a stock chart
23
chart = anychart.stock();
24
25
// create a plot and a range area series
26
var ohlcSeries = chart.plot(0).rangeArea(mapping);
27
ohlcSeries.name("ACME Corp.");
28
29
// set the chart title
30
chart.title("Table Data: Iterating");
31
32
// set the range of points shown on the chart
33
chart.selectRange("2015-12-26", "2015-12-30");
34
35
// display information about shown points
36
showSelected();
37
38
/* listen to changing the shown range of points
39
and update the displayed information */
40
chart.listen("selectedRangeChange", function(){
41
showSelected();
42
});
43
44
// set the container id
45
chart.container("container");
46
47
// initiate drawing the chart
48
chart.draw();
49
});
50
51
// display information about shown points
52
function showSelected() {
53
54
// create a table for displaying information
55
var tablePlaceholder = document.getElementById("tablePlaceholder");
56
tablePlaceholder.innerHTML = "<table id='tableInfo'>" +
57
"<caption>Shown Points</caption>" +
58
"<tr><td>Date</td><td>Low</td>" +
59
"<td>High</td></tr></table>";
60
61
// get the shown range of points
62
var range = chart.getSelectedRange();
63
64
/* create the selectable object
65
and select rows corresponding to the shown points */
66
selectable = mapping.createSelectable();
67
selectable.select(range.firstSelected, range.lastSelected);
68
69
// get the iterator
70
var iterator = selectable.getIterator();
71
72
// get the table for displaying information
73
var tableInfo = document.getElementById("tableInfo");
74
75
// display information about shown points in the table
76
while (iterator.advance()) {
77
var key = iterator.getKey();
78
var date = anychart.format.dateTime(key, "dd.MM.yyyy");
79
var low = iterator.get("low");
80
var high = iterator.get("high");
81
var newRow = document.createElement("tr");
82
var newColumnDate = document.createElement("td");
83
var newColumnLow = document.createElement("td");
84
var newColumnHigh = document.createElement("td");
85
newColumnDate.innerText = date;
86
newColumnLow.innerText = low;
87
newColumnHigh.innerText = high;
88
newRow.appendChild(newColumnDate);
89
newRow.appendChild(newColumnLow);
90
newRow.appendChild(newColumnHigh);
91
tableInfo.appendChild(newRow);
92
}
93
}