# NeoDash Grouped Bar Chart Query Structure

**URL:** <https://community.neo4j.com/t/neodash-grouped-bar-chart-query-structure/61089>\
**Category:** Cypher\
**Tags:** cypher, neodash\
**Created:** [February 23, 2023, 11:19pm UTC](https://community.neo4j.com/t/neodash-grouped-bar-chart-query-structure/61089 "2023-02-23T23:19:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gq16](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/gq16/32/27751_2.png) [@gq16](https://community.neo4j.com/u/gq16)\
**Post date:** [February 23, 2023, 11:19pm UTC](https://community.neo4j.com/t/neodash-grouped-bar-chart-query-structure/61089/1 "2023-02-23T23:19:42Z")

</div>

I want to create a bar chart like this where I can see multiple quantities stacked for a given Product:

![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/3/0/303a1d9fdf733ea47aa06948831795d330016cd2.png)

**In my NeoDash Bar Chart Query, I'm returning this** (all the quantity values are properties of a single node (f:Forecast) and I'm applying some filters earlier in the query) **:**

```auto
RETURN pr.name AS Product, sum(f.contracted_qty) AS Contracted, sum(f.total_opportunities_qty) ,
sum(f.won_opportunities_qty) AS Won, sum(f.lost_opportunities_qty) AS Lost,
sum(f.open_opportunities_qty) AS, sum(f.shipped_qty) AS Shipped

```

I've been able to create grouped bar charts before in NeoDash, but the issue is that in this case, due to all the quantities being different properties within a single node, NeoDash can only display one value at a time for the given Product and I cannot group them into a stacked bar chart to get the view I'm hoping for.

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/4/0/402f0fe3010b87aab07cec309a9e2984b80b108b.png)

**Any advice on how I can aggregate/group the values so I can get the stacked bar chart like the above ?**

---

<div class="post-metadata">

**Author:** ![gq16](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/gq16/32/27751_2.png) [@gq16](https://community.neo4j.com/u/gq16)\
**Post date:** [March 16, 2023, 10:42pm UTC](https://community.neo4j.com/t/neodash-grouped-bar-chart-query-structure/61089/2 "2023-03-16T22:42:08Z")

</div>

@bennu_neo, @niels_dejong , @glilienfield  
Do you guys have any advice for this? I really want to create what was visualized above in the stacked bar chart from Tableau. The best workaround I've been able to achieve has been using the radar charts since they can visualize many properties of the same index, but it is a bit hard to look at and really understand if the data isn't close in value. I really would just like the radar chart logic to work in the bar chart.

Query Structure:  
`MATCH (p:Party { name: $neodash_party_name})-[:ASSIGNED_TO]->(f:Forecast)<-[:IS_ASSIGNED_TO]-(pr:Product), (f)-[:FORECASTED_FOR]->(i:Industry), (f)-[:HAS_MODE]->(m:Mode), (f)-[:SHIPS_TO]->(st:StateOrProvince) WHERE pr.type = CASE WHEN isEmpty($neodash_product_type) THEN pr.type ELSE $neodash_product_type END RETURN pr.type, SUM(f.total_opportunities_qty) as totalOppQty, ROUND(SUM(f.won_opportunities_qty), 2) as wonOppQty, ROUND(SUM(f.lost_opportunities_qty), 2) as lostOppQty, ROUND(SUM(f.open_opportunities_qty), 2) as openOppQty, ROUND(SUM(f.shipped_qty), 2) as shippedQty`

Radar Chart Result:

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/9/7/97a013d0c8b26facc2019603f716cef6af19e8e7.png)

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [March 17, 2023, 2:44am UTC](https://community.neo4j.com/t/neodash-grouped-bar-chart-query-structure/61089/3 "2023-03-17T02:44:40Z")

</div>

I have not used NeoDash, but I just went a reviewed on of their example dashboards that has a stacked bar graph. From what I gather, you need three values: 1) category (this is what is shown on the x-axis), 2) a group (different segments of the staged bar graph), and 3) a value (represents the height of the segment). Once you have these items returned from your query, you need to select a 'Bar Chart' for the 'Type' and set the following under 'advanced-settings': 'Grouping' On, and 2) 'Group Mode' as 'Stacked'.

