|
<chart:SfChart.SecondaryAxis>
<chart:NumericalAxis LabelCreated="NumericalAxis_LabelCreated"/>
</chart:SfChart.SecondaryAxis> |
|
numericalAxis.LabelCreated += NumericalAxis_LabelCreated; |
|
private void NumericalAxis_LabelCreated(object sender, ChartAxisLabelEventArgs e)
{
e.LabelContent = Convert.ToDouble(e.LabelContent) / 1000 + "k";
} |
|
<chart:SfChart.PrimaryAxis>
<chart:CategoryAxis LabelClicked ="CategoryAxis_LabelClicked"/>
</chart:SfChart.PrimaryAxis> |
|
categoryAxis.LabelClicked += CategoryAxis_LabelClicked; |
|
private void CategoryAxis_LabelClicked(object sender, LabelClickedEventArgs e)
{
var annotation = new EllipseAnnotation();
annotation.X1 = e.Position;
annotation.Y1 = viewModel.Data[(int)e.Position].YValue;
annotation.Height = 20;
annotation.Width = 20;
Chart.ChartAnnotations.Clear();
Chart.ChartAnnotations.Add(annotation);
} |
|
private async void CategoryAxis_LabelClicked(object sender,LabelClickedEventArgs e)
{
var dataPoint = viewModel.Data[(int)e.Position] as Model;
float xPoint = (float)Chart.ValueToPoint(Chart.PrimaryAxis, (int)e.Position);
float yPoint = (float)Chart.ValueToPoint(Chart.SecondaryAxis, dataPoint.YValue);
await Task.Delay(500);
if (Device.RuntimePlatform != Device.UWP)
{
tooltipBehavior.Show(xPoint, yPoint, true);
}
else
DependencyService.Get<IChartDependencyService>().ShowTooltip(Chart, tooltipBehavior, dataPoint);
} |
|
<chart:SfChart.ChartBehaviors>
<chart:ChartTooltipBehavior x:Name="tooltipBehavior" />
</chart:SfChart.ChartBehaviors> |
|
ChartTooltipBehavior tooltipBehavior = new ChartTooltipBehavior();
Chart.ChartBehaviors.Add(tooltipBehavior); |
|
public class ChartDependencyService : IChartDependencyService
{
public void ShowTooltip(SfChart chart, ChartTooltipBehavior tooltip, ModeldataPoint)
{
var property = typeof(ChartTooltipBehavior).GetProperty("NativeObject",BindingFlags.Instance | BindingFlags.NonPublic);
var nativeChart = SfChartRenderer.GetNativeObject(typeof(SfChart), chart) as Native.SfChart;
Native.CartesianSeries series = nativeChart.Series[0] asNative.CartesianSeries;
var index = (series.ItemsSource as List<Model>).IndexOf(dataPoint);
var xPoint = nativeChart.ValueToPoint(nativeChart.PrimaryAxis, index);
var yPoint = nativeChart.ValueToPoint(nativeChart.SecondaryAxis, dataPoint.YValue);
FieldInfo fieldInfo = series.GetType().GetField("mousePos",BindingFlags.NonPublic | BindingFlags.Instance);
fieldInfo.SetValue(series, new Windows.Foundation.Point(xPoint + nativeChart.SeriesClipRect.Left, yPoint));
var data = new TooltipModel() { XData = dataPoint.XValue, YData = dataPoint.YValue };
MethodInfo info =typeof(Native.ChartSeriesBase).GetMethod("UpdateSeriesTooltip",BindingFlags.NonPublic | BindingFlags.Instance);
info?.Invoke(series, new object[] { data });
tooltip.BackgroundColor = chart.Series[0].Color;
}
} |
|
<chart:SfChart.Series>
<chart:ColumnSeries ItemsSource="{Binding Data}" XBindingPath="XValue" YBindingPath="YValue" EnableTooltip="True" Color="LightBlue"/>
</chart:SfChart.Series> |
|
ColumnSeries columnSeries = new ColumnSeries();
columnSeries.ItemsSource = viewModel.Data;
columnSeries.XBindingPath = "XValue";
columnSeries.YBindingPath = "YValue";
columnSeries.EnableTooltip = true;
columnSeries.Color = Color.LightBlue;
Chart.Series.Add(columnSeries); |