Chart.cs 155 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194
  1. using Microsoft.VisualBasic.CompilerServices;
  2. using MuchInfo.Chart.Data.EnumTypes;
  3. using MuchInfo.Chart.Data.Interfaces;
  4. using MuchInfo.Chart.Data.Models;
  5. using MuchInfo.Chart.FormulaEdit;
  6. using MuchInfo.Chart.Infrastructure.Data;
  7. using MuchInfo.Chart.Infrastructure.Helpers;
  8. using MuchInfo.Chart.Infrastructure.Utilities;
  9. using MuchInfo.Chart.WPF.Controls.Drawing;
  10. using MuchInfo.Chart.WPF.Controls.QuickTimeSpan;
  11. using MuchInfo.Chart.WPF.Controls.ValueScaling;
  12. using MuchInfo.Chart.WPF.Helpers;
  13. using MuchInfo.Chart.WPF.Primitives;
  14. using MuchInfo.Chart.WPF.Primitives.Drawing;
  15. using MuchInfo.Chart.WPF.Primitives.Interfaces;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Globalization;
  19. using System.Linq;
  20. using System.Runtime.CompilerServices;
  21. using System.Threading.Tasks;
  22. using System.Windows;
  23. using System.Windows.Controls;
  24. using System.Windows.Controls.Primitives;
  25. using System.Windows.Input;
  26. using System.Windows.Media;
  27. using System.Windows.Shapes;
  28. using System.Windows.Threading;
  29. using Xceed.Wpf.Toolkit;
  30. namespace MuchInfo.Chart.WPF
  31. {
  32. #region Delegates
  33. /// <summary>
  34. /// 通过CycleType加载历史周期数据
  35. /// </summary>
  36. /// <param name="exchangeCode">交易所代码</param>
  37. /// <param name="goodsCode">商品代码</param>
  38. /// <param name="type">周期</param>
  39. /// <param name="startTime">起始时间</param>
  40. /// <param name="endTime">结束时间</param>
  41. /// <param name="topN">数据前N条</param>
  42. /// <returns>List{ILineDataPoint}.</returns>
  43. public delegate List<ILineDataPoint> LoadHistoryCycleDataByCycleTypeEventHandler(int exchangeCode, string goodsCode, CycleType type, TimeSpan timeSpan, DateTime startTime, DateTime endTime, int topN);
  44. #endregion Delegates
  45. /// <summary>
  46. /// WPF图表控件
  47. /// </summary>
  48. public class Chart : Control
  49. {
  50. #region 依赖属性
  51. /// <summary>
  52. /// 左右键增减周期值
  53. /// </summary>
  54. public static readonly DependencyProperty LeftRightKeyToAccelerationProperty =
  55. DependencyProperty.Register("LeftRightKeyToAcceleration", typeof(int), typeof(Chart), new PropertyMetadata(10));
  56. /// <summary>
  57. /// 鼠标滚轮加速缩放因数
  58. /// </summary>
  59. public static readonly DependencyProperty AcceleratedWheelFactorProperty =
  60. DependencyProperty.Register("AcceleratedWheelFactor", typeof(int), typeof(Chart), new PropertyMetadata(10));
  61. /// <summary>
  62. /// 鼠标滚轮加速缩放键
  63. /// </summary>
  64. public static readonly DependencyProperty AcceleratedWheelModifierKeysProperty =
  65. DependencyProperty.Register("AcceleratedWheelModifierKeys", typeof(ModifierKeys), typeof(Chart),
  66. new PropertyMetadata(ModifierKeys.Control));
  67. /// <summary>
  68. /// 背景色
  69. /// </summary>
  70. public static readonly DependencyProperty ChartBackgroundProperty =
  71. DependencyProperty.Register("ChartBackground", typeof(SolidColorBrush), typeof(Chart),
  72. new PropertyMetadata(new SolidColorBrush(Colors.Black),
  73. new PropertyChangedCallback(Chart.ChartFontSizeProperty_Changed)));
  74. /// <summary>
  75. /// 字体大小
  76. /// </summary>
  77. public static readonly DependencyProperty ChartFontSizeProperty = DependencyProperty.Register("ChartFontSize",
  78. typeof(double), typeof(Chart),
  79. new PropertyMetadata(12d, new PropertyChangedCallback(Chart.ChartFontSizeProperty_Changed)));
  80. /// <summary>
  81. /// 绘图类型
  82. /// </summary>
  83. public static readonly DependencyProperty ChartLanguageProperty = DependencyProperty.Register("ChartLanguage",
  84. typeof(ChartLanguageType), typeof(Chart),
  85. new PropertyMetadata(ChartLanguageType.SimplifiedChinese,
  86. new PropertyChangedCallback(Chart.ChartLanguageProperty_Changed)));
  87. /// <summary>
  88. /// The chart time span property
  89. /// </summary>
  90. public static readonly DependencyProperty ChartTimeSpanProperty = DependencyProperty.Register("ChartTimeSpan",
  91. typeof(TimeSpan), typeof(Chart),
  92. new PropertyMetadata(default(TimeSpan), Chart.ChartTimeSpanProperty_Changed));
  93. /// <summary>
  94. /// The current goods property
  95. /// </summary>
  96. public static readonly DependencyProperty CurrentGoodsProperty = DependencyProperty.Register("CurrentGoods",
  97. typeof(GoodsInfo), typeof(Chart), new PropertyMetadata(null, Chart.CurrentGoodsProperty_Changed));
  98. /// <summary>
  99. /// 商品周期
  100. /// </summary>
  101. public static readonly DependencyProperty CycleTypeProperty = DependencyProperty.Register("CycleType",
  102. typeof(CycleType), typeof(Chart),
  103. new PropertyMetadata(CycleType.Minute, new PropertyChangedCallback(Chart.ChartCycleTypeProperty_Changed)));
  104. /// <summary>
  105. /// 商品周期集合
  106. /// </summary>
  107. public static readonly DependencyProperty CycleItemsProperty =
  108. DependencyProperty.Register("CycleItems", typeof(List<CycleType>),
  109. typeof(Chart), new PropertyMetadata(new List<CycleType>(), new PropertyChangedCallback(Chart.ChartCycleTypeItemsProperty_Changed)));
  110. /// <summary>
  111. /// 当前Tik数据,只需更新Tik数据即可,控件自动累加
  112. /// </summary>
  113. public BarDataPoint CurrentTik
  114. {
  115. get { return (BarDataPoint)GetValue(CurrentTikProperty); }
  116. set { SetValue(CurrentTikProperty, value); }
  117. }
  118. public static readonly DependencyProperty CurrentTikProperty =
  119. DependencyProperty.Register("CurrentTik", typeof(BarDataPoint), typeof(Chart),
  120. new PropertyMetadata(null, new PropertyChangedCallback(Chart.ChartTikProperty_Changed)));
  121. /// <summary>
  122. /// 画线工具位置
  123. /// </summary>
  124. public static readonly DependencyProperty DrawingToolPositionProperty =
  125. DependencyProperty.Register("DrawingToolPosition", typeof(DrawingToolPositionType), typeof(Chart),
  126. new PropertyMetadata(DrawingToolPositionType.Left,
  127. new PropertyChangedCallback(Chart.DrawingToolPositionProperty_Changed)));
  128. /// <summary>
  129. /// 当前画线工具:可通过设置值来模拟画线工具单击事件
  130. /// </summary>
  131. public static readonly DependencyProperty DrawingToolTypeProperty =
  132. DependencyProperty.Register("DrawingToolType", typeof(DrawingToolType), typeof(Chart),
  133. new PropertyMetadata(DrawingToolType.Cross, new PropertyChangedCallback(Chart.DrawingToolType_Changed)));
  134. /// <summary>
  135. /// 蜡烛图叠加是否填充
  136. /// </summary>
  137. public static readonly DependencyProperty FillComparisonCandleProperty =
  138. DependencyProperty.Register("FillComparisonCandle", typeof(bool), typeof(Chart),
  139. new PropertyMetadata(false, Chart.ChartProperty_Changed));
  140. /// <summary>
  141. /// 网格透明度
  142. /// </summary>
  143. public static readonly DependencyProperty GridOpacityProperty = DependencyProperty.Register("GridOpacity",
  144. typeof(double), typeof(Chart),
  145. new PropertyMetadata(0.15d, new PropertyChangedCallback(Chart.ChartProperty_Changed)));
  146. /// <summary>
  147. /// 绘图类型
  148. /// </summary>
  149. public static readonly DependencyProperty LinePlotTypeProperty = DependencyProperty.Register("LinePlotType",
  150. typeof(LinePlotType), typeof(Chart),
  151. new PropertyMetadata(LinePlotType.Candlestick,
  152. new PropertyChangedCallback(Chart.ChartLineTypePlotProperty_Changed)));
  153. /// <summary>
  154. /// 最大加载数据条数
  155. /// </summary>
  156. public static readonly DependencyProperty MaxLoadRecordCountProperty =
  157. DependencyProperty.Register("MaxLoadRecordCount", typeof(int), typeof(Chart), new PropertyMetadata(500));
  158. /// <summary>
  159. /// 最大显示数据条数
  160. /// </summary>
  161. public static readonly DependencyProperty MaxRecordCountProperty = DependencyProperty.Register(
  162. "MaxRecordCount", typeof(int), typeof(Chart),
  163. new PropertyMetadata(1000, new PropertyChangedCallback(Chart.ChartMinMaxRecordProperty_Changed)));
  164. /// <summary>
  165. /// 最小显示数据条数
  166. /// </summary>
  167. public static readonly DependencyProperty MinRecordCountProperty = DependencyProperty.Register(
  168. "MinRecordCount", typeof(int), typeof(Chart),
  169. new PropertyMetadata(50, new PropertyChangedCallback(Chart.ChartMinMaxRecordProperty_Changed)));
  170. /// <summary>
  171. /// 显示数据条数
  172. /// </summary>
  173. public static readonly DependencyProperty RecordCountProperty = DependencyProperty.Register("RecordCount",
  174. typeof(int), typeof(Chart),
  175. new PropertyMetadata(100, new PropertyChangedCallback(Chart.ChartRecordCountProperty_Changed)));
  176. /// <summary>
  177. /// 是否显示画线工具
  178. /// </summary>
  179. public static readonly DependencyProperty ShowDrawingToolProperty =
  180. DependencyProperty.Register("ShowDrawingTool", typeof(Visibility), typeof(Chart),
  181. new PropertyMetadata(Visibility.Visible,
  182. new PropertyChangedCallback(ElementInvalidator.PropertyChanged_InvalidateMeasureArrange)));
  183. /// <summary>
  184. /// 画图工具是否可用
  185. /// </summary>
  186. public bool DrawingToolIsEnabled
  187. {
  188. get { return (bool)GetValue(DrawingToolIsEnabledProperty); }
  189. set { SetValue(DrawingToolIsEnabledProperty, value); }
  190. }
  191. public static readonly DependencyProperty DrawingToolIsEnabledProperty =
  192. DependencyProperty.Register("DrawingToolIsEnabled", typeof(bool), typeof(Chart), new PropertyMetadata(true));
  193. /// <summary>
  194. /// 最多可显示多少个ChartPane
  195. /// </summary>
  196. public int MaxChartPaneCount
  197. {
  198. get { return (int)GetValue(MaxChartPaneCountProperty); }
  199. set { SetValue(MaxChartPaneCountProperty, value); }
  200. }
  201. public static readonly DependencyProperty MaxChartPaneCountProperty =
  202. DependencyProperty.Register("MaxChartPaneCount", typeof(int), typeof(Chart), new PropertyMetadata(5));
  203. /// <summary>
  204. /// 设置分时图网格与刻度的坐标
  205. /// </summary>
  206. public Brush TimeSharingGridBrush
  207. {
  208. get { return (Brush)GetValue(TimeSharingGridBrushProperty); }
  209. set { SetValue(TimeSharingGridBrushProperty, value); }
  210. }
  211. public static readonly DependencyProperty TimeSharingGridBrushProperty =
  212. DependencyProperty.Register("TimeSharingGridBrush", typeof(Brush), typeof(Chart),
  213. new PropertyMetadata(Brushes.Red));
  214. /// <summary>
  215. /// 设置合适的边距缓冲区
  216. /// </summary>
  217. public int RightBuffer
  218. {
  219. get { return (int)GetValue(RightBufferProperty); }
  220. set { SetValue(RightBufferProperty, value); }
  221. }
  222. public static readonly DependencyProperty RightBufferProperty =
  223. DependencyProperty.Register("RightBuffer", typeof(int), typeof(Chart), new PropertyMetadata(20));
  224. public Visibility CycleBarVisibility
  225. {
  226. get { return (Visibility)GetValue(CycleBarVisibilityProperty); }
  227. set { SetValue(CycleBarVisibilityProperty, value); }
  228. }
  229. // Using a DependencyProperty as the backing store for CycleBarVisibility. This enables animation, styling, binding, etc...
  230. public static readonly DependencyProperty CycleBarVisibilityProperty =
  231. DependencyProperty.Register("CycleBarVisibility", typeof(Visibility), typeof(Chart), new PropertyMetadata(Visibility.Visible, new PropertyChangedCallback(Chart.ChartCycleBarVisibity)));
  232. /// <summary>
  233. /// 加载历史周期数据 By CycleType
  234. /// </summary>
  235. public LoadHistoryCycleDataByCycleTypeEventHandler LoadHistoryCycleDataByCycleType;
  236. /// <summary>
  237. /// drawLINE
  238. /// </summary>
  239. private static IDrawingTool _oldDrawingTool;
  240. //头容器
  241. private StackPanel _chartHeaderStackPanel;
  242. //chart pane容器
  243. private Grid _chartPanesGrid;
  244. private ChartToolbarExpander _chartToolbarExpander;
  245. private List<ComparisonGoods> _comparisonGoodsList;
  246. private DateTime _crossDateTime;
  247. /// <summary>
  248. /// 十字光标鼠标按下状态
  249. /// </summary>
  250. private bool _crossFlag;
  251. //十字光标容器
  252. private Canvas _crossHairGrid;
  253. private Path _crossPath;
  254. private Line _hline = new Line(); //横线
  255. private Line _vline = new Line(); //竖线
  256. /// <summary>
  257. /// 十字光标面板x坐标
  258. /// </summary>
  259. private double _crossX;
  260. private IDrawingTool _currentDrawingTool;
  261. private double _currentX;
  262. private double _currentY;
  263. //数据滑块容器
  264. private StackPanel _dateChartSliderStackPanel;
  265. private DateScaleDisplay _dateScaleDisplay;
  266. private Button _downloadMoreButton;
  267. private TextBlock _downloadMoreButtonPlaceHolder;
  268. private ChartDrawingToolbar _drawingToolbar;
  269. //画线工具容器
  270. private StackPanel _drawingToolStackPanel;
  271. //商品信息
  272. private GoodsInfo _goodsInfo;
  273. ///指标器容器
  274. //private ComboBox _indicatorComboBox;
  275. public Grid _mainGrid;
  276. private BusyIndicator _exportBusyIndicator;
  277. /// <summary>
  278. /// 上一个鼠标样式
  279. /// </summary>
  280. private PointerType _oldPointerType;
  281. private Grid _pointerGrid;
  282. private Popup _pointerPopup;
  283. /// <summary>
  284. /// 当前鼠标样式,初始为显示十字光标
  285. /// </summary>
  286. private PointerType _pointerType;
  287. private Popup _popup;
  288. /// <summary>
  289. /// The timespan control
  290. /// </summary>
  291. private QuickTimeSpan _timeSpanControl;
  292. //显示标签控件
  293. //private TextBlock _symbolTextBlock;
  294. private Image _toolImage;
  295. /// <summary>
  296. /// 当前图中添加图表容器
  297. /// </summary>
  298. private List<ChartPane> _chartPanes = new List<ChartPane>();
  299. private DispatcherTimer _timeSharingTimer;
  300. private DisplayFormula timeSharingFormulaModel; //分时图实体,用于更新当前分时图数据
  301. private DisplayFormula _tikFormulaModel; //即时图,更新Tik图表数据
  302. private DisplayFormula _minuteFormulaModel; //
  303. private Dictionary<string, DisplayFormula> _comparisonFommulaHsSet = new Dictionary<string, DisplayFormula>();
  304. private List<ChartPane> _chartCachePanes = new List<ChartPane>(); //缓存切换周期时商品状态
  305. private static readonly object _lockObject = new Object();
  306. #endregion Fields
  307. #region Constructors
  308. /// <summary>
  309. /// 静态构造函数
  310. /// </summary>
  311. static Chart()
  312. {
  313. DefaultStyleKeyProperty.OverrideMetadata(typeof(Chart), new FrameworkPropertyMetadata(typeof(Chart)));
  314. }
  315. /// <summary>
  316. /// 构造函数
  317. /// </summary>
  318. public Chart()
  319. {
  320. this.Focusable = true;
  321. //默认加载简体中文
  322. LanguageManager.SetLanguage(CultureInfo.InstalledUICulture.Name);
  323. _dateScaleDisplay = new DateScaleDisplay(this);
  324. DateScaler = new AllDateScaler(this);
  325. _toolImage = new Image();
  326. DateChartSlider = new DateChartSlider(this);
  327. TimeSpanPicker = new QuickTimeSpan(this);
  328. RightBuffer = 0;
  329. //ChartRedrawTimer.Interval = new TimeSpan(0, 0, 0, 0, ChartRedrawSpeed);
  330. _oldDrawingTool = new DrawingTrendLine();
  331. _pointerType = PointerType.CursorWithData;
  332. _oldPointerType = PointerType.CursorWithData;
  333. LockPainting = false;
  334. ComparisionDictionary = new Dictionary<string, List<ILineDataPoint>>();
  335. _timeSharingTimer = new DispatcherTimer();
  336. _timeSharingTimer.Interval = new TimeSpan(0, 0, 1, 0);
  337. _timeSharingTimer.Tick += _timeSharingTimer_Tick;
  338. ComparisionTimeShareDic = new Dictionary<string, List<ILineDataPoint>>();
  339. }
  340. #endregion Constructors
  341. #region Properties
  342. #region Public Properties
  343. /// <summary>
  344. /// ChartRedrawSpeed,图表重绘速度,用于鼠标拖动,画图工具拖动等,单位为毫秒
  345. /// </summary>
  346. public int ChartRedrawSpeed = 1000 / 30;
  347. /// <summary>
  348. /// ChartRedrawTimer,图表重绘定时器
  349. /// </summary>
  350. DispatcherTimer ChartRedrawTimer = new DispatcherTimer();
  351. private const string _defaoltIndecator = "EMA4,MACD";
  352. private DateTime _goodsQuoteTime; //分时图行情时间
  353. /// <summary>
  354. /// 左右键增减周期值
  355. /// </summary>
  356. public int LeftRightKeyToAcceleration
  357. {
  358. get { return (int)GetValue(LeftRightKeyToAccelerationProperty); }
  359. set { SetValue(LeftRightKeyToAccelerationProperty, value); }
  360. }
  361. /// <summary>
  362. /// 加速因数
  363. /// </summary>
  364. public int AcceleratedWheelFactor
  365. {
  366. get
  367. {
  368. return (int)base.GetValue(Chart.AcceleratedWheelFactorProperty);
  369. }
  370. set
  371. {
  372. base.SetValue(Chart.AcceleratedWheelFactorProperty, value);
  373. }
  374. }
  375. /// <summary>
  376. /// 加速鼠标中键缩放键
  377. /// </summary>
  378. public ModifierKeys AcceleratedWheelModifierKeys
  379. {
  380. get
  381. {
  382. return (ModifierKeys)base.GetValue(Chart.AcceleratedWheelModifierKeysProperty);
  383. }
  384. set
  385. {
  386. base.SetValue(Chart.AcceleratedWheelModifierKeysProperty, value);
  387. }
  388. }
  389. /// <summary>
  390. /// 获取或设置一个用于描述控件背景的画笔。
  391. /// </summary>
  392. public SolidColorBrush ChartBackground
  393. {
  394. get
  395. {
  396. return (SolidColorBrush)base.GetValue(Chart.ChartBackgroundProperty);
  397. }
  398. set
  399. {
  400. base.SetValue(Chart.ChartBackgroundProperty, value);
  401. }
  402. }
  403. /// <summary>
  404. /// Gets the color of the chart background.
  405. /// </summary>
  406. public Color ChartBackgroundColor
  407. {
  408. get
  409. {
  410. return ColorHelper.ColorFromBrush(ChartBackground);
  411. }
  412. }
  413. /// <summary>
  414. /// 获取或设置字号。
  415. /// </summary>
  416. public double ChartFontSize
  417. {
  418. get
  419. {
  420. var value = (double)base.GetValue(Chart.ChartFontSizeProperty);
  421. if (value <= 0) value = 12;
  422. return value;
  423. }
  424. set
  425. {
  426. if (value <= 0) value = 12;
  427. base.SetValue(Chart.ChartFontSizeProperty, value);
  428. }
  429. }
  430. /// <summary>
  431. /// 图表语言类型
  432. /// </summary>
  433. public ChartLanguageType ChartLanguage
  434. {
  435. get
  436. {
  437. return (ChartLanguageType)base.GetValue(Chart.ChartLanguageProperty);
  438. }
  439. set
  440. {
  441. base.SetValue(Chart.ChartLanguageProperty, value);
  442. }
  443. }
  444. /// <summary>
  445. /// Gets or sets the chart time span.
  446. /// </summary>
  447. /// <value>The chart time span.</value>
  448. public TimeSpan ChartTimeSpan
  449. {
  450. get { return (TimeSpan)GetValue(ChartTimeSpanProperty); }
  451. set { SetValue(ChartTimeSpanProperty, value); }
  452. }
  453. public IDrawingTool CurrentDrawingTool
  454. {
  455. get
  456. {
  457. return _currentDrawingTool;
  458. }
  459. set
  460. {
  461. _currentDrawingTool = value;
  462. bool flag = value != null;
  463. if (flag)
  464. {
  465. PointerType = PointerType.Drawing;
  466. }
  467. }
  468. }
  469. ///// <summary>
  470. ///// 商品信息
  471. //// </summary>
  472. public GoodsInfo CurrentGoods
  473. {
  474. get { return (GoodsInfo)GetValue(CurrentGoodsProperty); }
  475. set { SetValue(CurrentGoodsProperty, value); }
  476. }
  477. /// <summary>
  478. /// 商品周期
  479. /// </summary>
  480. public List<CycleType> CycleItems
  481. {
  482. get { return (List<CycleType>)GetValue(CycleItemsProperty); }
  483. set { SetValue(CycleItemsProperty, value); }
  484. }
  485. /// <summary>
  486. /// 根据周期生成TimeSpan
  487. /// </summary>
  488. public TimeSpan CurrentTimeFrame
  489. {
  490. get
  491. {
  492. switch (CycleType)
  493. {
  494. case CycleType.Minute:
  495. return new TimeSpan(0, 1, 0);
  496. case CycleType.Minute3:
  497. return new TimeSpan(0, 3, 0);
  498. case CycleType.Minute5:
  499. return new TimeSpan(0, 5, 0);
  500. case CycleType.Minute10:
  501. return new TimeSpan(0, 10, 0);
  502. case CycleType.Minute15:
  503. return new TimeSpan(0, 15, 0);
  504. case CycleType.Minute30:
  505. return new TimeSpan(0, 30, 0);
  506. case CycleType.Minute60:
  507. return new TimeSpan(1, 0, 0);
  508. case CycleType.Minute90:
  509. return new TimeSpan(1, 30, 0);
  510. case CycleType.Minute120:
  511. return new TimeSpan(2, 0, 0);
  512. case CycleType.Minute180:
  513. return new TimeSpan(3, 0, 0);
  514. case CycleType.Minute240:
  515. return new TimeSpan(4, 0, 0);
  516. case CycleType.Hour:
  517. return new TimeSpan(1, 0, 0);
  518. case CycleType.Day:
  519. return new TimeSpan(1, 0, 0, 0);
  520. case CycleType.Week:
  521. return new TimeSpan(7, 0, 0, 0, 0);
  522. case CycleType.Month:
  523. return new TimeSpan(30, 0, 0, 0);
  524. case CycleType.Quarter:
  525. return new TimeSpan(90, 0, 0, 0);
  526. case CycleType.Year:
  527. return new TimeSpan(255, 0, 0, 0);
  528. case CycleType.Custom:
  529. return ChartTimeSpan;
  530. default:
  531. //分时图为1分
  532. return new TimeSpan(0, 1, 0);
  533. }
  534. }
  535. }
  536. /// <summary>
  537. /// 是否在周期内
  538. /// </summary>
  539. public bool IsNotInCycle(DateTime tikTime, ref DateTime CycleDate)
  540. {
  541. var Rlt = default(bool);
  542. if (DataSet != null && DataSet.DataPoints != null && DataSet.DataPoints.Count() > 0 && tikTime != DateTime.MinValue)
  543. {
  544. var dt = DataSet.DataPoints.Last().Date;
  545. var ts = tikTime - dt;
  546. var newDt = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, 0).AddMinutes(CurrentTimeFrame.TotalMinutes);
  547. switch (CycleType)
  548. {
  549. case CycleType.Minute:
  550. case CycleType.Minute3:
  551. case CycleType.Minute5:
  552. case CycleType.Minute10:
  553. case CycleType.Minute15:
  554. case CycleType.Minute30:
  555. case CycleType.Minute60:
  556. case CycleType.Minute90:
  557. case CycleType.Minute120:
  558. case CycleType.Minute180:
  559. case CycleType.Minute240:
  560. case CycleType.Custom:
  561. case CycleType.Hour:
  562. Rlt = tikTime >= newDt;
  563. if (Rlt)
  564. {
  565. CycleDate = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, 0).AddMinutes((Math.Floor((ts.TotalMinutes / CurrentTimeFrame.TotalMinutes)) * CurrentTimeFrame.TotalMinutes));
  566. }
  567. break;
  568. case CycleType.Day:
  569. Rlt = tikTime >= new DateTime(dt.Year, dt.Month, dt.Day ).AddDays(1);
  570. if (Rlt)
  571. {
  572. CycleDate = new DateTime(dt.Year, dt.Month, dt.Day).AddDays((Math.Floor((ts.TotalDays / CurrentTimeFrame.TotalDays)) * CurrentTimeFrame.TotalDays));
  573. }
  574. break;
  575. case CycleType.Week:
  576. Rlt = tikTime >= new DateTime(dt.Year, dt.Month, dt.Day ).AddDays((7 - ((int)(DayOfWeek)dt.DayOfWeek)));
  577. if (Rlt)
  578. {
  579. CycleDate = new DateTime(dt.Year, dt.Month, dt.Day).AddDays((Math.Floor((ts.TotalDays / CurrentTimeFrame.TotalDays)) * CurrentTimeFrame.TotalDays));
  580. }
  581. break;
  582. case CycleType.Month:
  583. Rlt = tikTime >= new DateTime(dt.Year, dt.Month , 1).AddMonths(1);
  584. if (Rlt)
  585. {
  586. CycleDate = new DateTime(dt.Year, tikTime.Month, 1);
  587. }
  588. break;
  589. case CycleType.Quarter:
  590. Rlt = tikTime >= new DateTime(dt.Year, dt.Month , 1).AddMonths(3);
  591. if (Rlt)
  592. {
  593. var mm = (Math.Floor((ts.TotalDays / CurrentTimeFrame.TotalDays)) * CurrentTimeFrame.TotalDays);
  594. CycleDate = new DateTime(dt.Year, dt.Month, 1).AddDays(mm);
  595. }
  596. break;
  597. case CycleType.Year:
  598. Rlt = tikTime >= new DateTime(dt.Year + 1, 1, 1);
  599. if (Rlt)
  600. {
  601. CycleDate = new DateTime(tikTime.Year, 1, 1);
  602. }
  603. break;
  604. default:
  605. Rlt = false;
  606. break;
  607. }
  608. //Console.WriteLine("time:{0} tikTime:{1} newdate Rlt:{2} RltTime:{3}", dt.ToString(), tikTime.ToString(), newDt.ToString(), Rlt.ToString(), CycleDate.ToString());
  609. }
  610. return Rlt;
  611. }
  612. /// <summary>
  613. /// 商品周期
  614. /// </summary>
  615. public CycleType CycleType
  616. {
  617. get
  618. {
  619. return (CycleType)base.GetValue(Chart.CycleTypeProperty);
  620. }
  621. set
  622. {
  623. if (value != CycleType.Custom)
  624. {
  625. ChartTimeSpan = default(TimeSpan);
  626. }
  627. base.SetValue(Chart.CycleTypeProperty, value);
  628. }
  629. }
  630. /// <summary>
  631. /// 数据滑块
  632. /// </summary>
  633. public DateChartSlider DateChartSlider
  634. {
  635. get;
  636. set;
  637. }
  638. /// <summary>
  639. /// Gets the drawing min chart span percent.
  640. /// </summary>
  641. public float DrawingMinChartSpanPercent
  642. {
  643. get
  644. {
  645. return 5f;
  646. }
  647. }
  648. /// <summary>
  649. /// 画线工具位置
  650. /// </summary>
  651. public DrawingToolPositionType DrawingToolPosition
  652. {
  653. get
  654. {
  655. return (DrawingToolPositionType)base.GetValue(Chart.DrawingToolPositionProperty);
  656. }
  657. set
  658. {
  659. base.SetValue(Chart.DrawingToolPositionProperty, value);
  660. }
  661. }
  662. /// <summary>
  663. /// 当前画线工具
  664. /// </summary>
  665. public DrawingToolType DrawingToolType
  666. {
  667. get
  668. {
  669. return (DrawingToolType)base.GetValue(Chart.DrawingToolTypeProperty);
  670. }
  671. set
  672. {
  673. base.SetValue(Chart.DrawingToolTypeProperty, value);
  674. }
  675. }
  676. /// <summary>
  677. /// 蜡烛图叠加是否填充
  678. /// </summary>
  679. public bool FillComparisonCandle
  680. {
  681. get { return (bool)GetValue(FillComparisonCandleProperty); }
  682. set { SetValue(FillComparisonCandleProperty, value); }
  683. }
  684. /// <summary>
  685. /// 获取或设置一个用于描述控件背景的画笔。
  686. /// </summary>
  687. public double GridOpacity
  688. {
  689. get
  690. {
  691. return (double)base.GetValue(Chart.GridOpacityProperty);
  692. }
  693. set
  694. {
  695. base.SetValue(Chart.GridOpacityProperty, value);
  696. }
  697. }
  698. /// <summary>
  699. /// Gets a value indicating whether this instance is dark background.
  700. /// </summary>
  701. public bool IsDarkBackground
  702. {
  703. get
  704. {
  705. var brush = ChartBackground as SolidColorBrush;
  706. if (brush != null)
  707. {
  708. return ColorHelper.ColorIsDark(brush.Color);
  709. }
  710. return false;
  711. }
  712. }
  713. /// <summary>
  714. /// Gets a value indicating whether [limit drawings by span].
  715. /// </summary>
  716. public bool LimitDrawingsBySpan
  717. {
  718. get
  719. {
  720. return true;
  721. }
  722. }
  723. /// <summary>
  724. /// 获取或设置一个用于描述控件背景的画笔。
  725. /// </summary>
  726. public LinePlotType LinePlotType
  727. {
  728. get
  729. {
  730. return (LinePlotType)base.GetValue(Chart.LinePlotTypeProperty);
  731. }
  732. set
  733. {
  734. base.SetValue(Chart.LinePlotTypeProperty, value);
  735. }
  736. }
  737. /// <summary>
  738. /// 最大加载数据条数
  739. /// </summary>
  740. public int MaxLoadRecordCount
  741. {
  742. get { return (int)GetValue(MaxLoadRecordCountProperty); }
  743. set { SetValue(MaxLoadRecordCountProperty, value); }
  744. }
  745. /// <summary>
  746. /// 最大条数
  747. /// </summary>
  748. public int MaxRecordCount
  749. {
  750. get
  751. {
  752. return (int)base.GetValue(Chart.MaxRecordCountProperty);
  753. }
  754. set
  755. {
  756. base.SetValue(Chart.MaxRecordCountProperty, value);
  757. }
  758. }
  759. /// <summary>
  760. /// 最小条数
  761. /// </summary>
  762. public int MinRecordCount
  763. {
  764. get
  765. {
  766. return (int)base.GetValue(Chart.MinRecordCountProperty);
  767. }
  768. set
  769. {
  770. base.SetValue(Chart.MinRecordCountProperty, value);
  771. }
  772. }
  773. /// <summary>
  774. /// 返回叠加商品
  775. /// </summary>
  776. public List<ComparisonGoods> ComparisonGoodsList
  777. {
  778. get
  779. {
  780. return _comparisonGoodsList;
  781. }
  782. }
  783. /// <summary>
  784. /// 分时图使用的显示最大点数据,根据开收盘时间计算
  785. /// </summary>
  786. private int _timeSharingMaxPointCount = 60;
  787. internal int MaxRecordCountInternal
  788. {
  789. get
  790. {
  791. if (CycleType == CycleType.TimeSharing)
  792. {
  793. return _timeSharingMaxPointCount;
  794. }
  795. else
  796. {
  797. return MaxRecordCount;
  798. }
  799. }
  800. }
  801. internal int MinRecordCountInternal
  802. {
  803. get
  804. {
  805. if (CycleType == CycleType.TimeSharing)
  806. {
  807. return _timeSharingMaxPointCount;
  808. }
  809. else
  810. {
  811. return MinRecordCount;
  812. }
  813. }
  814. }
  815. internal int RecordCountInternal
  816. {
  817. get
  818. {
  819. if (CycleType == CycleType.TimeSharing)
  820. {
  821. return _timeSharingMaxPointCount;
  822. }
  823. else
  824. {
  825. return RecordCount;
  826. }
  827. }
  828. }
  829. /// <summary>
  830. /// Gets or sets the type of the pointer.
  831. /// </summary>
  832. /// <value>The type of the pointer.</value>
  833. public PointerType PointerType
  834. {
  835. get
  836. {
  837. return _pointerType;
  838. }
  839. set
  840. {
  841. bool flag = value != _pointerType;
  842. if (flag)
  843. {
  844. bool flag2 = value != PointerType.Drawing && value != PointerType.Erase;
  845. if (flag2)
  846. {
  847. _oldPointerType = value;
  848. }
  849. _pointerType = value;
  850. flag2 = (_pointerType != PointerType.Drawing);
  851. if (flag2)
  852. {
  853. CurrentDrawingTool = null;
  854. }
  855. }
  856. }
  857. }
  858. /// <summary>
  859. /// 显示数据条数
  860. /// </summary>
  861. public int RecordCount
  862. {
  863. get
  864. {
  865. return (int)base.GetValue(Chart.RecordCountProperty);
  866. }
  867. set
  868. {
  869. base.SetValue(Chart.RecordCountProperty, value);
  870. }
  871. }
  872. /// <summary>
  873. /// Gets or sets the show drawing tool.
  874. /// </summary>
  875. public Visibility ShowDrawingTool
  876. {
  877. get
  878. {
  879. return (Visibility)base.GetValue(Chart.ShowDrawingToolProperty);
  880. }
  881. set
  882. {
  883. base.SetValue(Chart.ShowDrawingToolProperty, value);
  884. }
  885. }
  886. /// <summary>
  887. /// Gets or sets a value indicating whether [show legend arrows].
  888. /// </summary>
  889. public bool ShowLegendArrows
  890. {
  891. get;
  892. set;
  893. }
  894. /// <summary>
  895. /// Gets or sets the time span picker.
  896. /// </summary>
  897. public QuickTimeSpan TimeSpanPicker
  898. {
  899. get
  900. {
  901. return _timeSpanControl;
  902. }
  903. set
  904. {
  905. _timeSpanControl = value;
  906. }
  907. }
  908. #endregion Public Properties
  909. #region Internal Properties
  910. /// <summary>
  911. /// 叠加商品字典
  912. /// </summary>
  913. internal Dictionary<string, List<ILineDataPoint>> ComparisionDictionary
  914. {
  915. get;
  916. set;
  917. }
  918. /// <summary>
  919. /// 分时图商品字典
  920. /// </summary>
  921. internal Dictionary<string, List<ILineDataPoint>> ComparisionTimeShareDic
  922. {
  923. get;
  924. set;
  925. }
  926. /// <summary>
  927. /// Gets the current symbol.
  928. /// </summary>
  929. internal string CurrentSymbol
  930. {
  931. get
  932. {
  933. var goods = CurrentGoods;
  934. if (goods == null || string.IsNullOrWhiteSpace(goods.Symbol)) return string.Empty;
  935. return goods.Symbol;
  936. }
  937. }
  938. /// <summary>
  939. /// 数据集
  940. /// </summary>
  941. internal ChartDataSet DataSet
  942. {
  943. get;
  944. set;
  945. }
  946. /// <summary>
  947. /// Gets or sets the date scaler.
  948. /// </summary>
  949. internal IDateScaler DateScaler
  950. {
  951. get;
  952. set;
  953. }
  954. private int _offSetNum = 0;
  955. /// <summary>
  956. /// Gets or sets a value indicating whether [lock painting].
  957. /// </summary>
  958. internal bool LockPainting
  959. {
  960. get;
  961. set;
  962. }
  963. /// <summary>
  964. /// 是否为分时图,控制部分内容不显示
  965. /// </summary>
  966. //internal bool IsTimeShare
  967. //{
  968. // get;
  969. // set;
  970. //}
  971. #endregion Internal Properties
  972. #endregion Properties
  973. #region Methods
  974. #region Public Methods
  975. /// <summary>
  976. /// 添加指标窗口
  977. /// </summary>
  978. public void AddIndicator()
  979. {
  980. //todo:
  981. //var formula = new FormulaModel();
  982. //formula.FormulaName = indicatorType;
  983. //var displayFormula = new DisplayFormula(formula, DataSet, this);
  984. // AddPlot(displayFormula, null);
  985. var listPlots = DisplayFormula.CreateDisplayFormula(DataSet, this);
  986. if (listPlots != null)
  987. {
  988. ChartPane pane = null;
  989. foreach (var displayFormula in listPlots)
  990. {
  991. pane = AddFormula(displayFormula, false, pane);
  992. }
  993. }
  994. Refresh(true);
  995. }
  996. /// <summary>
  997. /// 清除叠加商品
  998. /// </summary>
  999. public void ClearComparison()
  1000. {
  1001. if (_comparisonGoodsList != null) _comparisonGoodsList.Clear();
  1002. if ((ComparisionDictionary == null || !ComparisionDictionary.Any()) &&
  1003. (ComparisionTimeShareDic == null || !ComparisionTimeShareDic.Any())) return;
  1004. ComparisionDictionary.Clear();
  1005. var mainScalelayer = GetMainScaleLayer();
  1006. if (mainScalelayer == null) return;
  1007. var plots = mainScalelayer.AllPlots.Where(z => z is ComparisonPlot);
  1008. if (plots == null || !plots.Any()) return;
  1009. var comparsions = plots.ToList();
  1010. foreach (var comparison in comparsions)
  1011. {
  1012. comparison.Delete();
  1013. }
  1014. mainScalelayer.ComparisonFormulas.Clear();
  1015. //清除分时图叠加
  1016. if (ComparisionTimeShareDic != null && ComparisionTimeShareDic.Any())
  1017. {
  1018. ComparisionTimeShareDic.Clear();
  1019. }
  1020. if (_comparisonFommulaHsSet != null && _comparisonFommulaHsSet.Any())
  1021. {
  1022. _comparisonFommulaHsSet.Clear();
  1023. }
  1024. if (timeSharingFormulaModel != null)
  1025. {
  1026. var compesionsPlots = timeSharingFormulaModel.ScaleLayer.AllPlots.Where(z => z is ComparisonPlot);
  1027. if (compesionsPlots.Any()) return;
  1028. comparsions = compesionsPlots.ToList();
  1029. foreach (var comparison in comparsions)
  1030. {
  1031. comparison.Delete();
  1032. }
  1033. }
  1034. Refresh();
  1035. }
  1036. /// <summary>
  1037. /// 在派生类中重写后,每当应用程序代码或内部进程调用 <see cref="M:System.Windows.FrameworkElement.ApplyTemplate" />,都将调用此方法。
  1038. /// </summary>
  1039. public override void OnApplyTemplate()
  1040. {
  1041. base.OnApplyTemplate();
  1042. try
  1043. {
  1044. BuildParts();
  1045. //初始化画图工具栏
  1046. InitializeDrawingTool();
  1047. //添加历史价格图和成交量图
  1048. AddDefaultIndicators();
  1049. //添加图表底部控件
  1050. BuildBottomControls();
  1051. }
  1052. catch { ;}
  1053. }
  1054. /// <summary>
  1055. /// 设置叠加商品
  1056. /// </summary>
  1057. /// <param name="goodsList">The goods list.</param>
  1058. public void SetComparisonGoodsList(List<ComparisonGoods> goodsList)
  1059. {
  1060. Dispatcher.BeginInvoke(new Action(() =>
  1061. {
  1062. if (goodsList == null || !goodsList.Any()) return;
  1063. ClearComparison();
  1064. if (this.CycleType == CycleType.TimeSharing)
  1065. {
  1066. foreach (var goods in goodsList)
  1067. {
  1068. AddComparisonFormula(null, null, goods);
  1069. }
  1070. _comparisonGoodsList = goodsList;
  1071. Refresh(true);
  1072. return;
  1073. }
  1074. //Clear里有清空变量,放在后面
  1075. _comparisonGoodsList = goodsList;
  1076. var chartPane = GetMainChartPane(); //要叠加的容器
  1077. foreach (var goods in goodsList)
  1078. {
  1079. //不叠加空和自己
  1080. if (goods == null || string.IsNullOrWhiteSpace(goods.Symbol) ||
  1081. string.CompareOrdinal(goods.Symbol, CurrentSymbol) == 0) continue;
  1082. var dataPoints = LoadComparisonDataSet(goods);
  1083. if (dataPoints == null) continue;
  1084. ComparisionDictionary.Add(goods.Symbol, dataPoints);
  1085. //添加叠加商品
  1086. if (chartPane != null)
  1087. {
  1088. AddComparisonFormula(new ChartDataSet(dataPoints), chartPane, goods);
  1089. }
  1090. }
  1091. Refresh(true);
  1092. }), DispatcherPriority.Background);
  1093. }
  1094. /// <summary>
  1095. /// 更新数据集合-行情组件取服务数据回发时调用
  1096. /// </summary>
  1097. /// <param name="points">The points.</param>
  1098. public void UpdateDataPoints(IEnumerable<ILineDataPoint> points)
  1099. {
  1100. Task.Factory.StartNew(new Action(() =>
  1101. {
  1102. var lineDataPoints = points == null ? null : points.ToList();
  1103. if (lineDataPoints == null || !lineDataPoints.Any()) return;
  1104. DataSet = DataSet ?? new ChartDataSet();
  1105. //集合都要按时间顺排序,才能画图
  1106. var oldDataPoints = DataSet.DataPoints ?? new List<ILineDataPoint>();
  1107. var totalDataPoints = lineDataPoints.Union(oldDataPoints, new DataPointComparer<ILineDataPoint>());
  1108. DataSet.DataPoints = CaclulateCycleDataIncrease(totalDataPoints.OrderBy(z => z.Date).ToList());
  1109. Dispatcher.BeginInvoke(new Action(() =>
  1110. {
  1111. var allDateScaler = (AllDateScaler)DateScaler;
  1112. allDateScaler.SetDates(DataSet.DataPoints, CurrentTimeFrame, false);
  1113. Refresh(true);
  1114. //刷新叠加商品
  1115. RefreshComparsionGoods();
  1116. }), DispatcherPriority.Background);
  1117. }));
  1118. }
  1119. /// <summary>
  1120. /// 更新数据集合-行情组件取服务数据回发时调用
  1121. /// </summary>
  1122. /// <param name="points">The points.</param>
  1123. public void UpdateTimeSharingDataPoints(IEnumerable<ILineDataPoint> points)
  1124. {
  1125. Dispatcher.BeginInvoke(new Action(() =>
  1126. {
  1127. var lineDataPoints = points == null ? null : points.ToList();
  1128. if (lineDataPoints == null || !lineDataPoints.Any()) return;
  1129. DataSet = DataSet ?? new ChartDataSet();
  1130. //集合都要按时间顺排序,才能画图
  1131. DataSet.DataPoints = points.OrderBy(z => z.Date).ToList();
  1132. if (timeSharingFormulaModel == null) return;
  1133. {
  1134. timeSharingFormulaModel.UpdateTimeSharingData(DataSet);
  1135. }
  1136. Refresh(true);
  1137. }), DispatcherPriority.Background);
  1138. }
  1139. /// <summary>
  1140. /// 更新叠加商品数据集合
  1141. /// </summary>
  1142. /// <param name="comparisonSymbol">The comparison symbol.</param>
  1143. /// <param name="points">The points.</param>
  1144. public void UpdateComparisonDataPoints(string comparisonSymbol, IEnumerable<ILineDataPoint> points)
  1145. {
  1146. Dispatcher.BeginInvoke(new Action(() =>
  1147. {
  1148. if (_comparisonGoodsList == null || !_comparisonGoodsList.Any()) return;
  1149. var comparisonGoods = _comparisonGoodsList.FirstOrDefault(z => z.Symbol == comparisonSymbol);
  1150. if (comparisonGoods == null) return;
  1151. //集合都要按时间顺排序,才能画图
  1152. var dataPoints = points ?? new List<ILineDataPoint>();
  1153. var oldDataPoints = ComparisionDictionary[comparisonSymbol] ?? new List<ILineDataPoint>();
  1154. var totalDataPoints =
  1155. dataPoints.Union(oldDataPoints, new DataPointComparer<ILineDataPoint>())
  1156. .OrderBy(z => z.Date)
  1157. .ToList();
  1158. //目前只能一个商品叠加,更新的时候清除所有
  1159. var list = new List<ComparisonGoods>();
  1160. list.AddRange(_comparisonGoodsList);
  1161. #region 商品叠加
  1162. ClearComparison();
  1163. _comparisonGoodsList = list;
  1164. ComparisionDictionary.Add(comparisonSymbol, totalDataPoints);
  1165. var disPlayFormula = new DisplayFormula(new FormulaModel("main"), new ChartDataSet(totalDataPoints), this, comparisonGoods);
  1166. var scaleLayer = GetMainScaleLayer();
  1167. if (scaleLayer == null) return;
  1168. disPlayFormula.PlotBases.Clear();
  1169. scaleLayer.ComparisonFormulas.Clear();
  1170. foreach (var indicator in disPlayFormula.FormulaModel.IndicatorList)
  1171. {
  1172. var comparisonPlot = new ComparisonPlot(indicator, this, comparisonGoods)
  1173. {
  1174. IsActive = true,
  1175. ScaleLayer = scaleLayer
  1176. };
  1177. scaleLayer.AllPlots.Add(comparisonPlot);
  1178. disPlayFormula.PlotBases.Add(comparisonPlot); //添加到公式中
  1179. }
  1180. scaleLayer.ComparisonFormulas.Add(disPlayFormula);
  1181. disPlayFormula.ScaleLayer = scaleLayer;
  1182. #endregion
  1183. Refresh(true);
  1184. }), DispatcherPriority.Background);
  1185. }
  1186. /// <summary>
  1187. /// 导出数据到Excel
  1188. /// </summary>
  1189. public void ExportToExcel()
  1190. {
  1191. //正在下载数据则返回
  1192. if (_exportBusyIndicator.IsBusy) return;
  1193. if (this.CurrentGoods == null) return;
  1194. if (this.DataSet == null || this.DataSet.DataPoints == null || !this.DataSet.DataPoints.Any()) return;
  1195. var width = this.ActualWidth;
  1196. this.Width = width;
  1197. var displayName = string.IsNullOrWhiteSpace(this.CurrentGoods.DisplayName)
  1198. ? this.CurrentGoods.GoodsCode
  1199. : this.CurrentGoods.DisplayName;
  1200. var exportHelper = new ExportHelper();
  1201. exportHelper.OnStartExport += OnStartExport;
  1202. exportHelper.OnExportCompleted += OnExportCompleted;
  1203. exportHelper.ExportToExcel(displayName, CycleType, this.DataSet.DataPoints, this.CurrentGoods.MinUnit);
  1204. }
  1205. /// <summary>
  1206. /// 数据导出开始
  1207. /// </summary>
  1208. /// <param name="sender">The sender.</param>
  1209. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  1210. private void OnStartExport(object sender, EventArgs e)
  1211. {
  1212. //要在SaveFileDiaolog关闭后再启动BusyIndicator,否则界面会有SaveFileDiaolog窗口的黑影
  1213. _exportBusyIndicator.IsBusy = true;
  1214. }
  1215. /// <summary>
  1216. /// 数据导出完成
  1217. /// </summary>
  1218. /// <param name="message">The message.</param>
  1219. /// <param name="caption">The caption.</param>
  1220. private void OnExportCompleted(string message, string caption)
  1221. {
  1222. _exportBusyIndicator.IsBusy = false;
  1223. //message为空,只不弹出信息
  1224. if (string.IsNullOrWhiteSpace(message)) return;
  1225. System.Windows.MessageBox.Show(Application.Current.MainWindow, message, caption);
  1226. }
  1227. #endregion Public Methods
  1228. #region Internal Methods
  1229. /// <summary>
  1230. /// sts.
  1231. /// </summary>
  1232. /// <param name="parent">The parent.</param>
  1233. /// <param name="indicatorModel">The indicator model.</param>
  1234. /// <param name="AllowDefaults">if set to <c>true</c> [allow defaults].</param>
  1235. /// <returns>PlotBase.</returns>
  1236. internal PlotBase AddChildPlot(LinePlot parent, IndicatorModel indicatorModel, bool AllowDefaults)
  1237. {
  1238. ScaleLayer scaleLayer = parent.ScaleLayer;
  1239. bool flag = scaleLayer != null;
  1240. PlotBase result = null;
  1241. if (flag)
  1242. {
  1243. //bool flag2 = aCalc is ILineCalculation;
  1244. //if (flag2)
  1245. //{
  1246. var linePlot = new LinePlot(indicatorModel, parent.GetChart());
  1247. //flag2 = (aCalc is calcVolumeAtPrice);
  1248. //if (flag2)
  1249. //{
  1250. // linePlot.PlotOpacity = ((calcVolumeAtPrice)aCalc).DefaultOpacity();
  1251. //}
  1252. linePlot.InitAsNew();
  1253. //if (AllowDefaults)
  1254. //{
  1255. // WebChart_5948(linePlot);
  1256. //}
  1257. // scaleLayer.AddPlot(linePlot);
  1258. linePlot.ScaleLayer = scaleLayer;
  1259. result = linePlot;
  1260. //}
  1261. //else
  1262. //{
  1263. //flag2 = (aCalc is IChannelCalc);
  1264. //if (flag2)
  1265. //{
  1266. // ChannelPlot channelPlot = new ChannelPlot(parent.LinePlot_4866(), (IChannelCalc)aCalc);
  1267. // channelPlot.InitAsNew();
  1268. // if (AllowDefaults)
  1269. // {
  1270. // WebChart_5948(channelPlot);
  1271. // }
  1272. // scaleLayer.ScaleLayer_5530(channelPlot);
  1273. // channelPlot.ScaleLayer = scaleLayer;
  1274. // WebChart_2077 = true;
  1275. // result = channelPlot;
  1276. //}
  1277. //else
  1278. //{
  1279. // flag2 = (aCalc is ITwoLineCalc);
  1280. // if (flag2)
  1281. // {
  1282. // TwoLinePlot twoLinePlot = new TwoLinePlot(parent.LinePlot_4866(), (ITwoLineCalc)aCalc);
  1283. // twoLinePlot.InitAsNew();
  1284. // if (AllowDefaults)
  1285. // {
  1286. // WebChart_5948(twoLinePlot);
  1287. // }
  1288. // scaleLayer.ScaleLayer_5530(twoLinePlot);
  1289. // twoLinePlot.ScaleLayer = scaleLayer;
  1290. // WebChart_2077 = true;
  1291. // result = twoLinePlot;
  1292. // }
  1293. //}
  1294. //}
  1295. }
  1296. return result;
  1297. }
  1298. /// <summary>
  1299. /// 添加叠加商品
  1300. /// </summary>
  1301. public void AddComparisonFormula(ILineDataSet dataSet, ChartPane addToPane, GoodsInfo goodsInfo)
  1302. {
  1303. if (this.CycleType == CycleType.TimeSharing && this.timeSharingFormulaModel != null)
  1304. {
  1305. if (goodsInfo == null || goodsInfo.GoodsCode == this.CurrentGoods.GoodsCode ||
  1306. ComparisionTimeShareDic.ContainsKey(goodsInfo.GoodsCode)) return;
  1307. var dataList = OnLoadHistoryTimeSpaningCycleData(CurrentGoods, _timeSharingMaxPointCount, timeSharingFormulaModel.OpenTime,
  1308. timeSharingFormulaModel.CloseTime);
  1309. var diplayformula = DisplayFormula.TimeSharingLineFormula(this, goodsInfo, dataList);
  1310. ComparisionTimeShareDic[goodsInfo.GoodsCode] = dataList;
  1311. CreateComparisonPlot(diplayformula, this.timeSharingFormulaModel.ScaleLayer);
  1312. }
  1313. else
  1314. {
  1315. var disPlayFormula = new DisplayFormula(new FormulaModel("main"), dataSet, this, goodsInfo);
  1316. ChartPane pane = addToPane;
  1317. bool flag = addToPane == null;
  1318. if (flag)
  1319. {
  1320. pane = new ChartPane(this);
  1321. }
  1322. flag = (addToPane == null);
  1323. if (flag)
  1324. {
  1325. //取最大grid高度做*
  1326. var stars = _chartPanesGrid.RowDefinitions.Count > 0 ? _chartPanesGrid.RowDefinitions.Max(z => z.Height.Value) : 1.0;
  1327. AddChartPane(pane, stars);
  1328. }
  1329. var scaleLayer = GetMainScaleLayer();
  1330. if (scaleLayer == null) return;
  1331. disPlayFormula.PlotBases.Clear();
  1332. scaleLayer.ComparisonFormulas.Clear();
  1333. foreach (var indicator in disPlayFormula.FormulaModel.IndicatorList)
  1334. {
  1335. var comparisonPlot = new ComparisonPlot(indicator, this, goodsInfo);
  1336. comparisonPlot.IsActive = true;
  1337. comparisonPlot.ScaleLayer = scaleLayer;
  1338. scaleLayer.AllPlots.Add(comparisonPlot);
  1339. disPlayFormula.PlotBases.Add(comparisonPlot); //添加到公式中
  1340. }
  1341. scaleLayer.ComparisonFormulas.Add(disPlayFormula);
  1342. disPlayFormula.ScaleLayer = scaleLayer;
  1343. }
  1344. }
  1345. /// <summary>
  1346. /// 移除叠加商品
  1347. /// </summary>
  1348. public void RemoveComparisonForLuma()
  1349. {
  1350. }
  1351. /// <summary>
  1352. /// 添加图例
  1353. /// </summary>
  1354. /// <param name="formula"></param>
  1355. /// <param name="childTarget"></param>
  1356. internal void AddPlot(DisplayFormula formula, PlotBase childTarget)
  1357. {
  1358. bool flag = formula == null;
  1359. if (!flag)
  1360. {
  1361. flag = (childTarget != null && childTarget.ScaleLayer != null &&
  1362. childTarget.ScaleLayer.GetChartPane() != null);
  1363. if (flag)
  1364. {
  1365. var addToPane = childTarget.ScaleLayer.GetChartPane();
  1366. AddFormula(formula, true, addToPane);
  1367. }
  1368. else
  1369. {
  1370. AddFormula(formula, true);
  1371. }
  1372. }
  1373. }
  1374. internal List<ChartPane> AllPanes()
  1375. {
  1376. var list = new List<ChartPane>();
  1377. if (_chartPanesGrid == null || _chartPanesGrid.Children == null || _chartPanesGrid.Children.Count == 0)
  1378. return list;
  1379. list.AddRange(_chartPanesGrid.Children.OfType<ChartPane>());
  1380. return list;
  1381. }
  1382. /// <summary>
  1383. /// 下载前N条数据(N = MaxLoadRecordCount)
  1384. /// </summary>
  1385. internal void DownloadNextDataBlock()
  1386. {
  1387. var goods = CurrentGoods;
  1388. var exchangeCode = CurrentGoods.ExchangeCode;
  1389. var goodsCode = CurrentGoods.GoodsCode;
  1390. var cycleType = CycleType;
  1391. var timeSpan = CurrentTimeFrame;
  1392. var maxLoadRecordCount = MaxLoadRecordCount;
  1393. Task.Factory.StartNew(new Action(() =>
  1394. {
  1395. //数据集有值,取最小时间, 无值,取当前时间
  1396. var maxTime = (DataSet != null && DataSet.DataPoints != null && DataSet.DataPoints.Any())
  1397. ? DataSet.DataPoints.Min(z => z.Date)
  1398. : DateTime.Now;
  1399. //减去周期ticks,作为当前下载最大值
  1400. maxTime = maxTime.AddTicks(-timeSpan.Ticks);
  1401. if (DataSet == null)
  1402. {
  1403. DataSet = new ChartDataSet();
  1404. }
  1405. List<ILineDataPoint> newData = null;
  1406. var cycleTypeHandler = LoadHistoryCycleDataByCycleType;
  1407. if (cycleTypeHandler != null)
  1408. {
  1409. newData = cycleTypeHandler(exchangeCode, goodsCode, cycleType,
  1410. timeSpan, DateTime.MinValue, maxTime, maxLoadRecordCount);
  1411. }
  1412. var dataPoints = new List<ILineDataPoint>();
  1413. if (newData != null && newData.Any())
  1414. {
  1415. dataPoints.AddRange(CaclulateCycleDataIncrease(newData));
  1416. if (DataSet.DataPoints != null && dataPoints.Any())
  1417. {
  1418. //只更新前面数据块第一条数据涨幅
  1419. var firstOld = DataSet.DataPoints[0] as IOHLCDataPoint;
  1420. var lastNew = newData[newData.Count - 1] as IOHLCDataPoint;
  1421. if (firstOld != null && lastNew != null)
  1422. {
  1423. firstOld.Increase = lastNew.Close.Equals(0)
  1424. ? 0f
  1425. : (firstOld.Close - lastNew.Close) / lastNew.Close;
  1426. firstOld.IncreaseValue = lastNew.Close.Equals(0) ? 0f : (firstOld.Close - lastNew.Close);
  1427. }
  1428. dataPoints.AddRange(DataSet.DataPoints);
  1429. }
  1430. DataSet.DataPoints = dataPoints.OrderBy(z => z.Date).ToList();
  1431. Dispatcher.BeginInvoke(new Action(() =>
  1432. {
  1433. var allDateScaler = (AllDateScaler)DateScaler;
  1434. allDateScaler.SetDates(DataSet.DataPoints, timeSpan, false);
  1435. //前一数据块的最后时间做为当前的结束时间
  1436. allDateScaler.DesiredEndDate = maxTime.AddTicks(timeSpan.Ticks);
  1437. Refresh(true);
  1438. //刷新叠加商品
  1439. RefreshComparsionGoods();
  1440. }), DispatcherPriority.Background);
  1441. }
  1442. }));
  1443. }
  1444. /// <summary>
  1445. /// 若画图则隐藏十字光标
  1446. /// </summary>
  1447. internal void DrawingToolExecute()
  1448. {
  1449. if (PointerType != PointerType.Drawing)
  1450. {
  1451. if (_crossHairGrid.Visibility == Visibility.Collapsed)
  1452. {
  1453. _crossHairGrid.Visibility = Visibility.Visible;
  1454. }
  1455. //TODO:更改所有控件的背景颜色
  1456. var panes = AllPanes();
  1457. foreach (var chartPane in panes)
  1458. {
  1459. chartPane.OnDrawing();
  1460. }
  1461. }
  1462. else
  1463. {
  1464. if (_crossHairGrid.Visibility == Visibility.Visible)
  1465. {
  1466. _crossHairGrid.Visibility = Visibility.Collapsed;
  1467. }
  1468. }
  1469. }
  1470. internal double GetChartPanelOffset()
  1471. {
  1472. return _chartPanesGrid.ColumnDefinitions[0].ActualWidth;
  1473. }
  1474. /// <summary>
  1475. /// 得到的图表窗格的宽度.
  1476. /// </summary>
  1477. /// <returns>System.Double.</returns>
  1478. internal double GetChartPaneWidth()
  1479. {
  1480. //0:左纵坐标, 1:Chart, 2:右纵坐标
  1481. return Math.Round(_chartPanesGrid.ColumnDefinitions[1].ActualWidth) - RightBuffer;
  1482. }
  1483. /// <summary>
  1484. /// 获取当前已打开的主图LinePlot
  1485. /// </summary>
  1486. /// <returns>PlotBase.</returns>
  1487. internal T GetPlot<T>(string formulaName) where T : PlotBase
  1488. {
  1489. var panes = AllPanes();
  1490. if (panes == null || !panes.Any()) return default(T);
  1491. var name = formulaName.ToLower();
  1492. foreach (var chartPane in panes)
  1493. {
  1494. var scaleLayers = chartPane.AllScaleLayers;
  1495. if (scaleLayers == null || !scaleLayers.Any()) continue;
  1496. foreach (var scaleLayer in scaleLayers)
  1497. {
  1498. var plots = scaleLayer.AllPlots;
  1499. if (plots == null || !plots.Any()) continue;
  1500. foreach (var plot in plots)
  1501. {
  1502. if (plot != null && (plot is T) && plot.Indicator.ParentFormula.FormulaName.ToLower() == name) return plot as T;
  1503. }
  1504. }
  1505. }
  1506. return null;
  1507. }
  1508. private ScaleLayer GetMainScaleLayer()
  1509. {
  1510. var panes = AllPanes();
  1511. if (panes == null || !panes.Any()) return null;
  1512. foreach (var chartPane in panes)
  1513. {
  1514. var scaleLayers = chartPane.AllScaleLayers;
  1515. if (scaleLayers == null || !scaleLayers.Any()) continue;
  1516. foreach (var scaleLayer in scaleLayers)
  1517. {
  1518. var formulas = scaleLayer.AllDisplayFormula;
  1519. if (formulas == null || !formulas.Any()) continue;
  1520. foreach (var formula in formulas)
  1521. {
  1522. if (formula != null && formula.FormulaModel.IsMain)
  1523. {
  1524. return scaleLayer;
  1525. }
  1526. }
  1527. }
  1528. }
  1529. return null;
  1530. }
  1531. /// <summary>
  1532. /// 初始化画图工具栏
  1533. /// </summary>
  1534. internal void InitializeDrawingTool()
  1535. {
  1536. if (_drawingToolStackPanel == null) return;
  1537. if (_drawingToolbar == null) _drawingToolbar = new ChartDrawingToolbar(this);
  1538. if (_chartToolbarExpander == null) _chartToolbarExpander = new ChartToolbarExpander(_drawingToolbar);
  1539. //隐藏画线工具展开按钮
  1540. _chartToolbarExpander.Visibility = Visibility.Hidden;
  1541. switch (DrawingToolPosition)
  1542. {
  1543. case DrawingToolPositionType.Left:
  1544. _drawingToolStackPanel.SetValue(Grid.RowProperty, 0);
  1545. _drawingToolStackPanel.SetValue(Grid.RowSpanProperty, 4);
  1546. _drawingToolStackPanel.SetValue(Grid.ColumnProperty, 0);
  1547. _drawingToolStackPanel.Orientation = Orientation.Vertical;
  1548. _drawingToolStackPanel.HorizontalAlignment = HorizontalAlignment.Left;
  1549. _drawingToolbar.UpdateLayout(Orientation.Vertical);
  1550. break;
  1551. case DrawingToolPositionType.Right:
  1552. _drawingToolStackPanel.SetValue(Grid.RowProperty, 0);
  1553. _drawingToolStackPanel.SetValue(Grid.RowSpanProperty, 4);
  1554. _drawingToolStackPanel.SetValue(Grid.ColumnProperty, 2);
  1555. _drawingToolStackPanel.Orientation = Orientation.Vertical;
  1556. _drawingToolStackPanel.HorizontalAlignment = HorizontalAlignment.Right;
  1557. _drawingToolbar.UpdateLayout(Orientation.Vertical);
  1558. break;
  1559. case DrawingToolPositionType.Top:
  1560. _drawingToolStackPanel.SetValue(Grid.RowProperty, 1);
  1561. _drawingToolStackPanel.SetValue(Grid.RowSpanProperty, 1);
  1562. _drawingToolStackPanel.SetValue(Grid.ColumnProperty, 1);
  1563. _drawingToolStackPanel.Orientation = Orientation.Horizontal;
  1564. _drawingToolStackPanel.HorizontalAlignment = HorizontalAlignment.Stretch;
  1565. _chartToolbarExpander.VerticalAlignment = VerticalAlignment.Top;
  1566. _drawingToolbar.UpdateLayout(Orientation.Horizontal);
  1567. break;
  1568. case DrawingToolPositionType.Bottom:
  1569. _drawingToolStackPanel.SetValue(Grid.RowProperty, 3);
  1570. _drawingToolStackPanel.SetValue(Grid.RowSpanProperty, 1);
  1571. _drawingToolStackPanel.SetValue(Grid.ColumnProperty, 1);
  1572. _drawingToolStackPanel.Orientation = Orientation.Horizontal;
  1573. _drawingToolStackPanel.HorizontalAlignment = HorizontalAlignment.Stretch;
  1574. _chartToolbarExpander.VerticalAlignment = VerticalAlignment.Top;
  1575. _drawingToolbar.UpdateLayout(Orientation.Horizontal);
  1576. break;
  1577. }
  1578. if (!_drawingToolStackPanel.Children.Contains(_chartToolbarExpander))
  1579. _drawingToolStackPanel.Children.Add(_chartToolbarExpander);
  1580. if (!_drawingToolStackPanel.Children.Contains(_drawingToolbar))
  1581. _drawingToolStackPanel.Children.Add(_drawingToolbar);
  1582. }
  1583. private int i = 0;
  1584. private double currentX = 0;
  1585. private double currentY = 0;
  1586. /// <summary>
  1587. /// 刷新图表
  1588. /// </summary>
  1589. /// <param name="isUpdataSet">if set to <c>true</c> [is updata set].</param>
  1590. internal void Refresh(bool isUpdataSet = false)
  1591. {
  1592. lock (_lockObject)
  1593. {
  1594. if (!LockPainting)
  1595. {
  1596. Dispatcher.BeginInvoke(new Action<bool>(DrawingChart), DispatcherPriority.Background, isUpdataSet);
  1597. }
  1598. }
  1599. }
  1600. internal void RemoveChartPane(ChartPane aPane)
  1601. {
  1602. int num = Conversions.ToInteger(aPane.GetValue(Grid.RowProperty));
  1603. _chartPanesGrid.Children.Remove(aPane);
  1604. _chartPanes.Remove(aPane);
  1605. if (aPane.RightScaleControl != null)
  1606. {
  1607. _chartPanesGrid.Children.Remove(aPane.RightScaleControl);
  1608. }
  1609. if (aPane.LeftScaleControl != null)
  1610. {
  1611. _chartPanesGrid.Children.Remove(aPane.LeftScaleControl);
  1612. }
  1613. _chartPanesGrid.RowDefinitions.RemoveAt(num);
  1614. UIElement currentSplitter = null;
  1615. foreach (var uiElement in _chartPanesGrid.Children)
  1616. {
  1617. var current = uiElement as UIElement;
  1618. if (current != null &&
  1619. (current is ChartPane || current is ValueScaleDisplayCollection || current is GridSplitter))
  1620. {
  1621. int rowIndex = Conversions.ToInteger(current.GetValue(Grid.RowProperty));
  1622. if (rowIndex < 1) continue;
  1623. if (rowIndex > num)
  1624. {
  1625. current.SetValue(Grid.RowProperty, rowIndex - 2 < 0 ? 0 : rowIndex - 2);
  1626. }
  1627. else if (rowIndex == num - 1 && current is GridSplitter)
  1628. {
  1629. currentSplitter = current;
  1630. }
  1631. }
  1632. }
  1633. if (currentSplitter != null)
  1634. {
  1635. _chartPanesGrid.Children.Remove(currentSplitter);
  1636. _chartPanesGrid.RowDefinitions.RemoveAt(num - 1);
  1637. }
  1638. aPane.RemoveAllScaleLayers();
  1639. Refresh(true);
  1640. }
  1641. internal void SetCursorImagePos(bool showIt, MouseEventArgs e)
  1642. {
  1643. if (showIt)
  1644. {
  1645. switch (PointerType)
  1646. {
  1647. case PointerType.CursorWithData:
  1648. _crossHairGrid.Cursor = Cursors.Arrow;
  1649. _chartPanesGrid.Cursor = Cursors.Arrow;
  1650. _toolImage.Source = null;
  1651. break;
  1652. case PointerType.Cursor:
  1653. _crossHairGrid.Cursor = Cursors.Arrow;
  1654. _chartPanesGrid.Cursor = Cursors.Arrow;
  1655. _toolImage.Source = null;
  1656. break;
  1657. case PointerType.Pan:
  1658. _crossHairGrid.Cursor = Cursors.Hand;
  1659. _chartPanesGrid.Cursor = Cursors.Hand;
  1660. _toolImage.Source = null;
  1661. break;
  1662. case PointerType.Erase:
  1663. _crossHairGrid.Cursor = Cursors.Arrow;
  1664. _toolImage.Source = ImageHelper.GetImage("Eraser.png");
  1665. break;
  1666. case PointerType.Drawing:
  1667. _toolImage.Source = CurrentDrawingTool == null ? null : CurrentDrawingTool.Icon();
  1668. break;
  1669. }
  1670. if (_toolImage.Source == null)
  1671. {
  1672. CleanQuoteBoard();
  1673. }
  1674. else
  1675. {
  1676. if (_popup == null)
  1677. {
  1678. _popup = new Popup
  1679. {
  1680. Child = _toolImage,
  1681. Placement = PlacementMode.Relative,
  1682. AllowsTransparency = true
  1683. };
  1684. _chartPanesGrid.Children.Add(_popup);
  1685. _popup.IsOpen = true;
  1686. }
  1687. }
  1688. }
  1689. else
  1690. {
  1691. CleanQuoteBoard();
  1692. }
  1693. //}
  1694. if (showIt && e != null && _toolImage.Source != null)
  1695. {
  1696. Point position = e.GetPosition(_chartPanesGrid);
  1697. _popup.HorizontalOffset = (position.X + 8.0);
  1698. _popup.VerticalOffset = (position.Y - 8.0);
  1699. }
  1700. else
  1701. {
  1702. if (!showIt)
  1703. {
  1704. CleanQuoteBoard();
  1705. }
  1706. }
  1707. }
  1708. /// <summary>
  1709. /// 设置当前画图工具为null
  1710. /// </summary>
  1711. internal void SetDrawingTool()
  1712. {
  1713. _oldDrawingTool = CurrentDrawingTool;
  1714. CurrentDrawingTool = null;
  1715. _pointerType = _oldPointerType;
  1716. DrawingToolExecute();
  1717. var panes = AllPanes();
  1718. foreach (var current in panes)
  1719. {
  1720. current.OnDrawing();
  1721. }
  1722. _drawingToolbar.ButtonPress();
  1723. }
  1724. /// <summary>
  1725. /// 显示/隐藏Download More Button
  1726. /// </summary>
  1727. /// <param name="isShow">if set to <c>true</c> [is show].</param>
  1728. internal void ShowDownloadButton(bool isShow)
  1729. {
  1730. if (isShow)
  1731. {
  1732. _downloadMoreButton.Width = DateChartSlider.Height;
  1733. _downloadMoreButton.Height = DateChartSlider.Height;
  1734. _downloadMoreButton.Visibility = Visibility.Visible;
  1735. }
  1736. else
  1737. {
  1738. _downloadMoreButton.Visibility = Visibility.Collapsed;
  1739. }
  1740. }
  1741. #endregion Internal Methods
  1742. #region Protected Methods
  1743. /// <summary>
  1744. /// 鼠标滚轮事件
  1745. /// </summary>
  1746. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Input.MouseWheelEventArgs" />。</param>
  1747. protected override void OnMouseWheel(MouseWheelEventArgs e)
  1748. {
  1749. e.Handled = true;
  1750. var factor = 1;
  1751. if ((Keyboard.Modifiers & AcceleratedWheelModifierKeys) == AcceleratedWheelModifierKeys)
  1752. {
  1753. factor = AcceleratedWheelFactor;
  1754. }
  1755. int num = DateScaler.NumBars + (factor) * (int)Math.Round((double)e.Delta / 120.0);
  1756. num = num < 5 ? 5 : num;
  1757. var scalerCount = DateScaler.Count();
  1758. num = num > scalerCount ? scalerCount : num;
  1759. DateScaler.NumBars = num;
  1760. Refresh();
  1761. }
  1762. #endregion Protected Methods
  1763. #region Private Static Methods
  1764. private static void ChartFontSizeProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
  1765. {
  1766. var chart = d as Chart;
  1767. if (chart == null) return;
  1768. UpdateDrawingTool(chart);
  1769. }
  1770. private static void ChartLanguageProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
  1771. {
  1772. ChartLanguageType language;
  1773. if (!Enum.TryParse(e.NewValue.ToString(), out language))
  1774. {
  1775. language = ChartLanguageType.SimplifiedChinese;
  1776. }
  1777. switch (language)
  1778. {
  1779. case ChartLanguageType.SimplifiedChinese:
  1780. LanguageManager.SetLanguage("zh-CN");
  1781. break;
  1782. case ChartLanguageType.TraditionalChinese:
  1783. LanguageManager.SetLanguage("zh-TW");
  1784. break;
  1785. case ChartLanguageType.English:
  1786. LanguageManager.SetLanguage("en-US");
  1787. break;
  1788. default:
  1789. LanguageManager.SetLanguage("zh-CN");
  1790. break;
  1791. }
  1792. var chart = d as Chart;
  1793. if (chart == null) return;
  1794. UpdateDrawingTool(chart);
  1795. }
  1796. private static void ChartLineTypePlotProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
  1797. {
  1798. var chart = (Chart)d;
  1799. //更新plot type
  1800. var newValue = (LinePlotType)e.NewValue;
  1801. var pricePlot = chart.GetPlot<LinePlot>("main");
  1802. if (pricePlot != null && pricePlot.Indicator != null) pricePlot.Indicator.LinePlotType = newValue;
  1803. var comparisonPlot = chart.GetPlot<ComparisonPlot>("main");
  1804. if (comparisonPlot != null && comparisonPlot.Indicator != null) comparisonPlot.Indicator.LinePlotType = newValue;
  1805. chart.Refresh();
  1806. }
  1807. private static void ChartCycleBarVisibity(DependencyObject d, DependencyPropertyChangedEventArgs e)
  1808. {
  1809. var chart = (Chart)d;
  1810. if (chart.TimeSpanPicker != null)
  1811. {
  1812. chart.TimeSpanPicker.Visibility = (Visibility)e.NewValue;
  1813. }
  1814. }
  1815. /// <summary>
  1816. /// 图的最小最大记录改变.
  1817. /// </summary>
  1818. /// <param name="d">The d.</param>
  1819. /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
  1820. private static void ChartMinMaxRecordProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
  1821. {
  1822. var chart = (Chart)d;
  1823. var numBars = chart.DateScaler.NumBars;
  1824. var oldNum = (int)e.OldValue;
  1825. var newNum = (int)e.NewValue;
  1826. //numBars为矩形柱间距,矩形柱数量实际要-1
  1827. if (numBars == 4 || numBars == oldNum - 1)
  1828. {
  1829. chart.DateScaler.NumBars = newNum - 1;
  1830. chart.Refresh();
  1831. }
  1832. }
  1833. /// <summary>
  1834. /// Charts the property_ changed.
  1835. /// </summary>
  1836. /// <param name="d">The d.</param>
  1837. /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
  1838. private static void ChartProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
  1839. {
  1840. var chart = (Chart)d;
  1841. if (chart == null) return;
  1842. if ((e.Property == FillComparisonCandleProperty && chart.LinePlotType == LinePlotType.Candlestick &&
  1843. chart.ComparisionDictionary.Any())
  1844. || e.Property != FillComparisonCandleProperty)
  1845. {
  1846. chart.Refresh();
  1847. }
  1848. }
  1849. /// <summary>
  1850. /// Charts the record count property_ changed.
  1851. /// </summary>
  1852. /// <param name="d">The d.</param>
  1853. /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
  1854. private static void ChartRecordCountProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
  1855. {
  1856. var chart = (Chart)d;
  1857. //numBars为矩形柱间距,矩形柱数量实际要-1
  1858. chart.DateScaler.NumBars = (int)e.NewValue - 1;
  1859. chart.Refresh();
  1860. }
  1861. /// <summary>
  1862. /// Charts the time span property_ changed.
  1863. /// </summary>
  1864. /// <param name="d">The d.</param>
  1865. /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
  1866. private static void ChartTimeSpanProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
  1867. {
  1868. var chart = (Chart)d;
  1869. if (chart == null) return;
  1870. if (chart.ChartTimeSpan != default(TimeSpan))
  1871. {
  1872. if (chart.CycleType == CycleType.Custom)
  1873. {
  1874. //直接调用
  1875. chart.CycleTypePropertyChanged(false);
  1876. }
  1877. else
  1878. {
  1879. //通过设置CycleType触发数据下载
  1880. chart.CycleType = CycleType.Custom;
  1881. }
  1882. }
  1883. }
  1884. /// <summary>
  1885. /// Currents the goods property_ changed.
  1886. /// </summary>
  1887. /// <param name="d">The d.</param>
  1888. /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
  1889. private static void CurrentGoodsProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
  1890. {
  1891. var chart = (Chart)d;
  1892. if (chart == null) return;
  1893. // chart.ClearComparison();
  1894. chart.OnCycleTypeChanged(true);
  1895. }
  1896. /// <summary>
  1897. /// 周期属性改变后回调函数
  1898. /// </summary>
  1899. /// <param name="d">The Dependency Object.</param>
  1900. /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
  1901. private static void ChartCycleTypeProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
  1902. {
  1903. var chart = (Chart)d;
  1904. if (chart == null) return;
  1905. chart.CycleTypePropertyChanged(CycleType.TimeSharing.Equals(e.OldValue) || CycleType.Tik.Equals(e.OldValue));
  1906. }
  1907. /// <summary>
  1908. /// 周期集合属性改变后回调函数
  1909. /// </summary>
  1910. /// <param name="d">The Dependency Object.</param>
  1911. /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
  1912. private static void ChartCycleTypeItemsProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
  1913. {
  1914. var chart = (Chart)d;
  1915. if (chart == null || chart.TimeSpanPicker == null) return;
  1916. }
  1917. /// <summary>
  1918. /// Tik属性改变后回调函数
  1919. /// </summary>
  1920. private static void ChartTikProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
  1921. {
  1922. var chart = (Chart)d;
  1923. if (chart == null) return;
  1924. if (chart.CurrentTik != null && chart.DataSet != null && chart.DataSet.DataPoints != null && chart.DataSet.DataPoints.Count() > 0)
  1925. {
  1926. var lastTik = chart.DataSet.DataPoints.Last() as BarDataPoint;
  1927. var date = default(DateTime);
  1928. if (chart.IsNotInCycle(chart.CurrentTik.Date, ref date))
  1929. {
  1930. var item = new BarDataPoint(date, chart.CurrentTik.Open, chart.CurrentTik.High, chart.CurrentTik.Low, chart.CurrentTik.Close, chart.CurrentTik.Volume,
  1931. chart.CurrentTik.Turnover, chart.CurrentTik.HoldVolume);
  1932. chart.DataSet.DataPoints.Add(item);
  1933. chart.SetDatesAndRefresh(true,false);
  1934. }
  1935. else
  1936. {
  1937. var barPointTmp = chart.DataSet.DataPoints.Last() as BarDataPoint;
  1938. barPointTmp.Close = chart.CurrentTik.Close;
  1939. barPointTmp.High = chart.CurrentTik.Value > lastTik.High ? chart.CurrentTik.Value : lastTik.High;
  1940. barPointTmp.Low = chart.CurrentTik.Value < lastTik.Low ? chart.CurrentTik.Value : lastTik.Low;
  1941. barPointTmp.HoldVolume = chart.CurrentTik.HoldVolume;
  1942. barPointTmp.Turnover = chart.CurrentTik.Turnover;
  1943. barPointTmp.Volume += chart.CurrentTik.Volume;
  1944. chart.SetDatesAndRefresh(true, false);
  1945. }
  1946. }
  1947. }
  1948. /// <summary>
  1949. /// Drawings the tool position property_ changed.
  1950. /// </summary>
  1951. /// <param name="d">The d.</param>
  1952. /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
  1953. private static void DrawingToolPositionProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
  1954. {
  1955. var chart = (Chart)d;
  1956. chart.InitializeDrawingTool();
  1957. }
  1958. /// <summary>
  1959. /// Currents the drawing tool type_ changed.
  1960. /// </summary>
  1961. /// <param name="d">The d.</param>
  1962. /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
  1963. private static void DrawingToolType_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
  1964. {
  1965. var chart = (Chart)d;
  1966. if (chart == null || chart._drawingToolbar == null) return;
  1967. var button = chart._drawingToolbar.GetToolbarButton(chart.DrawingToolType);
  1968. if (button == null) return;
  1969. button.ButtonClick();
  1970. }
  1971. /// <summary>
  1972. /// Updates the drawing tool.
  1973. /// </summary>
  1974. /// <param name="chart">The chart.</param>
  1975. private static void UpdateDrawingTool(Chart chart)
  1976. {
  1977. if (chart == null) return;
  1978. //更新画图工具
  1979. if (chart._drawingToolStackPanel != null)
  1980. {
  1981. chart._drawingToolStackPanel.Children.Clear();
  1982. chart._drawingToolbar = new ChartDrawingToolbar(chart);
  1983. chart._chartToolbarExpander = new ChartToolbarExpander(chart._drawingToolbar);
  1984. chart.InitializeDrawingTool();
  1985. }
  1986. //更新图表
  1987. chart.Refresh();
  1988. }
  1989. #endregion Private Static Methods
  1990. #region Private Methods
  1991. /// <summary>
  1992. /// 刷新叠加商品
  1993. /// </summary>
  1994. private void RefreshComparsionGoods()
  1995. {
  1996. if (_comparisonGoodsList != null && _comparisonGoodsList.Any())
  1997. {
  1998. var list = new List<ComparisonGoods>();
  1999. list.AddRange(_comparisonGoodsList);
  2000. SetComparisonGoodsList(list);
  2001. }
  2002. }
  2003. /// <summary>
  2004. /// 获取主图所在的图表容器
  2005. /// </summary>
  2006. /// <returns></returns>
  2007. private ChartPane GetMainChartPane()
  2008. {
  2009. foreach (var chartPane in _chartPanes)
  2010. {
  2011. if (IsMainChartPane(chartPane)) return chartPane;
  2012. }
  2013. return null;
  2014. }
  2015. /// <summary>
  2016. /// 判断是否是主图容器
  2017. /// </summary>
  2018. /// <param name="chartPane"></param>
  2019. /// <returns></returns>
  2020. private bool IsMainChartPane(ChartPane chartPane)
  2021. {
  2022. foreach (var scaleLayer in chartPane.AllScaleLayers)
  2023. {
  2024. foreach (var displayFormula in scaleLayer.AllDisplayFormula)
  2025. {
  2026. if (displayFormula.FormulaModel != null && displayFormula.FormulaModel.IsMain)
  2027. {
  2028. return true;
  2029. }
  2030. }
  2031. }
  2032. return false;
  2033. }
  2034. /// <summary>
  2035. /// 添加pane(图例)
  2036. /// </summary>
  2037. /// <param name="p"></param>
  2038. /// <param name="stars"></param>
  2039. /// <returns></returns>
  2040. private ChartPane AddChartPane(ChartPane p, double stars, bool isMain = false)
  2041. {
  2042. int count = _chartPanesGrid.RowDefinitions.Count;
  2043. if (count > 0)
  2044. {
  2045. AddRowDefinition(count, 1, GridUnitType.Auto);
  2046. var splitter = new GridSplitter();
  2047. splitter.IsTabStop = (false);
  2048. splitter.Background = (new SolidColorBrush(ColorHelper.DimGray));
  2049. splitter.SetValue(Grid.RowProperty, count);
  2050. splitter.SetValue(Grid.ColumnProperty, 1);
  2051. splitter.SetValue(Grid.ColumnSpanProperty, 2);
  2052. splitter.HorizontalAlignment = HorizontalAlignment.Stretch;
  2053. splitter.VerticalAlignment = VerticalAlignment.Center;
  2054. splitter.Height = (2.0);
  2055. splitter.Cursor = Cursors.SizeNS;
  2056. splitter.Margin = new Thickness(0, 2, 0, 2);
  2057. _chartPanesGrid.Children.Add(splitter);
  2058. //AddRowDefinition(count + 1, stars <= 0.0 ? 1.0 : stars, GridUnitType.Star);
  2059. AddRowDefinition(count + 1, isMain ? 2 : 1, GridUnitType.Star);
  2060. }
  2061. else
  2062. {
  2063. AddRowDefinition(count, isMain ? 2 : 1, GridUnitType.Star);
  2064. //AddRowDefinition(count, stars <= 0.0 ? 1.0 : stars, GridUnitType.Star);
  2065. }
  2066. p.SetValue(Grid.RowProperty, count > 0 ? count + 1 : 0);
  2067. p.SetValue(Grid.ColumnProperty, 1);
  2068. _chartPanesGrid.Children.Add(p);
  2069. _chartPanes.Add(p);
  2070. if (p.RightScaleControl != null)
  2071. {
  2072. p.RightScaleControl.SetValue(Grid.RowProperty, count > 0 ? count + 1 : 0);
  2073. p.RightScaleControl.SetValue(Grid.ColumnProperty, 2);
  2074. _chartPanesGrid.Children.Add(p.RightScaleControl);
  2075. }
  2076. if (p.LeftScaleControl != null)
  2077. {
  2078. p.LeftScaleControl.SetValue(Grid.RowProperty, count > 0 ? count + 1 : 0);
  2079. p.LeftScaleControl.SetValue(Grid.ColumnProperty, 0);
  2080. _chartPanesGrid.Children.Add(p.LeftScaleControl);
  2081. }
  2082. return p;
  2083. }
  2084. /// <summary>
  2085. /// 添加默认指标器-历史价格,成交量
  2086. /// </summary>
  2087. private void AddDefaultIndicators()
  2088. {
  2089. AddFormula(new DisplayFormula(new FormulaModel("Main"), DataSet, this), true);
  2090. var indecators = _defaoltIndecator.Split(',');
  2091. foreach (var indecator in indecators)
  2092. {
  2093. AddIndicatorByName(indecator, false);
  2094. }
  2095. var menuItem = FormulaHelper.CreateIndicatorMenu("", IndicatorItemClick);
  2096. menuItem.Header = "指标";
  2097. this.ContextMenu = new ContextMenu();
  2098. this.ContextMenu.Items.Add(menuItem);
  2099. var menuItem1 = new MenuItem() { Header = "编辑参数" };
  2100. this.ContextMenu.Items.Add(menuItem1);
  2101. menuItem1.Click += menuItem1_Click;
  2102. var MainItem = new MenuItem() { Header = "清除主图" };
  2103. this.ContextMenu.Items.Add(MainItem);
  2104. MainItem.Click += MainItem_Click;
  2105. //打开公式管理器
  2106. MainItem = new MenuItem() { Header = "公式管理" };
  2107. this.ContextMenu.Items.Add(MainItem);
  2108. MainItem.Click += (cs, ce) =>
  2109. {
  2110. OpenFormulEditer();
  2111. };
  2112. }
  2113. //清楚主图中的指标
  2114. void MainItem_Click(object sender, RoutedEventArgs e)
  2115. {
  2116. ClearMainPaneIndicator();
  2117. }
  2118. /// <summary>
  2119. /// 设置当前公式的参数
  2120. /// </summary>
  2121. /// <param name="sender"></param>
  2122. /// <param name="e"></param>
  2123. void menuItem1_Click(object sender, RoutedEventArgs e)
  2124. {
  2125. SetCurrectFormulaParam();
  2126. }
  2127. //选择指标菜单时触发的事件处理
  2128. private void IndicatorItemClick(object sender, RoutedEventArgs e)
  2129. {
  2130. var selectItem = sender as MenuItem;
  2131. if (selectItem != null && selectItem.Tag is ProgramModel)
  2132. {
  2133. var formulaModel = (selectItem.Tag as ProgramModel);
  2134. AddIndicatorByName(formulaModel.Name);
  2135. }
  2136. }
  2137. private void AddFormula(DisplayFormula displayFormula, bool allowDefaults)
  2138. {
  2139. AddFormula(displayFormula, allowDefaults, null, true);
  2140. }
  2141. public ChartPane AddFormula(DisplayFormula displayFormula, bool allowDefaults, ChartPane addToPane, bool isMain = false)
  2142. {
  2143. ChartPane pane = addToPane;
  2144. bool flag = addToPane == null;
  2145. if (flag)
  2146. {
  2147. pane = new ChartPane(this);
  2148. //取最大grid高度做*
  2149. var stars = _chartPanesGrid.RowDefinitions.Count > 0
  2150. ? _chartPanesGrid.RowDefinitions.Max(z => z.Height.Value)
  2151. : 1.0;
  2152. AddChartPane(pane, stars, isMain);
  2153. }
  2154. ScaleLayer scaleLayer = null;
  2155. if (flag)
  2156. {
  2157. scaleLayer = new ScaleLayer();
  2158. pane.AddScaleLayer(scaleLayer);
  2159. }
  2160. else
  2161. {
  2162. if (IsMainChartPane(pane))
  2163. {
  2164. scaleLayer = GetMainScaleLayer();
  2165. }
  2166. else
  2167. {
  2168. scaleLayer = new ScaleLayer();
  2169. pane.AddScaleLayer(scaleLayer);
  2170. }
  2171. }
  2172. displayFormula.PlotBases.Clear();
  2173. scaleLayer.AllDisplayFormula.Add(displayFormula);
  2174. displayFormula.ScaleLayer = scaleLayer;
  2175. pane.DisplayFristGrid();
  2176. return pane;
  2177. }
  2178. /// <summary>
  2179. /// 添加指标时如主图指标则添加到主图,副图指标添加或替换.副图不能添加超过允许最大数量
  2180. /// </summary>
  2181. /// <param name="displayFormula">指标公式</param>
  2182. /// <param name="allowDefaults"></param>
  2183. /// <param name="addToPane"></param>
  2184. /// <returns></returns>
  2185. public ChartPane AddFormulaFromPane(DisplayFormula displayFormula)
  2186. {
  2187. ChartPane pane = null;
  2188. var flag = displayFormula.FormulaModel.IsMainIndicator;
  2189. //var scaleLayer = new ScaleLayer();
  2190. ScaleLayer scaleLayer = new ScaleLayer();
  2191. if (flag)
  2192. {
  2193. pane = GetMainChartPane();
  2194. scaleLayer = GetMainScaleLayer();
  2195. }
  2196. else
  2197. {
  2198. var maxChart = MaxChartPaneCount > 0 && _chartPanes.Count >= MaxChartPaneCount;
  2199. if (!maxChart) //超过最大数量限制
  2200. {
  2201. pane = new ChartPane(this);
  2202. //取最大grid高度做*
  2203. var stars = _chartPanesGrid.RowDefinitions.Count > 0
  2204. ? _chartPanesGrid.RowDefinitions.Max(z => z.Height.Value)
  2205. : 1.0;
  2206. AddChartPane(pane, stars);
  2207. }
  2208. else
  2209. {
  2210. pane = _chartPanes[_chartPanes.Count - 1];
  2211. pane.RemoveAllScaleLayers();
  2212. }
  2213. scaleLayer = new ScaleLayer();
  2214. pane.AddScaleLayer(scaleLayer);
  2215. }
  2216. displayFormula.PlotBases.Clear();
  2217. scaleLayer.AllDisplayFormula.Add(displayFormula);
  2218. displayFormula.ScaleLayer = scaleLayer;
  2219. pane.DisplayFristGrid();
  2220. return pane;
  2221. }
  2222. /// <summary>
  2223. /// 在ChartCont指点位置插入一行
  2224. /// </summary>
  2225. /// <param name="index"></param>
  2226. /// <param name="value"></param>
  2227. /// <param name="type"></param>
  2228. private void AddRowDefinition(int index, double value, GridUnitType type)
  2229. {
  2230. var rowDefinition = new RowDefinition { Height = new GridLength(value, type) };
  2231. _chartPanesGrid.RowDefinitions.Insert(index, rowDefinition);
  2232. }
  2233. /// <summary>
  2234. /// 添加图表底部控件
  2235. /// </summary>
  2236. private void BuildBottomControls()
  2237. {
  2238. var grid = new Grid();
  2239. grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Auto) });
  2240. grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
  2241. //隐藏用hidden,不用Collapsed,需要用来占位
  2242. _downloadMoreButtonPlaceHolder = new TextBlock() { Visibility = Visibility.Hidden };
  2243. _downloadMoreButton = new Button()
  2244. {
  2245. Visibility = Visibility.Collapsed
  2246. };
  2247. _downloadMoreButton.Click -= DownloadMoreButton_Click;
  2248. _downloadMoreButton.Click += DownloadMoreButton_Click;
  2249. _downloadMoreButton.MouseEnter -= DownloadMoreButton_MouseEnter;
  2250. _downloadMoreButton.MouseEnter += DownloadMoreButton_MouseEnter;
  2251. _downloadMoreButton.SetValue(Grid.ColumnProperty, 0);
  2252. _downloadMoreButtonPlaceHolder.SetValue(Grid.ColumnProperty, 0);
  2253. var style = (Style)FindResource("LeftArrowButton");
  2254. _downloadMoreButton.Style = style;
  2255. var gridRight = new Grid();
  2256. gridRight.Children.Add(DateChartSlider);
  2257. gridRight.Children.Add(TimeSpanPicker); //不再大小图表上显示周期
  2258. gridRight.SetValue(Grid.ColumnProperty, 1);
  2259. grid.Children.Add(gridRight);
  2260. var gridLeft = new Grid { HorizontalAlignment = HorizontalAlignment.Right };
  2261. gridLeft.Children.Add(_downloadMoreButton);
  2262. gridLeft.Children.Add(_downloadMoreButtonPlaceHolder);
  2263. gridLeft.SetValue(Grid.ColumnProperty, 0);
  2264. grid.Children.Add(gridLeft);
  2265. _dateChartSliderStackPanel.Children.Add(_dateScaleDisplay);
  2266. _dateChartSliderStackPanel.Children.Add(grid);
  2267. }
  2268. /// <summary>
  2269. /// 生成控件
  2270. /// </summary>
  2271. private void BuildParts()
  2272. {
  2273. _exportBusyIndicator = (BusyIndicator)TreeHelper.TemplateFindName("PART_BusyIndicator", this);
  2274. _exportBusyIndicator.IsBusy = false;
  2275. _exportBusyIndicator.BusyContent = LanguageManager.FindResource(LanguageConst.ExportWaiting);
  2276. _mainGrid = (Grid)TreeHelper.TemplateFindName("PART_MainGrid", this);
  2277. _mainGrid.MouseEnter -= MainGrid_MouseEnter;
  2278. _mainGrid.MouseEnter += MainGrid_MouseEnter;
  2279. _mainGrid.MouseMove -= MainGrid_MouseMove;
  2280. _mainGrid.MouseMove += MainGrid_MouseMove;
  2281. _mainGrid.MouseDown -= _mainGrid_MouseDown;
  2282. _mainGrid.MouseDown += _mainGrid_MouseDown;
  2283. _mainGrid.PreviewKeyDown += _mainGrid_PreviewKeyDown;
  2284. _chartHeaderStackPanel = (StackPanel)TreeHelper.TemplateFindName("PART_HeaderStackPanel", this);
  2285. _drawingToolStackPanel = (StackPanel)TreeHelper.TemplateFindName("PART_DrawingToolStackPanel", this);
  2286. _chartPanesGrid = (Grid)TreeHelper.TemplateFindName("PART_ChartPanesGrid", this);
  2287. _dateChartSliderStackPanel =
  2288. (StackPanel)TreeHelper.TemplateFindName("PART_DateChartSliderStackPanel", this);
  2289. _crossHairGrid = (Canvas)TreeHelper.TemplateFindName("PART_CrossHairGrid", this);
  2290. _crossHairGrid.LostMouseCapture -= CrossHairGrid_LostMouseCapture;
  2291. _crossHairGrid.LostMouseCapture += CrossHairGrid_LostMouseCapture;
  2292. _crossHairGrid.MouseEnter -= CrossHairGrid_MouseEnter;
  2293. _crossHairGrid.MouseEnter += CrossHairGrid_MouseEnter;
  2294. _crossHairGrid.MouseLeave -= CrossHairGrid_MouseLeave;
  2295. _crossHairGrid.MouseLeave += CrossHairGrid_MouseLeave;
  2296. _crossHairGrid.MouseLeftButtonDown -= CrossHairGrid_MouseLeftButtonDown;
  2297. _crossHairGrid.MouseLeftButtonDown += CrossHairGrid_MouseLeftButtonDown;
  2298. _crossHairGrid.MouseLeftButtonUp -= CrossHairGrid_MouseLeftButtonUp;
  2299. _crossHairGrid.MouseLeftButtonUp += CrossHairGrid_MouseLeftButtonUp;
  2300. _crossHairGrid.MouseMove -= CrossHairGrid_MouseMove;
  2301. _crossHairGrid.MouseMove += CrossHairGrid_MouseMove;
  2302. _pointerPopup = (Popup)TreeHelper.TemplateFindName("PART_PointerPopup", this);
  2303. _pointerGrid = (Grid)TreeHelper.TemplateFindName("PART_PointerGrid", this);
  2304. }
  2305. void _mainGrid_PreviewKeyDown(object sender, KeyEventArgs e)
  2306. {
  2307. //鼠标上键
  2308. if (Keyboard.IsKeyDown(Key.Down))
  2309. {
  2310. this._crossHairGrid_IsDoubleClick = false;
  2311. this.HiddenCrossHairGrid();
  2312. if (this.RecordCount + this.LeftRightKeyToAcceleration < this.MaxRecordCount)
  2313. {
  2314. this.RecordCount += this.LeftRightKeyToAcceleration;
  2315. }
  2316. else
  2317. {
  2318. this.RecordCount = this.MaxRecordCount;
  2319. }
  2320. }
  2321. else if (Keyboard.IsKeyDown(Key.Up)) //鼠标下键
  2322. {
  2323. this._crossHairGrid_IsDoubleClick = false;
  2324. this.HiddenCrossHairGrid();
  2325. if (this.RecordCount - this.LeftRightKeyToAcceleration > this.MinRecordCount)
  2326. {
  2327. this.RecordCount -= this.LeftRightKeyToAcceleration;
  2328. }
  2329. else
  2330. {
  2331. this.RecordCount = this.MinRecordCount;
  2332. }
  2333. }
  2334. else if (Keyboard.IsKeyDown(Key.Left)) //鼠标左键
  2335. {
  2336. Mouse.OverrideCursor = Cursors.None;
  2337. this.DrawingCross(false);
  2338. }
  2339. else if (Keyboard.IsKeyDown(Key.Right)) //鼠标右键
  2340. {
  2341. Mouse.OverrideCursor = Cursors.None;
  2342. this.DrawingCross(true);
  2343. }
  2344. else if (Keyboard.IsKeyDown(Key.Escape))
  2345. {
  2346. this._crossHairGrid_IsDoubleClick = false;
  2347. this.HiddenCrossHairGrid();
  2348. Mouse.OverrideCursor = Cursors.Arrow;
  2349. }
  2350. e.Handled = true;
  2351. }
  2352. private void _mainGrid_MouseDown(object sender, MouseButtonEventArgs e)
  2353. {
  2354. _mainGrid.Focusable = true;
  2355. FocusManager.SetFocusedElement(_mainGrid, _mainGrid);
  2356. }
  2357. /// <summary>
  2358. /// 获取和设置the root panel
  2359. /// 代替PopupManager.PopupRoot,一个应用程序多图表时,popup只针对当前图表
  2360. /// </summary>
  2361. internal Panel RootPanel
  2362. {
  2363. get
  2364. {
  2365. return _mainGrid;
  2366. }
  2367. }
  2368. /// <summary>
  2369. /// 计算历史周期涨幅
  2370. /// </summary>
  2371. /// <returns>IList{ILineDataPoint}.</returns>
  2372. private List<ILineDataPoint> CaclulateCycleDataIncrease(List<ILineDataPoint> source)
  2373. {
  2374. return source;
  2375. }
  2376. /// <summary>
  2377. /// 计算分时数据涨幅
  2378. /// </summary>
  2379. /// <param name="source">The source.</param>
  2380. private void CaclulateTikDataIncrease(IList<ILineDataPoint> source)
  2381. {
  2382. if (source == null || !source.Any()) return;
  2383. foreach (var lineDataPoint in source)
  2384. {
  2385. var current = lineDataPoint as IOHLCDataPoint;
  2386. if (current == null) continue;
  2387. current.Increase = CurrentGoods.PreClose.Equals(0)
  2388. ? 0f
  2389. : (current.Close - CurrentGoods.PreClose) / CurrentGoods.PreClose;
  2390. current.IncreaseValue = CurrentGoods.PreClose.Equals(0) ? 0f : (current.Close - CurrentGoods.PreClose);
  2391. }
  2392. }
  2393. /// <summary>
  2394. /// 清除报价牌
  2395. /// </summary>
  2396. private void CleanQuoteBoard()
  2397. {
  2398. bool flag = _popup != null;
  2399. if (flag)
  2400. {
  2401. _popup.Child = (null);
  2402. _chartPanesGrid.Children.Remove(_popup);
  2403. _popup = null;
  2404. }
  2405. }
  2406. private void CrossHairGrid_LostMouseCapture(object sender, MouseEventArgs e)
  2407. {
  2408. if (CurrentGoods == null || string.IsNullOrWhiteSpace(CurrentGoods.Symbol)) return;
  2409. if (_crossFlag)
  2410. {
  2411. if (PointerType != PointerType.Pan)
  2412. {
  2413. _crossHairGrid.Children.Clear();
  2414. var panes = AllPanes();
  2415. foreach (var chartPane in panes)
  2416. {
  2417. var layers = chartPane.AllScaleLayers;
  2418. if (layers == null || !layers.Any()) continue;
  2419. foreach (var scaleLayer in layers)
  2420. {
  2421. var left = scaleLayer.GetLeftValueScaleDisplay();
  2422. var right = scaleLayer.GetRightValueScaleDisplay();
  2423. if (left != null) left.BuildYList();
  2424. if (right != null) right.BuildYList();
  2425. }
  2426. }
  2427. }
  2428. _crossFlag = false;
  2429. _pointerPopup.IsOpen = false;
  2430. if (_mainGrid.Children.Contains(_pointerPopup))
  2431. {
  2432. _mainGrid.Children.Remove(_pointerPopup);
  2433. }
  2434. }
  2435. }
  2436. private void CrossHairGrid_MouseEnter(object sender, MouseEventArgs e)
  2437. {
  2438. if (CurrentGoods == null || string.IsNullOrWhiteSpace(CurrentGoods.Symbol)) return;
  2439. SetCursorImagePos(true, e);
  2440. DrawingToolExecute();
  2441. }
  2442. private void CrossHairGrid_MouseLeave(object sender, MouseEventArgs e)
  2443. {
  2444. if (CurrentGoods == null || string.IsNullOrWhiteSpace(CurrentGoods.Symbol)) return;
  2445. SetCursorImagePos(false, e);
  2446. }
  2447. private int _crossHairGrid_ClickTik = 0;
  2448. public bool _crossHairGrid_IsDoubleClick = false;
  2449. private void CrossHairGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  2450. {
  2451. _crossHairGrid_ClickTik += 1;
  2452. DispatcherTimer timer = new DispatcherTimer();
  2453. timer.Interval = new TimeSpan(0, 0, 0, 0, 300);
  2454. timer.Tick += (cs, ce) =>
  2455. {
  2456. timer.IsEnabled = false;
  2457. _crossHairGrid_ClickTik = 0;
  2458. };
  2459. timer.IsEnabled = true;
  2460. if (_crossHairGrid_ClickTik % 2 == 0)
  2461. {
  2462. timer.IsEnabled = false;
  2463. _crossHairGrid_ClickTik = 0;
  2464. _crossHairGrid_IsDoubleClick = !_crossHairGrid_IsDoubleClick;
  2465. if (CurrentGoods == null || string.IsNullOrWhiteSpace(CurrentGoods.Symbol))
  2466. {
  2467. return;
  2468. }
  2469. else
  2470. {
  2471. if (Keyboard.Modifiers == (ModifierKeys)2 && _oldDrawingTool != null)
  2472. {
  2473. SetDrawingTool(_oldDrawingTool);
  2474. var panes = AllPanes();
  2475. foreach (var current in panes)
  2476. {
  2477. Point position = e.GetPosition(current.PaintArea);
  2478. if (position.X >= 0.0 && position.Y >= 0.0 && position.X <= current.PaintArea.ActualWidth &&
  2479. position.Y <= current.PaintArea.ActualHeight)
  2480. {
  2481. current.SimulateDrawingMouseDown(RuntimeHelpers.GetObjectValue(sender), e);
  2482. break;
  2483. }
  2484. }
  2485. }
  2486. else
  2487. {
  2488. _crossHairGrid.CaptureMouse();
  2489. _crossFlag = true;
  2490. if (PointerType == PointerType.Pan)
  2491. {
  2492. _crossX = (float)e.GetPosition(_crossHairGrid).X;
  2493. _crossDateTime = DateScaler.DateFromX(_crossX);
  2494. }
  2495. else
  2496. {
  2497. if (PointerType == PointerType.CursorWithData)
  2498. {
  2499. _pointerPopup.IsOpen = true;
  2500. }
  2501. if (_crossHairGrid.Children.Count == 0)
  2502. {
  2503. _crossHairGrid.Children.Add(_hline);
  2504. _crossHairGrid.Children.Add(_vline);
  2505. }
  2506. DrawingCross(e);
  2507. }
  2508. }
  2509. }
  2510. }
  2511. }
  2512. private void CrossHairGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  2513. {
  2514. if (CurrentGoods == null || string.IsNullOrWhiteSpace(CurrentGoods.Symbol)) return;
  2515. if (_crossFlag)
  2516. {
  2517. if (!_crossHairGrid_IsDoubleClick)
  2518. {
  2519. _crossHairGrid.ReleaseMouseCapture();
  2520. if (PointerType == PointerType.Pan)
  2521. {
  2522. MoveChartPane(e);
  2523. }
  2524. else
  2525. {
  2526. _crossHairGrid.Children.Clear();
  2527. var panes = AllPanes();
  2528. var scaleLayerList = panes.Select(chartPane => chartPane.AllScaleLayers)
  2529. .Where(layers => layers != null && layers.Any())
  2530. .SelectMany(layers => layers)
  2531. .ToList();
  2532. foreach (var scaleLayer in scaleLayerList)
  2533. {
  2534. scaleLayer.GetRightValueScaleDisplay().BuildYList();
  2535. scaleLayer.GetLeftValueScaleDisplay().BuildYList();
  2536. }
  2537. }
  2538. _crossFlag = false;
  2539. _pointerPopup.IsOpen = false;
  2540. }
  2541. }
  2542. }
  2543. public void HiddenCrossHairGrid()
  2544. {
  2545. _crossHairGrid.ReleaseMouseCapture();
  2546. _pointerPopup.IsOpen = false;
  2547. }
  2548. DateTime LastRedrawTime = DateTime.Now;
  2549. private Point lastPoint = new Point();
  2550. private void CrossHairGrid_MouseMove(object sender, MouseEventArgs e)
  2551. {
  2552. Mouse.OverrideCursor = Cursors.Arrow;
  2553. if (CurrentGoods == null || string.IsNullOrWhiteSpace(CurrentGoods.Symbol))
  2554. {
  2555. return;
  2556. }
  2557. if (_crossHairGrid_IsDoubleClick)
  2558. {
  2559. this.Cursor = Cursors.None;
  2560. }
  2561. else
  2562. {
  2563. this.Cursor = Cursors.Arrow;
  2564. }
  2565. SetCursorImagePos(true, e);
  2566. if (_crossFlag)
  2567. {
  2568. if (PointerType == PointerType.Pan)
  2569. {
  2570. MoveChartPane(e);
  2571. }
  2572. else
  2573. {
  2574. DrawingCross(e);
  2575. }
  2576. }
  2577. }
  2578. /// <summary>
  2579. /// Handles the Click event of the _downloadMoreButton control.
  2580. /// </summary>
  2581. /// <param name="sender">The source of the event.</param>
  2582. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  2583. private void DownloadMoreButton_Click(object sender, RoutedEventArgs e)
  2584. {
  2585. _downloadMoreButton.Visibility = Visibility.Collapsed;
  2586. DownloadNextDataBlock();
  2587. }
  2588. private void DownloadMoreButton_MouseEnter(object sender, MouseEventArgs e)
  2589. {
  2590. _downloadMoreButton.ToolTip =
  2591. string.Format(LanguageManager.FindResource(LanguageConst.ToolTip_DownloadMore), MaxLoadRecordCount);
  2592. }
  2593. /// <summary>
  2594. /// 更新图表
  2595. /// </summary>
  2596. private void DrawingChart(bool isUpdataSet)
  2597. {
  2598. if (_chartPanesGrid == null) return;
  2599. if (CycleType == CycleType.TimeSharing)
  2600. {
  2601. DrawingTimeShareChart();
  2602. return; //分时图不使用这个方法更新图表
  2603. }
  2604. var allDateScaler = (AllDateScaler)DateScaler;
  2605. var width = (int)GetChartPaneWidth();
  2606. width = Math.Max(0, width);
  2607. var left = (float)GetChartPanelOffset();
  2608. allDateScaler.SetPlotInfo(width, left);
  2609. _dateScaleDisplay.BuildCanvas(allDateScaler, left, width, false);
  2610. //DateChartSlider.Margin = new Thickness(left, 0, 0, 0);
  2611. _downloadMoreButtonPlaceHolder.Width = left;
  2612. DateChartSlider.BuildDateChartSlider();
  2613. ShowDownloadButton(allDateScaler.StartDateIndex == 0);
  2614. var chartPanes = _chartPanesGrid.Children.OfType<ChartPane>();
  2615. var childrenCount = chartPanes.Count();
  2616. var paneIndex = 0;
  2617. if (CycleType == CycleType.Tik)
  2618. {
  2619. foreach (var current in _chartPanesGrid.Children)
  2620. {
  2621. var pane = current as ChartPane;
  2622. if (pane != null)
  2623. {
  2624. //最后一个ChartPane时更新
  2625. pane.RefreshChartFlag = true;
  2626. pane.BuildTimeShareChartPane(allDateScaler, _dateScaleDisplay);
  2627. }
  2628. }
  2629. }
  2630. else
  2631. {
  2632. foreach (var current in chartPanes)
  2633. {
  2634. paneIndex++;
  2635. if (current != null)
  2636. {
  2637. //最后一个ChartPane时更新
  2638. current.RefreshChartFlag = paneIndex == childrenCount;
  2639. current.BuildChartPane(allDateScaler, _dateScaleDisplay, isUpdataSet);
  2640. }
  2641. }
  2642. }
  2643. }
  2644. /// <summary>
  2645. /// 创建分时图
  2646. /// </summary>
  2647. private void DrawingTimeShareChart()
  2648. {
  2649. if (_chartPanesGrid == null) return;
  2650. var width = (int)GetChartPaneWidth();
  2651. width = Math.Max(0, width);
  2652. var left = (float)GetChartPanelOffset();
  2653. var allDateScaler = CreateDateScaler(width, left);
  2654. DateScaler = allDateScaler;
  2655. _dateScaleDisplay.BuildTimeShareCanvas(allDateScaler, left, width, false,
  2656. CurrentGoods.OpenCloseTimes, this);
  2657. int paneIndex = 0;
  2658. foreach (var current in _chartPanesGrid.Children)
  2659. {
  2660. var pane = current as ChartPane;
  2661. if (pane != null)
  2662. {
  2663. //最后一个ChartPane时更新
  2664. pane.RefreshChartFlag = paneIndex == 1;
  2665. pane.BuildTimeShareChartPane(allDateScaler, _dateScaleDisplay);
  2666. paneIndex++;
  2667. }
  2668. }
  2669. }
  2670. /// <summary>
  2671. /// 创建分时图的时间轴
  2672. /// </summary>
  2673. private AllDateScaler CreateDateScaler(int width, float left)
  2674. {
  2675. int count = SetCountPoint();
  2676. var dateScaler = new AllDateScaler(this)
  2677. {
  2678. NumBars = count,
  2679. DesiredEndDate = DateTime.Today.AddMinutes(count),
  2680. };
  2681. dateScaler.SetPlotInfo(width, left);
  2682. var dataPoints = new List<ILineDataPoint>();
  2683. if (CurrentGoods == null || CurrentGoods.OpenCloseTimes == null) return dateScaler;
  2684. var timeSpan = TimeSpan.Zero;
  2685. foreach (var openCloseTimes in CurrentGoods.OpenCloseTimes)
  2686. {
  2687. timeSpan = openCloseTimes.CloseTime - openCloseTimes.OpenTime;
  2688. for (int i = 0; i <= timeSpan.TotalMinutes; i++)
  2689. {
  2690. dataPoints.Add(new BarDataPoint(openCloseTimes.OpenTime.AddMinutes(i), 0));
  2691. }
  2692. }
  2693. dateScaler.SetDates(dataPoints, CurrentTimeFrame, false);
  2694. return dateScaler;
  2695. }
  2696. /// <summary>
  2697. /// 十字光标按下时画交叉线
  2698. /// </summary>
  2699. /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
  2700. private void DrawingCross(MouseEventArgs e)
  2701. {
  2702. var position = e.GetPosition(_crossHairGrid);
  2703. _currentX = position.X;
  2704. _currentY = position.Y;
  2705. currentX = _currentX;
  2706. currentY = _currentY;
  2707. _offSetNum = 0;
  2708. //限制超出ChartPane时十字光标的显示-新增OriginalDateValue, OriginalDateFromX
  2709. var start = DateScaler.OriginalDateValue(DateScaler.StartDateIndex);
  2710. var end = DateScaler.OriginalDateValue(DateScaler.EndDateIndex);
  2711. var distance = DateScaler.DistanceBetweenDates();
  2712. _currentX -= (float)distance / 2;
  2713. var aDate = (_currentX <= 0 || _currentX < distance) ? start : DateScaler.OriginalDateFromX(_currentX - 5);
  2714. if (aDate.Equals(DateTime.MinValue) || aDate > end)
  2715. {
  2716. aDate = end;
  2717. }
  2718. if (aDate.Equals(DateTime.MinValue) || aDate < start)
  2719. {
  2720. aDate = start;
  2721. }
  2722. //显示报价牌
  2723. ShowQuoteBoard(aDate);
  2724. var num = (float)((int)Math.Round((double)DateScaler.XforDate(aDate)));
  2725. //5.0f 是PART_ChartPanesGrid的Margin, 0.5f在LinePlot/DateScaleDisplay里都加有0.5的微调
  2726. //另外加上左边纵坐标引起的位移
  2727. num = num + 5f + 0.5f + (float)GetChartPanelOffset();
  2728. var y = _currentY < 0 ? 0 : _currentY;
  2729. var with = _crossHairGrid.ActualWidth;
  2730. var height = _crossHairGrid.ActualHeight;
  2731. var brush = IsDarkBackground
  2732. ? (new SolidColorBrush(Colors.White))
  2733. : (new SolidColorBrush(Colors.Black));
  2734. _hline.X1 = 0;
  2735. _hline.X2 = with;
  2736. _hline.Y1 = y;
  2737. _hline.Y2 = y;
  2738. _hline.Stroke = brush;
  2739. _vline.X1 = num;
  2740. _vline.X2 = num;
  2741. _vline.Y1 = 0;
  2742. _vline.Y2 = height;
  2743. _vline.Stroke = brush;
  2744. var panes = AllPanes();
  2745. foreach (var current in panes)
  2746. {
  2747. var position2 = e.GetPosition(current);
  2748. bool flag = position2.Y >= 0.0 && position2.Y <= current.ActualHeight;
  2749. var layers = current.AllScaleLayers;
  2750. if (layers == null || !layers.Any()) continue;
  2751. foreach (var scaleLayer in layers)
  2752. {
  2753. var left = scaleLayer.GetLeftValueScaleDisplay();
  2754. var right = scaleLayer.GetRightValueScaleDisplay();
  2755. if (flag)
  2756. {
  2757. if (left != null) left.BuildYList((float)position2.Y);
  2758. if (right != null) right.BuildYList((float)position2.Y);
  2759. }
  2760. else
  2761. {
  2762. if (left != null) left.BuildYList();
  2763. if (right != null) right.BuildYList();
  2764. }
  2765. }
  2766. }
  2767. }
  2768. /// <summary>
  2769. /// 十字光标按下时画交叉线
  2770. /// </summary>
  2771. /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
  2772. public void DrawingCross(bool isAddTime = true)
  2773. {
  2774. _offSetNum += isAddTime ? 1 : -1;
  2775. var aDate = DateScaler.OriginalDateFromX(currentX - 5, _offSetNum);
  2776. if (DateScaler.XforDate(aDate) < 0)
  2777. {
  2778. _offSetNum += (!isAddTime) ? 1 : -1;
  2779. return;
  2780. }
  2781. if (DateScaler.XforDate(aDate) > _chartPanesGrid.ActualWidth)
  2782. {
  2783. _offSetNum += (!isAddTime) ? 1 : -1;
  2784. return;
  2785. }
  2786. //显示报价牌
  2787. ShowQuoteBoard(aDate);
  2788. var num = (float)((int)Math.Round((double)DateScaler.XforDate(aDate)));
  2789. //5.0f 是PART_ChartPanesGrid的Margin, 0.5f在LinePlot/DateScaleDisplay里都加有0.5的微调
  2790. //另外加上左边纵坐标引起的位移
  2791. num = num + 5f + 0.5f + (float)GetChartPanelOffset();
  2792. var y = _currentY < 0 ? 0 : _currentY;
  2793. var with = _crossHairGrid.ActualWidth;
  2794. var height = _crossHairGrid.ActualHeight;
  2795. var brush = IsDarkBackground ? (new SolidColorBrush(Colors.White)) : (new SolidColorBrush(Colors.Black));
  2796. _hline.X1 = 0;
  2797. _hline.X2 = with;
  2798. _hline.Y1 = y;
  2799. _hline.Y2 = y;
  2800. _hline.Stroke = brush;
  2801. _vline.X1 = num;
  2802. _vline.X2 = num;
  2803. _vline.Y1 = 0;
  2804. _vline.Y2 = height;
  2805. _vline.Stroke = brush;
  2806. var panes = AllPanes();
  2807. foreach (var current in panes)
  2808. {
  2809. bool flag = currentY >= 0.0 && currentY <= current.ActualHeight;
  2810. var layers = current.AllScaleLayers;
  2811. if (layers == null || !layers.Any()) continue;
  2812. foreach (var scaleLayer in layers)
  2813. {
  2814. var left = scaleLayer.GetLeftValueScaleDisplay();
  2815. var right = scaleLayer.GetRightValueScaleDisplay();
  2816. if (flag)
  2817. {
  2818. if (left != null) left.BuildYList((float)currentY);
  2819. if (right != null) right.BuildYList((float)currentY);
  2820. }
  2821. else
  2822. {
  2823. if (left != null) left.BuildYList();
  2824. if (right != null) right.BuildYList();
  2825. }
  2826. }
  2827. }
  2828. }
  2829. /// <summary>
  2830. /// 加载叠加商品数据
  2831. /// </summary>
  2832. /// <param name="comparisonGoods">The comparison goods.</param>
  2833. /// <returns>IList{ILineDataPoint}.</returns>
  2834. private List<ILineDataPoint> LoadComparisonDataSet(ComparisonGoods comparisonGoods)
  2835. {
  2836. //历史数据回补后叠加商品记录条数以当前主商品记录条数为准
  2837. var flag = DataSet == null || DataSet.DataPoints == null || !DataSet.DataPoints.Any();
  2838. if (flag) return null;
  2839. var count = DataSet.DataPoints.Count;
  2840. var endTime = DataSet.DataPoints.Max(z => z.Date);
  2841. return OnLoadHistoryCycleData(comparisonGoods, CycleType, CurrentTimeFrame, MaxLoadRecordCount, endTime);
  2842. }
  2843. /// <summary>
  2844. /// Loads the configs from XML.
  2845. /// </summary>
  2846. /// <param name="configName">Name of the config.</param>
  2847. private void LoadConfigsFromXml(string configName)
  2848. {
  2849. //to-do:
  2850. }
  2851. /// <summary>
  2852. /// Handles the MouseEnter event of the MainGrid control.
  2853. /// </summary>
  2854. /// <param name="sender">The source of the event.</param>
  2855. /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
  2856. private void MainGrid_MouseEnter(object sender, MouseEventArgs e)
  2857. {
  2858. if (CurrentDrawingTool != null)
  2859. {
  2860. SetDrawingTool(CurrentDrawingTool);
  2861. }
  2862. else
  2863. {
  2864. DrawingToolExecute();
  2865. }
  2866. }
  2867. /// <summary>
  2868. /// Handles the MouseMove event of the MainGrid control.
  2869. /// </summary>
  2870. /// <param name="sender">The source of the event.</param>
  2871. /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
  2872. private void MainGrid_MouseMove(object sender, MouseEventArgs e)
  2873. {
  2874. if (CurrentDrawingTool != null)
  2875. {
  2876. SetDrawingTool(CurrentDrawingTool);
  2877. }
  2878. else
  2879. {
  2880. DrawingToolExecute();
  2881. }
  2882. //Console.WriteLine("鼠标移动开始:" + startTime);
  2883. //timer.IsEnabled = false;
  2884. //startTime = DateTime.Now;
  2885. //timer.IsEnabled = true;
  2886. }
  2887. /// <summary>
  2888. /// Pane工具相当于移动时间滑块
  2889. /// </summary>
  2890. /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
  2891. private void MoveChartPane(MouseEventArgs e)
  2892. {
  2893. DateTime desiredEndDate = DateScaler.DesiredEndDate;
  2894. float num = DateScaler.XforDate(_crossDateTime);
  2895. var num2 = (float)(_crossHairGrid.ActualWidth / (double)DateScaler.NumBars);
  2896. var num3 = (float)(e.GetPosition(_crossHairGrid).X - (double)num);
  2897. var num4 = checked((int)Math.Round((double)unchecked((float)DateScaler.EndDateIndex - num3 / num2)));
  2898. var count = DateScaler.Count();
  2899. bool flag = num4 >= count;
  2900. DateScaler.DesiredEndDate = flag ? DateTime.MaxValue : DateScaler.DateValue(num4);
  2901. if (!DateScaler.DesiredEndDate.Equals(desiredEndDate))
  2902. {
  2903. Refresh();
  2904. }
  2905. }
  2906. /// <summary>
  2907. /// 清除图表容器,切换周期时使用
  2908. /// </summary>
  2909. private void ChangePanesClear()
  2910. {
  2911. _chartPanes.Clear();
  2912. _chartPanesGrid.Children.Clear();
  2913. _chartPanesGrid.RowDefinitions.Clear();
  2914. }
  2915. /// <summary>
  2916. /// Called when [cycle type changed].
  2917. /// </summary>
  2918. /// <param name="refreshNow">if set to <c>true</c> [refresh now].</param>
  2919. /// <param name="isFromTimesharing">是否是从分时图中切换</param>
  2920. private void OnCycleTypeChanged(bool refreshNow, bool isFromTimesharing = false)
  2921. {
  2922. if (CurrentGoods == null) return;
  2923. var cycleType = CycleType;
  2924. var timeSpan = CurrentTimeFrame;
  2925. var goods = CurrentGoods;
  2926. var maxLoadRecordCount = MaxLoadRecordCount;
  2927. Dispatcher.BeginInvoke(new Action(() =>
  2928. {
  2929. DataSet = new ChartDataSet();
  2930. switch (cycleType)
  2931. {
  2932. case CycleType.TimeSharing:
  2933. ChangePanesClear();
  2934. DateChartSlider.Visibility = Visibility.Collapsed;
  2935. _downloadMoreButton.Visibility = Visibility.Collapsed;
  2936. OpenTimeSharePlot(CurrentGoods);
  2937. _timeSharingTimer.Start(); //开始定时器补数据
  2938. SetDatesAndRefresh(refreshNow);
  2939. break;
  2940. case CycleType.Tik:
  2941. ChangePanesClear();
  2942. // DateChartSlider.Visibility = Visibility.Collapsed;
  2943. DateChartSlider.Visibility = Visibility.Visible;
  2944. _downloadMoreButton.Visibility = Visibility.Collapsed;
  2945. _timeSharingTimer.Stop();
  2946. CreateTikChart(CurrentGoods);
  2947. SetDatesAndRefresh(refreshNow);
  2948. break;
  2949. default:
  2950. ////数据源更改,计算涨幅
  2951. if (isFromTimesharing)
  2952. {
  2953. ChangePanesClear();
  2954. foreach (var chartPane in _chartCachePanes)
  2955. {
  2956. //取最大grid高度做*
  2957. var stars = _chartPanesGrid.RowDefinitions.Count > 0
  2958. ? _chartPanesGrid.RowDefinitions.Max(z => z.Height.Value)
  2959. : 1.0;
  2960. AddChartPane(chartPane, stars, IsMainChartPane(chartPane));
  2961. }
  2962. }
  2963. //CreateMinuteChart(CurrentGoods);
  2964. _timeSharingTimer.Stop();
  2965. DateChartSlider.Visibility = Visibility.Visible;
  2966. DataSet.DataPoints = CaclulateCycleDataIncrease(OnLoadHistoryCycleData(goods, cycleType, timeSpan, maxLoadRecordCount, DateTime.Now));
  2967. SetDatesAndRefresh(refreshNow);
  2968. break;
  2969. }
  2970. }), DispatcherPriority.Background);
  2971. }
  2972. /// <summary>
  2973. /// 设置日期和刷新.
  2974. /// </summary>
  2975. /// <param name="refreshNow">if set to <c>true</c> [refresh now].</param>
  2976. private void SetDatesAndRefresh(bool refreshNow,bool isupdateBar=true)
  2977. {
  2978. Dispatcher.BeginInvoke(new Action(() =>
  2979. {
  2980. var allDateScaler = (AllDateScaler)DateScaler;
  2981. allDateScaler.SetDates(DataSet.DataPoints, CurrentTimeFrame, false);
  2982. //numBars为矩形柱间距,矩形柱数量实际要-1
  2983. if (isupdateBar)
  2984. {
  2985. allDateScaler.NumBars = RecordCountInternal - 1;
  2986. }
  2987. // if (refreshNow) Refresh(true);
  2988. if (DataSet.DataPoints != null && DataSet.DataPoints.Any())
  2989. { Refresh(refreshNow); }
  2990. }), DispatcherPriority.Background);
  2991. }
  2992. /// <summary>
  2993. /// Cycles the type property changed.
  2994. /// </summary>
  2995. /// <param name="isFromTimesharing">是否是从分时图中切换</param>
  2996. private void CycleTypePropertyChanged(bool isFromTimesharing)
  2997. {
  2998. Dispatcher.BeginInvoke(new Action(() =>
  2999. {
  3000. if (CycleType.TimeSharing == CycleType)
  3001. {
  3002. _chartCachePanes.Clear();
  3003. foreach (var chartPane in _chartPanes)
  3004. {
  3005. if (!chartPane.IsTimeSpaning)
  3006. {
  3007. _chartCachePanes.Add(chartPane);
  3008. }
  3009. }
  3010. }
  3011. if (_comparisonGoodsList == null || !_comparisonGoodsList.Any())
  3012. {
  3013. OnCycleTypeChanged(true, isFromTimesharing);
  3014. }
  3015. else
  3016. {
  3017. OnCycleTypeChanged(false, isFromTimesharing);
  3018. //刷新叠加商品
  3019. RefreshComparsionGoods();
  3020. }
  3021. //更新底部快捷周期状态
  3022. // TimeSpanPicker.UpdateCyleType(CycleType);
  3023. }), DispatcherPriority.Background);
  3024. }
  3025. /// <summary>
  3026. /// Called when [load goods data].
  3027. /// </summary>
  3028. /// <param name="goods">The goods.</param>
  3029. /// <param name="totalCount">The total count.</param>
  3030. /// <param name="endTime">The end time.</param>
  3031. /// <returns>ChartDataSet.</returns>
  3032. private List<ILineDataPoint> OnLoadHistoryCycleData(GoodsInfo goods, CycleType cycleType, TimeSpan timeSpan, int totalCount, DateTime endTime)
  3033. {
  3034. List<ILineDataPoint> result = null;
  3035. var cycleTypeHandler = LoadHistoryCycleDataByCycleType;
  3036. if (cycleTypeHandler != null)
  3037. {
  3038. result = cycleTypeHandler(goods.ExchangeCode, goods.GoodsCode, cycleType, timeSpan,
  3039. DateTime.MinValue, endTime, totalCount);
  3040. }
  3041. //集合都要按时间顺排序,才能画图
  3042. return result == null ? null : result.OrderBy(z => z.Date).ToList();
  3043. }
  3044. /// <summary>
  3045. /// 获取分时图的数据分钟数据
  3046. /// </summary>
  3047. /// <param name="goods">The goods.</param>
  3048. /// <param name="totalCount">The total count.</param>
  3049. /// <param name="startTime">开始时间</param>
  3050. /// <param name="endTime">The end time.</param>
  3051. /// <returns>ChartDataSet.</returns>
  3052. private List<ILineDataPoint> OnLoadHistoryTimeSpaningCycleData(GoodsInfo goods, int totalCount,
  3053. DateTime startTime, DateTime endTime)
  3054. {
  3055. List<ILineDataPoint> result = null;
  3056. var cycleTypeHandler = LoadHistoryCycleDataByCycleType;
  3057. if (cycleTypeHandler != null)
  3058. {
  3059. result = cycleTypeHandler(goods.ExchangeCode, goods.GoodsCode, CycleType.Minute, CurrentTimeFrame, startTime,
  3060. endTime, totalCount);
  3061. }
  3062. //集合都要按时间顺排序,才能画图
  3063. return result == null ? null : result.OrderBy(z => z.Date).ToList();
  3064. }
  3065. /// <summary>
  3066. /// 更改时间间隔后执行
  3067. /// </summary>
  3068. /// <param name="cycleType">Type of the cycle.</param>
  3069. private void OnTimeSpanChanged(CycleType cycleType)
  3070. {
  3071. DateScaler.DesiredEndDate = DateTime.MaxValue; //必须设为最大值
  3072. CycleType = cycleType;
  3073. }
  3074. /// <summary>
  3075. /// 设置当前画线工具,并处理它与图例间的变动
  3076. /// </summary>
  3077. /// <param name="aDrawing"></param>
  3078. private void SetDrawingTool(IDrawingTool aDrawing)
  3079. {
  3080. CurrentDrawingTool = aDrawing;
  3081. DrawingToolExecute();
  3082. var chartPanes = AllPanes();
  3083. foreach (var chartPane in chartPanes)
  3084. {
  3085. chartPane.OnDrawing();
  3086. }
  3087. }
  3088. /// <summary>
  3089. /// 显示当前时间的报价牌
  3090. /// </summary>
  3091. /// <param name="aDate">A date.</param>
  3092. private void ShowQuoteBoard(DateTime aDate)
  3093. {
  3094. if (_pointerPopup.IsOpen && !aDate.Equals(DateTime.MinValue))
  3095. {
  3096. var list = new List<ControlPair>();
  3097. ControlPair controlPair = null;
  3098. var controlPair2 = new ControlPair();
  3099. var textBlock = new TextBlock
  3100. {
  3101. Text = aDate.ToString("yyyy-MM-dd ddd HH:mm:ss"),
  3102. //aDate.ToString(aDate.TimeOfDay.Ticks == 0L ? "yyyy-MM-dd ddd" : "yyyy-MM-dd ddd hh:mm:ss"),
  3103. Foreground =
  3104. IsDarkBackground
  3105. ? (new SolidColorBrush(Colors.White))
  3106. : (new SolidColorBrush(Colors.Black)),
  3107. HorizontalAlignment = HorizontalAlignment.Center
  3108. };
  3109. controlPair2.ControlLeft = textBlock;
  3110. list.Add(controlPair2);
  3111. var panes = AllPanes();
  3112. foreach (var current in panes)
  3113. {
  3114. bool flag = false;
  3115. var scaleLayers = current.AllScaleLayers;
  3116. if (scaleLayers != null && scaleLayers.Any())
  3117. {
  3118. foreach (var scaleLayer in scaleLayers)
  3119. {
  3120. var allFormulas = new List<DisplayFormula>(scaleLayer.AllDisplayFormula);
  3121. allFormulas.AddRange(scaleLayer.ComparisonFormulas);
  3122. if (!allFormulas.Any()) continue;
  3123. foreach (var formula in allFormulas)
  3124. {
  3125. //plot.PlotBase_5031(aDate, IsDarkBackground, SymbolManager.Manager().SymbolPrecision(CurrentSymbol) > 2)
  3126. var controlPairs = formula.GetControlPairs(aDate, IsDarkBackground, this.CurrentGoods.MinUnit);
  3127. if (controlPairs != null && controlPairs.Any())
  3128. {
  3129. flag = true;
  3130. if (controlPair != null)
  3131. {
  3132. list.Add(controlPair);
  3133. controlPair = null;
  3134. }
  3135. list.AddRange(controlPairs);
  3136. }
  3137. }
  3138. }
  3139. }
  3140. if (flag)
  3141. {
  3142. controlPair = new ControlPair()
  3143. {
  3144. ControlLeft = new Border()
  3145. {
  3146. Margin = new Thickness(0.0, 2.0, 0.0, 2.0),
  3147. Height = 2.0,
  3148. BorderThickness = new Thickness(1.0),
  3149. BorderBrush = new SolidColorBrush(ColorHelper.DimGray)
  3150. }
  3151. };
  3152. }
  3153. }
  3154. _pointerGrid.RowDefinitions.Clear();
  3155. _pointerGrid.Children.Clear();
  3156. int num = 0;
  3157. foreach (var current in list)
  3158. {
  3159. var rowDefinition = new RowDefinition { Height = new GridLength(1.0, 0) };
  3160. _pointerGrid.RowDefinitions.Add(rowDefinition);
  3161. if (current.ControlLeft != null)
  3162. {
  3163. current.ControlLeft.SetValue(Control.FontSizeProperty, ChartFontSize);
  3164. if (current.ControlRight == null)
  3165. {
  3166. current.ControlLeft.SetValue(Grid.RowProperty, num);
  3167. current.ControlLeft.SetValue(Grid.ColumnSpanProperty, 2);
  3168. _pointerGrid.Children.Add(current.ControlLeft);
  3169. }
  3170. else
  3171. {
  3172. current.ControlRight.SetValue(Control.FontSizeProperty, ChartFontSize);
  3173. current.ControlLeft.SetValue(Grid.RowProperty, num);
  3174. current.ControlRight.SetValue(Grid.RowProperty, num);
  3175. current.ControlRight.SetValue(Grid.ColumnProperty, 1);
  3176. _pointerGrid.Children.Add(current.ControlLeft);
  3177. _pointerGrid.Children.Add(current.ControlRight);
  3178. }
  3179. }
  3180. num++;
  3181. }
  3182. //_pointerGrid.UpdateLayout();
  3183. //左边纵坐标宽度
  3184. var buffer = GetChartPanelOffset();
  3185. if (_pointerGrid.ActualWidth + buffer > (double)_currentX)
  3186. {
  3187. _pointerPopup.HorizontalOffset = (_pointerGrid.ActualWidth + buffer);
  3188. }
  3189. else
  3190. {
  3191. _pointerPopup.HorizontalOffset = buffer;
  3192. }
  3193. }
  3194. }
  3195. #endregion Private Methods
  3196. #region 分时图
  3197. private void _timeSharingTimer_Tick(object sender, EventArgs e)
  3198. {
  3199. //todo:修改使用行情时间
  3200. var updateTime = _goodsQuoteTime.AddMinutes(1);
  3201. //var updateTime = DateTime.Now.AddSeconds(-DateTime.Now.Second);
  3202. if (DataSet == null || DataSet.DataPoints == null || !DataSet.DataPoints.Any())
  3203. {
  3204. //没有数据源无法补数据
  3205. _timeSharingTimer.Stop();
  3206. }
  3207. else
  3208. {
  3209. var lastPoint = DataSet.DataPoints[DataSet.DataPoints.Count - 1];
  3210. var dataPoint = new BarDataPoint(lastPoint.Date, lastPoint.Value); ;
  3211. var timeSpan = updateTime - dataPoint.Date;
  3212. if (timeSpan.TotalSeconds >= 60)
  3213. {
  3214. dataPoint.Date = updateTime;
  3215. dataPoint.Volume = 0;
  3216. UpdateTimeShareData(dataPoint, this.CurrentGoods.GoodsCode);
  3217. }
  3218. UpdateTimerComparisonShareing(updateTime); //定时器更新叠加数据
  3219. }
  3220. }
  3221. /// <summary>
  3222. /// 定时器更新叠加商品数据
  3223. /// </summary>
  3224. /// <param name="updateTime">更新时间</param>
  3225. private void UpdateTimerComparisonShareing(DateTime updateTime)
  3226. {
  3227. if (this.ComparisionTimeShareDic != null)
  3228. {
  3229. var keyList = this.ComparisionTimeShareDic.Keys.ToList();
  3230. foreach (var goodsCodeKey in keyList)
  3231. {
  3232. if (this.ComparisionTimeShareDic[goodsCodeKey].Any())
  3233. {
  3234. var lastPoint = this.ComparisionTimeShareDic[goodsCodeKey][
  3235. this.ComparisionTimeShareDic[goodsCodeKey].Count - 1];
  3236. var dataPoint = new BarDataPoint(lastPoint.Date, lastPoint.Value); ;
  3237. var timeSpan = updateTime - dataPoint.Date;
  3238. if (timeSpan.TotalSeconds >= 60)
  3239. {
  3240. dataPoint.Date = updateTime;
  3241. UpdateComparisonData(goodsCodeKey, (IBarDataPoint)dataPoint);
  3242. }
  3243. }
  3244. }
  3245. }
  3246. }
  3247. /// <summary>
  3248. /// 更新实时数据数据集
  3249. /// </summary>
  3250. /// <param name="barDataPoint">新添加的数据</param>
  3251. /// <param name="goodsCode">更新数据的商品代码</param>
  3252. public void UpdateTimeShareData(IBarDataPoint barDataPoint, string goodsCode)
  3253. {
  3254. if (barDataPoint == null) return;
  3255. //格式化时间
  3256. barDataPoint.Date = barDataPoint.Date.Date.AddMinutes((int)barDataPoint.Date.TimeOfDay.TotalMinutes);
  3257. this.Dispatcher.BeginInvoke(new Action(() =>
  3258. {
  3259. if (this.CurrentGoods != null && goodsCode == this.CurrentGoods.GoodsCode)
  3260. {
  3261. if (DataSet == null)
  3262. {
  3263. DataSet = new ChartDataSet();
  3264. }
  3265. if (DataSet.DataPoints == null)
  3266. {
  3267. DataSet.DataPoints = new List<ILineDataPoint>();
  3268. }
  3269. if (DataSet.DataPoints.Any())
  3270. {
  3271. var volData = DataSet.DataPoints[DataSet.DataPoints.Count - 1];
  3272. var timeSpan = barDataPoint.Date.AddSeconds(-barDataPoint.Date.Second) -
  3273. volData.Date.AddSeconds(-barDataPoint.Date.Second);
  3274. int count = (int)timeSpan.TotalMinutes;
  3275. if (count < 0)
  3276. {
  3277. return;
  3278. }
  3279. if (count == 0)
  3280. {
  3281. DataSet.DataPoints[DataSet.DataPoints.Count - 1] = barDataPoint;
  3282. }
  3283. else
  3284. {
  3285. if (count >= 2 && count <= 1440) //使用定时器时更新可能不会出现这情况
  3286. {
  3287. var repairPoint = DataSet.DataPoints[DataSet.DataPoints.Count - 1];
  3288. var repairBarPoint = repairPoint as IBarDataPoint;
  3289. if (repairBarPoint != null)
  3290. {
  3291. repairBarPoint.Volume = 0;
  3292. }
  3293. for (int i = 0; i < count - 1; i++)
  3294. {
  3295. DataSet.DataPoints.Add(repairBarPoint);
  3296. }
  3297. }
  3298. DataSet.DataPoints.Add(barDataPoint);
  3299. }
  3300. }
  3301. else
  3302. {
  3303. DataSet.DataPoints.Add(barDataPoint);
  3304. }
  3305. if (timeSharingFormulaModel != null)
  3306. {
  3307. timeSharingFormulaModel.UpdatePointSharing(barDataPoint);
  3308. }
  3309. }
  3310. else
  3311. {
  3312. UpdateComparisonData(goodsCode, barDataPoint);
  3313. }
  3314. Refresh(true);
  3315. }));
  3316. }
  3317. /// <summary>
  3318. /// 更新叠加商品分时图
  3319. /// </summary>
  3320. /// <param name="goodsCode">商品代码</param>
  3321. /// <param name="barDataPoint"></param>
  3322. private void UpdateComparisonData(string goodsCode, IBarDataPoint barDataPoint)
  3323. {
  3324. //更新叠加分时图数据
  3325. if (ComparisionTimeShareDic.ContainsKey(goodsCode))
  3326. {
  3327. barDataPoint.Date = barDataPoint.Date.Date.AddMinutes((int)barDataPoint.Date.TimeOfDay.TotalMinutes);
  3328. ComparisionTimeShareDic[goodsCode] = ComparisionTimeShareDic[goodsCode] ?? new List<ILineDataPoint>();
  3329. if (ComparisionTimeShareDic[goodsCode].Any())
  3330. {
  3331. var volData = ComparisionTimeShareDic[goodsCode][ComparisionTimeShareDic[goodsCode].Count - 1];
  3332. var timeSpan = barDataPoint.Date.AddSeconds(-barDataPoint.Date.Second) -
  3333. volData.Date.AddSeconds(-barDataPoint.Date.Second);
  3334. int count = (int)timeSpan.TotalMinutes;
  3335. if (count < 0)
  3336. {
  3337. return;
  3338. }
  3339. else if (count == 0)
  3340. {
  3341. ComparisionTimeShareDic[goodsCode][ComparisionTimeShareDic[goodsCode].Count - 1] = barDataPoint;
  3342. }
  3343. else
  3344. {
  3345. if (count >= 2 && count <= 1440) //使用定时器时更新可能不会出现这情况
  3346. {
  3347. var repairPoint = ComparisionTimeShareDic[goodsCode][ComparisionTimeShareDic[goodsCode].Count - 1];
  3348. for (int i = 0; i < count - 1; i++)
  3349. {
  3350. ComparisionTimeShareDic[goodsCode].Add(repairPoint);
  3351. }
  3352. }
  3353. ComparisionTimeShareDic[goodsCode].Add(barDataPoint);
  3354. }
  3355. }
  3356. else
  3357. {
  3358. ComparisionTimeShareDic[goodsCode].Add(barDataPoint);
  3359. }
  3360. if (this._comparisonFommulaHsSet != null && _comparisonFommulaHsSet.ContainsKey(goodsCode))
  3361. {
  3362. _comparisonFommulaHsSet[goodsCode].UpdateComparsionPoint(barDataPoint);
  3363. }
  3364. }
  3365. }
  3366. /// <summary>
  3367. /// 更新单笔数据
  3368. /// </summary>
  3369. /// <param name="barDataPoint">tik数据</param>
  3370. /// <param name="goodsCode"></param>
  3371. public void UpdateTikData(IBarDataPoint barDataPoint, string goodsCode)
  3372. {
  3373. if (barDataPoint == null) return;
  3374. if (this.CurrentGoods != null && goodsCode == this.CurrentGoods.GoodsCode)
  3375. {
  3376. if (DataSet == null)
  3377. {
  3378. DataSet = new ChartDataSet();
  3379. }
  3380. if (DataSet.DataPoints == null)
  3381. {
  3382. DataSet.DataPoints = new List<ILineDataPoint>();
  3383. }
  3384. DataSet.DataPoints.Add(barDataPoint);
  3385. if (_tikFormulaModel != null)
  3386. {
  3387. _tikFormulaModel.UdpdateTickPoint(barDataPoint);
  3388. Refresh(true);
  3389. }
  3390. }
  3391. }
  3392. /// <summary>
  3393. /// 更新单笔数据
  3394. /// </summary>
  3395. public void UpdateMinuteData(IBarDataPoint barDataPoint, string goodsCode)
  3396. {
  3397. DataSet.DataPoints = CaclulateCycleDataIncrease(OnLoadHistoryCycleData(CurrentGoods, CycleType, CurrentTimeFrame, MaxLoadRecordCount, DateTime.Now));
  3398. SetDatesAndRefresh(true);
  3399. }
  3400. /// <summary>
  3401. /// 设置分时图当前点的数量
  3402. /// </summary>
  3403. /// <returns>当前数量</returns>
  3404. private int SetCountPoint()
  3405. {
  3406. if (CurrentGoods == null || CurrentGoods.OpenCloseTimes == null) return 0;
  3407. var timeSpan = TimeSpan.Zero;
  3408. foreach (var openCloseTime in CurrentGoods.OpenCloseTimes)
  3409. {
  3410. timeSpan += openCloseTime.CloseTime - openCloseTime.OpenTime;
  3411. }
  3412. var count = (int)timeSpan.TotalMinutes + CurrentGoods.OpenCloseTimes.Count; //加上每段时间需加1
  3413. _timeSharingMaxPointCount = count;
  3414. return count;
  3415. }
  3416. /// <summary>
  3417. /// 打开分时图
  3418. /// </summary>
  3419. /// <param name="goodsInfo">分时图商品</param>
  3420. public void OpenTimeSharePlot(GoodsInfo goodsInfo)
  3421. {
  3422. var pane = new ChartPane(this, true);
  3423. //取最大grid高度做*
  3424. var stars = _chartPanesGrid.RowDefinitions.Count > 0
  3425. ? _chartPanesGrid.RowDefinitions.Max(z => z.Height.Value)
  3426. : 1.0;
  3427. AddChartPane(pane, stars);
  3428. SetCountPoint();
  3429. var getEndDatatime = DateTime.Now;
  3430. var startDateTime = DateTime.Now.Date;
  3431. if (CurrentGoods.OpenCloseTimes != null &&
  3432. CurrentGoods.OpenCloseTimes.Any()) //取当前开收市时间
  3433. {
  3434. getEndDatatime = CurrentGoods.OpenCloseTimes[CurrentGoods.OpenCloseTimes.Count - 1].CloseTime;
  3435. startDateTime = CurrentGoods.OpenCloseTimes[0].OpenTime;
  3436. }
  3437. DataSet.DataPoints = OnLoadHistoryTimeSpaningCycleData(CurrentGoods, _timeSharingMaxPointCount, startDateTime,
  3438. getEndDatatime);
  3439. // GetComparisonDataSet(_timeSharingMaxPointCount, startDateTime, getEndDatatime);
  3440. //OnLoadHistoryTimeSpaningCycleData(CurrentGoods, _timeSharingMaxPointCount, startDateTime, getEndDatatime);
  3441. var scaleLayer = new ScaleLayer();
  3442. var displayFormula = new DisplayFormula(DataSet, this, goodsInfo)
  3443. {
  3444. OpenTime = startDateTime,
  3445. CloseTime = getEndDatatime,
  3446. };
  3447. timeSharingFormulaModel = displayFormula;
  3448. scaleLayer.AllDisplayFormula.Add(displayFormula);
  3449. displayFormula.ScaleLayer = scaleLayer;
  3450. displayFormula.IsTimeShare = true;
  3451. pane.AddScaleLayer(scaleLayer);
  3452. var plots = new List<PlotBase>();
  3453. foreach (var indicator in displayFormula.FormulaModel.IndicatorList)
  3454. {
  3455. if (!indicator.VolBar) //是否为成交量
  3456. {
  3457. var linePlot = new LinePlot(indicator, this) { ScaleLayer = scaleLayer };
  3458. scaleLayer.AllPlots.Add(linePlot);
  3459. plots.Add(linePlot);
  3460. }
  3461. else
  3462. {
  3463. //成交量显示在下方
  3464. //var volScaleLayer = new ScaleLayer();
  3465. //var volpane = new ChartPane(this);
  3466. ////取最大grid高度做*
  3467. //var start = _chartPanesGrid.RowDefinitions.Count > 0
  3468. // ? _chartPanesGrid.RowDefinitions.Max(z => z.Height.Value)
  3469. // : 1.0;
  3470. //AddChartPane(volpane, start);
  3471. //volpane.AddScaleLayer(volScaleLayer);
  3472. //var linePlot = new LinePlot(indicator, this);
  3473. //volScaleLayer.AllPlots.Add(linePlot);
  3474. //linePlot.ScaleLayer = volScaleLayer;
  3475. //plots.Add(linePlot);
  3476. }
  3477. }
  3478. //添加叠加分时图
  3479. GetComparisonDataSet(_timeSharingMaxPointCount, startDateTime, getEndDatatime, scaleLayer);
  3480. displayFormula.PlotBases = plots;
  3481. // DrawingTimeShareChart();
  3482. }
  3483. /// <summary>
  3484. /// 显示即时图
  3485. /// </summary>
  3486. /// <param name="goodsInfo">商品</param>
  3487. public void CreateTikChart(GoodsInfo goodsInfo)
  3488. {
  3489. var pane = new ChartPane(this);
  3490. //取最大grid高度做*
  3491. var stars = _chartPanesGrid.RowDefinitions.Count > 0
  3492. ? _chartPanesGrid.RowDefinitions.Max(z => z.Height.Value)
  3493. : 1.0;
  3494. AddChartPane(pane, stars);
  3495. var getEndDatatime = DateTime.Now;
  3496. var startDateTime = DateTime.Now.Date;
  3497. if (CurrentGoods.OpenCloseTimes != null &&
  3498. CurrentGoods.OpenCloseTimes.Any()) //取当前开收市时间
  3499. {
  3500. getEndDatatime = CurrentGoods.OpenCloseTimes[CurrentGoods.OpenCloseTimes.Count - 1].CloseTime;
  3501. startDateTime = CurrentGoods.OpenCloseTimes[0].OpenTime;
  3502. }
  3503. DataSet.DataPoints = OnLoadHistoryTimeSpaningCycleData(CurrentGoods, _timeSharingMaxPointCount, startDateTime,
  3504. getEndDatatime);
  3505. var scaleLayer = new ScaleLayer();
  3506. var formula = DisplayFormula.CreateTikChartFormula(this, goodsInfo, DataSet.DataPoints);
  3507. scaleLayer.AllDisplayFormula.Add(formula);
  3508. formula.ScaleLayer = scaleLayer;
  3509. pane.AddScaleLayer(scaleLayer);
  3510. _tikFormulaModel = formula;
  3511. var plots = new List<PlotBase>();
  3512. foreach (var indicator in formula.FormulaModel.IndicatorList)
  3513. {
  3514. var linePlot = new LinePlot(indicator, this) { ScaleLayer = scaleLayer };
  3515. scaleLayer.AllPlots.Add(linePlot);
  3516. plots.Add(linePlot);
  3517. }
  3518. formula.PlotBases = plots;
  3519. }
  3520. /// <summary>
  3521. /// 获取叠加商品数据
  3522. /// </summary>
  3523. /// <param name="dataCount">取数据量</param>
  3524. /// <param name="startTime">开始时间</param>
  3525. /// <param name="endDateTime">结束时间</param>
  3526. private void GetComparisonDataSet(int dataCount, DateTime startTime, DateTime endDateTime, ScaleLayer comparisonLayer)
  3527. {
  3528. ComparisionTimeShareDic.Clear();
  3529. _comparisonFommulaHsSet.Clear();
  3530. if (_comparisonGoodsList == null) return;
  3531. foreach (var goodseItem in _comparisonGoodsList)
  3532. {
  3533. var lineDataList = OnLoadHistoryTimeSpaningCycleData(goodseItem, dataCount, startTime,
  3534. endDateTime);
  3535. ComparisionTimeShareDic[goodseItem.GoodsCode] = lineDataList;
  3536. }
  3537. var formulas = DisplayFormula.TimeSharingFormulas(_comparisonGoodsList, this, ComparisionTimeShareDic);
  3538. foreach (var displayFormula in formulas)
  3539. {
  3540. CreateComparisonPlot(displayFormula, comparisonLayer);
  3541. }
  3542. }
  3543. /// <summary>
  3544. ///创建图形,添加到层中
  3545. /// </summary>
  3546. /// <param name="displayFormula">公式模型</param>
  3547. /// <param name="comparisonLayer">公式层</param>
  3548. private void CreateComparisonPlot(DisplayFormula displayFormula, ScaleLayer comparisonLayer)
  3549. {
  3550. displayFormula.PlotBases = new List<PlotBase>();
  3551. _comparisonFommulaHsSet[displayFormula.ComparionGoodsInfo.GoodsCode] = displayFormula;
  3552. comparisonLayer.ComparisonFormulas.Add(displayFormula);
  3553. foreach (var indicator in displayFormula.FormulaModel.IndicatorList)
  3554. {
  3555. var linePlot = new ComparisonPlot(indicator, this, displayFormula.ComparionGoodsInfo) { ScaleLayer = comparisonLayer };
  3556. linePlot.IsActive = true;
  3557. comparisonLayer.AllPlots.Add(linePlot);
  3558. displayFormula.PlotBases.Add(linePlot);
  3559. }
  3560. }
  3561. #endregion
  3562. #region 指标
  3563. /// <summary>
  3564. /// 设置当前公式参数
  3565. /// </summary>
  3566. public void SetCurrectFormulaParam()
  3567. {
  3568. if (this._chartPanes != null && _chartPanes.Any())
  3569. {
  3570. var scaleLayers = new List<ScaleLayer>();
  3571. var displayFormulas = new List<DisplayFormula>();
  3572. foreach (var chartPane in _chartPanes)
  3573. {
  3574. if (chartPane.AllScaleLayers != null
  3575. && chartPane.AllScaleLayers.Any())
  3576. {
  3577. scaleLayers.AddRange(chartPane.AllScaleLayers);
  3578. }
  3579. }
  3580. foreach (var scaleLayer in scaleLayers)
  3581. {
  3582. if (scaleLayer.AllDisplayFormula != null && scaleLayer.AllDisplayFormula.Any())
  3583. {
  3584. displayFormulas.AddRange(scaleLayer.AllDisplayFormula);
  3585. }
  3586. }
  3587. if (displayFormulas.Any())
  3588. {
  3589. var formulaModels = displayFormulas.Select((displayFormula) => displayFormula.FormulaModel).ToList();
  3590. var programModels = formulaModels.Select((formulaModel) => formulaModel.EditProgram).ToList();
  3591. programModels = programModels.Where((model) => model != null).ToList();
  3592. var paramsSetPage = new ParamsSetPage(programModels);
  3593. var program = paramsSetPage.ShowDialog();
  3594. if (program.HasValue && program.Value == true) //更新图表
  3595. {
  3596. foreach (var displayFormula in displayFormulas) //设置公式参数
  3597. {
  3598. displayFormula.FormulaModel.SetParamValue();
  3599. displayFormula.Calculation();
  3600. }
  3601. this.Refresh(true);
  3602. }
  3603. }
  3604. }
  3605. }
  3606. /// <summary>
  3607. /// 打开公式编辑器
  3608. /// </summary>
  3609. public void OpenFormulEditer()
  3610. {
  3611. if (this._chartPanes != null && _chartPanes.Any())
  3612. {
  3613. var scaleLayers = new List<ScaleLayer>();
  3614. var displayFormulas = new List<DisplayFormula>();
  3615. foreach (var chartPane in _chartPanes)
  3616. {
  3617. if (chartPane.AllScaleLayers != null
  3618. && chartPane.AllScaleLayers.Any())
  3619. {
  3620. scaleLayers.AddRange(chartPane.AllScaleLayers);
  3621. }
  3622. }
  3623. foreach (var scaleLayer in scaleLayers)
  3624. {
  3625. if (scaleLayer.AllDisplayFormula != null && scaleLayer.AllDisplayFormula.Any())
  3626. {
  3627. displayFormulas.AddRange(scaleLayer.AllDisplayFormula);
  3628. }
  3629. }
  3630. if (displayFormulas.Any())
  3631. {
  3632. var formulaModels = displayFormulas.Select((displayFormula) => displayFormula.FormulaModel).ToList();
  3633. var programModels = formulaModels.Select((formulaModel) => formulaModel.EditProgram).ToList();
  3634. programModels = programModels.Where((model) => model != null).ToList();
  3635. var vm = new EditFormulaViewModel(programModels);
  3636. vm.OpenEditer();
  3637. foreach (var displayFormula in displayFormulas) //设置公式参数
  3638. {
  3639. displayFormula.FormulaModel.SetParamValue();
  3640. displayFormula.Calculation();
  3641. }
  3642. this.Refresh(true);
  3643. }
  3644. }
  3645. }
  3646. /// <summary>
  3647. /// 清除主图中指标
  3648. /// </summary>
  3649. public void ClearMainPaneIndicator()
  3650. {
  3651. var pane = GetMainChartPane();
  3652. if (pane != null)
  3653. {
  3654. foreach (var scaleLayer in pane.AllScaleLayers)
  3655. {
  3656. scaleLayer.ClearFormulas();
  3657. }
  3658. this.Refresh();
  3659. }
  3660. }
  3661. /// <summary>
  3662. /// 通过名称添加指标
  3663. /// </summary>
  3664. /// <param name="formulaName">指标名称</param>
  3665. /// <param name="refresh">是否立刻刷新图表</param>
  3666. public void AddIndicatorByName(string formulaName, bool refresh = true)
  3667. {
  3668. var formula = FormulaHelper.GetFormulaByName(formulaName, string.Empty);
  3669. if (formula != null)
  3670. {
  3671. var displayFormula = new DisplayFormula(formula, DataSet, this);
  3672. AddFormulaFromPane(displayFormula);
  3673. displayFormula.SetSource(DataSet, false);
  3674. if (refresh)
  3675. {
  3676. Refresh(true);
  3677. }
  3678. }
  3679. }
  3680. /// <summary>
  3681. /// 添加指标
  3682. /// </summary>
  3683. /// <param name="Program">指标配置</param>
  3684. public void AddIndicator(ProgramModel program)
  3685. {
  3686. var formulaModel = program.CovertToFormulaModel();
  3687. var displayFormula = new DisplayFormula(formulaModel, DataSet, this);
  3688. AddFormulaFromPane(displayFormula);
  3689. displayFormula.SetSource(DataSet, false);
  3690. Refresh(true);
  3691. }
  3692. #endregion
  3693. #endregion Methods
  3694. }
  3695. }