HQChart小程序教程3-新版2D单画布如何切换K线图和分时图
K线图和分时图图形切换流程图代码讲解K线周期切换创建K线图步骤
配色修改禁止hqchart缓存绘制
HQChart代码地址完整示例代码wxmlwxssjs
K线图和分时图
传统的一般会使用2个画布来显示,当一个画布显示的时候,另外一个画布就隐藏掉。 本教程是另外一个思路,使用一个画布来动态显示K线图和分时图,这样可以节省资源,但在切换的时候需要重新创建图形和下载数据,会慢一点。 如是使用2D画布详见教程HQChart小程序教程2-如何使用新版2D画布创建一个K线图
图形切换流程图
下图是分时图切换到K线图的流程,K线切换到分时图流程也差不多, 我就不画了。
代码讲解
主要使用8795以后的版本,老的版本画布会放大 对实例中的部分代码解释下,你也可以直接跳过看下面的完整源码
K线周期切换
对应上面的流程图
ChangeKLinePeriod(period
)
{
if (!this.HistoryChart
)
{
this.CreateKLineChart(period
);
}
else
{
if (this.HistoryChart
.JSChartContainer
.ClassName
=="KLineChartContainer")
{
this.HistoryChart
.ChangePeriod(period
);
}
else
{
this.ClearHQChart();
this.CreateKLineChart(period
);
}
}
},
创建K线图
步骤
动态调整画布大小,可能分时图和K线图UI设计的时候大小不一样,就需要切换的时候动态调整查询画布节点创建hqchart实例,并绑定画布
CreateKLineChart(period
)
{
console
.log('[App::CreateKLineChart] ');
this.setData({ Width
: this.data
.KLineWidth
, Height
: this.data
.KLineHeight
}, () => {});
var self
= this;
wx
.createSelectorQuery()
.select('#historychart')
.fields({
node
: true,
size
: true,
})
.exec(function(res
)
{
self
.data
.CanvasNode
= res
[0];
var element
= new JSCommon.JSCanvasElement();
element
.ID = 'historychart';
element
.CanvasNode
=self
.data
.CanvasNode
;
element
.Height
= self
.data
.KLineHeight
;
element
.Width
= self
.data
.KLineWidth
;
self
.HistoryChart
= JSCommon
.JSChart
.Init(element
);
self
.HistoryOption
.Symbol
=self
.data
.Symbol
;
self
.HistoryOption
.Period
=period
;
self
.HistoryChart
.SetOption(self
.HistoryOption
);
})
},
配色修改
在onReady()中可以对hqchart的全局配色文件修改,比如切换黑色风格
onReady()
{
this.InitalHQChart();
},
InitalHQChart()
{
console
.log('[App::InitalHQChart]');
var style
= JSCommonHQStyle
.GetStyleConfig(JSCommonHQStyle
.STYLE_TYPE_ID.BLACK_ID);
JSCommon
.JSChart
.SetStyle(style
);
this.CreateKLineChart(0);
},
禁止hqchart缓存绘制
由于小程序2D画布加载缓存图片在有些安卓的机器无效,可以通过在setoption里面设置禁止hqchart使用图片缓存机制
K线图:
HistoryOption
:
{
Type
: '历史K线图',
........
IsFullDraw
:true,
.........
}
分时图:
MinuteOption
:
{
.......
IsFullDraw
:true,
........
}
如果还有问题可以加交流QQ群: 950092318
HQChart代码地址
地址:https://github.com/jones2000/HQChart
完整示例代码
wxml
<view class="container">
<canvas class="historychart" id='historychart' type="2d"
style="height:{{Height}}px; width:{{Width}}px;"
bindtouchstart='historytouchstart' bindtouchmove='historytouchmove' bindtouchend='historytouchend'>
</canvas>
<view class="buttonlist" style="top:{{Height}}px; width:{{PageWidth}}px">
<button type="default" size='mini' bindtap='OnZoomIn' >放大</button>
<button type="default" size='mini' bindtap="OnZoomOut">缩小</button>
<button type="default" size='mini' bindtap="OnMoveForward">前进</button>
<button type="default" size='mini' bindtap="OnMoveBackword">后退</button>
<button type="default" size='mini' bindtap="OnMoveLatest">最新</button>
<button type="default" size='mini' bindtap="OnTest">切换</button>
<button type="default" size='mini' bindtap="OnChangeMinute">分时图</button>
<button type="default" size='mini' bindtap="OnKLineDayPeriod">日线</button>
<button type="default" size='mini' bindtap="OnKLine5MinutePeriod">5分钟</button>
</view>
</view>
wxss
page
{
background-color:rgb(0,0,0)
}
.historychart
{
position:absolute;
top: 0px;
left: 0px;
z-index: 0;
}
.container
{
position:absolute;
top: 0px;
left: 0px;
height: 100%;
width:100%;
}
.title
{
position:absolute;
top: 0px;
left: 0px;
height: 40px;
z-index:5;
}
.buttonlist
{
position:absolute;
}
js
const app
= getApp()
import { JSCommon
} from "../../jscommon/umychart.wechat.3.0.js";
import { JSCommonHQStyle
} from "../../jscommon/umychart.style.wechat.js";
Page(
{
data
:
{
Height
: 0,
Width
: 0,
CanvasNode
:null,
Symbol
:'600000.sh',
KLineWidth
:0,
KLineHeight
:0,
MinuteWidth
:0,
MinuteHeight
:0,
PageWidth
:0,
},
HistoryChart
: null,
HistoryOption
:
{
Type
: '历史K线图',
Windows
:
[
{ Index
: 'MA',IsDrawTitleBG
:true},
{ Index
: 'VOL', IsDrawTitleBG
:true},
],
Symbol
: "000001.sz",
IsAutoUpdate
: true,
AutoUpdateFrequency
:3000,
CorssCursorInfo
: { Left
: 2, Right
: 2, Bottom
: 1, IsShowCorss
: true, IsShowClose
: false, VPenType
: 0, HPenType
:0 },
CorssCursorTouchEnd
:false,
IsClickShowCorssCursor
:true,
IsFullDraw
:true,
KLine
:
{
DragMode
:1,
Right
: 1,
Period
:1,
MaxReqeustDataCount
: 1000,
MaxRequestMinuteDayCount
:5,
PageSize
: 15,
InfoDrawType
:0,
DrawType
: 0,
IsShowMaxMinPrice
: false,
RightSpaceCount
: 2,
},
Overlay
:
[
],
KLineTitle
:
{
IsShowName
: true,
IsShowSettingInfo
: true,
LineCount
:1,
},
Border
:
{
Left
: 0,
Right
: 50,
Top
:30,
Bottom
:25,
},
Frame
:
[
{
SplitCount
: 3, IsShowLeftText
: false, IsShowRightText
: true,
Custom
:
[
{
Type
: 0,
Position
: 'right',
},
{
Type
: 1,
Position
: 'left', IsShowLine
: true,
Data
:
[
{
Value
: 15.55,
Text
: '止损线',
Color
: 'rgb(255,185,255)', TextColor
: 'rgb(255,255,255)',
},
{
Value
: 17.55,
Text
: '止盈线',
Color
: 'rgb(255,185,0)', TextColor
: 'rgb(255,255,255)',
}
]
}
]
},
{ SplitCount
: 2 },
{ SplitCount
: 2 },
{ SplitCount
: 2 }
],
ExtendChart
:
[
{ Name
: 'KLineTooltip'},
],
},
MinuteOption
:
{
Type
: '分钟走势图',
Symbol
: "600000.sh",
IsAutoUpate
: true,
CorssCursorTouchEnd
: true,
IsFullDraw
:true,
Windows
:
[
{ Index
: 'MACD', Modify
: false, Change
: false, TitleHeight
: 16 },
],
Border
:
{
Left
: 1,
Right
: 1,
Top
: 35,
Bottom
:30
},
Frame
:
[
{ SplitCount
: 5, StringFormat
: 1 },
{ SplitCount
: 2, StringFormat
: 1 },
{ SplitCount
: 2, StringFormat
: 1 },
{ SplitCount
: 2, StringFormat
: 1 }
],
MinuteTitle
:
{
IsShow
: true,
IsShowName
: false,
LineCount
: 2,
},
ExtendChart
:
[
{ Name
: 'MinuteTooltip'}
],
},
onLoad()
{
var self
= this
wx
.getSystemInfo({
success
: (res
) =>
{
console
.log(res
);
console
.log('height=' + res
.windowHeight
);
console
.log('width=' + res
.windowWidth
);
var kLineWidth
=res
.windowWidth
;
var kLineHeight
=res
.windowHeight
- 70;
var minuteWidth
=res
.windowWidth
-100;
var minuteHeight
=res
.windowHeight
- 70;
self
.setData(
{
Height
:kLineHeight
,
Width
: kLineWidth
,
PageWidth
:res
.windowWidth
,
KLineWidth
:kLineWidth
,
KLineHeight
:kLineHeight
,
MinuteWidth
:minuteWidth
,
MinuteHeight
:minuteHeight
});
}
});
},
onReady()
{
this.InitalHQChart();
},
CreateMinuteChart(dayCount
)
{
console
.log('[App::CreateMinuteChart]');
this.setData({ Width
: this.data
.MinuteWidth
, Height
: this.data
.MinuteHeight
}, () => {});
var self
= this
wx
.createSelectorQuery()
.select('#historychart')
.fields({
node
: true,
size
: true,
})
.exec(function(res
)
{
self
.data
.CanvasNode
= res
[0];
var element
= new JSCommon.JSCanvasElement();
element
.ID = 'historychart';
element
.CanvasNode
=self
.data
.CanvasNode
;
element
.Height
= self
.data
.MinuteHeight
;
element
.Width
= self
.data
.MinuteWidth
;
self
.HistoryChart
= JSCommon
.JSChart
.Init(element
);
self
.MinuteOption
.Symbol
=self
.data
.Symbol
;
self
.MinuteOption
.DayCount
=dayCount
;
self
.HistoryChart
.SetOption(self
.MinuteOption
);
});
},
ChangeMinuteDayCount(dayCount
)
{
if (!this.HistoryChart
)
{
this.CreateMinuteChart(dayCount
);
}
else
{
if (this.HistoryChart
.JSChartContainer
.ClassName
!="MinuteChartContainer")
{
this.ClearHQChart();
this.CreateMinuteChart(dayCount
);
}
else
{
this.HistoryChart
.ChangeDayCount(dayCount
);
}
}
},
ChangeKLinePeriod(period
)
{
if (!this.HistoryChart
)
{
this.CreateKLineChart(period
);
}
else
{
if (this.HistoryChart
.JSChartContainer
.ClassName
=="KLineChartContainer")
{
this.HistoryChart
.ChangePeriod(period
);
}
else
{
this.ClearHQChart();
this.CreateKLineChart(period
);
}
}
},
CreateKLineChart(period
)
{
console
.log('[App::CreateKLineChart] ');
this.setData({ Width
: this.data
.KLineWidth
, Height
: this.data
.KLineHeight
}, () => {});
var self
= this
wx
.createSelectorQuery()
.select('#historychart')
.fields({
node
: true,
size
: true,
})
.exec(function(res
)
{
self
.data
.CanvasNode
= res
[0];
var element
= new JSCommon.JSCanvasElement();
element
.ID = 'historychart';
element
.CanvasNode
=self
.data
.CanvasNode
;
element
.Height
= self
.data
.KLineHeight
;
element
.Width
= self
.data
.KLineWidth
;
self
.HistoryChart
= JSCommon
.JSChart
.Init(element
);
self
.HistoryOption
.Symbol
=self
.data
.Symbol
;
self
.HistoryOption
.Period
=period
;
self
.HistoryChart
.SetOption(self
.HistoryOption
);
var customVerticalInfo
=
[
{ Type
: 0, Name
:'刻度1', Date
: 20200114, Time
:1420, Line
: { Color
: 'rgb(35,222,0)', Type
: 0 } },
{ Type
: 1, Name
: '刻度2', Space
: 3, Line
: { Color
: 'rgb(35,0,255)', Type
: 1 } },
];
self
.HistoryChart
.JSChartContainer
.SetCustomVerical(0, customVerticalInfo
);
self
.HistoryChart
.AddEventCallback({ event
: JSCommon
.JSCHART_EVENT_ID.RECV_HISTROY_DATA, callback
: self
.OnRecvKLineData
});
self
.HistoryChart
.AddEventCallback({ event
: JSCommon
.JSCHART_EVENT_ID.ON_TITLE_DRAW, callback
: self
.OnTitleDraw
});
self
.HistoryChart
.AddEventCallback({ event
: JSCommon
.JSCHART_EVENT_ID.ON_CUSTOM_VERTICAL_DRAW, callback
: self
.OnCustmVericalDraw
});
self
.HistoryChart
.AddEventCallback({ event
: JSCommon
.JSCHART_EVENT_ID.ON_CLICK_INDEXTITLE, callback
: self
.OnClickIndexName
})
})
},
ClearHQChart()
{
if (this.HistoryChart
)
{
this.HistoryChart
.StopAutoUpdate();
this.HistoryChart
=null;
}
},
InitalHQChart()
{
console
.log('[App::InitalHQChart]');
var style
= JSCommonHQStyle
.GetStyleConfig(JSCommonHQStyle
.STYLE_TYPE_ID.BLACK_ID);
JSCommon
.JSChart
.SetStyle(style
);
this.CreateKLineChart(0);
},
OnRecvKLineData
: function (event
, data
, obj
)
{
console
.log('[OnRecvKLineData]', event
, data
);
},
OnCustmVericalDraw(event
, data
, obj
)
{
console
.log('[OnCustmVericalDraw]', event
, data
);
},
OnTitleDraw(event
, data
, obj
)
{
console
.log('[OnTitleDraw]', event
, data
);
},
OnClickIndexName(event
,data
,obj
)
{
console
.log('[OnClickIndexName]', event
, data
);
},
OnChangeMinute(event
,data
,obj
)
{
this.ChangeMinuteDayCount(1);
},
OnKLineDayPeriod(event
,data
,obj
)
{
this.ChangeKLinePeriod(0);
},
OnKLine5MinutePeriod(event
,data
,obj
)
{
this.ChangeKLinePeriod(5);
},
historytouchstart
: function (event
)
{
if (this.HistoryChart
) this.HistoryChart
.OnTouchStart(event
);
},
historytouchmove
: function (event
) {
if (this.HistoryChart
) this.HistoryChart
.OnTouchMove(event
);
},
historytouchend
: function (event
)
{
if (this.HistoryChart
) this.HistoryChart
.OnTouchEnd(event
);
},
IsKLineChart()
{
if (!this.HistoryChart
) return false;
return this.HistoryChart
.JSChartContainer
.ClassName
=="KLineChartContainer";
},
OnZoomOut
: function (event
)
{
if (!this.IsKLineChart()) return;
var obj
= { ID: 3 };
this.HistoryChart
.JSChartContainer
.ChartOperator(obj
);
},
OnZoomIn
: function (event
)
{
if (!this.IsKLineChart()) return;
var obj
= { ID: 4 };
this.HistoryChart
.JSChartContainer
.ChartOperator(obj
);
},
OnMoveForward(event
)
{
if (!this.IsKLineChart()) return;
var obj
={ ID: 1, Step
: 1 };
this.HistoryChart
.JSChartContainer
.ChartOperator(obj
);
},
OnMoveBackword(evnet
)
{
if (!this.IsKLineChart()) return;
var obj
= { ID: 2, Step
: 1 };
this.HistoryChart
.JSChartContainer
.ChartOperator(obj
);
},
OnMoveLatest(evnet
)
{
if (!this.IsKLineChart()) return;
var obj
= { ID: 5 };
this.HistoryChart
.JSChartContainer
.ChartOperator(obj
);
},
OnTest(evnet
)
{
if (!this.IsKLineChart()) return;
var option
=
{
Windows
:
[
{ Index
: "MA" },
{ Index
: "CCI" },
],
};
this.HistoryChart
.ChangeIndexTemplate(option
);
},
})