summaryrefslogtreecommitdiffstats
path: root/qtruby/rubylib/examples/qt-examples/chart/chartform_canvas.rb
blob: 86c66f76cdeb3335e393a0553b541a9a7dbd4db1 (plain)
1
2
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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
class ChartForm

    def drawElements()
        list = @canvas.allItems()
        list.each do |it|
            it.dispose
        end
    
            # 360 * 16 for pies Qt works with 16ths of degrees
        scaleFactor = @chartType == PIE ? 5760 :
                            @chartType == VERTICAL_BAR ? @canvas.height() :
                                @canvas.width()
        biggest = 0.0
        count = 0
        total = 0.0
        scales = Array.new(MAX_ELEMENTS)
    
        for i in 0...MAX_ELEMENTS
            if  @elements[i].isValid() 
                value = @elements[i].value()
                count += 1
                total += value
                if  value > biggest
                    biggest = value
                end
                scales[i] = @elements[i].value() * scaleFactor
            end
        end
    
        if count > 0
                # 2nd loop because of total and biggest
            for i in 0...MAX_ELEMENTS
                if  @elements[i].isValid()
                    if  @chartType == PIE
                        scales[i] = (@elements[i].value() * scaleFactor) / total
                    else
                        scales[i] = (@elements[i].value() * scaleFactor) / biggest
                    end
                end
            end
    
            case @chartType 
                when PIE
                    drawPieChart( scales, total, count )
                when VERTICAL_BAR:
                    drawVerticalBarChart( scales, total, count )
                when HORIZONTAL_BAR:
                    drawHorizontalBarChart( scales, total, count )
            end
        end
    
        @canvas.update()
    end
    
    
    def drawPieChart( scales, total, i )
        width = @canvas.width().to_f
        height = @canvas.height().to_f
        size = width > height ? height : width
        x = width / 2
        y = height / 2
        angle = 0
    
        for i in 0...MAX_ELEMENTS
            if  @elements[i].isValid() 
                extent = scales[i]
                arc = Qt::CanvasEllipse.new( size, size, angle, extent, @canvas )
                arc.setX( x )
                arc.setY( y )
                arc.setZ( 0 )
                arc.setBrush( Qt::Brush.new( @elements[i].valueColor(),
                                    @elements[i].valuePattern() ) )
                arc.show()
                angle += extent
                label = @elements[i].label()
                if  !label.empty? || @addValues != NO 
                    label = valueLabel( label, @elements[i].value(), total )
                    text = CanvasText.new( i, label, @font, @canvas )
                    proX = @elements[i].proX( PIE ).to_f
                    proY = @elements[i].proY( PIE ).to_f
                    if  proX < 0 || proY < 0 
                        # Find the centre of the pie segment
                        rect = arc.boundingRect()
                        proX = ( rect.width() / 2 ) + rect.x()
                        proY = ( rect.height() / 2 ) + rect.y()
                        # Centre text over the centre of the pie segment
                        rect = text.boundingRect()
                        proX -= ( rect.width() / 2 )
                        proY -= ( rect.height() / 2 )
                        # Make proportional
                        proX /= width
                        proY /= height
                    end
                    text.setColor( @elements[i].labelColor() )
                    text.setX( proX * width )
                    text.setY( proY * height )
                    text.setZ( 1 )
                    text.show()
                    @elements[i].setProX( PIE, proX )
                    @elements[i].setProY( PIE, proY )
                end
            end
        end
    end
    
    
    def drawVerticalBarChart(scales, total, count )
        width = @canvas.width().to_f
        height = @canvas.height().to_f
        prowidth = width / count
        x = 0
        pen = Qt::Pen.new
        pen.style = NoPen
    
        for i in 0...MAX_ELEMENTS
            if  @elements[i].isValid() 
                extent = scales[i]
                y = height - extent
                rect = Qt::CanvasRectangle.new(x, y, prowidth, extent, @canvas )
                rect.setBrush( Qt::Brush.new( @elements[i].valueColor(),
                                        @elements[i].valuePattern() ) )
                rect.setPen( pen )
                rect.setZ( 0 )
                rect.show()
                label = @elements[i].label()
                if  !label.empty? || @addValues != NO 
                    proX = @elements[i].proX( VERTICAL_BAR ).to_f
                    proY = @elements[i].proY( VERTICAL_BAR ).to_f
                    if  proX < 0 || proY < 0 
                        proX = x / width
                        proY = y / height
                    end
                    label = valueLabel( label, @elements[i].value(), total )
                    text = CanvasText.new( i, label, @font, @canvas )
                    text.setColor( @elements[i].labelColor() )
                    text.setX( proX * width )
                    text.setY( proY * height )
                    text.setZ( 1 )
                    text.show()
                    @elements[i].setProX( VERTICAL_BAR, proX )
                    @elements[i].setProY( VERTICAL_BAR, proY )
                end
                x += prowidth
            end
        end
    end
    
    
    def drawHorizontalBarChart(scales, total, count )
        width = @canvas.width().to_f
        height = @canvas.height().to_f
        proheight = height / count
        y = 0
        pen = Qt::Pen.new
        pen.style = NoPen
    
        for i in 0...MAX_ELEMENTS
            if  @elements[i].isValid() 
                extent = scales[i]
                rect = Qt::CanvasRectangle.new(0, y, extent, proheight, @canvas )
                rect.setBrush( Qt::Brush.new( @elements[i].valueColor(),
                                        @elements[i].valuePattern() ) )
                rect.setPen( pen )
                rect.setZ( 0 )
                rect.show()
                label = @elements[i].label()
                if  !label.empty? || @addValues != NO 
                    proX = @elements[i].proX( HORIZONTAL_BAR ).to_f
                    proY = @elements[i].proY( HORIZONTAL_BAR ).to_f
                    if  proX < 0 || proY < 0 
                        proX = 0
                        proY = y / height
                    end
                    label = valueLabel( label, @elements[i].value(), total )
                    text = CanvasText.new( i, label, @font, @canvas )
                    text.setColor( @elements[i].labelColor() )
                    text.setX( proX * width )
                    text.setY( proY * height )
                    text.setZ( 1 )
                    text.show()
                    @elements[i].setProX( HORIZONTAL_BAR, proX )
                    @elements[i].setProY( HORIZONTAL_BAR, proY )
                end
                y += proheight
            end
        end
    end
    
    
    def valueLabel(label, value, total )
        if  @addValues == NO
            return label
        end
    
        newLabel = label
        if  !label.empty?
            if  @chartType == VERTICAL_BAR
                newLabel += "\n"
            else
                newLabel += ' '
            end
        end
        if  @addValues == YES
            newLabel += "%.#{@decimalPlaces}f" % value
        elsif  @addValues == AS_PERCENTAGE
            newLabel += "%.#{@decimalPlaces}f%s" % [(value / total) * 100, '%']
        end
        return newLabel
    end

end