Results 1 to 6 of 6

Thread: [RESOLVED] Adding a Chart dynamically fails to show legend

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2019
    Posts
    108

    Resolved [RESOLVED] Adding a Chart dynamically fails to show legend

    I have a chart on a form that has all its default properties unchanged from when I added it from the toolbox.

    Call a routine named "AddInletCurve" during runtime and it behaves as desired. In particular all legends are visible.

    In a separate reporting routine I add several chart objects dynamically inside an loop of index iFlow

    Code:
    		FlowChartLine(iFlow) = New System.Windows.Forms.DataVisualization.Charting.Chart
    
    		With FlowChartLine(iFlow)
    			.Top = NewTop
    			.Left = NewLeft
    			Me.Controls.Add(FlowChartLine(iFlow))
    			.Name = "X_" & Me.Controls.Count
    			.Height = 605
    			.Width = 1520
    			.Refresh()
    			NewTop = NewTop + .Height + 20
    		End With

    for each of those charts I apply the same "AddInletCurve" routine.

    The chart is displayed correctly with all the correct values, EXCEPT the legend is not visible.

    I have tried resizing the chart various ways, but in all cases the legend which should appear at the top right is not displayed.

    Nothing beyond the extent of the horizontal axis is displayed.

    Any suggestions

  2. #2
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Adding a Chart dynamically fails to show legend

    do you populate the legend with :

    Code:
    name_graph.Series(serie).LegendText = text
    you can also set

    Code:
     name_graph.Series(serie).IsVisibleInLegend = true
    but it should be true by default.

    the legend is applied to the serie not to the graph

    what is inside the "AddInletCurve" routine ?
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2019
    Posts
    108

    Re: Adding a Chart dynamically fails to show legend

    showing the whole "AddInletCurve" routine is not practical here, but here is some of the code. I have verified that these lines are executed for both the instance where the legend appears and the instance where it does not.



    Code:
    							.Series.Add("Solid" & gsOPERATION_POINT(iOpPt))
    							iSolidOil = .Series.Count - 1
    							SolidIndex(iOpPt) = iSolidOil
    							.Series(iSolidOil).ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line
    							.Series(iSolidOil).IsVisibleInLegend = True
    							.Series(iSolidOil).LegendText = .Series(iSolidOil).Name
    
    							.Series.Add("Spec" & gsOPERATION_POINT(iOpPt))
    							iSpecPoint = .Series.Count - 1
    							SpecPointIndex(iOpPt) = iSpecPoint
    							.Series(iSpecPoint).ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point
    							.Series(iSpecPoint).IsVisibleInLegend = True
    							.Series(iSpecPoint).LegendText = .Series(iSpecPoint).Name
    
    							If bPlotData And iOpPtFilter <> -1 Then
    								.Series.Add("PlotData" & gsOPERATION_POINT(iOpPt))
    								iPlotData = .Series.Count - 1
    								PlotDataIndex(iOpPt) = iPlotData
    								.Series(iPlotData).ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line
    								.Series(iPlotData).IsVisibleInLegend = True
    								.Series(iPlotData).LegendText = .Series(iPlotData).Name
    							End If

    Further down in the code these values are populated

    Here is the opening statement in the routine

    Code:
    Public Sub AddInletCurve(ByRef ThisChart As Chart, ByVal gsPlotFlow As String, ByVal bPlotData As Boolean, ByVal fPlotData(,) As Single, ByVal mbairflow As Boolean)
    You will note that the chart object is passed as an argument.

    In the instance where it works that chart argument represents a chart control that already exists on a form.

    In the instance where it does not work the chart argument is the control that is created dynamically (FlowChartLine(iFlow)) as shown in my original posting.

  4. #4
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Adding a Chart dynamically fails to show legend

    I will check that this weekend when I will have some time.

    in you with block try to put that :

    Code:
    .Legends[0].LegendPosition.auto= true
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  5. #5
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Adding a Chart dynamically fails to show legend

    Ok, I made some tests.

    When you create dynamically a new graph, there is nothing inside by default so you need to create everything (chartarea, series and legend)

    You need at some point to create your legend so when you create the new graph you need to include the following line in the with block :

    Code:
    FlowChartLine(iFlow).Legends.Add(New Legend)
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Nov 2019
    Posts
    108

    Re: Adding a Chart dynamically fails to show legend

    That is an excellent solution.
    Thank you very much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width