从clojurescript(canvasjs)调用javascript

时间:2014-03-29 13:39:35

标签: javascript interop clojurescript canvasjs

我无法将以下针对canvasjs的javscript调用转换为clojurescript:

var chart = new CanvasJS.Chart("chartContainer", {

  title:{
    text: "Fruits sold in First Quarter"              
  },
  data: [//array of dataSeries              
    { //dataSeries object

     /*** Change type "column" to "bar", "area", "line" or "pie"***/
     type: "column",
     dataPoints: [
     { label: "banana", y: 18 },
     { label: "orange", y: 29 },
     { label: "apple", y: 40 },                                    
     { label: "mango", y: 34 },
     { label: "grape", y: 24 }
     ]
   }
   ]
 });

有人可以指出我正确的方向,如何做或提供一个例子?

最诚挚的问候, 斯文

1 个答案:

答案 0 :(得分:1)

您遇到的问题是什么?这是简单的js interop:

(def chart (js/CanvasJS.Chart. "chartContainer" (clj->js {
  :title {:text "Fruits sold in First Quarter"}
  :data [{:type "column"
          :dataPoints [{ :label "banana" y: 18 }
                       { :label "orange" y: 29 }
                       { :label "apple"  y: 40 }
                       { :label "mango"  y: 34 }
                       { :label "grape"  y: 24 }]}]})))

编译为:

cljs.user.chart = (new CanvasJS.Chart("chartContainer", cljs.core.clj__GT_js.call(
  null, new cljs.core.PersistentArrayMap(null, 2, [new cljs.core.Keyword(
      null, "title", "title", 1124275658), new cljs.core.PersistentArrayMap(
      null, 1, [new cljs.core.Keyword(null, "text", "text", 1017460895),
        "Fruits sold in First Quarter"
      ], null), new cljs.core.Keyword(null, "data", "data", 1016980252),
    new cljs.core.PersistentVector(null, 1, 5, cljs.core.PersistentVector
      .EMPTY_NODE, [new cljs.core.PersistentArrayMap(null, 2, [new cljs.core
        .Keyword(null, "type", "type", 1017479852), "column", new cljs
        .core.Keyword(null, "dataPoints", "dataPoints", 2200126623),
        new cljs.core.PersistentVector(null, 5, 5, cljs.core.PersistentVector
          .EMPTY_NODE, [new cljs.core.PersistentArrayMap.fromArray([
            new cljs.core.Keyword(null, "label", "label",
              1116631654), "banana", cljs.user.y_COLON_, 18
          ], true, false), new cljs.core.PersistentArrayMap.fromArray(
            [new cljs.core.Keyword(null, "label", "label",
              1116631654), "orange", cljs.user.y_COLON_, 29], true,
            false), new cljs.core.PersistentArrayMap.fromArray([new cljs
            .core.Keyword(null, "label", "label", 1116631654),
            "apple", cljs.user.y_COLON_, 40
          ], true, false), new cljs.core.PersistentArrayMap.fromArray(
            [new cljs.core.Keyword(null, "label", "label",
              1116631654), "mango", cljs.user.y_COLON_, 34], true,
            false), new cljs.core.PersistentArrayMap.fromArray([new cljs
            .core.Keyword(null, "label", "label", 1116631654),
            "grape", cljs.user.y_COLON_, 24
          ], true, false)], null)
      ], null)], null)
  ], null))))

您还可以使用#js标记的文字并创建简单的js数据(即使它看起来更丑):

(def chart (js/CanvasJS.Chart. "chartContainer" #js{
  :title #js{:text "Fruits sold in First Quarter"}
  :data #js[#js{:type "column"
  ...