(amCharts) 我有一个图表,其中的数据有两个不同的前缀。有什么方法可以告诉函数哪些数据有哪些前缀?

(amCharts) I have a chart which data has two different prefixes. Any way to tell the function which data has which prefix?

我有一个 amChart 图表,其数据取自其下方的两个 table(代码已修改 )。每个 table 中的每一行类别都以不同的前缀开头:第一个 table 以 (Sales) 开头,第二个 table 以 (市场).

为了更好地说明,这是我的代码:http://jsfiddle.net/uoxbm2d3/2/

因为有两个 table 并且它是一个 column/bar 图表,我需要根据 table 或它们具有的前缀来堆叠 columns/bars .所以 (Sales) 只会与 (Sales) 叠加,换句话说,来自 Table 1 的数据只会与 Table 1 叠加。与 (Market)/Table 相同2 只会与其兄弟堆叠在一起。像这样:

green/yellow/orange颜色来自Table1(销售); blue/purple 颜色来自 Table 2(市场)。

有没有办法告诉函数哪个图来自哪个table?或者至少告诉函数哪些数据有哪些前缀?

这是一段脚本

    function generateGraphsFromData(chart, chartData) {
    //get the chart graph value fields as lookup table with a seen property set to false by default
    //this will be used to determine what graphs need to be removed when looking at the data
    var graphValueFields = chart.graphs.reduce(function(valueFields, graph) {
        valueFields[graph.valueField] = 0;
        return valueFields;
    }, {});

    var removeValueFields = [];

    //create an array of new graph value fields by filtering out the categoryField
    //and the currently known valueFields. 
    var newGraphValueFields = [];

    Object.keys(chartData[0]).filter(function(key) {
        return key != chart.categoryField;
    }).forEach(function(valueField) {
        //if this is a new graph, add it to the list
        if (graphValueFields[valueField] === undefined) {
            newGraphValueFields.push(valueField);
        } else {
            //otherwise, set the seen flag to 1
            graphValueFields[valueField] = 1;
        }
    });

    //for each new value field left over, create a graph object and add to the chart.
    newGraphValueFields.forEach(function(valueField) {
        var graph = new AmCharts.AmGraph();
        graph.title = valueField;
        graph.valueField = valueField;
        graph.balloonText = "<strong>[[title]]</strong><br /> Rp[[value]]";
        graph.id = valueField; //use the valueField as the ID for ease of removal
        graph.type = 'column';
        graph.lineAlpha = 0;
        graph.fillAlphas = 0.5;
        graph.bullet = "none";
        graph.stackable = true; // disable stacking
        chart.addGraph(graph);
    });

    //loop through the graphValueFields lookup table and remove all graphs that were not seen when
    //rescanning the data
    Object.keys(graphValueFields).forEach(function(removeGraphValueField) {
        if (graphValueFields[removeGraphValueField] === 0) {
            chart.removeGraph(chart.getGraphById(removeGraphValueField));
        }
    })
    }

添加新图表并删除所有旧图表后,您可以按标题排序,然后再次遍历它们以找到第一个 non-matching 前缀并设置图表的 newStack 属性 为 true,同时在其余图表中将其设置为 false。例如:

  chart.graphs.sort(function(lhs, rhs) {
    //sort by title, ensures that the sales (first table) data comes first
    var lhsSalesIdx = lhs.title.indexOf('(Sales)');
    var rhsSalesIdx = rhs.title.indexOf('(Sales)');

    if (lhsSalesIdx !== -1 && rhsSalesIdx === -1) {
      return -1;
    }
    else if (lhsSalesIdx === -1 && rhsSalesIdx !== -1) {
      return 1;
    }
    else {
        return lhs.title.localeCompare(rhs.title);
    }
  });
  var stackSet = false;
  //find the first instance of the market row graph and set its newStack property to true
  //while clearing out the others
    for (var i = 0; i < chart.graphs.length; ++i) {
    if (!stackSet && chart.graphs[i].title.indexOf('(Sales)') === -1) {
        chart.graphs[i].newStack = true;
        stackSet = true;
    }
    else {
      chart.graphs[i].newStack = false;
    }     
  }

Updated fiddle - 请注意,我在您的新图形循环中将 stackable 设置回 true 以使其也能正常工作。