> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-fbfa8bee.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> 该函数会在区间 `[min_x, max_x]` 内，根据值 `x` 及其对应的出现频率 `y` 绘制频率直方图。

# sparkbar

<div id="sparkbar">
  ## sparkbar
</div>

引入版本：v21.11.0

该函数根据值 `x` 及这些值在区间 `[min_x, max_x]` 内的出现频率 `y` 绘制频率直方图。
落入同一分桶的所有 `x` 的出现次数会取平均值，因此数据应预先聚合。
负的出现次数会被忽略。

如果未指定区间，则使用最小的 `x` 作为区间起点、最大的 `x` 作为区间终点。
否则，区间之外的值会被忽略。

**语法**

```sql theme={null}
sparkbar(buckets[, min_x, max_x])(x, y)
```

**别名**: `sparkBar`

**参数**

* `buckets` — 分段数量。[`(U)Int*`](/zh/reference/data-types/int-uint)
* `min_x` — 可选。区间起点。[`(U)Int*`](/zh/reference/data-types/int-uint) 或 [`Float*`](/zh/reference/data-types/float) 或 [`Decimal`](/zh/reference/data-types/decimal)
* `max_x` — 可选。区间终点。[`(U)Int*`](/zh/reference/data-types/int-uint) 或 [`Float*`](/zh/reference/data-types/float) 或 [`Decimal`](/zh/reference/data-types/decimal)

**参数说明**

* `x` — 值所在的字段。[`const String`](/zh/reference/data-types/string)
* `y` — 值频率所在的字段。[`const String`](/zh/reference/data-types/string)

**返回值**

返回频率直方图。[`String`](/zh/reference/data-types/string)

**示例**

**不指定区间时**

```sql title=Query theme={null}
CREATE TABLE spark_bar_data (`value` Int64, `event_date` Date) ENGINE = MergeTree ORDER BY event_date;

INSERT INTO spark_bar_data VALUES (1,'2020-01-01'), (3,'2020-01-02'), (4,'2020-01-02'), (-3,'2020-01-02'), (5,'2020-01-03'), (2,'2020-01-04'), (3,'2020-01-05'), (7,'2020-01-06'), (6,'2020-01-07'), (8,'2020-01-08'), (2,'2020-01-11');

SELECT sparkbar(9)(event_date, cnt) FROM (SELECT sum(value) AS cnt, event_date FROM spark_bar_data GROUP BY event_date);
```

```response title=Response theme={null}
┌─sparkbar(9)(event_date, cnt)─┐
│ ▂▅▂▃▆█  ▂                    │
└──────────────────────────────┘
```

**指定时间间隔**

```sql title=Query theme={null}
SELECT sparkbar(9, toDate('2020-01-01'), toDate('2020-01-10'))(event_date, cnt) FROM (SELECT sum(value) AS cnt, event_date FROM spark_bar_data GROUP BY event_date);
```

```response title=Response theme={null}
┌─sparkbar(9, toDate('2020-01-01'), toDate('2020-01-10'))(event_date, cnt)─┐
│ ▂▅▂▃▇▆█                                                                  │
└──────────────────────────────────────────────────────────────────────────┘
```
