wxr
2020-06-15 b8e94316e41eba72d927d5ca7d931b26139ee8ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using Android.Webkit;
using System.Collections.Generic;
 
namespace Shared
{
    /// <summary>
    /// Button 按键
    /// </summary>
    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;
            }
        }
 
        /// <summary>
        /// 创新界面
        /// </summary>
        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
        {
            /// <summary>
            /// 标题内容
            /// </summary>
            /// <value>The title.</value>
            [Newtonsoft.Json.JsonIgnore]
            public string Title
            {
                get
                {
                    return title["text"].ToString();
                }
                set
                {
                    title["text"] = value;
                }
            }
            [Newtonsoft.Json.JsonProperty]
            readonly Dictionary<string, object> title = new Dictionary<string, object> { ["text"] = "" };
            [Newtonsoft.Json.JsonProperty]
            readonly Dictionary<string, object> tooltip = new Dictionary<string, object> { ["trigger"] = "axis" };
            /// <summary>
            /// X轴坐标点
            /// </summary>
            /// <value>The XA rray.</value>
            [Newtonsoft.Json.JsonIgnore]
            public string[] XArray
            {
                get
                {
                    return (xAxis[0] as Dictionary<string, object>)["data"] as string[];
                }
                set
                {
                    (xAxis[0] as Dictionary<string, object>)["data"] = value;
                }
            }
            [Newtonsoft.Json.JsonProperty]
            readonly List<object> xAxis = new List<object> { new Dictionary<string, object> { ["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<string, object>)["axisLabel"] as Dictionary<string, object>)["formatter"].ToString().Substring("{value}".Length);
                }
                set
                {
                    ((yAxis[0] as Dictionary<string, object>)["axisLabel"] as Dictionary<string, object>)["formatter"] = "{value}" + value;
                }
            }
            [Newtonsoft.Json.JsonProperty]
            readonly List<object> yAxis = new List<object> { new Dictionary<string, object> { ["type"] = "value", ["axisLabel"] = new Dictionary<string, object> { ["formatter"] = "{value}" } } };
            /// <summary>
            /// Y轴坐标点
            /// </summary>
            /// <value>The YA rray.</value>
            [Newtonsoft.Json.JsonIgnore]
            public object[] YArray
            {
                get
                {
                    return (series[0] as Dictionary<string, object>)["data"] as object[];
                }
                set
                {
                    (series[0] as Dictionary<string, object>)["data"] = value;
                }
            }
            [Newtonsoft.Json.JsonProperty]
            readonly List<object> series = new List<object> { new Dictionary<string, object> { ["name"] = "Value", ["type"] = "line", ["data"] = new object[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, ["markPoint"] = new Dictionary<string, object> { ["data"] = new List<object> { new Dictionary<string, object> { ["type"] = "max", ["name"] = "Max" }, new Dictionary<string, object> { ["type"] = "min", ["name"] = "Min" } } }, ["markLine"] = new Dictionary<string, object> { ["data"] = new List<object> { new Dictionary<string, object> { ["type"] = "average", ["name"] = "Average" } } } } };
        }
    }
}