不能使用分配的独立轴。这可能是由于轴的取消设置Orientation属性wpf图表c# [英] Assigned independent axis cannot be used. This may be due to an unset Orientation property for the axis.in wpf chart c#

查看:380
本文介绍了不能使用分配的独立轴。这可能是由于轴的取消设置Orientation属性wpf图表c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在代码中使用LinearAxis在x轴上实现轴标签的旋转和间隔。

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

推荐答案

您有这个错误,因为您的键不是数字,为了使用 LinearAxis

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

然后添加一个方法将原始集合转换为 ChartItemModel

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

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

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

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

我的代码只是一个例子,它可能无法在你的应用程序中工作。但是概念是一样的,你应该做的是根据你的数据重写 MapChartItemsList 方法。

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.

这篇关于不能使用分配的独立轴。这可能是由于轴的取消设置Orientation属性wpf图表c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