ClickHouse function reference

sparkbar

Generates a frequency histogram as an ASCII bar chart for a set of values and their repetition rates.

Syntax:

sparkbar(buckets[, min_x, max_x])(x, y)

Alias:

  • sparkBar

Arguments:

  • buckets (UInt8): The number of segments in the histogram.
  • min_x (numeric, optional): The start of the interval.
  • max_x (numeric, optional): The end of the interval.
  • x (numeric): The field containing the values.
  • y (numeric): The field containing the frequency of values.

Returns:

An ASCII bar chart representing the frequency histogram.

  • The function plots a frequency histogram for values x and their repetition rates y over the interval [min_x, max_x].
  • If no interval is specified, the minimum x value is used as the interval start, and the maximum x value as the interval end.
  • Values outside the specified interval are ignored.
  • Repetitions for all x falling into the same bucket are averaged, so data should be pre-aggregated.
  • Negative repetitions are ignored.

Example:

-- Create a sample table with taco sales data
SELECT
    sparkbar(7)(sale_date, tacos_sold) AS taco_sales_chart
FROM
(
    SELECT
        toDate('2023-05-01') AS sale_date, 50 AS tacos_sold
    UNION ALL
    SELECT
        toDate('2023-05-02') AS sale_date, 75 AS tacos_sold
    UNION ALL
    SELECT
        toDate('2023-05-03') AS sale_date, 60 AS tacos_sold
    UNION ALL
    SELECT
        toDate('2023-05-04') AS sale_date, 80 AS tacos_sold
    UNION ALL
    SELECT
        toDate('2023-05-05') AS sale_date, 100 AS tacos_sold
    UNION ALL
    SELECT
        toDate('2023-05-06') AS sale_date, 90 AS tacos_sold
    UNION ALL
    SELECT
        toDate('2023-05-07') AS sale_date, 70 AS tacos_sold
    UNION ALL
    SELECT
        toDate('2023-05-08') AS sale_date, 85 AS tacos_sold
    UNION ALL
    SELECT
        toDate('2023-05-09') AS sale_date, 95 AS tacos_sold
) AS taco_sales;

Result:

| taco_sales_chart |
|------------------|
| ▅▅▇█▆▇█          |

In this example, the sparkbar function creates an ASCII bar chart showing the trend of taco sales over the 9-day period, divided into 9 buckets. Each character in the result represents the relative height of taco sales for that period.