gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:08:22 26 4
gpt4 key购买 nike

我有一个 amChart 图表,其数据取自其下面的两个表(代码已修改 from this SO answer )。每个表中的每一行类别都以不同的前缀开头:第一个表以 (销售) 开头,第二个表以 (市场) 开头。

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

由于有两个表并且它是一个柱形图/条形图,因此我需要根据哪个表或它们具有什么前缀来堆叠列/条形图。因此,(Sales) 将仅与 (Sales) 堆叠,或者换句话说,表 1 中的数据将仅与表 1 堆叠。(Market)/表 2 也是如此,仅与其同级数据堆叠。像这样的事情:

Sorry for the terrible editing, but you get the idea

绿色/黄色/橙色来自表1(销售);蓝色/紫色来自表 2(市场)。

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

这是脚本的一部分

    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));
}
})
}

最佳答案

添加新图表并删除旧图表后,您可以按标题排序,然后再次循环遍历它们以查找第一个不匹配的前缀并设置图表的 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,这样它也能正常工作。

关于javascript - (amCharts)我有一个图表,其中数据有两个不同的前缀。有什么方法可以告诉函数哪些数据具有哪个前缀?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47063043/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com