using UIKit; using Foundation; using Shared.IO; using CoreFoundation; using System.Collections.Generic; using System; using System.Runtime; using WebKit; namespace Shared { /// /// MyEchartsView /// 2020-06-19 UIWebView替换为WKWebView /// UIWebView已经弃用,后面再使用的APP将不能发布上架 /// public class MyEchartsView : View { JsonData jsonData = new JsonData { }; WKWebView uIWebView; public MyEchartsView() { //默认布局,宽高为0的话会异常 var mFrame = new CoreGraphics.CGRect(base.X, base.Y, 10, 10); uIWebView = new WKWebView(mFrame, new WKWebViewConfiguration()) { }; uiView = uIWebView; uIWebView.ScrollView.Bounces = false; uIWebView.ScrollView.ScrollEnabled = false; uIWebView.NavigationDelegate = new OnWKNavigationDelegate(this); var filePath = NSBundle.MainBundle.PathForResource("echarts", "html"); var url = new NSUrl(filePath, false); uIWebView.LoadFileUrl(url, url); } public void Show(string title, string[] xArray, object[] yArray) { jsonData.Title = title; jsonData.XArray = xArray; jsonData.YArray = yArray; EvaluateJavascript(); } public string Unit { get { return jsonData.Unit; } set { jsonData.Unit = value; } } public void EvaluateJavascript() { WKJavascriptEvaluationResult handler = (NSObject result, NSError err) => { if (err != null) { HDLUtils.WriteLine("EvaluateJavascript:" + err); } if (result != null) { HDLUtils.WriteLine("EvaluateJavascript:" + result); } }; uIWebView.EvaluateJavaScript($"loadEcharts({Newtonsoft.Json.JsonConvert.SerializeObject(jsonData)},'undefined')", handler); } public class OnWKNavigationDelegate : WKNavigationDelegate { [Weak] MyEchartsView _MyEchartsView; public OnWKNavigationDelegate(MyEchartsView view) { _MyEchartsView = view; } public override void DidFinishNavigation(WKWebView webView, WKNavigation navigation) { Console.WriteLine("WKWebView DidFinishNavigation"); _MyEchartsView.EvaluateJavascript(); } } [Serializable] class JsonData { /// /// 标题内容 /// /// The title. [Newtonsoft.Json.JsonIgnore] public string Title { get { return title["text"].ToString(); } set { title["text"] = value; } } [Newtonsoft.Json.JsonProperty] readonly Dictionary title = new Dictionary { ["text"] = "" }; [Newtonsoft.Json.JsonProperty] readonly Dictionary tooltip = new Dictionary { ["trigger"] = "axis" }; /// /// X轴坐标点 /// /// The XA rray. [Newtonsoft.Json.JsonIgnore] public string[] XArray { get { return (xAxis[0] as Dictionary)["data"] as string[]; } set { (xAxis[0] as Dictionary)["data"] = value; } } [Newtonsoft.Json.JsonProperty] readonly List xAxis = new List { new Dictionary { ["type"] = "category", ["boundaryGap"] = false, ["data"] = new object[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } } }; [Newtonsoft.Json.JsonIgnore] public string Unit { get { return ((yAxis[0] as Dictionary)["axisLabel"] as Dictionary)["formatter"].ToString().Substring("{value}".Length); } set { ((yAxis[0] as Dictionary)["axisLabel"] as Dictionary)["formatter"] = "{value}" + value; } } [Newtonsoft.Json.JsonProperty] readonly List yAxis = new List { new Dictionary { ["type"] = "value", ["axisLabel"] = new Dictionary { ["formatter"] = "{value}" } } }; /// /// Y轴坐标点 /// /// The YA rray. [Newtonsoft.Json.JsonIgnore] public object[] YArray { get { return (series[0] as Dictionary)["data"] as object[]; } set { (series[0] as Dictionary)["data"] = value; } } [Newtonsoft.Json.JsonProperty] readonly List series = new List { new Dictionary { ["name"] = "Value", ["type"] = "line", ["data"] = new object[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, ["markPoint"] = new Dictionary { ["data"] = new List { new Dictionary { ["type"] = "max", ["name"] = "Max" }, new Dictionary { ["type"] = "min", ["name"] = "Min" } } }, ["markLine"] = new Dictionary { ["data"] = new List { new Dictionary { ["type"] = "average", ["name"] = "Average" } } } } }; } } }