我需要使用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 值?
答案 0 :(得分: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);