缩放并在d3.js中选择是不起作用的

问题描述 投票:-2回答:1

我试图在缩放后选择数据,但是d3.event.selection总是获得相同的值。有人有主意吗?

  private zoomChart(DataType: any[], color) {
    this.createChart(DataType, this.t, color);
    this.zoom = d3.zoom().scaleExtent([0, 1000])
        .on("zoom", () => {
            const transform = d3.event.transform;
            this.t = transform;

          this.draw(this.context, transform, DataType, this.train, 
         this.gxAxis, this.gyAxis,        
         this.xAxis, this.yAxis, this.x, this.y)
        });

    this.canvasChart.call(this.zoom);
    this.canvasChart.style('z-index', 1);
    this.svgChart.style('z-index', 1);
   }
    private selectionChart(DataType: any[], color) {
    this.zoomChart(DataType, color);
    this.brush = d3.brush().extent([
        [0, 0],
        [this.width, this.height]
    ])
        .on("start", () => {
            this.brustStart(DataType);
        })
        .on("end", () => {
            this.brudhtest(DataType, this.train, this.dataSelection, this.t);
        });
    if (!this.svgChart) {
        this.svgChart = this.svgChart
            .append("g")
            .attr("class", "brush")
            .style("color", "red");
    }

    this.svgChart.call(this.brush);
    this.svgChart.style('z-index', 1);
    this.canvasChart.style('z-index', 0);
 }


      brudhtest(test, test2, dataSel, transform) {

        let extent = d3.event.selection
        console.log(extent)
       console.log(transform.k)
       if (!extent) return;
        var x0 = (extent[0][0]),
        x1 = (extent[1][0]),
        y0 = (extent[0][1]),
        y1 = (extent[1][1])


        let elementsOfInterest = test.filter(element => {
            if (
                x0 <= this.x(element[a])
                && this.x(element[a]) <= x1 &&
                y0 <= this.y(element[b])
                && this.y(element[b]) <= y1
            ) {
                console.log(x0);
                 }})

这是代码。当我不缩放时,选择是正确的,但是当我进行缩放然后选择时不起作用,只有当我选择与我未缩放相同的区域时,画笔才会浮起。有什么想法吗?

angular d3.js canvas scale brush
1个回答
0
投票

我得到了答案。我必须将this.x(element [a])更改为scaleX(element [a])

© www.soinside.com 2019 - 2024. All rights reserved.