This is a migrated thread and some comments may be shown as answers.

Unable to set Foreground color in a ChartAnnotationLabelDefinition?

6 Answers 761 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
Joe
Top achievements
Rank 2
Iron
Iron
Veteran
Joe asked on 05 Apr 2020, 05:47 PM

My RadCartesianChart uses an AnnotationsProvider showing CartesianPlotBandAnnotations.  I set the LabelDefinitions with a DefaultVisualStyle that had the text as I wanted it.  And it honors every property I set except the text color.  That gets ignored, no matter what I set it to.  

I've attached an image that shows what it looks like.  You can see the region labels (literally named "Top" and "Bottom").  They show up in Italics as I want and using the FontSize I want.  But the color remains grey when I'm trying to set it to white.

Here's my label definition resource

<tk:ChartAnnotationLabelDefinition x:Key="BandLabelDefinition" Location="Inside" VerticalAlignment="Bottom">
    <tk:ChartAnnotationLabelDefinition.DefaultVisualStyle>
        <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource TextBlockStyle}">
            <Setter Property="FontSize"  Value="{Binding Source={core:Settings}, Path=FontSizeSmall}"/>
            <Setter Property="FontStyle" Value="Italic"/>
            <Setter Property="Foreground" Value="White"/>
        </Style>
    </tk:ChartAnnotationLabelDefinition.DefaultVisualStyle>
</tk:ChartAnnotationLabelDefinition>


And here's the bit where I set it in the style of the CartesianPlotBandAnnotation

<Style x:Key="RegionBandStyle"
       TargetType="{x:Type tk:CartesianPlotBandAnnotation}">
 
    <d:Style.DataContext>
        <x:Type Type="core:IProfileRegion" />
    </d:Style.DataContext>
 
    <Setter Property="Label"                     Value="{Binding Name}"/>
    <Setter Property="LabelDefinition"     Value="{StaticResource BandLabelDefinition}"/>

What am I missing here?  How may I set the font color in a label definition?

