用分析仪表板LWC在分析中使用D3.j处理点击/悬停事件

发布于 2025-01-27 03:43:48 字数 2541 浏览 4 评论 0原文

我目前正在尝试使用D3库来构建分析仪表板LWC。我希望能够聆听SVG元素上的某些事件,但是无论我尝试什么,似乎都忽略了该事件。我最初认为,由于闪电储物柜的服务,也许是不可能的,但是查看此lwc-recipe 在这里您可以看到Drag and Tick事件处理程序正常工作。

这基本上是我尝试的:

vis.js

import { LightningElement, api } from 'lwc';
import d3_lib from '@salesforce/resourceUrl/d3';
import { loadScript, loadStyle } from 'lightning/platformResourceLoader';

export default class TimelineGraph extends LightningElement {
     _results;
    @api get results() {
        return this._results;
    }
    set results(value) {
        console.log(value);
        this._results = value;
        this.updateGraph();
    }

    updateGraph() {
        if (this.jsInitialized) {
            return;
        }

        Promise.all([
            loadScript(this, d3_lib + '/d3.min.js')
        ]).then(() => {
            this.jsInitialized = true;

            this.initializeGraph();
        }).catch(err => {
            console.error(err);
        }) 
    }

    initializeGraph() {
        try {
            const d3 = window.d3;
            // Create SVG
            const svg = d3.select(this.template.querySelector('.timeline-viz'))
                .append("svg")
                .attr("title", "My Title")
                .attr("id", "timeline")
                .attr("class", "my-svg")
                .attr("width", this.params.svg.width)
                .attr("height", this.params.svg.height)
                .on('mouseover', (event, d) => console.log("EVENT"));
                // Right here is where I would expect the mouseover event to be handled
                // however nothing is ever logged to the console with this code,
                // it appears that this event is ignored completely.
          
            // Then theres a bunch more code here but I've removed it because
            // I don't think it's important.
     }
}

vis.html

<template>
    <div class="timeline-viz" lwc:dom="manual"></div>
</template>

此图显示为预期,但是事件处理程序只是行不通的。我尝试使用this.template.addeventlistenerthis.addeventlistener(记录),但没有雪茄。

我想做的是可能的吗?如果是这样,我该如何正确处理LWC内D3的事件?

编辑:我已经确认,如果我为记录页面或应用程序页面创建相同的确切组件,则可以处理D3事件。但是,我将其作为分析仪表板LWC构建,并且在试图将事件在CRM Analytics仪表板中工作时,什么也不会发生。

I am currently trying to build an Analytics Dashboard LWC using the d3 library. I want to be able to listen for certain events on the SVG element, however no matter what I try it seems like that event is ignored. I initially thought that maybe it wasn't possible because of the Lightning Locker service, but looking at this lwc-recipe here you can see that the drag and tick event handlers are working fine.

This is basically what I am trying:

vis.js

import { LightningElement, api } from 'lwc';
import d3_lib from '@salesforce/resourceUrl/d3';
import { loadScript, loadStyle } from 'lightning/platformResourceLoader';

export default class TimelineGraph extends LightningElement {
     _results;
    @api get results() {
        return this._results;
    }
    set results(value) {
        console.log(value);
        this._results = value;
        this.updateGraph();
    }

    updateGraph() {
        if (this.jsInitialized) {
            return;
        }

        Promise.all([
            loadScript(this, d3_lib + '/d3.min.js')
        ]).then(() => {
            this.jsInitialized = true;

            this.initializeGraph();
        }).catch(err => {
            console.error(err);
        }) 
    }

    initializeGraph() {
        try {
            const d3 = window.d3;
            // Create SVG
            const svg = d3.select(this.template.querySelector('.timeline-viz'))
                .append("svg")
                .attr("title", "My Title")
                .attr("id", "timeline")
                .attr("class", "my-svg")
                .attr("width", this.params.svg.width)
                .attr("height", this.params.svg.height)
                .on('mouseover', (event, d) => console.log("EVENT"));
                // Right here is where I would expect the mouseover event to be handled
                // however nothing is ever logged to the console with this code,
                // it appears that this event is ignored completely.
          
            // Then theres a bunch more code here but I've removed it because
            // I don't think it's important.
     }
}

vis.html

<template>
    <div class="timeline-viz" lwc:dom="manual"></div>
</template>

This graph is showing up as expected, however the event handler just doesn't work. I've tried to do some janky stuff with the this.template.addEventListener and this.addEventListener (documented here), but no cigar.

Is what I'm trying to do possible? And if so, how can I properly handle events from d3 inside of an LWC?

Edit: I have confirmed that the d3 events are handled just fine if I create this same exact component for a Record Page or an App Page. However I am building this as an Analytics Dashboard LWC, and when trying to get the events to work in a CRM Analytics dashboard, nothing is happening.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

王权女流氓 2025-02-03 03:43:48

我发现问题是什么。在Analytics Studio中,CSS属性Pointer-Evest默认情况下设置为。为了允许SVG进行交互式,我需要明确设置指针事件CSS属性:

  svg {
    pointer-events: all;
  }

在记录页中,此属性设置为all默认情况下,这就是为什么我的组件在记录中正常工作页面,但不在分析仪表板中。希望,如果其他人在建立分析仪表板LWC的同时遇到了这个问题,那么这篇文章可以为您节省一天的撞击墙。

编辑:CRM Analytics团队已确认这一行为不会预期。指针事件应在仪表板编辑页面中禁用,但应在预览页面中启用。 22夏季版本应该解决问题,并且此问题可能不再重要。

I found out what the issue is. In Analytics Studio, the css property pointer-events is set to none by default. In order to allow the SVG to be interactive, I needed to explicitly set the pointer events css property:

  svg {
    pointer-events: all;
  }

In record pages, this property is set to all by default, which is why my component works fine in record pages but not in an Analytics Dashboard. Hopefully if someone else is running into this issue while building an Analytics Dashboard LWC this post can save you a day of banging your head against the wall.

EDIT: The CRM Analytics team has confirmed that this behavior is not expected. Pointer events should be disabled in the dashboard edit page, but should be enabled in the preview page. A fix should be out for the Summer '22 release, and this issue may no longer be relevant.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文