RadChart - X轴本身的工具提示 - Telerik

时间:2014-10-02 10:13:38

标签: wpf telerik radchart

我正在使用WPF应用程序并使用RadChart控件。  我熟悉ItemToolTipFormat和DataPointMember =“Tooltip”功能,但我想知道以下是否可行:

我附上了一张图片用于演示:

enter image description here

有可能当我将鼠标光标悬停在x轴类别上时,我会得到一个工具提示:  例如:在附图中,当我用鼠标光标悬停在单词May(或9月或11月等)上时,我会得到一个工具提示。

上面提到的功能会发生什么,我在图表上得到了一个工具提示,但如上所述,我想在x轴上对类别本身进行工具提示(当我将鼠标悬停在图像中显示的月份字样上时)。

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

这是一个很好的问题。据我所知,如果不使用有争议的实施,您将无法在这些标签上设置工具提示。我有一个解决方案,但它相当hacky,只支持固定轴数据。它不支持绑定(我还没有找到通过ItemMapping元素传递ToolTip内容的方法)。

解决方案有3个部分;一个ResourceDictionary,一个ConverterRadChart控件。

ResourceDictionary(名为" ToolTipResources.xaml ",位于同一dll的资源文件夹中)包含内容工具提示:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel
        x:Key="Jan">
        <TextBlock
            HorizontalAlignment="Left"
            Text="Jan"
            FontWeight="Bold" />
        <TextBlock
            HorizontalAlignment="Left"
            Text="In January" />
    </StackPanel>
    <StackPanel
        x:Key="Feb">
        <TextBlock
            HorizontalAlignment="Left"
            Text="Feb"
            FontWeight="Bold" />
        <TextBlock
            HorizontalAlignment="Left"
            Text="In February" />
    </StackPanel>
    <StackPanel
        x:Key="Mar">
        <TextBlock
            HorizontalAlignment="Left"
            Text="Mar"
            FontWeight="Bold" />
        <TextBlock
            HorizontalAlignment="Left"
            Text="In March" />
    </StackPanel>
    <StackPanel
        x:Key="Apr">
        <TextBlock
            HorizontalAlignment="Left"
            Text="Apr"
            FontWeight="Bold" />
        <TextBlock
            HorizontalAlignment="Left"
            Text="In April" />
    </StackPanel>
    <StackPanel
        x:Key="May">
        <TextBlock
            HorizontalAlignment="Left"
            Text="May"
            FontWeight="Bold" />
        <TextBlock
            HorizontalAlignment="Left"
            Text="In May" />
    </StackPanel>
    <StackPanel
        x:Key="Jun">
        <TextBlock
            HorizontalAlignment="Left"
            Text="Jun"
            FontWeight="Bold" />
        <TextBlock
            HorizontalAlignment="Left"
            Text="In June" />
    </StackPanel>
    <StackPanel
        x:Key="Jul">
        <TextBlock
            HorizontalAlignment="Left"
            Text="Jul"
            FontWeight="Bold" />
        <TextBlock
            HorizontalAlignment="Left"
            Text="In July" />
    </StackPanel>
    <StackPanel
        x:Key="Aug">
        <TextBlock
            HorizontalAlignment="Left"
            Text="Aug"
            FontWeight="Bold" />
        <TextBlock
            HorizontalAlignment="Left"
            Text="In August" />
    </StackPanel>
    <StackPanel
        x:Key="Sep">
        <TextBlock
            HorizontalAlignment="Left"
            Text="Sep"
            FontWeight="Bold" />
        <TextBlock
            HorizontalAlignment="Left"
            Text="In September" />
    </StackPanel>
    <StackPanel
        x:Key="Oct">
        <TextBlock
            HorizontalAlignment="Left"
            Text="Oct"
            FontWeight="Bold" />
        <TextBlock
            HorizontalAlignment="Left"
            Text="In October" />
    </StackPanel>
    <StackPanel
        x:Key="Nov">
        <TextBlock
            HorizontalAlignment="Left"
            Text="Nov"
            FontWeight="Bold" />
        <TextBlock
            HorizontalAlignment="Left"
            Text="In November" />
    </StackPanel>
    <StackPanel
        x:Key="Dec">
        <TextBlock
            HorizontalAlignment="Left"
            Text="Dec"
            FontWeight="Bold" />
        <TextBlock
            HorizontalAlignment="Left"
            Text="In December" />
    </StackPanel>
