PySide6(QTChart)如何放大X轴并动态更改Y轴范围

2024-09-25 00:20:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图让我的烛台图表能够滚动进出,这将影响x轴上的日期和y轴上的范围。例如,如果我缩小,则日期应该变宽,范围也随之变宽,同时使所有烛台保持一定程度的集中,并且当我放大y轴范围时,范围应该变紧,从而使烛台集中得更近,而x轴日期之间的距离更小,目前,这是一个最小的工作产品,我有图表工作现在:

from PySide6.QtCharts import *
from PySide6.QtGui import *
from PySide6.QtWidgets import *
from PySide6.QtCore import *
from functools import cached_property
import sys
sys.path.append("..")


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setWindowState(Qt.WindowMaximized)
        self.setWindowTitle("Indicator Tool")

        data = [(15, 1626237960, 4353.75, 4354.5, 4353.75, 4354.25, 73.0), (30, 1626238860, 4352.75, 4353.25, 4352.5, 4352.5, 210.0), (45, 1626239760, 4354.75, 4354.75, 4354.0, 4354.5, 96.0), (60, 1626240660, 4355.25, 4355.75, 4355.25, 4355.5, 90.0), (75, 1626241560, 4356.0, 4356.25, 4355.75, 4356.0, 83.0), (90, 1626242460, 4353.0, 4353.75, 4353.0, 4353.0, 177.0), (105, 1626243360, 4351.25, 4352.25, 4351.25, 4352.0, 351.0), (120, 1626244260, 4355.5, 4355.5, 4355.5, 4355.5, 46.0), (135, 1626245160, 4355.0, 4355.5, 4354.75, 4355.0, 87.0), (150, 1626246060, 4351.25, 4352.0, 4350.25, 4351.75, 787.0), (165, 1626246960, 4354.25, 4355.0, 4353.75, 4354.75, 431.0), (180, 1626247860, 4356.25, 4356.25, 4355.25, 4355.5, 393.0), (195, 1626248760, 4361.0, 4361.0, 4360.0, 4360.0, 365.0), (210, 1626249660, 4357.0, 4357.5, 4356.75, 4356.75, 381.0), (225, 1626250560, 4355.25, 4355.5, 4355.0, 4355.0, 189.0), (240, 1626251460, 4359.75, 4359.75, 4358.25, 4358.5, 268.0), (255, 1626252360, 4359.75, 4360.25, 4358.75, 4359.0, 300.0), (270, 1626253260, 4358.25, 4360.0, 4358.0, 4359.75, 573.0), (285, 1626254160, 4362.25, 4362.75, 4362.25, 4362.25, 182.0), (300, 1626255060, 4363.75, 4364.75, 4363.25, 4364.75, 370.0), (315, 1626255960, 4365.0, 4365.5, 4364.5, 4365.0, 509.0), (330, 1626256860, 4361.75, 4362.25, 4361.75, 4362.25, 115.0), (345, 1626257760, 4362.5, 4362.75, 4362.5, 4362.75, 60.0), (360, 1626258660, 4364.0, 4364.0, 4363.5, 4363.5, 52.0), (375, 1626259560, 4364.5, 4364.75, 4364.25, 4364.75, 264.0), (390, 1626260460, 4364.0, 4364.0, 4363.25, 4363.25, 264.0), (405, 1626261360, 4365.25, 4365.75, 4365.25, 4365.75, 159.0), (420, 1626262260, 4365.75, 4366.25, 4365.75, 4366.25, 102.0), (435, 1626263160, 4365.5, 4366.0, 4365.25, 4366.0, 73.0), (450, 1626264060, 4368.25, 4368.5, 4368.0, 4368.0, 191.0), (465, 1626264960, 4368.75, 4369.25, 4368.5, 4369.0, 336.0), (480, 1626265860, 4372.5, 4374.0, 4370.5, 4374.0, 3336.0), (495, 1626266760, 4377.0, 4377.75, 4377.0, 4377.5, 335.0), (510, 1626267660, 4377.75, 4379.0, 4377.75, 4378.75, 405.0), (525, 1626268560, 4377.75, 4377.75, 4377.5, 4377.75, 141.0), (540, 1626269460, 4379.75, 4382.25, 4379.75, 4382.25, 5678.0), (555, 1626270360, 4381.25, 4382.75, 4381.0, 4382.25, 4247.0), (570, 1626271260, 4378.25, 4379.5, 4377.0, 4377.0, 5458.0), (585, 1626272160, 4378.5, 4379.0, 4377.5, 4378.75, 2199.0), (600, 1626273060, 4373.5, 4373.5, 4372.0, 4373.0, 3043.0), (615, 1626273960, 4375.25, 4375.5, 4374.25, 4375.25, 1543.0), (630, 1626274860, 4374.5, 4375.0, 4373.5, 4374.25, 1348.0), (645, 1626275760, 4370.5, 4370.75, 4369.5, 4370.75, 1878.0), (660, 1626276660, 4364.75, 4365.5, 4363.75, 4363.75, 3083.0), (675, 1626277560, 4359.25, 4359.25, 4357.25, 4358.5, 4229.0), (690, 1626278460, 4359.5, 4361.75, 4359.25, 4361.5, 5614.0), (705, 1626279360, 4365.25, 4366.0, 4364.0, 4365.5, 2277.0), (720, 1626280260, 4369.25, 4369.5, 4368.5, 4368.5, 1383.0), (735, 1626281160, 4370.75, 4371.0, 4369.0, 4370.75, 3953.0), (750, 1626282060, 4371.0, 4371.25, 4370.5, 4370.75, 1144.0), (765, 1626282960, 4366.75, 4367.0, 4365.5, 4365.5, 1228.0), (780, 1626283860, 4366.0, 4367.75, 4365.75, 4367.5, 2396.0), (795, 1626284760, 4371.5, 4371.75, 4370.25, 4370.5, 1205.0), (810, 1626285660, 4371.0, 4371.25, 4369.75, 4370.25, 2373.0), (825, 1626286560, 4374.0, 4374.75, 4373.75, 4374.75, 2617.0), (840, 1626287460, 4373.25, 4373.5, 4372.0, 4373.0, 1476.0), (855, 1626288360, 4372.0, 4372.5, 4371.75, 4372.5, 873.0), (870, 1626289260, 4372.0, 4372.0, 4371.25, 4371.5, 831.0), (885, 1626290160, 4369.75, 4370.25, 4369.25, 4370.0, 1904.0), (900, 1626291060, 4366.5, 4368.75, 4366.5, 4368.0, 2710.0), (915, 1626291960, 4369.75, 4370.75, 4369.25, 4370.75, 2395.0), (930, 1626292860, 4369.75, 4370.0, 4369.0, 4369.75, 5744.0), (945, 1626293760, 4368.5, 4369.25, 4368.5, 4369.25, 203.0), (960, 1626294660, 4369.0, 4369.25, 4369.0, 4369.25, 26.0), (975, 1626295560, 4368.5, 4368.5, 4368.25, 4368.25, 43.0), (990, 1626300000, 4366.5, 4367.25, 4365.75, 4366.25, 341.0), (1005, 1626300900, 4367.75, 4367.75, 4367.25, 4367.5, 99.0), (1020, 1626301800, 4368.25, 4368.5, 4368.0, 4368.0, 85.0), (1035, 1626302700, 4368.0, 4368.25, 4368.0, 4368.0, 49.0), (1050, 1626303600, 4367.0, 4367.5, 4367.0, 4367.0, 126.0), (1065, 1626304500, 4367.25, 4367.5, 4367.25, 4367.25, 11.0), (1080, 1626305400, 4367.25, 4367.25, 4367.25, 4367.25, 1.0), (1095, 1626306300, 4366.0, 4366.25, 4365.75, 4366.25, 91.0), (1110, 1626307200, 4367.75, 4368.0, 4367.5, 4367.75, 132.0), (1125, 1626308100, 4363.5, 4363.5, 4362.5, 4362.5, 558.0), (1140, 1626309000, 4363.25, 4363.75, 4363.0, 4363.0, 87.0), (1155, 1626309900, 4362.0, 4362.0, 4361.75, 4361.75, 57.0), (1170, 1626310800, 4360.25, 4360.25, 4359.25, 4359.25, 242.0), (1185, 1626311700, 4359.0, 4359.0, 4358.5, 4358.5, 124.0), (1200, 1626312600, 4360.25, 4361.0, 4360.25, 4360.25, 272.0), (1215, 1626313500, 4362.75, 4363.25, 4362.5, 4362.5, 159.0), (1230, 1626314400, 4362.25, 4362.25, 4362.0, 4362.25, 52.0), (1245, 1626315300, 4362.25, 4362.25, 4362.0, 4362.0, 62.0), (1260, 1626316200, 4364.0, 4364.25, 4363.25, 4363.5, 241.0), (1275, 1626317100, 4362.5, 4362.75, 4362.0, 4362.5, 117.0), (1290, 1626318000, 4362.5, 4363.0, 4362.0, 4362.0, 165.0), (1305, 1626318900, 4363.0, 4363.0, 4362.75, 4363.0, 56.0), (1320, 1626319800, 4363.75, 4364.0, 4363.75, 4364.0, 146.0), (1335, 1626320700, 4365.25, 4365.5, 4365.0, 4365.25, 73.0), (1350, 1626321600, 4364.75, 4364.75, 4364.5, 4364.75, 16.0), (1365, 1626322500, 4364.25, 4364.5, 4364.25, 4364.5, 57.0), (1380, 1626323400, 4365.25, 4365.25, 4365.0, 4365.0, 25.0), (1395, 1626324300, 4364.25, 4364.5, 4364.25, 4364.5, 31.0), (1410, 1626325200, 4363.0, 4363.5, 4362.75, 4363.5, 125.0), (1425, 1626326100, 4364.25, 4364.25, 4364.25, 4364.25, 40.0), (1440, 1626327000, 4365.0, 4365.25, 4364.5, 4364.75, 212.0), (1455, 1626327900, 4364.25, 4364.25, 4364.0, 4364.25, 22.0), (1470, 1626328800, 4364.75, 4365.75, 4364.75, 4365.25, 321.0), (1485, 1626329700, 4364.75, 4364.75, 4364.5, 4364.5, 97.0), (1500, 1626330600, 4364.75, 4365.0, 4364.25, 4364.5, 298.0), (1515, 1626331500, 4363.75, 4364.25, 4363.75, 4364.0, 135.0), (1530, 1626332400, 4364.0, 4364.5, 4361.0, 4362.0, 1189.0), (1545, 1626333300, 4365.0, 4365.0, 4363.0, 4363.5, 587.0), (1560, 1626334200, 4357.25, 4357.25, 4354.75, 4355.25, 1219.0), (1575, 1626335100, 4362.0, 4362.25, 4360.75, 4360.75, 346.0), (1590, 1626336000, 4360.5, 4360.5, 4360.0, 4360.0, 395.0), (1605, 1626336900, 4366.0, 4367.0, 4366.0, 4366.0, 364.0), (1620, 1626337800, 4369.75, 4370.0, 4368.25, 4369.5, 443.0), (1635, 1626338700, 4368.75, 4369.5, 4368.75, 4369.25, 358.0), (1650, 1626339600, 4368.75, 4368.75, 4368.0, 4368.5, 137.0), (1665, 1626340500, 4365.0, 4365.5, 4364.25, 4364.5, 196.0), (1680, 1626341400, 4362.5, 4362.75, 4362.0, 4362.5, 633.0), (1695, 1626342300, 4362.25, 4362.75, 4362.0, 4362.75, 199.0), (1710, 1626343200, 4364.75, 4365.0, 4364.0, 4364.75, 736.0), (1725, 1626344100, 4358.5, 4358.5,4357.0, 4357.25, 925.0), (1740, 1626345000, 4354.25, 4354.5, 4352.5, 4353.25, 1259.0), (1755, 1626345900, 4357.5, 4358.0, 4357.25, 4357.75, 208.0), (1770, 1626346800, 4353.5, 4355.5, 4353.25, 4355.0, 1080.0), (1785, 1626347700, 4353.5, 4353.5, 4352.5, 4352.5, 394.0), (1800, 1626348600, 4348.25, 4349.0, 4348.0, 4349.0, 360.0), (1815, 1626349500, 4353.25, 4353.5, 4352.75, 4353.25, 550.0), (1830, 1626350400, 4353.25, 4354.0, 4352.75, 4353.0, 829.0), (1845, 1626351300, 4350.75, 4351.5, 4350.25, 4350.5, 590.0), (1860, 1626352200, 4353.0, 4353.25, 4352.0, 4353.0, 389.0), (1875, 1626353100, 4355.5, 4355.75, 4354.25, 4354.75, 523.0), (1890, 1626354000, 4351.5, 4352.5, 4351.25, 4352.5, 1360.0), (1905, 1626354900, 4354.0, 4354.5, 4353.75, 4354.25, 1866.0), (1920, 1626355800, 4353.25, 4357.5, 4353.0, 4356.0, 12422.0), (1935, 1626356700, 4348.75, 4352.0, 4347.75, 4351.0, 6192.0), (1950, 1626357600, 4358.75, 4360.0, 4358.25, 4358.75, 6794.0), (1965, 1626358500, 4359.5, 4360.5, 4359.25, 4359.75, 2912.0), (1980, 1626359400, 4351.5, 4353.5, 4351.0, 4352.75, 3718.0), (1995, 1626360300, 4350.0, 4352.0, 4347.75, 4350.75, 6836.0), (2010, 1626361200, 4353.25, 4354.25, 4351.75, 4353.75, 3954.0), (2025, 1626362100, 4355.0, 4355.25, 4354.25, 4354.25, 1948.0), (2040, 1626363000, 4356.0, 4356.5, 4355.5, 4356.25, 3067.0), (2055, 1626365460, 4348.75, 4349.5, 4347.0, 4347.25, 4812.0), (2070, 1626366360, 4351.0, 4351.5, 4350.75, 4351.25, 1268.0), (2085, 1626367260, 4353.5, 4353.75, 4352.5, 4352.75, 1096.0), (2100, 1626368160, 4352.25, 4353.25, 4352.0, 4352.0, 1504.0), (2115, 1626369060, 4347.0, 4347.5, 4346.75, 4347.5, 1716.0), (2130, 1626369960, 4343.5, 4345.0, 4343.5, 4344.75, 1026.0), (2145, 1626370860, 4338.25, 4338.75, 4337.75, 4338.75, 2774.0), (2160, 1626371760, 4334.0, 4335.0, 4332.5, 4333.0, 7733.0), (2175, 1626661980, 4300.0, 4300.5, 4300.0, 4300.5, 109.0), (2190, 1626662880, 4301.25, 4301.5, 4301.0, 4301.0, 85.0), (2205, 1626663780, 4303.75, 4304.0, 4303.25, 4303.5, 152.0), (2220, 1626664680, 4304.0, 4304.5, 4304.0, 4304.25, 108.0), (2235, 1626665580, 4305.0, 4305.25, 4304.75, 4305.0, 93.0), (2250, 1626666480, 4303.75, 4304.0, 4303.0, 4303.0, 134.0), (2265, 1626667380, 4305.0, 4305.0, 4304.25, 4304.5, 23.0), (2280, 1626668280, 4303.25, 4303.25, 4302.75, 4303.0, 77.0), (2295, 1626669180, 4303.75, 4303.75, 4303.5, 4303.5, 48.0), (2310, 1626670080, 4299.5, 4299.5, 4298.75, 4298.75, 208.0), (2325, 1626670980, 4301.0, 4302.0, 4301.0, 4302.0, 89.0), (2340, 1626671880, 4303.25, 4303.5, 4302.75, 4302.75, 92.0), (2355, 1626672780, 4304.25, 4305.25, 4304.0, 4305.0, 151.0), (2370, 1626673680, 4303.0, 4303.0, 4301.5, 4301.5, 272.0), (2385, 1626674580, 4304.5, 4304.75, 4304.0, 4304.75, 233.0), (2400, 1626675480, 4304.0, 4304.5, 4303.5, 4304.25, 187.0), (2415, 1626676380, 4305.5, 4305.5, 4305.0, 4305.25, 174.0), (2430, 1626677280, 4307.0, 4307.5, 4306.75, 4307.0, 293.0), (2445, 1626678180, 4305.25, 4305.5, 4303.5, 4304.0, 860.0), (2460, 1626679080, 4305.0, 4305.75, 4304.0, 4304.5, 933.0), (2475, 1626679980, 4298.0, 4299.75, 4297.5, 4298.0, 840.0), (2490, 1626680880, 4295.0, 4295.75, 4294.25, 4295.25, 497.0), (2505, 1626681780, 4303.5, 4304.25, 4303.0, 4303.75, 794.0), (2520, 1626682680, 4302.25, 4303.5, 4302.25, 4303.25, 378.0), (2535, 1626683580, 4297.25, 4298.75, 4296.75, 4298.25, 479.0), (2550, 1626684480, 4287.5, 4288.75, 4287.25, 4288.75, 569.0), (2565, 1626685380, 4287.5, 4289.0, 4287.0, 4288.75, 603.0), (2580, 1626686280, 4289.25, 4290.25, 4289.25, 4289.75, 181.0), (2595, 1626687180, 4287.5, 4289.0, 4287.5, 4288.75, 493.0), (2610, 1626688080, 4291.5, 4291.75, 4291.0, 4291.75, 175.0), (2625, 1626688980, 4291.0, 4291.25, 4289.5, 4289.75, 476.0), (2640, 1626689880, 4286.75, 4287.5, 4286.25, 4286.5, 317.0), (2655, 1626690780, 4288.25, 4288.25, 4287.75, 4288.0, 241.0), (2670, 1626691680, 4287.0, 4288.0, 4287.0, 4287.25, 380.0), (2685, 1626692580, 4287.5, 4287.75, 4283.75, 4284.0, 1194.0), (2700, 1626693480, 4282.0, 4282.75, 4281.75, 4282.5, 390.0), (2715, 1626694380, 4272.25, 4273.0, 4271.0, 4272.5, 2019.0), (2730, 1626695280, 4270.75, 4272.25, 4270.0, 4271.5, 1550.0), (2745, 1626696180, 4273.0, 4273.75, 4272.5, 4272.75, 584.0), (2760, 1626697080, 4265.75, 4265.75, 4264.5, 4265.25, 1076.0), (2775, 1626697980, 4269.25, 4269.75, 4268.0, 4269.75, 969.0), (2790, 1626698880, 4267.25, 4267.5, 4265.75, 4266.0, 1419.0), (2805, 1626699780, 4266.75, 4267.5, 4265.5, 4265.75, 1155.0), (2820, 1626700680, 4264.0, 4264.75, 4262.5, 4262.75, 1385.0), (2835, 1626701580, 4263.5, 4266.5, 4261.0, 4265.0, 17859.0), (2850, 1626702480, 4264.5, 4264.75, 4261.75, 4261.75, 6718.0), (2865, 1626703380, 4240.75, 4242.75, 4239.25, 4241.75, 13594.0), (2880, 1626704280, 4234.25, 4236.5, 4231.25, 4232.25, 8556.0), (2895, 1626705180, 4238.75, 4239.5, 4237.5, 4239.25, 5309.0), (2910, 1626706080, 4247.5, 4249.75, 4247.0, 4249.75, 3954.0), (2925, 1626706980, 4248.5, 4253.25, 4248.0, 4253.25, 7975.0), (2940, 1626707880, 4254.25, 4255.0, 4253.0, 4254.25, 4967.0), (2955, 1626708780, 4255.5, 4257.5, 4255.25, 4256.0, 3957.0), (2970, 1626709680, 4256.5, 4258.25, 4255.25, 4256.0, 2902.0), (2985, 1626710580, 4246.25, 4247.0, 4243.5, 4245.75, 5645.0), (3000, 1626711480, 4244.5, 4244.75, 4243.25, 4243.75, 1884.0), (3015, 1626712380, 4235.5, 4236.5, 4234.5, 4234.75, 2849.0), (3030, 1626713280, 4230.75, 4231.25, 4229.5, 4230.0, 2907.0), (3360, 1626732660, 4260.25, 4260.5, 4260.0, 4260.0, 78.0), (3375, 1626733560, 4258.75, 4258.75, 4258.75, 4258.75, 17.0), (3390, 1626734460, 4258.75, 4259.0, 4258.5, 4258.5, 28.0), (3405, 1626735360, 4259.75, 4259.75, 4259.5, 4259.5, 45.0), (3420, 1626736260, 4261.25, 4261.5, 4261.0, 4261.0, 145.0), (3435, 1626737160, 4260.0, 4260.0, 4259.25, 4259.75, 125.0), (3450, 1626738060, 4262.0, 4262.75, 4261.75, 4262.5, 151.0), (3465, 1626738960, 4262.5, 4262.5, 4262.0, 4262.0, 145.0), (3480, 1626739860, 4261.5, 4262.25, 4261.25, 4262.25, 299.0), (3495, 1626740760, 4265.5, 4267.0, 4265.5, 4267.0, 461.0), (3510, 1626741660, 4266.75, 4267.25, 4266.5, 4266.75, 168.0), (3525, 1626742560, 4268.0, 4268.0, 4267.5, 4267.5, 52.0), (3540, 1626743460, 4265.75, 4267.0, 4265.5, 4267.0, 218.0), (3555, 1626744360, 4268.25, 4268.75, 4268.25, 4268.5, 123.0), (3570, 1626745260, 4268.75, 4269.25, 4268.75, 4269.25, 80.0), (3585, 1626746160, 4272.5, 4273.0, 4272.5, 4272.5, 217.0), (3600, 1626747060, 4274.25, 4274.25, 4273.5, 4273.5, 146.0), (3615, 1626747960, 4273.5, 4274.0, 4273.25, 4274.0, 208.0), (3630, 1626748860, 4273.5, 4273.5, 4273.0, 4273.0, 102.0), (3645, 1626749760, 4272.5, 4272.5, 4271.25, 4271.5, 330.0), (3660, 1626750660, 4270.25, 4270.75, 4270.25, 4270.75, 96.0), (3675, 1626751560, 4269.25, 4269.75, 4269.0, 4269.0, 69.0), (3690, 1626752460, 4258.5, 4258.75, 4257.5, 4258.0, 619.0), (3705, 1626753360, 4259.0, 4259.25, 4257.5, 4258.0, 432.0), (3720, 1626754260, 4258.0, 4258.75, 4258.0, 4258.75, 130.0), (3735, 1626755160, 4257.75, 4257.75, 4257.0, 4257.25, 179.0)]

        self.stockWidget = StockChart(data) 
        
        self.initUI()
        

    def initUI(self):
        grid = QGridLayout()
        grid.setRowStretch(0, 1)
        grid.setRowStretch(1, 2)

        #Widget, row, column, rowspan, colspan
        grid.addWidget(self.stockWidget, 0, 0, 1, 2)

        centralWidget = QWidget()
        centralWidget.setLayout(grid)
        self.setCentralWidget(centralWidget)


