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

Change grid Vertical/Horizontal color of chart

7 Answers 68 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Valentin
Top achievements
Rank 1
Iron
Valentin asked on 13 May 2016, 09:44 AM

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.

7 Answers, 1 is accepted

Sort by
0
Dinko
Telerik team
answered on 14 May 2016, 11:24 AM
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.
0
Valentin
Top achievements
Rank 1
Iron
answered on 17 May 2016, 08:11 AM

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

0
Petar Marchev
Telerik team
answered on 20 May 2016, 07:07 AM
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.
0
Valentin
Top achievements
Rank 1
Iron
answered on 20 May 2016, 08:25 AM
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 ?
0
Valentin
Top achievements
Rank 1
Iron
answered on 23 May 2016, 02:58 PM

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

0
Petar Marchev
Telerik team
answered on 25 May 2016, 07:59 AM
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.
0
Valentin
Top achievements
Rank 1
Iron
answered on 25 May 2016, 11:58 AM

Hello Petar,

 

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

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

 

Valentin.

Tags
Chart
Asked by
Valentin
Top achievements
Rank 1
Iron
Answers by
Dinko
Telerik team
Valentin
Top achievements
Rank 1
Iron
Petar Marchev
Telerik team
Share this question
or