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
// set the data
7
var table = anychart.data.table('x');
8
table.addData(data_us_population());
9
10
// map the data
11
mapping = table.mapAs({x:'x', value:'value'});
12
13
// set the chart type
14
var chart = anychart.stock();
15
16
// set the series type and title
17
var series = chart.plot(0).line(mapping);
18
series.name("Population");
19
20
// format y-axis labels
21
chart.plot(0).yAxis().labels().format("{%Value}m");
22
23
// set the chart title and padding
24
chart.title('Switching the Series Type in Stock Charts');
25
chart.title().padding(20);
26
27
// create a scroller series with values
28
var scrollerSeries = chart.scroller().area(table.mapAs({'value': 'value'}));
29
30
// set the chart container and initiate drawing the chart
31
chart.container(stage);
32
chart.draw();
33
34
// create a label for switching to the line series
35
createLabel("Switch to Line", 10, function() {
36
// change the series type
37
series.seriesType("line");
38
scrollerSeries.seriesType("line");
39
chart.title("Switching the Series Type in Stock Charts \n Line");
40
});
41
42
// create a label for switching to the column series
43
createLabel("Switch to Column", 127, function() {
44
// change the series type
45
series.seriesType("column");
46
scrollerSeries.seriesType("column");
47
chart.title("Switching the Series Type in Stock Charts \n Column");
48
});
49
50
// create a label for switching to the area series
51
createLabel("Switch to Area", 265, function() {
52
// change the series type
53
series.seriesType("area");
54
scrollerSeries.seriesType("area");
55
chart.title("Switching the Series Type in Stock Charts \n Area");
56
});
57
58
});
59
60
// a helper function for creating buttons
61
function createLabel(text, offset, action){
62
var label = anychart.standalones.label();
63
label.background({fill: "#1976d2"});
64
label.text(text);
65
label.fontColor("#fff");
66
label.padding(5);
67
label.parentBounds(offset, 5, 130, 100);
68
label.listen("click", action);
69
label.container(stage);
70
label.draw();
71
label.listen("mouseOver", function(){
72
label.background().fill("#1976d2 0.5")
73
document.body.style.cursor = "pointer";
74
label.draw();
75
});
76
label.listen("mouseOut", function(){
77
label.background().fill("#1976d2")
78
document.body.style.cursor = "auto";
79
label.draw();
80
});
81
return label;
82
}
83
84
// a helper function for setting the data
85
function data_us_population() {
86
return [
87
{x:'1790-01-01', value:3.929214},
88
{x:'1795-01-01', value:4.390561},
89
{x:'1800-01-01', value:5.236631},
90
{x:'1805-01-01', value:5.989289},
91
{x:'1810-01-01', value:7.239881},
92
{x:'1815-01-01', value:8.722382},
93
{x:'1820-01-01', value:9.638453},
94
{x:'1825-01-01', value:10.923857},
95
{x:'1830-01-01', value:12.866020},
96
{x:'1835-01-01', value:15.454230},
97
{x:'1840-01-01', value:17.069453},
98
{x:'1845-01-01', value:19.234543},
99
{x:'1850-01-01', value:23.191876},
100
{x:'1855-01-01', value:28.347121},
101
{x:'1860-01-01', value:31.443321},
102
{x:'1865-01-01', value:34.098412},
103
{x:'1870-01-01', value:38.558371},
104
{x:'1875-01-01', value:44.728322},
105
{x:'1880-01-01', value:49.371340},
106
{x:'1885-01-01', value:53.989443},
107
{x:'1890-01-01', value:62.979766},
108
{x:'1895-01-01', value:71.883243},
109
{x:'1900-01-01', value:76.212168},
110
{x:'1905-01-01', value:85.023411},
111
{x:'1910-01-01', value:92.228496},
112
{x:'1915-01-01', value:100.378232},
113
{x:'1920-01-01', value:106.021537},
114
{x:'1925-01-01', value:118.323005},
115
{x:'1930-01-01', value:123.202624},
116
{x:'1935-01-01', value:126.232034},
117
{x:'1940-01-01', value:132.164569},
118
{x:'1945-01-01', value:144.773641},
119
{x:'1950-01-01', value:151.325798},
120
{x:'1955-01-01', value:160.460555},
121
{x:'1960-01-01', value:179.323175},
122
{x:'1965-01-01', value:188.232951},
123
{x:'1970-01-01', value:203.211926},
124
{x:'1975-01-01', value:215.789873},
125
{x:'1980-01-01', value:226.545805},
126
{x:'1985-01-01', value:239.905274},
127
{x:'1990-01-01', value:248.709873},
128
{x:'1995-01-01', value:272.119084},
129
{x:'2000-01-01', value:281.421906},
130
{x:'2005-01-01', value:299.456285},
131
{x:'2010-01-01', value:308.745538},
132
{x:'2015-01-01', value:318.914629}
133
]};