简体   繁体   English

我该如何克服错误:“选择”类型上不存在“节点”属性 <string[]> “?

[英]How can I overcome the error: Property 'nodes' does not exist on type 'Selection<string[]>'?

The following code was adapted with some help provided for this earlier question about how to arrange legend entries. 以下代码经过改编,并为先前有关如何排列图例条目的问题提供了一些帮助。 It is working fine for me, except for the last part (the part that should space out the groups). 对于我来说,它工作正常,除了最后一部分(应该隔开组的那一部分)。 It chokes on the groups.nodes() . 它阻塞了groups.nodes()

I'm getting the error: [ts] Property 'nodes' does not exist on type 'Selection<string[]>'. 我收到错误消息: [ts] Property 'nodes' does not exist on type 'Selection<string[]>'.

I think this may be happening because I'm using TypeScript. 我认为这可能是因为我正在使用TypeScript而发生的。 I'm also using d3 v3.5.5. 我也在使用d3 v3.5.5。

Any idea how to make this work? 任何想法如何使这项工作?

        var legendData = [["OA", "yellow", "circle"], ["OI", "blue", "cross"], ["RARC", "green", "diamond"], ["CAPE", "red", "square"], ["Other", "black", "triangle-down"]];

        this.svg.selectAll('.legend').remove() //remove remnants of previous legend so new legend has clean slate...eliminates overlays during resizing

        var svg = d3.select('svg')
            .append('svg').
            attr('width', 800)
            .attr('height', 200);

        var groups = svg.selectAll('g')
            .data(legendData)
            .enter()
            .append('g')
            .attr("class", "legend");

        var symb = groups.append('path')
            .attr('transform', 'translate ( 15 , 15 )')
            .attr("d", d3.svg.symbol().type((d) => {
                return d[2]
            }
            ).size((d3.min([height, width]) * ScatterChart.Config.axisFontMultiplier) * (d3.min([height, width]) * ScatterChart.Config.symbolSizeMultiplier)))
            .style("fill", function (d) {
                return d[1];
            })
            .attr('stroke', 'black')
            ;

        var text = groups.append('text')
            .attr('y', 20)
            .attr('x', 30)
            .text(function (d) { return d[0]; })
            .style('font-size', function () { return d3.min([height, width]) * ScatterChart.Config.axisFontMultiplier + "px" })
            ;

        // Now space the groups out after they have been appended:
        var padding = 10;
        groups.attr('transform', function (d, i) {
            return "translate(" + (d3.sum(legendData, function (e, j) {
                if (j < i) { return groups.nodes()[j].getBBox().width; } else return 0;
            }) + padding * i) + ",0)";
        })

With help from @mkaran and @Andrew Reid, responding to this question , I got to a solution: 在@mkaran和@Andrew Reid的帮助下,回答了这个问题 ,我找到了一个解决方案:

        // Now space the groups out after they have been appended:
        var padding = 10;
        groups.attr('transform', function (d, i) {
            return "translate(" + (d3.sum(legendData, function (e, j) {
                if (j < i) { return (<any>groups)[0][j].getBBox().width; } else return 0;
            }) + padding * i) + ",0)";
        })

Thanks @mkaran and @Andrew Reid! 谢谢@mkaran和@Andrew Reid!

暂无
暂无

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

相关问题 属性在类型字符串错误上不存在 - Property does not exist on type String error 我该如何解决“属性&#39;样式&#39;不存在类型&#39;元素&#39;错误? - How can i solve a "Property 'style' does not exist on type 'Element' error? 我该如何解决“'元素'类型上不存在属性'checkValidity'。” ts(2339) 错误 - How can i solve a "Property 'checkValidity' does not exist on type 'Element'." ts(2339) error 我该如何解决 - “订阅”类型上不存在“那么”属性 - How can I solve - Property 'then' does not exist on type 'Subscription' Vue i18n中带有TypeScript的错误:类型&#39;VueConstructor&#39;上不存在属性&#39;$ t&#39;。 ”。 我该如何解决? - Error in Vue i18n with TypeScript: “Property '$t' does not exist on type 'VueConstructor'. ” . How can I fix it? Typescript 错误:类型“[字符串,未知] []”上不存在属性“平面” - Typescript error: Property 'flat' does not exist on type '[string, unknown][]' 错误TS2339:类型“字符串”上不存在属性“默认”。** - Error TS2339: Property 'Default' does not exist on type 'string'.** 类型“字符串”上不存在属性“substr”| 细绳[]' - Property 'substr' does not exist on type 'string | string[]' 当属性存在时,类型错误时属性不存在 - Property does not exist on type error when the property does exist 如何解决错误“TS2339:&#39;JQuery 类型上不存在属性&#39;仪表&#39;<HTMLElement> &#39;。” - How do I resolve error "TS2339: Property 'gauge' does not exist on type 'JQuery<HTMLElement>'."
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM