-
-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: move vega charts to API #185
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b25035e
wip: working on creating api for charts. It works :)
Marc-AntoineA fa47a4b
Merge branch 'main' of github.com:openfoodfacts/search-a-licious into…
Marc-AntoineA 91d3f72
feat: improve PR, almost done
Marc-AntoineA 5dea4e5
fix: fix checks
Marc-AntoineA 0ab9246
clean: autoreview, remove console.log, print and debug code
Marc-AntoineA ded8a3c
clean: remove two typos
Marc-AntoineA 3da3450
Merge branch 'main' of github.com:openfoodfacts/search-a-licious into…
Marc-AntoineA File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
from ._types import ChartsInfos, SuccessSearchResponse | ||
|
||
|
||
def build_charts( | ||
search_result: SuccessSearchResponse, | ||
charts_names: list[str] | None, | ||
) -> ChartsInfos: | ||
charts: ChartsInfos = {} | ||
aggregations = search_result.aggregations | ||
|
||
if charts_names is None or aggregations is None: | ||
return charts | ||
|
||
for chart_name in charts_names: | ||
agg_data = aggregations.get(chart_name, {}) | ||
|
||
buckets = agg_data.get("buckets", []) if agg_data else [] | ||
|
||
# Filter unknown values | ||
values = [ | ||
{"category": bucket["key"], "amount": bucket["doc_count"]} | ||
for bucket in buckets | ||
if bucket["key"] != "unknown" | ||
] | ||
values.sort(key=lambda x: x["category"]) | ||
|
||
charts[chart_name] = { | ||
"$schema": "https://vega.github.io/schema/vega/v5.json", | ||
"title": chart_name, | ||
"autosize": {"type": "fit", "contains": "padding"}, | ||
"signals": [ | ||
{ | ||
"name": "width", | ||
"init": "containerSize()[0]", | ||
"on": [{"events": "window:resize", "update": "containerSize()[0]"}], | ||
}, | ||
{ | ||
"name": "tooltip", | ||
"value": {}, | ||
"on": [ | ||
{"events": "rect:pointerover", "update": "datum"}, | ||
{"events": "rect:pointerout", "update": "{}"}, | ||
], | ||
}, | ||
], | ||
"height": 140, | ||
"padding": 5, | ||
"data": [ | ||
{ | ||
"name": "table", | ||
"values": values, | ||
}, | ||
], | ||
"scales": [ | ||
{ | ||
"name": "xscale", | ||
"type": "band", | ||
"domain": {"data": "table", "field": "category"}, | ||
"range": "width", | ||
"padding": 0.05, | ||
"round": True, | ||
}, | ||
{ | ||
"name": "yscale", | ||
"domain": {"data": "table", "field": "amount"}, | ||
"nice": True, | ||
"range": "height", | ||
}, | ||
], | ||
"axes": [ | ||
{"orient": "bottom", "scale": "xscale", "domain": False, "ticks": False} | ||
], | ||
"marks": [ | ||
{ | ||
"type": "rect", | ||
"from": {"data": "table"}, | ||
"encode": { | ||
"enter": { | ||
"x": {"scale": "xscale", "field": "category"}, | ||
"width": {"scale": "xscale", "band": 1}, | ||
"y": {"scale": "yscale", "field": "amount"}, | ||
"y2": {"scale": "yscale", "value": 0}, | ||
}, | ||
"update": { | ||
"fill": {"value": "steelblue"}, | ||
}, | ||
"hover": { | ||
"fill": {"value": "red"}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
"type": "text", | ||
"encode": { | ||
"enter": { | ||
"align": {"value": "center"}, | ||
"baseline": {"value": "bottom"}, | ||
"fill": {"value": "#333"}, | ||
}, | ||
"update": { | ||
"x": { | ||
"scale": "xscale", | ||
"signal": "tooltip.category", | ||
"band": 0.5, | ||
}, | ||
"y": { | ||
"scale": "yscale", | ||
"signal": "tooltip.amount", | ||
"offset": -2, | ||
}, | ||
"text": {"signal": "tooltip.amount"}, | ||
"fillOpacity": [ | ||
{"test": "datum === tooltip", "value": 0}, | ||
{"value": 1}, | ||
], | ||
}, | ||
}, | ||
}, | ||
], | ||
} | ||
|
||
return charts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For me, you have to give data by API and then build the options in frontend like this :
This allows to avoid chart options for every graph
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After discussion, we will keep it like this right now. Might be re-discussed with Alex.