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
;