It looks like your category is 'Product'. Do you want the group to have values: 'Contracted', 'Won', 'Lost', 'Shipped', and the other two missing, and the values of each corresponding to the 'sums'? This would mean the segments of each stacked bar chart would be the group values. Is this what you want?

---

<div class="post-metadata">

**Author:** ![gq16](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/gq16/32/27751_2.png) [@gq16](https://community.neo4j.com/u/gq16)\
**Post date:** [March 17, 2023, 4:53am UTC](https://community.neo4j.com/t/neodash-grouped-bar-chart-query-structure/61089/4 "2023-03-17T04:53:57Z")

</div>

You are correct. The true goal is to have just 4 values, Won, Lost, and Open Opportunity Quantities, and then the Shipped Quantities all summed with relation to a product.  
Here is the tricky part:  
Shipped Qty is a subset of the Won Quantity (how much of the opportunities that we "Won" have been shipped to the customer), so really, I wouldn't want it "grouped" with the three "Opportunity" Quantities. I want to essentially see some version of these two mockups from other platforms.  
In this one, the black horizontal line represents the shipped qty:  
 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/3/0/303a1d9fdf733ea47aa06948831795d330016cd2.png)  
In this one the second bar represents the shipped qty:  
 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/7/9/790d34a89c42514190e45fafd312247476029202.png)

This is my latest attempt, but I'm still doesn't work:

```auto
MATCH (p:Party)-[:ASSIGNED_TO]->(f:Forecast{current_flg:'Y'})<-[:IS_ASSIGNED_TO]-(pr:Product),
(f)-[:SHIPS_FROM]->(city:City),
(f)-[:SHIPS_FROM]->(state:StateOrProvince),
(f)-[:SHIPS_FROM]->(coun:Country),
(f)-[:SHIPS_TO]->(city2:City),
(f)-[:SHIPS_TO]->(state2:StateOrProvince),
(f)-[:SHIPS_TO]->(coun2:Country),
//(f)<-[:CREATED]->(marketer:ContractOwner),
//(f)-[:TITLE_TRANSFER_AT]->(t:TitleTransfer),
//(f)-[:FORECASTED_FOR]->(i:Industry),
(f)-[:HAS_MODE]->(m:Mode)

WHERE p.name = $neodash_party_name 
AND city.id = CASE WHEN isEmpty($neodash_city_id) THEN city.id ELSE $neodash_city_id END
AND f.forecast_month >= CASE WHEN isEmpty(toString($neodash_forecast_forecast_month_1)) THEN f.forecast_month ELSE $neodash_forecast_forecast_month_1 END
AND f.forecast_month <= CASE WHEN isEmpty(toString($neodash_forecast_forecast_month_2)) THEN f.forecast_month ELSE $neodash_forecast_forecast_month_2 END
//AND pr.type = CASE WHEN isEmpty($neodash_product_type) THEN pr.type ELSE $neodash_product_type END

WITH pr.type AS productType, 
round(sum(f.won_opportunities_qty),2) AS won_qty,
round(sum(f.lost_opportunities_qty),2) AS lost_qty,
round(sum(f.open_opportunities_qty),2) AS open_qty,
round(sum(f.shipped_qty),2) AS shipped_qty
WITH productType, {Won:won_qty,Lost:lost_qty,Open:open_qty,Shipped:shipped_qty} AS typeQtyMap
//WITH productType, collect([productType,won_qty]) AS won,
//collect([productType,lost_qty]) AS lost,
//collect([productType,open_qty]) AS open,
//collect([productType,shipped_qty]) AS shipped

RETURN productType, typeQtyMap, keys(typeQtyMap) AS keys

```

---

<div class="post-metadata">

