ChartJS onclick填充区域(雷达图) [英] ChartJS onclick fill area (Radar Chart)

查看:401
本文介绍了ChartJS onclick填充区域(雷达图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在JQuery中创建一个函数,该函数可以增加和减少点击的点数.我已经设法创建了一个onclick事件,该事件可以增加得分,但无法弄清楚如何降低得分.

$("#radarChart").click(
    function (evt) {
        var activePoints = myRadarChart.getPointsAtEvent(evt);
        var Values = activePoints[0].label + ' = ' + activePoints[0].value;
        activePoints[0].value++;
    }
);

上面的函数在单击时会增加一个点的值,为了减小该点,我需要知道是否单击了填充区域.

我查看了ChartJS文档,但没有发现.其他图表都具有此功能,例如极地面积图",当您将鼠标悬停在某个节上时,它会突出显示,这意味着有一个功能可以检测鼠标悬停在线段上.

雷达图中是否存在类似的功能?

我的 codepen .

如果没有,那么对我如何实现这一目标的任何想法将不胜感激.

我能想到的唯一选择是创建10个按钮,5个标签.按钮将为+和-,增加和减少标签.我认为这占用了太多空间,因此我尝试避免这种情况.

谢谢

解决方案

确实没有特定的chart.js API可以满足您的需求,但是您可以使用canvas API和一些几何图形来获得相同的结果./p>

基本上,如果用户单击当前值的区域之外,则希望增加该值;如果用户单击当前值的区域内部,则希望减少该值.

我已经修改了您的点击处理程序来做到这一点.

function getElementPosition(obj) {
  var curleft = 0, curtop = 0;
  if (obj.offsetParent) {
    do {
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    } while (obj = obj.offsetParent);
      return { x: curleft, y: curtop };
  }
  return undefined;
};

function getEventLocation(element,event){
  // Relies on the getElementPosition function.
  var pos = getElementPosition(element);

  return {
    x: (event.pageX - pos.x),
    y: (event.pageY - pos.y)
  };
};

function pointDistance(point1, point2) {
  return Math.sqrt(Math.pow(point2.x - point1.x, 2) + Math.pow(point2.y - point1.y, 2));
};

//Get the context of the Radar Chart canvas element we want to select
var ctx = document.getElementById("radarChart").getContext("2d");

// Create the Radar Chart
var myRadarChart = new Chart(ctx).Radar(radarData, radarOptions);

$("#radarChart").click(function (evt) {
  var eventLocation = getEventLocation(this,evt); 
  var activePoints = myRadarChart.getPointsAtEvent(evt);
  var eventLocDistToCenter = pointDistance({x: myRadarChart.scale.xCenter, y: myRadarChart.scale.yCenter}, eventLocation);
  var activePointDistToCenter = pointDistance({x: myRadarChart.scale.xCenter, y: myRadarChart.scale.yCenter}, activePoints[0]);

  if (eventLocDistToCenter < activePointDistToCenter) {
    activePoints[0].value--;
  } else {
    activePoints[0].value++;
  }

  myRadarChart.update();
});

请注意,我还添加了对.update()的调用,以便图表可以立即呈现更改.使用实现方式,直到下一次渲染(即鼠标移动时),您才能看到图表发生变化.

这是从您的工作解决方案中派生出来的 codepen .点击四周即可查看.

最后,您可能要考虑升级到chart.js 2.0(最新版本为2.5). 1.0很久以来就一直不受支持,并且最新版本具有很多改进.您应该可以轻松地将其移植过来.如果您需要帮助,请发布新问题.

I'm trying to create a function in JQuery that increase and decrease the points clicked. I've managed to create the onclick event that increases the points, but can't figure out how to decrease it.

$("#radarChart").click(
    function (evt) {
        var activePoints = myRadarChart.getPointsAtEvent(evt);
        var Values = activePoints[0].label + ' = ' + activePoints[0].value;
        activePoints[0].value++;
    }
);

The above function increases the value of a point when clicked, in order to decrease it I need to know if the filled area is clicked.

I looked at ChartJS documentation and didn't come across it. Other charts do have it, for example Polar Area Chart, when you are hovering a section, it highlights, meaning there is a function that detects mouse hovering over segments.

Does a similar function exist in Radar Charts?

My codepen.

If not, any ideas on how I could achieve this, would be appreciated.

The only alternative I can think of, would be to create 10 buttons, 5 labels. The buttons would be + and -, increase and decrease the label. This takes too much space in my opinion, so I'm trying to avoid it.

Thanks,

解决方案

There really is no specific chart.js API to do what you are wanting, but you can achieve the same results using the canvas API and a little geometry.

Basically, you want to increase the value if the user clicks outside the current value's region, and you want to decrease if the user clicks inside the current value's region.

I've modified your click handler to do just that.

function getElementPosition(obj) {
  var curleft = 0, curtop = 0;
  if (obj.offsetParent) {
    do {
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    } while (obj = obj.offsetParent);
      return { x: curleft, y: curtop };
  }
  return undefined;
};

function getEventLocation(element,event){
  // Relies on the getElementPosition function.
  var pos = getElementPosition(element);

  return {
    x: (event.pageX - pos.x),
    y: (event.pageY - pos.y)
  };
};

function pointDistance(point1, point2) {
  return Math.sqrt(Math.pow(point2.x - point1.x, 2) + Math.pow(point2.y - point1.y, 2));
};

//Get the context of the Radar Chart canvas element we want to select
var ctx = document.getElementById("radarChart").getContext("2d");

// Create the Radar Chart
var myRadarChart = new Chart(ctx).Radar(radarData, radarOptions);

$("#radarChart").click(function (evt) {
  var eventLocation = getEventLocation(this,evt); 
  var activePoints = myRadarChart.getPointsAtEvent(evt);
  var eventLocDistToCenter = pointDistance({x: myRadarChart.scale.xCenter, y: myRadarChart.scale.yCenter}, eventLocation);
  var activePointDistToCenter = pointDistance({x: myRadarChart.scale.xCenter, y: myRadarChart.scale.yCenter}, activePoints[0]);

  if (eventLocDistToCenter < activePointDistToCenter) {
    activePoints[0].value--;
  } else {
    activePoints[0].value++;
  }

  myRadarChart.update();
});

Note, I also added a call to .update() so that the chart renders the change immediately. With the way you had it implemented, you would not see the chart change until the next render (i.e. when the mouse moves).

Here is a codepen forked from yours with the working solution. Click around to check it out.

Lastly, you probably want to think about upgrading to chart.js 2.0 (latest release is 2.5). 1.0 is long since unsupported and the latest version has LOTS of improvements. You should be able to easily port this over. Post a new question if you need help.

这篇关于ChartJS onclick填充区域(雷达图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