RadControls for Windows 8

本文介绍如何使用 Telerik 的 RadCartesianChart 控件创建图表,并展示了如何配置静态数据、设置轴范围、添加线条系列及自定义标签模板、点模板和提示信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

RadControls for Windows 8

http://www.telerik.com/help/wpf/radchartview-populating-with-data-static-data.html


RadCartesianChart chart = new RadCartesianChart();

            chart.HorizontalAxis = new CategoricalAxis();
            chart.VerticalAxis = new LinearAxis() { Maximum = 100 };


            LineSeries line = new LineSeries();
            line.Stroke = new SolidColorBrush(Colors.Blue);
            line.StrokeThickness = 2;
            line.DataPoints.Add(new CategoricalDataPoint() { Value = 20 });
            line.DataPoints.Add(new CategoricalDataPoint() { Value = 40 });
            line.DataPoints.Add(new CategoricalDataPoint() { Value = 35 });
            line.DataPoints.Add(new CategoricalDataPoint() { Value = 40 });
            line.DataPoints.Add(new CategoricalDataPoint() { Value = 30 });
            line.DataPoints.Add(new CategoricalDataPoint() { Value = 50 });
            chart.Series.Add(line);


            LineSeries line2 = new LineSeries();
            line2.Stroke = new SolidColorBrush(Colors.Orange);
            line2.StrokeThickness = 2;
            line2.DataPoints.Add(new CategoricalDataPoint() { Value = 5 });
            line2.DataPoints.Add(new CategoricalDataPoint() { Value = 44 });
            line2.DataPoints.Add(new CategoricalDataPoint() { Value = 3 });
            line2.DataPoints.Add(new CategoricalDataPoint() { Value = 55 });
            line2.DataPoints.Add(new CategoricalDataPoint() { Value = 22 });
            line2.DataPoints.Add(new CategoricalDataPoint() { Value = 11 });
            chart.Series.Add(line2);


            this.LayoutRoot.Children.Add(chart);


提示信息:

http://www.telerik.com/help/silverlight/radchartview-features-tooltip.html


<telerik:RadCartesianChart x:Name="radCartesianChart" Margin="8" Grid.Row="2" Grid.ColumnSpan="2">                               
    <telerik:RadCartesianChart.Behaviors>
    <telerik:ChartTooltipBehavior Placement="Top" />
</telerik:RadCartesianChart.Behaviors>
<telerik:RadCartesianChart.TooltipTemplate>
                <DataTemplate>
                    <Grid>
                        <Path Data="M-1236,-441 L-1180,-441 -1180,-424 -1228,-424 -1230.5,-420 -1233,-424 -1236,-424 z"
                                  Stretch="Fill" Fill="Beige"  StrokeThickness="2"/>
<StackPanel Margin="5,5,5,18">
<StackPanel Orientation="Horizontal">
                                   <TextBlock Text=" 值:" FontWeight="Bold" FontSize="11"/>
                                   <TextBlock Text="{Binding Path=DataItem.Value}" FontWeight="Bold" FontSize="11"/>
<TextBlock Text=" "/>
                       </StackPanel>

<StackPanel Orientation="Horizontal">
                                   <TextBlock Text=" 时间:" FontWeight="Bold" FontSize="11"/>
                                   <TextBlock Text="{Binding Path=DataItem.Category}" FontWeight="Bold" FontSize="11"/>                                 
<TextBlock Text=" "/>
                       </StackPanel>
                        </StackPanel>
                    </Grid>
                </DataTemplate>
            </telerik:RadCartesianChart.TooltipTemplate>


//标签模板

<telerik:LineSeries ShowLabels="True" PointTemplate="{StaticResource PointTemplate1}">
    <telerik:LineSeries.LabelDefinitions>
<telerik:ChartSeriesLabelDefinition>
         <telerik:ChartSeriesLabelDefinition.Template>
             <DataTemplate>
                <TextBlock Text="MyTest"/>
             </DataTemplate>
         </telerik:ChartSeriesLabelDefinition.Template>
     </telerik:ChartSeriesLabelDefinition>
</telerik:LineSeries.LabelDefinitions>
    <telerik:CategoricalDataPoint Category="{x:Null}" IsSelected="False" Label="10" Value="10"/>
    <telerik:CategoricalDataPoint Category="{x:Null}" IsSelected="False" Label="20" Value="20"/>
    <telerik:CategoricalDataPoint Category="{x:Null}" IsSelected="False" Label="30" Value="30"/>
    </telerik:LineSeries>


代码:

                #region //标签模板,显示标签                
                line.LabelDefinitions.Add(new ChartSeriesLabelDefinition { Template = (DataTemplate)XamlReader.Load(CreateItemTemplate()) });
                #endregion


