using System; using Android.Webkit; using System.Collections.Generic; namespace Shared { /// /// Button 按键 /// public class MyEchartsView : View { JsonData jsonData = new JsonData { }; public MyEchartsView() { webView = new MyWebView(Application.Activity, this); webView.SetWebViewClient(new MyWebViewClient { pageFinishedAction = () => { refresh($"javascript:loadEcharts({Newtonsoft.Json.JsonConvert.SerializeObject(jsonData)},'undefined')"); } }); webView.LoadUrl("file:///android_asset/Echarts/index.html"); } public void Show(string title, string[] xArray, object[] yArray) { jsonData.Title = title; jsonData.XArray = xArray; jsonData.YArray = yArray; refresh($"javascript:refreshWithOption({ Newtonsoft.Json.JsonConvert.SerializeObject(jsonData)})"); } public string Unit{ get{ return jsonData.Unit; }set{ jsonData.Unit = value; } } /// /// 创新界面 /// void refresh(string js) { if ((int)Android.OS.Build.VERSION.SdkInt < 18) { webView.LoadUrl(js); } else { webView.EvaluateJavascript(js, webView); } } MyWebView webView { get { return AndroidView as MyWebView; } set { AndroidView = value; } } internal class MyWebViewClient : Android.Webkit.WebViewClient { [Obsolete] public override bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, string url) { view.LoadUrl(url); return true; } public override void OnPageFinished(WebView view, string url) { base.OnPageFinished(view, url); pageFinishedAction?.Invoke(); } internal Action pageFinishedAction; } class MyWebView : Android.Webkit.WebView, IValueCallback { View view; public MyWebView(Android.Content.Context context, View view) : base(context) { this.view = view; SetBackgroundColor(Android.Graphics.Color.Transparent); Settings.AllowFileAccess = true; Settings.JavaScriptEnabled = true; HorizontalScrollBarEnabled = false; VerticalScrollBarEnabled = false; Settings.SetSupportZoom(true); Settings.JavaScriptCanOpenWindowsAutomatically = true; } public void OnReceiveValue(Java.Lang.Object value) { Shared.HDLUtils.WriteLine("Respone " + value); } } [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" } } } } }; } } }