From 045871b3c0b92106c2730c8a3dcefcdfd8c9fdb5 Mon Sep 17 00:00:00 2001 From: Chris Clark Date: Wed, 10 Jul 2024 13:35:33 -0400 Subject: [PATCH] remove pie charts --- docs/features.rst | 6 +- explorer/charts.py | 24 +++---- explorer/templates/explorer/preview_pane.html | 70 ++----------------- explorer/views/utils.py | 3 +- 4 files changed, 20 insertions(+), 83 deletions(-) diff --git a/docs/features.rst b/docs/features.rst index 475538d8..51811d56 100644 --- a/docs/features.rst +++ b/docs/features.rst @@ -114,10 +114,10 @@ Pivot Table exact pivot setup to share with colleagues. - Download the pivot view as a CSV. -Displaying query results as charts +Displaying query results as line chart ---------------------------------- -If the results table adheres to a certain format, the results can be displayed as a pie chart or a line chart. +If the results table adheres to a certain format, the results can be displayed as a line chart. To enable this feature, set ``EXPLORER_CHARTS_ENABLED`` setting to ``True`` and install the plotting library ``matplotlib`` with: @@ -126,7 +126,7 @@ To enable this feature, set ``EXPLORER_CHARTS_ENABLED`` setting to ``True`` and pip install "django-sql-explorer[charts]" -This will add the "Pie chart" and the "Line chart" tabs alongside the "Preview" and the "Pivot" tabs in the query results view. +This will add the "Line chart" tab alongside the "Preview" and the "Pivot" tabs in the query results view. The tabs show the respective charts if the query result table adheres to a format which the chart widget can read. Otherwise a message explaining the required format together with an example query is displayed. diff --git a/explorer/charts.py b/explorer/charts.py index 0f6ad73a..943380cb 100644 --- a/explorer/charts.py +++ b/explorer/charts.py @@ -17,20 +17,17 @@ from .models import QueryResult -def get_pie_chart(result: QueryResult) -> Optional[str]: - if len(result.data) < 1 or len(result.data[0]) < 2: - return None - not_none_rows = [row for row in result.data if row[0] is not None and row[1] is not None] - labels = [row[0] for row in not_none_rows] - values = [row[1] for row in not_none_rows] - if not is_numeric(values): - return None - fig, ax = plt.subplots(figsize=(4.5, 4.5)) - ax.pie(values, labels=labels) - return get_svg(fig) - - def get_line_chart(result: QueryResult) -> Optional[str]: + """ + Return a line chart in SVG format if the result table adheres to the expected format. + A line chart is rendered if + * there is at least on row of in the result table + * there is at least one numeric column (the first column (with index 0) does not count) + The first column is used as x-axis labels. + All other numeric columns represent a line on the chart. + The name of the column is used as the name of the line in the legend. + Not numeric columns (except the first on) are ignored. + """ if len(result.data) < 1: return None numeric_columns = [ @@ -45,6 +42,7 @@ def get_line_chart(result: QueryResult) -> Optional[str]: ax.plot(labels, [row[col_num] for row in result.data], label=result.headers[col_num]) ax.set_xlabel(result.headers[0]) ax.legend() + # Rotate x-axis labels by 20 degrees to reduce overlap for label in ax.get_xticklabels(): label.set_rotation(20) label.set_ha("right") diff --git a/explorer/templates/explorer/preview_pane.html b/explorer/templates/explorer/preview_pane.html index b6b2cb3d..a3e1e0ad 100644 --- a/explorer/templates/explorer/preview_pane.html +++ b/explorer/templates/explorer/preview_pane.html @@ -10,8 +10,7 @@ {% endif %} {% if data %} - {% if charts_enabled %} - + {% if charts_enabled and line_chart_svg %} {% endif %} {% endif %} @@ -147,71 +146,12 @@

{{ snapshots|length }} Snapshots (oldest first)

- {% if charts_enabled %} - + {% if charts_enabled and line_chart_svg %} {% endif %} diff --git a/explorer/views/utils.py b/explorer/views/utils.py index 4033e66d..f0b6da0d 100644 --- a/explorer/views/utils.py +++ b/explorer/views/utils.py @@ -1,7 +1,7 @@ from django.db import DatabaseError from explorer import app_settings -from explorer.charts import get_line_chart, get_pie_chart +from explorer.charts import get_line_chart from explorer.models import QueryFavorite from explorer.schema import schema_json_info @@ -65,7 +65,6 @@ def query_viewmodel(request, query, title=None, form=None, message=None, "unsafe_rendering": app_settings.UNSAFE_RENDERING, "fullscreen_params": fullscreen_params.urlencode(), "charts_enabled": app_settings.EXPLORER_CHARTS_ENABLED, - "pie_chart_svg": get_pie_chart(res) if app_settings.EXPLORER_CHARTS_ENABLED and has_valid_results else None, "line_chart_svg": get_line_chart(res) if app_settings.EXPLORER_CHARTS_ENABLED and has_valid_results else None, "is_favorite": is_favorite, "show_sql_by_default": app_settings.EXPLORER_SHOW_SQL_BY_DEFAULT,