(Note: I also tried the approach of using the ChartAnnotationLabelDefinition.Template property instead of "DefaultVisualStyle".  That didn't help at all)

 


6 Answers, 1 is accepted

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 09 Apr 2020, 05:54 AM

Hello Joe,

Thank you for the provided details.

My guess here is the setting the LabelDefinition thought style will have lower priority. Can you set the LabelDifinition directly to the CartesianPlotBandAnnotation, like in the following code snippet and let me know if it makes any difference?

<telerik:CartesianPlotBandAnnotation From="10" To="25" Axis="{Binding ElementName=verticalAxis}" Label="MY LABEL">
    <telerik:CartesianPlotBandAnnotation.LabelDefinition>
        <telerik:ChartAnnotationLabelDefinition  Location="Inside" VerticalAlignment="Bottom">
            <telerik:ChartAnnotationLabelDefinition.DefaultVisualStyle>
                <Style TargetType="{x:Type TextBlock}" >
                    <Setter Property="FontSize"  Value="35"/>
                    <Setter Property="FontStyle" Value="Italic"/>
                    <Setter Property="Foreground" Value="White"/>
                </Style>
            </telerik:ChartAnnotationLabelDefinition.DefaultVisualStyle>
        </telerik:ChartAnnotationLabelDefinition>
    </telerik:CartesianPlotBandAnnotation.LabelDefinition>
</telerik:CartesianPlotBandAnnotation>

A question arises after looking at the image. Are you using NoXAML binaries? If yes, can you share which theme is merged in your application? I am asking this because this behavior could be related to a specific theme.

Regards,
Dinko
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Joe
Top achievements
Rank 2
Iron
Iron
Veteran
answered on 09 Apr 2020, 06:30 PM

Hi Dinko,

First of all I'm using all the assemblies with Xaml.  e.g.

Telerik.Windows.Controls.for.Wpf.Xaml
Telerik.Windows.Controls.Data.for.Wpf.Xaml

etc etc.

But that's moot because I think I've figured it out.

The labels are children of the annotation, are they not?  If so that explains it perfectly.  I'm setting the Opacity of the PlotBandAnnotation to allow the user to see the plot line through them.  And that Opacity would naturally affect all children, including labels. 

Please confirm that's the case.  Because if it is I'm going to have to figure out how to supply a matching text annotation (with Opacity 1.0) for each and every PlotBand Annotation

0
Joe
Top achievements
Rank 2
Iron
Iron
Veteran
answered on 09 Apr 2020, 07:01 PM
(I can probably just apply the desired opacity to the brush rather than the the Annotation as a whole...)
0
Dinko | Tech Support Engineer
Telerik team
answered on 10 Apr 2020, 01:00 PM

Hi Joe,

You are right. Setting Opacity to the CartesianPlotBandAnnotation will be propagated to its children. As you said in your post, you can set Opacity to the color. Check the following code snippet.

<telerik:CartesianPlotBandAnnotation.Fill>
    <SolidColorBrush Color="Red" Opacity=".5" />
</telerik:CartesianPlotBandAnnotation.Fill>

Regards,
Dinko
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Joe
Top achievements
Rank 2
Iron
Iron
Veteran
answered on 10 Apr 2020, 07:14 PM

Thanks Dinko.   Unfortunately my opacity cannot be a hard-coded number.  It must actually must change depending on whether or not the user has selected the PlotBandAnnotation in question (because I let them select and drag the annotations).  That means I needed to lay out the brush explicitly in verbose XAML

Here is how I am currently setting the opacity in the RegionBandStyle.  This worked fine (but now needs to migrate to the Fill property Brush)

<Setter Property="Opacity">
    <Setter.Value>
        <MultiBinding Converter="{StaticResource CvtEqualToAny}"
                      ConverterParameter="{net:BoolVal False}"
                      FallbackValue="{net:DoubleVal 0}">
 
            <Binding />                                                          <!-- Is THIS region... -->
            <Binding ElementName="Root" Path="ProfileAnalysis.SelectedRegion"/>  <!-- ... the selected region? -->
            <Binding Source="{net:DoubleVal 0.6}"/>                              <!-- Use this Opacity if true -->
            <Binding Source="{net:DoubleVal 0.3}"/>                              <!-- Use this Opacity if false -->
 
        </MultiBinding>
    </Setter.Value>
</Setter>

 

(Yeah, that's basically an IMultiValueConverter that does an if-then and chooses one value or another depending on whether or not the current region is selected.)

But when I try to move that complicated decision to choose the Opacity of the Brush in the Fill property binding.  It doesn't work. 

<Setter Property="Fill">
    <Setter.Value>
        <SolidColorBrush Color="{Binding Usage, Converter={StaticResource CvtUsageToColor}}">
            <SolidColorBrush.Opacity>
                <MultiBinding Converter="{StaticResource CvtEqualToAny}"
                              ConverterParameter="{net:BoolVal False}"
                              FallbackValue="{net:DoubleVal 0}">
 
                    <Binding />                                                          <!-- Is THIS region... -->
                    <Binding ElementName="Root" Path="ProfileAnalysis.SelectedRegion"/>  <!-- ... the selected region? -->
                    <Binding Source="{net:DoubleVal 0.6}"/>                              <!-- Use this Opacity if true -->
                    <Binding Source="{net:DoubleVal 0.3}"/>                              <!-- Use this Opacity if false -->
 
                </MultiBinding>
            </SolidColorBrush.Opacity>
        </SolidColorBrush>
    </Setter.Value>
</Setter>


Suddenly I don't have the datacontext anymore.  I am pretty sure I know why.  Brush is not in the visual tree, so that first "<Binding />" element in the MultBinding finds nothing.  And tricks like FindAncestor to find the "parent" PlotBandAnnotation, also fail for the same reason -- the brush isn't in the visual tree

I've found a StackOverflow question that appears to encounter the same issue.

https://stackoverflow.com/questions/5647055/data-binding-outside-the-visual-tree-data-context-bridging

It's not clear that the poster ever really found a solution to his problem.  I guess I might just be forced to move all this out of XAML and into a view model

0
Accepted
Dinko | Tech Support Engineer
Telerik team
answered on 13 Apr 2020, 01:21 PM

Hi Joe,

I would go in a different way. In your post, you mentioned that you had implemented a drag-drop of annotation. I am assuming that the user is holding the mouse while dragging the annotation. With this in hand, on the mouse down event (for example), you can add the opacity of the annotation. When the user releases the annotation, end dragging, or mouse up, you can remove the opacity. Is this approach achievable in your application?

As for your approach. You can try to add RelativeSource binding to find the parent annotation. I am not sure that if this is going to work, but you can try and let me know the outcome.

<MultiBinding Converter="{StaticResource CvtEqualToAny}"
            ConverterParameter="{net:BoolVal False}"
            FallbackValue="{net:DoubleVal 0}">
    <Binding RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType=telerik:CartesianPlotBandAnnotation}"/>
    <!-- Is THIS region... -->
    <Binding ElementName="Root" Path="ProfileAnalysis.SelectedRegion"/>
    <!-- ... the selected region? -->
    <Binding Source="{net:DoubleVal 0.6}"/>
    <!-- Use this Opacity if true -->
    <Binding Source="{net:DoubleVal 0.3}"/>
    <!-- Use this Opacity if false -->
</MultiBinding>

Regards,
Dinko
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
ChartView
Asked by
Joe
Top achievements
Rank 2
Iron
Iron
Veteran
Answers by
Dinko | Tech Support Engineer
Telerik team
Joe
Top achievements
Rank 2
Iron
Iron
Veteran
Share this question
or