Reputation: 1365
I need to set the chart series title programatically using the ModernUI WPF charting.
I create the charts in the following way, allowing for easy data entry:
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; }
}
And then in the Main Window:
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;
}
In the 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>
How do I set programmatically the SeriesTitle value?
Upvotes: 0
Views: 2652
Reputation: 14037
In order to change the title programmatically, you can access properties of the chart control and set some other values:
var chartSeries = test1.Series.First();
chartSeries.SeriesTitle = "New title";
Also you can create and add series in C# code:
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);
Upvotes: 3