Kivy:如何使用操作栏控制实时图表

时间:2015-02-26 05:54:06

标签: python graph kivy

我有一个实时更新和添加点的图表,但我希望能够通过ActionBar控制它(即播放,暂停,停止等)。到目前为止,无论我尝试什么,它都没有用。我是否需要将图形转换为某种动画?

以下是我的.py文件

    class Generator(object):
        def __init__(self, period):
            self.period = period
            self.i = 0
        def __call__(self):
            result = self.i%self.period + sin(self.i)+1
            self.i += 1

            return result

    class GraphView(BoxLayout):
        def __init__(self, **kwargs):
            super(GraphView, self).__init__(**kwargs)
            graph_theme = {'label_options': {'color': rgb('444444'),
                                             'bold': True},
                           'background_color': rgb('f8f8f2'),
                           'tick_color': rgb('808080'),
                           'border_color': rgb('808080')}
            self.graph = Graph(xlabel='Times',
                               ylabel='Sales',
                               x_ticks_minor=5,
                               x_ticks_major=10,
                               y_ticks_minor=5,
                               y_ticks_major=10,
                               y_grid_label=True,
                               x_grid_label=True,
                               padding=5,
                               xlog=False,
                               ylog=False,
                               x_grid=True,
                               y_grid=True,
                               **graph_theme)
            self.input_plot = MeshLinePlot(color=[0, 1, 0, 1])
            self.prediction_plot = MeshLinePlot(color=[1, 0, 0, 1])
            self.input_plotpoints = deque(maxlen=100)
            self.predictions_plotpoints = deque(maxlen=100)
            self.graph.add_plot(self.input_plot)
            self.graph.add_plot(self.prediction_plot)
            for i in xrange(10):

                self.wave1 = Generator(24)

                self.wave2 = Generator(13)
                Clock.schedule_interval(self.update_points, 1 / 100)
                Clock.schedule_interval(self.update_graph, 1 / 100)

                Clock.schedule_interval(lambda *args: self.add_points(self.wave1(), self.wave2()), 0.2)
            self.add_widget(self.graph)

        def update_points(self, *args):
            self.input_plot.points = zip(range(len(self.input_plotpoints)), self.input_plotpoints)
            self.prediction_plot.points = zip(range(len(self.predictions_plotpoints)), self.predictions_plotpoints)

        def update_graph(self, *args):
            if len(self.input_plotpoints) >3:
                self.graph.ymin= math.ceil(min(self.input_plotpoints))
                self.graph.ymax= math.ceil(max(self.input_plotpoints))

        def add_points(self,active,predictive):
            self.input_plotpoints.append(active)
            self.predictions_plotpoints.append(predictive)
        def pause():
            time.sleep(5)      
class ActionView(BoxLayout):
    pass

以下是.kv文件:

<ActionView>:

    use_separator: True
    ActionPrevious:
        title: 'Action Bar'
        with_previous: False
    ActionOverflow:
    ActionButton:
        text: 'Btn0'
        icon: 'atlas://data/images/defaulttheme/audio-volume-high'
    ActionButton:
        text: 'Start'
    ActionButton:
        text: 'Pause'
        on_release:root.GraphView.pause
    ActionButton:
        text: 'Resume'
    ActionButton:
        text: 'Stop'
    ActionGroup:
        text: 'Group1'
        ActionButton:
            text: 'Btn5'
        ActionButton:
            text: 'Btn6'
        ActionButton:
            text: 'Btn7'

1 个答案:

答案 0 :(得分:0)

好的,答案比预期的要容易。所需要的只是一个布尔值(即pause = False)和一些if如果更新点内的条件和添加点函数检查暂停是否为True。

创建一个单独的函数,将暂停从默认的false更改为True,并从操作栏中调用该函数(在.kv中)