无法在实时图表中设置Y轴上的对数刻度

时间:2019-07-17 04:46:17

标签: wpf livecharts

我的要求是绘制实时图表的Y轴以显示对数刻度,LiveCharts刻度中给出的样本 在X轴上绘制它的效果非常好,但是当我将其更改为Y轴时,它没有提供所需的输出。

<lvc:CartesianChart Series="{Binding SeriesCollection}">
        <lvc:CartesianChart.Resources>
            <Style TargetType="lvc:Separator">
                <Setter Property="Stroke" Value="LightGray"></Setter>
            </Style>
        </lvc:CartesianChart.Resources>

        <lvc:CartesianChart.AxisY>
            <lvc:LogarithmicAxis LabelFormatter="{Binding Formatter}" 
                                 Base="{Binding Base}" >
                <lvc:LogarithmicAxis.Separator>
                    <lvc:Separator StrokeThickness="1" IsEnabled="True"></lvc:Separator>
                </lvc:LogarithmicAxis.Separator>
            </lvc:LogarithmicAxis>
        </lvc:CartesianChart.AxisY>
    </lvc:CartesianChart>

文件后面的代码类似于下面的

public SeriesCollection SeriesCollection { get; set; }
    public Func<double, string> Formatter { get; set; }
    public double Base { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        Base = 10;

        //var mapper = Mappers.Xy<ObservablePoint>()
        //    .X(point => Math.Log(point.X, Base)) //a 10 base log scale in the X axis
        //    .Y(point => point.Y);

        var mapper = Mappers.Xy<ObservablePoint>()
           .Y(point => Math.Log(point.Y, Base)) //a 10 base log scale in the X axis
           .X(point => point.X);

        SeriesCollection = new SeriesCollection(mapper)
        {
            //new LineSeries
            //{
            //    Values = new ChartValues<ObservablePoint>
            //    {
            //        new ObservablePoint(1, 5),
            //        new ObservablePoint(10, 6),
            //        new ObservablePoint(100, 4),
            //        new ObservablePoint(1000, 2),
            //        new ObservablePoint(10000, 8),
            //        new ObservablePoint(100000, 2),
            //        new ObservablePoint(1000000, 9),
            //        new ObservablePoint(10000000, 8)
            //    }
            //}

            new LineSeries
            {
                Values = new ChartValues<ObservablePoint>
                {
                    new ObservablePoint(5, 1),
                    new ObservablePoint(6, 10),
                    new ObservablePoint(4, 100),
                    new ObservablePoint(2, 1000),
                    new ObservablePoint(8, 10000)
                }
            }
        };

        Formatter = value => Math.Pow(Base, value).ToString("N");

        DataContext = this;
    }

当我在X轴上应用对数刻度时,我得到的输出类似于下图 enter image description here 当我在Y轴上应用相同的内容时,输出与预期的不一样 enter image description here

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法,我做过的问题之一是“轴格式化”。请查看下面的更新代码。

<lvc:CartesianChart Series="{Binding SeriesCollection}">
        <lvc:CartesianChart.Resources>
            <Style TargetType="lvc:Separator">
                <Setter Property="Stroke" Value="LightGray"></Setter>
            </Style>
        </lvc:CartesianChart.Resources>

        <lvc:CartesianChart.AxisY>
            <lvc:LogarithmicAxis LabelFormatter="{Binding FormatterY}" 
                                 Base="{Binding Base}" >
                <lvc:LogarithmicAxis.Separator>
                    <lvc:Separator Step="1" StrokeThickness="1" IsEnabled="False"></lvc:Separator>
                </lvc:LogarithmicAxis.Separator>
            </lvc:LogarithmicAxis>
        </lvc:CartesianChart.AxisY>
        <lvc:CartesianChart.AxisX>
            <lvc:Axis LabelFormatter="{Binding FormatterX}" >
                <lvc:Axis.Separator>
                    <lvc:Separator StrokeThickness="1" Step="1"  IsEnabled="False"></lvc:Separator>
                </lvc:Axis.Separator>
            </lvc:Axis>
        </lvc:CartesianChart.AxisX>
    </lvc:CartesianChart>

和文件后面的代码将如下所示。

    public partial class MainWindow : Window
{
    public SeriesCollection SeriesCollection { get; set; }
    public Func<double, string> FormatterX { get; set; }
    public Func<double, string> FormatterY { get; set; }

    public double Base { get; set; }

    public MainWindow()
    {
        InitializeComponent();

        var mapper = Mappers.Xy<ObservablePoint>()
           .X(point => point.X)
           .Y(point => Math.Log(point.Y, Base)); //a 10 base log scale in the Y axis

        Base = 10;

        SeriesCollection = new SeriesCollection(mapper)
        {
            new LineSeries
            {
                Values = new ChartValues<ObservablePoint>
                {
                    new ObservablePoint(1, 66),
                    new ObservablePoint(2, 45),
                    new ObservablePoint(7, 7),
                    new ObservablePoint(8, 250),
                    new ObservablePoint(10, 13334),
                    new ObservablePoint(11, 80),
                    new ObservablePoint(12, 97),
                    new ObservablePoint(13, 587)
                }
            }

        };

        FormatterY = value => Math.Pow(Base, value).ToString("N0");
        FormatterX = value => value.ToString("N0");
        DataContext = this;
    }

}

,输出将采用以下格式 enter image description here