我正在通过尝试使用 d3.js 从 JSON 结构中绘制一些网格来了解 Clojurescript 的工作原理。我正在使用笔画来访问 d3。
JSON 看起来像这样:
[[{"players":{"0":{"rep":0},"1":{"rep":0}}},{"players":{"0":{"rep":0},"1":{"rep":0}}},
{"players":{"0":{"rep":0},"1":{"rep":0}}},{"players":{"0":{"rep":0},"1":{"rep":0}}}],
[{"players":{"0":{"rep":0},"1":{"rep":0}}},{"players":{"0":{"rep":0},"1":{"rep":0}}},
{"players":{"0":{"rep":0},"1":{"rep":0}}},{"players":{"0":{"rep":0},"1":{"rep":0}}}],
[{"players":{"0":{"rep":0},"1":{"rep":0}}},{"players":{"0":{"rep":0},"1":{"rep":0}}},
{"players":{"0":{"rep":0},"1":{"rep":0}}},{"players":{"0":{"rep":0},"1":{"rep":0}}}],
[{"players":{"0":{"rep":0},"1":{"rep":0}}},{"players":{"0":{"rep":0},"1":{"rep":0}}},
{"players":{"0":{"rep":0},"1":{"rep":0}}},{"players":{"0":{"rep":0},"1":{"rep":0}}}]]
它代表一个 4 x 4 的网格。我正在尝试向单元格添加值,例如高度、宽度、x 和 y 坐标,以便将数据传递给 d3 以进行绘制只是一个简单的情况。
例如,它看起来像这样:
[[{"width":32,"height":32,"x":0,"y":0,"value":{"players":{"0":{"rep":0},"1":{"rep":0}}}},
{"width":32,"height":32,"x":32,"y":0,"value":{"players":{"0":{"rep":0},"1":{"rep":0}}}},...
通常我会使用转换函数映射结构,以将单元格从其当前值转换为新形式,但是这种方法似乎不起作用。我已经尝试过map-indexed: (map-indexed #(doto %2 (aset "width" %1)) row)
,但这似乎无法正确转换值。我很可能错误地访问或设置了这些值。
当前的代码迭代如下所示:
(defn board->grid [grid-width grid-height board square]
(let [x-length (count board)
y-length (count (first board))
same (min (/ grid-width x-length) (/ grid-height y-length))
grid-item-width (if square same (/ grid-width x-length))
grid-item-height (if square same (/ grid-height y-length))
start-x (/ grid-item-width 2)
start-y (/ grid-item-height 2)
values (array)
grid (array)
data (js->clj board :keywordize-keys true)]
(doseq [x (range x-length)
y (range y-length)]
(let [current-cell (aget data y x)]
(.log js/console (apply str (aset (aget data y x) "a" "b")))
(.push grid (aget data y x))))
(.text ($ :#status) grid)))
任何帮助,将不胜感激!或者更好的是,更好的方法的建议,我不禁觉得我做的有点不对!