This is a migrated thread and some comments may be shown as answers.

Get CategoricalAxis value on mouse move

1 Answer 160 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
Kirti
Top achievements
Rank 1
Kirti asked on 23 May 2020, 12:23 PM

I found this code on telerik resource site. This function return value of LinearAxis,

    Private Function GetVerticalAxisValueFromMouse(ByVal e As MouseEventArgs) As Object
        Dim axis As LinearAxis = TryCast(RadChartView1.Axes(1), LinearAxis)
        Dim delta As Double = axis.ActualRange.Maximum - axis.ActualRange.Minimum
        Dim totalHeight As Double = axis.Model.LayoutSlot.Height
        Dim ratio As Double = 1 - (e.Location.Y - Me.RadChartView1.Area.View.Viewport.Y - axis.Model.LayoutSlot.Y) / totalHeight
        Dim value As Double = axis.ActualRange.Minimum + delta * ratio
        Return value
    End Function

 

I want to get value of CategoricalAxis value from CandleStick Chart

1 Answer, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 27 May 2020, 03:25 PM

Hello Kirti,

According to the provided information, I understand that you have RadChartView with CandleStickSeries that plots its data on the DateTimeCategorical axis for the horizontal axis and Linear axis for the vertical axis. Your requirement is to get the value from the horizontal axis when there is an interaction with the mouse.  

In order to achieve this, I used an "onePixelTime" variable that shows how many ticks a single pixel of the chart represents. It can also be helpful when the view is zoomed to a particular date range. In the MouseDown event, the exact location is calculated. Please refer to the following example:

public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        CandlestickSeries candlestickSeries;
        public RadForm1()
        {
            InitializeComponent();
            this.radChartView1.MouseDown += this.RadChartView1_MouseDown;
            candlestickSeries = new CandlestickSeries();
            candlestickSeries.DataPoints.Add(new OhlcDataPoint(10, 11, 7, 8, DateTime.Now));
            candlestickSeries.DataPoints.Add(new OhlcDataPoint(8, 9, 5, 9, DateTime.Now.AddDays(1)));
            candlestickSeries.DataPoints.Add(new OhlcDataPoint(12, 12, 9, 10, DateTime.Now.AddDays(2)));
            candlestickSeries.DataPoints.Add(new OhlcDataPoint(7, 10, 6, 9, DateTime.Now.AddDays(3)));
            this.radChartView1.Series.Add(candlestickSeries);
        }
        
        // Represents how many ticks a single pixel of the chart represents
        double onePixelTime = 0;
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            
            var first = (OhlcDataPoint)candlestickSeries.DataPoints[0];
            var last = (OhlcDataPoint)candlestickSeries.DataPoints[candlestickSeries.DataPoints.Count - 1];
            TimeSpan span = (DateTime)last.Category - (DateTime)first.Category;
            
            double totalWidth = last.LayoutSlot.X + last.LayoutSlot.Width / 2 - first.LayoutSlot.X - first.LayoutSlot.Width / 2;
            this.onePixelTime = span.Ticks / totalWidth;
        }

        private void RadChartView1_MouseDown(object sender, MouseEventArgs e)
        {
            IChartView view = this.radChartView1.View;
            var firstDataPoint = (OhlcDataPoint)candlestickSeries.DataPoints[0];
            var offset = this.radChartView1.Area.View.Viewport.X + firstDataPoint.LayoutSlot.X + firstDataPoint.LayoutSlot.Width / 2;
            
            double location = e.Location.X - offset - view.PlotOriginX;
            long ticks = (long)(location * this.onePixelTime / view.ZoomWidth);
            TimeSpan span = TimeSpan.FromTicks(ticks);
            
            DateTime startDate = (DateTime)firstDataPoint.Category;
            DateTime clickedDate = startDate.Add(span);
            Console.WriteLine(clickedDate);
        }
    }

Note that this is just a sample approach and it may not cover all possible cases. Feel free to modify and extend it in a way that suits your requirements best.

Another approach that I would like to note is that RadChartView offers trackball behavior. It can display a vertical line across the chart plot area and also to display little visual indicators (circles by default) at points where the trackball line crosses the visualization of a series object. More information on this topic you can find here

I hope this information helps. If you need any further assistance please don't hesitate to contact me.  

Regards,
Nadya
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
ChartView
Asked by
Kirti
Top achievements
Rank 1
Answers by
Nadya | Tech Support Engineer
Telerik team
Share this question
or