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 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 range of points shown on the chart
30
chart.selectRange("2015-12-26", "2015-12-30");
31
32
/* create the selectable object
33
and select all rows in the table*/
34
selectable = mapping.createSelectable();
35
36
// enable html in the chart title
37
chart.title().useHtml(true);
38
39
/* get the values of the first and last shown point
40
and show them in the chart title */
41
showSelected();
42
43
/* listen to changing the shown range of points
44
and update the chart title*/
45
chart.listen("selectedRangeChange", function(){
46
showSelected();
47
});
48
49
// set the container id
50
chart.container("container");
51
52
// initiate drawing the chart
53
chart.draw();
54
});
55
56
/* get the values of the first and last
57
shown point and display them in the chart title */
58
function showSelected() {
59
60
// get the shown range of points
61
var range = chart.getSelectedRange();
62
63
// get the values of the first and last point of the range
64
var firstPoint = selectable.search(range.firstSelected, "nearest");
65
var lastPoint = selectable.search(range.lastSelected, "nearest");
66
var firstDate = firstPoint.getColumn(0);
67
var firstLow = firstPoint.get("low");
68
var firstHigh = firstPoint.get("high");
69
var lastDate = lastPoint.getColumn(0);
70
var lastLow = lastPoint.get("low");
71
var lastHigh = lastPoint.get("high");
72
73
// set the chart title
74
chart.title("Table Data: Reading<br><br>" +
75
"first selected = <span style = 'color:#990000'>" +
76
firstDate + " (L: " + firstLow + ", H: " + firstHigh + ")</span>" +
77
"<br>last selected = <span style = 'color:#990000'>" +
78
lastDate + " (L: " + lastLow + ", H: " + lastHigh + ")</span>");
79
80
}