简体   繁体   English

d3.csv操作数据切换列显示

[英]d3.csv manipulate data switch column displayed

The data are read from a cvs.file 从cvs.file读取数据

    "col1","col2"
    "row1col1","row1col2"

I would like to switch the column data in a force layout to select column 2 or column 1, but dynamically. 我想在强制布局中切换列数据以选择列2或列1,但要动态选择。

    node = nodeG.selectAll(".node")
            .data(data, function(d) {return d.column1;});//first option

    node = nodeG.selectAll(".node")
            .data(data, function(d) {return d.column2;});//second option

I tried to use a variable, for some reason it didn't work 我尝试使用一个变量,由于某种原因它不起作用

    var data_variable = function(d) {return d.column1;}
                        || function(d) {return d.column2;} 
                        // pseudocode, data from input options

    node = nodeG.selectAll(".node")
            .data(data, data_variable);

Is this the correct approach or are the data better manipulated before display ? 这是正确的方法还是在显示之前更好地处理了数据?

It ended up to be a simple solution inside the main function: 最终成为主要功能内部的一个简单解决方案:

var ls = d3.select("#selector").property("value");

if (ls === 'it' ){

  idValue = function(d) {
  return d.col1;
  };

  textValue = function(d) {
  return d.col1;
  };

 } else {

  idValue = function(d) {
  return d.col2;
  };

  textValue = function(d) {
  return d.col2;
  };

 };

I simply obtain the value from a drop down list and chose the returned functions conditionally 我只是从下拉列表中获取值,然后有条件地选择了返回的函数

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

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