winform+webBrowser+Echarts+JavaScript(多图表一个Echarts)
最近安排做一个winform客户端项目,现在就webBrowser和Echarts做一些知识点的总结
需求:连续不断加载PLC传入的数据,在winform界面显示,用echarts图表展示出来
步骤
1、一个form,一个panel,拖入webBrowser,注意命名规范
2、Html加载进webBrowser,显示echarts图表
(1)
最好在本项目下的bin目录下创建一个showEchart文件夹,大家可以在string midcharts 打断点,看这个 System.Environment.CurrentDirectory 显示什么路径,其实就是获取当前项目可执行文件的路径
(2)
wb_fullchart是webBrowser的name,fullchart是文件路径,放在frame_Load方法里面,启动就能加载;这样就可以显示Echarts内容了
3、可能遇到的问题
(1)浏览器兼容问题
1.1html 里面的js添加这行代码
window.console = window.console || (function () {
var c = {}; c.log = c.warn = c.debug = c.info = c.error = c.time = c.dir = c.profile
= c.clear = c.exception = c.trace = c.assert = function () { };
return c;
})();
1.2 可以写浏览器兼容工具类,可以在frame控制里面写,然后在frame.cs调用SetWebBrowserFeatures(1100);变成内核为iE11就好了,如果是工具类注意新建对象调用;
public void SetWebBrowserFeatures(int ieVersion)
{
// don't change the registry if running in-proc inside Visual Studio
if (LicenseManager.UsageMode != LicenseUsageMode.Runtime)
return;
//获取程序及名称
var appName = System.IO.Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
//得到浏览器的模式的值
UInt32 ieMode = GeoEmulationModee(ieVersion);
var featureControlRegKey = @"HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\";
//设置浏览器对应用程序(appName)以什么模式(ieMode)运行
Registry.SetValue(featureControlRegKey + "FEATURE_BROWSER_EMULATION",
appName, ieMode, RegistryValueKind.DWord);
// enable the features which are "On" for the full Internet Explorer browser
//不晓得设置有什么用
Registry.SetValue(featureControlRegKey + "FEATURE_ENABLE_CLIPCHILDREN_OPTIMIZATION",
appName, 1, RegistryValueKind.DWord);
}
/// <summary>
/// 获取浏览器的版本
/// </summary>
/// <returns></returns>
public int GetBrowserVersion()
{
int browserVersion = 0;
using (var ieKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer",
RegistryKeyPermissionCheck.ReadSubTree,
System.Security.AccessControl.RegistryRights.QueryValues))
{
var version = ieKey.GetValue("svcVersion");
if (null == version)
{
version = ieKey.GetValue("Version");
if (null == version)
throw new ApplicationException("Microsoft Internet Explorer is required!");
}
int.TryParse(version.ToString().Split('.')[0], out browserVersion);
}
//如果小于7
if (browserVersion < 10)
{
throw new ApplicationException("no support ie");
}
return browserVersion;
}
/// <summary>
/// 通过版本得到浏览器模式的值
/// </summary>
/// <param name="browserVersion"></param>
/// <returns></returns>
public UInt32 GeoEmulationModee(int browserVersion)
{
UInt32 mode = 11000; // Internet Explorer 11. Webpages containing standards-based !DOCTYPE directives are displayed in IE11 Standards mode.
switch (browserVersion)
{
case 7:
mode = 7000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode.
break;
case 8:
mode = 8000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode.
break;
case 9:
mode = 9000; // Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode.
break;
case 10:
mode = 10000; // Internet Explorer 10.
break;
case 11:
mode = 11000; // Internet Explorer 11
break;
}
return mode;
}
(2)webBrowser 显示不全
我们要把WebBrowser当成是html对象,用css 去掉边框,自己想自己改变背景颜色自己试一下
<style type="text/css">
html {border:0px; overflow:hidden;background-color: black;}
</style>
(3)多个图表在在一个echarts页面(四个图表一个Echarts)
xAxis: [{
gridIndex :0,//这个索引是很关键的,每一个xAxis 的gridIndex代表这个你画的第一个图的X轴配置,同样对应yAxis的gridIndex
axisLabel:{
show:false,
textStyle:{
color:'#FFFFFF'
},
interval:0
},
axisLine: {
lineStyle: {
color:'#F0FFFF'
}
},
type: 'category',
data: date
},{
gridIndex :1,//
axisLabel:{
show:false,
textStyle:{
color:'#FFFFFF'
},
interval:0
},
axisLine: {
lineStyle: {
color:'#0B438B'
}
},
type: 'category',
data: date
},{
gridIndex :2,
axisLabel:{
show:false,
textStyle:{
color:'#FFFFFF'
},
interval:0
},
axisLine: {
lineStyle: {
color:'#0B438B'
}
},
type: 'category',
data: date
},{
gridIndex :3,
axisLabel:{
show:true,
textStyle:{
color:'#FFFFFF'
},
interval:0
},
axisLine: {
lineStyle: {
color:'#0B438B'
}
},
type: 'category',
data: date
},
],
yAxis: [{
gridIndex:0,
minInterval:1, // y轴的间隔
splitLine:{
show:false
},
axisLine: {
lineStyle: {
color:'#0B438B'
}
},
axisLabel: {
textStyle: {
color: '#FFFFFF'
}
},
boundaryGap: [0, '10%'],
type: 'value'
},{
gridIndex:1,
minInterval:1,
splitLine:{
show:true
},
axisLine: {
lineStyle: {
color:'#0B438B'
}
},
axisLabel: {
textStyle: {
color: '#FFFFFF'
}
},
boundaryGap: [0, '10%'],
type: 'value'
},{
gridIndex:2,
minInterval:1,
splitLine:{
show:true
},
axisLine: {
lineStyle: {
color:'#0B438B'
}
},
axisLabel: {
textStyle: {
color: '#FFFFFF'
}
},
boundaryGap: [0, '10%'],
type: 'value'
},{
gridIndex:3,
minInterval:1,
splitLine:{
show:true
},
axisLine: {
lineStyle: {
color:'#0B438B'
}
},
axisLabel: {
textStyle: {
color: '#FFFFFF'
}
},
boundaryGap: [0, '10%'],
type: 'value'
}
],
grid:[{ //grid 是四块区域的边框间距
top:'5%',
left: '3%',
right: '5%',
bottom: '76%'
}, {
top: '26%',
left: '3%',
right: '5%',
bottom: '52%'
},{
top: '50%',
left: '3%',
right: '5%',
bottom: '26%'
},
{
top: '76%',
left: '3%',
right: '5%',
bottom: '5%'
}],
series: [
{
barWidth:'50%',//柱状图的宽度
color: '#F0FFFF',
type:'bar',
data:data_Ly,
xAxisIndex: 0, //四个图表的X,Y要对应上序号
yAxisIndex: 0
},
{
barWidth:'50%',
color: '#DC143C',
type:'bar',
data:data_ay,
xAxisIndex: 1,
yAxisIndex: 1
},
{
barWidth:'50%',
color: '#FFFF00',
type:'bar',
data:data_by,
xAxisIndex: 2,
yAxisIndex: 2
},
{
barWidth:'50%',
color: '#00FA9A',
type:'bar',
data:data_Ey,
xAxisIndex: 3,
yAxisIndex: 3
},
]
};
总结:至此,前台基本上满足需求,后面webBrowser通过控制传数据,另写一篇