码迷,mamicode.com
首页 > 其他好文 > 详细

UWP 使用Telerik Chart控件

时间:2018-03-18 01:21:51      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:htm   viewer   情况   cat   str   rap   效果   var   tla   

原文:UWP 使用Telerik Chart控件

Telerik开发的chart功能异常强大 用户可以自行在商店搜索“UI for uwp demos”。

技术分享图片

 

 

下面我就结合以下我的软件,来说明一下饼状图的实现。

看看效果:
技术分享图片

 

 先看一下Xaml代码实现

 

<ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Hidden">
            <StackPanel x:Name="spBalanceAndChart" Margin="0,17,0,0" CornerRadius="7" HorizontalAlignment="Center" VerticalAlignment="Center">

                <TextBlock x:Uid="tbChartHeader" x:Name="tbChartHeader" Text="ChartHeader" FontSize="30" FontWeight="ExtraBold" HorizontalAlignment="Stretch" TextAlignment="Center" TextWrapping="Wrap"/>

                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                    <chart:RadPieChart x:Name="chart" ClipToBounds="False" PaletteName="DefaultDark" Margin="38,38,38,0" Height="270" Width="270">
                        <chart:DoughnutSeries x:Name="MyDoughnutSeries" ShowLabels="True" >
                            <chart:DoughnutSeries.ValueBinding>
                                <chart:PropertyNameDataPointBinding PropertyName="Value"/>
                            </chart:DoughnutSeries.ValueBinding>
                            <chart:DoughnutSeries.LegendTitleBinding >
                                <chart:PropertyNameDataPointBinding PropertyName="Title"/>
                            </chart:DoughnutSeries.LegendTitleBinding>
                        </chart:DoughnutSeries>
                    </chart:RadPieChart>

                    <primitives:RadLegendControl LegendProvider="{Binding ElementName=chart}" VerticalAlignment="Center">
                        <primitives:RadLegendControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel Orientation="Vertical"/>
                            </ItemsPanelTemplate>
                        </primitives:RadLegendControl.ItemsPanel>
                        <primitives:RadLegendControl.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <Ellipse Fill="{Binding Fill}" Stroke="{Binding Stroke}"
                                    StrokeThickness="1" Width="10" Height="10"/>
                                    <TextBlock Text="{Binding Title}" Foreground="{Binding Fill}"
                                    Margin="10,0,0,0" FontStyle="Italic"/>
                                </StackPanel>
                            </DataTemplate>
                        </primitives:RadLegendControl.ItemTemplate>
                    </primitives:RadLegendControl>

                </StackPanel>
            </StackPanel>
        </ScrollViewer>

 

 

解释一下,之所以在最外层用ScrollViewer包一下,是因为怕有些设备宽高不够用、或者用户将窗口调节的非常小,导致不能查看的情况。最好把滚动条隐藏了,这样好看。

开头引用xmlns:chart="using:Telerik.UI.Xaml.Controls.Chart"

 

后台,我的操作逻辑是,通过上一篇的签到数据,选择一行数据,然后生成累计的签到图。

所以先写一个LoadData的函数

        private void LoadChartData(SignInSummaryData sid)
        {
            var chartItems = new List<ChartDataPoint>();
            if ((Application.Current as App).strCurrentLanguage.ToLower().Equals("zh-cn"))
            {
                // Create a new chart data point for each value you want in the PieSeries
                var sliceOne = new ChartDataPoint { Value = sid.SignInAttendanceRate, Title = "到席" };
                var sliceTwo = new ChartDataPoint { Value = 1 - sid.SignInAttendanceRate, Title = "缺席" };

                // Add those items to the list
                chartItems.Add(sliceOne);
                chartItems.Add(sliceTwo);

                tbChartHeader.Text = sid.Name + "(" + sid.PersonId + ")" + sid.SignInItemName + sid.SignInItemTimes + "签到情况";
            }
            else
            {
                // Create a new chart data point for each value you want in the PieSeries
                var sliceOne = new ChartDataPoint { Value = sid.SignInAttendanceRate, Title = "Attendance" };
                var sliceTwo = new ChartDataPoint { Value = 1 - sid.SignInAttendanceRate, Title = "Absence" };

                // Add those items to the list
                chartItems.Add(sliceOne);
                chartItems.Add(sliceTwo);

                tbChartHeader.Text = sid.Name + "(" + sid.PersonId + ") " + sid.SignInItemName + sid.SignInItemTimes + " Attendance Chart";
            }

            MyDoughnutSeries.ItemsSource = chartItems;

        }

 

 

然后在 OnNavigatedTo 中调用:

protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            SignInSummaryData sid = null;
            if (e.Parameter != null)
            {
                sid = e.Parameter as SignInSummaryData;
                LoadChartData(sid);
            }
        }

 

所以和上一篇文章联系气来,连贯的操作gif如下图

 

技术分享图片

 

UWP 使用Telerik Chart控件

标签:htm   viewer   情况   cat   str   rap   效果   var   tla   

原文地址:https://www.cnblogs.com/lonelyxmas/p/8593938.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!