d3-sankey links not updating on drag event - vue.js

Issue-1 : I am unable to update d3-sankey links on drag events. I am trying to replicate d3 vertical and horizontal drag events similar to this : https://bl.ocks.org/d3noob/5028304
When drag event is used it updates the node and the links are not getting updated with it.
I looked into this too https://observablehq.com/#geekplux/dragable-d3-sankey-diagram and I am still having other issues.
I was unable to replicate the code in jsfiddle so I am sharing the JS code here.
const d3 : any = require('d3');
const sankey : any = require('d3-sankey');
const width = 960;
const height = 500;
const svg = d3.select('#chart')
.append('svg')
.attr('viewBox', [0, 0, width, height]);
const formatNumber = d3.format(',.0f');
const format = function (d: any) { return `${formatNumber(d)}TWh`; };
const color = d3.scaleOrdinal(d3.schemeCategory10);
const path = d3.sankeyLinkHorizontal();
const sankeyGraph = d3.sankey()
.nodeWidth(15)
.nodePadding(10)
.size([width, height]);
const graph = JSON.parse(JSON.stringify({
nodes: mainData.data.nodes.filter((val:any) => {
return val.name !== '';
})
.map((d : any, i : any) => {
return Object.assign({}, d, { nodeId : i });
}),
links: mainData.data.links.filter((val:any) => {
return (val.source !== '') || (val.target !== '') || (val.value !== '');
})
.map((d : any, i : any) => {
return Object.assign({}, d, { linkId : i });
}),
}));
sankeyGraph(graph);
let link = svg.append('g')
.attr('class', 'links')
.attr('fill', 'none')
.attr('stroke', '#000')
.attr('stroke-opacity', 0.2)
.selectAll('path');
let node = svg.append('g')
.attr('class', 'nodes')
.attr('font-size', 10)
.selectAll('g');
link = link
.data(graph.links)
.enter()
.append('path')
.attr('d', path) // d3.sankeyLinkHorizontal()
.attr('stroke-width', (d: any) => { return Math.max(1, d.width); });
// .on('mouseover', handleMouseOver)
// .on('mouseout', handleMouseOut);
link.append('title')
.text((d: any) => {
return `${d.source.name} β†’ ${d.target.name} -> ${format(d.value)}`;
});
node = node
.data(graph.nodes)
.enter()
.append('g');
node.append('rect')
.attr('x', (d: any) => { return d.x0; })
.attr('y', (d: any) => { return d.y0; })
.attr('height', (d: any) => { return d.y1 - d.y0; })
.attr('width', (d: any) => { return d.x1 - d.x0; })
.attr('fill', (d: any) => { return color(d.name.replace(/ .*/, '')); })
.attr('stroke', '#000')
.call(d3.drag()
.subject((d:any) => d)
// .on('start', () => d3.event.currenTarget.appendChild(d3.event.currenTarget))
.on('drag', dragmove));
function dragmove(d:any, i: number, n: any) { // tslint:disable-line
console.log(d3.event, d3);
// console.log('drag', d3.event.sourceEvent.target, d3.event, d3.sankey);
// const m = d3.select(n[i])
// .node()
// .getCTM();
const x = d3.event.x - d3.event.dx;
const y = d3.event.y - d3.event.dy;
// console.log(m, x, y);
d3.select(n[i])
.attr('x', x)
.attr('y', y);
// *********the issue is here********
d3.sankey(graph)
.update(graph);
// d3.sankey()
// .relayout(graph);
link.attr('d', path); // d3.sankeyLinkHorizontal()
}
node.append('text')
.attr('x', (d: any) => { return d.x0 - 6; })
.attr('y', (d: any) => { return (d.y1 + d.y0) / 2; })
.attr('dy', '0.35em')
.attr('text-anchor', 'end')
.text((d: any) => { return d.name; })
.filter((d: any) => { return d.x0 < width / 2; })
.attr('x', (d: any) => { return d.x1 + 6; })
.attr('text-anchor', 'start');
node.append('title')
.text((d: any) => {
return `${d.name} => ${format(d.value)}`;
});
Issue 2: sometimes this event cannot be used in Vue-D3. So I am having trouble with drag start event code. Please suggest an alternative.
this.parentNode.appendChild(this);
Issue 3: Has anybody replicated the following for sankey with d3-V5. I need the flow/link before and after for each node on hover.
http://bl.ocks.org/tomshanley/11277583
Framework used : Vuejs + typescript
D3 version : 5.9.2
D3 sankey package : https://github.com/d3/d3-sankey
mainData JSON :
"data": {
"nodes": [
{
"name": "Agricultural waste"
},
{
"name": "Bio-conversion"
},
{
"name": "Liquid"
},
{
"name": "Losses"
},
{
"name": "Solid"
},
{
"name": "Gas"
},
{
"name": "Biofuel imports"
},
{
"name": "Biomass imports"
},
{
"name": "Coal imports"
},
{
"name": "Coal"
},
{
"name": "Coal reserves"
},
{
"name": "District heating"
},
{
"name": "Industry"
},
{
"name": "Heating and cooling - commercial"
},
{
"name": "Heating and cooling - homes"
},
{
"name": "Electricity grid"
},
{
"name": "Over generation / exports"
},
{
"name": "H2 conversion"
},
{
"name": "Road transport"
},
{
"name": "Agriculture"
},
{
"name": "Rail transport"
},
{
"name": "Lighting & appliances - commercial"
},
{
"name": "Lighting & appliances - homes"
},
{
"name": "Gas imports"
},
{
"name": "Ngas"
},
{
"name": "Gas reserves"
},
{
"name": "Thermal generation"
},
{
"name": "Geothermal"
},
{
"name": "H2"
},
{
"name": "Hydro"
},
{
"name": "International shipping"
},
{
"name": "Domestic aviation"
},
{
"name": "International aviation"
},
{
"name": "National navigation"
},
{
"name": "Marine algae"
},
{
"name": "Nuclear"
},
{
"name": "Oil imports"
},
{
"name": "Oil"
},
{
"name": "Oil reserves"
},
{
"name": "Other waste"
},
{
"name": "Pumped heat"
},
{
"name": "Solar PV"
},
{
"name": "Solar Thermal"
},
{
"name": "Solar"
},
{
"name": "Tidal"
},
{
"name": "UK land based bioenergy"
},
{
"name": "Wave"
},
{
"name": "Wind"
}
],
"links": [
{
"source": 0,
"target": 1,
"value": 124.729
},
{
"source": 1,
"target": 2,
"value": 0.597
},
{
"source": 1,
"target": 3,
"value": 26.862
},
{
"source": 1,
"target": 4,
"value": 280.322
},
{
"source": 1,
"target": 5,
"value": 81.144
},
{
"source": 6,
"target": 2,
"value": 35
},
{
"source": 7,
"target": 4,
"value": 35
},
{
"source": 8,
"target": 9,
"value": 11.606
},
{
"source": 10,
"target": 9,
"value": 63.965
},
{
"source": 9,
"target": 4,
"value": 75.571
},
{
"source": 11,
"target": 12,
"value": 10.639
},
{
"source": 11,
"target": 13,
"value": 22.505
},
{
"source": 11,
"target": 14,
"value": 46.184
},
{
"source": 15,
"target": 16,
"value": 104.453
},
{
"source": 15,
"target": 14,
"value": 113.726
},
{
"source": 15,
"target": 17,
"value": 27.14
},
{
"source": 15,
"target": 12,
"value": 342.165
},
{
"source": 15,
"target": 18,
"value": 37.797
},
{
"source": 15,
"target": 19,
"value": 4.412
},
{
"source": 15,
"target": 13,
"value": 40.858
},
{
"source": 15,
"target": 3,
"value": 56.691
},
{
"source": 15,
"target": 20,
"value": 7.863
},
{
"source": 15,
"target": 21,
"value": 90.008
},
{
"source": 15,
"target": 22,
"value": 93.494
},
{
"source": 23,
"target": 24,
"value": 40.719
},
{
"source": 25,
"target": 24,
"value": 82.233
},
{
"source": 5,
"target": 13,
"value": 0.129
},
{
"source": 5,
"target": 3,
"value": 1.401
},
{
"source": 5,
"target": 26,
"value": 151.891
},
{
"source": 5,
"target": 19,
"value": 2.096
},
{
"source": 5,
"target": 12,
"value": 48.58
},
{
"source": 27,
"target": 15,
"value": 7.013
},
{
"source": 17,
"target": 28,
"value": 20.897
},
{
"source": 17,
"target": 3,
"value": 6.242
},
{
"source": 28,
"target": 18,
"value": 20.897
},
{
"source": 29,
"target": 15,
"value": 6.995
},
{
"source": 2,
"target": 12,
"value": 121.066
},
{
"source": 2,
"target": 30,
"value": 128.69
},
{
"source": 2,
"target": 18,
"value": 135.835
},
{
"source": 2,
"target": 31,
"value": 14.458
},
{
"source": 2,
"target": 32,
"value": 206.267
},
{
"source": 2,
"target": 19,
"value": 3.64
},
{
"source": 2,
"target": 33,
"value": 33.218
},
{
"source": 2,
"target": 20,
"value": 4.413
},
{
"source": 34,
"target": 1,
"value": 4.375
},
{
"source": 24,
"target": 5,
"value": 122.952
},
{
"source": 35,
"target": 26,
"value": 839.978
},
{
"source": 36,
"target": 37,
"value": 504.287
},
{
"source": 38,
"target": 37,
"value": 107.703
},
{
"source": 37,
"target": 2,
"value": 611.99
},
{
"source": 39,
"target": 4,
"value": 56.587
},
{
"source": 39,
"target": 1,
"value": 77.81
},
{
"source": 40,
"target": 14,
"value": 193.026
},
{
"source": 40,
"target": 13,
"value": 70.672
},
{
"source": 41,
"target": 15,
"value": 59.901
},
{
"source": 42,
"target": 14,
"value": 19.263
},
{
"source": 43,
"target": 42,
"value": 19.263
},
{
"source": 43,
"target": 41,
"value": 59.901
},
{
"source": 4,
"target": 19,
"value": 0.882
},
{
"source": 4,
"target": 26,
"value": 400.12
},
{
"source": 4,
"target": 12,
"value": 46.477
},
{
"source": 26,
"target": 15,
"value": 525.531
},
{
"source": 26,
"target": 3,
"value": 787.129
},
{
"source": 26,
"target": 11,
"value": 79.329
},
{
"source": 44,
"target": 15,
"value": 9.452
},
{
"source": 45,
"target": 1,
"value": 182.01
},
{
"source": 46,
"target": 15,
"value": 19.013
},
{
"source": 47,
"target": 15,
"value": 289.366
}
]
}
Thank you for your solutions in advance.
function sg(mainData) {
const width = 960;
const height = 500;
const svg = d3.select('#chart')
.append('svg')
.attr('viewBox', [0, 0, width, height]);
const formatNumber = d3.format(',.0f');
const format = function (d: any) { return `${formatNumber(d)}TWh`; };
const color = d3.scaleOrdinal(d3.schemeCategory10);
const path = d3.sankeyLinkHorizontal();
const sankeyGraph = d3.sankey()
.nodeWidth(15)
.nodePadding(10)
.size([width, height]);
const graph = JSON.parse(JSON.stringify({
nodes: mainData.data.nodes.filter((val:any) => {
return val.name !== '';
})
.map((d : any, i : any) => {
return Object.assign({}, d, { nodeId : i });
}),
links: mainData.data.links.filter((val:any) => {
return (val.source !== '') || (val.target !== '') || (val.value !== '');
})
.map((d : any, i : any) => {
return Object.assign({}, d, { linkId : i });
}),
}));
sankeyGraph(graph);
let link = svg.append('g')
.attr('class', 'links')
.attr('fill', 'none')
.attr('stroke', '#000')
.attr('stroke-opacity', 0.2)
.selectAll('path');
let node = svg.append('g')
.attr('class', 'nodes')
.attr('font-size', 10)
.selectAll('g');
link = link
.data(graph.links)
.enter()
.append('path')
.attr('d', path) // d3.sankeyLinkHorizontal()
.attr('stroke-width', (d: any) => { return Math.max(1, d.width); });
// .on('mouseover', handleMouseOver)
// .on('mouseout', handleMouseOut);
link.append('title')
.text((d: any) => {
return `${d.source.name} β†’ ${d.target.name} -> ${format(d.value)}`;
});
node = node
.data(graph.nodes)
.enter()
.append('g');
node.append('rect')
.attr('x', (d: any) => { return d.x0; })
.attr('y', (d: any) => { return d.y0; })
.attr('height', (d: any) => { return d.y1 - d.y0; })
.attr('width', (d: any) => { return d.x1 - d.x0; })
.attr('fill', (d: any) => { return color(d.name.replace(/ .*/, '')); })
.attr('stroke', '#000')
.call(d3.drag()
.subject((d:any) => d)
// .on('start', () => d3.event.currenTarget.appendChild(d3.event.currenTarget))
.on('drag', dragmove));
function dragmove(d:any, i: number, n: any) { // tslint:disable-line
console.log(d3.event, d3);
// console.log('drag', d3.event.sourceEvent.target, d3.event, d3.sankey);
// const m = d3.select(n[i])
// .node()
// .getCTM();
const x = d3.event.x - d3.event.dx;
const y = d3.event.y - d3.event.dy;
// console.log(m, x, y);
d3.select(n[i])
.attr('x', x)
.attr('y', y);
// *********the issue is here********
d3.sankey(graph)
.update(graph);
// d3.sankey()
// .relayout(graph);
link.attr('d', path); // d3.sankeyLinkHorizontal()
}
node.append('text')
.attr('x', (d: any) => { return d.x0 - 6; })
.attr('y', (d: any) => { return (d.y1 + d.y0) / 2; })
.attr('dy', '0.35em')
.attr('text-anchor', 'end')
.text((d: any) => { return d.name; })
.filter((d: any) => { return d.x0 < width / 2; })
.attr('x', (d: any) => { return d.x1 + 6; })
.attr('text-anchor', 'start');
node.append('title')
.text((d: any) => {
return `${d.name} => ${format(d.value)}`;
});
}
const data = {
"data": {
"nodes": [{
"name": "Agricultural waste"
},
{
"name": "Bio-conversion"
},
{
"name": "Liquid"
},
{
"name": "Losses"
},
{
"name": "Solid"
},
{
"name": "Gas"
},
{
"name": "Biofuel imports"
},
{
"name": "Biomass imports"
},
{
"name": "Coal imports"
},
{
"name": "Coal"
},
{
"name": "Coal reserves"
},
{
"name": "District heating"
},
{
"name": "Industry"
},
{
"name": "Heating and cooling - commercial"
},
{
"name": "Heating and cooling - homes"
},
{
"name": "Electricity grid"
},
{
"name": "Over generation / exports"
},
{
"name": "H2 conversion"
},
{
"name": "Road transport"
},
{
"name": "Agriculture"
},
{
"name": "Rail transport"
},
{
"name": "Lighting & appliances - commercial"
},
{
"name": "Lighting & appliances - homes"
},
{
"name": "Gas imports"
},
{
"name": "Ngas"
},
{
"name": "Gas reserves"
},
{
"name": "Thermal generation"
},
{
"name": "Geothermal"
},
{
"name": "H2"
},
{
"name": "Hydro"
},
{
"name": "International shipping"
},
{
"name": "Domestic aviation"
},
{
"name": "International aviation"
},
{
"name": "National navigation"
},
{
"name": "Marine algae"
},
{
"name": "Nuclear"
},
{
"name": "Oil imports"
},
{
"name": "Oil"
},
{
"name": "Oil reserves"
},
{
"name": "Other waste"
},
{
"name": "Pumped heat"
},
{
"name": "Solar PV"
},
{
"name": "Solar Thermal"
},
{
"name": "Solar"
},
{
"name": "Tidal"
},
{
"name": "UK land based bioenergy"
},
{
"name": "Wave"
},
{
"name": "Wind"
}
],
"links": [{
"source": 0,
"target": 1,
"value": 124.729
},
{
"source": 1,
"target": 2,
"value": 0.597
},
{
"source": 1,
"target": 3,
"value": 26.862
},
{
"source": 1,
"target": 4,
"value": 280.322
},
{
"source": 1,
"target": 5,
"value": 81.144
},
{
"source": 6,
"target": 2,
"value": 35
},
{
"source": 7,
"target": 4,
"value": 35
},
{
"source": 8,
"target": 9,
"value": 11.606
},
{
"source": 10,
"target": 9,
"value": 63.965
},
{
"source": 9,
"target": 4,
"value": 75.571
},
{
"source": 11,
"target": 12,
"value": 10.639
},
{
"source": 11,
"target": 13,
"value": 22.505
},
{
"source": 11,
"target": 14,
"value": 46.184
},
{
"source": 15,
"target": 16,
"value": 104.453
},
{
"source": 15,
"target": 14,
"value": 113.726
},
{
"source": 15,
"target": 17,
"value": 27.14
},
{
"source": 15,
"target": 12,
"value": 342.165
},
{
"source": 15,
"target": 18,
"value": 37.797
},
{
"source": 15,
"target": 19,
"value": 4.412
},
{
"source": 15,
"target": 13,
"value": 40.858
},
{
"source": 15,
"target": 3,
"value": 56.691
},
{
"source": 15,
"target": 20,
"value": 7.863
},
{
"source": 15,
"target": 21,
"value": 90.008
},
{
"source": 15,
"target": 22,
"value": 93.494
},
{
"source": 23,
"target": 24,
"value": 40.719
},
{
"source": 25,
"target": 24,
"value": 82.233
},
{
"source": 5,
"target": 13,
"value": 0.129
},
{
"source": 5,
"target": 3,
"value": 1.401
},
{
"source": 5,
"target": 26,
"value": 151.891
},
{
"source": 5,
"target": 19,
"value": 2.096
},
{
"source": 5,
"target": 12,
"value": 48.58
},
{
"source": 27,
"target": 15,
"value": 7.013
},
{
"source": 17,
"target": 28,
"value": 20.897
},
{
"source": 17,
"target": 3,
"value": 6.242
},
{
"source": 28,
"target": 18,
"value": 20.897
},
{
"source": 29,
"target": 15,
"value": 6.995
},
{
"source": 2,
"target": 12,
"value": 121.066
},
{
"source": 2,
"target": 30,
"value": 128.69
},
{
"source": 2,
"target": 18,
"value": 135.835
},
{
"source": 2,
"target": 31,
"value": 14.458
},
{
"source": 2,
"target": 32,
"value": 206.267
},
{
"source": 2,
"target": 19,
"value": 3.64
},
{
"source": 2,
"target": 33,
"value": 33.218
},
{
"source": 2,
"target": 20,
"value": 4.413
},
{
"source": 34,
"target": 1,
"value": 4.375
},
{
"source": 24,
"target": 5,
"value": 122.952
},
{
"source": 35,
"target": 26,
"value": 839.978
},
{
"source": 36,
"target": 37,
"value": 504.287
},
{
"source": 38,
"target": 37,
"value": 107.703
},
{
"source": 37,
"target": 2,
"value": 611.99
},
{
"source": 39,
"target": 4,
"value": 56.587
},
{
"source": 39,
"target": 1,
"value": 77.81
},
{
"source": 40,
"target": 14,
"value": 193.026
},
{
"source": 40,
"target": 13,
"value": 70.672
},
{
"source": 41,
"target": 15,
"value": 59.901
},
{
"source": 42,
"target": 14,
"value": 19.263
},
{
"source": 43,
"target": 42,
"value": 19.263
},
{
"source": 43,
"target": 41,
"value": 59.901
},
{
"source": 4,
"target": 19,
"value": 0.882
},
{
"source": 4,
"target": 26,
"value": 400.12
},
{
"source": 4,
"target": 12,
"value": 46.477
},
{
"source": 26,
"target": 15,
"value": 525.531
},
{
"source": 26,
"target": 3,
"value": 787.129
},
{
"source": 26,
"target": 11,
"value": 79.329
},
{
"source": 44,
"target": 15,
"value": 9.452
},
{
"source": 45,
"target": 1,
"value": 182.01
},
{
"source": 46,
"target": 15,
"value": 19.013
},
{
"source": 47,
"target": 15,
"value": 289.366
}
]
}
}
sg(data)
<script src="https://cdnjs.cloudflare.com/ajax/libs/typescript/3.7.5/typescript.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-sankey/0.7.1/d3-sankey.min.js"></script>
<div id="chart"></div>

