seolpyo-mplchart 0.0.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of seolpyo-mplchart might be problematic. Click here for more details.

@@ -0,0 +1,601 @@
1
+ from matplotlib.collections import LineCollection
2
+ from matplotlib.backend_bases import MouseEvent, MouseButton, cursors
3
+ import pandas as pd
4
+
5
+
6
+ from .cursor import CursorMixin, Chart as CM
7
+
8
+
9
+ def get_wickline(x: pd.Series):
10
+ v = x.values
11
+ return ((v[0], v[1]), (v[0], v[2]))
12
+ def get_volumeline(x: pd.Series):
13
+ v = x.values
14
+ return ((v[0], 0), (v[0], v[1]))
15
+
16
+
17
+ class Mixin(CursorMixin):
18
+ def on_click(self, e):
19
+ "This function works if mouse button click event active."
20
+ return
21
+ def on_release(self, e):
22
+ "This function works if mouse button release event active."
23
+ return
24
+
25
+ class NavgatorMixin(Mixin):
26
+ min_distance = 30
27
+ color_navigatorline = '#1e78ff'
28
+ color_navigator = 'k'
29
+
30
+ _x_click, _x_release = (0, 0)
31
+ is_click, is_move = (False, False)
32
+ _navcoordinate = (0, 0)
33
+
34
+ def _add_collection(self):
35
+ super()._add_collection()
36
+
37
+ # 슬라이더 네비게이터
38
+ self.navigator = LineCollection([], animated=True, edgecolors=[self.color_navigator, self.color_navigatorline], alpha=(0.2, 1.0))
39
+ self.ax_slider.add_artist(self.navigator)
40
+ return
41
+
42
+ def set_data(self, df):
43
+ super().set_data(df)
44
+
45
+ # 네비게이터 라인 선택 영역
46
+ xsub = self.xmax - self.xmin
47
+ self._navLineWidth = xsub * 0.008
48
+ if self._navLineWidth < 1: self._navLineWidth = 1
49
+ self._navLineWidth_half = self._navLineWidth / 2
50
+ return
51
+
52
+ def _connect_event(self):
53
+ super()._connect_event()
54
+ self.canvas.mpl_connect('axes_leave_event', lambda x: self._leave_axes(x))
55
+ self.canvas.mpl_connect('button_press_event', lambda x: self._on_click(x))
56
+ self.canvas.mpl_connect('button_release_event', lambda x: self._on_release(x))
57
+ return
58
+
59
+ def _leave_axes(self, e: MouseEvent):
60
+ if not self.is_click and e.inaxes is self.ax_slider:
61
+ self.canvas.set_cursor(cursors.POINTER)
62
+ return
63
+
64
+ def _on_click(self, e: MouseEvent):
65
+ if self.is_click or e.button != MouseButton.LEFT or e.inaxes is not self.ax_slider: return
66
+
67
+ self.is_click = True
68
+
69
+ x = e.xdata.__int__()
70
+ left, right = self._navcoordinate
71
+ lmin, lmax = (left-self._navLineWidth, left+self._navLineWidth_half)
72
+ rmin, rmax = (right-self._navLineWidth_half, right+self._navLineWidth)
73
+
74
+ gtl, ltr = (lmax < x, x < rmin)
75
+ if gtl and ltr:
76
+ self._x_click = x
77
+ self.is_move = True
78
+ self.canvas.set_cursor(cursors.MOVE)
79
+ else:
80
+ self.canvas.set_cursor(cursors.RESIZE_HORIZONTAL)
81
+ if not gtl and lmin <= x:
82
+ self._x_click = right
83
+ elif not ltr and x <= rmax:
84
+ self._x_click = left
85
+ else:
86
+ self._x_click = x
87
+
88
+ # 그리기 후 최초 클릭이면 좌표 수정
89
+ if left == right:
90
+ self._navcoordinate = (x, x)
91
+ return
92
+
93
+ def _on_release(self, e: MouseEvent):
94
+ if e.inaxes is not self.ax_slider: return
95
+ self.is_click, self.is_move = (False, False)
96
+
97
+ if self._navcoordinate[0] == self._navcoordinate[1]:
98
+ self._navcoordinate = (self._navcoordinate[0], self._navcoordinate[1]+self.min_distance)
99
+
100
+ self.background = None
101
+ self._draw()
102
+ return
103
+
104
+
105
+ class BackgroundMixin(NavgatorMixin):
106
+ def _on_draw(self, e):
107
+ self.background = None
108
+ self._restore_region()
109
+ return
110
+
111
+ def _restore_region(self, with_nav=True):
112
+ if not self.background: self._create_background()
113
+
114
+ if with_nav: self.canvas.restore_region(self.background_with_nav)
115
+ else: self.canvas.renderer.restore_region(self.background)
116
+ return
117
+
118
+ def _copy_bbox(self):
119
+ self.ax_slider.xaxis.draw(self.canvas.renderer)
120
+ self.ax_slider.yaxis.draw(self.canvas.renderer)
121
+ self.slidercollection.draw(self.canvas.renderer)
122
+
123
+ super()._copy_bbox()
124
+
125
+ self.navigator.draw(self.canvas.renderer)
126
+ self.background_with_nav = self.canvas.renderer.copy_from_bbox(self.fig.bbox)
127
+ return
128
+
129
+ def _draw_artist(self):
130
+ renderer = self.canvas.renderer
131
+
132
+ self.ax_price.xaxis.draw(renderer)
133
+ self.ax_price.yaxis.draw(renderer)
134
+
135
+ if self.candle_on_ma:
136
+ self.macollection.draw(renderer)
137
+ self.candlecollection.draw(renderer)
138
+ else:
139
+ self.candlecollection.draw(renderer)
140
+ self.macollection.draw(renderer)
141
+
142
+ self.ax_volume.xaxis.draw(renderer)
143
+ self.ax_volume.yaxis.draw(renderer)
144
+
145
+ self.volumecollection.draw(renderer)
146
+ return
147
+
148
+
149
+ class DrawMixin(BackgroundMixin):
150
+ def set_data(self, df):
151
+ super().set_data(df)
152
+
153
+ # 네비게이터 높이 설정
154
+ if 0 < self._slider_ymin: ysub = self._slider_ymax
155
+ else: ysub = self._slider_ymax - self._slider_ymin
156
+ self._ymiddle = ysub / 2
157
+ self.navigator.set_linewidth((ysub, 5))
158
+ return
159
+
160
+ def _on_release(self, e: MouseEvent):
161
+ if e.inaxes is not self.ax_slider: return
162
+ self.is_click, self.is_move = (False, False)
163
+
164
+ if self._navcoordinate[0] == self._navcoordinate[1]:
165
+ self._navcoordinate = (self._navcoordinate[0], self._navcoordinate[1]+self.min_distance)
166
+ self._set_navigator(*self._navcoordinate)
167
+
168
+ self.background = None
169
+ self._draw()
170
+ return
171
+
172
+ def _on_move(self, e: MouseEvent):
173
+ self._restore_region((not self.is_click))
174
+
175
+ self._on_move_action(e)
176
+
177
+ if self.in_slider:
178
+ self._change_coordinate()
179
+ if self.is_click:
180
+ if self.is_move: self._set_navigator(*self._navcoordinate)
181
+ elif self.intx is not None: self._set_navigator(self._x_click, self.intx)
182
+ self.navigator.draw(self.canvas.renderer)
183
+ self._slider_move_action(e)
184
+ elif self.is_click:
185
+ self.navigator.draw(self.canvas.renderer)
186
+ else:
187
+ if self.in_slider or self.in_price or self.in_volume:
188
+ self._slider_move_action(e)
189
+ if self.in_price or self.in_volume:
190
+ self._chart_move_action(e)
191
+
192
+ self._blit()
193
+ return
194
+
195
+ def _change_coordinate(self):
196
+ if self.intx is None: return
197
+ x = self.intx
198
+ left, right = self._navcoordinate
199
+
200
+ if not self.is_click:
201
+ lmin, lmax = (left-self._navLineWidth, left+self._navLineWidth_half)
202
+ rmin, rmax = (right-self._navLineWidth_half, right+self._navLineWidth)
203
+ ltel, gter = (x <= lmax, rmin <= x)
204
+ if ltel and lmin <= x:
205
+ self.canvas.set_cursor(cursors.RESIZE_HORIZONTAL)
206
+ elif gter and x <= rmax:
207
+ self.canvas.set_cursor(cursors.RESIZE_HORIZONTAL)
208
+ elif not ltel and not gter: self.canvas.set_cursor(cursors.MOVE)
209
+ else: self.canvas.set_cursor(cursors.POINTER)
210
+ else:
211
+ # 네비게이터 좌표 수정
212
+ intx = x.__int__()
213
+ if self.is_move:
214
+ xsub = self._x_click - intx
215
+ left, right = (left-xsub, right-xsub)
216
+ self._x_click = intx
217
+ else:
218
+ if intx == left: left = intx
219
+ elif intx == right: right = intx
220
+ else:
221
+ if self._x_click < intx: left, right = (self._x_click, intx)
222
+ else: left, right = (intx, self._x_click)
223
+
224
+ nsub = right - left
225
+ if right < 0 or self.df.index[-1] < left or nsub < self.min_distance: left, right = self._navcoordinate
226
+ self._navcoordinate = (left, right)
227
+ return
228
+
229
+ def _set_navigator(self, x1, x2):
230
+ xmin, xmax = (x1, x2) if x1 < x2 else (x2, x1)
231
+
232
+ left = ((self.xmin, self._ymiddle), (xmin, self._ymiddle))
233
+ right = ((xmax, self._ymiddle), (self.xmax, self._ymiddle))
234
+ leftline = ((xmin, self._slider_ymin), (xmin, self._slider_ymax))
235
+ rightline = ((xmax, self._slider_ymin), (xmax, self._slider_ymax))
236
+ self.navigator.set_segments((left, leftline, right, rightline))
237
+ return
238
+
239
+
240
+ class LimMixin(DrawMixin):
241
+ def _restore_region(self, with_nav=True, empty=False):
242
+ if not self.background: self._create_background()
243
+
244
+ if empty: self.canvas.restore_region(self.background_empty)
245
+ elif with_nav: self.canvas.restore_region(self.background_with_nav)
246
+ else: self.canvas.renderer.restore_region(self.background)
247
+ return
248
+
249
+ def _copy_bbox(self):
250
+ self.ax_slider.xaxis.draw(self.canvas.renderer)
251
+ self.ax_slider.yaxis.draw(self.canvas.renderer)
252
+ self.slidercollection.draw(self.canvas.renderer)
253
+ self.background_empty = self.canvas.renderer.copy_from_bbox(self.fig.bbox)
254
+
255
+ self._draw_artist()
256
+ self.background = self.canvas.renderer.copy_from_bbox(self.fig.bbox)
257
+
258
+ self.navigator.draw(self.canvas.renderer)
259
+ self.background_with_nav = self.canvas.renderer.copy_from_bbox(self.fig.bbox)
260
+ return
261
+
262
+ def _on_release(self, e: MouseEvent):
263
+ if e.inaxes is not self.ax_slider: return
264
+ self.is_click, self.is_move = (False, False)
265
+
266
+ if self._navcoordinate[0] == self._navcoordinate[1]:
267
+ self._navcoordinate = (self._navcoordinate[0], self._navcoordinate[1]+self.min_distance)
268
+ self._set_navigator(*self._navcoordinate)
269
+ self._lim()
270
+
271
+ self.background = None
272
+ self._draw()
273
+ return
274
+
275
+ def _on_move(self, e):
276
+ self._restore_region(with_nav=(not self.is_click), empty=self.is_click)
277
+
278
+ self._on_move_action(e)
279
+
280
+ if self.in_slider:
281
+ self._change_coordinate()
282
+ if self.is_click:
283
+ nsub = self._navcoordinate[1] - self._navcoordinate[0]
284
+ if self.min_distance <= nsub: self._lim()
285
+ if self.is_move: self._set_navigator(*self._navcoordinate)
286
+ elif self.intx is not None: self._set_navigator(self._x_click, self.intx)
287
+ self.navigator.draw(self.canvas.renderer)
288
+ self._draw_blit_artist()
289
+ self._slider_move_action(e)
290
+ elif self.is_click:
291
+ self.navigator.draw(self.canvas.renderer)
292
+ self._draw_blit_artist()
293
+ else:
294
+ if self.in_slider or self.in_price or self.in_volume:
295
+ self._slider_move_action(e)
296
+ if self.in_price or self.in_volume:
297
+ self._chart_move_action(e)
298
+
299
+ self._blit()
300
+ return
301
+
302
+ def _draw_blit_artist(self):
303
+ return self._draw_artist()
304
+
305
+ def _lim(self):
306
+ xmin, xmax = self._navcoordinate
307
+
308
+ xmax += 1
309
+ self.ax_price.set_xlim(xmin, xmax)
310
+ self.ax_volume.set_xlim(xmin, xmax)
311
+
312
+ indmin, indmax = (xmin, xmax)
313
+ if xmin < 0: indmin = 0
314
+ if indmax < 1: indmax = 1
315
+ if indmin == indmax: indmax += 1
316
+ ymin, ymax = (self.df[self.low][indmin:indmax].min(), self.df[self.high][indmin:indmax].max())
317
+ ysub = (ymax - ymin) / 15
318
+ pmin, pmax = (ymin-ysub, ymax+ysub)
319
+ self.ax_price.set_ylim(pmin, pmax)
320
+
321
+ ymax = self.df[self.volume][indmin:indmax].max()
322
+ # self._vol_ymax = ymax*1.2
323
+ volmax = ymax * 1.2
324
+ self.ax_volume.set_ylim(0, volmax)
325
+
326
+ self.set_text_coordante(xmin, xmax, pmin, pmax, volmax)
327
+ return
328
+
329
+
330
+ class SimpleMixin(LimMixin):
331
+ simpler = False
332
+ limit_volume = 2_000
333
+
334
+ def __init__(self, *args, **kwargs):
335
+ super().__init__(*args, **kwargs)
336
+
337
+ # 영역 이동시 주가 collection
338
+ self.blitcandle = LineCollection([], animated=True)
339
+ self.ax_price.add_collection(self.blitcandle)
340
+ self.priceline = LineCollection([], animated=True, edgecolors='k')
341
+ self.ax_price.add_artist(self.priceline)
342
+
343
+ # 영역 이동시 거래량 collection
344
+ self.blitvolume = LineCollection([], animated=True, edgecolors=self.colors_volume)
345
+ self.ax_volume.add_collection(self.blitvolume)
346
+ return
347
+
348
+ def set_data(self, df):
349
+ super().set_data(df)
350
+
351
+ seg = self.df[['x', self.high, self.low]].agg(get_wickline, axis=1)
352
+ self.blitcandle.set_segments(seg)
353
+ self.blitcandle.set_edgecolor(self.df['edgecolor'])
354
+ self.priceline.set_verts([self.df[['x', self.close]].apply(tuple, axis=1).tolist()])
355
+
356
+ volmax = self.df[self.volume].max()
357
+ l = self.df.__len__()
358
+ if l < self.limit_volume:
359
+ volseg = self.df.loc[:, ['x', self.volume]].agg(get_volumeline, axis=1)
360
+ else:
361
+ n, step = (1, 1 / self.limit_volume)
362
+ for _ in range(self.limit_volume):
363
+ n -= step
364
+ volmin = volmax * n
365
+ length = self.df.loc[volmin < self.df[self.volume]].__len__()
366
+ if self.limit_volume < length: break
367
+
368
+ volseg = self.df.loc[volmin < self.df[self.volume], ['x', self.volume]].agg(get_volumeline, axis=1)
369
+ self.blitvolume.set_segments(volseg)
370
+
371
+ index = self.df.index[-1]
372
+ if index < 120: self._navcoordinate = (int(self.xmin)-1, int(self.xmax)+1)
373
+ else: self._navcoordinate = (index-80, index+10)
374
+ self._set_navigator(*self._navcoordinate)
375
+ self._lim()
376
+ return self._draw()
377
+
378
+ def _draw_blit_artist(self):
379
+ renderer = self.canvas.renderer
380
+
381
+ self.ax_price.xaxis.draw(renderer)
382
+ self.ax_price.yaxis.draw(renderer)
383
+
384
+ if self.simpler: self.blitcandle.draw(renderer)
385
+ elif self.candle_on_ma:
386
+ self.macollection.draw(renderer)
387
+ self.candlecollection.draw(renderer)
388
+ else:
389
+ self.candlecollection.draw(renderer)
390
+ self.macollection.draw(renderer)
391
+
392
+ self.ax_volume.xaxis.draw(renderer)
393
+ self.ax_volume.yaxis.draw(renderer)
394
+
395
+ self.blitvolume.draw(renderer)
396
+ return
397
+
398
+
399
+ class ClickMixin(SimpleMixin):
400
+ is_click_chart = False
401
+
402
+ def _on_click(self, e: MouseEvent):
403
+ if not self.is_click and e.button == MouseButton.LEFT:
404
+ if e.inaxes is self.ax_slider: pass
405
+ elif e.inaxes is self.ax_price or e.inaxes is self.ax_volume: return self._on_chart_click(e)
406
+ else: return
407
+ else: return
408
+
409
+ self.is_click = True
410
+
411
+ x = e.xdata.__int__()
412
+ left, right = self._navcoordinate
413
+ lmin, lmax = (left-self._navLineWidth, left+self._navLineWidth_half)
414
+ rmin, rmax = (right-self._navLineWidth_half, right+self._navLineWidth)
415
+
416
+ gtl, ltr = (lmax < x, x < rmin)
417
+ if gtl and ltr:
418
+ self._x_click = x
419
+ self.is_move = True
420
+ self.canvas.set_cursor(cursors.MOVE)
421
+ else:
422
+ self.canvas.set_cursor(cursors.RESIZE_HORIZONTAL)
423
+ if not gtl and lmin <= x:
424
+ self._x_click = right
425
+ elif not ltr and x <= rmax:
426
+ self._x_click = left
427
+ else:
428
+ self._x_click = x
429
+
430
+ # 그리기 후 최초 클릭이면 좌표 수정
431
+ if left == right:
432
+ self._navcoordinate = (x, x)
433
+ return
434
+
435
+ def _on_release(self, e: MouseEvent):
436
+ if not self.is_click: return
437
+ elif e.inaxes is self.ax_slider: return super()._on_release(e)
438
+ elif not self.in_price and not self.in_volume and not self.is_click_chart: return
439
+ self.canvas.set_cursor(cursors.POINTER)
440
+ self.is_click, self.is_move = (False, False)
441
+ self.is_click_chart = False
442
+
443
+ self._draw()
444
+ return self._restore_region()
445
+
446
+ def _on_chart_click(self, e: MouseEvent):
447
+ self.is_click = True
448
+ self.is_click_chart = True
449
+ self._x_click = e.x.__int__()
450
+ self.canvas.set_cursor(cursors.RESIZE_HORIZONTAL)
451
+ return
452
+
453
+ def _change_coordinate(self):
454
+ if self.is_click_chart: self._change_coordinate_chart()
455
+ else: super()._change_coordinate()
456
+ return
457
+
458
+ def _change_coordinate_chart(self, e: MouseEvent):
459
+ x = e.x.__int__()
460
+ left, right = self._navcoordinate
461
+ nsub = right - left
462
+ xsub = x - self._x_click
463
+ xdiv = (xsub / (1200 / nsub)).__int__()
464
+ if xdiv:
465
+ left, right = (left-xdiv, right-xdiv)
466
+ if -1 < right and left < self.df.index[-1]:
467
+ self._navcoordinate = (left, right)
468
+ self._x_click = x
469
+ return
470
+
471
+ def _on_move(self, e):
472
+ self._restore_region(with_nav=(not self.is_click), empty=self.is_click)
473
+
474
+ self._on_move_action(e)
475
+
476
+ if self.in_slider and not self.is_click_chart:
477
+ self._change_coordinate()
478
+ if self.is_click:
479
+ nsub = self._navcoordinate[1] - self._navcoordinate[0]
480
+ if self.min_distance <= nsub: self._lim()
481
+ if self.is_move: self._set_navigator(*self._navcoordinate)
482
+ elif self.intx is not None: self._set_navigator(self._x_click, self.intx)
483
+ self.navigator.draw(self.canvas.renderer)
484
+ self._draw_blit_artist()
485
+ self._slider_move_action(e)
486
+ elif self.is_click:
487
+ if self.is_click_chart and (self.in_price or self.in_volume):
488
+ if (self.vmin, self.vmax) != self._navcoordinate:
489
+ self._change_coordinate_chart(e)
490
+ self._lim()
491
+ self._set_navigator(*self._navcoordinate)
492
+ self.navigator.draw(self.canvas.renderer)
493
+ self._draw_blit_artist()
494
+ else:
495
+ if self.in_slider or self.in_price or self.in_volume:
496
+ self._slider_move_action(e)
497
+ if self.in_price or self.in_volume:
498
+ self._chart_move_action(e)
499
+
500
+ self._blit()
501
+ return
502
+
503
+
504
+ class SliderMixin(ClickMixin):
505
+ pass
506
+
507
+
508
+ class Chart(SliderMixin, CM, Mixin):
509
+ r"""
510
+ You can see the guidance document:
511
+ Korean: https://white.seolpyo.com/entry/147/
512
+ English: https://white.seolpyo.com/entry/148/
513
+
514
+ Variables:
515
+ unit_price, unit_volume: unit for price and volume. default ('원', '주').
516
+
517
+ figsize: figure size if you use plt.show(). default (12, 6).
518
+ ratio_ax_slider, ratio_ax_legend, ratio_ax_price, ratio_ax_volume: Axes ratio. default (3, 2, 18, 5).
519
+ adjust: figure adjust. default dict(top=0.95, bottom=0.05, left=0.01, right=0.93, wspace=0, hspace=0).
520
+ slider_top: ax_slider is located at the top or bottom. default True.
521
+ color_background: color of background. default '#fafafa'.
522
+ color_grid: color of grid. default '#d0d0d0'.
523
+
524
+ df: stock data.
525
+ date: date column key. default 'date'
526
+ Open, high, low, close: price column key. default ('open', 'high', 'low', 'close')
527
+ volume: volume column key. default 'volume'
528
+
529
+ label_ma: moving average legend label format. default '{}일선'
530
+ list_ma: Decide how many days to draw the moving average line. default (5, 20, 60, 120, 240)
531
+ list_macolor: Color the moving average line. If the number of colors is greater than the moving average line, black is applied. default ('darkred', 'fuchsia', 'olive', 'orange', 'navy', 'darkmagenta', 'limegreen', 'darkcyan',)
532
+
533
+ candle_on_ma: Decide whether to draw candles on the moving average line. default True
534
+ color_sliderline: Color of closing price line in ax_slider. default 'k'
535
+ color_navigatorline: Color of left and right dividing lines in selected area. default '#1e78ff'
536
+ color_navigator: Color of unselected area. default 'k'
537
+
538
+ color_up: The color of the candle. When the closing price is greater than the opening price. default '#fe3032'
539
+ color_down: The color of the candle. When the opening price is greater than the opening price. default '#0095ff'
540
+ color_flat: The color of the candle. WWhen the closing price is the same as the opening price. default 'k'
541
+ color_up_down: The color of the candle. If the closing price is greater than the opening price, but is lower than the previous day's closing price. default 'w'
542
+ color_down_up: The color of the candle. If the opening price is greater than the closing price, but is higher than the closing price of the previous day. default 'w'
543
+ colors_volume: The color of the volume bar. default '#1f77b4'
544
+
545
+ lineKwargs: Options applied to horizontal and vertical lines drawn along the mouse position. default dict(edgecolor='k', linewidth=1, linestyle='-')
546
+ textboxKwargs: Options that apply to the information text box. dufault dict(boxstyle='round', facecolor='w')
547
+
548
+ fraction: Decide whether to express information as a fraction. default False
549
+ candleformat: Candle information text format. default '{}\n\n종가:  {}\n등락률: {}\n대비:  {}\n시가:  {}({})\n고가:  {}({})\n저가:  {}({})\n거래량: {}({})'
550
+ volumeformat: Volume information text format. default '{}\n\n거래량   : {}\n거래량증가율: {}'
551
+ digit_price, digit_volume: Number of decimal places expressed in informational text. default (0, 0)
552
+
553
+ min_distance: Minimum number of candles that can be selected with the slider. default 30
554
+ simpler: Decide whether to display candles simply when moving the chart. default False
555
+ limit_volume: Maximum number of volume bars drawn when moving the chart. default 2_000
556
+ """
557
+ def _generate_data(self, df):
558
+ super()._generate_data(df)
559
+ return self.generate_data(self.df)
560
+
561
+ def _on_draw(self, e):
562
+ super()._on_draw(e)
563
+ return self.on_draw(e)
564
+
565
+ def _on_pick(self, e):
566
+ self.on_pick(e)
567
+ return super()._on_pick(e)
568
+
569
+ def _draw_artist(self):
570
+ super()._draw_artist()
571
+ return self.create_background()
572
+
573
+ def _blit(self):
574
+ super()._blit()
575
+ return self.on_blit()
576
+
577
+ def _on_move(self, e):
578
+ super()._on_move(e)
579
+ return self.on_move(e)
580
+
581
+
582
+ if __name__ == '__main__':
583
+ import json
584
+ from time import time
585
+
586
+ import matplotlib.pyplot as plt
587
+ from pathlib import Path
588
+
589
+ file = Path(__file__).parent / 'data/samsung.txt'
590
+ # file = Path(__file__).parent / 'data/apple.txt'
591
+ with open(file, 'r', encoding='utf-8') as txt:
592
+ data = json.load(txt)
593
+ data = data
594
+ df = pd.DataFrame(data)
595
+
596
+ t = time()
597
+ c = SliderMixin()
598
+ c.set_data(df)
599
+ t2 = time() - t
600
+ print(f'{t2=}')
601
+ plt.show()
@@ -0,0 +1,38 @@
1
+ import json
2
+ from pathlib import Path
3
+ from typing import Literal
4
+
5
+ import matplotlib.pyplot as plt
6
+ import pandas as pd
7
+
8
+
9
+ from seolpyo_mplchart import Chart
10
+
11
+
12
+ _name = {'samsung', 'apple'}
13
+ def sample(name: Literal['samsung', 'apple']='samsung'):
14
+ if name not in _name:
15
+ print('name should be either samsung or apple.')
16
+ return
17
+ file = Path(__file__).parent / f'data/{name}.txt'
18
+ with open(file, 'r', encoding='utf-8') as txt:
19
+ data = json.load(txt)
20
+ data = data
21
+ df = pd.DataFrame(data)
22
+
23
+ c = Chart()
24
+ if name == 'apple':
25
+ c.unit_price = '$'
26
+ c.unit_volume = 'vol'
27
+ c.digit_price = 3
28
+ c.label_ma = 'ma{}'
29
+ c.candleformat = '{}\n\nclose: {}\nrate: {}\ncompare: {}\nopen: {}({})\nhigh: {}({})\nlow: {}({})\nvolume: {}({})'
30
+ c.volumeformat = '{}\n\nvolume: {}\nvolume rate: {}'
31
+ c.set_data(df)
32
+ plt.show()
33
+
34
+ return
35
+
36
+
37
+ if __name__ == '__main__':
38
+ sample('apple')
@@ -0,0 +1,45 @@
1
+ from re import search
2
+
3
+ def convert_num(num):
4
+ if isinstance(num, float) and num % 1: return num
5
+ return int(num)
6
+
7
+
8
+ def float_to_str(num: float, digit=0):
9
+ num.__round__(digit)
10
+ text = f'{num:,.{digit}f}'
11
+ return text
12
+
13
+
14
+ dict_unit = {
15
+ '경': 10_000_000_000_000_000,
16
+ '조': 1_000_000_000_000,
17
+ '억': 100_000_000,
18
+ '만': 10_000,
19
+ '천': 1_000,
20
+ }
21
+ dict_unit_en = {
22
+ 'Qd': 1_000_000_000_000_000,
23
+ 'T': 1_000_000_000_000,
24
+ 'B': 1_000_000_000,
25
+ 'M': 1_000_000,
26
+ 'K': 1_000,
27
+ }
28
+
29
+ def convert_unit(value, digit=0, word='원'):
30
+ v = abs(value)
31
+ du = dict_unit if search('[가-힣]', word) else dict_unit_en
32
+ for unit, n in du.items():
33
+ if n <= v:
34
+ num = value / n
35
+ return f'{float_to_str(num, digit)}{unit} {word}'
36
+ if not value % 1: value = int(value)
37
+ text = f'{float_to_str(value, digit)}{word}'
38
+ return text
39
+
40
+
41
+ if __name__ == '__main__':
42
+ a = 456.123
43
+ print(float_to_str(a))
44
+ print(float_to_str(a, 2))
45
+ print(float_to_str(a, 6))