SciNim / nim-plotly

plotly wrapper for nim-lang
https://scinim.github.io/nim-plotly/
MIT License
173 stars 15 forks source link

marker symbols #54

Open sheckman16 opened 4 years ago

sheckman16 commented 4 years ago

It would be nice to be able to set marker symbols, not just color and size. I think this is a 6 line change:

$ git diff
diff --git a/src/plotly/api.nim b/src/plotly/api.nim
index 413850f..f4a3f8f 100644
--- a/src/plotly/api.nim
+++ b/src/plotly/api.nim
@@ -379,6 +379,11 @@ func `%`*(m: Marker): JsonNode =
     fields["color"] = % m.colorVals
     fields["colorscale"] = serializeColormap(m.colormap, m.customColormap)
     fields["showscale"] = % true
+  if m.symbol.len > 0:
+    if m.symbol.len == 1:
+      fields["symbol"] = % m.symbol[0]
+    else:
+      fields["symbol"] = % m.symbol

   result = JsonNode(kind: JObject, fields: fields)

diff --git a/src/plotly/plotly_types.nim b/src/plotly/plotly_types.nim
index e44bb99..4970e56 100644
--- a/src/plotly/plotly_types.nim
+++ b/src/plotly/plotly_types.nim
@@ -153,6 +153,7 @@ type
   Marker*[T: SomeNumber] = ref object
     size*: seq[T]
     color*: seq[Color]
+    symbol*: seq[int]
     # alternatively use sequence of values defining color based on one of
     # the color maps
     colorVals*: seq[T]
brentp commented 4 years ago

I agree this would be useful. I'd rather if the symbol was an enum since 300 has no apparent meaning, but looks like there are a lot of possible values: https://plot.ly/javascript/reference/#scatter-marker-symbol

so, 44 values with +100, +200, for "open", "dot" respectively.

sheckman16 commented 4 years ago

Like this?

git diff plotly_types.nim
diff --git a/src/plotly/plotly_types.nim b/src/plotly/plotly_types.nim
index e44bb99..6c0264b 100644
--- a/src/plotly/plotly_types.nim
+++ b/src/plotly/plotly_types.nim
@@ -150,9 +150,65 @@ type
       errorsMinus*: seq[T]
       errorsPlus*: seq[T]

+  MarkerSymbol* = enum
+    circle = 0, square = 1, diamond = 2, cross = 3, x = 4, triangle_up = 5,
+    triangle_down = 6, triangle_left = 7, triangle_right = 8,
+    triangle_ne = 9, triangle_se = 10, triangle_sw = 11,
+    triangle_nw = 12, pentagon = 13, hexagon = 14, hexagon2 = 15,
+    octagon = 16, star = 17, hexagram = 18, star_triangle_up = 19,
+    star_triangle_down = 20, star_square = 21, star_diamond = 22,
+    diamond_tall = 23, diamond_wide = 24, hourglass = 25, bowtie = 26,
+    circle_cross = 27, circle_x = 28, square_cross = 29, square_x = 30,
+    diamond_cross = 31, diamond_x = 32, cross_thin = 33, x_thin = 34,
+    asterisk = 35, hash = 36, y_up = 37, y_down = 38, y_left = 39,
+    y_right = 40, line_ew = 41, line_ns = 42, line_ne = 43, line_nw = 44,
+    circle_open = 100, square_open = 101, diamond_open = 102,
+    cross_open = 103, x_open = 104, triangle_up_open = 105,
+    triangle_down_open = 106, triangle_left_open = 107,
+    triangle_right_open = 108, triangle_ne_open = 109,
+    triangle_se_open = 110, triangle_sw_open = 111,
+    triangle_nw_open = 112, pentagon_open = 113, hexagon_open = 114,
+    hexagon2_open = 115, octagon_open = 116, star_open = 117,
+    hexagram_open = 118, star_triangle_up_open = 119,
+    star_triangle_down_open = 120, star_square_open = 121,
+    star_diamond_open = 122, diamond_tall_open = 123,
+    diamond_wide_open = 124, hourglass_open = 125,
+    bowtie_open = 126, circle_cross_open = 127,
+    circle_x_open = 128, square_cross_open = 129,
+    square_x_open = 130, diamond_cross_open = 131,
+    diamond_x_open = 132, cross_thin_open = 133, x_thin_open = 134,
+    asterisk_open = 135, hash_open = 136, y_up_open = 137,
+    y_down_open = 138, y_left_open = 139, y_right_open = 140,
+    line_ew_open = 141, line_ns_open = 142, line_ne_open = 143,
+    line_nw_open = 144, circle_dot = 200, square_dot = 201,
+    diamond_dot = 202, cross_dot = 203, x_dot = 204,
+    triangle_up_dot = 205, triangle_down_dot = 206,
+    triangle_left_dot = 207, triangle_right_dot = 208,
+    triangle_ne_dot = 209, triangle_se_dot = 210,
+    triangle_sw_dot = 211, triangle_nw_dot = 212,
+    pentagon_dot = 213, hexagon_dot = 214, hexagon2_dot = 215,
+    octagon_dot = 216, star_dot = 217, hexagram_dot = 218,
+    star_triangle_up_dot = 219, star_triangle_down_dot = 220,
+    star_square_dot = 221, star_diamond_dot = 222,
+    diamond_tall_dot = 223, diamond_wide_dot = 224,
+    hash_dot = 236, circle_open_dot = 300, square_open_dot = 301,
+    diamond_open_dot = 302, cross_open_dot = 303,
+    x_open_dot = 304, triangle_up_open_dot = 305,
+    triangle_down_open_dot = 306, triangle_left_open_dot = 307,
+    triangle_right_open_dot = 308, triangle_ne_open_dot = 309,
+    triangle_se_open_dot = 310, triangle_sw_open_dot = 311,
+    triangle_nw_open_dot = 312, pentagon_open_dot = 313,
+    hexagon_open_dot = 314, hexagon2_open_dot = 315,
+    octagon_open_dot = 316, star_open_dot = 317,
+    hexagram_open_dot = 318, star_triangle_up_open_dot = 319,
+    star_triangle_down_open_dot = 320, star_square_open_dot = 321,
+    star_diamond_open_dot = 322, diamond_tall_open_dot = 323,
+    diamond_wide_open_dot = 324, hash_open_dot = 336
+
   Marker*[T: SomeNumber] = ref object
     size*: seq[T]
     color*: seq[Color]
+    symbol*: seq[MarkerSymbol]
     # alternatively use sequence of values defining color based on one of
     # the color maps
     colorVals*: seq[T]
brentp commented 4 years ago

Hah. yes, except make it pure then add a MarkerSymbolStyle enum like:

MarkerSymbolStyle* {.pure.} = enum
  open = 100
  dot = 200

then override + like:

proc `+`*(a:MarkerSymbol, b:MarkerSymbolStyle): MarkerSymbol =
  return MarkerSymbol(a.int + b.int)

so that you can do :

var s = MarkerSymbol.octagon | MarkerSymbolStyle.open | MarkerSymbolStyle.dot

would you put this into a pull request?