2021-04-18 14:34:55 +03:00
# frozen_string_literal: true
2020-10-05 12:46:20 +03:00
class AdditionalsChart
include ActiveRecord :: Sanitization
2019-10-03 18:35:12 +03:00
include Redmine :: I18n
2019-10-03 18:43:46 +03:00
CHART_DEFAULT_HEIGHT = 350
CHART_DEFAULT_WIDTH = 400
2019-10-03 18:35:12 +03:00
class << self
def color_schema
2021-09-20 13:44:10 +03:00
AdditionalsPlugin . active_reporting? ? RedmineReporting . setting ( :chart_color_schema ) : 'tableau.Classic20'
2019-10-03 18:35:12 +03:00
end
def data
raise 'overwrite it!'
end
# build return value
2021-04-18 14:34:55 +03:00
def build_chart_data ( datasets , ** options )
2019-10-03 18:35:12 +03:00
cached_labels = labels
2023-08-01 14:02:23 +03:00
2019-10-03 18:35:12 +03:00
data = { datasets : datasets . to_json ,
labels : cached_labels . keys ,
label_ids : cached_labels . values }
required_labels = options . key? ( :required_labels ) ? options . delete ( :required_labels ) : 2
2021-04-18 14:34:55 +03:00
data [ :valid ] = cached_labels . any? && cached_labels . count > = required_labels unless options . key? :valid
data [ :width ] = self :: CHART_DEFAULT_WIDTH unless options . key? :width
data [ :height ] = self :: CHART_DEFAULT_HEIGHT unless options . key? :height
data [ :value_link_method ] = '_project_issues_path' unless options . key? :value_link_method
2019-10-03 18:35:12 +03:00
data [ :color_schema ] = color_schema
2023-08-01 14:02:23 +03:00
data_values = Array ( datasets ) . first [ :data ]
data [ :data_sum ] = data_values . present? ? data_values . sum : 0
2021-04-18 14:34:55 +03:00
data . merge options
2019-10-03 18:35:12 +03:00
end
private
def build_values_without_gaps ( data , gap_value = 0 )
values = [ ]
2023-12-05 21:24:41 +03:00
labels . each_key do | label |
2021-04-18 14:34:55 +03:00
values << if data . key? label
2019-10-03 18:35:12 +03:00
data [ label ]
else
gap_value
end
end
values
end
def init_labels
@labels = { }
end
def labels
# NOTE: do not sort it, because color changes if user switch language
@labels . to_h
end
def add_label ( label , id )
return if @labels . key? label
@labels [ label ] = id
end
end
end