Search code examples
javascriptamcharts

Amcharts - How to open link in a new tab/window


I am trying to make chart using amcharts. In that, I have the categories as links which I want to open in new tab/window. I added the following code which opens the link in a new tab/window but at same time opens the link in the same tab also. Please tell what I am doing wrong. I do not want to use Jquery and I am new to javascript.

This is the code which I added to the javascript for opening category link in new tab -

window.open(event.serialDataItem.dataContext.url, '_blank');

This is the original AmCharts codepen - https://codepen.io/team/amcharts/pen/50c8b6a103e7126fac91ce40000192be

This is the snippet -

var chart = AmCharts.makeChart("chartdiv", {
  "type": "serial",
  "theme": "light",
  "addClassNames": true,
  "titles": [{
    "text": "Open this demo in a separate window for links to work"
  }],
  "dataProvider": [{
    "country": "USA",
    "visits": 2025,
    "url": "https://codepen.io"
  }, {
    "country": "China",
    "visits": 1882,
    "url": "https://codepen.io"
  }, {
    "country": "Japan",
    "visits": 1809,
    "url": "https://codepen.io"
  }, {
    "country": "Germany",
    "visits": 1322,
    "url": "https://codepen.io"
  }, {
    "country": "UK",
    "visits": 1122,
    "url": "https://codepen.io"
  }, {
    "country": "France",
    "visits": 1114,
    "url": "https://codepen.io"
  }, {
    "country": "India",
    "visits": 984,
    "url": "https://codepen.io"
  }, {
    "country": "Spain",
    "visits": 711,
    "url": "https://codepen.io"
  }, {
    "country": "Netherlands",
    "visits": 665,
    "url": "https://codepen.io"
  }, {
    "country": "Russia",
    "visits": 580,
    "url": "https://codepen.io"
  }, {
    "country": "South Korea",
    "visits": 443,
    "url": "https://codepen.io"
  }, {
    "country": "Canada",
    "visits": 441,
    "url": "https://codepen.io"
  }, {
    "country": "Brazil",
    "visits": 395,
    "url": "https://codepen.io"
  }],
  "valueAxes": [{
    "gridColor": "#FFFFFF",
    "gridAlpha": 0.2,
    "dashLength": 0
  }],
  "gridAboveGraphs": true,
  "startDuration": 1,
  "graphs": [{
    "balloonText": "[[category]]: <b>[[value]]</b>",
    "fillAlphas": 0.8,
    "lineAlpha": 0.2,
    "type": "column",
    "valueField": "visits"
  }],
  "chartCursor": {
    "categoryBalloonEnabled": false,
    "cursorAlpha": 0,
    "zoomable": false
  },
  "categoryField": "country",
  "categoryAxis": {
    "gridPosition": "start",
    "gridAlpha": 0,
    "tickPosition": "start",
    "tickLength": 20,
    "listeners": [{
      "event": "clickItem",
      "method": function(event) {
        window.location.href = event.serialDataItem.dataContext.url;
				window.open(event.serialDataItem.dataContext.url, '_blank');
      }
    }]
  }

});
#chartdiv {
  width: 100%;
  height: 400px;
}

.amcharts-axis-label {
  cursor: pointer;
}
<script src="https://www.amcharts.com/lib/3/amcharts.js"></script>
<script src="https://www.amcharts.com/lib/3/serial.js"></script>
<script src="https://www.amcharts.com/lib/3/themes/light.js"></script>
<div id="chartdiv"></div>


Solution

  • You should remove the line ..

     window.location.href = event.serialDataItem.dataContext.url;
    

    .. from your clickItem listener.

    This line instructs the browser to replace the current page location. Basically this line prevents the next line from executing.

    This should work:

    "listeners": [{
      "event": "clickItem",
      "method": function(event) {
        window.open(event.serialDataItem.dataContext.url, '_blank');
      }
    }]
    

    It might not work in a Codepen environment, but it should work elsewhere.