none
How to loop through the MSChart Legend item data? RRS feed

  • Question

  • I want to loop through the data in the legend of mschart, then get and modify the data, but i cannot get the data, the code is below.

      Dim avgColumn As LegendCellColumn = New LegendCellColumn
            avgColumn.Text = "#AVG{N1}"
            avgColumn.HeaderText = "Average"
            avgColumn.Name = "AvgColumn"
            avgColumn.HeaderBackColor = Color.WhiteSmoke
            Chart1.Legends("Default").CellColumns.Add(avgColumn)

            Dim totalColumn As LegendCellColumn = New LegendCellColumn
            totalColumn.Text = "#TOTAL{N0}"
            totalColumn.HeaderText = "Total"
            totalColumn.Name = "TotalColumn"
            totalColumn.HeaderBackColor = Color.WhiteSmoke
            Chart1.Legends("Default").CellColumns.Add(totalColumn)

    Please view the image below, for example how to get the value ("7066000","2124098"...)

    Saturday, February 17, 2018 12:56 PM

All replies

  • Maybe something similar to this although I believe those values are,srings which you would need to split to get the desired numeric data.

    The below code can be altered for just the chart, a specific legend and its custom items you want. 

    For i = 0 to Chart1.Legends.Count - 1
        For j = 0 To Chart1.Legends(i).CustomItems.Count -1
            For k = 0 To Chart1.Legends(i).CustomItems(j).Cells.Count - 1
                MessageBox.Show(Chart1.Legends(i).CustomItems(j).Cells(k).Text)
           Next
        Next
    Next


    La vida loca

    Saturday, February 17, 2018 2:27 PM
  • Sorry, your code did not work, the count is 0 for the Chart1.Legends(0).CustomItems.Count. I seem not add the custom items, thank you.
    Saturday, February 17, 2018 11:30 PM
  • Sorry, your code did not work, the count is 0 for the Chart1.Legends(0).CustomItems.Count. I seem not add the custom items, thank you.
    Yuor image displays them added. How do you know it is in legends 0? Maybe lgends "default" is not legends 0. Try the code as I wrote it to see if the msg box displays anything.

    La vida loca

    Sunday, February 18, 2018 2:55 AM
  • Yes, i only added ONE legend, please view the code below.

     Chart1.ChartAreas(0).BorderDashStyle = ChartDashStyle.Solid   
            Chart1.Titles.Add(adapterName)
            Chart1.Titles.Add("Band Width: " & Convert.ToString(currentBandWidth) & " bps")
            Chart1.Titles(0).Font = New Font("Segoe UI", 12, FontStyle.Bold, Drawing.GraphicsUnit.Point)
            Chart1.Titles(1).Font = New Font("Segoe UI", 9, FontStyle.Regular, Drawing.GraphicsUnit.Point)
            Chart1.Titles(1).ForeColor = Color.DarkGray
            Chart1.Titles(1).Alignment = ContentAlignment.TopRight

         
            Dim legend As Legend = New Legend("Default")
            legend.Alignment = StringAlignment.Center
            legend.Docking = Docking.Bottom
            legend.LegendStyle = LegendStyle.Column
            Chart1.Legends.Add(legend)
            legend.HeaderSeparator = LegendSeparatorStyle.Line
            legend.HeaderSeparatorColor = Color.DarkRed

            Dim firstColumn As LegendCellColumn = New LegendCellColumn
            firstColumn.ColumnType = LegendCellColumnType.SeriesSymbol
            firstColumn.HeaderText = "Color"
            firstColumn.HeaderBackColor = Color.WhiteSmoke
            Chart1.Legends("Default").CellColumns.Add(firstColumn)

            Dim secondColumn As LegendCellColumn = New LegendCellColumn
            secondColumn.ColumnType = LegendCellColumnType.Text
            secondColumn.HeaderText = "Name"
            secondColumn.Text = "#LEGENDTEXT"
            secondColumn.HeaderBackColor = Color.WhiteSmoke
            Chart1.Legends("Default").CellColumns.Add(secondColumn)

            Dim lastColumn As LegendCellColumn = New LegendCellColumn
            lastColumn.Text = "#LAST"
            lastColumn.HeaderText = "Current"
            lastColumn.Name = "LastColumn"
            lastColumn.HeaderBackColor = Color.WhiteSmoke
            Chart1.Legends("Default").CellColumns.Add(lastColumn)

            Dim maxColumn As LegendCellColumn = New LegendCellColumn
            maxColumn.Text = "#MAX{N0}"
            maxColumn.HeaderText = "Maximum"
            maxColumn.Name = "MaxColumn"
            maxColumn.HeaderBackColor = Color.WhiteSmoke
            Chart1.Legends("Default").CellColumns.Add(maxColumn)

            Dim avgColumn As LegendCellColumn = New LegendCellColumn
            avgColumn.Text = "#AVG{N0}"
            avgColumn.HeaderText = "Average"
            avgColumn.Name = "AvgColumn"
            avgColumn.HeaderBackColor = Color.WhiteSmoke
            Chart1.Legends("Default").CellColumns.Add(avgColumn)

            Dim totalColumn As LegendCellColumn = New LegendCellColumn
            totalColumn.Text = "#TOTAL{N0}"
            totalColumn.HeaderText = "Total"
            totalColumn.Name = "TotalColumn"
            totalColumn.HeaderBackColor = Color.WhiteSmoke
            Chart1.Legends("Default").CellColumns.Add(totalColumn)

    Sunday, February 18, 2018 10:07 AM
  • If you want help for this not every day problems than try to get an answers. 

    With dropping a piece of your own code which nobody beside you understand you seldom get help. 

    Create a small but expressive running example which shows the problem. 

    You will see, you have your answer in some hours. 


    Success
    Cor

    Sunday, February 18, 2018 10:21 AM
  • Yes, the source code is below, thank you.

    https://1drv.ms/u/s!AliCKla0zQRNk-gLkKKWpYEBXB2BKQ

    Sunday, February 18, 2018 11:25 AM
  • Yes, the source code is below, thank you.

    https://1drv.ms/u/s!AliCKla0zQRNk-gLkKKWpYEBXB2BKQ

    That is not an example you made to solve the problem. 

    That is your whole program. 


    Success
    Cor

    Sunday, February 18, 2018 1:01 PM
  • Yes, the source code is below, thank you.

    https://1drv.ms/u/s!AliCKla0zQRNk-gLkKKWpYEBXB2BKQ

    That is not an example you made to solve the problem. 

    That is your whole program. 


    Success
    Cor


    Yes.

    And, please describe the problem too. ie the overall goal.

    For example it makes no sense to me why you fill a chart legend with data and then want to read the data in the legend? You already have the data why read it from the legend? Or maybe I missed that part?

    Plus maybe others will but I wont download and run an unknown project from the internet. No offense. :)

    Sunday, February 18, 2018 1:15 PM
  • I want to convert the legend data (bps) to KB/S. MB/S.... so i need to read and get all the data from legend real time, thank you.
    Sunday, February 18, 2018 11:19 PM
  • Well then the code I provided will work if you change customitems to cellcolumns like the below code. However you will have to parse the returned string to get appropriate value and convert it to a numeric variable for performing math operations on.

    For i = 0 To Chart.Legends.Count - 1
       For j = 0 To Chart1.Legends(i).CellColumns.Count - 1
       
        MessageBox.Show(Chart1.Legends(i).CellColumns(j).Text)
       Next
    Next


    La vida loca

    Monday, February 19, 2018 6:22 AM
  • Sorry, your code only show the text "#LEGENDTEXT","#LAST","#MAX{N0}"..., but not the real vaules.
    Monday, February 19, 2018 7:34 AM
  • Sorry, your code only show the text "#LEGENDTEXT","#LAST","#MAX{N0}"..., but not the real vaules.
    Then the values are being provided by some other method which in this instance is the use of the keyword "Max{No}". The keyword Max" in CellColumns text field returns for display the maximum of Y values as can be seen in the properties for a cell columns text of a Legend. If you look in Legends, CellColumns and temporarily add a CellColumn then select its text a button will appeaer and if you select it a window will display for adding text or keywords. I saw the keyword "Max" appear in the text property when I selected maximum of y values and closed the window. I don't know what "Max{No}" returns as a keyword though. But if you do you should be able to iterae through the appropriate points of the series it is used for to get whatever value you need. It's your code so you must know where the value for "Max{No}" is coming from. Unless you took over the project and have no idea how keywords work. I never new about them before today.

    La vida loca

    Monday, February 19, 2018 2:38 PM
  • Thank you, i will research it.
    Monday, February 19, 2018 10:28 PM
  • Thank you, i will research it.
    You're welcome. Sorry I could'nt be more helpful.

    La vida loca

    Monday, February 19, 2018 11:11 PM
  • This problem linked with

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/2e7334f0-ed95-413b-8b9b-ff89e9fa4d7f/how-to-loop-through-the-mschart-legend-item-data?forum=vbgeneral

    I used the code below to add the value in MSChart legend, but how to get the real cell value?

     Dim lastColumn As New LegendCellColumn
            lastColumn.Text = "#LAST"
            lastColumn.HeaderText = str6
            lastColumn.Name = "LastColumn"
            lastColumn.HeaderBackColor = Color.WhiteSmoke
            legend.CellColumns.Add(lastColumn)

            Dim maxColumn As New LegendCellColumn
            maxColumn.Text = "#MAX{N0}"
            maxColumn.HeaderText = str7
            maxColumn.Name = "MaxColumn"
            maxColumn.HeaderBackColor = Color.WhiteSmoke
            legend.CellColumns.Add(maxColumn)

    Used the code to get the legend cell values, but the return values are #LAST, #MAX not the real values.

    For i = 0 To Chart.Legends.Count - 1
      
    For j = 0 To Chart1.Legends(i).CellColumns.Count - 1  
        MessageBox
    .Show(Chart1.Legends(i).CellColumns(j).Text)
      
    Next
    Next

    • Merged by Hart Wang Thursday, February 22, 2018 7:45 AM duplicate
    Wednesday, February 21, 2018 12:27 PM
  • Hi,

    I think you posted on wrong forum.  you can re-post the issue on VB forum. I will merge the two case.

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 22, 2018 2:46 AM