class StockChart(QWidget):
    def __init__(self, data, parent=None):
        super().__init__(parent)
        self.data = data
        self.initUI()

    @cached_property
    def view(self):
        return list()

    def initUI(self):
        lay = QVBoxLayout(self)

        series = QCandlestickSeries()
        series.setIncreasingColor(QColor(Qt.green))
        series.setDecreasingColor(QColor(Qt.red))

        print(self.data)

        for row in self.data:
            time = row[1] * 1000
            open = row[2]
            high = row[3]
            low = row[4]
            close = row[5]
            volume = row[6]
            series.append(QCandlestickSet(open, high, low, close, time)) 

        self.chart = QChart()
        self.chart.addSeries(series)
        self.chart.legend().hide()

        self.chart.createDefaultAxes()
        self.chart.setTitle("ES Real Time Chart")
        self.chart.setAnimationOptions(QChart.SeriesAnimations)
         
        chart_view = QChartView(self.chart)
        chart_view.setRenderHint(QPainter.Antialiasing)
        chart_view.setRubberBand(QChartView.HorizontalRubberBand)

        lay.addWidget(chart_view)
        self.view.append(chart_view)

    def wheelEvent(self, event: QWheelEvent) -> None:
        return super().wheelEvent(event)
        

if __name__ == "__main__":
    app = QApplication([])
    window = MainWindow()
    window.show()
    sys.exit(app.exec())

我尝试模拟大量数据,以便您可以看到我的问题,所有蜡烛都非常小,因为范围没有正确居中,我在x轴上有一个橡胶垫设置,用于选择特定的x轴值,我只是希望能够动态改变y轴,以匹配当前的最小/最大x轴值,也能够放大和缩小滚轮

以下是它当前的样子:

enter image description here 多谢各位


Tags: fromimportselfviewdatainitdefchart