您的位置:首页 > 其它

kivy画条线

2021-04-22 23:16 120 查看

from kivy.uix.gridlayout import GridLayout    from kivy.app import runTouchApp    from kivy.graphics import Line

    layout = GridLayout(cols=1)
    with layout.canvas:
    	#可以组合画线
        Line(points=[100,100, 100, 200], width=10)
        Line(points=[100, 100, 200, 200], width=10)
    runTouchApp(layout)

画刻度线

def draw_ticks(ticks_height,ones_ticks):
    # ticks_height=[100, 100, 100, 500]
    # ones_ticks=[[50, 100, 100, 100], [50, 200, 100, 200], [50, 300, 100, 300], [50, 400, 100, 400],
    #                   [50, 500, 100, 500]]
    layout = GridLayout(cols=1)
    with layout.canvas:
        Line(points=ticks_height, width=5)
        for point in ones_ticks:
            Line(points=point, width=5)
    return layout

四个数组成的列表代表线段的起点和终点

from kivy.uix.gridlayout import GridLayout    from kivy.app import runTouchApp    from kivy.graphics import Line    from kivy.graphics import Color

    layout = GridLayout(cols=1)
    with layout.canvas:
        
        Line(points=[100,100, 100, 500], width=5)
        for point in [[50,100, 100, 100],[50,200, 100, 200],[50,300, 100, 300],[50,400, 100, 400],[50,500, 100, 500]]:
            Color(1.0, 0.0, 0.0, 0.2)
            Line(points=point, width=5)

Color(1.0, 0.0, 0.0, 0.2)
加在哪个line的前面就控制谁的颜色

画刻度

from kivy.uix.gridlayout import GridLayoutfrom kivy.app import runTouchAppfrom kivy.graphics import Linefrom kivy.graphics import Colordef draw_ticks(ticks_height,ones_ticks,ticks_colors=(1,1,1)):
    # ticks_height=[100, 100, 100, 500]
    # ones_ticks=[[50, 100, 100, 100], [50, 200, 100, 200], [50, 300, 100, 300], [50, 400, 100, 400],
    #                   [50, 500, 100, 500]]
    layout = GridLayout(cols=1)
    with layout.canvas:
        Color(ticks_colors[0], ticks_colors[1], ticks_colors[2], 1)
        Line(points=ticks_height, width=5)
        for point in ones_ticks:
            Color(ticks_colors[0], ticks_colors[1], ticks_colors[2], 1)
            Line(points=point, width=5)
    return layout
runTouchApp(draw_ticks(ticks_height=[100, 100, 100, 500],ones_ticks=[[50, 100, 100, 100], [50, 200, 100, 200], [50, 300, 100, 300], [50, 400, 100, 400],[50, 500, 100, 500]]))

条形图

from kivy.uix.gridlayout import GridLayoutfrom kivy.app import runTouchAppfrom kivy.graphics import Linefrom kivy.graphics import Colordef draw_bars(ones_ticks,ticks_colors):
    # ticks_colors=[(1,1,1),()]
    # ones_ticks=[[50, 100, 100, 100], [50, 200, 100, 200], [50, 300, 100, 300], [50, 400, 100, 400],
    #                   [50, 500, 100, 500]]
    layout = GridLayout(cols=1)
    with layout.canvas:

        for point,color in ones_ticks,ticks_colors:
            Color(color[0],color[0],color[0],1)
            Line(points=point, width=5)
    return layout

固定值的条形图

