HTMLcopy
1
<button onclick="attachDetach()">Detach/Attach</button>
2
<div id="container"></div>
CSScopy
9
1
html, body, #container {
2
width: 100%;
3
height: 100%;
4
margin: 0;
5
padding: 0;
6
}
7
button {
8
margin: 10px 0 0 10px;
9
}
JavaScriptcopy
x
1
var lineChart, columnChart, lineCustomContextMenu, columnCustomContextMenu;
2
anychart.onDocumentReady(function () {
3
// To change the way how context menu looks you need to change CSS.
4
// By default CSS from AnyChart CDN is used: https://cdn.anychart.com/releases/8.13.0/css/anychart-ui.min.css
5
// Download it, alter, upload to your server and link from your pages to change the look.
6
7
var stage = anychart.graphics.create('container');
8
9
lineChart = anychart.line([
10
{x: 'January', value: 1},
11
{x: 'February', value: 2},
12
{x: 'March', value: 1.3},
13
{x: 'April', value: 2.9},
14
{x: 'May', value: 1.5}
15
]);
16
lineChart.bounds(0, 50, '50%', '95%');
17
lineChart.contextMenu(false);
18
19
lineCustomContextMenu = anychart.ui.contextMenu();
20
21
// Attach context menu to the chart
22
lineCustomContextMenu.attach(lineChart);
23
24
lineCustomContextMenu.itemsProvider(function () {
25
return [
26
{
27
text: 'Open AnyChart API',
28
href: 'https://api.anychart.com'
29
}
30
];
31
});
32
lineChart.container(stage);
33
lineChart.draw();
34
35
columnChart = anychart.column([
36
{x: 'January', value: 1},
37
{x: 'February', value: 2},
38
{x: 'March', value: 1.3},
39
{x: 'April', value: 2.9},
40
{x: 'May', value: 1.5}
41
]);
42
columnChart.bounds('50%', 50, '50%', '95%');
43
columnChart.contextMenu(false);
44
45
columnCustomContextMenu = anychart.ui.contextMenu();
46
columnCustomContextMenu.itemsProvider(function () {
47
return [
48
{
49
text: 'Open AnyChart API',
50
href: 'https://api.anychart.com'
51
}
52
];
53
});
54
columnChart.container(stage);
55
columnChart.draw();
56
57
var customTitle = anychart.standalones.title();
58
customTitle.parentBounds('50%', 0, 100, 100);
59
customTitle.text('Detach/Attach a custom context menu to the chart. Click on labels and right-click on the chart');
60
customTitle.container(stage);
61
customTitle.draw();
62
});
63
64
function attachDetach() {
65
66
// Detach the custom context menu from the line chart.
67
lineCustomContextMenu.detach(lineChart);
68
69
// Attach context menu to the column chart.
70
columnCustomContextMenu.attach(columnChart);
71
72
}