1
+ [[_building_bar_charts]]
2
+ == 条形图
1
3
2
- == Building Bar Charts
4
+ 聚合还有一个令人激动的特性就是能够十分容易地将它们转换成图表和图形。((("bar charts, building from aggregations", id="ix_barcharts", range="startofrange")))((("aggregations", "building bar charts from")))本章中,
5
+ 我们正在通过示例数据来来完成各种各样的聚合分析,最终,我们将会发现聚合功能是非常强大的。
3
6
4
- One of the exciting aspects of aggregations are how easily they are converted
5
- into charts and graphs.((("bar charts, building from aggregations", id="ix_barcharts", range="startofrange")))((("aggregations", "building bar charts from"))) In this chapter, we are focusing
6
- on various analytics that we can wring out of our example dataset. We will also
7
- demonstrate the types of charts aggregations can power.
7
+ 直方图 ++histogram++ 特别有用。((("buckets", "histogram")))((("histogram bucket")))((("histograms"))) 它本质上是一个条形图,如果有创建报表或分析仪表盘的经验,那么我们会毫无疑问的发现里面有一些图表是条形图。
8
+ 创建直方图需要指定一个区间,如果我们要为售价创建一个直方图,可以将间隔设为 20,000。这样做将会在每个 $20,000 档创建一个新桶,然后文档会被分到对应的桶中。
8
9
9
- The ++histogram++ bucket is particularly useful.((("buckets", "histogram")))((("histogram bucket")))((("histograms"))) Histograms are essentially
10
- bar charts, and if you've ever built a report or analytics dashboard, you
11
- undoubtedly had a few bar charts in it. The histogram works by specifying an interval. If we were histogramming sale
12
- prices, you might specify an interval of 20,000. This would create a new bucket
13
- every $20,000. Documents are then sorted into buckets.
10
+ 对于仪表盘来说,我们希望知道每个售价区间内汽车的销量。我们还会想知道每个售价区间内汽车所带来的收入,可以通过对每个区间内已售汽车的售价求和得到。
14
11
15
- For our dashboard, we want to know how many cars sold in each price range. We
16
- would also like to know the total revenue generated by that price bracket. This is
17
- calculated by summing the price of each car sold in that interval.
18
-
19
- To do this, we use a `histogram` and a nested `sum` metric:
12
+ 可以用 `histogram` 和一个嵌套的 `sum` 度量得到我们想要的答案:
20
13
21
14
[source,js]
22
15
--------------------------------------------------
@@ -41,23 +34,17 @@ GET /cars/transactions/_search
41
34
}
42
35
--------------------------------------------------
43
36
// SENSE: 300_Aggregations/30_histogram.json
44
- <1> The `histogram` bucket requires two parameters: a numeric field, and an
45
- interval that defines the bucket size.
37
+ <1> `histogram` 桶要求两个参数:一个数值字段以及一个定义桶大小间隔。
46
38
// Mention use of "size" to get back just the top result?
47
- <2> A `sum` metric is nested inside each price range, which will show us the
48
- total revenue for that bracket
39
+ <2> `sum` 度量嵌套在每个售价区间内,用来显示每个区间内的总收入。
49
40
50
- As you can see, our query is built around the `price` aggregation, which contains
51
- a `histogram` bucket. This bucket requires a numeric field to calculate
52
- buckets on, and an interval size. The interval defines how "wide" each bucket
53
- is. An interval of 20000 means we will have the ranges `[0-19999, 20000-39999, ...]`.
41
+ 如我们所见,查询是围绕 `price` 聚合构建的,它包含一个 `histogram` 桶。它要求字段的类型必须是数值型的同时需要设定分组的间隔范围。
42
+ 间隔设置为 20,000 意味着我们将会得到如 `[0-19999, 20000-39999, ...]` 这样的区间。
54
43
55
- Next, we define a nested metric inside the histogram. This is a `sum` metric, which
56
- will sum up the `price` field from each document landing in that price range.
57
- This gives us the revenue for each price range, so we can see if our business
58
- makes more money from commodity or luxury cars.
44
+ 接着,我们在直方图内定义嵌套的度量,这个 `sum` 度量,它会对落入某一具体售价区间的文档中 `price` 字段的值进行求和。
45
+ 这可以为我们提供每个售价区间的收入,从而可以发现到底是普通家用车赚钱还是奢侈车赚钱。
59
46
60
- And here is the response:
47
+ 响应结果如下:
61
48
62
49
[source,js]
63
50
--------------------------------------------------
@@ -93,30 +80,23 @@ And here is the response:
93
80
}
94
81
--------------------------------------------------
95
82
96
- The response is fairly self-explanatory, but it should be noted that the
97
- histogram keys correspond to the lower boundary of the interval. The key `0`
98
- means `0-19,999`, the key `20000` means `20,000-39,999`, and so forth.
83
+ 结果很容易理解,不过应该注意到直方图的键值是区间的下限。键 `0` 代表区间 `0-19,999` ,键 `20000` 代表区间 `20,000-39,999` ,等等。
99
84
100
85
[NOTE]
101
86
=====================
102
- You'll notice that empty intervals, such as $40,000-60,000, is missing in the
103
- response. The `histogram` bucket omits these by default, since it could lead
104
- to the unintended generation of potentially enormous output.
87
+ 我们可能会注意到空的区间,比如:$40,000-60,000,没有出现在响应中。 `histogram` 桶默认会忽略它,因为它有可能会导致不希望的潜在错误输出。
105
88
106
- We'll discuss how to include empty buckets in the next section, <<_returning_empty_buckets>>.
89
+ 我们会在下一小节中讨论如何包括空桶。返回空桶 <<_returning_empty_buckets>> 。
107
90
=====================
108
91
109
- Graphically, you could represent the preceding data in the histogram shown in <<barcharts-histo1>>.
92
+ 可以在图 <<barcharts-histo1>> 中看到以上数据直方图的图形化表示。
110
93
111
94
[[barcharts-histo1]]
112
95
.Sales and Revenue per price bracket
113
96
image::images/elas_28in01.png["Sales and Revenue per price bracket"]
114
97
115
- Of course, you can build bar charts with any aggregation that emits categories
116
- and statistics, not just the `histogram` bucket. Let's build a bar chart of the
117
- top 10 most popular makes, and their average price, and then calculate the standard
118
- error to add error bars on our chart. This will use the `terms` bucket and
119
- an `extended_stats` ((("extended_stats metric")))metric:
98
+ 当然,我们可以为任何聚合输出的分类和统计结果创建条形图,而不只是 `直方图` 桶。让我们以最受欢迎 10 种汽车以及它们的平均售价、标准差这些信息创建一个条形图。
99
+ 我们会用到 `terms` 桶和 `extended_stats` ((("extended_stats metric")))度量:
120
100
121
101
[source,js]
122
102
----
@@ -141,20 +121,17 @@ GET /cars/transactions/_search
141
121
}
142
122
----
143
123
144
- This will return a list of makes (sorted by popularity) and a variety of statistics
145
- about each. In particular, we are interested in `stats.avg`, `stats.count`,
146
- and `stats.std_deviation`. Using((("standard error, calculating"))) this information, we can calculate the standard error:
124
+ 上述代码会按受欢迎度返回制造商列表以及它们各自的统计信息。我们对其中的 `stats.avg` 、 `stats.count` 和 `stats.std_deviation` 信息特别感兴趣,并用 ((("standard error, calculating"))) 它们计算出标准差:
147
125
148
126
................................
149
127
std_err = std_deviation / count
150
128
................................
151
129
152
- This will allow us to build a chart like <<barcharts-bar1>>.
130
+ 创建图表如图 <<barcharts-bar1>> 。
153
131
154
132
[[barcharts-bar1]]
155
133
.Average price of all makes, with error bars
156
134
image::images/elas_28in02.png["Average price of all makes, with error bars"]
157
135
158
136
159
137
((("bar charts, building from aggregations", range="endofrange", startref="ix_barcharts")))
160
-
0 commit comments