如何自定義visifire圖表主題和樣式?
visifire中內建了5種主題,然而在實際專案中,你可以根據需要自定義visifire圖表主題。今天,我們一起來看看如何自定義visifire圖表主題和樣式。
如何使用一個預設主題:
visifire提供了5種內建主題,分別是:
1) Theme1.xaml
2) Theme2.xaml
3) Theme3.xaml
4) Theme4.xaml
5) Theme5.xaml
要使用visifire提供的主題,你必須設定Chart XAML中的Theme屬性,程式碼如下:
<vc:Chart xmlns:vc="clr-namespace:Visifire.Charts;assembly=SLVisifire.Charts"
Theme="Theme1" Width="500" Height="300">
如何在silverlight程式中建立自定義主題:
第1步:建立一個silverlight託管程式碼應用程式。
第2步:一旦在VS中建立了新的專案,找到Page.xaml檔案,在"LayoutRoot" Grid 中編寫一個 chart XAML,並在<UserControl>標籤中將寬和高分別改為"500"和"300"。
藍色部分為修改 Page.xaml部分。
注:vc是用於建立Visifire圖表物件的名稱空間。
<UserControl x:Class="SLVisifireApp.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vc="clr-namespace:Visifire.Charts;assembly=SLVisifire.Charts"
Width="500" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<vc:Chart xmlns:vc="clr-namespace:Visifire.Charts;assembly=SLVisifire.Charts" Width="500" Height="300" Theme="Theme1" >
<vc:Chart.Titles>
<vc:Title Text="Global Fortune 5 Companies 2007" />
</vc:Chart.Titles>
<vc:Chart.AxesX>
<vc:Axis Title="Companies" />
</vc:Chart.AxesX>
<vc:Chart.AxesY>
<vc:Axis Title="Revenue in Million dollars" />
</vc:Chart.AxesY>
<vc:Chart.Series>
<vc:DataSeries LegendText="Series1" RenderAs="Column" AxisYType="Primary" >
<vc:DataSeries.DataPoints>
<vc:DataPoint AxisXLabel="Wall-Mart" YValue="351139" />
<vc:DataPoint AxisXLabel="Exxon Mobil" YValue="345254" />
<vc:DataPoint AxisXLabel="Shell" YValue="318845" />
<vc:DataPoint AxisXLabel="BP" YValue="274316" />
<vc:DataPoint AxisXLabel="General Motors" YValue="207349" />
</vc:DataSeries.DataPoints>
</vc:DataSeries>
</vc:Chart.Series>
</vc:Chart>
</Grid>
</UserControl>
在添加了Chart XAML後,Page.xaml的效果如圖所示:
第3步:
在同一個應用程式域中建立一個自定義主題。
右鍵單擊SilverlightApplication資料夾,選擇Add > New Item,便會出現一個新的Add New Item 對話方塊,選擇“Silverlight User Control”,將其命名為“MyTheme.xaml”,點選"Add",如圖所示:
第4步:
在Project Explorer中展開MyTheme.xaml,將顯示MyTheme.xaml.cs檔案。因為我們不需要託管程式碼檔案時,刪除MyTheme.xaml.cs檔案。
第5步:
一旦你點選“Add”按鈕,便建立了MyTheme.xaml檔案。刪除XAML檔案中的所有內容,以ResourceDictionary取代它。
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vc="clr-namespace:Visifire.Charts;assembly=SLVisifire.Charts"
>
</ResourceDictionary>
第6步:
在ResourceDictionary 標籤中為圖表元素建立圖表樣式。利用下面的程式碼建立圖表和DataSeries的樣式。
<Style x:Key="Chart" TargetType="vc:Chart">
<Setter Property="Background" Value="Gray"/>
<Setter Property="View3D" Value="True"/>
<Setter Property="AnimationEnabled" Value="False"/>
</Style>
<Style x:Key="DataSeries" TargetType="vc:DataSeries">
<Setter Property="RadiusX" Value="5"/>
<Setter Property="RadiusY" Value="5"/>
<Setter Property="Color" Value="Orange"/>
</Style>
第7步:
一旦建立主題檔案,找到MyTheme.xaml屬性,修改BuildAction to Content。
第8步:
將主題新增至Page.xaml,如下圖紅色邊框部分:
第9步:
執行Silverlight應用程式,便可檢視到visifire圖表的自定義效果:
下載visifire:http://www.evget.com/zh-CN/product/2648/feature.aspx