当用户滚动到左端时将数据添加到 highcharts(Highstock)

发布于 2024-12-11 01:33:03 字数 232 浏览 0 评论 0原文

我在我的网站上使用 highstock。股票图表导航器中的滚动条是使用 SVG 绘制的。当用户滚动到最左端时,我想向图表添加更多数据(通过 ajax)。

我是 SVG 新手,不知道如何检测用户已滚动到末尾并基于此触发 ajax 查询。谁能帮我解决这个问题。

谢谢, 西瓦库玛尔。

I'm using highstock on my website. The scroll bar in the navigator of stock chart is drawn using SVG. I want to add more data(via ajax) to the graph when user scrolls to the leftmost end.

I am new to SVG and not sure how to detect that user has scrolled to the end and fire a ajax query based on that. Can anyone help me with this.

Thanks,
Sivakumar.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南风几经秋 2024-12-18 01:33:03

所以,今天我遇到了同样的问题,我刚刚发现了你的问题。

我不知道您是否有任何理由仅在用户移动滚动条时加载新数据,我建议如果用户可视化最左侧的数据,则触发 ajax 查询(即:滚动滚动条,按向左箭头、拖动导航图区域等)。

如果此解决方案适用于您,您可以尝试使用以下方法:

chart = new Highcharts.StockChart({
        chart: {
            renderTo: 'chart',
            events: {
                redraw: function(event) {
                    if (chart.xAxis) {
                        var extremes = chart.xAxis[0].getExtremes();
                        if (extremes && extremes.min == extremes.dataMin) {
                            console.log("time to load more data!");
                        }
                    }
                }
            }
        }, [...]

So, today I had the same problem, and I just found your question.

I don't know if you have any reason for loading the new data only when the user moves the scrollbar, I would recommend to fire the ajax query if the user visualizes the left-most data, instead (that is: scrolling the bar, pressing the left arrow, dragging the area of the navigation chart, and so on).

If this solution applies to you, you can try with something like this:

chart = new Highcharts.StockChart({
        chart: {
            renderTo: 'chart',
            events: {
                redraw: function(event) {
                    if (chart.xAxis) {
                        var extremes = chart.xAxis[0].getExtremes();
                        if (extremes && extremes.min == extremes.dataMin) {
                            console.log("time to load more data!");
                        }
                    }
                }
            }
        }, [...]
回首观望 2024-12-18 01:33:03

我通过以下方式解决了这个问题。

<script>

import axios from 'axios';

export default {
    name: 'Chart',
    data() {
        return {
            chartOptions: {
                chart: {
                    events: {
                        // we will lose the context of the component if we define a function here
                    }
                },
                series: [{
                    type: 'candlestick',
                    data: null,
                }],
            },
        };
    },
    methods: {
        onRedraw: function () {
            let chart = this.$refs.chart.chart
            if (chart.xAxis) {
                let extremes = chart.xAxis[0].getExtremes()
                console.log(extremes)
                if (extremes && extremes.min <= extremes.dataMin) {
                    console.log("time to load more data!");
                }
            }
        }

    },
    created() {
        axios.get('https://demo-live-data.highcharts.com/aapl-ohlcv.json').then((response) => {
            this.chartOptions.series[0].data = response.data
            this.chartOptions.series[0].name = 'AAPL'
        });
        
        this.chartOptions.chart.events.redraw = this.onRedraw
    },
};
</script>

<template>
  <highcharts :constructor-type="'stockChart'" :options="chartOptions" ref="chart"></highcharts>
</template>

I solved this problem in the following way.

<script>

import axios from 'axios';

export default {
    name: 'Chart',
    data() {
        return {
            chartOptions: {
                chart: {
                    events: {
                        // we will lose the context of the component if we define a function here
                    }
                },
                series: [{
                    type: 'candlestick',
                    data: null,
                }],
            },
        };
    },
    methods: {
        onRedraw: function () {
            let chart = this.$refs.chart.chart
            if (chart.xAxis) {
                let extremes = chart.xAxis[0].getExtremes()
                console.log(extremes)
                if (extremes && extremes.min <= extremes.dataMin) {
                    console.log("time to load more data!");
                }
            }
        }

    },
    created() {
        axios.get('https://demo-live-data.highcharts.com/aapl-ohlcv.json').then((response) => {
            this.chartOptions.series[0].data = response.data
            this.chartOptions.series[0].name = 'AAPL'
        });
        
        this.chartOptions.chart.events.redraw = this.onRedraw
    },
};
</script>

<template>
  <highcharts :constructor-type="'stockChart'" :options="chartOptions" ref="chart"></highcharts>
</template>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文