</ResourceDictionary>

Converter标签名称及其相关的工具提示内容:

/// <summary>
/// Converts chart label names into associated ToolTip content.
/// </summary>
[ValueConversion(typeof(string), typeof(object))]
public class MonthToToolTipConverter : IValueConverter
{
    private static string _assemblyName;

    static MonthToToolTipConverter()
    {
        _assemblyName = Assembly.GetExecutingAssembly().FullName;
    }

    /// <summary>
    /// Converts a value.
    /// </summary>
    /// <param name="value">The value produced by the binding source.</param>
    /// <param name="targetType">The type of the binding target property.</param>
    /// <param name="parameter">The converter parameter to use.</param>
    /// <param name="culture">The culture to use in the converter.</param>
    /// <returns>A converted value. If the method returns null, the valid null value is used.</returns>
    public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        object result = null;
        var labelName = value as string;
        if (labelName != null)
        {
            var toolTipResourcesDictionary = new ResourceDictionary();

            toolTipResourcesDictionary.Source = new Uri(String.Format("pack://application:,,,/{0};component/Resources/ToolTipResources.xaml", _assemblyName), UriKind.Absolute);
            result = toolTipResourcesDictionary[labelName];
        }
        return result;
    }

    /// <summary>
    /// Converts a value.
    /// </summary>
    /// <param name="value">The value that is produced by the binding target.</param>
    /// <param name="targetType">The type to convert to.</param>
    /// <param name="parameter">The converter parameter to use.</param>
    /// <param name="culture">The culture to use in the converter.</param>
    /// <returns>A converted value. If the method returns null, the valid null value is used.</returns>
    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

添加RadChart控件:

<telerikCharting:ChartArea.AxisX>
    <telerikCharting:AxisX>
        <telerikCharting:AxisX.AxisStyles>
            <telerikCharting:AxisStyles>
                <telerikCharting:AxisStyles.ItemLabelStyle>
                    <Style
                        TargetType="{x:Type TextBlock}">
                        <Setter
                            Property="ToolTip"
                            Value="{Binding Text, RelativeSource={RelativeSource Self}, Converter={StaticResource MonthToToolTipConverter}}" />
                    </Style>
                </telerikCharting:AxisStyles.ItemLabelStyle>
            </telerikCharting:AxisStyles>
        </telerikCharting:AxisX.AxisStyles>
    </telerikCharting:AxisX>
</telerikCharting:ChartArea.AxisX>

完整的RadChart视图(来自Telerik Documentation的示例):

