如何填充Silverlight图表WP7

时间:2012-02-02 13:10:56

标签: windows-phone-7 collections charts

我需要创建一个包含多个系列的(WP7)图表。我试图想象的数据:

F.ex跟随集合,包含UserName,Date,Points:

  1. User1,2011-11-09,6
  2. User2,2011-11-09,8
  3. User1,2011-11-02,9
  4. User2,2011-11-02,8
  5. 数据中可以有两个以上的用户。

    XAML命名空间

    xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    

    代码隐藏

    mdChart.Series.Add(MDSeries);                
    mdChart.Title = "Statistics";
    MDSeries.SetBinding(ColumnSeries.ItemsSourceProperty, new Binding());
    MDSeries.ItemsSource = lCompetitionStats;
    MDSeries.DependentValuePath = "Points";
    series.IndependentValuePath = "Date";
    MDSeries.IndependentValuePath = "UserName";
    

    XAML

    <controls:PanoramaItem Header="mdscores">
        <Grid>
            <charting:Chart x:Name="mdChart" Foreground="Blue" Background="Black">
                <charting:ColumnSeries Background="Black"/>
            </charting:Chart>
        </Grid>
    </controls:PanoramaItem>
    

    因此,我有X轴上的用户和Y轴上的点。我需要X轴上的日期,Y轴上的点和earch用户的单独系列。如何创建/填充这样的图表运行时?

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

代码隐藏

public class ColumnChartValues
{
    #region Variables
    public string Label { get; private set; }
    public double Value { get; private set; }
    #endregion

    #region Constructor and Initialization
    public ColumnChartValues()
    {
        Label = string.Empty;
        Value = 0;
    }

    public ColumnChartValues(string label, double value)
    {
        Label = label;
        Value = value;
    }
    #endregion
}


public partial class StatsPage : Page
{
    public ObservableCollection<ColumnChartValues> ColumnChartDatabase { get; private set; }

    public StatsPage()
    {
        DataContext = this;
        InitializeColumnChart();
    }

    private void InitializeColumnChart()
    {                
        mdChart.Title = "Statistics";

        ColumnChartDatabase = new ObservableCollection<BarChartValues>();
        ColumnChartDatabase.Add(new ColumnChartValues("Value One: ", 1));
        ColumnChartDatabase.Add(new ColumnChartValues("Value Two: ", 2));
        ColumnChartDatabase.Add(new ColumnChartValues("Value Three: ", 3));
    }
}

XAML

<charting:Chart x:Name="mdChart" Foreground="Blue" Background="Black">
    <charting:ColumnSeries  Background="Black" ItemsSource="{Binding ColumnChartDatabase}" IndependentValueBinding="{Binding Label}" DependentValueBinding="{Binding Value}"/>
</charting:Chart>

FYI

您必须编辑ColumnChartValues图表以满足您的需求