SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, and now iOS Charting & Android Chart Components

Welcome to the SciChart Community Forums!

Please use the forums below to ask questions about SciChart. Take a moment to read our Question asking guidelines on how to ask a good question and our support policy. We also have a tag=SciChart on Stackoverflow.com where you can earn rep for your questions!

Please note: SciChart team will only answer questions from customers with active support subscriptions. Expired support questions will be ignored. If your support status shows incorrectly, contact us and we will be glad to help.

Answered
1
0

I am trying to color the lines inside the chart of the major grid lines with:

<Setter Property="MajorGridLineStyle">
    <Setter.Value>
    <Style TargetType="Line">
       <Setter Property="Stroke" Value="{Binding XAxis.TickTextBrush}"/>
       <Setter Property="StrokeThickness" Value="1"/>
    </Style>
  </Setter.Value>
</Setter>

But with this code they vanish completely. I have seen the sample for xaml styling. There are 2 other options:
One is StrockDashArray, but I don’t want a dashed line. I just want the line. (But this works).
And there is another sample setting Y2 and X2. But I don’t understand that. It has no effect for me (still no lines). Looking at the documentation it says these are the points where the line ends. But I don’t know where the line should end.

How can I style the major grid lines (or minor grid lines)?

  • You must to post comments
Best Answer
2
0

I coded the following behavior which seems to work for me. Even if the grid is showing data. And without calling InvalidateElement on sciChartSurface.

internal class GridLineColorBehavior
{
    /// <summary>
    /// Defines the gridlinestyle
    /// </summary>
    public static readonly DependencyProperty GridLineStyleProperty = DependencyProperty.RegisterAttached(
        "GridLineColor", typeof(Color), typeof(GridLineColorBehavior), new PropertyMetadata(default(Color), OnGridLineColorChanged));

    private static void OnGridLineColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        NumericAxis numericAxis = d as NumericAxis;
        if (numericAxis == null)
        {
            throw new ArgumentException("unexpected object", "d");
        }
        if (e.NewValue is Color)
        {

            Style newStyle = new Style(typeof(Line));
            newStyle.Setters.Add(new Setter(Line.StrokeProperty, new SolidColorBrush((Color)e.NewValue)));
            numericAxis.MajorGridLineStyle = newStyle;
        }
    }

    public static void SetGridLineColor(DependencyObject element, Color value)
    {
        element.SetValue(GridLineStyleProperty, value);
    }

    public static Color GetGridLineColor(DependencyObject element)
    {
        return (Color)element.GetValue(GridLineStyleProperty);
    }

}

And in XAML:

<Style x:Key="YAxisStyle" TargetType="sciChart:AxisBase">
    <Setter Property="ui:GridLineColorBehavior.GridLineColor" Value="{Binding Color}"/>
</Style>

Everytime I change the color then the color of the gridlines get changed as well. It seems even in the middle while appending data.

  • Andrew
    Great answer, thanks for sharing!
  • You must to post comments
0
0

If you replace this line

<Setter Property="Stroke" Value="{Binding XAxis.TickTextBrush}"/>

with this

<Setter Property="Stroke" Value="Red"/>

What happens? I’m guessing a red gridline (as per our styling axis gridlines KB article).

If so, then its likely the binding is not working. Why? because internally to SciChart we do not use WPF lines, we simply extract the properties from a style to apply to our own bitmap renderer. This is part of the reason why SciChart is so fast – we don’t use WPF for the drawing.

So a binding to XAxis.TickTextBrush won’t work. Instead you’re going to need to set it to a hard-coded color in the style.

Let me know if this helps,

Andrew

  • Uwe Hafner
    I read about the bitmapping but somehow it did not diffuse to my brain that I won't be able to set the stroke color as well. :(. Could I somehow reset "something" "somehow". Setting this color could only be a user setting which can be set in the application settings and does not need to be "on the fly". I could recreate or reset almost everything of scichart on this change. I just would like to use a property of my settings. Its only on my xAxis because I change the color of everything else as well and the lines then would have the same color as the numbers and title.
  • Andrew
    You can try swapping out the style for a new instance. This will force the bitmap engine to pick up the new colour. Or, you can try binding to a static property and after updating it call Scichartsurface.Invalidateelement to force a redraw
  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.