Assigned independent axis cannot be used. This may be due to an unset Orientation property for the axis.in wpf chart c#

StackOverflow https://stackoverflow.com/questions/20631049

  •  18-09-2022
  •  | 
  •  

Question

I want implement rotation and interval of axis label on x axis with LinearAxis in code behind.

lineSeria = new LineSeries();
linAxis = new LinearAxis();
linAxis.Orientation = AxisOrientation.X;
linAxis.Location = AxisLocation.Bottom;
linAxis.Interval = 10;    

var xLabel = new Style(typeof(AxisLabel));
var rotation = new Setter(AxisLabel.RenderTransformProperty, 
                          new RotateTransform() 
                              { 
                                  Angle = -90, 
                                  CenterX = 50, 
                                  CenterY = 1 
                              }
                          );

xLabel.Setters.Add(rotation);
linAxis.AxisLabelStyle = xLabel;

lineSeria.ItemsSource = drowMap[zoomedPointElem.Key];
lineSeria.DependentValuePath = "Value";
lineSeria.IndependentValuePath = "Key";
lineSeria.IndependentAxis = linAxis;
chart[coefficient].Series.Add(lineSeria);

I did this way but something i missed, got this problem "Assigned independent axis cannot be used. This may be due to an unset Orientation property for the axis." How can i fix it, need code behind please. Thank you

Was it helpful?

Solution

You had this error because your keys were not numbers, and in order to use LinearAxis you should convert your strings to numbers.

At first create a new class for chart items:

public class ChartItemModel 
{
    public double Key { get; set; }

    public double Value { get; set; }
}

Then add a method for converting your original collection to the collection of ChartItemModel:

private List<ChartItemModel> MapChartItemsList(Dictionary<string, double> drawMap) 
{
    return drawMap.Select(kv => MapChartItem(kv)).ToList();
}

private ChartItemModel MapChartItem(KeyValuePair<string, double> kv) 
{
    var model = new ChartItemModel();
    model.Key = double.Parse(kv.Key);
    model.Value = kv.Value;

    return model;
} 

Then change your code where you set lineSeria.ItemsSource = drowMap[zoomedPointElem.Key];, use the next code instead:

lineSeria.ItemsSource = MapChartItemsList(drowMap[zoomedPointElem.Key]);

My code above is just an example and it may not work in your application. But the concept is the same, all that you should do is to rewrite the MapChartItemsList method according to your data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top