0

我正在为网页实现一个带有 Xamarin Forms 的小应用程序,问题是在这个网页中是一个包含多个条目的线性图表,如果用户点击该线的一个点,则会显示有关该点的信息,如您所见在图片里:

网页折线图

经过一些工作,我可以使用 OxyPlot.Xamarin.Forms 插件创建或多或少相似的折线图,其中包含多个显示点的条目

我的应用折线图

这是我的代码:

        OnPropertyChanged("GraphModel");

        var model = new PlotModel
        {
            LegendPlacement = LegendPlacement.Outside,
            LegendPosition = LegendPosition.BottomCenter,
            LegendOrientation = LegendOrientation.Horizontal,
            LegendBorderThickness = 0
        };
        model.PlotType = PlotType.XY;
        model.InvalidatePlot(false);

        Dictionary<string, List<Prices>> values = HistoricData[Selected.ProductId];

        int colorIndex = 0;
        List<string> x_names = new List<string>();

        foreach (var item in values.Keys)
        {

            if (item.ToUpper() == Selected.ProductName) { SelectedIndex = colorIndex; }
            var lineSeries = new LineSeries()
            {
                Title = item,
                MarkerType = MarkerType.Circle,
            };
            lineSeries.MarkerResolution = 3;
            lineSeries.MarkerFill = OxyPlot.OxyColor.Parse(SubCategoriesViewModel.AvailableColors[colorIndex]);
            lineSeries.MarkerStroke = OxyPlot.OxyColor.Parse(SubCategoriesViewModel.AvailableColors[colorIndex]);
            lineSeries.MarkerSize = 3;

            var points = new List<DataPoint>();


            lineSeries.Color = OxyColor.Parse(SubCategoriesViewModel.AvailableColors[colorIndex]);

            foreach (var price in values[item])
            {
                points.Add(new DataPoint(price.Week+price.Year, price.Price));

            }

            if (ButtonsVisibility.Count == 0)
            {
                lineSeries.IsVisible = (Selected.ProductName == item.ToUpper()) ? true : false;
            }
            else
            {
                lineSeries.IsVisible = ButtonsVisibility[colorIndex];
            }


            lineSeries.ItemsSource = points;
            lineSeries.MarkerType = OxyPlot.MarkerType.Circle;
            model.Series.Add(lineSeries);
            colorIndex++;
        }

        NumButtons = colorIndex;
        LinearAxis yaxis = new LinearAxis();
        yaxis.Position = AxisPosition.Left;
        yaxis.MajorGridlineStyle = LineStyle.Dot;
        model.Axes.Add(yaxis);

        LineChart = model;
        OnPropertyChanged("GraphModel");
        return LineChart;

我的疑问是我应该使用哪个属性并至少显示一个具体点的值,我已经看到属性 OnTouchStarted 但仅适用于所有 LineSeries 而不是单个点。我在一些文章中读到 OxyPlot.Xamarin.Forms 包含一个跟踪器。我在我的代码中添加了这一行:

lineSeries.TrackerFormatString = "X={2},\nY={4}";

应该在点击时显示 x 和 y 值,但不显示任何内容,有什么建议吗?应该显示类似的内容:Tracker info on point

来自以下示例:跟踪器示例

更新代码