//点模板、标签模板、提示信息

<telerik:RadCartesianChart Margin="10,10,8,0" >    
<telerik:RadCartesianChart.Resources>
               <DataTemplate x:Key="PointTemplate1">
                   <Ellipse x:Name="ellipse" Height="6" Width="6" Fill="Black"/>
               </DataTemplate>
           </telerik:RadCartesianChart.Resources>
    <telerik:RadCartesianChart.Behaviors>
    <telerik:ChartTooltipBehavior Placement="Top" VerticalOffset="20"/>
</telerik:RadCartesianChart.Behaviors>
<telerik:RadCartesianChart.TooltipTemplate>
                <DataTemplate>
                    <Grid>
                        <Path Data="M-1236,-441 L-1180,-441 -1180,-424 -1228,-424 -1230.5,-420 -1233,-424 -1236,-424 z"
                                  Stretch="Fill" Fill="Beige" Stroke="Gray" StrokeThickness="2"/>
<TextBlock Text="Profit for " FontWeight="Bold" FontSize="11"/>
<TextBlock Text="{Binding Path=DataItem.Value}" FontWeight="Bold" FontSize="11"/>
                    </Grid>
                </DataTemplate>
            </telerik:RadCartesianChart.TooltipTemplate>
    <telerik:RadCartesianChart.HorizontalAxis>
    <telerik:CategoricalAxis/>
    </telerik:RadCartesianChart.HorizontalAxis>
    <telerik:RadCartesianChart.VerticalAxis>
    <telerik:LogarithmicAxis LabelOffset="0" LogarithmBase="10" LastLabelVisibility="Visible" LineThickness="1" MajorTickOffset="0" TickThickness="1" ZIndex="0" ShowLabels="False"/>
    </telerik:RadCartesianChart.VerticalAxis>
    <telerik:LineSeries ShowLabels="True" PointTemplate="{StaticResource PointTemplate1}">
    <telerik:LineSeries.LabelDefinitions>
<telerik:ChartSeriesLabelDefinition>
         <telerik:ChartSeriesLabelDefinition.Template>
             <DataTemplate>
                <TextBlock Text="MyTest"/>
             </DataTemplate>
         </telerik:ChartSeriesLabelDefinition.Template>
     </telerik:ChartSeriesLabelDefinition>
</telerik:LineSeries.LabelDefinitions>
    <telerik:CategoricalDataPoint Category="{x:Null}" IsSelected="False" Label="10" Value="10"/>
    <telerik:CategoricalDataPoint Category="{x:Null}" IsSelected="False" Label="20" Value="20"/>
    <telerik:CategoricalDataPoint Category="{x:Null}" IsSelected="False" Label="30" Value="30"/>
    </telerik:LineSeries>
<telerik:LineSeries ShowLabels="True">
    <telerik:CategoricalDataPoint Category="{x:Null}" IsSelected="False" Label="10" Value="50"/>
    <telerik:CategoricalDataPoint Category="{x:Null}" IsSelected="False" Label="1111" Value="30"/>
    <telerik:CategoricalDataPoint Category="{x:Null}" IsSelected="False" Label="30" Value="30"/>
    </telerik:LineSeries>
    </telerik:RadCartesianChart>


//代码添加模板



                var pointTemplate = chartObject.Resources["PointTemplate1"] as DataTemplate; //获取xaml中的模板对象

                #region //标签模板,显示标签                
                line.LabelDefinitions.Add(new ChartSeriesLabelDefinition { Template = (DataTemplate)XamlReader.Load(CreateItemTemplate(c.ToString())) });
                #endregion

 private string CreateItemTemplate(string strColor)
        {
            string strText = string.Format("<TextBlock Text=\"{0}\" Foreground=\"{1}\" Margin=\"0,0,0,0\"/>", "{Binding Label}", strColor);
            StringBuilder template = new StringBuilder();
            template.Append("<DataTemplate ");
            template.Append("xmlns='http://schemas.microsoft.com/winfx/");
            template.Append("2006/xaml/presentation' ");
            template.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>");


            //template.Append("<Grid>");
            //template.Append("<Path Data=\"M-1236,-441 L-1180,-441 -1180,-424 -1228,-424 -1230.5,-420 -1233,-424 -1236,-424 z\" Stretch=\"Fill\" Fill=\"Beige\" Stroke=\"Gray\" StrokeThickness=\"2\"/>");
            //template.Append("<TextBlock Text=\"{Binding Label}\" Foreground=\"{Binding m_listColors}\" Margin=\"0,0,0,0\"/>");
            //template.Append("</Grid>");


            template.Append(strText);


            template.Append("</DataTemplate>");
            return template.ToString();
        }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值