How to set/change the color for a new added line on XYCharts on runtime?

Charts for WPF Forum

Posted 2 years ago by Nerd22 - Germany
Version: 22.1.4
Avatar

Hello there,

I'm on my way to find out a way, how to set or change the color for a new added line on a XYChart. When I'm adding a second, third, fourth, ... n-th line, it seems the colors aren't fixed?

for instance:

On a XYChart two lines existing, where the base line has the default color (some blue) and the second is orange.

When I'm adding a third line, the base line still has its default color, however, the second line now has a green color or else whereas

the new third line now has the orange color.

This might look really ugly, because it may cause not to see all sub lines. Is there a way to set a given color for each new line?

I would like to set:

base line (default, maybe also in black, if possible)

second line: orange

third line: red

fourth line: green

...

appendix:

with:

demoChart.SeriesStyleSelector = new SeriesPaletteStyleSelector(new Palette(PaletteKind.Melon));
demoChart.Refresh();

I'm able to change the base line color, however, this might not work for a sub line.

A sub line here is defined as:

LineSeries ls = new LineSeries() {
	ItemsSource = /*some list here*/,
        XPath = "X",
        YPath = "Y",
        LineKind = XYSeriesLineKind.Step
};

demoChart.Series.Add(ls);

The LineSeries doesn't come with a series style selector property or similar.

Thanks. :o)

Comments (5)

Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

The SeriesPaletteStyleSelector will use the series.Index to choose which color index from the palette to use.  For instance, the first series will use the color at palette index 0, the second series will use the color at palette index 1, etc.

You can create a custom Palette class instance with colors black, orange, red, green to achieve your desired colors.  There is a constructor overload that takes a set of custom base colors.  Then pass that Palette instance to the SeriesPaletteStyleSelector overload that takes a Palette parameter.  If you use that style selector, it should get things working how you want.


Actipro Software Support

Posted 2 years ago by Nerd22 - Germany
Avatar

Thanks, I'm on my way to test that. (:

Posted 2 years ago by Nerd22 - Germany
Avatar

Hey there, I tried it, however, no graph is displayed on runtime.

Let's say, I'm defining these three colors and put that to the SeriesPaletteStyleSelector, the graph is unable to see:

Color red = new Color() { R = 255, G = 0, B = 0 };
Color orange = new Color() { R = 255, G = 140, B = 0 };
Color green = new Color() { R = 0, G = 100, B = 0 };

SeriesPaletteStyleSelector spss = new SeriesPaletteStyleSelector(new Palette(new Color[] { red, orange, green }));

/*	Graph can't be displayed?	*/
demoChart.SeriesStyleSelector = spss;

However, by using the given PaletteKind, in that case "Melon", the graph can be displayed here.

/*	Graph is able to display...?	*/
demoChart.SeriesStyleSelector = new SeriesPaletteStyleSelector(new Palette(PaletteKind.Melon));

By the way, it doesn't matter, if I define the Color array with one element, the same result appears.

Color red = new Color() { R = 255, G = 0, B = 0 };

SeriesPaletteStyleSelector spss = new SeriesPaletteStyleSelector(new Palette(new Color[] { red }));

/*	Graph is still "invisible"...	*/
demoChart.SeriesStyleSelector = spss;

Could it be, that the extented constructor for Color[] is unable to work?

Do I still miss something or what's wrong here?

Thanks in advise. :o)

Answer - Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

I believe that the problem you saw is due to how you created the Colors.  When you use that Color constructor, A is left as 0 so you ended up with transparent colors.

If you change it to this, it works:

Color red = Color.FromRgb(255, 0, 0);
Color orange = Color.FromRgb(255, 140, 0);
Color green = Color.FromRgb(0, 100, 0);


Actipro Software Support

Posted 2 years ago by Nerd22 - Germany
Avatar

Oh, thats why. °(^.^)

The latest build of this product (v24.1.3) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.