I'm having some issues with returning a dynamic object. I'm using a react-native chart library, which requires me to return the data like so:
return [{
'Manually Sold': { value: manuallySoldValue },
'Trailing Stop Loss': { value: trailingStopLossValue },
'Manual Short': { value: manualShortValue },
'Profit': { value: profitValue },
'Manual Buy': { value: manualBuy },
'Strategy': { value: strategy },
'Manual Close Short': { value: manualCloseShort },
'available': { value: available },
'in open positions': { value: inOpenPositions },
'reserved': { value: reserved },
}];
Which works fine, if the keys won't change.
StackedBarChart rendering:
stackedBarChart(counts, triggers) {
return (
<View>
<StackedBarChart
style={{ height: 10 }}
colors={this.triggerColors()}
contentInset={{ top: 30, bottom: 30 }}
data={this.triggerValues(counts)}
horizontal={true}
keys={this.triggerKeys(counts)}
valueAccessor={({ item, key }) => item[key].value}
/>
</View>
);
}
this.triggerColors
triggerColors() {
return ['#00c8c6', '#44baf1', '#c7e486', '#efdc6c'];
}
this.triggerKeys
triggerKeys(data) {
return Object.keys(data);
}
Current implementation to build the required structure:
triggerValues(data) {
// The trigger values can be a lot more, so this needs to be dynamic
const keys = this.triggerKeys(data);
let manuallySoldValue = '';
let trailingStopLossValue = '';
let manualShortValue = '';
let profitValue = '';
let manualBuy = '';
let strategy = '';
let manualCloseShort = '';
let available = '';
let inOpenPositions = '';
let reserved = '';
keys.map((key) => {
switch (key) {
case 'Manually Sold':
manuallySoldValue = data[key];
break;
case 'Trailing Stop Loss':
trailingStopLossValue = data[key];
break;
case 'Manual Short':
manualShortValue = data[key];
break;
case 'Profit':
profitValue = data[key];
break;
case 'Manual Buy':
manualBuy = data[key];
break;
case 'Strategy':
strategy = data[key];
break;
case 'Manual Close Short':
manualCloseShort = data[key];
break;
case 'available':
available = data[key];
break;
case 'in open positions':
inOpenPositions = data[key];
break;
case 'reserved':
reserved = data[key];
break;
default:
break;
}
});
return [{
'Manually Sold': { value: manuallySoldValue },
'Trailing Stop Loss': { value: trailingStopLossValue },
'Manual Short': { value: manualShortValue },
'Profit': { value: profitValue },
'Manual Buy': { value: manualBuy },
'Strategy': { value: strategy },
'Manual Close Short': { value: manualCloseShort },
'available': { value: available },
'in open positions': { value: inOpenPositions },
'reserved': { value: reserved },
}];
}
That would work if only the keys were set in stone. So I would like to build that part where the amount and keys won't matter. I've tried to build it with iteration over the data object (example objects below), but I can't seem to get the correct structure as mentioned below.
Example of data objects:
{available: "46.09", in open positions: "53.91", reserved: "0.00"}
-
{Manual Buy: 11, Manual Close Short: 7, Strategy: 42}
-
{Trailing Stop Loss: 3, Manual Short: 7, Profit: 46, Manually Sold: 5}
If you need any more information, please ask.