How can I add links in a Highcharts tooltip that will open on mobile's browser?

人走茶凉 提交于 2019-12-09 02:07:08

问题


I am developing a React Native app with expo. One of the screens contains a graphic created with Highcharts. All points have an associated tooltip with some text, to which I would like to add a link that would open the URL in the browser (that is, outside the app).

Very basically, the app code looks like this:

import ChartView from 'react-native-highcharts';


render() {
        let Highcharts = "Highcharts";
        let config ={
            chart: {
                type: "line",
                animation: Highcharts.svg,
            ...
            tooltip: {
                followTouchMove: false,
                useHTML: true,
                formatter: function () {
                    return `<div class="text">bla bla bla
                                <a href="http://www.google.cat">my link here/a>
                            </div>`;
                }
            },
        };

That gets rendered in:

    return(
    <View style={styles.container}>
        <ChartView
            config={config}
        />
    </View>

Checkin Link inside of a Highcharts tooltip I saw interesting ideas like adding this info inside the charts key:

    events: {
        click: function (event) {
            var url = 'http://www.google.cat';
            window.open(url, '_blank');
        }
    }

Which works, but it opens the link inside the ChartView of React Native. That is, the space with the graph shows the given URL. However, I want the URL to open in the browser.

Is there a way to open the links in the browser? React Native's Linking.openURL(url); is the way to do so, but I cannot see how can I bing Linking.openURL from within the config settings.


回答1:


I solved it by using onMessage from the CharView:

return(
<View style={styles.container}>
    <ChartView
        onMessage={m => this.onMessage(m)}
        config={config}
    />
</View>

This triggers this method to open the URL:

onMessage = (m) => {
    let data = JSON.parse(m.nativeEvent.data);
    Linking.openURL(data.url)
};

And the URL gets populated through a global variable window.myURL and sending the message with postMessage():

render() {
    let Highcharts = "Highcharts";
    let config ={
        ...
        plotOptions: {
            series: {
                stickyTracking: false,
                point: {
                    events: {
                        click: function(e) {
                            window.postMessage(JSON.stringify({'url': window.myUrl}));
                        }
                    }
                }
            },
        },
        tooltip: {
            useHTML: true,
            formatter: function () {
                window.myUrl = extras.url;
                return `<div class="text">bla bla bla
                            <a href="http://www.google.cat">my link here/a>
                        </div>`;
            }
    };

It works well on iOS, but not in Android.



来源:https://stackoverflow.com/questions/55358659/how-can-i-add-links-in-a-highcharts-tooltip-that-will-open-on-mobiles-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!