Drawing a pie chart

773 views
Skip to first unread message

Gita

unread,
May 15, 2014, 1:41:13 AM5/15/14
to kivy-...@googlegroups.com
Hallo,
I am trying to do a simple app in Kivy. A window with 4 buttons at the bottom part, and a pie chart on the top part showing how many times the buttons have been clicked (it is recorded in a file that will be later used to draw the pie chart). The problem is I am not being able to draw the chart. I got a code for pie chart in
http://matplotlib.org/1.2.1/examples/pylab_examples/pie_demo.html. Now in my .kv file I am trying to execute that code (pie_demo.py) as a function call pie() that has been defined in main.py.  How do I do that i.e
the function pie() will be executed automatically?

Thank you. 


In main.py
class PieChart(Widget):
   
def pie():
        drawPie
()


 
........
drawPie()
...contains code from pie_demo.py
......

In .kv file:

<PieChart>:
    canvas:
        Color:
            rgb: 1, 1, .5
        Rectangle:
            pos: self.pos
            size: self.size
              ...... root.pie() # here is the problem, I can't get to do the right coding

<WidgetLayout>:
    orientation: 'vertical'
    chart: pie_chart
    canvas:
        Rectangle:
            pos: 0,0
            size: self.width, 10
   
    PieChart:
        id: pie_chart

    GridLayout:
        cols: 2
        Button:
            text: 'A'
            font_size: 50
            on_press: root.callback(self.text)
        Button:
            text: 'B'
            font_size: 50
            on_press: root.callback(self.text)
        Button:
            text: 'C'
            font_size: 50
            on_press: root.callback(self.text)
        Button:
            text: 'D'
            font_size: 50
            on_press: root.callback(self.text)

   


Gita

unread,
May 19, 2014, 9:31:49 PM5/19/14
to kivy-...@googlegroups.com
Hello,
Is there anybody with any suggestions?
Thanks again!

ZenCODE

unread,
May 20, 2014, 3:11:31 AM5/20/14
to kivy-...@googlegroups.com
Not positive I understand what you mean? But my guess would be the button press should be more like:

    on_press: pie_chart.pie()

You will probably need to adapt the pie code to work with the kivy drawing instructions, but I hope that gets you started? Otherwise, perhaps post the complete code so we can run and test, instead of guess. ;-)

Peace out

Gita

unread,
May 21, 2014, 1:28:20 AM5/21/14
to kivy-...@googlegroups.com
Thank you 'ZenCODE' for your reply.

The following code is kind of working in the sense it does now draw the pie chart but with two issues. Firstly, I now need to click (i.e on_touch_down) on the window to display the chart. How can I make the display automatic (i.e
without click)? If I use a normal pie() function in PieChart class, nothing happens. Secondly, the chart opens in a new window, and then the A/B/C/D button clicks do not work (without chart display they do work fine). Somehow I need to modify the pie code to make the display on the same window as the buttons. 

main.py

from kivy.app import App
from kivy.uix.scatter import Scatter
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.uix.widget import Widget
import requests

list = [0, 0, 0, 0]

class CombWidget(Widget):
    chart = ObjectProperty(None)

    def callback(self, value):
        print ('The %s button has been pressed' % value)
        counter(value)

class PieChart(Widget):
    def on_touch_down(self, touch):
        drawPie()

class TestApp(App):
    def build(self):
        return CombWidget()

def counter(x):
    global list
    if x == 'A':
        list[0] = list[0] + 1
    elif x == 'B':
        list[1] = list[1] + 1
    elif x == 'C':
        list[2] = list[2] + 1
    elif x == 'D':
        list[3] = list[3] + 1

def drawPie():
    from pylab import *
    # make a square figure and axes
    figure(1, figsize=(6,6))
    ax = axes([0.1, 0.1, 0.8, 0.8])
    # The slices will be ordered and plotted counter-clockwise.
    labels = 'A', 'B', 'C', 'D'
    fracs = [15, 30, 45, 10]
    explode=(0, 0.05, 0, 0)
    pie(fracs, explode=explode, labels=labels,
                autopct='%1.1f%%', shadow=True, startangle=90)
                # The default startangle is 0, which would start
                # the Frogs slice on the x-axis.  With startangle=90,
                # everything is rotated counter-clockwise by 90 degrees,
                # so the plotting starts on the positive y-axis.
    title('Button clicks', bbox={'facecolor':'0.8', 'pad':5})
    show()

if __name__ == "__main__":
    TestApp().run()


test.kv
<CombWidget>:
    BoxLayout:
        orientation: 'vertical'
        size: root.size
        PieChart:
            size_hint: 1, 0.8
        GridLayout:
            cols: 2
            size_hint: 1, 0.2
            row_force_default: True
            row_default_height: 50
            spacing: 10
            padding_bottom: 10
            Button:
                text: 'A'
                font_size: 50
                on_press: root.callback(self.text)
            Button:
                text: 'B'
                font_size: 50
                on_press: root.callback(self.text)
            Button:
                text: 'C'
                font_size: 50
                on_press: root.callback(self.text)
            Button:
                text: 'D'
                font_size: 50
                on_press: root.callback(self.text)

ZenCODE

unread,
May 21, 2014, 3:03:19 PM5/21/14
to kivy-...@googlegroups.com
'Ello

Okay, you can do this to draw it immediately.

from kivy.clock import Clock

class CombWidget(Widget):
    chart
= ObjectProperty(None)


   
def __init__(self, **kwargs):
       
super(CombWidget, self).__init__(**kwargs)
       
Clock.schedule_once(lambda dt: drawPie())

 I can't actually run it as I don't have pylab, so get a "pylab import error", but that should work. As for the issue of a new window, that is because pylab does not know anything about the kivy window and creates it's own. I've no idea how to help you with that, but depending on what you're doing, it's probably a bad idea as (a) it won't be portable to other OS's with kivy and (b) making the two windows interact will mean having to dig into the guts of kivy and fight what it's trying to do. Kivy apps are designed to run in a single window.....

Kivy windows and mostly simple OpenGL surface containers. Pylab is not based on OpenGL (as far as I know), so none of it's drawing will really port to kivy (if I'm wrong, someone please correct me). And kivy will not understand what it's drawn unless you use some kind of intermediate image format - slow and cumbersome.

I would suggest looking in the kivy-garden for some native kivy graphs and graphics work. Pie graphs are pretty simple to draw as they are simple circle (okay, an exaggeration, but you get the idea).

https://github.com/kivy-garden

Particularly https://github.com/kivy-garden/garden.graph by the legendary tito himself and https://github.com/kivy-garden/garden.gauge.

Hope that helps? Good luck...:-)



Gita

unread,
May 21, 2014, 7:46:47 PM5/21/14
to kivy-...@googlegroups.com
Thank you so much for your help. Yes, it now draws immediately.
I do understand the problem. I have looked at Kivy garden, and hopefully can work it out.
Thank you again. 
Reply all
Reply to author
Forward
0 new messages