chartdata.nim 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # author: Ethosa
  2. import
  3. algorithm,
  4. sequtils,
  5. color,
  6. enums
  7. type
  8. ChartDataValue* = object
  9. case kind*: ChartDataValueType
  10. of INTEGER_VALUE:
  11. ival*: int
  12. of FLOAT_VALUE:
  13. fval*: float
  14. of STRING_VALUE:
  15. sval*: string
  16. of CHAR_VALUE:
  17. cval*: char
  18. ChartData* = ref object
  19. chart_type*: ChartType
  20. data_color*: ColorRef
  21. data_name*: string
  22. x_axis*: seq[ChartDataValue]
  23. y_axis*: seq[ChartDataValue]
  24. proc newChartData*(data_name: string = "data", data_color: ColorRef = Color(),
  25. chart_type: ChartType = LINE_CHART): ChartData =
  26. ## Creates a new empty ChartData.
  27. runnableExamples:
  28. var my_chart_data = newChartData()
  29. ChartData(x_axis: @[], y_axis: @[], data_name: data_name, data_color: data_color, chart_type: chart_type)
  30. proc newChartData*(x_length, y_length: int, data_name: string = "data",
  31. data_color: ColorRef = Color(), chart_type: ChartType = LINE_CHART): ChartData =
  32. ## Creates a new ChartData with specified length.
  33. ##
  34. ## Arguments:
  35. ## - x_length -- length of `x_axis`;
  36. ## - y_length -- length of `y_axis`.
  37. runnableExamples:
  38. var my_chart_data1 = newChartData(5, 5)
  39. result = newChartData(data_name, data_color, chart_type)
  40. result.x_axis.setLen(x_length)
  41. result.y_axis.setLen(y_length)
  42. proc newChartData*(x_data, y_data: seq[ChartDataValue], data_name: string = "data",
  43. data_color: ColorRef = Color(), chart_type: ChartType = LINE_CHART): ChartData =
  44. ## Creates a new ChartData from specified data.
  45. ##
  46. ## Arguments:
  47. ## - x_data -- specified data for `x_axis`;
  48. ## - y_data -- specified data for `y_axis`.
  49. runnableExamples:
  50. var my_chart_data2 = newChartData(@[1, 5, 2], @["10.10.2021", "10.11.2021", "10.01.2021"])
  51. ChartData(x_axis: x_data, y_axis: y_data, data_name: data_name, data_color: data_color, chart_type: chart_type)
  52. proc cmp(x, y: ChartDataValue): int =
  53. if x.kind == y.kind:
  54. case x.kind
  55. of INTEGER_VALUE:
  56. return cmp(x.ival, y.ival)
  57. of FLOAT_VALUE:
  58. return cmp(x.fval, y.fval)
  59. of STRING_VALUE:
  60. return cmp(x.sval, y.sval)
  61. of CHAR_VALUE:
  62. return cmp(x.cval, y.cval)
  63. proc findMax*(data: seq[ChartDataValue]): ChartDataValue =
  64. ## Returns max value from sequence.
  65. (data.sorted do (x, y: ChartDataValue) -> int: cmp(x, y))[^1]
  66. proc findMax*(chart_data: ChartData): tuple[x, y: ChartDataValue] =
  67. ## Returns max values from ChartData.
  68. (x: findMax(chart_data.x_axis), y: findMax(chart_data.y_axis))
  69. proc len*(data: ChartData): int =
  70. zip(data.x_axis, data.y_axis).len
  71. proc getNum*(val: ChartDataValue): float =
  72. case val.kind
  73. of INTEGER_VALUE:
  74. val.ival.float
  75. of FLOAT_VALUE:
  76. val.fval
  77. of CHAR_VALUE:
  78. ord(val.cval).float
  79. of STRING_VALUE:
  80. 0f
  81. proc getSorted*(data: ChartData): seq[tuple[x, y: ChartDataValue]] =
  82. zip(data.x_axis, data.y_axis).sorted do (a, b: tuple[x, y: ChartDataValue]) -> int: cmp(a.y.getNum(), b.y.getNum())
  83. proc getSum*(data: ChartData): float =
  84. for i in data.y_axis:
  85. result += i.getNum()
  86. converter toChartDataValue*(val: seq[int]): seq[ChartDataValue] =
  87. result = @[]
  88. for i in val:
  89. result.add(ChartDataValue(kind: INTEGER_VALUE, ival: i))
  90. converter toChartDataValue*(val: seq[float]): seq[ChartDataValue] =
  91. result = @[]
  92. for i in val:
  93. result.add(ChartDataValue(kind: FLOAT_VALUE, fval: i))
  94. converter toChartDataValue*(val: seq[string]): seq[ChartDataValue] =
  95. result = @[]
  96. for i in val:
  97. result.add(ChartDataValue(kind: STRING_VALUE, sval: i))
  98. converter toChartDataValue*(val: seq[char]): seq[ChartDataValue] =
  99. result = @[]
  100. for i in val:
  101. result.add(ChartDataValue(kind: CHAR_VALUE, cval: i))