我正在为每周统计数据的可视化创建一个图表。这意味着我需要一个7列图表,如果数据不可用,请在相应的列上留一个空格。 我遇到了很多麻烦,因为我还没有找到适当的WPF图表指南(如果你有任何可以随意分享的话)
这是我的图表:
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>
这就是背后的代码:
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;
}
}
如果缺少数据,这就是我填写图表的方式:
if (LastWeek.Count() < 7)
for (int i = LastWeek.Count(); i < 7; i++)
LastWeek.Insert(0, new SingleBar());
因此,如果数据少于7个,则会在开头插入空数据。 现在我在代码中看到了条形图的添加,但是在图表上只显示了一个空条,无论我添加了多少个空条。有人可以帮帮我吗?
编辑:要重现它,请尝试以下方法:
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;
答案 0 :(得分:0)
我不知道它是否相关,但我过去使用过Oxyplot并且发现它更容易使用(读作:wpf工具包很痛苦)。
以下是在每个系列的开头使用带有一些空条的oxyplot的示例:
您可以找到他们的示例here。只需单击您喜欢的那个,就可以查看代码(在底部您有代码/绘图选项卡)。
以下是screetshot的代码,以防你想知道:
的Xaml:
<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;
}