Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

[BOT-1807] Add metabot treemap chart support (#77568)

Tyler Plude98424e1a61f0
diff --git a/src/metabase/metabot/tools/charts.clj b/src/metabase/metabot/tools/charts.clj
index e23dc2cbc74..a652ca43f4b 100644
--- a/src/metabase/metabot/tools/charts.clj
+++ b/src/metabase/metabot/tools/charts.clj
@@ -21,7 +21,7 @@
"<instructions>\n" (instructions/chart-created-instructions chart-id) "\n</instructions>")))
(def ^:private chart-type-enum
- [:enum "table" "bar" "line" "pie" "sunburst" "area" "combo"
+ [:enum "table" "bar" "line" "pie" "sunburst" "treemap" "area" "combo"
"row" "pivot" "scatter" "waterfall" "sankey" "scalar"
"smartscalar" "gauge" "progress" "funnel" "object" "map"])
diff --git a/src/metabase/metabot/tools/charts/create.clj b/src/metabase/metabot/tools/charts/create.clj
index bf7f9e4b2e0..81bd42a757c 100644
--- a/src/metabase/metabot/tools/charts/create.clj
+++ b/src/metabase/metabot/tools/charts/create.clj
@@ -9,7 +9,7 @@
(def ^:private valid-chart-types
"Valid chart types supported by Metabase."
- #{:table :bar :line :pie :sunburst :area :combo :row :pivot
+ #{:table :bar :line :pie :sunburst :treemap :area :combo :row :pivot
:scatter :waterfall :sankey :scalar :smartscalar :gauge
:progress :funnel :object :map})
diff --git a/src/metabase/metabot/tools/charts/edit.clj b/src/metabase/metabot/tools/charts/edit.clj
index e493ef27b63..8c0ccd6b718 100644
--- a/src/metabase/metabot/tools/charts/edit.clj
+++ b/src/metabase/metabot/tools/charts/edit.clj
@@ -5,7 +5,7 @@
(def ^:private valid-chart-types
"Valid chart types supported by Metabase."
- #{:table :bar :line :pie :sunburst :area :combo :row :pivot
+ #{:table :bar :line :pie :sunburst :treemap :area :combo :row :pivot
:scatter :waterfall :sankey :scalar :smartscalar :gauge
:progress :funnel :object :map})
diff --git a/test/metabase/metabot/tools/charts/create_test.clj b/test/metabase/metabot/tools/charts/create_test.clj
index c888d0dcf6b..18696f7c1df 100644
--- a/test/metabase/metabot/tools/charts/create_test.clj
+++ b/test/metabase/metabot/tools/charts/create_test.clj
@@ -30,7 +30,7 @@
(let [queries-state {"q-456" {:query-id "q-456"
:sql "SELECT COUNT(*) FROM users"
:database 1}}]
- (doseq [chart-type [:line :pie :table :scatter :area]]
+ (doseq [chart-type [:line :pie :table :scatter :area :treemap]]
(let [result (create-chart/create-chart
{:query-id "q-456"
:chart-type chart-type
diff --git a/test/metabase/metabot/tools/charts/edit_test.clj b/test/metabase/metabot/tools/charts/edit_test.clj
index 9a544dd9c5d..d97e0ae2178 100644
--- a/test/metabase/metabot/tools/charts/edit_test.clj
+++ b/test/metabase/metabot/tools/charts/edit_test.clj
@@ -40,7 +40,7 @@
(let [mp (mt/metadata-provider)
charts-state {"chart-456" {:chart-id "chart-456"
:queries [(lib/native-query mp "SELECT * FROM orders")]}}]
- (doseq [new-type [:pie :table :scatter :area :sunburst]]
+ (doseq [new-type [:pie :table :scatter :area :sunburst :treemap]]
(let [{:keys [result]} (edit-chart/edit-chart
{:chart-id "chart-456"
:new-chart-type new-type
diff --git a/test/metabase/metabot/tools/charts_test.clj b/test/metabase/metabot/tools/charts_test.clj
index c13dd42a725..f80499fe4c1 100644
--- a/test/metabase/metabot/tools/charts_test.clj
+++ b/test/metabase/metabot/tools/charts_test.clj
@@ -12,14 +12,17 @@
(def ^:private stub-query
{:database 1 :type "query" :query {:source-table 1}})
-(defn- run-create-chart [profile-id]
- (let [memory (atom {:state {:queries {"q-1" stub-query}}
- :context {}})]
- (binding [shared/*memory-atom* memory
- shared/*profile-id* profile-id]
- (charts/create-chart-tool {:data_source {:query_id "q-1"}
- :viz_settings {:chart_type "bar"}
- :title "Orders by month"}))))
+(defn- run-create-chart
+ ([profile-id]
+ (run-create-chart profile-id "bar"))
+ ([profile-id chart-type]
+ (let [memory (atom {:state {:queries {"q-1" stub-query}}
+ :context {}})]
+ (binding [shared/*memory-atom* memory
+ shared/*profile-id* profile-id]
+ (charts/create-chart-tool {:data_source {:query_id "q-1"}
+ :viz_settings {:chart_type chart-type}
+ :title "Orders by month"})))))
(deftest create-chart-inline-viz-test
(testing "emits a single generated_entity card part in the NLQ profile"
@@ -45,6 +48,12 @@
(is (str/starts-with? (:data (first parts)) "/question#"))
(is (not-any? #(= "generated_entity" (:data-type %)) parts)))))
+(deftest create-chart-treemap-test
+ (testing "the tool schema accepts treemap as a chart type"
+ (let [result (run-create-chart :nlq "treemap")]
+ (is (= :treemap (get-in result [:structured-output :chart-type])))
+ (is (= "treemap" (get-in result [:data-parts 0 :data :display]))))))
+
(deftest edit-chart-query-fallback-test
(testing "edit_chart resolves the query from queries-state when chart memory has no query"
(let [memory (atom {:state {:queries {"q-1" stub-query}