xsfh
Posts: 3
Joined: Fri Dec 06, 2024 8:09 am

can drag stock chart left and right in Win browser but not in mobile browser

Hi all,

I'm new to all this, so much to learn.

I'm working on a simple web site, (nginx + nwsgi + python flask), I calculate my own data in python, then show the data in web page with help of highcharts stock.

Problem is, I can drag candle sticks left and right with mouse in windows browser, like a normal stock chart. Is it called pan? But if I visit in mobile browser (tried on multiple browsers in android, and safari in iphone), touch and drag won't move it. Even, in android, the focus seems to be on the whole page, so dragging left would get back to previous page, etc.

I've tried disabling tooltip, does not help. And in windows browser, tooltip does not disturb the mouse dragging, so it should be none relevant.

Could anybody please enlighten me ?

Here's part of the code in my index.html:

Code: Select all

function plotCharts(chartdata, volume) {    
        Highcharts.stockChart('container', {
            navigation: {
                bindings: {
                    rect: {
                        annotationsOptions: {
                            shapeOptions: {
                                fill: 'rgba(255, 0, 0, 0.8)'
                            }
                        }
                    }
                },
                annotationsOptions: {
                    typeOptions: {
                        line: {
                            stroke: 'rgba(255, 0, 0, 1)',
                            strokeWidth: 10
                        }
                    }
                }
            },
            chart: {	
                panning: true,
                panKey: 'shift',  // Optional: specify a key for panning
                events: {
                    load: function () {
                        const chart = this;
                        // Prevent default touch behavior
                        Highcharts.wrap(Highcharts.Pointer.prototype, 'touchMove', function (proceed, e) {
                            if (e.touches.length === 1) {
                                e.preventDefault(); // Prevent default scrolling
                                this.chart.pan(e); // Call pan method
                            } else {
                                proceed.call(this, e); // Proceed with default behavior for other gestures
                            }
                        });
                    }
                }
            },
            yAxis: [{
                labels: {
                    align: 'left',
                    formatter: function() {
                        return this.value; // Return the full value without abbreviation
                    }
                },
                height: '80%'
            }, {
                labels: {
                    align: 'left',
                    formatter: function() {
                        return this.value; // Return the full value without abbreviation
                    }
                },
                top: '80%',
                height: '20%',
                offset: 0
            }],
            tooltip: {
                shared: true,
                valueDecimals: 2, // Set decimal places for values
                formatter: function() {
                    let s = `<b>${Highcharts.dateFormat('%A, %b %e, %Y', this.x)}</b><br/>`;
                    this.points.forEach(point => {
                        s += `${point.series.name}: ${point.y.toFixed(2)}<br/>`; // Display full price
                    });
                    return s;
                }
            },
            series: [{
                type: 'candlestick',
                id: 'aapl-ohlc',
                name: 'AAPL Stock Price',
                data: chartdata,
                color: 'red',           // Color for bearish candles
                upColor: 'green',       // Color for bullish candles
                dataGrouping: {
                    enabled: false // Disable data grouping
                }
            },{
                type: 'column',
                id: 'aapl-volume',
                name: 'AAPL Volume',
                data: volume,
                dataGrouping: {
                    enabled: false // Disable data grouping
                },
                yAxis: 1
            },{
                type: 'sma',
                linkedTo: 'aapl-ohlc',
                params: {
                    period: 20
                },
                lineWidth: 1, // Change this value to set the desired line width
                color: '#FF0000', // Change this value to set the desired color (e.g., red)
                marker: { // Disable markers (dots)
                    enabled: false
                }
            }]
        });


    }

And related CSS:

Code: Select all

#container {
    touch-action: pan-x; /* Allow horizontal panning */
    -ms-touch-action: pan-x; /* Microsoft support */
	width: 100%; /* Ensure the chart takes the full width */
    height: 400px; /* Set fixed height for the chart */
}
xsfh
Posts: 3
Joined: Fri Dec 06, 2024 8:09 am

Re: can drag stock chart left and right in Win browser but not in mobile browser

Allow me to borrow an example from another thread:
https://jsfiddle.net/BlackLabel/xakzurgb/
Please visit with mobile browser and try touch and drag the stock line left and right. Now dragging it would move the page, instead of line/candlebars.
andrzej.b
Site Moderator
Posts: 508
Joined: Mon Jul 15, 2024 12:34 pm

Re: can drag stock chart left and right in Win browser but not in mobile browser

Hi,

Welcome to our forum and thanks for contacting us with your question!

In Highcharts Stock, by default tooltip.followTouchMove is enabled, which affects dragging - it works on mobile devices only when dragging with two fingers, please see the API reference.
You can test this demo on a mobile device, where followTouchMove is disabled, and dragging with one finger works: https://jsfiddle.net/BlackLabel/Lezod2w1/

Feel free to reach out if you have any further questions.

Kind regards,
Andrzej
Highcharts Developer
xsfh
Posts: 3
Joined: Fri Dec 06, 2024 8:09 am

Re: can drag stock chart left and right in Win browser but not in mobile browser

andrzej.b wrote: Fri Dec 06, 2024 2:07 pm Hi,

Welcome to our forum and thanks for contacting us with your question!

In Highcharts Stock, by default tooltip.followTouchMove is enabled, which affects dragging - it works on mobile devices only when dragging with two fingers, please see the API reference.
You can test this demo on a mobile device, where followTouchMove is disabled, and dragging with one finger works: https://jsfiddle.net/BlackLabel/Lezod2w1/

Feel free to reach out if you have any further questions.

Kind regards,
Marvellous! Works like a charm. Also thanks for pointing out the API ref, so handy with search function.

Much appreciated!

Return to “Highcharts Stock”