Srender renders the BarChart as a string.
()
| 101 | |
| 102 | // Srender renders the BarChart as a string. |
| 103 | func (p BarChartPrinter) Srender() (string, error) { |
| 104 | maxAbsValue := func(value1 int, value2 int) int { |
| 105 | min := value1 |
| 106 | max := value2 |
| 107 | |
| 108 | if value1 > value2 { |
| 109 | min = value2 |
| 110 | max = value1 |
| 111 | } |
| 112 | |
| 113 | maxAbs := max |
| 114 | |
| 115 | if min < 0 && -min > max { // This is to avoid something like "int(math.Abs(float64(minBarValue)))" |
| 116 | maxAbs = -min // (--) == (+) |
| 117 | } |
| 118 | |
| 119 | return maxAbs |
| 120 | } |
| 121 | |
| 122 | abs := func(value int) int { |
| 123 | if value < 0 { |
| 124 | return -value |
| 125 | } |
| 126 | |
| 127 | return value |
| 128 | } |
| 129 | // =================================== VERTICAL BARS RENDERER ====================================================== |
| 130 | |
| 131 | type renderParams struct { |
| 132 | repeatCount int |
| 133 | bar Bar |
| 134 | positiveChartPartHeight int |
| 135 | negativeChartPartHeight int |
| 136 | positiveChartPartWidth int |
| 137 | negativeChartPartWidth int |
| 138 | indent string |
| 139 | showValue bool |
| 140 | moveUp bool |
| 141 | moveRight bool |
| 142 | } |
| 143 | |
| 144 | renderPositiveVerticalBar := func(renderedBarRef *string, rParams renderParams) { |
| 145 | if rParams.showValue { |
| 146 | *renderedBarRef += Sprint(rParams.indent + strconv.Itoa(rParams.bar.Value) + rParams.indent + "\n") |
| 147 | } |
| 148 | |
| 149 | for i := rParams.positiveChartPartHeight; i > 0; i-- { |
| 150 | if i > rParams.repeatCount { |
| 151 | *renderedBarRef += rParams.indent + " " + rParams.indent + " \n" |
| 152 | } else { |
| 153 | *renderedBarRef += rParams.indent + rParams.bar.Style.Sprint(p.VerticalBarCharacter) + rParams.indent + " \n" |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // Used when we draw diagram with both POSITIVE and NEGATIVE values. |
| 158 | // In such case we separately draw top and bottom half of chart. |
| 159 | // And we need MOVE UP positive part to top part of chart, |
| 160 | // technically by adding empty pillars with height == height of chart's bottom part. |
no test coverage detected