Changing line series color in runtime is not working

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
bairog
Advanced
Posts: 128
Joined: Fri Dec 07, 2018 12:00 am

Changing line series color in runtime is not working

Post by bairog » Wed Aug 21, 2019 8:59 am

I've faced a strange issue while changing line series color in runtime.
I'm using tChart1_ClickLegend event to show ColorPickerDialog where user can select a color for a clicked line and it's points:

Code: Select all

private void tChart1_ClickLegend(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                var seriesInLegend = tChart1.Series[tChart1.Legend.Clicked(e.X, e.Y)] as Line;

                colorDialog1.Color = seriesInLegend.Pointer.Color;
                if (colorDialog1.ShowDialog() == DialogResult.OK)
                {
                    lineEquivalentData.Color = colorDialog1.Color;
                    lineEquivalentData.Pointer.Color = colorDialog1.Color;
                }
            }
        }
And it's working unpredictable - sometimes changing only points color, sometimes changing nothing:
Image
Image
On that screenshots I'm trying to change second (dark green) line color. As you can see - series color in legend is correct in both situations.
I was unable to reproduce issue in supersimple test app, so I've shorten my code where I've faced this problem. I've uploaded it here

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Changing line series color in runtime is not working

Post by Christopher » Wed Aug 21, 2019 3:07 pm

That's because you're adding in point colors to your series:

Code: Select all

          lineActualData.Add(i, Values[i].ActualValue, Values[i].FlightDate.ToString(dateTimeMask) + " " + Values[i].FlightShift + " " + Values[i].FlightNum, lineActualData.Color);
          lineEquivalentData.Add(i, Values[i].EquivalentValue, Values[i].FlightDate.ToString(dateTimeMask) + " " + Values[i].FlightShift + " " + Values[i].FlightNum, lineEquivalentData.Color);
Adding in point colors (the last parameter of the Add function) means that there is no series color. To resolve this, either:
1) remove the last parameter of the Add function
2) change all the point colors, i.e.

Code: Select all

                if (colorDialog1.ShowDialog() == DialogResult.OK)
                {
                    for (int i = 0; i < lineEquivalentData.Count; i++)
                    {
                      lineEquivalentData[i].Color = colorDialog1.Color;
                    }
                    lineEquivalentData.Pointer.Color = colorDialog1.Color;
                }
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

bairog
Advanced
Posts: 128
Joined: Fri Dec 07, 2018 12:00 am

Re: Changing line series color in runtime is not working

Post by bairog » Thu Aug 22, 2019 5:41 am

Now I'm using the following code (points >=6 should be always Gold, points >=8 should be always Tomato):

Code: Select all

using System;
using System.Drawing;
using System.Windows.Forms;

namespace TeeChartColorTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();            
            //line1.Color = line1.Pointer.Color = Color.Blue;
            var rnd = new Random();
            int currentValue;
            for (int i = 0; i < 20; i++)
            {
                currentValue = rnd.Next(11);
                if (currentValue >=8)
                    line1.Add(i, currentValue, i.ToString(), Color.Tomato);
                else
                    if (currentValue >= 6)
                    line1.Add(i, currentValue, i.ToString(), Color.Gold);
                else
                    line1.Add(i, currentValue, i.ToString(), line1.Pointer.Color);
            }
        }

        private void tChart1_ClickLegend(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                colorDialog1.Color = line1.Color;
                if (colorDialog1.ShowDialog() == DialogResult.OK)
                {
                    for (int i = 0; i < line1.Count; i++)
                    //change point colors (except for Tomato and Gold)
                        if (line1[i].Color == line1.Color)
                            line1[i].Color = colorDialog1.Color;

                    line1.Color = line1.Pointer.Color = colorDialog1.Color;
                }
            }
        }
    }
}
With commented line it works as expected:
Image
Changing color to cyan:
Image
But when I uncomment that line (where I manually change line color to Blue) - all point are Blue despite I'm setting every point color explicitly in the Add function:
Image

P.S.
1) As far as I understand Steema.TeeChart.Styles.Line.Color is default color for line, Steema.TeeChart.Styles.Line.Pointer.Color is default color for line points, line1.Color (Steema.TeeChart.Styles.SeriesXYPoint.Color) is color of a given point (in case I explicitly set color in the last parameter of the Add function).
But what Steema.TeeChart.Styles.Line.Brush.Color, Steema.TeeChart.Styles.Line.Brush.ForegroundColor and Steema.TeeChart.Styles.Line.LinePen.Color stands for? Do I need to set them when I change line color via ColorDialog?

