定义silverlight报表样式-Styling a Silverlight Chart

news/2024/7/1 20:19:17
定义silverlight报表样式-Styling a Silverlight Chart
A article on how to style a standard Silverlight chart to look like the Google Analytics chart.
  • Download source code - 11.1 KB

GoogleAnalyticsGraph

Introduction

I love Google Analytics! I use it to monitor every site I own… Google’s chart looks very slick! Here is a walkthrough on how to style your Silverlight chart to look like the Google Analytics chart!

Before we start

We need some data. I created very basic TrafficInfo and TrafficInfoCollection objects with some dummy data that I can bind to.

public class TrafficInfo
{
public DateTime Date { get; set; }
public int Visits { get; set; }
}

We also need the Silverlight Toolkit (I used the port to WPF created by Jaime Rodriquez).

Let's start with the basics

Add the following two namespaces:

xmlns:charting="clr-namespace:Microsoft.Windows.Controls.DataVisualization.
Charting;assembly=Microsoft.Windows.Controls.DataVisualization"
xmlns:datavis="clr-namespace:Microsoft.Windows.Controls.DataVisualization;
assembly=Microsoft.Windows.Controls.DataVisualization"

Now, let's create a simple line chart:

<charting:Chart Width="800" Height="175">
<charting:Chart.Series>
<charting:LineSeries IsSelectionEnabled="True"
Title="Visits"
ItemsSource="{StaticResource TrafficInfo}"
IndependentValueBinding="{Binding Date}"
DependentValueBinding="{Binding Visits}" />
</charting:Chart.Series>
</charting:Chart>

And here is our master piece:

SilverlightChartTry1.jpg

Not bad, but…

Style, style, style

Let's start styling our chart… I will split the styling into two parts; the first part will be specific to line charts, and the second part might be relevant to other types of charts too…

Styling the LineSeries

Each LineSeries has a PolylineStyle property. The PolylineStyle controls how the line looks. Here is our GooglePolylineStyle:

<Style x:Key="GooglePolylineStyle" TargetType="Polyline">
<Setter Property="StrokeThickness" Value="5"/>
</Style>

And, here is the style for the LineDataPoint:

<Style x:Key="GoogleLineDataPointStyle" TargetType="charting:LineDataPoint">
<Setter Property="Background" Value="#0077CC" />
<Setter Property="BorderBrush" Value="White"/>
<Setter Property="BorderThickness" Value="2"/>
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="charting:LineDataPoint">
<Grid x:Name="Root" Opacity="1">
<ToolTipService.ToolTip>
<StackPanel Margin="2,2,2,2">
<ContentControl Content="{TemplateBinding IndependentValue}"
ContentStringFormat="{}{0:MMMM d, yyyy}"/>
<ContentControl Content="{TemplateBinding DependentValue}"
ContentStringFormat="Visits {0:###,###,###}"/>
</StackPanel>
</ToolTipService.ToolTip>
<Ellipse StrokeThickness="{TemplateBinding BorderThickness}"
Stroke="{TemplateBinding BorderBrush}"
Fill="{TemplateBinding Background}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

Also update the chart’s LineSeries to use our newly created styles:

<charting:LineSeries IsSelectionEnabled="True"
PolylineStyle="{StaticResource GooglePolylineStyle}"
DataPointStyle="{StaticResource GoogleLineDataPointStyle}"
MarkerHeight="10" MarkerWidth="10"
Title="Visits"
ItemsSource="{StaticResource TrafficInfo}"
IndependentValueBinding="{Binding Date}"
DependentValueBinding="{Binding Visits}" />

And, here is the result:

GoogleLineDataPoint.jpg

There are a few things to notice here. Each DataPoint has the following properties you can use and display in your tooltip:

  • DependentValue
  • FormattedDependentValue
  • IndependentValue
  • FormattedIndependentValue

Each LineSeries can specify the DataPoint marker size using MarkerWidth and MarkerHeight.

Styling the chart

I want to remove the title and the ledger of the chart. There are two options to removing these items! You can create new styles for the title and the ledger that sets its visibility to Collapsed. (This trick also works if you don’t want DataPointmarkers.)

<Style x:Key="GoogleNoTitle" TargetType="datavis:Title">
<Setter Property="Visibility" Value="Collapsed"/>
</Style>

Then, just set the TitleStyle and LedgerStyle properties on the chart:

TitleStyle="{StaticResource GoogleNoTitle}"

The seconds method of removing these is to rather create a new ControlTemplate for the chart and remove them permanently!

<Style x:Key="GoogleChart" TargetType="charting:Chart">
<Setter Property="PlotAreaStyle">
<Setter.Value>
<Style TargetType="Grid">
<Setter Property="Background" Value="White" />
</Style>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="charting:Chart">
<Border
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="10">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="1" Margin="0,15,0,15">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid x:Name="ChartArea"
Style="{TemplateBinding ChartAreaStyle}">
<Grid x:Name="PlotArea"
Style="{TemplateBinding PlotAreaStyle}"
Margin="0,0,0,0" >
<Grid x:Name="GridLinesContainer" />
<Grid x:Name="SeriesContainer" />
<Border Margin="0,0,0,0"
BorderBrush="#FF919191"
BorderThickness="0,1,0,1"/>
</Grid>
</Grid>
</Grid>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

The new ControlTemplate merely removes the title and ledger! The last part of the chart we need to style are the axes. If you look at the Google chart, you will notice that the vertical grid lines indicate where a week starts; how difficult would this be?

Each chart has an Axes property, which can contain multiple axis! Here are some of the axis properties I used:

LabelStringFormatMMMM d, yyyyChanges the format of the labels
ShowGridLinesTrueShows the grid lines
ShowTickMarksTrueShows the grid line markers
ShouldIncludeZeroTrueScales from 0
IntervalTypeDaysIndicates what each unit on the axis is
Interval7Interval between points on the axis
IntervalOffset1Offset used in creating the axis
IntervalOffsetTypeDaysOffset type

Here is the markup:

<charting:Chart.Axes>
<charting:Axis Orientation="Horizontal"
AxisType="DateTime" ShowGridLines="True"
ShowTickMarks="True" LabelStringFormat="MMMM d, yyyy"
IntervalType="Days" Interval="7"
IntervalOffset="1" IntervalOffsetType="Days"
Style="{StaticResource GoogleAxisStyle}" />
<charting:Axis Orientation="Vertical" AxisType="Linear" ShowTickMarks="False"
Interval="4000" IntervalType="Number"
ShowGridLines="True" ShouldIncludeZero="True"
Style="{StaticResource GoogleAxisStyle}"/>
</charting:Chart.Axes>

And that is it.

SilverlightChartDone.jpg

The Silverlight Toolkit Chart control is very flexible and powerful! Try it out, and you will be surprised at the endless ways you can style it!

If you found this article useful or interesting, please vote for it!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

 

http://www.codeproject.com/KB/WPF/WPFSLChart.aspx?display=Print 


posted on 2010-07-26 18:21 搏击的小船 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/guanjie20/archive/2010/07/26/1785491.html


http://www.niftyadmin.cn/n/3613036.html

相关文章

linux input subsystem 架构分析

主要数据结构 struct input_dev { void *private; const char *name; const char *phys; const char *uniq; struct input_id id; unsigned long evbit[NBITS(EV_MAX)]; unsigned long keybit[NBITS(KEY_MAX)]; unsigned long relbit[NBITS(REL_MAX)]; unsigned long absbit[N…

HDU 2020,2021,2024,2028,2029,2030

//Made by syx //Time 2010年7月29日 09:55:28 // //2020 绝对值排序 //2021 发工资咯&#xff1a;&#xff09; //2024 C语言合法标识符 //2028 Lowest Common Multiple Plus //2029 Palindromes_easy version 回文串 //2030 汉字统计 /*//2030 汉字统计 #include <iostrea…

linux内存寻址

本章节介绍linux寻址技术&#xff0c;详细描述80x86微处理器怎样进行芯片级的内存寻址&#xff0c;linux又是如何寻址硬件的。 1. linux内存地址 80x86微处理器下主要有三种不同的地址&#xff1a;逻辑地址&#xff0c;线性地址&#xff0c;物理地址。 逻辑地址&#xff1a; 主…

Linux Input Device 介紹: APIs

Linux Input Device 介紹: APIs jollen 發表於 April 8, 2009 12:18 PM Linux 的 Input Device 是重要的一個 subsystem&#xff0c;在進行實例介紹前&#xff0c;先大略了解一下相關的 API。 Linux Input Device &#xfeff;input.c是Linux的”input”驅動程式&#xff0c;主…

Android 如何破解兼容性困局

最新的消息表明&#xff0c;Android 手机的销售量超越 iPhone&#xff0c;虽然在整体市场占有率上&#xff0c;仍然不及竞争对手&#xff0c;但是却已经初现王者风范&#xff0c;一些文章也预测 Android 最终会稳坐智能手机第一把交椅。Android 的确是十分有潜力的&#xff0c;…

webpack学习路径(01.webpack的配置及使用)

对于webpack&#xff0c;我的理解是一种工具提供了友好的前端模块化开发的支持&#xff0c;可以解决代码压缩混淆&#xff0c;浏览器兼容性问题&#xff0c;以及提升性能等主要功能&#xff0c; 安装配置过程&#xff1a; 首先创建一个根目录&#xff08;随便创建一个文件夹&…