Xamarin.forms 折线图显示点击点的信息
我正在用Xamarin表单为一个网页实现一个小应用程序,问题是在这个网页中是一个包含多个条目的线性图表,如果用户单击线条的某个点,就会显示该点的信息,如图所示: 经过一些工作,我可以使用OxyPlot.Xamarin.Forms插件创建一个或多或少类似的折线图,其中包含多个显示点的条目 这是我的代码:Xamarin.forms 折线图显示点击点的信息,xamarin.forms,linechart,oxyplot,multiple-entries,Xamarin.forms,Linechart,Oxyplot,Multiple Entries,我正在用Xamarin表单为一个网页实现一个小应用程序,问题是在这个网页中是一个包含多个条目的线性图表,如果用户单击线条的某个点,就会显示该点的信息,如图所示: 经过一些工作,我可以使用OxyPlot.Xamarin.Forms插件创建一个或多或少类似的折线图,其中包含多个显示点的条目 这是我的代码: OnPropertyChanged("GraphModel"); var model = new PlotModel {
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;
应该在单击时显示x和y值,但不显示任何内容,有什么建议吗?
应该显示这样的内容:
根据以下示例:
更新代码
打印视图的Xaml声明:
<oxy:PlotView
Grid.Row="2"
Grid.RowSpan="2"
Grid.ColumnSpan="4"
x:Name="Plot" />
你的问题在于下面这句话
lineSeries.TrackerKey = "X={2},\nY={4}";
当您使用series.TrackerKey时,您指定您使用的是CustomTracker,而在本例中您不是。如果您需要为模型中的每个系列使用不同的跟踪器,则自定义跟踪器将非常有用
在您的情况下,应该删除该行并仅使用TrackPerformatString
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接口变得方便。您可以通过实现该接口来创建自定义数据点,然后还可以在工具提示中包含相同的信息
根据评论更新
此外,要包括触地,可以在PlotController中指定触地。可以通过在viewModel中定义PlotController来执行此操作,如下所示
public PlotController CustomPlotController{get;set;}
您可以按如下方式定义属性
CustomPlotController = new PlotController();
CustomPlotController.UnbindAll();
CustomPlotController.BindTouchDown(PlotCommands.PointsOnlyTrackTouch);
在你的Xaml中
<oxy:Plot Controller="{Binding CustomPlotController}"......
你用过OxyPlot.Xamarin.Forms吗?是的@LucasZhang MSFT!但这不会改变任何事情。。你什么意思?所以,现在没有显示工具提示,或者工具提示是空的,或者其他什么?@AnuViswan没有显示工具提示,这与命名有点混淆。哪一个是绑定在xaml中的PlotModel?它叫折线图、图形模型还是绘图?你也可以分享你的xaml吗?我用不同的方式尝试了图形部分,在文章示例之后,我的问题是,只有当我在一个点上做了标记后运行应用程序时,才使用TrackPerformatString参数来显示x和y值,但仍然不清楚你的意思。你到底在哪里面对这些问题?你根本没有得到工具提示,或者它只在鼠标点击时显示,你想在鼠标上方显示吗?当我点击一个点时,工具提示不会显示在任何地方,我想要的是在用户点击/点击后,轨迹会像点图像上的跟踪器信息一样显示。你是否尝试删除这行代码lineSeries.TrackerKey=X={2},\nY={4}?是的,我只尝试了以下行:lineSeries.trackperformatstring=X={2},\nY={4};
public PlotController CustomPlotController{get;set;}
CustomPlotController = new PlotController();
CustomPlotController.UnbindAll();
CustomPlotController.BindTouchDown(PlotCommands.PointsOnlyTrackTouch);
<oxy:Plot Controller="{Binding CustomPlotController}"......