2) I still have some chart print requirements that are critical for our software (access to zoom property for printer and dashed grid poor print quality). I've asked about current progress here, but got no answer.

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Changing line series color in runtime is not working

Post by Christopher » Thu Aug 22, 2019 7:34 am

bairog wrote:
Thu Aug 22, 2019 5:41 am
But when I uncomment that line (where I manually change line color to Blue) - all point are Blue despite I'm setting every point color explicitly in the Add function:
In this scenario there is no need to complicate matters with the series color at all, i.e.

Code: Select all

    public partial class Form1 : Form
    {
        private readonly TChart tChart1 = new TChart();
        private readonly Line line1 = new Line();
        private readonly ColorDialog colorDialog1 = new ColorDialog();
        private Color oldColor;

        public Form1()
        {
            InitializeComponent();

            tChart1.Dock = DockStyle.Fill;
            splitContainer1.Panel2.Controls.Add(tChart1);

            InitializeChart();
        }

        private void InitializeChart()
        {
            tChart1.Series.Add(line1);
            line1.Pointer.Visible = true;
            tChart1.Legend.LegendStyle = LegendStyles.Series;
            tChart1.ClickLegend += TChart1_ClickLegend;

            oldColor = line1.Pointer.Color = Color.Blue;


            Random rnd = new Random();
            int currentValue;
            for (int i = 0; i < 20; i++)
            {
                currentValue = rnd.Next(11);
                if (currentValue >= 8)
                {
                    line1.Add(i, currentValue, i.ToString(), Color.Tomato);
                }
                else
                    if (currentValue >= 6)
                {
                    line1.Add(i, currentValue, i.ToString(), Color.Gold);
                }
                else
                {
                    line1.Add(i, currentValue, i.ToString(), oldColor);
                }
            }
        }

        private void TChart1_ClickLegend(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                colorDialog1.Color = line1.Color;
                if (colorDialog1.ShowDialog() == DialogResult.OK)
                {
                    for (int i = 0; i < line1.Count; i++)
                    {
                        //change point colors (except for Tomato and Gold)
                        if (line1[i].Color == oldColor)
                        {
                            line1[i].Color = colorDialog1.Color;
                        }
                    }

                    oldColor = colorDialog1.Color;
                }
            }
        }
    }
bairog wrote:
Thu Aug 22, 2019 5:41 am
2) I still have some chart print requirements that are critical for our software (access to zoom property for printer and dashed grid poor print quality). I've asked about current progress here, but got no answer.
No, there is no progress to report.
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

bairog
Advanced
Posts: 128
Joined: Fri Dec 07, 2018 12:00 am

Re: Changing line series color in runtime is not working

Post by bairog » Thu Aug 22, 2019 8:02 am

Christopher wrote:
Thu Aug 22, 2019 7:34 am
In this scenario there is no need to complicate matters with the series color at all, i.e.
With your code all points are Blue (some of them should be Tomato or Gold as the corresponding lines are):
Image
Christopher wrote:
Thu Aug 22, 2019 7:34 am
No, there is no progress to report.
Sad to hear. Hope something will change because it's critical requirement for our software.

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Changing line series color in runtime is not working

Post by Christopher » Thu Aug 22, 2019 8:11 am

bairog wrote:
Thu Aug 22, 2019 8:02 am
With your code all points are Blue (some of them should be Tomato or Gold as the corresponding lines are):
Ah, so you don't want all the Pointers to be of the same color? In which case, change the following line:

Code: Select all

 oldColor = line1.Pointer.Color = Color.Blue;
to this:

Code: Select all

oldColor = Color.Blue;
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

bairog
Advanced
Posts: 128
Joined: Fri Dec 07, 2018 12:00 am

Re: Changing line series color in runtime is not working

Post by bairog » Thu Aug 22, 2019 9:12 am

Christopher wrote:
Thu Aug 22, 2019 8:11 am
Ah, so you don't want all the Pointers to be of the same color? In which case, change the following line:
I supposed it is possible without saving current line color without separate variable..
Thank you, it's working.

Post Reply