简体   繁体   English

如何在echarts条形图中使条形周围的空间可点击?

[英]How to make space around bar clickable in echarts bar chart?

The 'click' event gets triggered only when clicked exactly inside the bar. 'click' 事件仅在刚好在栏内单击时才会触发。 This is not convenient if we have a bar with small width or height.如果我们有一个小宽度或高度的条,这很不方便。 When i hover the bar, some space around the bar is highlighted with full chart height and tooltip is displayed.当我悬停栏时,栏周围的一些空间会以完整的图表高度突出显示,并显示工具提示。 i want to make the highlighted region clickable and the bar data should be available in the event.我想让突出显示的区域可点击,并且条形数据应该在事件中可用。 I have gone through the docs and tried chart.on('click', 'xAxis.category', function () {...});我已经浏览了文档并尝试了 chart.on('click', 'xAxis.category', function () {...}); But the function is not triggered.但该功能没有被触发。

In this demo, Alert is triggered only when i click inside a bar.在此演示中,仅当我在栏内单击时才会触发警报。 How do i make the surrounding area clickable?如何使周围区域可点击?

https://codesandbox.io/s/cocky-banzai-6j5pg?file=/src/Chart.js https://codesandbox.io/s/cocky-banzai-6j5pg?file=/src/Chart.js

It's true, you cannot receive the common event when click outside bar but Echarts is mature framework and almost any events can received with low-level object zRender .确实,在栏外单击时您无法接收常见事件,但 Echarts 是成熟的框架,几乎所有事件都可以通过低级对象zRender You need to get access to zRender with getZr() and then convert coordinates of the clicked pixel to chart coordinates.您需要使用getZr()访问zRender ,然后将单击的像素的坐标转换为图表坐标。 After it you will have index of series datapoint and with this to fetch category will be easy.在它之后,您将拥有系列数据点的索引,并且使用它来获取类别将很容易。

myChart.getZr().on('click', params => {
  var pointInPixel = [params.offsetX, params.offsetY];
  var pointInGrid = myChart.convertFromPixel('grid', pointInPixel);
  var category = myChart.getModel().get('xAxis')[0].data[pointInGrid[0]]
  console.log(category);
});

See example:见示例:

 var myChart = echarts.init(document.getElementById('main')); var option = { title: { text: 'ECharts' }, tooltip: {}, legend: { data: ['Label'] }, xAxis: { data: ["Category1", "Category2", "Category3", "Category4", "Category5", "Category6"] }, yAxis: {}, tooltip: { trigger: "axis", confine: true, enterable: true, axisPointer: { type: "shadow", shadowStyle: { color: "rgba(255,0,0, 0.5)" } }, backgroundColor: "rgba(255,255,255,1)", textStyle: { color: "#6D6D70" }, extraCssText: `box-shadow: 3px 6px 14px #cccccc61;border-radius: 10px;` }, series: [{ name: 'Series', type: 'bar', data: [5, 20, 36, 10, 10, 20], showBackground: true, }] }; myChart.setOption(option); myChart.getZr().on('click', params => { var pointInPixel = [params.offsetX, params.offsetY]; var pointInGrid = myChart.convertFromPixel('grid', pointInPixel); var category = myChart.getModel().get('xAxis')[0].data[pointInGrid[0]] console.log(category); });
 <script src="https://cdn.jsdelivr.net/npm/echarts@4.9.0/dist/echarts.min.js"></script> <div id="main" style="width: 600px;height:400px;"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM