WPF非常漂亮图表控件 WPF with ECharts WPF快速集成图表Echarts WPF 与echarts绘图控件数据交互 WPF功能强大图表控件 WPF显示各种图表 WPF显示图表快速入门

前两篇已经介绍WPF集成webview2,以及wpf与JS相互通信

在WPF应用程序中,选择合适的图表库对于数据可视化至关重要。其中,ECharts和LiveCharts是两种常用的图表库。然而,从多方面考虑,集成ECharts比集成LiveCharts显示图表更具优势。

首先,ECharts拥有极其丰富的帮助文档和社区支持。这使得开发者在遇到问题时,能够快速找到解决方案,大大提高了开发效率。相比之下,LiveCharts的文档和社区资源相对有限,可能在开发过程中带来不便。

其次,ECharts的功能性和灵活性超越了LiveCharts。ECharts提供了大量的图表类型和样式选项,可以满足各种复杂的数据展示需求。而LiveCharts的功能更新相对较慢,显示内容和样式的选择相对较少,可能无法满足一些高级或特定的可视化需求。

再者,随着Web技术的发展,使用WebView2集成ECharts变得非常便捷。WebView2使得WPF应用程序能够无缝地嵌入和控制HTML内容,从而轻松实现ECharts的集成。这种方式不仅简化了开发过程,而且利用了Web的跨平台特性,使得应用程序具有更好的兼容性和扩展性。

尽管LiveCharts在某些场景下可能也是一个不错的选择,但在功能丰富性、文档支持和集成便利性等方面,ECharts无疑在WPF应用程序中显示图表方面具有更大的优势。因此,对于需要进行复杂数据可视化和追求高效开发的WPF项目,集成ECharts是一个更为理想的选择。

完整例子DEMO

private void DefineTimer_Tick(object sender, EventArgs e)
        {
            try
            {
                JArray jsonArray = new JArray();
                JArray jsonArray2 = new JArray();
                for (int i = 0; i < 6; i++) {
                    System.Threading.Thread.Sleep(10);
                    Random random = new Random();
                    int randomNumber = random.Next(10, 51);//随机数10-50
                    jsonArray.Add(randomNumber);
                }
                for (int i = 0; i < 7; i++)
                {
                    System.Threading.Thread.Sleep(10);
                    Random random = new Random();
                    int randomNumber = random.Next(1000, 1501);//随机数10-50
                    jsonArray2.Add(randomNumber);
                }
                this.Dispatcher.BeginInvoke(new Action(() =>
                {
                    if (webView.CoreWebView2 != null)
                    {
                        try
                        {
                            string jsonData = JsonConvert.SerializeObject(jsonArray);
                            Console.WriteLine(jsonData);
                            webView.CoreWebView2.PostWebMessageAsJson(jsonData);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Error sending message: " + ex.Message);
                        }
                    }
                    if (webView2.CoreWebView2 != null)
                    {
                        try
                        {
                            string jsonData = JsonConvert.SerializeObject(jsonArray2);
                            //Console.WriteLine(jsonData);
                            webView2.CoreWebView2.PostWebMessageAsJson(jsonData);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Error sending message: " + ex.Message);
                        }
                    }
                }));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

 

<Window x:Class="WPF_WebView2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPF_WebView2" 
        xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
        mc:Ignorable="d"
        WindowStartupLocation="CenterScreen"
        Title="WPF_Echarts完整例子" Height="500" Width="1000" Loaded="Window_Loaded">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid Grid.Row="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition  Width="*"/>
                <ColumnDefinition Width="150"/>
            </Grid.ColumnDefinitions>
            <Grid Grid.Column="0">
                <TextBox  x:Name="inputUrl"  VerticalContentAlignment="Center" KeyDown="inputUrl_KeyDown" />
            </Grid>
            <StackPanel Grid.Column="2" Orientation="Horizontal" HorizontalAlignment="Left" Margin="5 0 0 0">
                <Button Content="GO" Width="60" Height="30" Click="Button_Click"/>
                <Button  Click="Button_Click_1"  Content="发送信息" Margin="10 0 0 0" Width="60" />
            </StackPanel>
        </Grid>
        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*" />
                <ColumnDefinition Width="1*" />
            </Grid.ColumnDefinitions>
            <Grid Grid.Column="0">
                <wv2:WebView2 Name="webView" Source="https://www.baidu.com/"  />
            </Grid>
            <Grid Grid.Column="1">
                <wv2:WebView2 Name="webView2" Source="https://www.baidu.com/"  />
            </Grid>
        </Grid>
    </Grid>
</Window>

  • 19
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
之前三此发的库http://download.csdn.net/detail/maiker/9621027可以作废,本次为最新的,其中测试了历史曲线的动态显示,这是对Microsoft WpfToolkit的更新和扩展: 1、将库版本升级到.NET 4.6.1,对命名控件进行了替换处理 2、实现了单数据Chart图的混合颜色显示,可通过修改资源字典调整和添加颜色 3、可控制各种Chart图形的数据显示 4、坐标轴文本可倾斜显示 5、添加了StepLine图和圆环图,圆环半径比例系数可设定,同时添加了饼图的半径比系数,这样可以更好的控制饼图的标签显示,避免重叠 6、Legend可位于区域四侧,对齐方式也可以设定,以及是否显示, 可控制Chart Title是否显示 7、柱状图缝隙间隔可调整 8、全方面的加入了各种加载动画效果,动画效果可屏蔽,同时原系统自带动画效果,可叠加,这是两种不同的效果,可以互补性的单个使用 9、设计了四种主题颜色,各人可以针对图形颜色和背景色进行更好的搭配,在用户项目中,可以自己添加新的主题。 10、扩展了时间轴的应用,在新的DateTimeChart中可以通过鼠标平移和缩放时间轴,并测试了两个例子用于动态显示历史曲线,一个是外部定时更新,一个是内部定时更新 11、对LineDataPoint样式进行了设计,现在可以选择线图的点样式(如空心圆、五角星、三角形,矩形等),同时这些不同的点样式可以体现在Legend上,从而实现颜色和图形的双重区分。 总而言之,微软的控件库做得很标准,还是很好修改的。 最后一直想在历史曲线中取消点动画(及默认的透明度动画),以便提高执行效率,但微软的那部分动画不熟,总是修改失败。 可以发邮件wuyang26@live.cn讨论。
要在WPF自定义控件使用LiveCharts显示两个图表,可以使用以下步骤: 1. 引用LiveCharts NuGet包。 2. 在自定义控件的XAML文件中,添加LiveCharts的命名空间:xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf" 3. 在自定义控件的XAML文件中,添加两个LiveCharts控件(比如LineSeries、ColumnSeries等),并分别设置它们的数据源和属性。 4. 在自定义控件的代码文件中,为每个LiveCharts控件创建一个依赖属性,并在属性变化时重新绘制图表。 下面是一个简单的示例代码,其中MyCustomControl是自定义控件,其中包含两个LiveCharts控件: ```csharp public class MyCustomControl : Control { static MyCustomControl() { DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomControl), new FrameworkPropertyMetadata(typeof(MyCustomControl))); } public SeriesCollection Series1 { get { return (SeriesCollection)GetValue(Series1Property); } set { SetValue(Series1Property, value); } } public static readonly DependencyProperty Series1Property = DependencyProperty.Register("Series1", typeof(SeriesCollection), typeof(MyCustomControl), new PropertyMetadata(null, OnSeries1PropertyChanged)); private static void OnSeries1PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var control = d as MyCustomControl; control.DrawChart1(); } public SeriesCollection Series2 { get { return (SeriesCollection)GetValue(Series2Property); } set { SetValue(Series2Property, value); } } public static readonly DependencyProperty Series2Property = DependencyProperty.Register("Series2", typeof(SeriesCollection), typeof(MyCustomControl), new PropertyMetadata(null, OnSeries2PropertyChanged)); private static void OnSeries2PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var control = d as MyCustomControl; control.DrawChart2(); } private void DrawChart1() { //绘制第一个图表 chart1.Series = Series1; } private void DrawChart2() { //绘制第二个图表 chart2.Series = Series2; } private LiveCharts.Wpf.CartesianChart chart1; private LiveCharts.Wpf.CartesianChart chart2; public override void OnApplyTemplate() { base.OnApplyTemplate(); chart1 = GetTemplateChild("PART_Chart1") as LiveCharts.Wpf.CartesianChart; chart2 = GetTemplateChild("PART_Chart2") as LiveCharts.Wpf.CartesianChart; DrawChart1(); DrawChart2(); } } ``` 在自定义控件的XAML文件中,可以使用以下代码来定义两个LiveCharts控件: ```xml <Style TargetType="{x:Type local:MyCustomControl}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:MyCustomControl}"> <Grid> <lvc:CartesianChart x:Name="PART_Chart1" /> <lvc:CartesianChart x:Name="PART_Chart2" /> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> ``` 然后,在使用自定义控件的地方,可以使用以下代码来设置两个图表数据源: ```xml <local:MyCustomControl Series1="{Binding Data1}" Series2="{Binding Data2}" /> ``` 其中,Data1和Data2是两个SeriesCollection类型的数据源,可以在ViewModel中定义并绑定到视图中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weijia3624

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值