Change grid Vertical/Horizontal color of chart

8 posts, 0 answers
  1. Valentin
    Valentin avatar
    73 posts
    Member since:
    Mar 2016

    Posted 13 May Link to this post

    Hello,

     

    I want to change the color of vertical and horizontal gridLines of my chart (target on my screen '12'), but I don't find which property can do this changement..

    The MajorXLine and MajorYLine don't have a semblable property.

     

    Can you help me ?

     

     

    Thank you very much.

     

    Valentin.

  2. Dinko
    Admin
    Dinko avatar
    206 posts

    Posted 14 May Link to this post

    Hi Valentin,

    In order to change the color of the vertical and horizontal grid lines of the chart, you can specify a custom Style to the MajorXLineStyle / MajorYLineStyle.  Keep in mind that the TargetType property of the custom style should be set to "Line". 
    <telerik:RadCartesianChart.Grid>
        <telerik:CartesianChartGrid MajorLinesVisibility="XY">
            <telerik:CartesianChartGrid.MajorXLineStyle>
                <Style TargetType="Line">
                    <Setter Property="Stroke" Value="Red"/>
                </Style>
            </telerik:CartesianChartGrid.MajorXLineStyle>
            <telerik:CartesianChartGrid.MajorYLineStyle>
                <Style TargetType="Line">
                    <Setter Property="Stroke" Value="Green"/>
                </Style>
            </telerik:CartesianChartGrid.MajorYLineStyle>
        </telerik:CartesianChartGrid>               
    </telerik:RadCartesianChart.Grid>
    For your convenience, we have prepared a sample project demonstrating this approach. In addition, you can take a look at the CartesianChartGrid and PolarChartGrid help article in our documentation. 

    Hope this information is helpful.

    Regards,
    Dinko
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Valentin
    Valentin avatar
    73 posts
    Member since:
    Mar 2016

    Posted 17 May Link to this post

    Hello,

     

    Yes, is that I searched.

    So, I want to change it in code, and I can't give "Line" to TargetType :

    Style st = new Style();
    st.TargetType = "?" ; //Here
    this.chartVariables.Grid.MajorXLineStyle = st;

     

    Few month, I had tried this :

    couleurLignesHorizontales = new SolidColorBrush(this.cpCouleurLigne.SelectedColor);
     
    ((SolidColorBrush)((Setter)this.chartVariables.Grid.MajorXLineStyle.Setters[0]).Value).Color = couleurLignesHorizontales.Color;
     
    // OR
     
    ((Setter)this.chartVariables.Grid.MajorXLineStyle.Setters[0]).Value = couleurLignesHorizontales;

    But the compiler said me that the value is sealed and i can't modify it.

     

    Thank you

  5. Petar Marchev
    Admin
    Petar Marchev avatar
    968 posts

    Posted 20 May Link to this post

    Hi,

    The TargetType property of the Style is of type Type and this is why you need to set it to a typeof(Line):
    st.TargetType = typeof(Line);

    You got the "sealed" exception because the Style was already in use and you cannot modify it.

    Regards,
    Petar Marchev
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  6. Valentin
    Valentin avatar
    73 posts
    Member since:
    Mar 2016

    Posted 20 May in reply to Petar Marchev Link to this post

    Hello Petar,

    I've done this :

    Style st = new Style();
    st.TargetType = this.chartVariables.Grid.MajorXLineStyle.TargetType;//typeof(/*Not found*/);
     
    Setter newSetter = new Setter();
    newSetter.Value = couleurLignesHorizontales;
     
    st.Setters.Add(newSetter);
     
    this.chartVariables.Grid.MajorXLineStyle = st;

    (I did : st.TargetType = this.chartVariables.Grid.MajorXLineStyle.TargetType; because I cannot access to "Line" for the 'TargetType').


    But the changement is never effective.

    => How I can apply the changements ?
  7. Valentin
    Valentin avatar
    73 posts
    Member since:
    Mar 2016

    Posted 23 May in reply to Valentin Link to this post

    Hello,

     

    To try to apply the changement, I tried this :

    this.chartVariables.Grid.MajorLinesVisibility = GridLineVisibility.None;
    this.chartVariables.Grid.MajorLinesVisibility = GridLineVisibility.X;

    [..] to "reload" the grid. But there is an error :

    "Not equal value to null compulsory for ' Setter. Property"

     

    Thanks

  8. Petar Marchev
    Admin
    Petar Marchev avatar
    968 posts

    Posted 25 May Link to this post

    Hi Valentin,

    I do not know why you say that the type is Not Found. Perhaps you have not included the correct namespace. Line is a simple class and you should be able to declare a reference to a Line and if you can do this, you should be able to get the Line's type.

    I have created a simple project to demonstrate how to get it done. As I mentioned before, you are not allowed to modify a Style once it is in use, so make sure to fully set it up prior to passing it to the grid. Make sure you never modify this style afterwards.

    As you have discovered on your own, there seems to be a bug in the grid where dynamic change of line styles is not anticipated and when you change the style, the change is not applied. In order to work around the bug you can use the code you already have that toggles the visibility of the lines.

    Regards,
    Petar Marchev
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  9. Valentin
    Valentin avatar
    73 posts
    Member since:
    Mar 2016

    Posted 25 May Link to this post

    Hello Petar,

     

    Effectively, do not need to touch at the new Setter. 

    Now, it's working very nice, thank you !

     

    Valentin.

Back to Top
UI for WPF is Visual Studio 2017 Ready