0

我需要使用 ModernUI WPF 图表以编程方式设置图表系列标题。

我通过以下方式创建图表,以便轻松输入数据:

public class MainViewModel
{    
    public ObservableCollection<ChartData> Populations {get; set;}    
    public MainViewModel()
    {
        Populations = new ObservableCollection<ChartData>();         
    }

    public void Add(string key, int value)
    {
        Populations.Add(new ChartData() { dataName = key, dataValue = value });
    }
}

public class ChartData
{
    public string dataName { get; set; }
    public int dataValue  { get; set; }
}

然后在主窗口中:

public MainWindow()
{
    InitializeComponent();

    MainViewModel mvm = new MainViewModel();
    mvm.Add("asd", 123);
    mvm.Add("sdfs", 133);
    mvm.Add("asda", 129);
    mvm.Add("asgfgfhd", 23);

    test1.DataContext = mvm;
}

在 XAML 中:

    <chart:StackedColumnChart x:Name="test1" ChartSubTitle="Population in millions"
        ChartTitle="Countries by population" Margin="10,10,0,0" HorizontalAlignment="Left" Width="1573" Height="475" VerticalAlignment="Top">
        <chart:StackedColumnChart.Series>

            <chart:ChartSeries DisplayMember="dataName"
                ItemsSource="{Binding Populations}"
                *SeriesTitle="World largest populations"*
                ValueMember="dataValue" />
        </chart:StackedColumnChart.Series>      

    </chart:StackedColumnChart>

如何以编程方式设置SeriesTitle值?

4

1 回答 1

3

为了以编程方式更改标题,您可以访问图表控件的属性并设置一些其他值:

var chartSeries = test1.Series.First();
chartSeries.SeriesTitle = "New title";

您还可以在 C# 代码中创建和添加系列:

var series = new ChartSeries();
series.ItemsSource = items; // a collection from somewhere else
series.DisplayMember = "dataName";
series.ValueMember = "dataValue";
series.SeriesTitle = "Title";

test1.Series.Add(series);
于 2014-04-04T15:29:15.187 回答