使用LiveCharts(https://lvcharts.net/)和C#,我想要一个条形图,其中每一列在x轴上都有一个标签。但是,我仅在第二列上得到一个标签。标签以数据方式存在,当我将鼠标悬停在列上时,我会在弹出窗口中看到它们。
我尝试弄乱轴上的各种标签属性以及我的labelformatter,但我每隔两列都只会得到一个标签。
我的图表的XAML实现:
<lvc:CartesianChart Name="CartesianChart" Series="{Binding Series}" MinHeight="400" MinWidth="800" >
<lvc:CartesianChart.AxisX>
<lvc:Axis MinValue="0" Labels="{Binding XAxisLabels}" ShowLabels="True" LabelsRotation="50">
<lvc:Axis.Separator>
<lvc:Separator Stroke="{Binding AxisStrokeColor}">/lvc:Separator>
</lvc:Axis.Separator>
</lvc:Axis>
</lvc:CartesianChart.AxisX>
<lvc:CartesianChart.AxisY>
<lvc:Axis MinValue="0" MaxValue="10" LabelFormatter="{Binding LabelFormatter}">
<lvc:Axis.Separator>
<lvc:Separator Stroke="{Binding AxisStrokeColor}">/lvc:Separator>
</lvc:Axis.Separator>
</lvc:Axis>
</lvc:CartesianChart.AxisY>
</lvc:CartesianChart>
用于实例化图表的代码:
public partial class Chart : UserControl
{
public SolidColorBrush AxisStrokeColor { get; set; }
public SeriesCollection Series { get; set; }
public string[] XAxisLabels { get; set; }
public Func<double, string> LabelFormatter { get; set; }
public Chart()
{
Series = new SeriesCollection();
InitializeComponent();
AxisStrokeColor = Brushes.Transparent;
CartesianChart.DataContext = this;
CartesianChart.LegendLocation = LegendLocation.Right;
}
public void addBarSeries(List<KeyValuePair<string, double>> data, string title)
{
ChartValues<ObservableValue> values = new ChartValues<ObservableValue>();
string[] labels = new string[data.Count];
for (int i = 0; i < data.Count; i++)
{
values.Add(new ObservableValue(data[i].Value));
labels[i] = data[i].Key;
}
Series.Add(new ColumnSeries
{
Title = title,
Values = values,
DataLabels = true,
});
XAxisLabels = labels;
LabelFormatter = value => value.ToString("N");
}
}
我用来用测试数据填充图表的功能
Chart.addBarSeries(seedData3(), "myTitle");
public List<KeyValuePair<string, double>> seedData3()
{
var returnCollection = new List<KeyValuePair<string, double>>();
for (int i = 0; i < 10; i++)
{
returnCollection.Add(new KeyValuePair<string, double>("Timmy " + i, i));
}
return returnCollection;
}
如何确保每一列都显示一个标签?
答案 0 :(得分:1)
轴上显示的标签数量是自动计算的,以适合视图而不会重叠。
如果您想强制渲染标签的值,只需在Step
上添加Separator
属性:
<lvc:Separator Step="1" Stroke="{Binding AxisStrokeColor}"></lvc:Separator>