**Author:** ![niels\_dejong](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/niels_dejong/32/11380_2.png) [@niels\_dejong](https://community.neo4j.com/u/niels_dejong)\
**Post date:** [March 17, 2023, 10:49am UTC](https://community.neo4j.com/t/neodash-grouped-bar-chart-query-structure/61089/5 "2023-03-17T10:49:47Z")

</div>

Hi @gq16 @glilienfield ,

Perhaps you can use a transformation in the Cypher query to shape the data into the format the bar chart expects. Something like this?

```auto
CREATE (a:Company{name: "Apple", revenue: 14, profit: 3, loss: 4})
CREATE (b:Company{name: "Banana", revenue: 17, profit: 2, loss: 6})

```

Then to visualize it:

```auto
MATCH (c:Company)
UNWIND ['revenue', 'profit', 'loss'] as group
WITH *
RETURN c.name, group, c[group] as value

```

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/2/b/2bcc662af01de80aa17f6f74396e8a7f74db3968.jpeg)

Would something like this work?

Best regards,  
Niels

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [March 17, 2023, 11:46am UTC](https://community.neo4j.com/t/neodash-grouped-bar-chart-query-structure/61089/6 "2023-03-17T11:46:57Z")

</div>

Yes, that was my thought. Just wanted see what he wanted before modifying his code.

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [March 17, 2023, 12:25pm UTC](https://community.neo4j.com/t/neodash-grouped-bar-chart-query-structure/61089/7 "2023-03-17T12:25:40Z")

</div>

Following @niels_dejong suggestion, you can modify your query to the following. BTW- some of the matches from 'f' to other entities are not used, so they should be removed for efficiency purposes.

```auto
MATCH (p:Party)-[:ASSIGNED_TO]->(f:Forecast{current_flg:'Y'})<-[:IS_ASSIGNED_TO]-(pr:Product),
(f)-[:SHIPS_FROM]->(city:City),
(f)-[:SHIPS_FROM]->(state:StateOrProvince),
(f)-[:SHIPS_FROM]->(coun:Country),
(f)-[:SHIPS_TO]->(city2:City),
(f)-[:SHIPS_TO]->(state2:StateOrProvince),
(f)-[:SHIPS_TO]->(coun2:Country),
(f)-[:HAS_MODE]->(m:Mode)

WHERE p.name = $neodash_party_name 
AND city.id = CASE WHEN isEmpty($neodash_city_id) THEN city.id ELSE $neodash_city_id END
AND f.forecast_month >= CASE WHEN isEmpty(toString($neodash_forecast_forecast_month_1)) THEN f.forecast_month ELSE $neodash_forecast_forecast_month_1 END
AND f.forecast_month <= CASE WHEN isEmpty(toString($neodash_forecast_forecast_month_2)) THEN f.forecast_month ELSE $neodash_forecast_forecast_month_2 END

WITH pr.type AS productType, 
round(sum(f.won_opportunities_qty),2) AS won_qty,
round(sum(f.lost_opportunities_qty),2) AS lost_qty,
round(sum(f.open_opportunities_qty),2) AS open_qty,
round(sum(f.shipped_qty),2) AS shipped_qty
WITH productType, {Won:won_qty,Lost:lost_qty,Open:open_qty,Shipped:shipped_qty} AS typeQtyMap

UNWIND keys(typeQtyMap) as key
RETURN productType as category, key as group, typeQtyMap[key] as value

```

---

<div class="post-metadata">

**Author:** ![gq16](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/gq16/32/27751_2.png) [@gq16](https://community.neo4j.com/u/gq16)\
**Post date:** [March 17, 2023, 2:00pm UTC](https://community.neo4j.com/t/neodash-grouped-bar-chart-query-structure/61089/8 "2023-03-17T14:00:03Z")

</div>

@glilienfield and @niels_dejong you guys are heroes. Thanks for your help! I definitely will use this logic if I'm in a similar situation going forward.

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/8/3/8380ee0ac4e8e6a318c4832346784b05179c1feb.png)
