Oxyplot中的工具提示位置

时间:2019-06-08 09:38:13

标签: oxyplot

我最近正在使用OxyPlot为WPF项目绘制图表。我在一系列系列中的工具提示有问题。工具提示的矩形框太靠近鼠标所定位的位置。我想知道是否有任何方法可以修改工具提示框的位置并使它离光标更远。非常感谢。例如,我想将工具提示框(在红色框中)向右移一点。enter image description here

[编辑]

按照Anu的想法,我添加了以下代码,但似乎无法正常工作。

在xaml.cs文件中,我添加了

<UserControl.Resources>
    <popups:ScreenPointToOffSetScreenPointConverter x:Key="ScreenPointToOffSetScreenPointConverter"/>
</UserControl.Resources>

然后将我的OxyPlot更改为

<ItemsControl ItemsSource="{Binding AmplitudeDtos}" Margin="0" Name="Amplitude" >
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid popups:GridUtilities.ItemsPerRow="1" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate DataType="{x:Type dtoModels:SignalDto}">
            <oxy:PlotView Height="Auto" Width="Auto" MinHeight="40" MinWidth="100" HorizontalContentAlignment="Stretch" 
                          FontStyle="Normal" FontFamily="Roboto, Microsoft YaHei" FontSize="8" Model="{Binding PlotModel}" Controller="{Binding PlotController}" Padding="8" Margin="0">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Loaded">
                        <i:InvokeCommandAction Command="{Binding SignalLoadedCommand}" CommandParameter="{Binding}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
                <oxy:PlotView.DefaultTrackerTemplate>
                    <ControlTemplate>
                        <oxy:TrackerControl Position="{Binding Position, Converter={StaticResource ScreenPointToOffSetScreenPointConverter}}" LineExtents="{Binding PlotModel.PlotArea}">
                            <oxy:TrackerControl.Content>
                                <TextBlock Text="{Binding}" />
                            </oxy:TrackerControl.Content>
                        </oxy:TrackerControl>
                    </ControlTemplate>
                </oxy:PlotView.DefaultTrackerTemplate>
            </oxy:PlotView>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>    

请注意,我的OxyPlot位于DataTemplate中。

ScreenPointToOffSetScreenPointConverter类与Anu的示例完全相同。

1 个答案:

答案 0 :(得分:0)

相对简单的解决方法是修改DefaultTrackerTemplate并使用自定义转换器来更改位置。例如,您可以按以下方式定义ScreenPoint转换器

public class ScreenPointToOffSetScreenPointConverter : IValueConverter
{
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if(value is ScreenPoint screenPoint)
            {
                return new ScreenPoint(screenPoint.X + 50, screenPoint.Y);
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // TODO: Implement
            throw new NotImplementedException();
        }
    }

然后在您的XAML中

<oxy:PlotView  Model="{Binding MyModel}">
            <oxy:PlotView.DefaultTrackerTemplate>
                <ControlTemplate>
                    <oxy:TrackerControl Position="{Binding Position,Converter={StaticResource ScreenPointToOffSetScreenPointConverter}}" LineExtents="{Binding PlotModel.PlotArea}">
                        <TextBlock Text="{Binding}" />
                    </oxy:TrackerControl>
                </ControlTemplate>
            </oxy:PlotView.DefaultTrackerTemplate>
        </oxy:PlotView>

转换器将为Tracker的ScreenPoint位置添加一个偏移量。如果对您的应用程序要求有意义,则可以修改转换器的偏移值。

相关问题