this works for me.
var node = svg.append('g')
.selectAll('.node')
.data(sankeydata.nodes)
.enter()
.append('g')
.attr('class', 'node')
.attr('transform', d => `translate(${d.x0},${d.y0})`)
.call(d3.drag()
.subject(d => d)
.on('start', function () { this.parentNode.appendChild(this); })
.on('drag', dragmove))
function dragmove(d) {
var rectY = d3.select(this).select("rect").attr("y");
var rectX = d3.select(this).select("rect").attr("X");
d.y0 = d.y0 + d3.event.dy;
d.x1 = d.x1 + d3.event.dx;
d.x0 = d.x0 + d3.event.dx;
var yTranslate = d.y0 - rectY;
var xTranslate = d.x0 - rectX;
d3.select(this).attr('transform', "translate(" + (xTranslate) + "," + (yTranslate) + ")");
sankey.update(graph);
link.attr('d', sankeyLinkHorizontal());
}

Related

How to add a specific product variant to the shopping cart

The task is to add a product with selection options to the cart. For example, take product with id:2, which has different color and size options. By clicking on the add product button, the product of the selected option should be added to the cart. Product options are predefined. You can see them in the products array below. At the moment, I have created a cart entity in the global store, where I can add a product without selection options and render it on the cart page.
"products" : [
{
"type": "simple",
"id": 1,
"sku": "s1",
"title": "Product 1",
"regular_price": {
"currency": "USD",
"value": 27.12
},
"image": "/images/1.png",
"brand": 9
},
{
"type": "configurable",
"id": 2,
"sku": "c1",
"title": "Product 2",
"regular_price": {
"currency": "USD",
"value": 54.21
},
"image": "/images/conf/default.png",
"configurable_options": [
{
"attribute_id": 93,
"attribute_code": "color",
"label": "Color",
"values": [
{
"label": "Red",
"value_index": 931,
"value": "#ff0000"
},
{
"label": "Blue",
"value_index": 932,
"value": "#0000ff"
},
{
"label": "Black",
"value_index": 933,
"value": "#000"
}
]
},
{
"attribute_code": "size",
"attribute_id": 144,
"position": 0,
"id": 2,
"label": "Size",
"values": [
{
"label": "M",
"value_index": 1441,
"value": 1
},
{
"label": "L",
"value_index": 1442,
"value": 2
}
]
}
],
"variants": [
{
"attributes": [
{
"code": "color",
"value_index": 931
},
{
"code": "size",
"value_index": 1441
}
],
"product": {
"id": 2001,
"sku": "c1-red-m",
"image": "/image/conf/red.png"
}
},
{
"attributes": [
{
"code": "color",
"value_index": 931
},
{
"code": "size",
"value_index": 1442
}
],
"product": {
"id": 2002,
"sku": "c1-red-l",
"image": "/image/conf/red.png"
}
},
{
"attributes": [
{
"code": "color",
"value_index": 932
},
{
"code": "size",
"value_index": 1441
}
],
"product": {
"id": 2003,
"sku": "c1-blue-m",
"image": "/image/conf/blue.png"
}
},
{
"attributes": [
{
"code": "color",
"value_index": 933
},
{
"code": "size",
"value_index": 1442
}
],
"product": {
"id": 2004,
"sku": "c1-black-l",
"image": "/image/conf/black.png"
}
}
],
"brand": 1
}]
My store:
import {createStore} from 'vuex'
import apiRequests from "#/store/actions/apiRequests";
import commonActions from './actions/actions'
import mutations from './mutations/mutations'
import getters from './getters/getters'
const actions = {...commonActions, ...apiRequests}
export default createStore({
state: {
products: [],
brands: [],
cart: []
},
mutations,
actions,
getters
})
actions:
export default {
ADD_TO_CART({commit}, product) {
commit('SET_CART', product)
},
INCREMENT_CART_ITEM({commit}, index) {
commit('INCREMENT', index)
},
DECREMENT_CART_ITEM({commit}, index) {
commit('DECREMENT', index)
},
DELETE_FROM_CART({commit}, index) {
commit('REMOVE_ITEM_FROM_CART', index)
}
}
mutations:
export default {
SET_PRODUCTS_TO_STATE: (state, products) => {
state.products = products
},
SET_BRANDS_TO_STATE: (state, brands) => {
const includesBrands = {}
for (const brand of brands) {
for (const product of state.products) {
if (product.brand === brand.id) {
includesBrands[brand.id] = brand
}
}
}
state.brands = Object.values(includesBrands)
state.brands.unshift({title: "All brands"})
},
SET_CART: (state, product) => {
if (state.cart.length) {
let isProductExists = false
state.cart.map((item) => {
if (item.id === product.id) {
isProductExists = true
item.quantity++
}
})
if (!isProductExists) {
state.cart.push({...product, quantity: 1})
}
} else {
state.cart.push({...product, quantity: 1})
}
},
REMOVE_ITEM_FROM_CART: (state, index) => {
state.cart.splice(index, 1)
},
INCREMENT: (state, index) => {
state.cart[index].quantity++
},
DECREMENT: (state, index) => {
if (state.cart[index].quantity > 1) {
state.cart[index].quantity--
}
}
}
and getters:
export default {
BRANDS(state) {
return state.brands
},
CART(state) {
return state.cart
},
PRODUCTS(state) {
return state.products = state.products.map((product) => {
const brand = state.brands.find((b) => b.id === product.brand)
return {...product, brandName: brand?.title || 'no brand'}
})
},
}
Now I am faced with the fact that I do not understand how I can add a certain product variant to the cart and draw it on the cart page.

