Skip to content

Commit 97471f6

Browse files
Cristine Guadelupejosevalim
andauthored
:text_color for heatmap (#67)
Co-authored-by: José Valim <[email protected]>
1 parent 088c71d commit 97471f6

File tree

2 files changed

+148
-7
lines changed

2 files changed

+148
-7
lines changed

lib/vega_lite/data.ex

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ defmodule VegaLite.Data do
109109
@doc """
110110
Returns the specification of a heat map for a given data and a list of fields to be encoded.
111111
112-
As a specialized chart, the heatmap expects an `:x` and `:y` and optionally a `:color` and a
113-
`:text` field. Defaults to `:nominal` for the axes and `:quantitative` for color and text if
114-
types are not specified.
112+
As a specialized chart, the heatmap expects an `:x` and `:y` and optionally a `:color`, a
113+
`:text` and a `:text_color` fields. Defaults to `:nominal` for the axes and `:quantitative`
114+
for color and text if types are not specified.
115115
116116
## Examples
117117
@@ -140,8 +140,13 @@ defmodule VegaLite.Data do
140140
defp heatmap_no_data(vl, data, fields, fun) do
141141
cols = columns_for(data)
142142
fields = normalize_fields(fields, fun)
143-
text_fields = Keyword.take(fields, [:text, :x, :y])
144-
rect_fields = Keyword.delete(fields, :text)
143+
text_fields = Keyword.take(fields, [:text, :text_color, :x, :y])
144+
rect_fields = Keyword.drop(fields, [:text, :text_color])
145+
146+
{text_color, text_fields} = Keyword.pop(text_fields, :text_color)
147+
148+
text_fields =
149+
if text_color, do: Keyword.put_new(text_fields, :color, text_color), else: text_fields
145150

146151
text_layer = if fields[:text], do: [encode_layer(cols, :text, text_fields)], else: []
147152
rect_layer = [encode_layer(cols, :rect, rect_fields)]
@@ -163,8 +168,8 @@ defmodule VegaLite.Data do
163168
Returns the specification of a density heat map for a given data and a list of fields to be encoded.
164169
165170
As a specialized chart, the density heatmap expects the `:x` and `:y` axes, a `:color` field and
166-
optionally a `:text` field. All data must be `:quantitative` and the default aggregation
167-
function is `:count`.
171+
optionally a `:text` and a `:text_color` fields. All data must be `:quantitative` and the default
172+
aggregation function is `:count`.
168173
169174
## Examples
170175

test/vega_lite/data_test.exs

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,74 @@ defmodule VegaLite.DataTest do
264264
assert vl == Data.heatmap(@data, x: "height", y: "weight", color: "height", text: "height")
265265
end
266266

267+
test "with text_color" do
268+
vl =
269+
Vl.new()
270+
|> Vl.data_from_values(@data, only: ["height", "weight"])
271+
|> Vl.layers([
272+
Vl.new()
273+
|> Vl.mark(:rect)
274+
|> Vl.encode_field(:x, "height", type: :nominal)
275+
|> Vl.encode_field(:y, "weight", type: :nominal)
276+
|> Vl.encode_field(:color, "height", type: :quantitative),
277+
Vl.new()
278+
|> Vl.mark(:text)
279+
|> Vl.encode_field(:x, "height", type: :nominal)
280+
|> Vl.encode_field(:y, "weight", type: :nominal)
281+
|> Vl.encode_field(:text, "height", type: :quantitative)
282+
|> Vl.encode_field(:color, "height", type: :quantitative)
283+
])
284+
285+
assert vl ==
286+
Data.heatmap(@data,
287+
x: "height",
288+
y: "weight",
289+
color: "height",
290+
text: "height",
291+
text_color: "height"
292+
)
293+
end
294+
295+
test "with text_color with condition" do
296+
vl =
297+
Vl.new()
298+
|> Vl.data_from_values(@data, only: ["height", "weight"])
299+
|> Vl.layers([
300+
Vl.new()
301+
|> Vl.mark(:rect)
302+
|> Vl.encode_field(:x, "height", type: :nominal)
303+
|> Vl.encode_field(:y, "weight", type: :nominal)
304+
|> Vl.encode_field(:color, "height", type: :quantitative),
305+
Vl.new()
306+
|> Vl.mark(:text)
307+
|> Vl.encode_field(:x, "height", type: :nominal)
308+
|> Vl.encode_field(:y, "weight", type: :nominal)
309+
|> Vl.encode_field(:text, "height", type: :quantitative)
310+
|> Vl.encode_field(:color, "height",
311+
type: :quantitative,
312+
condition: [
313+
[test: "datum['height'] < 0", value: :white],
314+
[test: "datum['height'] >= 0", value: :black]
315+
]
316+
)
317+
])
318+
319+
assert vl ==
320+
Data.heatmap(@data,
321+
x: "height",
322+
y: "weight",
323+
color: "height",
324+
text: "height",
325+
text_color: [
326+
field: "height",
327+
condition: [
328+
[test: "datum['height'] < 0", value: :white],
329+
[test: "datum['height'] >= 0", value: :black]
330+
]
331+
]
332+
)
333+
end
334+
267335
test "with title and extra fields" do
268336
vl =
269337
Vl.new(title: "Heatmap")
@@ -450,6 +518,74 @@ defmodule VegaLite.DataTest do
450518
)
451519
end
452520

521+
test "with text_color" do
522+
vl =
523+
Vl.new()
524+
|> Vl.data_from_values(@data, only: ["height", "weight"])
525+
|> Vl.layers([
526+
Vl.new()
527+
|> Vl.mark(:rect)
528+
|> Vl.encode_field(:x, "height", type: :quantitative, bin: true)
529+
|> Vl.encode_field(:y, "weight", type: :quantitative, bin: true)
530+
|> Vl.encode_field(:color, "height", type: :quantitative, aggregate: :count),
531+
Vl.new()
532+
|> Vl.mark(:text)
533+
|> Vl.encode_field(:x, "height", type: :quantitative, bin: true)
534+
|> Vl.encode_field(:y, "weight", type: :quantitative, bin: true)
535+
|> Vl.encode_field(:text, "height", type: :quantitative, aggregate: :count)
536+
|> Vl.encode_field(:color, "height", type: :quantitative)
537+
])
538+
539+
assert vl ==
540+
Data.density_heatmap(@data,
541+
x: "height",
542+
y: "weight",
543+
color: "height",
544+
text: "height",
545+
text_color: "height"
546+
)
547+
end
548+
549+
test "with text_color with condition" do
550+
vl =
551+
Vl.new()
552+
|> Vl.data_from_values(@data, only: ["height", "weight"])
553+
|> Vl.layers([
554+
Vl.new()
555+
|> Vl.mark(:rect)
556+
|> Vl.encode_field(:x, "height", type: :quantitative, bin: true)
557+
|> Vl.encode_field(:y, "weight", type: :quantitative, bin: true)
558+
|> Vl.encode_field(:color, "height", type: :quantitative, aggregate: :count),
559+
Vl.new()
560+
|> Vl.mark(:text)
561+
|> Vl.encode_field(:x, "height", type: :quantitative, bin: true)
562+
|> Vl.encode_field(:y, "weight", type: :quantitative, bin: true)
563+
|> Vl.encode_field(:text, "height", type: :quantitative, aggregate: :count)
564+
|> Vl.encode_field(:color, "height",
565+
type: :quantitative,
566+
condition: [
567+
[test: "datum['height'] < 0", value: :white],
568+
[test: "datum['height'] >= 0", value: :black]
569+
]
570+
)
571+
])
572+
573+
assert vl ==
574+
Data.density_heatmap(@data,
575+
x: "height",
576+
y: "weight",
577+
color: "height",
578+
text: "height",
579+
text_color: [
580+
field: "height",
581+
condition: [
582+
[test: "datum['height'] < 0", value: :white],
583+
[test: "datum['height'] >= 0", value: :black]
584+
]
585+
]
586+
)
587+
end
588+
453589
test "with specified aggregate for text" do
454590
vl =
455591
Vl.new()

0 commit comments

Comments
 (0)