Page 1 of 1

Bug in ColorGrid Legend using Palette

Posted: Mon Aug 30, 2010 6:52 am
by 16055146
The legend shows wrong colors when using own palette

The example below runs ok with version 3.2 showing in legend colors red, yellow, white, turquoise and blue.
With the latest 4.1 version the colors shown are wrong (some colors shown twice)

To use the example just create a form test.vb in windows.forms vb project referencing the appropriate teechart version
and replace the contents of created test.vb with code below.

Code: Select all

Imports Steema.TeeChart
Public Class test

    Private zValues(23) As Double
    Private ScaleColors() As Color
    Private ScalePoints() As Double

    Private iRangeCount As Integer = 5
    Private dRMin As Double
    Private dRMax As Double
    Private dTarget As Double
    Private dColorStep As Double

    Private TChart1 As New Steema.TeeChart.TChart
    Private ColorGrid1 As New Steema.TeeChart.Styles.ColorGrid

    Private Sub test_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        TChart1.Aspect.View3D = False
        ColorGrid1.IrregularGrid = False
        ColorGrid1.ShowInLegend = True
        ColorGrid1.UsePalette = True
        ColorGrid1.UseColorRange = False

        TChart1.Legend.Series = ColorGrid1
        TChart1.Legend.MaxNumRows = 5
        TChart1.Legend.LegendStyle = LegendStyles.Palette
        TChart1.Series.Add(ColorGrid1)

        Me.Controls.Add(TChart1)
        SetData()

    End Sub

    Private Sub SetData()

        Dim iC As Integer
        Dim iAdd As Integer = 0
        Dim rG As New System.Random

        ColorGrid1.ClearPalette()
        ColorGrid1.Clear()

        ' create random data
        iC = 0
        dRMin = Double.MaxValue
        dRMax = Double.MinValue
        For x As Integer = 0 To 3
            For y As Integer = 0 To 5
                zValues(iC) = rG.NextDouble
                If zValues(iC) < dRMin Then dRMin = zValues(iC)
                If zValues(iC) > dRMax Then dRMax = zValues(iC)
                iC += 1
            Next
        Next

        SetScalePointsAndScaleColors()

        ' Add palettes
        For iC = 0 To iRangeCount - 2
            ColorGrid1.AddPalette(ScalePoints(iC), ScaleColors(iC + iAdd))
            If iAdd = 0 AndAlso iC < iRangeCount - 2 Then
                If ScalePoints(iC + 1) > dTarget Then
                    iAdd = 1
                    ColorGrid1.AddPalette(dTarget, ScaleColors(iC + iAdd))
                End If
            End If
        Next

        ' Add data using palette colors
        iC = 0
        For x As Integer = 0 To 3
            For y As Integer = 0 To 5
                ColorGrid1.Add(y + 1, zValues(iC), x + 1, GetPointColor(zValues(iC)))
                iC += 1
            Next
        Next

    End Sub
    Private Sub SetScalePointsAndScaleColors()

        ' divide value range into desired number of subranges

        dColorStep = (dRMax - dRMin) / CDbl(iRangeCount)

        ReDim ScalePoints(iRangeCount - 2)
        SetStandardScalePoints()
        dTarget = (dRMax + dRMin) / 2.0

        ReDim ScaleColors(iRangeCount - 1)
        SetStandardScaleColors()

    End Sub
    Private Sub SetStandardScalePoints()

        For i As Integer = 0 To iRangeCount - 2
            ScalePoints(i) = dRMin + dColorStep * CDbl(i + 1)
        Next

    End Sub

    Private Sub SetStandardScaleColors()

        Dim iAlpha As Integer = 255

        ScaleColors(0) = Color.FromArgb(iAlpha, 0, 0, 240)
        ScaleColors(1) = Color.FromArgb(iAlpha, 0, 240, 240)
        ScaleColors(2) = Color.FromArgb(iAlpha, 240, 240, 240)
        ScaleColors(3) = Color.FromArgb(iAlpha, 240, 240, 0)
        ScaleColors(4) = Color.FromArgb(iAlpha, 240, 0, 0)

    End Sub
    Private Function GetPointColor(ByVal dVal As Double) As Color

        Dim retCol As Color = Color.Black
        Dim iIndex As Integer
        Try
            iIndex = CInt(Math.Floor(Math.Abs(dVal - dRMin) / dColorStep))
            If iIndex > iRangeCount - 1 Then
                iIndex = iRangeCount - 1
            End If
            If dVal > dTarget Then
                If dVal > ScalePoints(iIndex - 1) Then
                    retCol = ScaleColors(iIndex)
                Else
                    retCol = ScaleColors(iIndex - 1)
                End If
            Else
                If dVal < ScalePoints(iIndex) Then
                    retCol = ScaleColors(iIndex)
                Else
                    retCol = ScaleColors(iIndex + 1)
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

        Debug.WriteLine(dVal.ToString & " -> " & iIndex.ToString)

        Return retCol

    End Function

End Class

Re: Bug in ColorGrid Legend using Palette

Posted: Tue Aug 31, 2010 3:55 pm
by yeray
Hi TEh,

Could you please tell us in what exact TeeChart v3.2 build can you see a legend without repeated colors in the legend with that code?
Thanks in advance.

Re: Bug in ColorGrid Legend using Palette

Posted: Mon Sep 06, 2010 12:23 pm
by 16055146
Hello Yeray,

version 3.2.2763.26082

Re: Bug in ColorGrid Legend using Palette

Posted: Wed Sep 08, 2010 1:45 pm
by narcis
Hi TEh,

Thanks for the information. I have been able to reproduce the issue here and added it (TF02015141) to the defect list to be fixed.

Re: Bug in ColorGrid Legend using Palette

Posted: Mon Nov 19, 2012 11:32 am
by 16055988
Hello

Apparently this bug has never been fixed or it has reappeared.

Re: Bug in ColorGrid Legend using Palette

Posted: Mon Nov 19, 2012 11:41 am
by 16055988
By the way - If I add a new value to the series after the actual values with color.empty as color, the legend shows correct colors

ColorGrid1.Add(0, 0, 0, Color.Empty)

Re: Bug in ColorGrid Legend using Palette

Posted: Mon Nov 19, 2012 3:56 pm
by 10050769
Hello Teh,

I inform you that this bug is already fixed in build 4.1.2010.02809 (TeeChart for .NET). I have debugged your code and I find that you have made an error in next line of code:

Code: Select all

	ColorGrid1.Add(y + 1, zValues(iC), x + 1, GetPointColor(zValues(iC)))
Basically, you assign, the palette to the Grid, previously add values and at the moment to add values, it is contradictory and it causes the problem in last version of TeeChartFor.Net. If you modify the previous line of code for the next:

Code: Select all

ColorGrid1.Add(y + 1, zValues(iC), x + 1)
The code works fine.

I hope will helps.

Thanks,