简体   繁体   中英

WPF Toolkit : Label data points on chart with distinct labels

I want to add labels to my charts at the location of the data point on the graph. So for example, a column chart would have a label at the top of each column.

I need to do this programmatically and the labels should be different than the labels on the axes of the chart. For example, in addition to my KeyValuePair[] that supplies data for charting the points, I need to have another array that supplies labels for the chart. I also have to do this dynamically, not in the XAML.

Currently, I create my chart's data with an SQL query. Ideally, I would like to use the third row in my SQL query to populate the chart labels.

Well I figured it out. I made a dictionary of independent values and labels, set it as the DataContext of my window, and bound the textbox to it with an IValueConverter that uses the dependent value to map the label using the dictionary.

<TextBlock
            x:Name="textBox"
            Text="{Binding Key, Converter={StaticResource annotationsConverter}}"
            FontWeight="Bold"
            Margin="2"
            TextWrapping="Wrap"/>

and

public class myAnnotationsConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (MainWindow.annotations != null)
        {
            try
            {
                return MainWindow.annotations[value as string];
            }
            catch (KeyNotFoundException e)
            {
                return "NULL";
            }

        }
        throw new NotFiniteNumberException();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotFiniteNumberException();
    }
}

Possible improvement: support more than one column per category in the category axis. So, if I have three bars for June-2007, maintain a distinct label for all of them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM