Module: Echarts::Speedometer

Defined in:
lib/echarts/speedometer.rb

Class Method Summary collapse

Class Method Details

.compute_angle(value) ⇒ Float

Computes the angle for a given value.

Parameters:

  • value (Float)

    The value to compute the angle for.

Returns:

  • (Float)

    The angle in degrees.



18
19
20
21
# File 'lib/echarts/speedometer.rb', line 18

def self.compute_angle(value)
  # Compute the angles from the percentages knowing that the startAngle = 225 and the endAngle = -45 clockwise
  225 - (value * 270)
end

.compute_angles(value_in_percentage, min_in_percentage, max_in_percentage) ⇒ Array<Float>

Computes the angles for a given value and the minimum and maximum values in percentage.

Parameters:

  • value_in_percentage (Float)

    The value in percentage.

  • min_in_percentage (Float)

    The minimum value in percentage.

  • max_in_percentage (Float)

    The maximum value in percentage.

Returns:

  • (Array<Float>)

    The minimum angle, maximum angle, and value angle in degrees.



29
30
31
32
33
34
35
# File 'lib/echarts/speedometer.rb', line 29

def self.compute_angles(value_in_percentage, min_in_percentage, max_in_percentage)
  min_angle = compute_angle(min_in_percentage)
  max_angle = compute_angle(max_in_percentage)
  value_angle = compute_angle(value_in_percentage)

  return min_angle, max_angle, value_angle
end

.compute_color(min, max, min_in_percentage, max_in_percentage, color_ok = "limegreen", color_error = "tomato", color_generic = "dodgerblue") ⇒ Array<Array>

Computes the color for the gauge chart based on the minimum and maximum values.

Parameters:

  • min (Numeric)

    The minimum value.

  • max (Numeric)

    The maximum value.

  • min_in_percentage (Float)

    The minimum value in percentage.

  • max_in_percentage (Float)

    The maximum value in percentage.

  • color_ok (String) (defaults to: "limegreen")

    The color for values within the range.

  • color_error (String) (defaults to: "tomato")

    The color for values outside the range.

  • color_generic (String) (defaults to: "dodgerblue")

    The color for generic values.

Returns:

  • (Array<Array>)

    An array of color values and their corresponding percentages.



47
48
49
50
51
52
53
54
# File 'lib/echarts/speedometer.rb', line 47

def self.compute_color(min, max, min_in_percentage, max_in_percentage, color_ok = "limegreen", color_error = "tomato", color_generic = "dodgerblue")
  color = []
  color << [min_in_percentage, color_error] if !min.zero? && (max > min || max.zero?)
  color << [max_in_percentage, color_ok] if !max.zero? && (max > min || min.zero?)
  color << [1, max > min ? color_error : ( max == min && max != 0 || max == 0 && min ==0 ? color_generic : color_ok )] 

  color
end

.compute_percentage(value, lower_bound, upper_bound) ⇒ Float

Computes the percentage of a value within a given range.

Parameters:

  • value (Numeric)

    The value to compute the percentage for.

  • lower_bound (Numeric)

    The lower bound of the range.

  • upper_bound (Numeric)

    The upper bound of the range.

Returns:

  • (Float)

    The percentage of the value within the range.



10
11
12
# File 'lib/echarts/speedometer.rb', line 10

def self.compute_percentage(value, lower_bound, upper_bound)
  (value - lower_bound) / (upper_bound - lower_bound)
end

.get_config(value, min, max, lower_bound, upper_bound, title, subtitle, vLabel, color_ok = "limegreen", color_error = "tomato", color_generic = "dodgerblue") ⇒ Hash

Computes the options for the gauge chart.

Parameters:

  • value (Numeric)

    The value to display on the gauge chart.

  • min (Numeric)

    The minimum value.

  • max (Numeric)

    The maximum value.

  • lower_bound (Numeric)

    The lower bound of the range.

  • upper_bound (Numeric)

    The upper bound of the range.

  • measure_unit (String)

    The unit of measurement for the value.

  • name (String)

    The name of the gauge chart.

  • color_ok (String) (defaults to: "limegreen")

    The color for values within the range.

  • color_error (String) (defaults to: "tomato")

    The color for values outside the range.

  • color_generic (String) (defaults to: "dodgerblue")

    The color for generic values.

Returns:

  • (Hash)

    The options for the gauge chart.



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

def self.get_config(value, min, max, lower_bound, upper_bound, title, subtitle, vLabel, color_ok = "limegreen", color_error = "tomato", color_generic = "dodgerblue")
  min_in_percentage = compute_percentage(min, lower_bound, upper_bound)
  max_in_percentage = compute_percentage(max, lower_bound, upper_bound)
  value_in_percentage = compute_percentage(value, lower_bound, upper_bound)

  color = compute_color(min, max, min_in_percentage, max_in_percentage, color_ok, color_error, color_generic)
  min_angle, max_angle, value_angle = compute_angles(value_in_percentage, min_in_percentage, max_in_percentage)

  # The gauge chart
  raw_js_options = {
    grid: {
      top: 80,
    },
    title: {
      text: title,
      subtext: subtitle,
    },
    toolbox: {
      top: 'middle',
      right: 5,
      orient: "vertical",
      feature: {
        saveAsImage: {},
      }
    },
    series: [
      {
        type: "gauge",
        min: lower_bound,
        max: upper_bound,
        splitNumber: ((upper_bound - lower_bound) / lower_bound).abs,
        axisLine: {
          lineStyle: {
            width: 30,
            color: color,
          },
        },
        pointer: {
          itemStyle: {
            color: "auto",
          },
        },
        axisTick: {
          distance: -30,
          length: 8,
          lineStyle: {
            color: "#fff",
            width: 2,
          },
        },
        splitLine: {
          distance: -30,
          length: 30,
          lineStyle: {
            color: "#fff",
            width: 4,
          },
        },
        axisLabel: {
          color: "inherit",
          distance: 40,
          fontSize: 20,
        },
        detail: {
          valueAnimation: true,
          # formatter: "#{value.to_i}#{measure_unit}",
          color: "inherit",
        },
        data: [
          {
            value: value,
            name: vLabel,
          },
        ],
      },
      # Dummy Gauge to be used to show the min max thresholds
      {
        type: "gauge",
        z: 3,
        startAngle: min_angle, # Here you can dinamically change the value
        min: min,
        endAngle: max_angle, # Here you can dinamically change the value
        max: max,
        splitNumber: 1,
        axisLabel: {
          distance: -24,
        },
        axisTick: {
          show: false,
        },
        axisLine: {
          lineStyle: {
            width: 0,
          },
        },
        splitLine: {
          distance: -12,
          # length: 50,
          lineStyle: {
            width: 0,
          },
        },
      },
    ],
  }
  
  return raw_js_options
end