Jump to content
Software FX Community

Conditional custom background within DataView


Skredy
 Share

Recommended Posts

Hi,

I would like to change the background color of several DataView cells, depending on values from two different series.

I tried to create a new serie, wich contains the values of the two others, and reference it in my DataView (with his index). But I dont find how to get the right DataItem.

Here is my snippet:

=> where i reference the new serie in the Chart's SeriesAttributesCollection

ItemsSource="{Binding ValuesAndInfluence}"

Visibility="Collapsed" BindingPathX="Datetime" BindingPath="Value"

>

=> in the DataView

Text="{Binding Path=????}"

VerticalAlignment="Center" HorizontalAlignment="Right" Padding="5 2 5 2" />

If I could get the right dataItem, I would finish with a converter for the backgroundcolor...

Thanks for your help!

Link to comment
Share on other sites

I think the snippet got lost in your posting but this is a sample of how to change the background color of your dataview cells, the key of the sample is that in most logical items we generate (and you can style to change the appearance of the bars, legend items, etc.) the DataItem property will typically point to the data you provided to the chart.

Data Model

public class BusinessData
{

  public double Y1 { get; set; }

  public double Y2 { get; set; }

} 

XAML to customize chart, note that we use SeriesField, because of this you will automatically get some nice behaviors, for example if your axis was formatted as currency the ValueText property would reflect that, you could also have used DataField which can be used to place a field from your data even if it is not plotted as a series. Obviously you might want to place the template separately but this helps on sample simplicity.

<cfx:Chart.DataView>

  <cfx:DataView
Visibility="Visible">

  <cfx:DataView.Fields>

  <cfx:DataViewFieldAttributes>

  <cfx:DataViewFieldAttributes.Items>

  <cfx:SeriesField
SeriesIndex="0">

  <cfx:SeriesField.Template>

  <DataTemplate>

  <DataTemplate.Resources>

  <local:BusinessDataColorConverter x:Key="businessDataColorConverter"/>

  </DataTemplate.Resources>

  <TextBlock Text="{Binding Path=ValueText}"
Background="{Binding Path=DataItem,
Converter={StaticResource
businessDataColorConverter}}" Margin="{Binding Path=Margin}"
Foreground="{Binding Path=Foreground}"
FontFamily="{Binding Path=FontFamily}"
FontWeight="{Binding Path=FontWeight}"
FontSize="{Binding Path=FontSize}"
FontStyle="{Binding Path=FontStyle}" HorizontalAlignment="Right"
VerticalAlignment="Center"/>

  <DataTemplate.Triggers>

  <DataTrigger
Binding="{Binding Path=Dimmed}">

  <DataTrigger.Value>

  <sys:Boolean>True</sys:Boolean>

  </DataTrigger.Value>

  <Setter
Property="Opacity" Value="0.25"
/>

  </DataTrigger>

  </DataTemplate.Triggers>

  </DataTemplate>

  </cfx:SeriesField.Template>

  </cfx:SeriesField>

  </cfx:DataViewFieldAttributes.Items>

  </cfx:DataViewFieldAttributes>

  </cfx:DataView.Fields>

  </cfx:DataView>

</cfx:Chart.DataView>

 

Code to populate chart

List<BusinessData> list = new
List<BusinessData>();

list.Add(new
BusinessData() { Y1 = 10, Y2 = 12 });

list.Add(new
BusinessData() { Y1 = 14, Y2 = 11 });

list.Add(new
BusinessData() { Y1 = 11, Y2 = 17 });

 

chart1.Series.Add(new SeriesAttributes("Y1"));

chart1.Series.Add(new SeriesAttributes("Y2"));

chart1.ItemsSource = list;

Color Converter

object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture
{

  BusinessData businessData = (BusinessData) value;
 
if (businessData.Y1 > businessData.Y2)

return Brushes.Yellow;

  else

return Brushes.White;

 }

Regards,

JuanC

Link to comment
Share on other sites

Oups, you're right the snippet disappeared... :/

I tried your solution, it's perfect!

In fact, my series were independent each from the others (not in the same objet and binded to SeriesAttribute with ItemSource), so I added an objet like your BusinessData wich contains all of the collections.

Thanks for your help!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...