Infragistics XamWebChart 系列填充颜色绑定

发布于 2024-10-25 22:55:08 字数 2833 浏览 10 评论 0原文

我有一个包含 XamWebChartControlTemplate。对于我的系列中创建的每个饼图切片,我希望其填充颜色绑定到值和标签的同一来源。

我的饼图的当前 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 元素。

我遇到的唯一问题是绑定 GeographyDataDetailItemForegroundColor 属性。 IColorProvider 到饼图数据系列的 Fill 属性。我不知道该怎么做。

I have a ControlTemplate that contains a XamWebChart. For each pie slice created in my series, I would like its fill color bound to the same source the Value and Label are coming from.

The current xaml for my pie chart looks like:

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

The objects that are Bound to this ControlTemplate are:

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

}

With children:

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

Where IColorProvider is:

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

The ControlTemplate binding is set to bind to GeographyDataItem elements.

The only issue I am having is binding the ForegroundColor property of GeographyDataDetailItem . IColorProvider to the Fill property of the Pie Data Series. I am not sure how to go about this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我无法在 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 !-->
...

代码如下:

  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 中使用此方法以减少此代码隐藏的替代方案,那就太好了;不过,暂时这解决了我的问题。

I was unable to achieve what I wanted in XAML, but was able to support this use case in the control code-behind.

My solution is:

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

And the 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);
        }
    }            

So, now each time a XamWebChart is loaded for render, the Fill color is applied from the data source. Binding listening to property changes is not necessary as I do not expect the color to change following the render.

It would be nice to know an alternative to have this in XAML, to reduce this code-behind; however, for the time being this solved my problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文