如何设置图表样式以省略图表周围的多余图表区域

时间:2013-05-06 16:25:58

标签: silverlight charts silverlight-toolkit

我正在使用http://silverlight.codeplex.com/中的Silverlight Toolkit,并且我遇到了样式问题。根据微软的文档,我看到图表由4个基本组件组成。

  1. ChartAreaStyle ==>格
  2. LegendStyle ==>图例(类似于具有标题的ItemsControl的控件)
  3. PlotAreaStyle ==>格
  4. TitleStyle ==>标题(内容控制)
  5. ref http://msdn.microsoft.com/en-us/expression/dd433476.aspx

    我的问题是

    如何使用图表本身填充图表容器,而不是使用空的周围区域,如果可能,省略图例?

    我仍然试图围绕xaml和控制模板等等。我知道它可能是用它完成的,我只是不知道如何。

    以下是我想要实现的一个示例:

    Courtesy of Telerik's awesome controls

    以下是现在的样子:

    Horrible

1 个答案:

答案 0 :(得分:1)

所以我从Toolkit源代码中复制了Chart控件的样式,并从模板中删除了多余的元素。

Silverlight line chart

样式定义看起来如此:

<UserControl.Resources>
    <Style x:Key="ChartWithoutPaddings" TargetType="chart:Chart">
        <Setter Property="Padding" Value="0" />
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="ChartAreaStyle">
            <Setter.Value>
                <Style TargetType="Panel">
                    <Setter Property="MinWidth" Value="100" />
                    <Setter Property="MinHeight" Value="20" />
                </Style>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="chart:Chart">
                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                        <chartingprimitives:EdgePanel x:Name="ChartArea" Style="{TemplateBinding ChartAreaStyle}">
                            <Grid Canvas.ZIndex="-1" Style="{TemplateBinding PlotAreaStyle}" />
                        </chartingprimitives:EdgePanel>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="EmptyDataPoint" TargetType="Control">
        <Setter Property="Background" Value="Black" />
        <Setter Property="Template" Value="{x:Null}" />
    </Style>

    <Style x:Key="OnePixelLine" TargetType="Polyline">
        <Setter Property="StrokeThickness" Value="1" />
    </Style>   
</UserControl.Resources>

您还应该隐藏轴并指定图表的高度和宽度:

<chart:Chart Style="{StaticResource ChartWithoutPaddings}"
             VerticalAlignment="Center" HorizontalAlignment="Center"
             Width="200" Height="30">
    <chart:LineSeries ItemsSource="{Binding Items}" IndependentValuePath="Title" DependentValuePath="Value" 
                      DataPointStyle="{StaticResource EmptyDataPoint}" PolylineStyle="{StaticResource OnePixelLine}"  />

    <chart:Chart.Axes>
        <chart:CategoryAxis Orientation="X" Height="0" Opacity="0" />
        <chart:LinearAxis Orientation="Y" Width="0" Opacity="0" />
    </chart:Chart.Axes>
</chart:Chart>
相关问题