简体   繁体   English

使用 dc.js 生成的图表比例返回 NaN

[英]Scale of chart generated by using dc.js is returning NaN

I am very new to DC/D3 libraries.我对 DC/D3 库非常陌生。 I am trying to incorporate DC with ReactJS by having a separate pure JS file that is a reusable D3 component.我试图通过拥有一个单独的纯 JS 文件来将 DC 与 ReactJS 合并,该文件是可重用的 D3 组件。 I am following this example here.我在这里遵循这个例子 Here is the dummy data I am using: json snippet .这是我正在使用的虚拟数据: json snippet

This is my App.js:这是我的 App.js:

 state = { data: null, }; componentDidMount() { console.log("componentDidMount"); d3.json("dummy_data.json").then(data => { this.setState({data: data}); console.log("Data is updated;"). }) this;createVisualization(). } componentDidUpdate() { console;log("componentDidUpdate"). this;updateVisualization(). } createVisualization = () => { console;log("createVisualization"). this:visualization = new Dashboard({ parentElement. d3.select(this;node) }). } updateVisualization = () => { this.visualization.updateVis(this.state;data). console;log("updateVisualization finished"): } render() { return ( <div style={{width. '100vw'}} id="app" ref={node => this.node = node}> <h1>Hello World!!!</h1> </div> ) }

And this is my JS file (dashboard.js) used to generate/render chart:这是我用来生成/渲染图表的 JS 文件(dashboard.js):

 export default class Dashboard { constructor(_config) { // setting up based on input config this.parentElement = _config.parentElement; this.initVis(); } initVis() { let vis = this; vis.chartContainer = vis.parentElement.append("div").attr("id", "chart-container").style("width", "100%"); vis.chart = new dc.ScatterPlot("#chart-container"); vis.chart.width(768).height(480).margins({top: 0, right: 10, bottom: 20, left: 50}).x(d3.scaleLinear()).elasticX(true).elasticY(true).brushOn(false) // turn off brush-based range filter.symbolSize(8) // set dot radius.clipPadding(10); const f = vis.chart.x(); console.log(f.domain()); console.log("Finish initVis") } updateVis(newData) { let vis = this; vis.cf = crossfilter(newData); console.log(newData); vis.chart.on('pretransition', () => { // console.log(vis) // const xext = d3.extent(vis.chart.group().all(), d => d.key); // console.log(newData.map(d => [+d.age, +d.readmission_risk])); const r = regression.linear(newData.map(d => [d.age, d.readmission_risk])); const m = r.equation[0], b = r.equation[1]; const [x1, x2] = vis.chart.x().domain(); const points = [ [ x1, m * x1 + b ], [ x2, m * x2 + b ] ]; const xScale = vis.chart.x(); const yScale = vis.chart.y(); const margins = vis.chart.margins(); console.log(xScale(20)); var line = vis.chart.g().selectAll('line.regression').data([points]); // console.log(vis.chart.x().domain()); function do_points(line) { line.attr('x1', d => xScale(d[0][0]) + margins.left).attr('y1', d => yScale(d[0][1]) + margins.top).attr('x2', d => xScale(d[1][0]) + margins.left).attr('y2', d => yScale(d[1][1]) + margins.top); } line = line.enter().append('line').attr('class', 'regression').call(do_points).merge(line); line.transition().duration(vis.chart.transitionDuration()).call(do_points); }) vis.renderVis(); } renderVis() { let vis = this; const ageDimension = vis.cf.dimension(d => d.age); const ageGroup = ageDimension.group(); vis.chart.dimension(ageDimension).group(ageGroup); vis.chart.render(); console.log("rendering"); }

I have identified the problem to be the xScale in the call-back function in the updateVis method.我已经确定问题是updateVis方法中回调 function 中的xScale问题。 It is returning NaN .它正在返回NaN But I tried to call the xScale in the initVis the scale function seems working fine and return [0,1] .但我试图在initVis中调用xScale的比例 function 似乎工作正常并返回[0,1] I have been struggling because of this for a long time.我为此苦苦挣扎了很长时间。 Any help would be appreciated!任何帮助,将不胜感激!

Update: Here is a snapshot of the error message I got:更新:这是我收到的错误消息的快照: 在此处输入图像描述

Thanks for including a reproducible example.感谢您提供可重现的示例。 It's really hard to debug D3 and dc.js code by just staring at it without running it.只盯着它看而不运行它真的很难调试 D3 和 dc.js 代码。

The problem here is that the scatter plot expects the group keys to be a two-element array of numbers, not just a single number.这里的问题是 scatter plot 期望组键是数字的二元素数组,而不仅仅是单个数字。 You can see that the key_function() , in the regression example which you started from, returns a function returning such keys.您可以看到key_function()在您开始的回归示例中返回一个 function 返回此类键。

Changing your dimension accordingly:相应地更改您的尺寸:

    const ageDimension = vis.cf.dimension(d => [d.age, d.readmission_risk] );

caused the chart to appear.导致图表出现。

I also had to add the CSS from the example in order to get the regression line to show;我还必须从示例中添加 CSS 以显示回归线; I created dashboard.css :我创建了dashboard.css

  line.regression {
    stroke: red;
    stroke-width: 5;
    opacity: 0.5;
  }

And added it to dashboard.js :并将其添加到dashboard.js

import './dashboard.css';

Result:结果:

带有回归线的散点图

Incidentally, the way you have calculated the regression line, it won't update when the data is filtered.顺便说一句,您计算回归线的方式在过滤数据时不会更新。 If you want it to do so, this should work:如果你想让它这样做,这应该工作:

const r = regression.linear(vis.chart.group().all().filter(kv => kv.value).map(kv => [kv.key[0], kv.key[1]]));

This uses the keys from the chart's group, but only the ones which are filtered "in" (value > 0).这使用图表组中的键,但仅使用“输入”过滤的键(值 > 0)。

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

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