메뉴 건너뛰기

enjoyTools.net

Kivy garden에서 제공하는 graph 모듈을 이용한 간단한 예제

 

# 이 파일명을 graph.py로 하면 Graph 모듈명과 충돌나서 실행이 안 된다.
import kivy
kivy.require('1.10.0')
 
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
 
from math import sin
from kivy.garden.graph import Graph, MeshLinePlot
 
kvlang = '''
#:kivy 1.10.0
<SetGraph>:
BoxLayout:
padding: 10
Graph:
id: graph_plot
background_color: 0,0,0,1
tick_color: 0,1,0,1
border_color: 1,1,0,1
xlabel: 'X'
ylabel: 'Y'
x_ticks_major: 25
y_ticks_major: 1
x_ticks_minor: 5
y_ticks_minor: 0.5
y_grid_label: True
x_grid_label: True
padding: 5
x_grid: False
y_grid: False
xmin: -0
xmax: 100
ymin: -1
ymax: 1
'''
 
class SetGraph(BoxLayout):
def __init__(self):
super(SetGraph, self).__init__()
self.graph = self.children[0].children[0]
self.drawPlots()
 
def drawPlots(self):
plot = MeshLinePlot(color=[1, 0, 0, 1])
plot.points = [(x, sin(x / 10.)) for x in range(0, 101)]
 
plot2 = MeshLinePlot(color=[0, 0, 1, 1])
plot2.points = [(x, sin(x / 5.)) for x in range(0, 101)]
 
self.graph.label_options = {
'color': [0,3,1,1],
'bold': True
}
 
self.graph.add_plot(plot)
self.graph.add_plot(plot2)
 
class graphLayoutApp(App):
def build(self):
# Builder.load_file("setgraph.kv")
Builder.load_string(kvlang)
 
return SetGraph()
 
if __name__ == '__main__':
graphLayoutApp().run()
 

 

y_ticks_minor 는 안 먹히는 것 같다.

 

ScreenManager를 사용할 때 그래프가 안나오면 아래 내용을 ScreenManager 초입에 아래 속성을 추가한다.

canvas.before:
StencilPop

 

위와 같이 하니까 아래와 같은 에러가 뜬다.

Exception: Too much StencilPop (stack underflow)

 

폴링을 걸어놓은 상태에서 화면에 뭐라도 갱신될라치면 매번 저 오류가 떠서 신경 거슬리게 만드는데,

정 안되면 %home%\.kivy\garden\garden.graph\__init__.py 소스의 프레임버퍼 사용 여부를 아래와 같이 True 에서 False 로 수정한다.

label_options = DictProperty()

'''Label options that will be passed to `:class:`kivy.uix.Label`.

'''

 

# _with_stencilbuffer = BooleanProperty(True)

_with_stencilbuffer = BooleanProperty(False)

 

이렇게 하고나서 StencilPop 문구는 제거해야 에러도 사라진다.

 

 

띄어쓰기 제대로 안나오고 줄바꿈 이상하게 나와서 소스는 첨부파일로 추가함.

 

참고:

* https://stackoverflow.com/questions/33072077/how-to-change-axes-label-color-in-kivy-garden-graph

* https://github.com/kivy-garden/garden.graph/blob/master/__init__.py

* https://github.com/kivy-garden/garden.graph/issues/7

 

끝.

 

----

추가.

모듈 자체를 고칠 일이 많아서 지금은 걍 garden/graph 폴더 내 __init__.py 를 graph.py로 내 작업폴더에 복붙해서 쓰고 있다.

 

진짜 끝.