umychart.charttitle.wechat.js 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  1. /*
  2. copyright (c) 2018 jones
  3. http://www.apache.org/licenses/LICENSE-2.0
  4. 开源项目 https://github.com/jones2000/HQChart
  5. jones_2000@163.com
  6. 标题画法
  7. */
  8. import
  9. {
  10. JSCommonResource_Global_JSChartResource as g_JSChartResource,
  11. JSCommonResource_JSCHART_LANGUAGE_ID as JSCHART_LANGUAGE_ID,
  12. JSCommonResource_Global_JSChartLocalization as g_JSChartLocalization,
  13. } from './umychart.resource.wechat.js'
  14. import
  15. {
  16. JSCommon_ChartData as ChartData, JSCommon_HistoryData as HistoryData,
  17. JSCommon_SingleData as SingleData, JSCommon_MinuteData as MinuteData,
  18. JSCommon_CUSTOM_DAY_PERIOD_START as CUSTOM_DAY_PERIOD_START,
  19. JSCommon_CUSTOM_DAY_PERIOD_END as CUSTOM_DAY_PERIOD_END,
  20. JSCommon_CUSTOM_MINUTE_PERIOD_START as CUSTOM_MINUTE_PERIOD_START,
  21. JSCommon_CUSTOM_MINUTE_PERIOD_END as CUSTOM_MINUTE_PERIOD_END,
  22. JSCommon_CUSTOM_SECOND_PERIOD_START as CUSTOM_SECOND_PERIOD_START,
  23. JSCommon_CUSTOM_SECOND_PERIOD_END as CUSTOM_SECOND_PERIOD_END,
  24. } from "./umychart.data.wechat.js";
  25. import
  26. {
  27. JSCommonCoordinateData as JSCommonCoordinateData
  28. } from "./umychart.coordinatedata.wechat.js";
  29. import
  30. {
  31. JSCommon_KLINE_INFO_TYPE as KLINE_INFO_TYPE,
  32. } from "./umychart.klineinfo.wechat.js";
  33. import
  34. {
  35. JSCommonSplit_IFrameSplitOperator as IFrameSplitOperator,
  36. } from './umychart.framesplit.wechat.js'
  37. var MARKET_SUFFIX_NAME = JSCommonCoordinateData.MARKET_SUFFIX_NAME;
  38. //标题画法基类
  39. function IChartTitlePainting()
  40. {
  41. this.Frame;
  42. this.Data = new Array();
  43. this.Canvas; //画布
  44. this.IsDynamic = false; //是否是动态标题
  45. this.Position = 0; //标题显示位置 0 框架里的标题 1 框架上面
  46. this.CursorIndex; //数据索引
  47. this.Font = "12px 微软雅黑";//g_JSChartResource.DynamicTitleFont;//
  48. this.Title; //固定标题(可以为空)
  49. this.TitleColor = g_JSChartResource.DefaultTextColor;
  50. this.LanguageID = JSCHART_LANGUAGE_ID.LANGUAGE_CHINESE_ID;
  51. this.UpdateUICallback; //通知外面更新标题(老接口废弃)
  52. this.OnDrawEvent; //外部事件通知
  53. }
  54. var PERIOD_NAME = ["日线", "周线", "月线", "年线", "1分", "5分", "15分", "30分", "60分", "季线", "分笔", "2小时", "4小时", "", ""];
  55. var RIGHT_NAME = ['不复权', '前复权', '后复权'];
  56. //K线标题
  57. function DynamicKLineTitlePainting()
  58. {
  59. this.newMethod = IChartTitlePainting; //派生
  60. this.newMethod();
  61. delete this.newMethod;
  62. this.ClassName ='DynamicKLineTitlePainting';
  63. this.IsDynamic = true;
  64. this.IsShow = true; //是否显示
  65. this.LineCount = 1; //默认显示1行
  66. this.SpaceWidth = 1; //空格宽度
  67. this.Period; //周期
  68. this.UpColor = g_JSChartResource.UpTextColor;
  69. this.DownColor = g_JSChartResource.DownTextColor;
  70. this.UnchagneColor = g_JSChartResource.UnchagneTextColor;
  71. this.VolColor=g_JSChartResource.Title.VolColor;
  72. this.AmountColor=g_JSChartResource.Title.AmountColor;
  73. this.DateTimeColor=g_JSChartResource.Title.DateTimeColor;
  74. this.NameColor = g_JSChartResource.Title.NameColor;
  75. this.SettingColor=g_JSChartResource.Title.SettingColor; //周期 复权
  76. this.PositionColor=g_JSChartResource.Title.PositionColor; //持仓
  77. this.Symbol;
  78. this.UpperSymbol;
  79. this.Name;
  80. this.InfoData;
  81. this.InfoTextHeight = 15;
  82. this.InfoTextColor = g_JSChartResource.KLine.Info.TextColor;
  83. this.InfoTextBGColor = g_JSChartResource.KLine.Info.TextBGColor;
  84. this.IsShowName = true; //是否显示股票名称
  85. this.IsShowSettingInfo = true; //是否显示设置信息(周期 复权)
  86. this.GetCurrentKLineData = function () //获取当天鼠标位置所在的K线数据
  87. {
  88. if (this.CursorIndex == null || !this.Data) return null;
  89. if (this.Data.length <= 0) return null;
  90. var index = this.CursorIndex;
  91. index = parseInt(index.toFixed(0));
  92. var dataIndex = this.Data.DataOffset + index;
  93. if (dataIndex >= this.Data.Data.length) dataIndex = this.Data.Data.length - 1;
  94. if (dataIndex < 0) return null;
  95. var item = this.Data.Data[dataIndex];
  96. return item;
  97. }
  98. this.GetDataIndex=function()
  99. {
  100. if (this.CursorIndex == null || !this.Data) return null;
  101. if (this.Data.length <= 0) return null;
  102. var index = this.CursorIndex;
  103. index = parseInt(index.toFixed(0));
  104. var dataIndex = this.Data.DataOffset + index;
  105. if (dataIndex >= this.Data.Data.length) dataIndex = this.Data.Data.length - 1;
  106. if (dataIndex < 0) return null;
  107. return dataIndex;
  108. }
  109. this.SendUpdateUIMessage = function (funcName) //通知外面 标题变了
  110. {
  111. if (!this.UpdateUICallback) return;
  112. var sendData = {
  113. TitleName: 'K线标题', CallFunction: funcName, Stock: { Name: this.Name, Symbol: this.Symbol, },
  114. Rect:
  115. {
  116. Left: this.Frame.ChartBorder.GetLeft(), Right: this.Frame.ChartBorder.GetRight(),
  117. Top: 0, Bottom: this.Frame.ChartBorder.GetTop(),
  118. }
  119. };
  120. //有数据
  121. if (this.Data && this.Data.Data && this.Data.Data.length > 0) {
  122. let index = this.Data.Data.length - 1; //默认最后一天的数据
  123. if (this.CursorIndex) {
  124. let cursorIndex = Math.abs(this.CursorIndex - 0.5);
  125. cursorIndex = parseInt(cursorIndex.toFixed(0));
  126. index = this.Data.DataOffset + cursorIndex;
  127. if (index >= this.Data.Data.length) index = this.Data.Data.length - 1;
  128. }
  129. if (index >= 0) {
  130. let item = this.Data.Data[index];
  131. sendData.Stock.Data =
  132. {
  133. Date: item.Date,
  134. YClose: item.YClose, Open: item.Open, High: item.High, Low: item.Low, Close: item.Close,
  135. Vol: item.Vol, Amount: item.Amount
  136. }
  137. if (item.Time) sendData.Stock.Time = item.Time; //分钟K线才有时间
  138. }
  139. if (this.Data.Period != null) sendData.Stock.PeriodName = this.GetPeriodName(this.Data.Period); //周期名字
  140. if (this.Data.Right != null) sendData.Stock.RightName = RIGHT_NAME[this.Data.Right]; //复权名字
  141. }
  142. //console.log('[DynamicKLineTitlePainting::SendUpdateUIMessage', sendData);
  143. this.UpdateUICallback(sendData);
  144. }
  145. this.GetPeriodName = function (period)
  146. {
  147. var name = '';
  148. if (period > CUSTOM_MINUTE_PERIOD_START && period <= CUSTOM_MINUTE_PERIOD_END)
  149. name = (period - CUSTOM_MINUTE_PERIOD_START) + g_JSChartLocalization.GetText('自定义分钟', this.LanguageID);
  150. else if (period > CUSTOM_DAY_PERIOD_START && period <= CUSTOM_DAY_PERIOD_END)
  151. name = (period - CUSTOM_DAY_PERIOD_START) + g_JSChartLocalization.GetText('自定义日线',this.LanguageID);
  152. else if (period > CUSTOM_SECOND_PERIOD_START && period <= CUSTOM_SECOND_PERIOD_END)
  153. name = (period - CUSTOM_SECOND_PERIOD_START) + g_JSChartLocalization.GetText('自定义秒', this.LanguageID);
  154. else
  155. name = g_JSChartLocalization.GetText(ChartData.GetPeriodName(period), this.LanguageID);
  156. return name;
  157. }
  158. this.GetRightName = function (rightID, periodID)
  159. {
  160. //分钟K线没有复权
  161. if (ChartData.IsMinutePeriod(periodID, true) || ChartData.IsSecondPeriod(periodID))
  162. return null;
  163. if (MARKET_SUFFIX_NAME.IsSHSZStockA(this.UpperSymbol)) //A股有复权
  164. {
  165. var rightName = RIGHT_NAME[rightID];
  166. return rightName
  167. }
  168. return null;
  169. }
  170. this.FullDraw=function()
  171. {
  172. if (!this.IsShow) return;
  173. this.UpperSymbol=this.Symbol ? this.Symbol.toUpperCase():'';
  174. if (this.CursorIndex == null || !this.Data || this.Data.length <= 0)
  175. {
  176. this.OnDrawEventCallback(null, 'DynamicKLineTitlePainting::FullDraw');
  177. return;
  178. }
  179. this.SpaceWidth = this.Canvas.measureText(' ').width;
  180. var index = this.CursorIndex;
  181. index = parseInt(index.toFixed(0));
  182. var dataIndex = this.Data.DataOffset + index;
  183. if (dataIndex >= this.Data.Data.length) dataIndex=-1;
  184. if (dataIndex < 0)
  185. {
  186. this.OnDrawEventCallback(null, 'DynamicKLineTitlePainting::FullDraw');
  187. return;
  188. }
  189. var item = this.Data.Data[dataIndex];
  190. this.OnDrawEventCallback(item, 'DynamicKLineTitlePainting::FullDraw');
  191. if (this.Frame.IsHScreen === true)
  192. {
  193. this.Canvas.save();
  194. if (this.LineCount > 1) this.DrawMulitLine(item);
  195. else this.DrawSingleLine(item,true);
  196. this.Canvas.restore();
  197. if (!item.Time && item.Date && this.InfoData) this.HSCreenKLineInfoDraw(item.Date);
  198. }
  199. else
  200. {
  201. if (this.LineCount > 1) this.DrawMulitLine(item);
  202. else this.DrawSingleLine(item, true);
  203. if (!item.Time && item.Date && this.InfoData) this.KLineInfoDraw(item.Date);
  204. }
  205. }
  206. this.DrawTitle = function ()
  207. {
  208. this.UpperSymbol=this.Symbol ? this.Symbol.toUpperCase():'';
  209. this.SendUpdateUIMessage('DrawTitle');
  210. this.OnDrawEventCallback(null, 'DynamicKLineTitlePainting::DrawTitle');
  211. if (!this.IsShow) return;
  212. if (!this.IsShowName && !this.IsShowSettingInfo) return;
  213. if (this.LineCount > 1) return;
  214. if (this.Frame.IsHScreen === true)
  215. {
  216. this.Canvas.save();
  217. this.HScreenDrawTitle();
  218. this.Canvas.restore();
  219. return;
  220. }
  221. var left = this.Frame.ChartBorder.GetLeft();
  222. var bottom = this.Frame.ChartBorder.GetTop();
  223. var right = this.Frame.ChartBorder.GetRight();
  224. if (bottom < 5) return;
  225. this.Canvas.textAlign = "left";
  226. this.Canvas.textBaseline = "bottom";
  227. this.Canvas.font = this.Font;
  228. var position = { Left: left, Bottom: bottom, IsHScreen: false };
  229. if (this.IsShowName && this.Name)
  230. {
  231. if (!this.DrawKLineText(this.Name, this.NameColor, position)) return;
  232. }
  233. if (this.IsShowSettingInfo && this.Data.Period != null && this.Data.Right != null)
  234. {
  235. var periodName = this.GetPeriodName(this.Data.Period);
  236. var rightName = this.GetRightName(this.Data.Right,this.Data.Period);
  237. var text = "(" + periodName + ")";
  238. if (rightName) text = "(" + periodName + " " + rightName + ")";
  239. if (!this.DrawKLineText(text, this.SettingColor, position)) return;
  240. }
  241. }
  242. this.HScreenDrawTitle = function ()
  243. {
  244. var xText = this.Frame.ChartBorder.GetRight();
  245. var yText = this.Frame.ChartBorder.GetTop();
  246. var right = this.Frame.ChartBorder.GetHeight();
  247. if (this.Frame.ChartBorder.Right < 10) return;
  248. this.Canvas.translate(xText, yText);
  249. this.Canvas.rotate(90 * Math.PI / 180);
  250. this.Canvas.textAlign = "left";
  251. this.Canvas.textBaseline = "bottom";
  252. this.Canvas.font = this.Font;
  253. var left = 2;
  254. var bottom = -2;
  255. var position = { Left: left, Bottom: bottom, IsHScreen: false };
  256. if (this.IsShowName && this.Name)
  257. {
  258. if (!this.DrawKLineText(this.Name, this.NameColor, position)) return;
  259. }
  260. if (this.IsShowSettingInfo && this.Data.Period != null && this.Data.Right != null)
  261. {
  262. var periodName = this.GetPeriodName(this.Data.Period);
  263. var rightName = this.GetRightName(this.Data.Right,this.Data.Period);
  264. var text = "(" + periodName + ")";
  265. if (rightName) text = "(" + periodName + " " + rightName + ")";
  266. if (!this.DrawKLineText(text, this.SettingColor, position)) return;
  267. }
  268. }
  269. this.DrawMulitLine = function (item) //画多行
  270. {
  271. var isHScreen = this.Frame.IsHScreen === true;
  272. var leftSpace = 1;
  273. var bottomSpace = 1;
  274. var left = this.Frame.ChartBorder.GetLeft() + leftSpace;;
  275. var width = this.Frame.ChartBorder.GetWidth();
  276. var height = this.Frame.ChartBorder.GetTop();
  277. var defaultfloatPrecision = JSCommonCoordinateData.GetfloatPrecision(this.Symbol);//价格小数位数
  278. if (isHScreen)
  279. {
  280. var left = leftSpace;;
  281. var width = this.Frame.ChartBorder.GetHeight();
  282. var height = this.Frame.ChartBorder.Right;
  283. var xText = this.Frame.ChartBorder.GetChartWidth();
  284. var yText = this.Frame.ChartBorder.GetTop();
  285. this.Canvas.translate(xText, yText);
  286. this.Canvas.rotate(90 * Math.PI / 180);
  287. }
  288. var itemHeight = (height - bottomSpace) / this.LineCount;
  289. var itemWidth = (width - leftSpace) / 4;
  290. var bottom = itemHeight;
  291. this.Canvas.textAlign = "left";
  292. this.Canvas.textBaseline = "bottom";
  293. this.Canvas.font = this.Font;
  294. var text = IFrameSplitOperator.FormatDateString(item.Date);
  295. this.Canvas.setFillStyle(this.DateTimeColor);
  296. this.Canvas.fillText(text, left, bottom, itemWidth);
  297. left += itemWidth;
  298. this.Canvas.textAlign = "left";
  299. this.Canvas.setFillStyle(this.GetColor(item.Open, item.YClose));
  300. var text = g_JSChartLocalization.GetText('Tooltip-Open', this.LanguageID) + item.Open.toFixed(defaultfloatPrecision);
  301. this.Canvas.fillText(text, left, bottom, itemWidth);
  302. left += itemWidth;
  303. this.Canvas.setFillStyle(this.GetColor(item.High, item.YClose));
  304. var text = g_JSChartLocalization.GetText('Tooltip-High', this.LanguageID) + item.High.toFixed(defaultfloatPrecision);
  305. this.Canvas.fillText(text, left, bottom, itemWidth);
  306. left += itemWidth;
  307. var value = (item.Close - item.YClose) / item.YClose * 100;
  308. this.Canvas.setFillStyle(this.GetColor(value, 0));
  309. var text = g_JSChartLocalization.GetText('Tooltip-Increase', this.LanguageID) + value.toFixed(2) + '%';
  310. this.Canvas.fillText(text, left, bottom, itemWidth);
  311. left += itemWidth;
  312. bottom += itemHeight; //换行
  313. var left = this.Frame.ChartBorder.GetLeft() + leftSpace;
  314. if (isHScreen) left = leftSpace;
  315. if (ChartData.IsMinutePeriod(this.Period, true) && item.Time)
  316. {
  317. this.Canvas.setFillStyle(this.DateTimeColor);
  318. var text = IFrameSplitOperator.FormatTimeString(item.Time);
  319. this.Canvas.fillText(text, left, bottom, itemWidth);
  320. }
  321. else if (ChartData.IsSecondPeriod(this.Period) && item.Time)
  322. {
  323. this.Canvas.setFillStyle(this.SettingColor);
  324. var text = IFrameSplitOperator.FormatTimeString(item.Time, 'HH:MM:SS');
  325. this.Canvas.fillText(text, left, bottom, itemWidth);
  326. }
  327. left += itemWidth;
  328. this.Canvas.setFillStyle(this.GetColor(item.Close, item.YClose));
  329. var text = g_JSChartLocalization.GetText('Tooltip-Close', this.LanguageID) + item.Close.toFixed(defaultfloatPrecision);
  330. this.Canvas.fillText(text, left, bottom, itemWidth);
  331. left += itemWidth;
  332. this.Canvas.setFillStyle(this.GetColor(item.Low, item.YClose));
  333. var text = g_JSChartLocalization.GetText('Tooltip-Low', this.LanguageID) + item.Low.toFixed(defaultfloatPrecision);
  334. this.Canvas.fillText(text, left, bottom, itemWidth);
  335. left += itemWidth;
  336. this.Canvas.setFillStyle(this.AmountColor);
  337. var text = g_JSChartLocalization.GetText('Tooltip-Amount', this.LanguageID) + IFrameSplitOperator.FormatValueString(item.Amount, 2, this.LanguageID);
  338. this.Canvas.fillText(text, left, bottom, itemWidth);
  339. left += itemWidth;
  340. }
  341. this.DrawSingleLine = function (item,bDrawTitle) //画单行
  342. {
  343. var isHScreen = this.Frame.IsHScreen === true;
  344. var left = this.Frame.ChartBorder.GetLeft();
  345. //var bottom=this.Frame.ChartBorder.GetTop()-this.Frame.ChartBorder.Top/2;
  346. var bottom = this.Frame.ChartBorder.GetTop();
  347. //#ifdef MP-TOUTIAO
  348. bottom = bottom-10;
  349. //#endif
  350. var right = this.Frame.ChartBorder.GetRight();
  351. var defaultfloatPrecision = JSCommonCoordinateData.GetfloatPrecision(this.Symbol);//价格小数位数
  352. if (isHScreen)
  353. {
  354. right = this.Frame.ChartBorder.GetHeight();
  355. if (this.Frame.ChartBorder.Right < 5) return;
  356. left = 2;
  357. bottom = -2;
  358. var xText = this.Frame.ChartBorder.GetRight();
  359. var yText = this.Frame.ChartBorder.GetTop();
  360. this.Canvas.translate(xText, yText);
  361. this.Canvas.rotate(90 * Math.PI / 180);
  362. }
  363. else
  364. {
  365. if (bottom < 5) return;
  366. }
  367. this.Canvas.textAlign = "left";
  368. this.Canvas.textBaseline = "bottom";
  369. this.Canvas.font = this.Font;
  370. var position = { Left: left, Bottom: bottom, IsHScreen: isHScreen };
  371. if (this.IsShowName) //名称
  372. {
  373. if (!this.DrawKLineText(this.Name, this.NameColor, position, bDrawTitle==true)) return;
  374. }
  375. if (this.IsShowSettingInfo) //周期 复权信息
  376. {
  377. var periodName = this.GetPeriodName(this.Data.Period);
  378. var rightName = this.GetRightName(this.Data.Right,this.Data.Period);
  379. var text = "(" + periodName + ")";
  380. if (rightName) text = "(" + periodName + " " + rightName + ")";
  381. if (!this.DrawKLineText(text, this.SettingColor, position, bDrawTitle==true)) return;
  382. }
  383. var text = IFrameSplitOperator.FormatDateString(item.Date); //日期
  384. if (!this.DrawKLineText(text, this.DateTimeColor, position)) return;
  385. //时间
  386. if (ChartData.IsMinutePeriod(this.Period, true) && IFrameSplitOperator.IsNumber(item.Time))
  387. {
  388. var text = IFrameSplitOperator.FormatTimeString(item.Time);
  389. if (!this.DrawKLineText(text, this.DateTimeColor, position)) return;
  390. }
  391. else if (ChartData.IsSecondPeriod(this.Period) && IFrameSplitOperator.IsNumber(item.Time))
  392. {
  393. var text = IFrameSplitOperator.FormatTimeString(item.Time, "HH:MM:SS");
  394. if (!this.DrawKLineText(text, this.DateTimeColor, position)) return;
  395. }
  396. var color = this.GetColor(item.Open, item.YClose);
  397. var text = g_JSChartLocalization.GetText('KTitle-Open', this.LanguageID) + item.Open.toFixed(defaultfloatPrecision);
  398. if (!this.DrawKLineText(text, color, position)) return;
  399. var color = this.GetColor(item.High, item.YClose);
  400. var text = g_JSChartLocalization.GetText('KTitle-High', this.LanguageID) + item.High.toFixed(defaultfloatPrecision);
  401. if (!this.DrawKLineText(text, color, position)) return;
  402. var color = this.GetColor(item.Low, item.YClose);
  403. var text = g_JSChartLocalization.GetText('KTitle-Low', this.LanguageID) + item.Low.toFixed(defaultfloatPrecision);
  404. if (!this.DrawKLineText(text, color, position)) return;
  405. var color = this.GetColor(item.Close, item.YClose);
  406. var text = g_JSChartLocalization.GetText('KTitle-Close', this.LanguageID) + item.Close.toFixed(defaultfloatPrecision);
  407. if (!this.DrawKLineText(text, color, position)) return;
  408. if (IFrameSplitOperator.IsNumber(item.Vol))
  409. {
  410. var text = g_JSChartLocalization.GetText('KTitle-Vol', this.LanguageID) + IFrameSplitOperator.FormatValueString(item.Vol, 2);
  411. if (!this.DrawKLineText(text, this.VolColor, position)) return;
  412. }
  413. if (IFrameSplitOperator.IsNumber(item.Amount))
  414. {
  415. var text = g_JSChartLocalization.GetText('KTitle-Amount', this.LanguageID) + IFrameSplitOperator.FormatValueString(item.Amount, 2);
  416. if (!this.DrawKLineText(text, this.AmountColor, position)) return;
  417. }
  418. if (MARKET_SUFFIX_NAME.IsChinaFutures(this.UpperSymbol) && IFrameSplitOperator.IsNumber(item.Position))
  419. {
  420. var text = g_JSChartLocalization.GetText('KTitle-Position', this.LanguageID) + IFrameSplitOperator.FormatValueString(item.Position, 2);
  421. if (!this.DrawKLineText(text, this.PositionColor, position)) return;
  422. }
  423. }
  424. this.OnDrawEventCallback = function (drawData, explain)
  425. {
  426. if (!this.OnDrawEvent || !this.OnDrawEvent.Callback) return;
  427. var data = { Draw: drawData, Name: this.ClassName, Explain: explain };
  428. if (this.Data && this.Data.Data)
  429. {
  430. if (IFrameSplitOperator.IsNumber(this.CursorIndex))
  431. {
  432. var index = this.CursorIndex;
  433. index = parseInt(index.toFixed(0));
  434. var dataIndex = this.Data.DataOffset + index;
  435. }
  436. else
  437. {
  438. dataIndex=this.Data.Data.length-1;
  439. }
  440. var dataCount=this.Data.Data.length;
  441. data.DataIndex=dataIndex;
  442. data.DataCount=dataCount;
  443. }
  444. this.OnDrawEvent.Callback(this.OnDrawEvent, data, this);
  445. }
  446. this.Draw = function ()
  447. {
  448. this.UpperSymbol = this.Symbol ? this.Symbol.toUpperCase() : '';
  449. this.SendUpdateUIMessage('Draw');
  450. if (!this.IsShow) return;
  451. if (this.CursorIndex == null || !this.Data || this.Data.length <= 0)
  452. {
  453. this.OnDrawEventCallback(null, 'DynamicKLineTitlePainting::Draw');
  454. return;
  455. }
  456. this.SpaceWidth = this.Canvas.measureText(' ').width;
  457. var index = this.CursorIndex;
  458. index = parseInt(index.toFixed(0));
  459. var dataIndex = this.Data.DataOffset + index;
  460. if (dataIndex >= this.Data.Data.length) dataIndex = this.Data.Data.length - 1;
  461. if (dataIndex < 0)
  462. {
  463. this.OnDrawEventCallback(null, 'DynamicKLineTitlePainting::Draw');
  464. return;
  465. }
  466. var item = this.Data.Data[dataIndex];
  467. this.OnDrawEventCallback(item, 'DynamicKLineTitlePainting::Draw');
  468. if (this.Frame.IsHScreen === true)
  469. {
  470. this.Canvas.save();
  471. if (this.LineCount > 1) this.DrawMulitLine(item);
  472. else this.DrawSingleLine(item);
  473. this.Canvas.restore();
  474. if (!item.Time && item.Date && this.InfoData) this.HSCreenKLineInfoDraw(item.Date);
  475. }
  476. else
  477. {
  478. if (this.LineCount > 1) this.DrawMulitLine(item);
  479. else this.DrawSingleLine(item);
  480. if (!item.Time && item.Date && this.InfoData) this.KLineInfoDraw(item.Date);
  481. }
  482. }
  483. this.KLineInfoDraw = function (date) {
  484. var info = this.InfoData.get(date.toString());
  485. if (!info) return;
  486. var invesotrCount = 0; //互动易统计
  487. var researchCouunt = 0;
  488. var reportCount = 0;
  489. var blockTradeCount = 0; //大宗交易次数
  490. var tradeDetailCount = 0; //龙虎榜上榜次数
  491. var policyData = null;
  492. var reportTitle = null, pforecastTitle = null;
  493. //console.log(info);
  494. for (var i in info.Data) {
  495. var item = info.Data[i];
  496. switch (item.InfoType) {
  497. case KLINE_INFO_TYPE.INVESTOR:
  498. ++invesotrCount;
  499. break;
  500. case KLINE_INFO_TYPE.PFORECAST:
  501. pforecastTitle = item.Title;
  502. break;
  503. case KLINE_INFO_TYPE.ANNOUNCEMENT:
  504. ++reportCount;
  505. break;
  506. case KLINE_INFO_TYPE.ANNOUNCEMENT_QUARTER_1:
  507. case KLINE_INFO_TYPE.ANNOUNCEMENT_QUARTER_2:
  508. case KLINE_INFO_TYPE.ANNOUNCEMENT_QUARTER_3:
  509. case KLINE_INFO_TYPE.ANNOUNCEMENT_QUARTER_4:
  510. reportTitle = item.Title;
  511. break;
  512. case KLINE_INFO_TYPE.RESEARCH:
  513. ++researchCouunt;
  514. break;
  515. case KLINE_INFO_TYPE.BLOCKTRADING:
  516. ++blockTradeCount;
  517. break;
  518. case KLINE_INFO_TYPE.TRADEDETAIL:
  519. ++tradeDetailCount;
  520. break;
  521. case KLINE_INFO_TYPE.POLICY:
  522. policyData = item;
  523. break;
  524. }
  525. }
  526. var isHScreen = (this.Frame.IsHScreen === true);
  527. var right = this.Frame.ChartBorder.GetRight() - 4;
  528. var top = this.Frame.ChartBorder.GetTopEx();
  529. if (isHScreen) {
  530. right = this.Frame.ChartBorder.GetBottom() - 4;
  531. top = this.Frame.ChartBorder.GetRightEx();
  532. this.Canvas.translate(top, right);
  533. this.Canvas.rotate(90 * Math.PI / 180);
  534. right = 0; top = 0;
  535. }
  536. this.Canvas.font = this.Font;
  537. var aryTitle = [];
  538. var position = { Top: top, Right: right, IsHScreen: isHScreen };
  539. aryTitle.push(IFrameSplitOperator.FormatDateString(date));
  540. if (reportTitle) aryTitle.push(reportTitle); //季报
  541. if (pforecastTitle) aryTitle.push(pforecastTitle); //业绩预告
  542. if (reportCount > 0) aryTitle.push('公告数量:' + reportCount);
  543. if (researchCouunt > 0) aryTitle.push('机构调研次数:' + researchCouunt);
  544. if (tradeDetailCount > 0) aryTitle.push('龙虎榜上榜次数:' + tradeDetailCount);
  545. if (invesotrCount > 0) aryTitle.push('互动易数量:' + invesotrCount);
  546. if (blockTradeCount > 0) aryTitle.push('大宗交易次数:' + blockTradeCount);
  547. if (policyData) //策略选股
  548. {
  549. for (let i in policyData.ExtendData) //显示满足的策略
  550. {
  551. aryTitle.push(policyData.ExtendData[i].Name);
  552. }
  553. }
  554. var maxWidth = 0, textBGHeight = 0;
  555. for (let i in aryTitle) {
  556. var item = aryTitle[i];
  557. var textWidth = this.Canvas.measureText(item).width + 2; //后空2个像素
  558. if (maxWidth < textWidth) maxWidth = textWidth;
  559. textBGHeight += this.InfoTextHeight;
  560. }
  561. this.Canvas.setFillStyle(this.InfoTextBGColor);
  562. if (isHScreen) this.Canvas.fillRect(position.Right - maxWidth, position.Top, maxWidth + 2, textBGHeight + 2);
  563. else this.Canvas.fillRect(position.Right - maxWidth, position.Top, maxWidth + 2, textBGHeight + 2);
  564. for (let i in aryTitle) {
  565. var item = aryTitle[i];
  566. this.DrawInfoText(item, position);
  567. }
  568. }
  569. this.HSCreenKLineInfoDraw = function (date) {
  570. this.Canvas.save();
  571. this.KLineInfoDraw(date);
  572. this.Canvas.restore();
  573. }
  574. this.GetColor = function (price, yclse) {
  575. if (price > yclse) return this.UpColor;
  576. else if (price < yclse) return this.DownColor;
  577. else return this.UnchagneColor;
  578. }
  579. this.DrawInfoText = function (title, position) {
  580. if (!title) return true;
  581. this.Canvas.textAlign = "right";
  582. this.Canvas.textBaseline = "top";
  583. this.Canvas.setFillStyle(this.InfoTextColor);
  584. this.Canvas.fillText(title, position.Right, position.Top);
  585. position.Top += this.InfoTextHeight;
  586. return true;
  587. }
  588. this.DrawKLineText = function (title, color, position, isShow)
  589. {
  590. if (!title) return true;
  591. var isHScreen = this.Frame.IsHScreen === true;
  592. var right = this.Frame.ChartBorder.GetRight();
  593. if (isHScreen) right = this.Frame.ChartBorder.GetHeight();
  594. this.Canvas.setFillStyle(color);
  595. var textWidth = this.Canvas.measureText(title).width;
  596. if(title.length>2)
  597. this.Canvas.font="12px Arial";
  598. else
  599. this.Canvas.font="20px Arial";
  600. //console.log(this.Canvas.font);
  601. if (position.Left + textWidth > right) return false;
  602. if (!(isShow === false)) this.Canvas.fillText(title, position.Left, position.Bottom, textWidth);
  603. position.Left += textWidth + this.SpaceWidth;
  604. return true;
  605. }
  606. }
  607. //分时图标题
  608. function DynamicMinuteTitlePainting()
  609. {
  610. this.newMethod = DynamicKLineTitlePainting; //派生
  611. this.newMethod();
  612. delete this.newMethod;
  613. this.YClose;
  614. this.IsShowDate = false; //标题是否显示日期
  615. this.IsShowName = true; //标题是否显示股票名字
  616. this.Symbol;
  617. this.UpperSymbol;
  618. this.LastShowData; //保存最后显示的数据 给tooltip用
  619. this.ClassName ='DynamicMinuteTitlePainting';
  620. this.SpaceWidth = 2;
  621. this.IsShowAveragePrice=true; //是否显示均线价格
  622. this.GetCurrentKLineData = function () //获取当天鼠标位置所在的K线数据
  623. {
  624. if (this.LastShowData) return this.LastShowData;
  625. if (this.CursorIndex == null || !this.Data) return null;
  626. if (this.Data.length <= 0) return null;
  627. var index = Math.abs(this.CursorIndex);
  628. index = parseInt(index.toFixed(0));
  629. var dataIndex = this.Data.DataOffset + index;
  630. if (dataIndex >= this.Data.Data.length) dataIndex = this.Data.Data.length - 1;
  631. if (dataIndex < 0) return null;
  632. var item = this.Data.Data[dataIndex];
  633. return item;
  634. }
  635. this.SendUpdateUIMessage = function (funcName) //通知外面 标题变了
  636. {
  637. if (!this.UpdateUICallback) return;
  638. var sendData =
  639. {
  640. TitleName: '分钟标题', CallFunction: funcName, Stock: { Name: this.Name, Symbol: this.Symbol, },
  641. Rect:
  642. {
  643. Left: this.Frame.ChartBorder.GetLeft(), Right: this.Frame.ChartBorder.GetRight(),
  644. Top: 0, Bottom: this.Frame.ChartBorder.GetTop(),
  645. }
  646. };
  647. //有数据
  648. if (this.Data && this.Data.Data && this.Data.Data.length > 0) {
  649. let index = this.Data.Data.length - 1; //默认最后1分钟的数据
  650. if (this.CursorIndex) {
  651. let cursorIndex = Math.abs(this.CursorIndex - 0.5);
  652. cursorIndex = parseInt(cursorIndex.toFixed(0));
  653. index = this.Data.DataOffset + cursorIndex;
  654. if (index >= this.Data.Data.length) index = this.Data.Data.length - 1;
  655. }
  656. if (index >= 0) {
  657. let item = this.Data.Data[index];
  658. this.LastShowData = item;
  659. sendData.Stock.Data =
  660. {
  661. Time: item.Time, Close: item.Close, AvPrice: item.AvPrice,
  662. Vol: item.Vol, Amount: item.Amount
  663. }
  664. if (item.Time) sendData.Stock.Time = item.Time; //分钟K线才有时间
  665. }
  666. }
  667. this.UpdateUICallback(sendData);
  668. }
  669. this.DrawTitle = function ()
  670. {
  671. this.UpperSymbol = this.Symbol ? this.Symbol.toUpperCase() : '';
  672. this.SendUpdateUIMessage('DrawTitle');
  673. this.OnDrawEventCallback(null, "DynamicMinuteTitlePainting::DrawTitle");
  674. }
  675. this.GetDecimal = function (symbol)
  676. {
  677. return JSCommonCoordinateData.GetfloatPrecision(symbol);//价格小数位数
  678. }
  679. this.DrawMulitLine = function (item) //画多行
  680. {
  681. var leftSpace = 5;
  682. var bottomSpace = 2;
  683. var left = this.Frame.ChartBorder.GetLeft() + leftSpace;;
  684. var right = this.Frame.ChartBorder.GetRight();
  685. var width = this.Frame.ChartBorder.GetWidth();
  686. var height = this.Frame.ChartBorder.GetTop();
  687. var defaultfloatPrecision = this.GetDecimal(this.Symbol); //价格小数位数
  688. var itemHeight = (height - bottomSpace) / this.LineCount;
  689. var bottom = itemHeight;
  690. this.Canvas.textAlign = "left";
  691. this.Canvas.textBaseline = "bottom";
  692. this.Canvas.font = this.Font;
  693. this.Canvas.setFillStyle(this.UnchagneColor);
  694. this.Canvas.setFillStyle(this.UnchagneColor);
  695. var text = IFrameSplitOperator.FormatDateTimeString(item.DateTime, this.IsShowDate ? 'YYYY-MM-DD' : 'HH-MM');
  696. var timeWidth = this.Canvas.measureText(text).width + 5; //后空5个像素
  697. this.Canvas.fillText(text, left, bottom, timeWidth);
  698. if (this.IsShowDate) {
  699. var text = IFrameSplitOperator.FormatDateTimeString(item.DateTime, 'HH-MM');
  700. this.Canvas.fillText(text, left, bottom + itemHeight, timeWidth);
  701. }
  702. var itemWidth = (width - leftSpace - timeWidth) / 2;
  703. left += timeWidth;
  704. if (item.Close != null) {
  705. this.Canvas.setFillStyle(this.GetColor(item.Close, this.YClose));
  706. var text = g_JSChartLocalization.GetText('Tooltip-Price', this.LanguageID) + item.Close.toFixed(defaultfloatPrecision);
  707. this.Canvas.fillText(text, left, bottom, itemWidth);
  708. left += itemWidth;
  709. }
  710. if (item.Increase != null) {
  711. this.Canvas.setFillStyle(this.GetColor(item.Increase, 0));
  712. var text = g_JSChartLocalization.GetText('Tooltip-Increase', this.LanguageID) + item.Increase.toFixed(2) + '%';
  713. this.Canvas.fillText(text, left, bottom, itemWidth);
  714. left += itemWidth;
  715. }
  716. bottom += itemHeight; //换行
  717. var left = this.Frame.ChartBorder.GetLeft() + leftSpace + timeWidth;
  718. this.Canvas.setFillStyle(this.VolColor);
  719. var text = g_JSChartLocalization.GetText('Tooltip-Vol', this.LanguageID) + IFrameSplitOperator.FormatValueString(item.Vol, 2, this.LanguageID);
  720. this.Canvas.fillText(text, left, bottom, itemWidth);
  721. left += itemWidth;
  722. this.Canvas.setFillStyle(this.AmountColor);
  723. var text = g_JSChartLocalization.GetText('Tooltip-Amount', this.LanguageID) + IFrameSplitOperator.FormatValueString(item.Amount, 2, this.LanguageID);
  724. this.Canvas.fillText(text, left, bottom, itemWidth);
  725. left += itemWidth;
  726. }
  727. this.DrawItem = function (item)
  728. {
  729. var isHScreen = this.Frame.IsHScreen === true;
  730. var left = this.Frame.ChartBorder.GetLeft();;
  731. var bottom = this.Frame.ChartBorder.GetTop() - this.Frame.ChartBorder.Top / 2;
  732. var right = this.Frame.ChartBorder.GetRight();
  733. var defaultfloatPrecision = this.GetDecimal(this.Symbol); //价格小数位数
  734. if (isHScreen)
  735. {
  736. if (this.Frame.ChartBorder.Right < 5) return;
  737. var left = 2;
  738. var bottom = this.Frame.ChartBorder.Right / 2; //上下居中显示
  739. var right = this.Frame.ChartBorder.GetHeight();
  740. var xText = this.Frame.ChartBorder.GetChartWidth();
  741. var yText = this.Frame.ChartBorder.GetTop();
  742. this.Canvas.translate(xText, yText);
  743. this.Canvas.rotate(90 * Math.PI / 180);
  744. }
  745. else
  746. {
  747. if (bottom < 5) return;
  748. }
  749. this.Canvas.textAlign = "left";
  750. this.Canvas.textBaseline = "middle";
  751. this.Canvas.font = this.Font;
  752. var position = { Left: left, Bottom: bottom, IsHScreen: isHScreen };
  753. if (this.IsShowName)
  754. {
  755. if (!this.DrawMinuteText(this.Name, this.NameColor, position, true)) return;
  756. }
  757. this.Canvas.setFillStyle(this.UnchagneColor);
  758. var text = IFrameSplitOperator.FormatDateTimeString(item.DateTime, this.IsShowDate ? 'YYYY-MM-DD HH-MM' : 'HH-MM');
  759. if (!this.DrawMinuteText(text, this.DateTimeColor, position)) return;
  760. if (IFrameSplitOperator.IsNumber(item.Close))
  761. {
  762. var color = this.GetColor(item.Close, this.YClose);
  763. var text = g_JSChartLocalization.GetText('MTitle-Close', this.LanguageID) + item.Close.toFixed(defaultfloatPrecision);
  764. if (!this.DrawMinuteText(text, color, position)) return;
  765. }
  766. if (IFrameSplitOperator.IsNumber(item.Increase))
  767. {
  768. var color = this.GetColor(item.Increase, 0);
  769. var text = g_JSChartLocalization.GetText('MTitle-Increase', this.LanguageID) + item.Increase.toFixed(2) + '%';
  770. if (!this.DrawMinuteText(text, color, position)) return;
  771. }
  772. if (IFrameSplitOperator.IsNumber(item.AvPrice) && this.IsShowAveragePrice==true)
  773. {
  774. var color = this.GetColor(item.AvPrice, this.YClose);
  775. var text = g_JSChartLocalization.GetText('MTitle-AvPrice', this.LanguageID) + item.AvPrice.toFixed(defaultfloatPrecision);
  776. if (!this.DrawMinuteText(text, color, position)) return;
  777. }
  778. if (IFrameSplitOperator.IsNumber(item.Vol))
  779. {
  780. var text = g_JSChartLocalization.GetText('MTitle-Vol', this.LanguageID) + IFrameSplitOperator.FormatValueString(item.Vol, 2, this.LanguageID);
  781. if (!this.DrawMinuteText(text, this.VolColor, position)) return;
  782. }
  783. if (IFrameSplitOperator.IsNumber(item.Amount))
  784. {
  785. var text = g_JSChartLocalization.GetText('MTitle-Amount', this.LanguageID) + IFrameSplitOperator.FormatValueString(item.Amount, 2, this.LanguageID);
  786. if (!this.DrawMinuteText(text, this.AmountColor, position)) return;
  787. }
  788. if (MARKET_SUFFIX_NAME.IsChinaFutures(this.UpperSymbol) && IFrameSplitOperator.IsNumber(item.Position))
  789. {
  790. var text = g_JSChartLocalization.GetText('MTitle-Position', this.LanguageID) + IFrameSplitOperator.FormatValueString(item.Position, 2, this.LanguageID);
  791. if (!this.DrawMinuteText(text, this.VolColor, position)) return;
  792. }
  793. }
  794. this.FullDraw=function()
  795. {
  796. this.Draw();
  797. }
  798. this.Draw = function ()
  799. {
  800. this.UpperSymbol = this.Symbol ? this.Symbol.toUpperCase() : '';
  801. this.LastShowData = null;
  802. this.SendUpdateUIMessage('Draw');
  803. if (!this.IsShow) return;
  804. if (this.CursorIndex == null || !this.Data || !this.Data.Data || this.Data.Data.length <= 0)
  805. {
  806. this.OnDrawEventCallback(null,"DynamicMinuteTitlePainting::Draw");
  807. return;
  808. }
  809. var index = this.CursorIndex;
  810. index = parseInt(index.toFixed(0));
  811. var dataIndex = index + this.Data.DataOffset;
  812. if (dataIndex >= this.Data.Data.length) dataIndex = this.Data.Data.length - 1;
  813. var item = this.Data.Data[dataIndex];
  814. this.LastShowData = item;
  815. this.OnDrawEventCallback(item, "DynamicMinuteTitlePainting::Draw");
  816. if (this.LineCount > 1 && !(this.Frame.IsHScreen === true))
  817. {
  818. this.DrawMulitLine(item);
  819. return;
  820. }
  821. this.Canvas.save();
  822. this.DrawItem(item);
  823. this.Canvas.restore();
  824. }
  825. this.DrawMinuteText = function (title, color, position, isShow)
  826. {
  827. if (!title) return true;
  828. var isHScreen = this.Frame.IsHScreen === true;
  829. var right = this.Frame.ChartBorder.GetRight();
  830. if (isHScreen) right = this.Frame.ChartBorder.GetHeight();
  831. this.Canvas.setFillStyle(color);
  832. var textWidth = this.Canvas.measureText(title).width;
  833. if (position.Left + textWidth > right) return false;
  834. if (!(isShow === false)) this.Canvas.fillText(title, position.Left, position.Bottom, textWidth);
  835. position.Left += textWidth + this.SpaceWidth;
  836. return true;
  837. }
  838. }
  839. //字符串输出格式
  840. var STRING_FORMAT_TYPE =
  841. {
  842. DEFAULT: 1, //默认 2位小数 单位自动转化 (万 亿)
  843. ORIGINAL: 2, //原始数据
  844. THOUSANDS: 21, //千分位分割
  845. };
  846. function DynamicTitleData(data, name, color) //指标标题数据
  847. {
  848. this.Data = data;
  849. this.Name = name;
  850. this.Color = color; //字体颜色
  851. this.DataType; //数据类型
  852. this.StringFormat = STRING_FORMAT_TYPE.DEFAULT; //字符串格式
  853. this.FloatPrecision = 2; //小数位数
  854. this.GetTextCallback; //自定义数据转文本回调
  855. this.IsShow=true;
  856. }
  857. //指标标题
  858. function DynamicChartTitlePainting()
  859. {
  860. this.newMethod = IChartTitlePainting; //派生
  861. this.newMethod();
  862. delete this.newMethod;
  863. this.IsDynamic = true;
  864. this.Data = new Array();
  865. this.Explain;
  866. this.TitleBG; //标题背景色
  867. this.TitleBGHeight = 20; //标题背景色高度
  868. this.TitleAlign = 'middle';//对其方式
  869. this.TitleBottomDistance = 1; //标题靠底部输出的时候 字体和底部的间距
  870. this.Text = new Array(); //副标题 Text:'文本', Color:'颜色'
  871. this.EraseRect;
  872. this.EraseColor = g_JSChartResource.BGColor; //用来擦出的背景色
  873. this.TitleRect; //指标名字显示区域
  874. this.IsDrawTitleBG=false; //是否绘制指标名字背景色
  875. this.BGColor=g_JSChartResource.IndexTitleBGColor;
  876. this.IsShowIndexName = true; //是否显示指标名字
  877. this.ParamSpace = 2; //参数显示的间距
  878. this.OutName=null; //动态标题
  879. this.IsFullDraw=true; //手势离开屏幕以后是否显示最后的价格
  880. this.SetDynamicOutName=function(outName, args)
  881. {
  882. if (!this.OutName) this.OutName=new Map();
  883. else this.OutName.clear();
  884. var mapArgs=new Map();
  885. for(var i in args)
  886. {
  887. var item=args[i];
  888. mapArgs.set(`{${item.Name}}`, item);
  889. }
  890. for(var i in outName)
  891. {
  892. var item=outName[i];
  893. var aryFond = item.DynamicName.match(/{\w*}/i);
  894. if (!aryFond || aryFond.length<=0)
  895. {
  896. this.OutName.set(item.Name, item.DynamicName);
  897. }
  898. else
  899. {
  900. var dyName=item.DynamicName;
  901. var bFind=true;
  902. for(var j=0;j<aryFond.length;++j)
  903. {
  904. var findItem=aryFond[j];
  905. if (mapArgs.has(findItem))
  906. {
  907. var value=mapArgs.get(findItem).Value;
  908. dyName=dyName.replace(findItem,value.toString());
  909. }
  910. else
  911. {
  912. bFind=false;
  913. break;
  914. }
  915. }
  916. if (bFind) this.OutName.set(item.Name, dyName);
  917. }
  918. }
  919. }
  920. this.GetDynamicOutName=function(outName)
  921. {
  922. if (!this.OutName || this.OutName.size<=0) return null;
  923. if (!this.OutName.has(outName)) return null;
  924. return this.OutName.get(outName);
  925. }
  926. this.IsClickTitle=function(x,y) //是否点击了指标标题
  927. {
  928. if (!this.TitleRect) return false;
  929. if (x>this.TitleRect.Left && x<this.TitleRect.Left+this.TitleRect.Width && y>this.TitleRect.Top && y<this.TitleRect.Top+this.TitleRect.Height)
  930. {
  931. return true;
  932. }
  933. return false;
  934. }
  935. this.FormatValue = function (value, item)
  936. {
  937. if (item.StringFormat == STRING_FORMAT_TYPE.DEFAULT)
  938. return IFrameSplitOperator.FormatValueString(value, item.FloatPrecision, this.LanguageID);
  939. else if (item.StringFormat = STRING_FORMAT_TYPE.THOUSANDS)
  940. return IFrameSplitOperator.FormatValueThousandsString(value, item.FloatPrecision);
  941. else if (item.StringFormat == STRING_FORMAT_TYPE.ORIGINAL)
  942. return value.toFixed(item.FloatPrecision).toString();
  943. }
  944. this.FormatMultiReport = function (data, format)
  945. {
  946. var text = "";
  947. for (var i in data) {
  948. var item = data[i];
  949. let quarter = item.Quarter;
  950. let year = item.Year;
  951. let value = item.Value;
  952. if (text.length > 0) text += ',';
  953. text += year.toString();
  954. switch (quarter) {
  955. case 1:
  956. text += '一季报 ';
  957. break;
  958. case 2:
  959. text += '半年报 ';
  960. break;
  961. case 3:
  962. text += '三季报 ';
  963. break;
  964. case 4:
  965. text += '年报 ';
  966. break;
  967. }
  968. text += this.FormatValue(value, format);
  969. }
  970. return text;
  971. }
  972. this.SendUpdateUIMessage = function (funcName) //通知外面 标题变了
  973. {
  974. if (!this.UpdateUICallback) return;
  975. var sendData = {
  976. TitleName: '指标标题', CallFunction: funcName,
  977. TitleData: { Title: this.Title, Identify: this.Frame.Identify, Data: [] },
  978. Rect: //标题的位置
  979. {
  980. Top: this.Frame.ChartBorder.GetTop(), Left: this.Frame.ChartBorder.GetLeft(),
  981. Right: this.Frame.ChartBorder.GetRight(), Bottom: this.Frame.ChartBorder.GetBottom()
  982. }
  983. };
  984. for (var i in this.Data) {
  985. var item = this.Data[i];
  986. if (!item || !item.Data || !item.Data.Data) continue;
  987. if (item.Data.Data.length <= 0) continue;
  988. var titleItem = { Name: item.Name, Color: item.Color };
  989. if (item.DataType) titleItem.DataType = item.DataType;
  990. if (item.DataType == "StraightLine") //直线只有1个数据
  991. {
  992. titleItem.Value = item.Data.Data[0];
  993. }
  994. else {
  995. var index = item.Data.Data.length - 1;
  996. if (this.CursorIndex != null) {
  997. var cursorIndex = Math.abs(this.CursorIndex - 0.5);
  998. cursorIndex = parseInt(cursorIndex.toFixed(0));
  999. index = item.Data.DataOffset + cursorIndex
  1000. }
  1001. if (index >= item.Data.Data.length) index = item.Data.Data.length - 1;
  1002. titleItem.Value = item.Data.Data[index];
  1003. }
  1004. sendData.TitleData.Data.push(titleItem);
  1005. }
  1006. //console.log('[DynamicChartTitlePainting::SendUpdateUIMessage', sendData);
  1007. this.UpdateUICallback(sendData);
  1008. }
  1009. this.FullDraw=function()
  1010. {
  1011. this.EraseRect = null;
  1012. this.TitleRect=null;
  1013. this.IsDrawTitleBG=this.Frame.IsDrawTitleBG;
  1014. if (this.Frame.ChartBorder.TitleHeight < 5) return;
  1015. if (this.Frame.IsShowTitle == false) return;
  1016. if (this.Frame.IsMinSize) return;
  1017. this.IsShowIndexName = this.Frame.IsShowIndexName;
  1018. this.ParamSpace = this.Frame.IndexParamSpace;
  1019. if (this.Frame.IsHScreen === true)
  1020. {
  1021. this.Canvas.save();
  1022. this.DrawItem(true,true);
  1023. this.Canvas.restore();
  1024. return;
  1025. }
  1026. this.DrawItem(true,true);
  1027. }
  1028. this.DrawTitle = function ()
  1029. {
  1030. this.IsDrawTitleBG=this.Frame.IsDrawTitleBG;
  1031. this.EraseRect = null;
  1032. this.TitleRect=null;
  1033. this.SendUpdateUIMessage('DrawTitle');
  1034. if (this.Frame.ChartBorder.TitleHeight < 5) return;
  1035. if (this.Frame.IsShowTitle == false) return;
  1036. this.IsShowIndexName = this.Frame.IsShowIndexName;
  1037. this.ParamSpace = this.Frame.IndexParamSpace;
  1038. if (this.Frame.IsHScreen === true)
  1039. {
  1040. this.Canvas.save();
  1041. this.DrawItem(true,false);
  1042. this.Canvas.restore();
  1043. return;
  1044. }
  1045. this.DrawItem(true,false);
  1046. }
  1047. this.EraseTitle = function ()
  1048. {
  1049. if (!this.EraseRect) return;
  1050. this.Canvas.setFillStyle(this.EraseColor);
  1051. this.Canvas.fillRect(this.EraseRect.Left, this.EraseRect.Top, this.EraseRect.Width, this.EraseRect.Height);
  1052. }
  1053. this.Draw = function ()
  1054. {
  1055. this.TitleRect=null;
  1056. this.SendUpdateUIMessage('Draw');
  1057. if (this.CursorIndex == null) return;
  1058. if (!this.Data) return;
  1059. if (this.Frame.ChartBorder.TitleHeight < 5) return;
  1060. if (this.Frame.IsShowTitle == false) return;
  1061. this.IsShowIndexName = this.Frame.IsShowIndexName;
  1062. this.ParamSpace = this.Frame.IndexParamSpace;
  1063. if (this.Frame.IsHScreen === true)
  1064. {
  1065. this.Canvas.save();
  1066. this.DrawItem(false,true);
  1067. this.Canvas.restore();
  1068. return;
  1069. }
  1070. this.DrawItem(false,true);
  1071. }
  1072. this.DrawItem=function(bDrawTitle, bDrawValue)
  1073. {
  1074. var isHScreen=(this.Frame.IsHScreen === true);
  1075. var left = this.Frame.ChartBorder.GetLeft() + 1;
  1076. var bottom = this.Frame.ChartBorder.GetTop() + this.Frame.ChartBorder.TitleHeight / 2; //上下居中显示
  1077. if (this.TitleAlign == 'bottom') bottom = this.Frame.ChartBorder.GetTopEx() - this.TitleBottomDistance;
  1078. var right = this.Frame.ChartBorder.GetRight();
  1079. var textWidth;
  1080. if (isHScreen)
  1081. {
  1082. let xText = this.Frame.ChartBorder.GetRightTitle();
  1083. let yText = this.Frame.ChartBorder.GetTop();
  1084. this.Canvas.translate(xText, yText);
  1085. this.Canvas.rotate(90 * Math.PI / 180);
  1086. left = 1;
  1087. bottom = -(this.Frame.ChartBorder.TitleHeight / 2); //上下居中显示
  1088. if (this.TitleAlign == 'bottom') bottom = -this.TitleBottomDistance;
  1089. right = this.Frame.ChartBorder.GetHeight();
  1090. }
  1091. this.EraseTitle();
  1092. this.Canvas.textAlign = "left";
  1093. this.Canvas.textBaseline = this.TitleAlign;
  1094. this.Canvas.font = this.Font;
  1095. if (this.TitleBG && this.Title) //指标名称
  1096. {
  1097. textWidth = this.Canvas.measureText(this.Title).width + 2;
  1098. let height = this.Frame.ChartBorder.TitleHeight;
  1099. let top = this.Frame.ChartBorder.GetTop();
  1100. if (height > 20)
  1101. {
  1102. top += (height - 20) / 2 + (height - 45) / 2;
  1103. height = 20;
  1104. }
  1105. if (this.TitleAlign == 'bottom') //底部输出文字
  1106. {
  1107. top = this.Frame.ChartBorder.GetTopEx() - 20;
  1108. if (top < 0) top = 0;
  1109. }
  1110. if (bDrawTitle)
  1111. {
  1112. this.Canvas.setFillStyle(this.TitleBG);
  1113. this.Canvas.fillRect(left, top, textWidth, height);
  1114. }
  1115. }
  1116. if (this.Title && this.IsShowIndexName) //指标参数
  1117. {
  1118. const metrics = this.Canvas.measureText(this.Title);
  1119. textWidth = metrics.width + 2;
  1120. if (bDrawTitle)
  1121. {
  1122. if (this.IsDrawTitleBG) //绘制指标名背景色
  1123. {
  1124. var spaceSize=1;
  1125. this.Canvas.fillStyle=this.BGColor;
  1126. if (isHScreen)
  1127. {
  1128. this.TitleRect= {Left:this.Frame.ChartBorder.GetRightTitle(),Top:this.Frame.ChartBorder.GetTop(),Width:this.Frame.ChartBorder.TitleHeight ,Height:textWidth}; //保存下标题的坐标
  1129. let drawRect={Left:left, Top:-this.Frame.ChartBorder.TitleHeight+spaceSize, Width:textWidth, Height:this.Frame.ChartBorder.TitleHeight-(spaceSize*2)};
  1130. this.Canvas.fillRect(drawRect.Left,drawRect.Top,drawRect.Width,drawRect.Height);
  1131. }
  1132. else
  1133. {
  1134. this.TitleRect={Left:left, Top:this.Frame.ChartBorder.GetTop()+spaceSize, Width:textWidth, Height:this.Frame.ChartBorder.TitleHeight-(spaceSize*2)}; //保存下标题的坐标
  1135. this.Canvas.fillRect(this.TitleRect.Left,this.TitleRect.Top,this.TitleRect.Width,this.TitleRect.Height);
  1136. }
  1137. }
  1138. this.Canvas.setFillStyle(this.TitleColor);
  1139. this.Canvas.fillText(this.Title, left, bottom, textWidth);
  1140. }
  1141. left += textWidth;
  1142. }
  1143. if (this.Text && this.Text.length > 0)
  1144. {
  1145. for (let i in this.Text)
  1146. {
  1147. let item = this.Text[i];
  1148. this.Canvas.setFillStyle(item.Color);
  1149. textWidth = this.Canvas.measureText(item.Text).width + 2;
  1150. this.Canvas.fillText(item.Text, left, bottom, textWidth);
  1151. left += textWidth;
  1152. }
  1153. }
  1154. if (bDrawValue)
  1155. {
  1156. for (var i in this.Data)
  1157. {
  1158. var item = this.Data[i];
  1159. if (!item || !item.Data || !item.Data.Data) continue;
  1160. if (item.Data.Data.length <= 0) continue;
  1161. if (item.IsShow==false) continue;
  1162. var value = null;
  1163. var valueText = null;
  1164. if (item.DataType == "StraightLine") //直线只有1个数据
  1165. {
  1166. value = item.Data.Data[0];
  1167. valueText = this.FormatValue(value, item);
  1168. }
  1169. else
  1170. {
  1171. var index = this.CursorIndex - 0.5;
  1172. if (index<0) index=0;
  1173. index = parseInt(index.toFixed(0));
  1174. if (item.Data.DataOffset + index >= item.Data.Data.length) continue;
  1175. value = item.Data.Data[item.Data.DataOffset + index];
  1176. if (value == null) continue;
  1177. if (item.DataType == "HistoryData-Vol")
  1178. {
  1179. value = value.Vol;
  1180. valueText = this.FormatValue(value, item);
  1181. }
  1182. else if (item.DataType == "MultiReport")
  1183. {
  1184. valueText = this.FormatMultiReport(value, item);
  1185. }
  1186. else
  1187. {
  1188. if (item.GetTextCallback) valueText = item.GetTextCallback(value, item);
  1189. else valueText = this.FormatValue(value, item);
  1190. }
  1191. }
  1192. this.Canvas.setFillStyle(item.Color);
  1193. var text;
  1194. if (item.Name)
  1195. {
  1196. var dyTitle=this.GetDynamicOutName(item.Name);
  1197. if (dyTitle) text=dyTitle+ ":" + valueText;
  1198. else text = item.Name + ":" + valueText;
  1199. }
  1200. else
  1201. {
  1202. text=valueText;
  1203. }
  1204. textWidth = this.Canvas.measureText(text).width + this.ParamSpace; //后空2个像素
  1205. this.Canvas.fillText(text, left, bottom, textWidth);
  1206. left += textWidth;
  1207. }
  1208. }
  1209. else
  1210. {
  1211. left += 4;
  1212. var eraseRight = left, eraseLeft = left;
  1213. for (var i in this.Data)
  1214. {
  1215. var item = this.Data[i];
  1216. if (!item || !item.Data || !item.Data.Data) continue;
  1217. if (item.Data.Data.length <= 0) continue;
  1218. var indexName = '●' + item.Name;
  1219. this.Canvas.setFillStyle(item.Color);
  1220. textWidth = this.Canvas.measureText(indexName).width + this.ParamSpace;
  1221. if (left + textWidth >= right) break;
  1222. this.Canvas.fillText(indexName, left, bottom, textWidth);
  1223. left += textWidth;
  1224. eraseRight = left;
  1225. }
  1226. if (eraseRight > eraseLeft)
  1227. {
  1228. if (isHScreen)
  1229. {
  1230. this.EraseRect =
  1231. {
  1232. Left: eraseLeft, Right: eraseRight, Top: -(this.Frame.ChartBorder.TitleHeight - 1),
  1233. Width: eraseRight - eraseLeft, Height: this.Frame.ChartBorder.TitleHeight - 2
  1234. };
  1235. }
  1236. else
  1237. {
  1238. this.EraseRect =
  1239. {
  1240. Left: eraseLeft, Right: eraseRight, Top: (this.Frame.ChartBorder.GetTop() + 1),
  1241. Width: eraseRight - eraseLeft, Height: this.Frame.ChartBorder.TitleHeight - 2
  1242. };
  1243. }
  1244. }
  1245. }
  1246. }
  1247. }
  1248. //导出统一使用JSCommon命名空间名
  1249. module.exports =
  1250. {
  1251. JSCommonChartTitle:
  1252. {
  1253. IChartTitlePainting: IChartTitlePainting,
  1254. DynamicKLineTitlePainting: DynamicKLineTitlePainting,
  1255. DynamicMinuteTitlePainting: DynamicMinuteTitlePainting,
  1256. DynamicChartTitlePainting: DynamicChartTitlePainting,
  1257. DynamicTitleData: DynamicTitleData,
  1258. STRING_FORMAT_TYPE: STRING_FORMAT_TYPE,
  1259. },
  1260. //单个类导出
  1261. JSCommonChartTitle_IChartTitlePainting: IChartTitlePainting,
  1262. JSCommonChartTitle_DynamicKLineTitlePainting: DynamicKLineTitlePainting,
  1263. JSCommonChartTitle_DynamicMinuteTitlePainting: DynamicMinuteTitlePainting,
  1264. JSCommonChartTitle_DynamicChartTitlePainting: DynamicChartTitlePainting,
  1265. JSCommonChartTitle_DynamicTitleData: DynamicTitleData,
  1266. JSCommonChartTitle_STRING_FORMAT_TYPE: STRING_FORMAT_TYPE,
  1267. };