Skip to content
Permalink
Browse files

let chartkick generates right javascript function

  • Loading branch information...
yfractal committed Jun 4, 2017
1 parent c68b71c commit 97a9a987e061c9834d5ac60769c1a66fa8dcaba0
Showing with 78 additions and 11 deletions.
  1. +4 −3 project.clj
  2. +63 −5 src/chartkick/core.clj
  3. +11 −3 test/chartkick/core_test.clj
@@ -1,6 +1,7 @@
(defproject chartkick "0.1.0-SNAPSHOT"
:description "FIXME: write description"
(defproject chartkick "0.1.0"
:description "Create beautiful JavaScript charts with one line of Clojure"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.7.0"]])
:dependencies [[org.clojure/clojure "1.7.0"]
[org.clojure/data.json "0.2.6"]])
@@ -1,6 +1,64 @@
(ns chartkick.core)
(ns chartkick.core
(:require [clojure.data.json :as json]))

(defn foo
"I don't do a whole lot."
[x]
(println x "Hello, World!"))
(defn- chartkick-tag
[id, height]
(format "<div id=\"%s\" style=\"height: %s; text-align: center; color: #999; line-height: #{height}; font-size: 14px; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helvetica, sans-serif;\">Loading...</div>"
id height))

(defn- chartkick-script
[class id data-source options-json]
(format "<script type=\"text/javascript\">new Chartkick.%s('%s', %s, %s);</script>"
class, id, data-source, options-json))

(defn- generate-element-id
[]
(str (java.util.UUID/randomUUID)))

(defn chartkick-chart
([class data-source]
(chartkick-chart class data-source {}))
([class data-source options]
(let [id (or (:id options) (generate-element-id))
height (or (:height options) "300px")
only (:only options)]
(str (if-not (= only :script)
(chartkick-tag id, height))
(if-not (= only :html)
(chartkick-script class, id, (json/write-str data-source), (json/write-str options)))))))

(defn pie-chart
[data-source & options]
(apply chartkick-chart "PieChart" data-source options))

(defn column-chart
[data-source & options]
(apply chartkick-chart "ColumnChart" data-source options))

(defn bar-chart
[data-source & options]
(apply chartkick-chart "BarChart" data-source options))

(defn area-chart
[data-source & options]
(apply chartkick-chart "AreaChart" data-source options))

(defn combo-chart
[data-source & options]
(apply chartkick-chart "ComboChart" data-source options))

(defn geo-chart
[data-source & options]
(apply chartkick-chart "GeoChart" data-source options))

(defn scatter-chart
[data-source & options]
(apply chartkick-chart "ScatterChart" data-source options))

(defn timeline
[data-source & options]
(apply chartkick-chart "Timeline" data-source options))

(defn line-chart
[data-source & options]
(apply chartkick-chart "LineChart" data-source options))
@@ -2,6 +2,14 @@
(:require [clojure.test :refer :all]
[chartkick.core :refer :all]))

(deftest a-test
(testing "FIXME, I fail."
(is (= 0 1))))
(deftest test-generate-right-script-function
(doseq [[script expected] [[(pie-chart {}) "new Chartkick.PieChart("]
[(column-chart {}) "new Chartkick.ColumnChart("]
[(bar-chart {}) "new Chartkick.BarChart("]
[(area-chart {}) "new Chartkick.AreaChart("]
[(combo-chart {}) "new Chartkick.ComboChart("]
[(geo-chart {}) "new Chartkick.GeoChart("]
[(scatter-chart {}) "new Chartkick.ScatterChart("]
[(timeline {}) "new Chartkick.Timeline("]
[(line-chart {}) "new Chartkick.LineChart("]]]
(is (.contains script expected))))

0 comments on commit 97a9a98

Please sign in to comment.
You can’t perform that action at this time.