如何在WPF图表控件上只获取一个x轴值

时间:2013-12-17 21:57:40

标签: wpf charts

我在图表中的每一行的x轴上获得一个新值。我只需要图表行x-axit上的第一个(或最后一个)值或更好,它只显示1-12个月。

  LineSeries lineSeries = new LineSeries();
  lineSeries.DataPointStyle = dataPointStyle;
  lineSeries.DependentValuePath = "Indextal";
  lineSeries.IndependentValuePath = "Dato";

  lineSeries.IndependentAxis = new DateTimeAxis
  {
    Orientation = AxisOrientation.X,
    Location = AxisLocation.Bottom,
    IntervalType = DateTimeIntervalType.Months
  };
  //((DateTimeAxis)lineSeries.IndependentAxis).Visibility = Visibility.Hidden;

  lineSeries.ItemsSource = IndsætGrafData(år);
  chart_LineSeries.Series.Add(lineSeries);

图片显示了我得到的结果。我每个月只需要1个日期。 There are to many dates

2 个答案:

答案 0 :(得分:0)

这是因为您创建了DateTimeAxis 2次。将此轴的构造移动到这样的构造函数,以便只创建一次:

private DateTimeAxis _xAxis;

public MainWindow()
{
    InitializeComponent();

    this._xAxis = new DateTimeAxis
    {
        Orientation = AxisOrientation.X,
        Location = AxisLocation.Bottom,
        IntervalType = DateTimeIntervalType.Months
    };

    chart_LineSeries.Axes.Add(this._xAxis);

    // ...
}

然后更改设置轴的代码:

lineSeries.IndependentAxis = this._xAxis;

答案 1 :(得分:0)

Vorrtex,当我尝试你的答案时,我得到了这个结果: enter image description here

以下是一些代码,也许它可以提供帮助?

private DateTimeAxis _xAxis;

public MainWindow()
{
InitializeComponent();

this._xAxis = new DateTimeAxis
{
    Orientation = AxisOrientation.X,
    Location = AxisLocation.Bottom,
    IntervalType = DateTimeIntervalType.Months
};

chart_LineSeries.Axes.Add(this._xAxis);
}


void GrafListe()
{
  label_Info.Content = "";
  chart_LineSeries.Series.Clear();
  aktiekursListe = controller_Aktiekurs_Database.HentAltFraAktiekursTabellen(idVirksomhedensStamdata);

  if (aktiekursListe.Count() > 0)
  {
    datoListe = controller_Dato.HentAlleDatoer();
    chart_LineSeries.Visibility = Visibility.Visible;

    var myYear =
      from a in aktiekursListe
      join d in datoListe on a.IdDato equals d.Id
      orderby d.DagTid descending
      select new { d.DagTid.Year };

    int[] antalÅr = new int[myYear.Distinct().Count()]; 
    int antal = 0;
    foreach (var item in myYear.Distinct())
    {
      antalÅr[antal] = item.Year;
      antal++;
    }

    for (int i = 0; i < antalÅr.Count(); i++)
    {
      GrafData(antalÅr[i]);  
    }
  }
  else
    label_Info.Content = "xx";
}




void GrafData(int år)
{
  Style dataPointStyle;
  dataPointStyle = GetNewDataPointStyle(år); 

  LineSeries lineSeries = new LineSeries();
  lineSeries.DataPointStyle = dataPointStyle;
  lineSeries.Title = år.ToString();
  lineSeries.DependentValuePath = "Indextal";
  lineSeries.IndependentValuePath = "Dato";
  lineSeries.ItemsSource = IndsætGrafData(år);
  chart_LineSeries.Series.Add(lineSeries);
}