Skip to content
Snippets Groups Projects
Unverified Commit a98eb1d8 authored by Cam Saul's avatar Cam Saul Committed by GitHub
Browse files

Static viz: allow rendering non-timeseries line charts (#18279)

* Static viz: respect custom X and Y axis labels

* Static viz: allow rendering non-timeseries line charts

* Test fixes :wrench:
parent aec94624
No related branches found
No related tags found
No related merge requests found
......@@ -56,6 +56,15 @@ function categorical_bar(data, labels, settings) {
});
}
function categorical_line(data, labels, settings) {
return StaticViz.RenderChart("categorical/line", {
data: toJSArray(data),
labels: toJSMap(labels),
accessors: positional_accessors,
settings: JSON.parse(settings),
});
}
function categorical_donut(rows, colors) {
return StaticViz.RenderChart("categorical/donut", {
data: toJSArray(rows),
......
......@@ -6,7 +6,6 @@
[metabase.pulse.render.image-bundle :as image-bundle]
[metabase.pulse.render.png :as png]
[metabase.pulse.render.style :as style]
[metabase.types :as types]
[metabase.util.i18n :refer [trs tru]]
[metabase.util.urls :as urls]
[schema.core :as s]))
......@@ -97,10 +96,9 @@
(and (= @col-sample-count 2)
(> @row-sample-count 1)
(types/temporal-field? @col-1)
(number-field? @col-2)
(not= display-type :waterfall))
(chart-type :sparkline "result has 2 cols (%s (temporal) and %s (number)) and > 1 row" (col-description @col-1) (col-description @col-2))
(not (#{:waterfall :pie} display-type)))
(chart-type :sparkline "result has 2 cols (%s and %s (number)) and > 1 row" (col-description @col-1) (col-description @col-2))
(and (= @col-sample-count 2)
(number-field? @col-2)
......
......@@ -19,7 +19,7 @@
[schema.core :as s])
(:import [java.text DecimalFormat DecimalFormatSymbols]))
(def error-rendered-info
(def ^:private error-rendered-info
"Default rendered-info map when there is an error displaying a card. Is a delay due to the call to `trs`."
(delay {:attachments
nil
......@@ -455,11 +455,14 @@
labels (datetime/format-temporal-string-pair timezone-id
(map x-axis-rowfn last-rows)
(x-axis-rowfn cols))
render-fn (if (isa? (-> cols x-axis-rowfn :effective_type) :type/Temporal)
js-svg/timelineseries-line
js-svg/categorical-line)
image-bundle (image-bundle/make-image-bundle
render-type
(js-svg/timelineseries-line (mapv (juxt x-axis-rowfn y-axis-rowfn) rows)
(x-and-y-axis-label-info x-col y-col viz-settings)
(->js-viz x-col y-col viz-settings)))]
(render-fn (mapv (juxt x-axis-rowfn y-axis-rowfn) rows)
(x-and-y-axis-label-info x-col y-col viz-settings)
(->js-viz x-col y-col viz-settings)))]
{:attachments
(when image-bundle
(image-bundle/image-bundle->attachment image-bundle))
......
......@@ -102,7 +102,7 @@
(defn timelineseries-line
"Clojure entrypoint to render a timeseries line char. Rows should be tuples of [datetime numeric-value]. Labels is a
map of {:left \"left-label\" :right \"right-label\"}. Returns a byte array of a png file."
map of {:left \"left-label\" :botton \"bottom-label\"}. Returns a byte array of a png file."
[rows labels settings]
(let [svg-string (.asString (js/execute-fn-name @context "timeseries_line" rows
(map (fn [[k v]] [(name k) v]) labels)
......@@ -111,16 +111,25 @@
(defn timelineseries-bar
"Clojure entrypoint to render a timeseries bar char. Rows should be tuples of [datetime numeric-value]. Labels is a
map of {:left \"left-label\" :right \"right-label\"}. Returns a byte array of a png file."
map of {:left \"left-label\" :botton \"bottom-label\"}. Returns a byte array of a png file."
[rows labels settings]
(let [svg-string (.asString (js/execute-fn-name @context "timeseries_bar" rows
(map (fn [[k v]] [(name k) v]) labels)
(json/generate-string settings)))]
(svg-string->bytes svg-string)))
(defn categorical-line
"Clojure entrypoint to render a categorical line chart. Rows should be tuples of [stringable numeric-value]. Labels is
a map of {:left \"left-label\" :botton \"bottom-label\". Returns a byte array of a png file."
[rows labels settings]
(let [svg-string (.asString (js/execute-fn-name @context "categorical_line" rows
(map (fn [[k v]] [(name k) v]) labels)
(json/generate-string settings)))]
(svg-string->bytes svg-string)))
(defn categorical-bar
"Clojure entrypoint to render a categorical bar chart. Rows should be tuples of [stringable numeric-value]. Labels is
a map of {:left \"left-label\" :right \"right-label\". Returns a byte array of a png file. "
a map of {:left \"left-label\" :botton \"bottom-label\". Returns a byte array of a png file."
[rows labels settings]
(let [svg-string (.asString (js/execute-fn-name @context "categorical_bar" rows
(map (fn [[k v]] [(name k) v]) labels)
......
......@@ -50,12 +50,21 @@
{:cols [{:base_type :type/Text}
{:base_type :type/Number}]
:rows [["A" 2]]})))
;; timeseries line chart
(is (= :sparkline
(render/detect-pulse-chart-type {:display :line}
{:cols [{:base_type :type/Temporal}
{:base_type :type/Number}]
:rows [[#t "2020" 2]
[#t "2021" 3]]})))
;; Category line chart
(is (= :sparkline
(render/detect-pulse-chart-type {:display :line}
{:cols [{:base_type :type/Text}
{:base_type :type/Number}]
:rows [["Red" 2]
["Blue" 3]]})))
(is (= :categorical/donut
(render/detect-pulse-chart-type {:display :pie}
{:cols [{:base_type :type/Text}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment