Skip to content

Commit 8ba7f46

Browse files
authored
Allow strings in datasets (#11)
* Allow spaces in strings in datasets Useful for labels Closes #10
1 parent af5514d commit 8ba7f46

File tree

5 files changed

+25
-7
lines changed

5 files changed

+25
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ by adding `gnuplot` to your list of dependencies in `mix.exs`:
100100
```elixir
101101
def deps do
102102
[
103-
{:gnuplot, "~> 0.19.87"}
103+
{:gnuplot, "~> 1.19.88"}
104104
]
105105
end
106106
```

examples/so327576.exs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ defmodule BarChart do
44
@moduledoc "Chart from https://stackoverflow.com/a/11551808/3366"
55

66
def run do
7-
87
chart = [
98
[:set, :term, :png, :size, '512,512'],
109
[:set, :output, Path.join("/tmp", "barchart.PNG")],
@@ -13,11 +12,11 @@ defmodule BarChart do
1312
[:plot, "-", :using, '1:3:xtic(2)', :with, :boxes]
1413
]
1514

16-
dataset = [[0, "label", 100], [1, "label2", 450], [2, "bar_label", 75]]
15+
dataset = [[0, "label", 100], [1, "label2", 450], [2, "bar label", 75]]
1716

1817
plot(chart, [dataset])
1918
end
2019
end
2120

22-
# mix run examples/so327576.exs
21+
# mix run examples/so327576.exs && open /tmp/barchart.PNG
2322
BarChart.run()

lib/gnuplot/dataset.ex

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,19 @@ defmodule Gnuplot.Dataset do
3131

3232
defp format_point(point) do
3333
point
34-
|> Enum.map(&Kernel.to_string/1)
34+
|> Enum.map(&to_str/1)
3535
|> Enum.join(" ")
3636
end
37+
38+
defp to_str(f) when is_float(f), do: Float.to_string(f)
39+
defp to_str(i) when is_integer(i), do: Integer.to_string(i)
40+
defp to_str(s) when is_binary(s) do
41+
if contains_space?(s) do
42+
"\"" <> s <> "\""
43+
else
44+
s
45+
end
46+
end
47+
48+
def contains_space?(s), do: String.contains?(s, " ")
3749
end

mix.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule Gnuplot.MixProject do
44
def project do
55
[
66
app: :gnuplot,
7-
version: "0.19.87",
7+
version: "1.19.88",
88
elixir: "~> 1.6",
99
start_permanent: Mix.env() == :prod,
1010
description: "Interface between Elixir and Gnuplot graphing library",
@@ -41,7 +41,7 @@ defmodule Gnuplot.MixProject do
4141
licenses: ["EPL-2.0"],
4242
links: %{
4343
"GitHub" => "https://github.com/devstopfix/gnuplot-elixir",
44-
"Travis" => "https://travis-ci.org/devstopfix/gnuplot-elixir"
44+
"Travis CI" => "https://travis-ci.org/devstopfix/gnuplot-elixir"
4545
}
4646
]
4747
end

test/gnuplot_test.exs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,11 @@ defmodule GnuplotTest do
8383
expected = "set xrange [-3:3];\nset yrange [-3:3];\nsplot sin(x) * cos(y)"
8484
assert {:ok, expected} == G.plot(plot)
8585
end
86+
87+
test "Strings with spaces in datasets" do
88+
input = [[0, "label", 100], [1, "label2", 450], [2, "bar label", 75]]
89+
expected = ["0 label 100", "\n", "1 label2 450", "\n", "2 \"bar label\" 75", "\ne\n"]
90+
assert expected ==
91+
[input] |> D.format_datasets() |> Enum.to_list()
92+
end
8693
end

0 commit comments

Comments
 (0)