【问题标题】:Chart doesn't make space for empty columns图表不为空列腾出空间
【发布时间】:2014-06-10 09:18:23
【问题描述】:

我正在为每周统计数据的可视化创建图表。这意味着我需要一个 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;

【问题讨论】:

  • 您的问题无法从您的代码中重现。
  • 我从外部设备读取日志,因此您无法重现它。试试我现在添加的代码,它创建了相同的条件。

标签: wpf charts wpftoolkit


【解决方案1】:

我不知道它是否相关,但我过去使用过 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;

}

【讨论】:

  • 看起来不错;老实说,我首先看到了 Oxyplot,但后来我决定使用 WPF 工具包,因为它看起来更简单。不幸的是,我在使用它时发现了很多限制,所以我可能会重新考虑......
  • 很久没有使用它,我花了几个小时才站起来,但是当我第一次开始寻找图表时,我尝试了 WPF 工具包,结果非常失望。我用 Oxyplot 做了一些很酷的事情,它正在开发中,你也可以在他们的论坛上获得帮助。
  • 谢谢,我一定会试试的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多