带有条形图的单击时的Chartist URL-javascript

时间:2018-09-11 14:34:49

标签: javascript chartist.js

我正尝试使用以下代码打开新的网址(不使用JQuery),以便与Chartist一起使用onClick功能:

var chart = new Chartist.Bar('#chart1', {
    labels: [1, 2, 3, 4],
    series: [[5, 2, 8, 3]]
  }, {
  distributeSeries: false
},{
  seriesBarDistance: 20,
  low: 0,
  high: 10
});
chart.on('draw', function(data) {
    if(data.type === 'bar') {
        data.element._node.onclick = func (){window.location = "www.google.com"}


    }
});

但是当我实施此操作时,它不会等待点击,它会自动打开www.google.com。

如何让它等待点击?

有人可以澄清我在做什么吗?

2 个答案:

答案 0 :(得分:1)

我不知道您的问题,但是func ()不是有效的javascript关键字

您应该将其更改为:

data.element._node.onclick = function(){ 
  window.location = "www.google.com"; 
}

答案 1 :(得分:1)

尝试一下

HTML

   <div id="chartContainer" style="height: 360px; width: 100%;"></div>

JAVASCRIPT

var chart = new CanvasJS.Chart("chartContainer",
{

  data: [
  {
         type: "bar",
         cursor:"pointer",
         click: onClick,          
         dataPoints: [
                 { label: "Google", y: 85, link:"http://google.com/" },
                 { label: "Bing", y: 15, link:"http://bing.com/"},
                 { label: "Yahoo Search", y: 13, link:"http://search.yahoo.com/" },
                 { label: "DuckDuckGo", y: 6, link:"http://www.duckduckgo.com/" },
                 { label: "DogPile", y: 3, link: "http://www.dogpile.com/" }        
           ]
          }
       ]
     });

  chart.render();


function onClick(e){ 
        window.open(e.dataPoint.link,'_blank');  
        };

https://jsfiddle.net/canvasjs/ccxob6bp/