Ver Fonte

make charts multiple per line

Daniel Sheffield há 8 meses atrás
pai
commit
e54185b3b5

+ 38 - 0
sqlpage/grocery/trend/Quantity.sql

@@ -0,0 +1,38 @@
+SET height = '500';
+SET type = 'line';
+SET marker = 4;
+SET max_line_series = 50;
+
+
+SELECT 'chart' AS component
+, 'Cost per '||$unit AS title
+, $height AS height
+, '$/'||$unit AS ytitle
+, CASE
+    WHEN (
+      SELECT count(DISTINCT product)
+      FROM sqlpage_txn
+      WHERE unit = $unit
+    ) > $max_line_series::numeric
+    THEN 'scatter' ELSE $type
+  END AS type
+, $log_scale = 'true' AS logarithmic
+, TRUE AS time
+, CASE
+    WHEN $log_scale = 'true'
+    THEN 1 ELSE 0
+  END AS ymin
+, ((
+    SELECT max(price/quantity)
+    FROM sqlpage_txn
+    WHERE unit = $unit
+  )/10 + 1)::int AS ystep
+, $marker AS marker
+;
+SELECT product AS series
+, ts AS x
+, to_char(price/quantity, 'FM999999999.000') AS y
+FROM sqlpage_txn
+WHERE unit = $unit
+ORDER BY ts
+;

+ 1 - 0
sqlpage/grocery/volume.sql

@@ -1,2 +1,3 @@
 SET title = 'Volume';
+SET input_log_scale = 'hidden';
 SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/entry.sql') AS properties;

+ 35 - 0
sqlpage/grocery/volume/Expense.sql

@@ -0,0 +1,35 @@
+SET height = '500';
+SET type = 'pie';
+
+SET group_by = (CASE
+WHEN (SELECT count(DISTINCT "group") FROM sqlpage_txn) > 2
+THEN 'Group'
+WHEN (SELECT count(DISTINCT category) FROM sqlpage_txn) > 2
+THEN 'Category'
+ELSE 'Product'
+END);
+SET total = (SELECT sum(price) FROM sqlpage_txn);
+SELECT 'chart' AS component
+, 'Share of $'||to_char($total::numeric, 'FM999999999.00') AS title
+, $height AS height
+, $type AS type
+, TRUE AS labels
+;
+SELECT "group" AS series
+, sum(price) AS value
+FROM sqlpage_txn
+WHERE $group_by = 'Group'
+GROUP BY "group"
+;
+SELECT category AS series
+, sum(price) AS value
+FROM sqlpage_txn
+WHERE $group_by = 'Category'
+GROUP BY category
+;
+SELECT product AS series
+, sum(price) AS value
+FROM sqlpage_txn
+WHERE $group_by = 'Product'
+GROUP BY product
+;

+ 35 - 0
sqlpage/grocery/volume/Quantity.sql

@@ -0,0 +1,35 @@
+SET height = '500';
+SET type = 'pie';
+
+SET group_by = (CASE
+WHEN (SELECT count(DISTINCT "group") FROM sqlpage_txn WHERE unit = $unit) > 2
+THEN 'Group'
+WHEN (SELECT count(DISTINCT category) FROM sqlpage_txn WHERE unit = $unit) > 2
+THEN 'Category'
+ELSE 'Product'
+END);
+SET total = (SELECT sum(quantity) FROM sqlpage_txn WHERE unit = $unit);
+SELECT 'chart' AS component
+, 'Share of '||to_char($total::numeric, 'FM999999999.00')||' '||$unit AS title
+, $height AS height
+, $type AS type
+, TRUE AS labels
+;
+SELECT "group" AS series
+, sum(quantity) AS value
+FROM sqlpage_txn
+WHERE $group_by = 'Group' AND unit = $unit
+GROUP BY "group"
+;
+SELECT category AS series
+, sum(quantity) AS value
+FROM sqlpage_txn
+WHERE $group_by = 'Category' AND unit = $unit
+GROUP BY category
+;
+SELECT product AS series
+, sum(quantity) AS value
+FROM sqlpage_txn
+WHERE $group_by = 'Product' AND unit = $unit
+GROUP BY product
+;

