原創(chuàng)|使用教程|編輯:郝浩|2013-03-06 10:46:11.000|閱讀 1729 次
概述:有時候你需要漂亮的圖表和大量比較絢麗的動畫效果,但是有時候又沒有必要展示太多視覺上的控件,想要最小化圖表空間,使用WPF圖表控件Chart FX for WPF就可以很好的實現(xiàn)這兩種需求。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
有時候你需要漂亮的圖表和大量比較絢麗的動畫效果,但是有時候又沒有必要展示太多視覺上的控件,想要最小化圖表空間,使用WPF圖表控件Chart FX for WPF就可以很好的實現(xiàn)這兩種需求。
比如說你有一系列的產(chǎn)品,你想要展示關(guān)于這些產(chǎn)品一些列的信息,包括產(chǎn)品名稱、版本等。
public class ProductInfo { public string Name { get; set; } public string Version { get; set; } public List<ProductDownloads> Downloads { get; set; } public string LatestRelease { get; set; } } public class ProductDownloads { public double Count { get; set; } } For simplicity we will use a templated listbox to show multiple columns so our first approach to the problem will look like this <ListBox Grid.IsSharedSizeScope="true"> <ListBox.ItemTemplate> <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" SharedSizeGroup="ColumnName"/> <ColumnDefinition Width="Auto" SharedSizeGroup="ColumnVersion"/> <ColumnDefinition Width="150" /> <ColumnDefinition Width="Auto" SharedSizeGroup="ColumnRelease"/> </Grid.ColumnDefinitions> <TextBlock VerticalAlignment="Center" Grid.Column="0" Text="{Binding Path=Name}" Margin="4,0"/> <TextBlock VerticalAlignment="Center" Grid.Column="1" Text="{Binding Path=Version}" Margin="4,0"/> <cfx:Chart x:Name="chart1" Grid.Column="2" ItemsSource="{Binding Path=Downloads}" Gallery="Line" Margin="4,0"> <cfx:Chart.Series> <cfx:SeriesAttributes BindingPath="Count"/> </cfx:Chart.Series> <cfx:Chart.LegendBox> <cfx:LegendBox Visibility="Collapsed"/> </cfx:Chart.LegendBox> </cfx:Chart> <TextBlock VerticalAlignment="Center" Grid.Column="3" Text="{Binding Path=LatestRelease}" Margin="4,0"/> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
現(xiàn)在在我們上面得到的示例的基礎(chǔ)上進行圖表的簡化:
<cfx:Chart x:Name="chart1" Grid.Column="2" ItemsSource="{Binding Path=Downloads}" Gallery="Line" Margin="4,0" Height="20"> <cfx:Chart.Series> <cfx:SeriesAttributes BindingPath="Count" Stroke="Black" StrokeThickness="1"> <cfx:SeriesAttributes.Marker> <cfx:MarkerAttributes Visibility="Collapsed"/> </cfx:SeriesAttributes.Marker> </cfx:SeriesAttributes> </cfx:Chart.Series> <cfx:Chart.LegendBox> <cfx:LegendBox Visibility="Collapsed"/> </cfx:Chart.LegendBox> <cfx:Chart.AxisY> <cfx:Axis Visibility="Collapsed"/> </cfx:Chart.AxisY> <cfx:Chart.AxisX> <cfx:Axis Visibility="Collapsed"/> </cfx:Chart.AxisX> <cfx:Chart.PlotArea> <cfx:PlotAreaAttributes Margin="0" AxesStyle="None" Background="{x:Null}" Stroke="{x:Null}"/> </cfx:Chart.PlotArea> <cfx:Chart.Template> <ControlTemplate> <Border cfx:Chart.PanelName="Plot"/> </ControlTemplate> </cfx:Chart.Template> </cfx:Chart>
在這一步中,我們做了一些簡單的變化,比如說隱藏了里那個軸和標(biāo)記,同時還設(shè)置了一些筆刷,值得注意的是,在這里的plotarea通常是指頁邊空白包圍,但是在一些小的圖表中,不需要這個頁邊空白。因此我們可以將圖表的高度設(shè)置到20。
我們也簡化了圖表模版,通過使用預(yù)先定義好的樣式可以很好實現(xiàn),但是這個樣式允許使用legend box或者是dataview。
接下來就是一些調(diào)試就可以完成了,注意在默認(rèn)的情況下,列表框?qū)淖冞x定項的前景色為白色,在DataTemplate中需要一個觸發(fā)器來完成這個操作:
<DataTemplate.Triggers> <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True"> <Setter TargetName="series" Property="Stroke" Value="White" /> </DataTrigger> </DataTemplate.Triggers>
因為 DataTemplate 實際上應(yīng)用于 ContentPresenter,我們需要觸發(fā)器找到ListBoxItem類型的第一個ancestor。
雖然說這個圖表很小,但是依然支持鼠標(biāo)的停靠操作,顯示提示信息,具體代碼如下:
<cfx:SeriesAttributes.ToolTips> <cfx:ToolTipAttributes x:Name="tooltips" IsEnabled="False"/> </cfx:SeriesAttributes.ToolTips> <DataTemplate.Triggers> <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True"> <Setter TargetName="series" Property="Stroke" Value="White" /> <Setter TargetName="tooltips" Property="IsEnabled" Value="True" /> </DataTrigger> </DataTemplate.Triggers>
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都控件