How to parse dynamic key values from api response

Here is my response
{
"status": 1,
"msg": "success",
"data": {
"118377742": {
"gid": 118377742,
"employee_id": 1,
"status": "Present",
"intime": "2022-09-24 10:42:50"
},
"118377785": {
"gid": 118377785,
"employee_id": 2,
"status": "Present",
"intime": "2022-09-24 09:50:59"
},
"118373696": {
"gid": 118373696,
"employee_id": 4,
"name": "Rajasekaran G",
"status": "Leave",
"intime": null
},
"118378064": {
"gid": 118378064,
"employee_id": 14,
"status": "Leave",
"intime": null
},
"118378229": {
"gid": 118378229,
"employee_id": 18,
"status": "Leave",
"intime": null
},
"118378271": {
"gid": 118378271,
"employee_id": 19,
"status": "Leave",
"intime": null
},
}
}
I am new to React Native application development and I have response from api like above. I want to store employee_id and staus values from this response.I don't what value will be coming under data object. How can I parse this JSON?
One way is to loop over the values from the key-value pairs of the data. This way, even if we do not know the keys before hand, we can still access the values.
For example, if the response object is stored in a variable called res:
Object.values(res["data"]).forEach((employee_obj) => {
// do something with employee_obj["employee_id"]
// do something with employee_obj["status"]
})
Sample demo below:
var res = {
"status": 1,
"msg": "success",
"data": {
"118377742": {
"gid": 118377742,
"employee_id": 1,
"status": "Present",
"intime": "2022-09-24 10:42:50"
},
"118377785": {
"gid": 118377785,
"employee_id": 2,
"status": "Present",
"intime": "2022-09-24 09:50:59"
},
"118373696": {
"gid": 118373696,
"employee_id": 4,
"name": "Rajasekaran G",
"status": "Leave",
"intime": null
},
"118378064": {
"gid": 118378064,
"employee_id": 14,
"status": "Leave",
"intime": null
},
"118378229": {
"gid": 118378229,
"employee_id": 18,
"status": "Leave",
"intime": null
},
"118378271": {
"gid": 118378271,
"employee_id": 19,
"status": "Leave",
"intime": null
},
}
};
// loop over employees
Object.values(res["data"]).forEach((employee_obj) => {
// for eqach employee:
// print employee ID of the current employee
console.log(employee_obj["employee_id"]);
// print status of the current employee
console.log(employee_obj["status"]);
})

