如何将自定义(定义的)顺序应用于 DC.js 中的折线图的行?

How can I apply a custom (defined) order to the rows of a row chart in DC.js?

我有一个当前看起来像这样的行图:

deltaChart
    .width(300)
    .height(250)
    .dimension(deltaDim)
    .group(pointsByDelta)
    .xAxis().ticks(4);

其中deltaDim维度由11个左右的唯一字符串值组成,pointsByDelta表示相关计数,deltaChart是rowChart类型。

由于 deltaDim 值是字符串而不是数字,排序是乱序的。我已经尝试应用我在 wiki 以及此处其他帖子中找到的内容来手动重新排序,但完全没有运气。无论格式如何,我都会收到控制台错误:.ordering is not a function()

我试过像这样简单的排序:

.ordering(function(d){return -d.value})

这会导致错误。理想情况下,我希望能够执行以下操作:

.ordering(function(d) {
        if(d.value == "5 ft") return 0;
        else if(d.value == "4 ft") return 1;
        else if(d.value == "3 ft") return 2;
        else if(d.value == "2 ft") return 3;
        else if(d.value == "1 ft") return 4;
        else if(d.value == "0 ft") return 5;
        else if(d.value == "-1 ft") return 6;
        else if(d.value == "-2 ft") return 7;
        else if(d.value == "-3 ft") return 8;
        else if(d.value == "-4 ft") return 9;
        else if(d.value == "-5 ft") return 10;
        else return 11;
    })

似乎大多数关于此主题的帖子都以升序或降序排序的方式进行了回复。我什至无法让它正常运行,就我而言,如果可能的话,我需要能够指定顺序。

感谢任何帮助。谢谢!

如果字符串像上面那样,您可以在函数中使用它们。
拆分:

The split() method splits a String object into an array of strings by separating the string into substrings.

通过字符串中的 space 拆分字符串,然后 return 数组中的第一项
然后解析这一项,将5到-5变成一个int

.ordering(function(d){  
     return parseInt(d.split(" ")[0]);  
 })

您确定 d3.js 有 .ordering() 方法吗?也许您正在寻找旧的.sort()。问题是你必须在遍历数组时替换单元 "ft",我们可以使用正则表达式模式 \s+ft$ 来删除它。该模式与字符串末尾的术语 <any_amount_of_space>ft 相匹配。替换后,只需将其转换为浮点数即可。我避免使用 parseInt() 因为你可能有非整数值。

var arr = ["5 ft", "4 ft", "3 ft", "2 ft", "1 ft", "0 ft", "-1 ft", "-2 ft", "-3 ft", "-4 ft", "-5 ft"];

var arr_sorted = arr.sort(function(a, b) {
  // Convert to float
  var x = parseFloat(a.replace(/\s+ft$/, "")),
      y = parseFloat(b.replace(/\s+ft$/, ""));

  // Return, sort by descending order
  return ((x < y) ? 1 : ((x > y) ? -1 : 0));

  // If ascending order is needed, use:
  // return ((x > y) ? 1 : ((x < y) ? -1 : 0));
});
console.log(arr_sorted);

结合 Gordon 上面关于 .xAxis() 的输入,有效的解决方案如下。

deltaChart
    .width(300)
    .height(250)
    .dimension(bfeDeltaDim)
    .group(pointsByBFEDelta)
    .ordering(function(d) {
        if(d.key == "5 ft") return 0;
        else if(d.key == "4 ft") return 1;
        else if(d.key == "3 ft") return 2;
        else if(d.key == "2 ft") return 3;
        else if(d.key == "1 ft") return 4;
        else if(d.key == "0 ft") return 5;
        else if(d.key == "-1 ft") return 6;
        else if(d.key == "-2 ft") return 7;
        else if(d.key == "-3 ft") return 8;
        else if(d.key == "-4 ft") return 9;
        else if(d.key == "-5 ft") return 10;
        else return 11;
    })
    .xAxis().ticks(4);

最好避免冗长的 if else 条件。这可以通过 d3 scales

轻松处理
  1. 使用首选顺序创建序数尺度和域。
  2. Range可以通过排序顺序用index来完成

    var scale = d3.scale.ordinal()
    .domain(['C','B','A'])
    .range([0,1,2]);
    

    排序函数中

    chart.ordering(function(k){
      return scale(k.key);
    })
    

jsFiddle Working copy