clojurescriptreact-vis

Insert the result of a sequence generated with for in a FlexibleXYPlot in clojurescript


In this question (How to return two customSVGSeries from a single function in clojurescript), I learned how to insert the result of a function creating two customSVGSeries into an XYPlot.

Now I want to insert the result of a for calling that same function:

(into [:> rvis/FlexibleXYPlot {...}] 
      (doall (for [[id {:keys [x y texto]}] etiquetas]
               (crear-etiqueta id x y texto))))

Where etiquetas is a map with this form:

{:etiqueta-1 {:x 0, :y 0, :texto ["176.6"]}, :etiqueta-2 {:x 1, :y 2, :texto ["HidrĂ³geno"]}}

and crear-etiqueta is the function returning two customSGVSeries. The problem is that using the code above, nothing is shown in the plot.

I uploaded a repo with a MWE: https://github.com/lopezsolerluis/annie-test-for-stackoverflow


Solution

  • Expanding on my comment to the question.

    Using for with two collections:

    (into [:> rvis/FlexibleXYPlot {...}] 
          (for [[id {:keys [x y texto]}] etiquetas
                series (crear-etiqueta id x y texto)]
            series))
    

    for will iterate over eqieuetas and for each item it will destructure it and pass the result into crear-etiqueta, which returns a collection. for then iterates over that collection and assigns the value of each item to series. Then the body is finally evaluated, which just returns the value of series.

    Using the mapcat transducer:

    (into [:> rvis/FlexibleXYPlot {...}]
          (mapcat (fn [[id {:keys [x y texto]}]]
                    (crear-etiqueta id x y texto)))
          etiquetas)
    

    I won't go into the details of how it works - it's all documented here. I definitely recommend reading that reference in full because of immense usefulness of transducers in many contexts.