如何使用dimple.js按不同的数据行对y轴进行排序

时间:2014-07-02 16:23:31

标签: javascript dimple.js

这是this topic的后续行动。我正在尝试通过不同的数据行动态排序y轴。我的数据结构:

var dataGraph = [];
for (var i = 0; i < data.length; i++) {
  //data[i].eusiInd = (data[i].eusiInd === 99) ? '' : data[i].eusiInd;
  dataGraph.push(
    {'Index' : 'Eusi-Index', 'Country' : data[i].ctry, 'Year' : data[i].year, 'Disaggregation' : data[i].disagg, 'Index Value' : data[i].eusiInd, 'Identifier' : 'Eusi-Index, '+data[i].disagg},
    {'Index' : 'Standard of Living', 'Country' : data[i].ctry, 'Year' : data[i].year, 'Disaggregation' : data[i].disagg, 'Index Value' : data[i].stdLiving, 'Identifier' : 'Standard of Living, '+data[i].disagg},
    {'Index' : 'Housing', 'Country' : data[i].ctry, 'Year' : data[i].year, 'Disaggregation' : data[i].disagg, 'Index Value' : data[i].housing, 'Identifier' : 'Housing, '+data[i].disagg},
    {'Index' : 'Health', 'Country' : data[i].ctry, 'Year' : data[i].year, 'Disaggregation' : data[i].disagg, 'Index Value' : data[i].health, 'Identifier' : 'Health, '+data[i].disagg},
    {'Index' : 'Social Relations', 'Country' : data[i].ctry, 'Year' : data[i].year, 'Disaggregation' : data[i].disagg, 'Index Value' : data[i].socRel, 'Identifier' : 'Social Relations, '+data[i].disagg},
    {'Index' : 'Work', 'Country' : data[i].ctry, 'Year' : data[i].year, 'Disaggregation' : data[i].disagg, 'Index Value' : data[i].work, 'Identifier' : 'Work, '+data[i].disagg}
  );
}
// Remove missing values
dataGraph = dataGraph.filter(function (element) {
 return (Math.floor(element['Index Value']) !== 99);
});

// Data for one year
var dataGraphFiltered = dimple.filterData(dataGraph, 'Year', '2010');

我创建了一个混合条/气泡/折线图,希望能够动态地对y轴进行排序。我的代码如下所示:

var barData = [],
    lineData = [],
    keyIndex = "Eusi-Index";

for (var i = 0; i < dataGraphFiltered.length; i++) {
    if (dataGraphFiltered[i]['Index'] === keyIndex) {
        barData.push(dataGraphFiltered[i]);
    } else {
        lineData.push(dataGraphFiltered[i]);
    }
}

var svg = dimple.newSvg("#graphic", 550, 700);

var chart = new dimple.chart(svg),
    bars,
    lines,
    dots;

chart.setBounds(50, 30, 480, 630);

var xAxis = chart.addMeasureAxis('x', 'Index Value');
xAxis.overrideMax = 1;
var yAxis = chart.addCategoryAxis('y', 'Country');

bars = chart.addSeries('Identifier', dimple.plot.bar);
bars.data = barData;
bars.getTooltipText = function (e) {
  return [
  'Country: '+e['cy'],
  e['aggField'][0],
  'Index Value: '+Math.round10(e['xValue'],-3)
  ];
};
chart.assignColor('Eusi-Index, total', '#D3D3D3', '#D3D3D3');

lines = chart.addSeries('Identifier', dimple.plot.line);
lines.data = lineData;

dots = chart.addSeries('Identifier', dimple.plot.bubble);
dots.data = lineData;
dots.getTooltipText = function (e) {
  return [
  'Country: '+e['cy'],
  e['aggField'][0],
  'Index Value: '+Math.round10(e['xValue'],-3)
  ];
};

yAxis.addOrderRule('Index Value', false);

chart.draw();

我可能需要使用带有“addOrderRule”的排序功能,以便能够按图表中显示的每个“标识符”数据行进行排序,但我无法找到如何实现这一点。我非常感谢你的建议。

1 个答案:

答案 0 :(得分:1)

我可以使用以下方法解决排序问题:

yAxis.addOrderRule(function(a,b){
  var avalue, bvalue;
  for (var i=0; i<a['Identifier'].length; i++) {
    avalue = 0;
    if (a['Identifier'][i] === 'Housing, total') {
      avalue = a['Index Value'][i*2];
      break;
    }
  }
  for (var i=0; i<b['Identifier'].length; i++) {
    bvalue = 0;
    if (b['Identifier'][i] === 'Housing, total') {
      bvalue = b['Index Value'][i*2];
      break;
    }
  }
  return avalue-bvalue;
}, false);

通过更改'Housing,total',我可以决定使用哪个标识符对y轴进行排序