from kivy.graphics import Colorfrom kivy.uix.gridlayout import GridLayoutfrom kivy.app import runTouchAppfrom kivy.graphics import Linefrom kivy.uix.label import Labelfrom kivy.uix.button import Buttonclass BarChart():
    def __init__(self):
        self.ticks_y_height = [300, 40, 300, 540]
        self.ones_y_ticks = [[280, 40, 300, 40], [280, 140, 300, 140], [280, 230, 300, 230], [280, 325, 300, 325],
                        [280, 420, 300, 420], [280, 510, 300, 510]]

        self.ticks_x_height = [300, 40, 800, 40]
        self.ones_x_ticks = [[325, 40, 325, 30], [340, 40, 340, 30], [355, 40, 355, 30], [370, 40, 370, 30],
                        [385, 40, 385, 30],
                        [400, 40, 400, 30], [415, 40, 415, 30], [430, 40, 430, 30], [445, 40, 445, 30],
                        [460, 40, 460, 30]]

        self.ticks_colors = (1, 0.5, 1)

        self.ones_bar = [[325, 43, 325, 163], [340, 43, 340, 123], [355, 43, 355, 123], [370, 43, 370, 123],
                    [385, 43, 385, 123],
                    [400, 43, 400, 163], [415, 43, 415, 123], [430, 43, 430, 123], [445, 43, 445, 123],
                    [460, 43, 460, 123]]

        self.ticks_colors_1 = [(0.1, 0.5, 0.5), (0, 1, 0), (1, 0, 0), (1, 1, 0), (1, 0, 1), (0, 1, 1), (1, 1, 0.5),
                          (1, 0.5, 1), (0.5, 1, 1), (0.5, 0.5, 1)]

        # name="bar_parent_layout"
        self.bar_parent_layout = GridLayout(rows=1,size_hint_y=None,height=800,size_hint_x=None,width=100)
        # name="y_label"
        self.y_label = GridLayout(cols=1,size_hint_y=None,height=560)
        # name="y_ticks"
        self.y_ticks = GridLayout(cols=1,rows=1)
        # name = "y_layout"
        self.y_layout = GridLayout(rows=1)
        # name = "title_layout"
        self.title_layout = GridLayout(rows=1)
        # name = "bar_layout"
        self.bar_layout = GridLayout(rows=1)
        # name="x_label"
        self.x_label = GridLayout(rows=1)
        # name="x_ticks"
        self.x_ticks = GridLayout(rows=1)
        # name="x_layout"
        self.x_layout = GridLayout(cols=1)
        # name = "log_layout"
        self.log_layout = GridLayout(cols=1)
        # name = "left_layout"
        self.left_layout =GridLayout(rows=1,size_hint_y=None,height=560,size_hint_x=None,width=800)
        # name="right_layout"
        self.right_layout = GridLayout(cols=1)
        # name = "center_layout"

        self.center_layout = GridLayout(cols=1,size_hint_y=None,height=600,size_hint_x=None,width=800)

    def draw_ticks(self, ticks_height, ones_ticks, ticks_colors=(1, 1, 1), width=5):
        layout = GridLayout(cols=1)
        with layout.canvas:
            Color(ticks_colors[0], ticks_colors[1], ticks_colors[2], 1)
            Line(points=ticks_height, width=width)
            for point in ones_ticks:
                Color(ticks_colors[0], ticks_colors[1], ticks_colors[2], 1)

                Line(points=point, width=width)
        return layout    def draw_bars(self, ones_ticks, ticks_colors, width=5):

        layout = GridLayout(cols=1)
        with layout.canvas:
            for point, color in zip(ones_ticks, ticks_colors):

                Color(color[0], color[1], color[2], 1)
                Line(points=point, width=width)
        return layout    def draw_x_label(self, x_labels):
        for x_label in x_labels:
            self.x_label.add_widget(Label(text=x_label,halign="left"))

    def draw_y_label(self, y_labels):

        for y_label in y_labels:

            self.y_label.add_widget(Label(text=y_label))

    def draw_log(self, logs_labels):
        for one_label in logs_labels:
            self.log_layout.add_widget(Label(text=one_label))

    def draw_bar(self):

        self.bar_parent_layout.add_widget(self.left_layout)
        self.bar_parent_layout.add_widget(self.center_layout)
        self.bar_parent_layout.add_widget(self.right_layout)

        # 左面
        # self.y_label横坐标的名字 这里只用label
        self.draw_y_label(("100", "80","60" , "40", "20", "0"))
        self.left_layout.add_widget(self.y_label)

        # self.y_ticks画一条刻度线,

        width = 2

        la=self.draw_ticks(self.ticks_y_height, self.ones_y_ticks, self.ticks_colors, width)
        self.y_ticks.add_widget(la)
        self.left_layout.add_widget(self.y_ticks)

        # 中心
        # self.center_layout

        # self.bar_layout
        width = 5
        self.bar_layout.add_widget(self.draw_bars(self.ones_bar, self.ticks_colors_1, width))
        self.center_layout.add_widget(self.bar_layout)

        # self.x_ticks

        width = 2

        la = self.draw_ticks(self.ticks_x_height, self.ones_x_ticks, self.ticks_colors, width)
        self.x_ticks.add_widget(la)
        self.center_layout.add_widget(self.x_ticks)

        # self.x_label
        # self.draw_x_label(("100", "80", "60", "40", "20", "0"))
        # self.center_layout.add_widget(self.x_label)
        self.center_layout.add_widget(Button(text="ddddd",size_hint_y=None,height=10))

        self.log_layout.add_widget(Label(text="now"))
        self.right_layout.add_widget(self.log_layout)

        # 总图

        return self.bar_parent_layoutif __name__ == '__main__':
      runTouchApp(BarChart().draw_bar())

采用这种方式就可以操作Line的颜色Color线的相对位置

line1=Builder.load_string("""
GridLayout:
    name:"right_1"
    rows:1
    canvas:
        Color:
            rgba: 1,0,1, 1
        Line:
            points:self.pos[0]+10,20,self.pos[0]+10,88
            width:5
               """)

line1.canvas.children

               

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: