使用Chart FX for WPF简化图表(一)

    有时候你须要漂亮的图表和大量比较绚丽的动画效果,可是有时候又没有必要展现太多视觉上的控件,想要最小化图表空间,使用WPF图表控件Chart FX for WPF就能够很好的实现这两种需求。 app

    好比说你有一系列的产品,你想要展现关于这些产品一些列的信息,包括产品名称、版本等。 动画

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>


 

如今在咱们上面获得的示例的基础上进行图表的简化: this

<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>

    在这一步中,咱们作了一些简单的变化,好比说隐藏了里那个轴和标记,同时还设置了一些笔刷,值得注意的是,在这里的plotarea一般是指页边空白包围,可是在一些小的图表中,不须要这个页边空白。所以咱们能够将图表的高度设置到20。 调试

    咱们也简化了图表模版,经过使用预先定义好的样式能够很好实现,可是这个样式容许使用legend box或者是dataview。 code

WPF图表控件Chart FX使用方法系列:如何简化图表

    接下来就是一些调试就能够完成了,注意在默认的状况下,列表框将会改变选定项的前景色为白色,在DataTemplate中须要一个触发器来完成这个操做: ip

<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 实际上应用于 ContentPresenter,咱们须要触发器找到ListBoxItem类型的第一个ancestor。 ci

WPF图表控件Chart FX使用方法系列:如何简化图表

    虽说这个图表很小,可是依然支持鼠标的停靠操做,显示提示信息,具体代码以下: get

<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>
相关文章
相关标签/搜索