Wpf 图表不会为空列留出空间
我正在为每周统计数据的可视化创建一个图表。这意味着我需要一个7列的图表,如果数据不可用,在相应的列上留下一个空白。 我遇到了很多麻烦,因为我还没有找到一个合适的指南,如果你有什么可以分享的话 这是我的图表:Wpf 图表不会为空列留出空间,wpf,charts,wpftoolkit,Wpf,Charts,Wpftoolkit,我正在为每周统计数据的可视化创建一个图表。这意味着我需要一个7列的图表,如果数据不可用,在相应的列上留下一个空白。 我遇到了很多麻烦,因为我还没有找到一个合适的指南,如果你有什么可以分享的话 这是我的图表: xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" xmlns
xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
xmlns:datavis="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
<charting:Chart Name="LastWeekChart" Grid.Row="0" Width="895" Height ="250" HorizontalAlignment="Center" >
<charting:Chart.LegendStyle>
<Style TargetType="datavis:Legend">
<Setter Property="Width" Value="0" />
</Style>
</charting:Chart.LegendStyle>
<charting:ColumnSeries DependentValuePath="Value" IndependentValuePath="Name" ItemsSource="{Binding}" />
</charting:Chart>
我不知道它是否相关,但我在过去使用过Oxyplot,并且发现它更容易使用read as:wpf工具包是一种痛苦 下面是一个使用oxyplot的示例,每个系列的开头都有一些空条: 你可以找到他们的例子。只需单击您喜欢的一个,您就可以在底部查看代码,您有一个“代码/打印”选项卡 下面是“尖叫秀”的代码,以防您产生疑问: Xaml: 代码隐藏:
public static List<SingleBar> LastWeek = new List<SingleBar>();
class SingleBar
{
public string Name { get; set; }
public double Value { get; set; }
public SingleBar()
{
this.Name = "";
this.Value = 0;
}
public SingleBar(string name, double value)
{
this.Name = name;
this.Value = value;
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
OutputChart = new PlotModel();
SetUpModel(OutputChart);
InitializeComponent();
}
private void SetUpModel(PlotModel plotModel)
{
var plotModel1 = new PlotModel();
plotModel1.LegendBorderThickness = 0;
plotModel1.LegendOrientation = LegendOrientation.Horizontal;
plotModel1.LegendPlacement = LegendPlacement.Outside;
plotModel1.LegendPosition = LegendPosition.BottomCenter;
plotModel1.Title = "No axes defined";
var linearAxis1 = new LinearAxis();
linearAxis1.MinimumPadding = 0;
linearAxis1.Position = AxisPosition.Bottom;
plotModel1.Axes.Add(linearAxis1);
var categoryAxis1 = new CategoryAxis();
categoryAxis1.MinorStep = 1;
categoryAxis1.Position = AxisPosition.Left;
categoryAxis1.Labels.Add("1");
categoryAxis1.Labels.Add("2");
categoryAxis1.Labels.Add("3");
categoryAxis1.Labels.Add("4");
categoryAxis1.Labels.Add("5");
categoryAxis1.Labels.Add("6");
categoryAxis1.Labels.Add("7");
plotModel1.Axes.Add(categoryAxis1);
var barSeries1 = new BarSeries();
barSeries1.StrokeThickness = 1;
barSeries1.Title = "Series 1";
barSeries1.Items.Add(new BarItem(0, -1));
barSeries1.Items.Add(new BarItem(0, -1));
barSeries1.Items.Add(new BarItem(0, -1));
barSeries1.Items.Add(new BarItem(0, -1));
barSeries1.Items.Add(new BarItem(137, -1));
barSeries1.Items.Add(new BarItem(18, -1));
barSeries1.Items.Add(new BarItem(40, -1));
plotModel1.Series.Add(barSeries1);
var barSeries2 = new BarSeries();
barSeries2.StrokeThickness = 1;
barSeries2.Title = "Series 2";
barSeries2.Items.Add(new BarItem(0, -1));
barSeries2.Items.Add(new BarItem(0, -1));
barSeries2.Items.Add(new BarItem(30, -1));
barSeries2.Items.Add(new BarItem(40, -1));
barSeries2.Items.Add(new BarItem(12, -1));
barSeries2.Items.Add(new BarItem(68, -1));
barSeries2.Items.Add(new BarItem(8, -1));
plotModel1.Series.Add(barSeries2);
OutputChart = plotModel1;
}
/// <summary>
/// Holds the plot (chart) we'll display
/// </summary>
public PlotModel OutputChart
{
get { return _outputChart; }
set
{
if (Equals(value, _outputChart)) return;
_outputChart = value;
}
}
private PlotModel _outputChart;
}
您的问题无法从代码中重现。我从外部设备读取日志,因此您无法重现它。试试我现在添加的代码,它创建了相同的条件;老实说,我首先看到了Oxyplot,但后来我决定使用WPF工具包,因为它看起来更简单。不幸的是,我在使用它的过程中发现了很多限制,所以我可能会重新考虑……在长时间不使用它之后,我花了几个小时才站稳脚跟,但当我第一次开始寻找图形时,我尝试了WPF工具包,并且非常失望。我用Oxyplot做了一些很酷的事情,它正在开发中,你也可以在他们的论坛上得到帮助。谢谢,我一定会尝试的!
LastWeek.Add(new SingleBar());
LastWeek.Add(new SingleBar());
LastWeek.Add(new SingleBar());
LastWeek.Add(new SingleBar());
LastWeek.Add(new SingleBar("Friday",50));
LastWeek.Add(new SingleBar("Saturday", 75));
LastWeek.Add(new SingleBar("Sunday",60));
LastWeekChart.DataContext = LastWeek;
<Window x:Class="wpfoxyempty.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:oxy="http://oxyplot.codeplex.com"
Title="MainWindow" Height="350" Width="525"
Name="OxyDemo"
>
<Grid>
<oxy:Plot Model="{Binding ElementName=OxyDemo, Path=OutputChart}"
Margin="5" />
</Grid>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
OutputChart = new PlotModel();
SetUpModel(OutputChart);
InitializeComponent();
}
private void SetUpModel(PlotModel plotModel)
{
var plotModel1 = new PlotModel();
plotModel1.LegendBorderThickness = 0;
plotModel1.LegendOrientation = LegendOrientation.Horizontal;
plotModel1.LegendPlacement = LegendPlacement.Outside;
plotModel1.LegendPosition = LegendPosition.BottomCenter;
plotModel1.Title = "No axes defined";
var linearAxis1 = new LinearAxis();
linearAxis1.MinimumPadding = 0;
linearAxis1.Position = AxisPosition.Bottom;
plotModel1.Axes.Add(linearAxis1);
var categoryAxis1 = new CategoryAxis();
categoryAxis1.MinorStep = 1;
categoryAxis1.Position = AxisPosition.Left;
categoryAxis1.Labels.Add("1");
categoryAxis1.Labels.Add("2");
categoryAxis1.Labels.Add("3");
categoryAxis1.Labels.Add("4");
categoryAxis1.Labels.Add("5");
categoryAxis1.Labels.Add("6");
categoryAxis1.Labels.Add("7");
plotModel1.Axes.Add(categoryAxis1);
var barSeries1 = new BarSeries();
barSeries1.StrokeThickness = 1;
barSeries1.Title = "Series 1";
barSeries1.Items.Add(new BarItem(0, -1));
barSeries1.Items.Add(new BarItem(0, -1));
barSeries1.Items.Add(new BarItem(0, -1));
barSeries1.Items.Add(new BarItem(0, -1));
barSeries1.Items.Add(new BarItem(137, -1));
barSeries1.Items.Add(new BarItem(18, -1));
barSeries1.Items.Add(new BarItem(40, -1));
plotModel1.Series.Add(barSeries1);
var barSeries2 = new BarSeries();
barSeries2.StrokeThickness = 1;
barSeries2.Title = "Series 2";
barSeries2.Items.Add(new BarItem(0, -1));
barSeries2.Items.Add(new BarItem(0, -1));
barSeries2.Items.Add(new BarItem(30, -1));
barSeries2.Items.Add(new BarItem(40, -1));
barSeries2.Items.Add(new BarItem(12, -1));
barSeries2.Items.Add(new BarItem(68, -1));
barSeries2.Items.Add(new BarItem(8, -1));
plotModel1.Series.Add(barSeries2);
OutputChart = plotModel1;
}
/// <summary>
/// Holds the plot (chart) we'll display
/// </summary>
public PlotModel OutputChart
{
get { return _outputChart; }
set
{
if (Equals(value, _outputChart)) return;
_outputChart = value;
}
}
private PlotModel _outputChart;
}