changing state of kepler.gl on a button click

I am working on kepler.gl . i am trying to add another point on map and change the state of kepler.gl on a button click . I mean when i click the button another point is mapped on keppler.gl map . wright now i am having 4 point on the kepler.gl map and that's working fine ... here is the code of 4 point on the map
import React, { useState } from "react";
import keplerGlReducer, {mapStateUpdaters} from "kepler.gl/reducers";
import { createStore, combineReducers, applyMiddleware } from "redux";
import { taskMiddleware } from "react-palm/tasks";
import { Provider, useDispatch } from "react-redux";
import KeplerGl from "kepler.gl";
import { addDataToMap } from "kepler.gl/actions";
import useSwr from "swr";
import {csv} from 'd3';
import datajson from './Data/data.json'
const reducers = combineReducers({
keplerGl: keplerGlReducer,
});
const store = createStore(reducers, {}, applyMiddleware(taskMiddleware));
export default function App() {
return (
<Provider store={store}>
<Map />
</Provider>
);
}
function Map() {
const dispatch = useDispatch();
const data=datajson;
React.useEffect(() => {
if (data) {
dispatch(
addDataToMap({
datasets: {
info: {
label: "COVID-19",
id: "covid19"
},
data
},
option: {
centerMap: true,
readOnly: false
},
config: {}
})
);
}
}, [dispatch, data]);
return (
// <button onClick ={this.handledata} >changedata</button>
<KeplerGl
id="covid"
mapboxApiAccessToken="pk.eyJ1IjoiYWxpcmF6YTcwNSIsImEiOiJjazh5d2hjb3AwOHBqM2VsY21wOHo5eXprIn0.9G5CE4KqfbvU9HQ6WBuo3w"
width={window.innerWidth}
height={window.innerHeight}
/>
);
}}
my data.jason file look like this ....
[
{
"id": 139010,
"state": 4,
"subState": 0,
"paid": 1,
"stateReason": "Trip finished by user",
"id_turistic": 5,
"priceFinal": 60,
"consumedFreeMinutes": 0,
"id_user": 4627,
"id_vehicle": 245,
"from": "nan",
"to": "Sol",
"batt_diff": -8,
"duracion": 412,
"fecha": "2018-09-01",
"start30": "2018-09-01 08:00:00",
"end30": "2018-09-01 08:00:00",
"distancia": 2,
"latitude": 40.0,
"longitude": -4.0
},
{
"id": 138888,
"state": 4,
"subState": 0,
"paid": 1,
"stateReason": "Trip finished by user",
"id_turistic": 5,
"priceFinal": 100,
"consumedFreeMinutes": 0,
"id_user": 4627,
"id_vehicle": 245,
"from": "Universidad",
"to": "Embajadores",
"batt_diff": -4,
"duracion": 646,
"fecha": "2018-09-01",
"start30": "2018-09-01 00:00:00",
"end30": "2018-09-01 00:00:00",
"distancia": 4,
"latitude": "40.0",
"longitude": "-4.0"
},
{
"id": 138878,
"state": 4,
"subState": 0,
"paid": 1,
"stateReason": "Trip finished by user",
"id_turistic": 5,
"priceFinal": 110,
"consumedFreeMinutes": 0,
"id_user": 10244,
"id_vehicle": 173,
"from": "Vallehermoso",
"to": "Cuatro Caminos",
"batt_diff": -7,
"duracion": 682,
"fecha": "2018-09-01",
"start30": "2018-09-01 00:00:00",
"end30": "2018-09-01 00:00:00",
"distancia": 3,
"latitude": "40.0",
"longitude": "-4.0"
},
{
"id": 138941,
"state": 4,
"subState": 0,
"paid": 0,
"stateReason": "Trip finished by user",
"id_turistic": 5,
"priceFinal": 170,
"consumedFreeMinutes": 0,
"id_user": 12133,
"id_vehicle": 207,
"from": "Justicia",
"to": "Prosperidad",
"batt_diff": -7,
"duracion": 1031,
"fecha": "2018-09-01",
"start30": "2018-09-01 02:00:00",
"end30": "2018-09-01 02:00:00",
"distancia": 5,
"latitude": "40.0",
"longitude": "-4.0"
}
]
now i want to add another point on a click of button ? like for example i want to add that point on a button click
{
"fields": [
{
"name": "id",
"format": "",
"type": "integer"
},
{
"name": "state",
"format": "",
"type": "integer"
},
{
"name": "substate",
"format": "",
"type": "integer"
},
{
"name": "paid",
"format": "",
"type": "integer"
},
{
"name": "stateReason",
"format": "",
"type": "string"
},
{
"name": "id_turistic",
"format": "",
"type": "integer"
},
{
"name": "priceFinal",
"format": "",
"type": "integer"
},
{
"name": "consumedFreeMinutes",
"format": "",
"type": "integer"
},
{
"name": "consumed",
"format": "",
"type": "integer"
},
{
"name": "id_user",
"format": "",
"type": "integer"
},
{
"name": "id_vehicle",
"format": "",
"type": "integer"
},
{
"name": "from",
"format": "",
"type": "string"
},
{
"name": "to",
"format": "",
"type": "string"
},
{
"name": "batt_diff",
"format": "",
"type": "string"
},
{
"name": "duracion",
"format": "",
"type": "string"
},
{
"name": "fecha",
"format": "YYYY-M-D H:m:s",
"type": "timestamp"
},
{
"name": "start30",
"format": "YYYY-M-D H:m:s",
"type": "timestamp"
},
{
"name": "end30",
"format": "YYYY-M-D H:m:s",
"type": "timestamp"
},
{
"name": "distancia",
"format": "",
"type": "integer"
},
{
"name": "latitude",
"format": "",
"type": "real"
},
{
"name": "longitude",
"format": "",
"type": "real"
},
{
"name": "lo",
"format": "",
"type": "real"
}
],
"rows": [
[
139010,
4,
0,
1,
"Trip finished by user",
5,
60,
0,
4627,
245,
"nan",
"Sol",
-8,
412,
"2018-09-01",
"2018-09-01 08:00:00",
"2018-09-01 08:00:00",
2,
40.0,
-12,
40.0
],
[
138888,
4,
0,
1,
"Trip finished by user",
5,
100,
0,
4627,
245,
"Universidad",
"Embajadores",
-4,
646,
"2018-09-01",
"2018-09-01 08:00:00",
"2018-09-01 08:00:00",
4,
40.0,
0,
40.0
],
[
138878,
4,
0,
1,
"Trip finished by user",
5,
110,
0,
10244,
173,
"Vallehermoso",
"Cuatro Caminos",
-7,
682,
"2018-09-01",
"2018-09-01 08:00:00",
"2018-09-01 08:00:00",
3,
40.0,
-8.0,
40.0
],
[
138941,
4,
0,
0,
"Trip finished by user",
5,
170,
0,
12133,
207,
"Justicia",
"Prosperidad",
-7,
1031,
"2018-09-01",
"2018-09-01 08:00:00",
"2018-09-01 08:00:00",
5,
40.0,
-4.0,
40.0
]
]
}
how can i do this can anyone help me for this? i already tried mapstateupdataer function but not been able to get desired results ?
I think you have not defined the button event handler. First, you need to define it and then you can dispatch the action with the new data.
For example, if you have a button with an event handler inside your render() method.
<Button onClick={addData}>Add Data</Button>
Update: To append existing data-set on the map. You have to import a new action from kepler/actions and dispatch it.
import {updateMap} from 'kepler.gl/actions'
const addData= () => {
const data = {latitude: 37.75043, longitude: -122.34679, width: 800, height: 1200};
dispatch(updateMap(data));
};
You have to assign your data (the array of objects in your question) to a variable. Let's call it as sampleData. And This is how your replaceData handler should be like:
const addData= () => {
const sampleData = ['your-new-data-set'];
const config = this.getMapConfig();
dispatch(
addDataToMap({
datasets: {
info: {
label: "COVID-19",
id: "covid19"
},
sampleData
},
option: {
centerMap: true,
readOnly: false
},
config: {}
})
);
};

