chart_js 1.0.0pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +5 -0
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +67 -0
  9. data/Rakefile +6 -0
  10. data/bin/console +14 -0
  11. data/bin/setup +8 -0
  12. data/chart_js.gemspec +27 -0
  13. data/examples/bar_chart.rb +18 -0
  14. data/examples/build.rb +20 -0
  15. data/examples/debug.rb +6 -0
  16. data/examples/line_chart.rb +18 -0
  17. data/examples/radar_chart.rb +18 -0
  18. data/examples/sinatra.rb +23 -0
  19. data/lib/chart_js.rb +21 -0
  20. data/lib/chart_js/chart/bar_chart/background.rb +20 -0
  21. data/lib/chart_js/chart/bar_chart/bar_chart.rb +94 -0
  22. data/lib/chart_js/chart/bar_chart/border.rb +48 -0
  23. data/lib/chart_js/chart/bar_chart/data.rb +37 -0
  24. data/lib/chart_js/chart/bar_chart/dataset.rb +87 -0
  25. data/lib/chart_js/chart/bar_chart/dataset/background.rb +20 -0
  26. data/lib/chart_js/chart/bar_chart/dataset/border.rb +48 -0
  27. data/lib/chart_js/chart/bar_chart/dataset/line.rb +16 -0
  28. data/lib/chart_js/chart/bar_chart/dataset/point.rb +55 -0
  29. data/lib/chart_js/chart/bar_chart/dataset/point_border.rb +44 -0
  30. data/lib/chart_js/chart/bar_chart/dataset/point_hover.rb +44 -0
  31. data/lib/chart_js/chart/bar_chart/file.rb +26 -0
  32. data/lib/chart_js/chart/bar_chart/line.rb +16 -0
  33. data/lib/chart_js/chart/bar_chart/opts.rb +18 -0
  34. data/lib/chart_js/chart/bar_chart/point.rb +55 -0
  35. data/lib/chart_js/chart/bar_chart/point_border.rb +44 -0
  36. data/lib/chart_js/chart/bar_chart/point_hover.rb +44 -0
  37. data/lib/chart_js/chart/chart.rb +99 -0
  38. data/lib/chart_js/chart/charts.rb +22 -0
  39. data/lib/chart_js/chart/data.rb +37 -0
  40. data/lib/chart_js/chart/dataset.rb +87 -0
  41. data/lib/chart_js/chart/dataset/background.rb +20 -0
  42. data/lib/chart_js/chart/dataset/border.rb +48 -0
  43. data/lib/chart_js/chart/dataset/line.rb +16 -0
  44. data/lib/chart_js/chart/dataset/point.rb +55 -0
  45. data/lib/chart_js/chart/dataset/point_border.rb +44 -0
  46. data/lib/chart_js/chart/dataset/point_hover.rb +44 -0
  47. data/lib/chart_js/chart/file.rb +26 -0
  48. data/lib/chart_js/chart/line_chart.rb +99 -0
  49. data/lib/chart_js/chart/line_chart/background.rb +20 -0
  50. data/lib/chart_js/chart/line_chart/border.rb +48 -0
  51. data/lib/chart_js/chart/line_chart/data.rb +37 -0
  52. data/lib/chart_js/chart/line_chart/dataset.rb +87 -0
  53. data/lib/chart_js/chart/line_chart/dataset/background.rb +20 -0
  54. data/lib/chart_js/chart/line_chart/dataset/border.rb +48 -0
  55. data/lib/chart_js/chart/line_chart/dataset/line.rb +16 -0
  56. data/lib/chart_js/chart/line_chart/dataset/point.rb +55 -0
  57. data/lib/chart_js/chart/line_chart/dataset/point_border.rb +44 -0
  58. data/lib/chart_js/chart/line_chart/dataset/point_hover.rb +44 -0
  59. data/lib/chart_js/chart/line_chart/file.rb +26 -0
  60. data/lib/chart_js/chart/line_chart/line.rb +16 -0
  61. data/lib/chart_js/chart/line_chart/line_chart.rb +94 -0
  62. data/lib/chart_js/chart/line_chart/opts.rb +18 -0
  63. data/lib/chart_js/chart/line_chart/point.rb +55 -0
  64. data/lib/chart_js/chart/line_chart/point_border.rb +44 -0
  65. data/lib/chart_js/chart/line_chart/point_hover.rb +44 -0
  66. data/lib/chart_js/chart/opts.rb +18 -0
  67. data/lib/chart_js/chart/radar_chart/background.rb +20 -0
  68. data/lib/chart_js/chart/radar_chart/border.rb +48 -0
  69. data/lib/chart_js/chart/radar_chart/data.rb +37 -0
  70. data/lib/chart_js/chart/radar_chart/dataset.rb +87 -0
  71. data/lib/chart_js/chart/radar_chart/dataset/background.rb +20 -0
  72. data/lib/chart_js/chart/radar_chart/dataset/border.rb +48 -0
  73. data/lib/chart_js/chart/radar_chart/dataset/line.rb +16 -0
  74. data/lib/chart_js/chart/radar_chart/dataset/point.rb +55 -0
  75. data/lib/chart_js/chart/radar_chart/dataset/point_border.rb +44 -0
  76. data/lib/chart_js/chart/radar_chart/dataset/point_hover.rb +44 -0
  77. data/lib/chart_js/chart/radar_chart/file.rb +26 -0
  78. data/lib/chart_js/chart/radar_chart/line.rb +16 -0
  79. data/lib/chart_js/chart/radar_chart/opts.rb +18 -0
  80. data/lib/chart_js/chart/radar_chart/point.rb +55 -0
  81. data/lib/chart_js/chart/radar_chart/point_border.rb +44 -0
  82. data/lib/chart_js/chart/radar_chart/point_hover.rb +44 -0
  83. data/lib/chart_js/chart/radar_chart/radar_chart.rb +94 -0
  84. data/lib/chart_js/version.rb +3 -0
  85. metadata +169 -0
