Search code examples
c#wpflivecharts

How do i change x Axis Label in Livecharts wpf c#


I am trying to change the x axis on a combobox event in Livecharts. Im using c# wpf. Everything works fine with the y axis but i havent found a way to swap out the x axis as well.

Ive been searching for quite a bit now, but i just can not find a soloution at all. Perhaps im just blind. Theres tons of info for the y axis but little to none for the x one. I could delete all points of the graph and add every new point to the graph. Unfortunately this soloution is ugly and extremely inefficient I havent written nor spoken a lot of english lately so sorry for reading this. Thanks for your help Best regards en0s


Solution

  • Since you did not provide code, we can't be sure about how your ComboBox event is handled, but here's an example that could work:

    private void YourComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (comboBox.SelectedItem is ComboBoxItem selectedItem)
            {
                string selectedContent = selectedItem.Content.ToString();
    
                switch (selectedContent)
                {
                    case "Example 1":
                        yourChart.AxisX.Clear();
                        yourChart.AxisX.Add(new Axis
                        {
                            Title = "X-Axis for Example 1",
                            Labels = new[] { "A", "B", "C", "D" }
                        });
                        break;
    
                    case "Example 2":
                        yourChart.AxisX.Clear();
                        yourChart.AxisX.Add(new Axis
                        {
                            Title = "X-Axis for Example 2",
                            Labels = new[] { "E", "F", "G", "H" }
                        });
                        break;
    
                    default:
                        break;
                }
            }
        }
    

    If you need something more in-depth, update with code snippets and what you've tried. The more information, the better.