Parsing Twitter Information with jq ("text": null)

I am trying to do some Twitter-analysis via Twurl and extract some information via jq.
I firstly get some tweets via twurl with the command
twurl /1.1/users/search.json?q=judo
Then i used the following line to structure the outpot with jq:
twurl /1.1/users/search.json?q=judo | jq
i get something like this:
[
{
"id": 173752759,
"id_str": "173752759",
"name": "#JudoWorlds πŸ₯‹",
"screen_name": "Judo",
"location": "Worldwide",
"description": "The Official Twitter Account of the International Judo Federation πŸ₯‹",
"url": "https:.....",
"entities": {
"url": {
"urls": [
{
"url": "https://......",
"expanded_url": "https://www.ijf.org/news/show/5-must-see-preliminary-round-clashes-2",
"display_url": "ijf.org/news/show/5-mu…",
"indices": [
0,
23
]
}
]
},
"description": {
"urls": []
}
},
"protected": false,
"followers_count": 59854,
"friends_count": 847,
"listed_count": 529,
"created_at": "Mon Aug 02 07:55:15 +0000 2010",
"favourites_count": 7074,
"utc_offset": null,
"time_zone": null,
"geo_enabled": true,
"verified": true,
"statuses_count": 16532,
"lang": null,
"status": {
"created_at": "Fri Aug 30 08:27:10 +0000 2019",
"id": 1167353053282013200,
"id_str": "1167353053282013184",
"text": "#JudoWorlds The Alternative Promo \n\n#NeilAdamsJudo https://.....",
"truncated": false,
"entities": {
"hashtags": [
{
"text": "JudoWorlds",
"indices": [
0,
11
]
}
],
"symbols": [],
"user_mentions": [
{
"screen_name": "NeilAdamsJudo",
"name": "Neil Adams MBE",
"id": 40488733,
"id_str": "40488733",
"indices": [
36,
50
]
}
],
"urls": [],
"media": [
{
"id": 1167352899267002400,
"id_str": "1167352899267002369",
"indices": [
51,
74
],
"media_url": "http://pbs.twimg.com/ext_tw_video_thumb/1167352899267002369/pu/img/6yD1r7uaPV7p3y6a.jpg",
"media_url_https": "https://pbs.twimg.com/ext_tw_video_thumb/1167352899267002369/pu/img/6yD1r7uaPV7p3y6a.jpg",
"url": "https://......",
"display_url": "pic.twitter.com/0RlLbKLkN8",
"expanded_url": "https://twitter.com/Judo/status/1167353053282013184/video/1",
"type": "photo",
"sizes": {
"thumb": {
"w": 150,
"h": 150,
"resize": "crop"
},
"medium": {
"w": 1200,
"h": 675,
"resize": "fit"
},
"small": {
"w": 680,
"h": 383,
"resize": "fit"
},
"large": {
"w": 1280,
"h": 720,
"resize": "fit"
}
}
}
]
},
"extended_entities": {
"media": [
{
"id": 1167352899267002400,
"id_str": "1167352899267002369",
"indices": [
51,
74
],
"media_url": "http://pbs.twimg.com/ext_tw_video_thumb/1167352899267002369/pu/img/6yD1r7uaPV7p3y6a.jpg",
"media_url_https": "https://pbs.twimg.com/ext_tw_video_thumb/1167352899267002369/pu/img/6yD1r7uaPV7p3y6a.jpg",
"url": "https://.....",
"display_url": "pic.twitter.com/0RlLbKLkN8",
"expanded_url": "https://twitter.com/Judo/status/1167353053282013184/video/1",
"type": "video",
"sizes": {
"thumb": {
"w": 150,
"h": 150,
"resize": "crop"
},
"medium": {
"w": 1200,
"h": 675,
"resize": "fit"
},
"small": {
"w": 680,
"h": 383,
"resize": "fit"
},
"large": {
"w": 1280,
"h": 720,
"resize": "fit"
}
},
"video_info": {
"aspect_ratio": [
16,
9
],
"duration_millis": 48800,
"variants": [
{
"bitrate": 256000,
"content_type": "video/mp4",
"url": "https://video.twimg.com/ext_tw_video/1167352899267002369/pu/vid/480x270/v4nkTg6qs9rpLq8M.mp4?tag=10"
},
{
"content_type": "application/x-mpegURL",
"url": "https://video.twimg.com/ext_tw_video/1167352899267002369/pu/pl/SQN57QxQFYcKWV7l.m3u8?tag=10"
},
{
"bitrate": 2176000,
"content_type": "video/mp4",
"url": "https://video.twimg.com/ext_tw_video/1167352899267002369/pu/vid/1280x720/8cyNocB_8CRjwVCI.mp4?tag=10"
},
{
"bitrate": 832000,
"content_type": "video/mp4",
"url": "https://video.twimg.com/ext_tw_video/1167352899267002369/pu/vid/640x360/uy2U7D_AEmbLdqEK.mp4?tag=10"
}
]
},
"additional_media_info": {
"monetizable": false
}
}
]
},
"source": "Twitter for Android",
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
"in_reply_to_user_id_str": null,
"in_reply_to_screen_name": null,
"geo": null,
"coordinates": null,
"place": null,
"contributors": null,
"is_quote_status": false,
"retweet_count": 4,
"favorite_count": 17,
"favorited": false,
"retweeted": false,
"possibly_sensitive": false,
"lang": "en"
},
"contributors_enabled": false,
"is_translator": false,
"is_translation_enabled": false,
"profile_background_color": "0099CC",
"profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png",
"profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png",
"profile_background_tile": false,
"profile_image_url": "http://pbs.twimg.com/profile_images/1057928008797970433/O3l2sKj0_normal.jpg",
"profile_image_url_https": "https://pbs.twimg.com/profile_images/1057928008797970433/O3l2sKj0_normal.jpg",
"profile_banner_url": "https://pbs.twimg.com/profile_banners/173752759/1565853008",
"profile_link_color": "0C3C42",
"profile_sidebar_border_color": "8F320A",
"profile_sidebar_fill_color": "F2CF41",
"profile_text_color": "000000",
"profile_use_background_image": true,
"has_extended_profile": false,
"default_profile": false,
"default_profile_image": false,
"following": false,
"follow_request_sent": false,
"notifications": false,
"translator_type": "none"
},
Because i am only interested in certain informations i tried to get the name and the text of the tweet with the following command:
twurl /1.1/users/search.json?q=judo | jq ".[] | { name: .name, text: .text }"
and i get this:
{
"name": "#JudoWorlds πŸ₯‹",
"text": null
}
{
"name": "#WeAreGBJudo",
"text": null
}
{
"name": "CBJ",
"text": null
}
{
"name": "Santos Futebol Clube",
"text": null
}
{
"name": "Marius Vizer",
"text": null
}
{
"name": "FF Judo",
"text": null
}
{
"name": "Santos FC πŸ‡Ί πŸ‡Έ πŸ‡¬ πŸ‡§ ",
"text": null
}
{
"name": "USA Judo",
"text": null
}
{
"name": "ε…¨ζ—₯ζœ¬ζŸ”ι“ι€£η›Ÿ -ゴジラジャパン-",
"text": null
}
{
"name": "Vila Belmiro",
"text": null
}
{
"name": "Deutscher Judo-Bund",
"text": null
}
{
"name": "Bruno Secco",
"text": null
}
{
"name": "Bobby Judo",
"text": null
}
{
"name": "African Judo Union",
"text": null
}
{
"name": "JudoInside.com",
"text": null
}
{
"name": "For competitive judo",
"text": null
}
{
"name": "Judo Canada",
"text": null
}
{
"name": "Neil Adams MBE",
"text": null
}
{
"name": "Sophie Cox",
"text": null
}
{
"name": "Galatasaray Judo",
"text": null
}
Why is this and how can i fix it?
I tried other commands like :
twurl /1.1/search/tweets.json?q=afd | jq ".[] | { name: .name, text: .text }"
but got the error
jq: error (at <stdin>:0): Cannot index array with string "name"
If you need nested 'text' value from 'status' field you should use:
.[] | { name: .name, text: .status.text }
Or shorter version:
.[] | { name, text: .status.text }

Highcharts-vue: Stacked Columns with Drilldown

I'm trying to replicate this chart http://jsfiddle.net/edzk8Loe/ using vue js.
To be honest, I'm learning vuejs and so I'm trying to replicate already ready projects to get confident quickly.
The issue I'm facing at the moment I guess is a logic one. The functions have to create new series as drilldown when click on a column, seams not triggered at all. I'm not getting any error in the console and I'm
Not having yet a totally clear idea about the structure, I think I'm messing up the code. Hereunder you'll find the code I'm using in VUE.
<template>
<div class="chartElem">
<div class="row">
<highcharts class="chart" :constructor-type="'chart'" :options="chartOptions"></highcharts>
</div>
</div>
</template>
<script>
import HighchartsVue from "highcharts-vue";
import Highcharts from "highcharts";
import dataModule from "highcharts/modules/data";
import drilldown from "highcharts/modules/drilldown";
drilldown( Highcharts );
dataModule( Highcharts );
let drilldownChart, drilldownEvent, drilldownLevel = 0;
const chartOptions = {
chart: {
"type": "column",
},
credits: {
"enabled": false
},
plotOptions: {
column: {
stacking: "normal",
events: {
click: function ( event ) {
return false;
}
}
},
"series": {
"borderWidth": 0,
"dataLabels": {
"enabled": true,
"style": {
"textShadow": false,
"fontSize": "10px"
}
}
}
},
"legend": {
"enabled": false,
},
"yAxis": {
"stackLabels": {
"enabled": false,
"style": {
"fontWeight": "bold",
"color": "gray"
}
}
},
"title": {
"text": "Stacked Column Drilldown Chart",
"fontWeight": "bold"
},
"xAxis": {
"title": {},
"type": "category"
},
"yAxis": [ {
"title": {
"text": "Number of Students"
},
"min": 0,
"allowDecimals": false
} ],
"series": [ {
"name": "Outstanding",
"color": "rgb(102, 168, 255)",
"data": [ {
"name": "English",
"y": 0,
"parentCategoryHierarchyId": "0",
"graphParentId": "105"
}, {
"name": "Social Science",
"y": 1,
"parentCategoryHierarchyId": "0",
"graphParentId": "119",
"drilldown": true
}, {
"name": "Science",
"y": 0,
"parentCategoryHierarchyId": "0",
"graphParentId": "126"
}, {
"name": "Maths",
"y": 0,
"parentCategoryHierarchyId": "0",
"graphParentId": "139"
}, {
"name": "Hindi",
"y": 0,
"parentCategoryHierarchyId": "0",
"graphParentId": "146"
} ]
}, {
type: 'column',
"name": "Very Good",
"color": "rgb(128, 183, 255)",
"data": [ {
"name": "English",
"y": 0,
"parentCategoryHierarchyId": "0",
"graphParentId": "105"
}, {
"name": "Social Science",
"y": 0,
"parentCategoryHierarchyId": "0",
"graphParentId": "119",
"drilldown": true
}, {
"name": "Science",
"y": 0,
"parentCategoryHierarchyId": "0",
"graphParentId": "126"
}, {
"name": "Maths",
"y": 0,
"parentCategoryHierarchyId": "0",
"graphParentId": "139"
}, {
"name": "Hindi",
"y": 0,
"parentCategoryHierarchyId": "0",
"graphParentId": "146"
} ]
}, {
type: 'column',
"name": "Satisfactory",
"color": "rgb(179, 212, 255)",
"data": [ {
"name": "English",
"y": 0,
"parentCategoryHierarchyId": "0",
"graphParentId": "105"
}, {
"name": "Social Science",
"y": 1,
"parentCategoryHierarchyId": "0",
"graphParentId": "119",
"drilldown": true
}, {
"name": "Science",
"y": 0,
"parentCategoryHierarchyId": "0",
"graphParentId": "126"
}, {
"name": "Maths",
"y": 0,
"parentCategoryHierarchyId": "0",
"graphParentId": "139"
}, {
"name": "Hindi",
"y": 0,
"parentCategoryHierarchyId": "0",
"graphParentId": "146"
} ]
}, {
type: 'column',
"name": "Needs Improvement",
"color": "rgb(204, 226, 255)",
"data": [ {
"name": "English",
"y": 0,
"parentCategoryHierarchyId": "0",
"graphParentId": "105"
}, {
"name": "Social Science",
"y": 0,
"parentCategoryHierarchyId": "0",
"graphParentId": "119",
"drilldown": true
}, {
"name": "Science",
"y": 0,
"parentCategoryHierarchyId": "0",
"graphParentId": "126"
}, {
"name": "Maths",
"y": 0,
"parentCategoryHierarchyId": "0",
"graphParentId": "139"
}, {
"name": "Hindi",
"y": 0,
"parentCategoryHierarchyId": "0",
"graphParentId": "146"
} ]
}, {
type: 'column',
"name": "Not Performing",
"color": "rgb(230, 242, 255)",
"data": [ {
"name": "English",
"y": 0,
"parentCategoryHierarchyId": "0",
"graphParentId": "105"
}, {
"name": "Social Science",
"y": 0,
"parentCategoryHierarchyId": "0",
"graphParentId": "119",
"drilldown": true
}, {
"name": "Science",
"y": 0,
"parentCategoryHierarchyId": "0",
"graphParentId": "126"
}, {
"name": "Maths",
"y": 0,
"parentCategoryHierarchyId": "0",
"graphParentId": "139"
}, {
"name": "Hindi",
"y": 0,
"parentCategoryHierarchyId": "0",
"graphParentId": "146"
} ]
} ]
};
export default {
data: () => ( {
chartOptions,
drilldownChart,
drilldownEvent,
drilldownLevel
} ),
methods: {},
computed:{
drilldown: function ( e ) {
if ( !e.seriesOptions ) {
updateGraph( true, this, e );
}
},
drillup: function ( e ) {
if ( !e.seriesOptions.flag ) {
drilldownLevel = e.seriesOptions._levelNumber;
updateGraph( false );
}
}
},
mounted() {
function updateGraph( isDrilldown, chart, e ) {
if ( isDrilldown ) {
drilldownLevel++;
drilldownChart = chart;
drilldownEvent = e;
if ( drilldownLevel === 1 ) {
var drilldowns = {
'Social Science': {
name: 'Outstanding',
color: 'rgb(102, 168, 255)',
data: [ {
name: 'Geography',
y: 7,
drilldown: true
}, {
name: 'History',
y: 4
}, {
name: 'Civics',
y: 9
} ]
}
},
drilldowns2 = {
'Social Science': {
name: 'Very Good',
color: 'rgb(128, 183, 255)',
data: [ {
name: 'Geography',
y: 4,
drilldown: true
}, {
name: 'History',
y: 8
}, {
name: 'Civics',
y: 2
} ],
},
},
drilldowns3 = {
'Social Science': {
name: 'Satisfactory',
color: 'rgb(179, 212, 255)',
data: [ {
name: 'Geography',
y: 4,
drilldown: true
}, {
name: 'History',
y: 7
}, {
name: 'Civics',
y: 1
} ],
}
},
drilldowns4 = {
'Social Science': {
name: 'Needs Improvement',
color: 'rgb(204, 226, 255)',
data: [ {
name: 'Geography',
y: 2,
drilldown: true
}, {
name: 'History',
y: 7
}, {
name: 'Civics',
y: 2
} ]
}
},
drilldowns5 = {
'Social Science': {
name: 'Not Performing',
color: 'rgb(230, 242, 255)',
data: [ {
name: 'Geography',
y: 6,
drilldown: true
}, {
name: 'History',
y: 3
}, {
name: 'Civics',
y: 0
} ],
}
},
series = drilldowns[ e.point.name ],
series2 = drilldowns2[ e.point.name ],
series3 = drilldowns3[ e.point.name ],
series4 = drilldowns4[ e.point.name ],
series5 = drilldowns5[ e.point.name ];
chart.addSingleSeriesAsDrilldown( e.point, series );
chart.addSingleSeriesAsDrilldown( e.point, series2 );
chart.addSingleSeriesAsDrilldown( e.point, series3 );
chart.addSingleSeriesAsDrilldown( e.point, series4 );
chart.addSingleSeriesAsDrilldown( e.point, series5 );
chart.applyDrilldown();
} else if ( drilldownLevel === 2 ) {
var drilldown1 = {
"Geography": {
"name": "Yes",
stacking: 'percent',
color: 'red',
"data": [ {
"name": "Q1",
"y": 1
}, {
"name": "Q2",
"y": 2
}, {
"name": "Q3",
"y": 3
}, {
"name": "Q4",
"y": 4
} ]
}
};
var drilldown2 = {
"Geography": {
"name": "No",
stacking: 'percent',
color: 'green',
"data": [ {
"name": "Q1",
"y": 1
}, {
"name": "Q2",
"y": 2
}, {
"name": "Q3",
"y": 3
}, {
"name": "Q4",
"y": 4
} ]
}
};
var drilldown3 = {
"exampleDrilldown": {
type: 'line',
"name": "Example",
color: 'black',
"data": [ {
"name": "Q1",
"y": 10
}, {
"name": "Q2",
"y": 20
}, {
"name": "Q3",
"y": 30
}, {
"name": "Q4",
"y": 40
} ]
}
}
chart.addSingleSeriesAsDrilldown( e.point, drilldown1[ e.point.name ] );
chart.addSingleSeriesAsDrilldown( e.point, drilldown2[ e.point.name ] );
chart.addSingleSeriesAsDrilldown( e.point, drilldown3[ 'exampleDrilldown' ] );
console.log( e );
chart.applyDrilldown();
}
}
}
}
}
</script>
I'm not looking for someone is doing the task instead of me, I won't learn anything. Ideas and tips are super welcomed.
Cheers
You've made several simple mistakes in your code.
1) Add updateGraph function to chart methods - that way you can invoke this method in drilldown callback
2) Save chart component reference in the chart object to be able to use updateGraph method from drilldown callback function. You can make it using mounted() hook:
mounted() {
this.$children[0].chart.vueRef = this;
}
3) Data should be a function that returns an object with properties:
data() {
return {
chartOptions,
drilldownChart,
drilldownEvent,
drilldownLevel
}
}
4) Add drilldown and drillup callbacks to chart.events object. There you can invoke updateGraph method from the chart component reference saved in mounted hook:
chart: {
"type": "column",
events: {
drilldown: function(e) {
if (!e.seriesOptions) {
this.vueRef.updateGraph(true, this, e);
}
},
drillup: function(e) {
if (!e.seriesOptions.flag) {
this.vueRef.drilldownLevel = e.seriesOptions._levelNumber;
this.vueRef.updateGraph(false);
}
}
}
}
Demo:
https://codesandbox.io/s/2w513lwpw0