public PlotModel GetLineChart()
{
    OnPropertyChanged("GraphModel");

    var model = new PlotModel
    {
        LegendPlacement = LegendPlacement.Outside,
        LegendPosition = LegendPosition.BottomCenter,
        LegendOrientation = LegendOrientation.Horizontal,
        LegendBorderThickness = 0
    };
    model.PlotType = PlotType.XY;
    model.InvalidatePlot(false);

    Dictionary<string, List<Prices>> values = HistoricData[Selected.ProductId];

    int colorIndex = 0;
    List<string> x_names = new List<string>();

    foreach (var item in values.Keys)
    {

        if (item.ToUpper() == Selected.ProductName) { SelectedIndex = colorIndex; }
        var lineSeries = new LineSeries()
        {
            Title = item,
            MarkerType = MarkerType.Circle,
            CanTrackerInterpolatePoints = false

        };
        lineSeries.MarkerResolution = 3;
        lineSeries.MarkerFill = OxyPlot.OxyColor.Parse(SubCategoriesViewModel.AvailableColors[colorIndex]);
        lineSeries.MarkerStroke = OxyPlot.OxyColor.Parse(SubCategoriesViewModel.AvailableColors[colorIndex]);
        lineSeries.MarkerSize = 3;

        var points = new List<DataPoint>();


        lineSeries.Color = OxyColor.Parse(SubCategoriesViewModel.AvailableColors[colorIndex]);

        foreach (var price in values[item])
        {
           points.Add(new DataPoint(price.Week+price.Year, price.Price)); 
        }

        if (ButtonsVisibility.Count == 0)
        {
            lineSeries.IsVisible = (Selected.ProductName == item.ToUpper()) ? true : false;
        }
        else
        {
            lineSeries.IsVisible = ButtonsVisibility[colorIndex];
        }


        lineSeries.ItemsSource = points;
        lineSeries.MarkerType = OxyPlot.MarkerType.Circle;

        lineSeries.TrackerFormatString = "X={2},\nY={4}";

        lineSeries.TextColor = OxyPlot.OxyColor.Parse(SubCategoriesViewModel.AvailableColors[colorIndex]);
        model.Series.Add(lineSeries);
        colorIndex++;
    }

    NumButtons = colorIndex;

    LinearAxis yaxis = new LinearAxis();
    yaxis.Position = AxisPosition.Left;
    //yaxis.StringFormat = "X={2},\nY={4}";
    yaxis.MajorGridlineStyle = LineStyle.Dot;
    model.Axes.Add(yaxis);

    LineChart = model;
    OnPropertyChanged("GraphModel");
    return LineChart;

}

}

 protected async override void OnAppearing()
        {
            await _viewModel.LinearViewModel.GetSubCategoryHistoricWeekPrices(App.ViewModel.LoginViewModel.SesionToken, FROM_DATE, TO_DATE);
            Plot.Model = _viewModel.LinearViewModel.GetLineChart();
            PlotController controller = new PlotController();
            controller.UnbindAll();
            controller.BindTouchDown(PlotCommands.PointsOnlyTrackTouch);
            Plot.Controller = controller;



            AddButtons();


        }

绘图视图的 Xaml 声明:

<oxy:PlotView 
            Grid.Row="2"
            Grid.RowSpan="2"
            Grid.ColumnSpan="4"            
            x:Name="Plot" />
4

1 回答 1

1

您的问题在于以下行。

lineSeries.TrackerKey = "X={2},\nY={4}";

当您使用 series.TrackerKey 时,您指定您使用的是 CustomTracker,在这种情况下您不是。如果您需要为模型中的每个系列使用不同的跟踪器,自定义跟踪器将很有用。

在您的情况下,您应该删除该行并仅使用 TrackerFormatString。

lineSeries.TrackerFormatString = "X={2},\nY={4}";

这将使用格式字符串参数显示工具提示,其中 {2} 表示 X 值,{4} 表示 Y。为了您的信息,以下是占位符。

{0} = Title of Series
{1} = Title of X-Axis
{2} = X Value
{3} = Title of Y-Axis
{4} = Y Value

如果您需要在工具中包含其他/自定义信息,则您的数据点需要包含该信息。这就是 IDataPointProvider 接口变得方便的地方。您可以通过实现接口来创建自定义数据点,然后您也可以在工具提示中包含相同的信息。

根据评论更新

此外,要包含“Touch”,您可以在 PlotController 中指定 TouchDown。您可以通过在 viewModel 中定义 PlotController 来做到这一点,如下所示。

public PlotController CustomPlotController{get;set;}

您可以按如下方式定义该属性。

CustomPlotController = new PlotController();
CustomPlotController.UnbindAll();
CustomPlotController.BindTouchDown(PlotCommands.PointsOnlyTrackTouch);

在你的 Xaml

<oxy:Plot Controller="{Binding CustomPlotController}"......
于 2019-01-29T14:34:57.723 回答