Echarts+WPF


C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace WpfBrowserApp1
{
    /// 
    /// Page1.xaml 的交互逻辑
    /// 
    public partial class Page1 : Page
    {
        public dynamic scriptObject = null;
        public Page1()
        {
            InitializeComponent();
            if (!BrowserInteropHelper.IsBrowserHosted)
            {
                MessageBox.Show("不满足与JS调用条件");
                return;
            }
            scriptObject = BrowserInteropHelper.HostScript;
            if (scriptObject != null)
            {
                scriptObject.SetWpfObj(new OprateBasic());
            }
            webbrowser.Source = new Uri("html页面的路径");
            webbrowser.ObjectForScripting = new OprateBasic();
//            m_webFrame.Source =
//                new Uri(
//                    "");
//            m_webFrame.NavigationUIVisibility = System.Windows.Navigation.NavigationUIVisibility.Hidden;
            // Web.Navigate();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //O option = JsonConvert.DeserializeObject(Readjson(""));
            webbrowser.InvokeScript("msg",Readjson(""));
            //Web.InvokeScript("msg", Tb1.Text);
        }
        /// 
        /// 读取JSON文件
        /// 
        /// JSON文件中的key值
        /// JSON文件中的value值
        public static string Readjson(string key)
        {
            string jsonfile = "";//JSON文件路径

            using (System.IO.StreamReader file = System.IO.File.OpenText(jsonfile))
            {
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    JObject o = (JObject)JToken.ReadFrom(reader);
                    var value = o.ToString();
                    return value;
                }
            }
        }
    }
    [System.Runtime.InteropServices.ComVisible(true)] // 将该类设置为com可访问
    public class OprateBasic
    {
        public void palymusic(string msg)
        {
            //string json = File.ReadAllText("json文件路径“);
            //dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
            
           // string output = JsonConvert.SerializeObject(msg, Newtonsoft.Json.Formatting.Indented);
            File.WriteAllText("data.json", msg);
           // MessageBox.Show("WPF弹出框:" + msg);
        }
    }
}

html 

 1 
 2 
 3 
 4 
 5     "utf-8">
 6     "X-UA-Compatible" content="IE=edge" />
 7     ECharts
 8     
 9     
10     <script src="../Scripts/echarts.js"></script>
11     <script src="../Scripts/echarts-all.js"></script>
12     <script src="http://code.jquery.com/jquery-latest.js"></script>
13 
14 
15     
16 
"main" style="width: 600px;height:400px;">
17 "text" id="txtInput" /> 18 19 <script> 20 var con = window.external 21 var option = {}; 22 function Test() { 23 option.xAxis[0].name = $("#txtInput").val() 24 con.palymusic(JSON.stringify(option)); 25 } 26 27 function msg(obj) { 28 alert(obj) 29 var myChart = echarts.init(document.getElementById('main')); 30 option = JSON.parse(obj) 31 myChart.setOption(option) 32 } 33 34 </script> 35 <script type="text/javascript"> 36 // 基于准备好的dom,初始化echarts实例 37 38 39 // 指定图表的配置项和数据 40 41 //var str = "" 42 // $.getJSON("../Data/data.json",function(data){ 43 // $.each(data,function(infoindex,info){ 44 //str+=info["dd"] 45 // }) 46 //}) 47 // alert(str); 48 //; 49 50 </script> 51 52
配置
 1 <?xml version="1.0" encoding="utf-8"?>
 2 "1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 3   "1.0.0.0" name="MyApplication.app" />
 4   "urn:schemas-microsoft-com:asm.v2">
 5     
 6       "urn:schemas-microsoft-com:asm.v3">
 7         
19         "asInvoker" uiAccess="false" />
20       
21       
22         "Custom" />
23         class="System.Security.PermissionSet" version="1" Unrestricted="true" ID="Custom" SameSite="site" />
24       
25     
26   
27   "urn:schemas-microsoft-com:compatibility.v1">
28     
29       
30       
31       
32     
33   
34   
35   
47 
C