Search code examples
c#wpfdynamic-data-display

Add vertical scroll in legend box (ChartPlotter) WPF


I am new for WPF application.I am working on a Sensor reading WPF application.In this application i have to select sensors out of 170 Sensors.After selecting sensors when i click on view report then chart plotter draw the Graph of selected sensors.It Works fine for me.

Problem :

Here due to page size limitation i have fixed height of graph.But when count of sensors is more than the height of graph then legends of selected sensors hide those who are not adjust in the height of graph.How i can add scroll to legend box so that user can scroll and check all sensor legends.

Please give some idea.

Thanks in advance.


Solution

  • I searched a lot on web for this problem but i got no solution for this.So to solve this problem i follow these steps.

    1.)first i make plotter legend visibility false by plotter.LegendVisible = false;

    2.)Second i add a listview control exactly on the graph where plotter Legend was appear.

      <ListView Height="Auto" HorizontalAlignment="Center" Margin="1100,139,0,0" Name="listview" ItemsSource="{Binding Items}" HorizontalContentAlignment="Stretch" VerticalAlignment="Top" Width="75" Grid.RowSpan="2" MaxHeight="260">
                                <ListView.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Text}" Foreground="{Binding BackgroundColor}">
                                        </TextBlock>
                                    </DataTemplate>
                                </ListView.ItemTemplate>
                                <ListView.BorderBrush>
                                    <SolidColorBrush />
                                </ListView.BorderBrush>
                            </ListView>
    

    3.)Then i do some work on back end as -

    -Add ItemVM.cs :

     class ItemVM : INotifyPropertyChanged
    {
        private string TextValue = String.Empty;
        private Brush BackgroundColorValue = null;
    
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    
        public ItemVM(Brush color, string objectData)
        {
            BackgroundColor = color;
            Text = objectData;
        }
        public string Text
        {
            get
            {
                return this.TextValue;
            }
    
            set
            {
                if (value != this.TextValue)
                {
                    this.TextValue = value;
                    NotifyPropertyChanged("Text");
                }
            }
        }
        public Brush BackgroundColor
        {
            get
            {
                return this.BackgroundColorValue;
            }
    
            set
            {
                if (value != this.BackgroundColorValue)
                {
                    this.BackgroundColorValue = value;
                    NotifyPropertyChanged("BackgroundColor");
                }
            }
        }
    
    }
    

    -In mainform.xaml.cs :

     List<ItemVM> Items;
                   List<string> lst = new List<string> {"item1","item2","item3" };
                   var converter = new System.Windows.Media.BrushConverter();
      Color[] colors = ColorHelper.CreateRandomColors(3);
      Items = new List<ItemVM>();
     for(int i=0;i<lst.count;i++)
     {
     Items.Add(new ItemVM((Brush)converter.ConvertFromString(colors[i].ToString()), SelectedItems[i]));
    }
     plotter.LegendVisible = false;
     listview.ItemsSource = Items;
    

    Now i got legend box on chart plotter with scroll and regend forecolor also reflect chart line color.