@@ -0,0 +1,44 @@
1
+ module ChartJS
2
+
3
+ class PointBorder
4
+
5
+ def initialize(container)
6
+ @container = container
7
+ end
8
+
9
+ def build(&block)
10
+ instance_eval(&block)
11
+ @container
12
+ end
13
+
14
+ def radius(value)
15
+ if value.is_a? Array
16
+ @container['pointHoverRadius'] = value.map(&:to_i)
17
+ else
18
+ @container['pointHoverRadius'] = value.to_i
19
+ end
20
+ end
21
+
22
+ def width(value)
23
+ if value.is_a? Array
24
+ @container['pointHoverBorderWidth'] = value.map(&:to_i)
25
+ else
26
+ @container['pointHoverBorderWidth'] = value.to_i
27
+ end
28
+ end
29
+
30
+ def color(value, type = :both)
31
+ case type
32
+ when :hover
33
+ @container['pointHoverBorderColor'] = value
34
+ when :no_hover
35
+ @container['pointBorderColor'] = value
36
+ when :both
37
+ color value, :hover
38
+ color value, :no_hover
39
+ end
40
+ end
41
+
42
+ end
43
+
44
+ end
@@ -0,0 +1,44 @@
1
+ module ChartJS
2
+
3
+ class PointHover
4
+
5
+ def initialize(container)
6
+ @container = container
7
+ end
8
+
9
+ def build(&block)
10
+ instance_eval(&block)
11
+ @container
12
+ end
13
+
14
+ def radius(value)
15
+ if value.is_a? Array
16
+ @container['pointHoverRadius'] = value.map(&:to_i)
17
+ else
18
+ @container['pointHoverRadius'] = value.to_i
19
+ end
20
+ end
21
+
22
+ def width(value)
23
+ if value.is_a? Array
24
+ @container['pointHoverBorderWidth'] = value.map(&:to_i)
25
+ else
26
+ @container['pointHoverBorderWidth'] = value.to_i
27
+ end
28
+ end
29
+
30
+ def color(value, type = :both)
31
+ case type
32
+ when :border
33
+ @container['pointHoverBorderColor'] = value
34
+ when :background
35
+ @container['pointHoverBackgroundColor'] = value
36
+ when :both
37
+ color value, :borer
38
+ color value, :background
39
+ end
40
+ end
41
+
42
+ end
43
+
44
+ end
@@ -0,0 +1,18 @@
1
+ require_relative "dataset.rb"
2
+
3
+ module ChartJS
4
+
5
+ class Options
6
+
7
+ def initialize
8
+ @container = Hash.new
9
+ end
10
+
11
+ def build(&block)
12
+ instance_eval(&block)
13
+ @container
14
+ end
15
+
16
+ end
17
+
18
+ end
@@ -0,0 +1,20 @@
1
+ module ChartJS
2
+
3
+ class Background
4
+
5
+ def initialize(container)
6
+ @container = container
7
+ end
8
+
9
+ def build(&block)
10
+ instance_eval(&block)
11
+ @container
12
+ end
13
+
14
+ def color(value)
15
+ @container['backgroundColor'] = value
16
+ end
17
+
18
+ end
19
+
20
+ end
@@ -0,0 +1,48 @@
1
+ module ChartJS
2
+
3
+ class Border
4
+
5
+ def initialize(container)
6
+ @container = container
7
+ end
8
+
9
+ def build(&block)
10
+ instance_eval(&block)
11
+ @container
12
+ end
13
+
14
+ def color(value)
15
+ @container['borderColor'] = value
16
+ end
17
+
18
+ def width(value)
19
+ if value.is_a? Array
20
+ @container['borderWidth'] = value.map(&:to_i)
21
+ else
22
+ @container['borderWidth'] = value.to_i
23
+ end
24
+ end
25
+
26
+ def dash(value)
27
+ if value.is_a? Array
28
+ @container['borderDash'] = value.map(&:to_i)
29
+ else
30
+ raise "Dash must be an array!"
31
+ end
32
+ end
33
+
34
+ def dash_offset(value)
35
+ @container['borderDashOffset'] = value.to_i
36
+ end
37
+
38
+ def cap(value)
39
+ @container['borderCapStyle'] = value
40
+ end
41
+
42
+ def join(value)
43
+ @container['borderJoinStyle'] = value
44
+ end
45
+
46
+ end
47
+
48
+ end
@@ -0,0 +1,37 @@
1
+ require_relative "dataset.rb"
2
+
3
+ module ChartJS
4
+
5
+ class Data
6
+
7
+ def initialize
8
+ @container = Hash.new
9
+ @datasets = Hash.new
10
+ end
11
+
12
+ def build(&block)
13
+ instance_eval(&block)
14
+ self
15
+ end
16
+
17
+ def labels(labels = nil)
18
+ raise "Not an array!" unless labels.is_a? Array
19
+ @container['labels'] = labels
20
+ end
21
+
22
+ def dataset(label, &block)
23
+ @datasets[label] = Dataset.new(label, &block)
24
+ end
25
+
26
+ def to_h
27
+ cont = @container.dup
28
+ cont['datasets'] = []
29
+ @datasets.each do |_, data|
30
+ cont['datasets'] << data.to_h
31
+ end
32
+ cont
33
+ end
34
+
35
+ end
36
+
37
+ end
@@ -0,0 +1,87 @@
1
+ require_relative "dataset/background.rb"
2
+ require_relative "dataset/border.rb"
3
+ require_relative "dataset/point.rb"
4
+ require_relative "dataset/line.rb"
5
+
6
+ module ChartJS
7
+
8
+ class Dataset
9
+
10
+ def initialize(label, &block)
11
+ @container = Hash.new
12
+ fill(false)
13
+ label(label)
14
+ build(&block)
15
+ end
16
+
17
+ def build(&block)
18
+ instance_eval(&block)
19
+ @container
20
+ end
21
+
22
+ def data(value)
23
+ @container['data'] = value
24
+ end
25
+
26
+ def label(value)
27
+ @container['label'] = value
28
+ end
29
+
30
+ def fill(value = true)
31
+ @container['fill'] = value
32
+ end
33
+
34
+ def axis_id(value, axis)
35
+ case axis
36
+ when :x
37
+ @container['xAxisID'] = value
38
+ when :y
39
+ @container['yAxisID'] = value
40
+ end
41
+ end
42
+
43
+ def color(value = :random, type = :both)
44
+ if value == :random
45
+ c = "##{SecureRandom.hex(3)}"
46
+ color c, :border if type == :both || type == :border
47
+ color c, :background if type == :both || type == :background
48
+ return
49
+ end
50
+ case type
51
+ when :border
52
+ @container['borderColor'] = value
53
+ when :background
54
+ @container['backgroundColor'] = value
55
+ when :both
56
+ color value, :border
57
+ color value, :background
58
+ end
59
+ end
60
+
61
+ def span_gaps(value = true)
62
+ @container['spanGaps'] = value
63
+ end
64
+
65
+ def background(&block)
66
+ @container = Background.new(@container).build(&block)
67
+ end
68
+
69
+ def border(&block)
70
+ @container = Border.new(@container).build(&block)
71
+ end
72
+
73
+ def point(&block)
74
+ @container = Point.new(@container).build(&block)
75
+ end
76
+
77
+ def line(&block)
78
+ @container = Line.new(@container).build(&block)
79
+ end
80
+
81
+ def to_h
82
+ @container
83
+ end
84
+
85
+ end
86
+
87
+ end
@@ -0,0 +1,20 @@
1
+ module ChartJS
2
+
3
+ class Background
4
+
5
+ def initialize(container)
6
+ @container = container
7
+ end
8
+
9
+ def build(&block)
10
+ instance_eval(&block)
11
+ @container
12
+ end
13
+
14
+ def color(value)
15
+ @container['backgroundColor'] = value
16
+ end
17
+
18
+ end
19
+
20
+ end
@@ -0,0 +1,48 @@
1
+ module ChartJS
2
+
3
+ class Border
4
+
5
+ def initialize(container)
6
+ @container = container
7
+ end
8
+
9
+ def build(&block)
10
+ instance_eval(&block)
11
+ @container
12
+ end
13
+
14
+ def color(value)
15
+ @container['borderColor'] = value
16
+ end
17
+
18
+ def width(value)
19
+ if value.is_a? Array
20
+ @container['borderWidth'] = value.map(&:to_i)
21
+ else
22
+ @container['borderWidth'] = value.to_i
23
+ end
24
+ end
25
+
26
+ def dash(value)
27
+ if value.is_a? Array
28
+ @container['borderDash'] = value.map(&:to_i)
29
+ else
30
+ raise "Dash must be an array!"
31
+ end
32
+ end
33
+
34
+ def dash_offset(value)
35
+ @container['borderDashOffset'] = value.to_i
36
+ end
37
+
38
+ def cap(value)
39
+ @container['borderCapStyle'] = value
40
+ end
41
+
42
+ def join(value)
43
+ @container['borderJoinStyle'] = value
44
+ end
45
+
46
+ end
47
+
48
+ end
@@ -0,0 +1,16 @@
1
+ module ChartJS
2
+
3
+ class Line
4
+
5
+ def initialize(container)
6
+ @container = container
7
+ end
8
+
9
+ def build(&block)
10
+ instance_eval(&block)
11
+ @container
12
+ end
13
+
14
+ end
15
+
16
+ end
@@ -0,0 +1,55 @@
1
+ require_relative 'point_hover.rb'
2
+ require_relative 'point_border.rb'
3
+
4
+ module ChartJS
5
+
6
+ class Point
7
+
8
+ def initialize(container)
9
+ @container = container
10
+ end
11
+
12
+ def build(&block)
13
+ instance_eval(&block)
14
+ @container
15
+ end
16
+
17
+ def hover(&block)
18
+ @container = PointHover.new(@container).build(&block)
19
+ end
20
+
21
+ def border(&block)
22
+ @container = PointBorder.new(@container).build(&block)
23
+ end
24
+
25
+ def radius(value)
26
+ if value.is_a? Array
27
+ @container['pointRadius'] = value.map(&:to_i)
28
+ else
29
+ @container['pointRadius'] = value.to_i
30
+ end
31
+ end
32
+
33
+ def hit_radius(value)
34
+ if value.is_a? Array
35
+ @container['pointHitRadius'] = value.map(&:to_i)
36
+ else
37
+ @container['pointHitRadius'] = value.to_i
38
+ end
39
+ end
40
+
41
+ def color(value, type = :both)
42
+ case type
43
+ when :border
44
+ @container['pointBorderColor'] = value
45
+ when :background
46
+ @container['pointBackgroundColor'] = value
47
+ when :both
48
+ color value, :borer
49
+ color value, :background
50
+ end
51
+ end
52
+
53
+ end
54
+
55
+ end