File: test_c3js.rb

package info (click to toggle)
ruby-svg-graph 2.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,312 kB
  • sloc: javascript: 23,548; ruby: 4,234; xml: 224; makefile: 2
file content (176 lines) | stat: -rw-r--r-- 3,925 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
require 'svggraph'
require 'minitest/autorun'
require 'minitest/reporters'

reporter_options = { :color => true }
Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new(reporter_options)]


class TestC3js < Minitest::Test

  def setup
    @bindto_id = "this_is_my_awesom_graph"
    @dataset1 = ['data1', 30, 200, 100, 400, 150, 250]
    @dataset2 = ['data2', 300, 20, 10, 40, 15, 25]

    #hash style
    @hash_chart_spec = {
      bindto: "##{@bindto_id}",
      data: {
        columns: [
          @dataset1,
          @dataset2
        ],
        axes: {
          data1: 'y',
          data2: 'y2',
        }
      },
      axis: {
        x: {
          label: 'X Label'
        },
        y: {
          label: {
            text: 'Y Axis Label',
            position: 'outer-middle'
          }
        },
        y2: {
          show: true,
          label: {
            text: 'Y2 Axis Label',
            position: 'outer-middle'
          }
        }
      },
      tooltip: {
    #   enabled: false
      },
      zoom: {
        enabled: true
      },
      subchart: {
        show: true
      }
    }
    #puts JSON.pretty_generate(@hash_chart_spec)
    @heredoc_chart_spec =<<-HEREDOC
    var my_chart1 = c3.generate({
      // bindto is mandatory
      "bindto": "##{@bindto_id}",
      "data": {
        "columns": [
            #{@dataset1},
            #{@dataset2}
        ],
        "axes": {
          "data1": "y",
          "data2": "y2"
        }
      },
      "axis": {
        "x": {
          "label": "X Label"
        },
        "y": {
          "label": {
            "text": "Y Axis Label",
            "position": "outer-middle"
          }
        },
        "y2": {
          "show": true,
          "label": {
            "text": "Y2 Axis Label",
            "position": "outer-middle"
          }
        }
      },
      "tooltip": {
      },
      "zoom": {
        "enabled": true
      },
      "subchart": {
        "show": true
      }
    });

    setTimeout(function () {
      my_chart1.load({
          columns: [
              ['data1', 230, 190, 300, 500, 300, 400]
          ]
      });
    }, 1000);

    setTimeout(function () {
      my_chart1.load({
            columns: [
                ['data3', 130, 150, 200, 300, 200, 100]
            ]
        });
    }, 1500);

    setTimeout(function () {
      my_chart1.unload({
            ids: 'data1'
        });
    }, 2000);
    HEREDOC


  end # def setup

  # @param method_name [String]
  # @param output [String]
  # @return [void]
  def write_file(method_name, output)
    File.open(File.expand_path("c3js_#{method_name}.html", __dir__), "w+") do |f|
      f.write(output)
    end # File.open
  end

  def test_burn
    g = SVG::Graph::C3js.new()
    g.add_chart_spec(@hash_chart_spec, "mychart1")
    write_file("hash_#{__method__}", g.burn)

    g = SVG::Graph::C3js.new()
    g.add_chart_spec(@heredoc_chart_spec)
    write_file("heredoc_#{__method__}", g.burn)
  end # def test_burn


  def test_burn_svg_only
    g = SVG::Graph::C3js.new()
    g.add_chart_spec(@hash_chart_spec, "mychart1")
    write_file(__method__, g.burn_svg_only)
  end # def test_burn_svg_only

  def test_invalid_file
    opts = {
      "inline_dependencies" => true,
      "d3_js"  => "/path/to/local/copy/of/d3.min.js",
      "c3_css" => "/path/to/local/copy/of/c3.min.css",
      "c3_js"  => "/path/to/local/copy/of/c3.min.js"
    }
    assert_raises(RuntimeError) { |e|
      g = SVG::Graph::C3js.new(opts)
    }
  end

  def test_inlined_dependencies
    opts = {
      "inline_dependencies" => true,
      "d3_js"  => File.expand_path("d3.min.js",__dir__),
      "c3_css" => File.expand_path("c3.min.css",__dir__),
      "c3_js"  => File.expand_path("c3.min.js",__dir__)
    }
    g = SVG::Graph::C3js.new(opts)
    g.add_chart_spec(@heredoc_chart_spec)
    write_file(__method__, g.burn)
  end

end # class TestC3js