<Window
    x:Class="YourChartProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
    xmlns:telerikCharting="clr-namespace:Telerik.Windows.Controls.Charting;assembly=Telerik.Windows.Controls.Charting"
    xmlns:local="clr-namespace:YourChartProject"
    Title="MainWindow"
    Height="350"
    Width="525">
    <Window.Resources>
        <local:MonthToToolTipConverter
            x:Key="MonthToToolTipConverter" />
    </Window.Resources>
    <Grid>
        <telerik:RadChart
            VerticalAlignment="Top">
            <telerik:RadChart.DefaultView>
                <telerikCharting:ChartDefaultView>
                    <telerikCharting:ChartDefaultView.ChartTitle>
                        <telerikCharting:ChartTitle
                            Content="Year 2009"
                            HorizontalAlignment="Center" />
                    </telerikCharting:ChartDefaultView.ChartTitle>
                    <telerikCharting:ChartDefaultView.ChartLegend>
                        <telerikCharting:ChartLegend
                            x:Name="chartLegend"
                            UseAutoGeneratedItems="True" />
                    </telerikCharting:ChartDefaultView.ChartLegend>
                    <telerikCharting:ChartDefaultView.ChartArea>
                        <telerikCharting:ChartArea
                            LegendName="chartLegend">
                            <telerikCharting:ChartArea.AxisX>
                                <telerikCharting:AxisX
                                    LayoutMode="Between"
                                    Title="Month">
                                    <telerikCharting:AxisX.AxisStyles>
                                        <telerikCharting:AxisStyles>
                                            <telerikCharting:AxisStyles.ItemLabelStyle>
                                                <Style
                                                    TargetType="{x:Type TextBlock}">
                                                    <Setter
                                                        Property="ToolTip"
                                                        Value="{Binding Text, RelativeSource={RelativeSource Self}, Converter={StaticResource MonthToToolTipConverter}}" />
                                                </Style>
                                            </telerikCharting:AxisStyles.ItemLabelStyle>
                                        </telerikCharting:AxisStyles>
                                    </telerikCharting:AxisX.AxisStyles>
                                </telerikCharting:AxisX>
                            </telerikCharting:ChartArea.AxisX>
                            <telerikCharting:ChartArea.DataSeries>
                                <!-- Line Chart -->
                                <telerikCharting:DataSeries
                                    LegendLabel="Turnover">
                                    <telerikCharting:DataSeries.Definition>
                                        <telerikCharting:LineSeriesDefinition></telerikCharting:LineSeriesDefinition>
                                    </telerikCharting:DataSeries.Definition>
                                    <telerikCharting:DataPoint
                                        YValue="154"
                                        XCategory="Jan" />
                                    <telerikCharting:DataPoint
                                        YValue="138"
                                        XCategory="Feb" />
                                    <telerikCharting:DataPoint
                                        YValue="143"
                                        XCategory="Mar" />
                                    <telerikCharting:DataPoint
                                        YValue="120"
                                        XCategory="Apr" />
                                    <telerikCharting:DataPoint
                                        YValue="135"
                                        XCategory="May" />
                                    <telerikCharting:DataPoint
                                        YValue="125"
                                        XCategory="Jun" />
                                    <telerikCharting:DataPoint
                                        YValue="179"
                                        XCategory="Jul" />
                                    <telerikCharting:DataPoint
                                        YValue="170"
                                        XCategory="Aug" />
                                    <telerikCharting:DataPoint
                                        YValue="198"
                                        XCategory="Sep" />
                                    <telerikCharting:DataPoint
                                        YValue="187"
                                        XCategory="Oct" />
                                    <telerikCharting:DataPoint
                                        YValue="193"
                                        XCategory="Nov" />
                                    <telerikCharting:DataPoint
                                        YValue="176"
                                        XCategory="Dec" />
                                </telerikCharting:DataSeries>
                                <!-- Bar Chart -->
                                <telerikCharting:DataSeries
                                    LegendLabel="Expenses">
                                    <telerikCharting:DataSeries.Definition>
                                        <telerikCharting:BarSeriesDefinition></telerikCharting:BarSeriesDefinition>
                                    </telerikCharting:DataSeries.Definition>
                                    <telerikCharting:DataPoint
                                        YValue="45"
                                        XCategory="Jan" />
                                    <telerikCharting:DataPoint
                                        YValue="48"
                                        XCategory="Feb" />
                                    <telerikCharting:DataPoint
                                        YValue="53"
                                        XCategory="Mar" />
                                    <telerikCharting:DataPoint
                                        YValue="41"
                                        XCategory="Apr" />
                                    <telerikCharting:DataPoint
                                        YValue="32"
                                        XCategory="May" />
                                    <telerikCharting:DataPoint
                                        YValue="28"
                                        XCategory="Jun" />
                                    <telerikCharting:DataPoint
                                        YValue="63"
                                        XCategory="Jul" />
                                    <telerikCharting:DataPoint
                                        YValue="74"
                                        XCategory="Aug" />
                                    <telerikCharting:DataPoint
                                        YValue="77"
                                        XCategory="Sep" />
                                    <telerikCharting:DataPoint
                                        YValue="85"
                                        XCategory="Oct" />
                                    <telerikCharting:DataPoint
                                        YValue="89"
                                        XCategory="Nov" />
                                    <telerikCharting:DataPoint
                                        YValue="80"
                                        XCategory="Dec" />
                                </telerikCharting:DataSeries>
                            </telerikCharting:ChartArea.DataSeries>
                        </telerikCharting:ChartArea>
                    </telerikCharting:ChartDefaultView.ChartArea>
                </telerikCharting:ChartDefaultView>
            </telerik:RadChart.DefaultView>
        </telerik:RadChart>
    </Grid>
</Window>