Is it possible to bind to the fill property in the DataMapping for a series like so:
<igChart:Series ChartType="Scatter" DataSource="{Binding Path=MinMaxPoints}" DataMapping="ValueX=X;ValueY=Y;Fill=LineColor">
Where LineColor is a brush specifying the color for the DataPoint. I really need to be able to do this using only xaml and no code-behind.
I can't help you do this with absolutely no code behind, but I did find a way that works well with MVVM with minimal code behind.
The trick is that you can't use DataMapping with Fill, but you can with ToolTip and that can be any object. So, if we map a tooltip object that will display the right text, we can also use it to carry along information about Fill, and we can set the fill brush when data binding is complete.
So, let's define a chart, with an event handler for data binding:
<Chart:XamChart Name="xamChart1" Grid.Row="0" Grid.Column="0" View3D="True" DataBind="xamChart1_DataBind">
Now, let's set up the data binding for our series:
<Chart:Series ChartType="Pie" DataSource="{Binding ComplianceValues}" DataMapping="Value = Value; Label = Label; ToolTip = ToolTipAndFill">
When the data binding is complete, the ToolTip property will have been filled in from the ToolTipAndFill property and then the event handler is executed. At this point we can iterate over all of the data points and set the fill value.
private void xamChart1_DataBind( object sender, DataBindEventArgs e )
{
foreach ( DataPoint point in xamChart1.Series[0].DataPoints ) ToolTipAndFill toolTipAndFill = point.ToolTip as ToolTipAndFill; if ( toolTipAndFill != null )
point.Fill = toolTipAndFill.Brush;
}
The ToolTipAndFill class is as follows:
public class ToolTipAndFill
public string ToolTip { get; set; } public object Fill { get; set; } public Color Color get
if ( Fill is Color )
return (Color) Fill; return (Color) ColorConverter.ConvertFromString( Fill.ToString() ); }
} public Brush Brush get
{return Fill as Brush ?? new SolidColorBrush( Color );
public override string ToString() return ToolTip;
Note that Fill is an object and can be set to a Brush, a Color, or any string which can be converted to a Color (see below).
The rest is pretty straight foreward. Here's the ComplianceValues class:
public class ComplianceValues
public int Value { get; set; } public string Label { get; set; } public ToolTipAndFill ToolTipAndFill { get; set; }
Here's the propery in my view model that is being bound to the chart:
public ComplianceValues[] ComplianceValues { get; set; }
And here's how I initialize the ComplianceValues to be databound to the pie chart (hard coded for this example). Note the Fill values are strings which can be converted to Color values and from there to a Brush:
ComplianceValues = new[] new ComplianceValues { Label = "Non-compliant", Value = 1, ToolTipAndFill= new ToolTipAndFill { ToolTip="25% Non-compliant", Fill= "#FFD70005"} }, new ComplianceValues { Label = "Compliant", Value = 3,ToolTipAndFill= new ToolTipAndFill {ToolTip="75% Compliant", Fill= "#FF6CA224" }}
I hope all this comes through OK.
Mike
Have you tested your sample? It tried it, but got no result...