Infragistics XamWebChart系列填充颜色装订

时间:2011-03-22 22:00:57

标签: c# silverlight silverlight-4.0 charts infragistics

我有 ControlTemplate ,其中包含 XamWebChart 。对于在我的系列中创建的每个饼图切片,我希望它的填充颜色绑定到Value和Label来自的相同源。

我的饼图的当前xaml如下所示:

<ControlTemplate x:Key="DataLocation">
 <Viewbox 
 Width="{TemplateBinding Width}" 
 Height="{TemplateBinding Height}"                
 Stretch="Fill">
 <Grid>
 <Grid.Resources>
 <Style x:Key="PieChartSeriesStyle" TargetType="Chart:Series">
  <Setter Property="Stroke" Value="#00FFFFFF" />                            
  <Setter Property="Marker">
    <Setter.Value>
     <Chart:Marker Foreground="#FFFFFFFF" />
    </Setter.Value>
  </Setter>
  </Style>
</Grid.Resources>
<Chart:XamWebChart x:Name="PieChart" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
  <Chart:XamWebChart.Scene>
   <Chart:Scene>
    <Chart:Scene.GridArea>
      <Chart:GridArea BorderThickness="0" Background="#00FFFFFF" />
    </Chart:Scene.GridArea>
    </Chart:Scene>
   </Chart:XamWebChart.Scene>

<Chart:XamWebChart.Series>
  <Chart:Series 
   ChartType="Pie" 
   Style="{StaticResource PieChartSeriesStyle}" 
   DataMapping="Value=Amount;Label=Identifier"                                
   DataSource="{Binding Path=ToolTip.Details}" 
    Fill="{Binding DetailItem.ColorProvider.BackgroundColor}">
  </Chart:Series>                                   
</Chart:XamWebChart.Series>                        
</Chart:XamWebChart>
</Grid>
</Viewbox>
</ControlTemplate>

绑定到此ControlTemplate的对象是:

public sealed class GeographyDataItem{
        public IEnumerable<GeographyDataDetailItem> Details
        {
            get { return _details; }
        }

}

带孩子:

public sealed class GeographyDataDetailItem
    {
        private readonly IColorProvider _colorProvider;

        public IColorProvider ColorProvider
        {
            get { return _colorProvider; }
        }

        public string Identifier { get;  private set; }
        public double Amount { get; private set; }

        public GeographyDataDetailItem(string identifier, double amount, IColorProvider colorProvider)
        {
            _colorProvider = colorProvider;
            Identifier = identifier;
            Amount = amount;
        }        
    }

IColorProvider是:

public interface IColorProvider
{
    Color ForegroundColor { get; }
    Color BackgroundColor { get; }
}

ControlTemplate绑定设置为绑定到 GeographyDataItem 元素。

我遇到的唯一问题是绑定 GeographyDataDetailItem ForegroundColor 属性。 IColorProvider 到Pie Data Series的 Fill 属性。我不知道该如何解决这个问题。

1 个答案:

答案 0 :(得分:2)

我无法在XAML中实现我想要的功能,但是能够在控制代码隐藏中支持这个用例。

我的解决方案是:

...
<Chart:XamWebChart.Series>
 <Chart:Series ChartType="Pie"
   Style="{StaticResource PieChartSeriesStyle}"
   DataMapping="Value=Amount;Label=Identifier"
   DataSource="{Binding Path=ToolTip.Details}" 
   Loaded="SeriesLoaded" /> 
<!-- Loaded Event Added !-->
...

和Code beind:

  private void SeriesLoaded(object sender, RoutedEventArgs e)
    {
        var series = (Series) sender;
        ColorDataPointSeries(series);
    }

    /// <summary>
    /// Colors in <see cref="DataPoint"/> from the bound DataSource for the pie chart layer control.
    /// </summary>
    /// <param name="series"></param>
    private static void ColorDataPointSeries(Series series)
    {
        //If nothing is bound there is nothing to do.
        if (null == series.DataSource)
            return;

        var dataSource = ((IEnumerable<GeographyDataDetailItem>) series.DataSource).ToList();
        var dataPoints = series.DataPoints;

        //Note, I am depending on the control to have the exact number of dataPoints as dataSource elements here.
        for (var sourceIndex = 0; sourceIndex < dataSource.Count(); sourceIndex++)
        {
            //Iterate through each dataSoruce, looking for a color provider.
            //If one exists, change the Fill property of the control DataPoint.
            var colorProvider = dataSource[sourceIndex].ColorProvider;
            if (null != colorProvider)
                dataPoints[sourceIndex].Fill = new SolidColorBrush(colorProvider.BackgroundColor);
        }
    }            

因此,现在每次加载XamWebChart进行渲染时,都会从数据源应用填充颜色。绑定侦听属性更改是没有必要的,因为我不希望在渲染后颜色发生变化。

知道在XAML中使用它的替代方法,以减少此代码隐藏,这将是很好的;但是,暂时这解决了我的问题。