不能使用指定的独立轴。这可能是由于轴的未设置Orientation属性。在wpf图表中c#

时间:2013-12-17 09:59:02

标签: c# wpf charts wpftoolkit

我希望在x轴上实现轴标签的旋转和间隔,并在后面的代码中使用LinearAxis。

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);

我这样做了,但我错过了一些,得到了这个问题“无法使用分配的独立轴。这可能是由于轴的未设置的Orientation属性。”我该如何解决它,需要背后的代码。谢谢

1 个答案:

答案 0 :(得分:0)

您遇到此错误,因为您的密钥不是数字,为了使用LinearAxis,您应该将字符串转换为数字。

首先为图表项创建一个新类:

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

    public double Value { get; set; }
}

然后添加一个方法,将原始集合转换为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;
} 

然后更改您设置lineSeria.ItemsSource = drowMap[zoomedPointElem.Key];的代码,改为使用下一个代码:

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

上面的代码只是一个示例,它可能无法在您的应用程序中运行。但是这个概念是一样的,你应该做的就是根据你的数据重写MapChartItemsList方法。