两种WPF Toolkit Chart LegendStyle
使用WPF数据可视化组件时候需要定义chart的图例风格:
下面从网上找来的,经过调整可以直接使用:
需要添加以下引用:
}System.Windows.Controls.DataVisualization.Toolkit.dll
}xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit“
}或者xmlns:chartingTookit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
1、 不显示Title和Legend的自定义Style
定义:
<Style x:Key="LegendStyle2" TargetType="Control">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="chartingToolkit:Chart">
<!--<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="10">-->
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--<datavis:Title Style="{TemplateBinding TitleStyle}" Content="{TemplateBinding Title}" />-->
<Grid Margin="0,15,0,15" Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<!--<datavis:Legend x:Name="Legend"
Style="{TemplateBinding LegendStyle}"
Grid.Column="1"
Title="{TemplateBinding LegendTitle}" />-->
<chartingprimitives:EdgePanel x:Name="ChartArea" Style="{TemplateBinding ChartAreaStyle}">
<Grid Style="{TemplateBinding PlotAreaStyle}" Canvas.ZIndex="-1" />
<!-- I removed the border-->
<!--<Border Canvas.ZIndex="10" BorderBrush="#FF919191" BorderThickness="1" />-->
</chartingprimitives:EdgePanel>
</Grid>
</Grid>
<!--</Border>-->
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="chartingToolkit:Chart">
<!--<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="10">-->
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--<datavis:Title Style="{TemplateBinding TitleStyle}" Content="{TemplateBinding Title}" />-->
<Grid Margin="0,15,0,15" Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<!--<datavis:Legend x:Name="Legend"
Style="{TemplateBinding LegendStyle}"
Grid.Column="1"
Title="{TemplateBinding LegendTitle}" />-->
<chartingprimitives:EdgePanel x:Name="ChartArea" Style="{TemplateBinding ChartAreaStyle}">
<Grid Style="{TemplateBinding PlotAreaStyle}" Canvas.ZIndex="-1" />
<!-- I removed the border-->
<!--<Border Canvas.ZIndex="10" BorderBrush="#FF919191" BorderThickness="1" />-->
</chartingprimitives:EdgePanel>
</Grid>
</Grid>
<!--</Border>-->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
使用:
<chartingToolkit:Chart DataContext="1,10 2,20 3,30 4,40" Name="chart1" Title="趋势图" Style="{StaticResource LegendStyle2}" >
<chartingToolkit:LineSeries DependentValuePath="X" IndependentValuePath="Y"/>
</chartingToolkit:Chart>
</chartingToolkit:Chart>