+ 1 - 0
sqlpage/sqlpage/internal/theme.sql

@@ -1,5 +1,6 @@
 SELECT 'shell' AS component
 , 'dark' AS theme
+, 'fluid' AS layout
 , $title AS title
 , '/grocery/'||lower($title)||'.sql' AS link
 , '/grocery/favicon.svg' AS image

+ 15 - 81
sqlpage/sqlpage/pages/Trend.sql

@@ -1,87 +1,21 @@
-SET height = '750';
-SET type = 'line';
-SET marker = 4;
-SET max_line_series = 50;
-
-SET unit_count_chart = (SELECT v#>>'{}' FROM json_array_elements($unit_count::json) j(v) LIMIT 1);
-SET display_by_count = (SELECT count(*) FROM json_array_elements($unit_count::json) j(v)) == 1
-AND (EXISTS (SELECT 1 FROM sqlpage_txn WHERE unit = $unit_count_chart LIMIT 1));
-SET display_by_mass = (EXISTS (SELECT 1 FROM sqlpage_txn WHERE unit = $unit_mass LIMIT 1));
-SET display_by_volume = (EXISTS (SELECT 1 FROM sqlpage_txn WHERE unit = $unit_volume LIMIT 1));
-
-SELECT 'chart' AS component
-, 'Cost by Weight' AS title
-, $height AS height
-, '$/'||$unit_mass AS ytitle
-, CASE
-    WHEN (
-      SELECT count(DISTINCT product)
-      FROM sqlpage_txn
-      WHERE unit = $unit_mass
-    ) > $max_line_series::numeric
-    THEN 'scatter' ELSE $type
-  END AS type
-, $log_scale = 'true' AS logarithmic
-, TRUE AS time
-, 0 AS ymin
-, $marker AS marker
-;
-SELECT product AS series
-, ts AS x
-, to_char(price/quantity, 'FM999999999.000') AS y
-FROM sqlpage_txn
-WHERE unit = $unit_mass
-ORDER BY ts
+SELECT 'card' AS component
+, 'Volume by Expendature and Quantity' AS title
+, 2 AS columns
 ;
 
-SELECT 'chart' AS component
-, 'Cost by Volume' AS title
-, $height AS height
-, '$/'||$unit_volume AS ytitle
-, CASE
-    WHEN (
-      SELECT count(DISTINCT product)
-      FROM sqlpage_txn
-      WHERE unit = $unit_volume
-    ) > $max_line_series::numeric
-    THEN 'scatter' ELSE $type
-  END AS type
-, $log_scale::bool AS logarithmic
-, TRUE AS time
-, 0 AS ymin
-, $marker AS marker
+SELECT '/grocery/trend/Quantity.sql?unit='||$unit_mass||'&log_scale='||$log_scale||'&_sqlpage_embed' AS embed
+, '' AS footer
+WHERE EXISTS (SELECT 1 FROM sqlpage_txn WHERE unit = $unit_mass LIMIT 1)
 ;
-SELECT product AS series
-, ts AS x
-, to_char(price/quantity, 'FM999999999.000') AS y
-FROM sqlpage_txn
-WHERE unit = $unit_volume
-ORDER BY ts
+
+SELECT '/grocery/trend/Quantity.sql?unit='||$unit_volume||'&log_scale='||$log_scale||'&_sqlpage_embed' AS embed
+, '' AS footer
+WHERE EXISTS (SELECT 1 FROM sqlpage_txn WHERE unit = $unit_volume LIMIT 1)
 ;
 
-SELECT 'chart' AS component
-, 'Cost by Count' AS title
-, $height AS height
-, '$/'||$unit_count_chart AS ytitle
-, CASE
-    WHEN (
-      SELECT count(DISTINCT product)
-      FROM sqlpage_txn
-      WHERE unit = $unit_count_chart
-    ) > $max_line_series::numeric
-    THEN 'scatter' ELSE $type
-  END AS type
-, $log_scale::bool AS logarithmic
-, TRUE AS time
-, 0 AS ymin
-, $marker AS marker
-WHERE $display_by_count::bool
+SET unit_count_chart = (SELECT v#>>'{}' FROM json_array_elements($unit_count::json) j(v) LIMIT 1);
+SELECT '/grocery/trend/Quantity.sql?unit='||$unit_count_chart||'&log_scale='||$log_scale||'&_sqlpage_embed' AS embed
+, '' AS footer
+WHERE EXISTS (SELECT 1 FROM sqlpage_txn WHERE unit = $unit_count_chart LIMIT 1)
+AND (SELECT count(DISTINCT v#>>'{}') FROM json_array_elements($unit_count::json) j(v)) == 1
 ;
-SELECT product AS series
-, ts AS x
-, to_char(price/quantity, 'FM999999999.000') AS y
-FROM sqlpage_txn
-WHERE $display_by_count::bool
-AND unit = $unit_count_chart
-ORDER BY ts
-;

+ 18 - 113
sqlpage/sqlpage/pages/Volume.sql

@@ -1,119 +1,24 @@
-SET height = '500';
-SET type = 'pie';
-
-SET unit_count_chart = (SELECT v#>>'{}' FROM json_array_elements($unit_count::json) j(v) LIMIT 1);
-SET display_by_count = (SELECT count(*) FROM json_array_elements($unit_count::json) j(v)) == 1
-AND (EXISTS (SELECT 1 FROM sqlpage_txn WHERE unit = $unit_count_chart LIMIT 1));
-SET display_by_mass = (EXISTS (SELECT 1 FROM sqlpage_txn WHERE unit = $unit_mass LIMIT 1));
-SET display_by_volume = (EXISTS (SELECT 1 FROM sqlpage_txn WHERE unit = $unit_volume LIMIT 1));
-
-SET group_by = (CASE
-WHEN (SELECT count(DISTINCT "group") FROM sqlpage_txn WHERE unit = $unit_mass) > 2
-THEN 'Group'
-WHEN (SELECT count(DISTINCT category) FROM sqlpage_txn WHERE unit = $unit_mass) > 2
-THEN 'Category'
-ELSE 'Product'
-END);
-SET total = (SELECT sum(quantity) FROM sqlpage_txn WHERE unit = $unit_mass);
-SELECT 'chart' AS component
-, 'Share by Weight - Total: '||$total||' '||$unit_mass AS title
-, $height AS height
-, $type AS type
-, TRUE AS labels
-WHERE $display_by_mass::bool
-;
-SELECT "group" AS series
-, sum(quantity) AS value
-FROM sqlpage_txn
-WHERE $display_by_mass::bool
-AND $group_by = 'Group' AND unit = $unit_mass
-GROUP BY "group"
-;
-SELECT category AS series
-, sum(quantity) AS value
-FROM sqlpage_txn
-WHERE $display_by_mass::bool
-AND $group_by = 'Category' AND unit = $unit_mass
-GROUP BY category
-;
-SELECT product AS series
-, sum(quantity) AS value
-FROM sqlpage_txn
-WHERE $display_by_mass::bool
-AND $group_by = 'Product' AND unit = $unit_mass
-GROUP BY product
+SELECT 'card' AS component
+, 'Volume by Expendature and Quantity' AS title
+, 2 AS columns
 ;
+SELECT '/grocery/volume/Expense.sql?_sqlpage_embed' AS embed
+, '' AS footer
+WHERE EXISTS (SELECT 1 FROM sqlpage_txn LIMIT 1);
 
-SET group_by = (CASE
-WHEN (SELECT count(DISTINCT "group") FROM sqlpage_txn WHERE unit = $unit_volume) > 2
-THEN 'Group'
-WHEN (SELECT count(DISTINCT category) FROM sqlpage_txn WHERE unit = $unit_volume) > 2
-THEN 'Category'
-ELSE 'Product'
-END);
-SET total = (SELECT sum(quantity) FROM sqlpage_txn WHERE unit = $unit_volume);
-SELECT 'chart' AS component
-, 'Share by Volume - Total: '||$total||' '||$unit_volume AS title
-, $height AS height
-, $type AS type
-, TRUE AS labels
-WHERE $display_by_volume::bool
-;
-SELECT "group" AS series
-, sum(quantity) AS value
-FROM sqlpage_txn
-WHERE $display_by_volume::bool
-AND $group_by = 'Group' AND unit = $unit_volume
-GROUP BY "group"
-;
-SELECT category AS series
-, sum(quantity) AS value
-FROM sqlpage_txn
-WHERE $display_by_volume::bool
-AND $group_by = 'Category' AND unit = $unit_volume
-GROUP BY category
-;
-SELECT product AS series
-, sum(quantity) AS value
-FROM sqlpage_txn
-WHERE $display_by_volume::bool
-AND $group_by = 'Product' AND unit = $unit_volume
-GROUP BY product
+SELECT '/grocery/volume/Quantity.sql?unit='||$unit_mass||'&_sqlpage_embed' AS embed
+, '' AS footer
+WHERE EXISTS (SELECT 1 FROM sqlpage_txn WHERE unit = $unit_mass LIMIT 1)
 ;
 
-SET group_by = (CASE
-WHEN (SELECT count(DISTINCT "group") FROM sqlpage_txn WHERE unit = $unit_count_chart) > 2
-THEN 'Group'
-WHEN (SELECT count(DISTINCT category) FROM sqlpage_txn WHERE unit = $unit_count_chart) > 2
-THEN 'Category'
-ELSE 'Product'
-END);
-SET total = (SELECT sum(quantity) FROM sqlpage_txn WHERE unit = $unit_count_chart);
-SELECT 'chart' AS component
-, 'Share by Volume - Total: '||$total||' '||$unit_count_chart AS title
-, $height AS height
-, $type AS type
-, TRUE AS labels
-WHERE $display_by_count::bool
+SELECT '/grocery/volume/Quantity.sql?unit='||$unit_volume||'&_sqlpage_embed' AS embed
+, '' AS footer
+WHERE EXISTS (SELECT 1 FROM sqlpage_txn WHERE unit = $unit_volume LIMIT 1)
 ;
-SELECT "group" AS series
-, sum(quantity) AS value
-FROM sqlpage_txn
-WHERE $display_by_count::bool
-AND $group_by = 'Group' AND unit = $unit_count_chart
-GROUP BY "group"
-;
-SELECT category AS series
-, sum(quantity) AS value
-FROM sqlpage_txn
-WHERE $display_by_count::bool
-AND $group_by = 'Category' AND unit = $unit_count_chart
-GROUP BY category
+
+SET unit_count_chart = (SELECT v#>>'{}' FROM json_array_elements($unit_count::json) j(v) LIMIT 1);
+SELECT '/grocery/volume/Quantity.sql?unit='||$unit_count_chart||'&_sqlpage_embed' AS embed
+, '' AS footer
+WHERE EXISTS (SELECT 1 FROM sqlpage_txn WHERE unit = $unit_count_chart LIMIT 1)
+AND (SELECT count(DISTINCT v#>>'{}') FROM json_array_elements($unit_count::json) j(v)) == 1
 ;
-SELECT product AS series
-, sum(quantity) AS value
-FROM sqlpage_txn
-WHERE $display_by_count::bool
-AND $group_by = 'Product' AND unit = $unit_count_chart
-GROUP BY product
-;