Adding custom buttons to Exporting Menu
This demo shows how we can push custom HTML elements into top level of exporting menu.
// Add export menulet menu = am5plugins_exporting.ExportingMenu.new(root, {});let exporting = am5plugins_exporting.Exporting.new(root, { menu: menu});
// Add custom menu buttonlet myButton = document.createElement("a");myButton.innerHTML = "???";myButton.className = "am5exporting am5exporting-icon am5exporting-align-right";myButton.style.top = "30px";menu.getPrivate("menuElement").appendChild(myButton);
myButton.addEventListener("click", function(ev) { alert("Custom button clicked");});// Add export menuvar menu = am5plugins_exporting.ExportingMenu.new(root, {});var exporting = am5plugins_exporting.Exporting.new(root, { menu: menu});
// Add custom menu buttonvar myButton = document.createElement("a");myButton.innerHTML = "???";myButton.className = "am5exporting am5exporting-icon am5exporting-align-right";myButton.style.top = "30px";menu.getPrivate("menuElement").appendChild(myButton);
myButton.addEventListener("click", function(ev) { alert("Custom button clicked");});/** * --------------------------------------- * This demo was created using amCharts 5. * * For more information visit: * https://www.amcharts.com/ * * Documentation is available at: * https://www.amcharts.com/docs/v5/ * --------------------------------------- */
// Create root element// https://www.amcharts.com/docs/v5/getting-started/#Root_elementvar root = am5.Root.new("chartdiv");
// Set themes// https://www.amcharts.com/docs/v5/concepts/themes/root.setThemes([am5themes_Animated.new(root)]);
// Create chart// https://www.amcharts.com/docs/v5/charts/xy-chart/var chart = root.container.children.push( am5xy.XYChart.new(root, { panX: false, panY: false, wheelX: "panX", wheelY: "zoomX", layout: root.verticalLayout }));
var data = [ { country: "USA", visits: 4025, columnSettings: { fill: chart.get("colors").next() } },
{ country: "China", visits: 1882, columnSettings: { fill: chart.get("colors").next() } },
{ country: "Japan", visits: 1809, columnSettings: { fill: chart.get("colors").next() } },
{ country: "Germany", visits: 1322, columnSettings: { fill: chart.get("colors").next() } },
{ country: "UK", visits: 1122, columnSettings: { fill: chart.get("colors").next() } },
{ country: "France", visits: 1114, columnSettings: { fill: chart.get("colors").next() } },
{ country: "India", visits: 984, columnSettings: { fill: chart.get("colors").next() } },
{ country: "Spain", visits: 711, columnSettings: { fill: chart.get("colors").next() } },
{ country: "Netherlands", visits: 665, columnSettings: { fill: chart.get("colors").next() } },
{ country: "Korea", visits: 443, columnSettings: { fill: chart.get("colors").next() } },
{ country: "Canada", visits: 441, columnSettings: { fill: chart.get("colors").next() } },
{ country: "Brazil", visits: 395, columnSettings: { fill: chart.get("colors").next() } },
{ country: "Italy", visits: 386, columnSettings: { fill: chart.get("colors").next() } },
{ country: "Australia", visits: 384, columnSettings: { fill: chart.get("colors").next() } },
{ country: "Taiwan", visits: 338, columnSettings: { fill: chart.get("colors").next() } },
{ country: "Poland", visits: 328, columnSettings: { fill: chart.get("colors").next() } }];
// Create axes// https://www.amcharts.com/docs/v5/charts/xy-chart/axes/var xRenderer = am5xy.AxisRendererX.new(root, { cellStartLocation: 0.1, cellEndLocation: 0.9, minGridDistance: 50});
var xAxis = chart.xAxes.push( am5xy.CategoryAxis.new(root, { categoryField: "country", renderer: xRenderer, tooltip: am5.Tooltip.new(root, {}) }));
xRenderer.grid.template.setAll({ location: 1});
xRenderer.labels.template.setAll({ multiLocation: 0.5});
xAxis.data.setAll(data);
var yAxis = chart.yAxes.push( am5xy.ValueAxis.new(root, { renderer: am5xy.AxisRendererY.new(root, { strokeOpacity: 0.1 }) }));
// Add series// https://www.amcharts.com/docs/v5/charts/xy-chart/series/var series = chart.series.push( am5xy.ColumnSeries.new(root, { xAxis: xAxis, yAxis: yAxis, valueYField: "visits", categoryXField: "country" }));
series.columns.template.setAll({ tooltipText: "{categoryX}: {valueY}", width: am5.percent(90), tooltipY: 0, strokeOpacity: 0, templateField: "columnSettings"});
series.data.setAll(data);
// Add export menuvar menu = am5plugins_exporting.ExportingMenu.new(root, {});var exporting = am5plugins_exporting.Exporting.new(root, { menu: menu});
// Add custom menu buttonvar myButton = document.createElement("a");myButton.innerHTML = "???";myButton.className = "am5exporting am5exporting-icon am5exporting-align-right";myButton.style.top = "30px";menu.getPrivate("menuElement").appendChild(myButton);
myButton.addEventListener("click", function (ev) { alert("Custom button clicked");});
// Make stuff animate on load// https://www.amcharts.com/docs/v5/concepts/animations/series.appear();chart.appear(1000, 100);<div id="chartdiv"></div>body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";}
#chartdiv { width: 100%; height: 95vh;}{ "type": "XYChart", "settings": { "panX": false, "panY": false, "wheelX": "panX", "wheelY": "zoomX", "layout": { "type": "VerticalLayout" } }, "properties": { "series": [ "#series-0" ], "xAxes": [ "#axis-0" ], "yAxes": [ "#axis-1" ] }, "refs": [ { "data-0": [ { "country": "USA", "visits": 4025, "columnSettings": { "fill": { "type": "Color", "value": "#67b7dc" }, "__parse": true }, "__parse": true }, { "country": "China", "visits": 1882, "columnSettings": { "fill": { "type": "Color", "value": "#6794dc" }, "__parse": true }, "__parse": true }, { "country": "Japan", "visits": 1809, "columnSettings": { "fill": { "type": "Color", "value": "#6771dc" }, "__parse": true }, "__parse": true }, { "country": "Germany", "visits": 1322, "columnSettings": { "fill": { "type": "Color", "value": "#8067dc" }, "__parse": true }, "__parse": true }, { "country": "UK", "visits": 1122, "columnSettings": { "fill": { "type": "Color", "value": "#a367dc" }, "__parse": true }, "__parse": true }, { "country": "France", "visits": 1114, "columnSettings": { "fill": { "type": "Color", "value": "#c767dc" }, "__parse": true }, "__parse": true }, { "country": "India", "visits": 984, "columnSettings": { "fill": { "type": "Color", "value": "#dc67ce" }, "__parse": true }, "__parse": true }, { "country": "Spain", "visits": 711, "columnSettings": { "fill": { "type": "Color", "value": "#dc67ab" }, "__parse": true }, "__parse": true }, { "country": "Netherlands", "visits": 665, "columnSettings": { "fill": { "type": "Color", "value": "#dc6788" }, "__parse": true }, "__parse": true }, { "country": "Korea", "visits": 443, "columnSettings": { "fill": { "type": "Color", "value": "#dc6967" }, "__parse": true }, "__parse": true }, { "country": "Canada", "visits": 441, "columnSettings": { "fill": { "type": "Color", "value": "#dc8c67" }, "__parse": true }, "__parse": true }, { "country": "Brazil", "visits": 395, "columnSettings": { "fill": { "type": "Color", "value": "#dcaf67" }, "__parse": true }, "__parse": true }, { "country": "Italy", "visits": 386, "columnSettings": { "fill": { "type": "Color", "value": "#dcd267" }, "__parse": true }, "__parse": true }, { "country": "Australia", "visits": 384, "columnSettings": { "fill": { "type": "Color", "value": "#c3dc67" }, "__parse": true }, "__parse": true }, { "country": "Taiwan", "visits": 338, "columnSettings": { "fill": { "type": "Color", "value": "#a0dc67" }, "__parse": true }, "__parse": true }, { "country": "Poland", "visits": 328, "columnSettings": { "fill": { "type": "Color", "value": "#7ddc67" }, "__parse": true }, "__parse": true } ] }, { "axis-0": { "type": "CategoryAxis", "settings": { "categoryField": "country", "renderer": { "type": "AxisRendererX", "settings": { "cellStartLocation": 0.1, "cellEndLocation": 0.9, "minGridDistance": 50 }, "properties": { "labels": { "properties": { "template": { "settings": { "multiLocation": 0.5 } } } }, "grid": { "properties": { "template": { "settings": { "location": 1 } } } } } }, "tooltip": { "type": "Tooltip" } }, "properties": { "data": "#data-0" } } }, { "axis-1": { "type": "ValueAxis", "settings": { "renderer": { "type": "AxisRendererY", "settings": { "strokeOpacity": 0.1 } } } } }, { "series-0": { "type": "ColumnSeries", "settings": { "xAxis": "#axis-0", "yAxis": "#axis-1", "valueYField": "visits", "categoryXField": "country" }, "properties": { "data": "#data-0", "columns": { "properties": { "template": { "settings": { "tooltipText": "{categoryX}: {valueY}", "width": { "type": "Percent", "value": 90 }, "tooltipY": 0, "strokeOpacity": 0, "templateField": "columnSettings" } } } } } } } ]}adding-custom-buttons-to-exporting-menu