无法在实时图表中设置 Y 轴的对数刻度
Unable to set the Log scale in Y-axis in live charts
我的要求是绘制实时图表的 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 轴上应用对数刻度时,我得到类似于下图的输出
当我在 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 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;
}
}
并且输出将采用以下格式
我的要求是绘制实时图表的 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 轴上应用对数刻度时,我得到类似于下图的输出
我找到了问题的解决方案,我完成的其中一个问题是轴格式化。请参阅下面更新的代码。
<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;
}
}
并且输出将采用以下格式