Module: Echarts::MultipleVectors

Defined in:
lib/echarts/multiple_vectors.rb

Class Method Summary collapse

Class Method Details

.get_config(stacks, legend, min, max, lower_bound, upper_bound, title, subtitle, xLabel, yLabel, color_min = "limegreen", color_max = "tomato") ⇒ Object



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
# File 'lib/echarts/multiple_vectors.rb', line 3

def self.get_config(stacks, legend, min, max, lower_bound, upper_bound, title, subtitle, xLabel, yLabel, color_min = "limegreen", color_max = "tomato")
  # Rails.logger.debug("Legend: #{legend}\nLegend size: #{legend.size}")
  # Rails.logger.debug("Stacks size: #{stacks.size}")
  # get first value of each stack present in the stacks array and use it as the x axis
  # Stacks is in the form [[[x1, y1], [x2, y2], [x3, y3], ...], [[x1, y1], [x2, y2], [x3, y3], ...], ...]
  # data must be [x1, x2, x3, ...]
  x_values = stacks.map { |stack| stack.map(&:first) }.flatten.uniq.sort
  # Rails.logger.debug("X Values: #{x_values}")
  {
    grid: {
      top: 80,
    },
    legend: {
      bottom: 2,
      show: true,
      data: legend,
      type: "scroll",
    },
    title: {
      text: title,
      subtext: subtitle,
    },
    toolbox: {
      top: 'middle',
      right: 5,
      orient: "vertical",
      feature: {
        saveAsImage: {},
        dataView: {},
        dataZoom: {},
        restore: {},
      },
    },
    tooltip: {
      trigger: "axis",
    },
    xAxis: {
      type: "value",
      name: xLabel,
      boundaryGap: false,
      data: x_values,
      min: x_values.first.floor,
      max: x_values.last.ceil,
    },
    yAxis: {
      type: "value",
      name: yLabel,
      # Set Upper and Lower bounds to the upper and lower bounds of the data
      min: lower_bound,
      max: upper_bound,
    },
    series: stacks.map.with_index do |stack, index|
      {
        name: legend[index],
        data: stack,
        type: "line",
        smooth: true,
        markLine: {
          data: [
            # Min  line (create an array repeating the min value for each x value)
            {
              name: "Min Reference",
              yAxis: min,
              lineStyle: { color: color_min },
            },
            # Max line (create an array repeating the max value for each x value)
            {
              name: "Max Reference",
              yAxis: max,
              lineStyle: { color: color_max },
            }
          ]
        }
      }
    end
  }
end