forked from explorerhq/sql-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
76 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from typing import Optional | ||
from io import BytesIO | ||
|
||
import matplotlib.pyplot as plt | ||
from matplotlib.figure import Figure | ||
|
||
from .models import QueryResult | ||
|
||
|
||
def get_pie_chart(result: QueryResult) -> Optional[str]: | ||
if len(result.data) < 1 \ | ||
or not isinstance(result.data[0][0], str) \ | ||
or not isinstance(result.data[0][1], (int, float)): | ||
return None | ||
labels = [row[0] for row in result.data] | ||
values = [row[1] for row in result.data] | ||
fig, ax = plt.subplots() | ||
ax.pie(values, labels=labels) | ||
return get_csv_as_hex(fig) | ||
|
||
|
||
def get_csv_as_hex(fig: Figure) -> str: | ||
buffer = BytesIO() | ||
fig.savefig(buffer, format='svg') | ||
buffer.seek(0) | ||
graph = buffer.getvalue().decode('utf-8') | ||
buffer.close() | ||
return graph |
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