简体   繁体   English

高图:更改选项后重绘图表

[英]Highcharts: redraw chart after changing options

Is it possible to redraw chart after changing options only ? 仅更改选项后可以重画图表吗?

 let options = {
            chart        : {
                renderTo: 'container',
                type    : 'line'
            },
            rangeSelector: {
                selected: 1
            },

            yAxis: {
                title: {
                    text: 'Number of downloads'
                }
            },

            xAxis: {
                type: 'datetime'
            },

            title: {
                text: 'Title'
            },


            series: undefined,

            plotOptions: {
                series: {
                    compare        : undefined,
                    showInNavigator: true
                }
            },


        };

You may notice that I control series in plotOptions as there will be multiples series on same chart. 您可能会注意到,我在plotOptions中控制系列,因为同一张图表上会有多个系列。

// Data needs some preparation so I inject it separately, here:
options.series = seriesOptions( preparedData )

// This correctly plots the chart
let chart = new Highcharts.stockChart( options )

Then I use jquery to change some options values: 然后,我使用jquery更改一些选项值:

 $( '#setWeekly' ).on( 'click', function( e ) {
            options.plotOptions.series.dataGrouping = {
                forced: true,
                units : [
                    [ 'week', [ 1 ] ]
                ]
            }
            options.title.text = "New title"
            chart.isDirty = true
            chart.redraw()
        } );

This does nothing. 这什么也没做。 When I replace the chart redraw by a 'new Highcharts.stockChart( options )' it will work and correctly recreate the chart with proper options. 当我用“新的Highcharts.stockChart(options)”替换图表重绘时,它将起作用并使用适当的选项正确地重新创建图表。 But I don't think this is the proper way in particular because all the options are reset, which is annoying in particular when data series were subsequently added to the chart. 但是我认为这不是正确的方法,特别是因为所有选项都已重置,当随后将数据系列添加到图表中时,这尤其令人讨厌。

So how to properly trigger redraw() so it will update the chart with the new options values ? 那么,如何正确触发redraw()以便它将使用新的选项值更新图表?

So it seems there are 2 ways of updating what you see in screen: 因此,似乎有两种方法可以更新屏幕上显示的内容:

simple chart update which update the chart information such as: 简单的图表更新,更新图表信息,例如:

  chart.update(
                {
                    title: {
                        text: 'new text'
                    }
                }
            )

and the series update, which update the data itself, but keep the data you already have so you are not forced to recreate a chart instance: 以及系列更新,它们会更新数据本身,但会保留您已有的数据,因此您不必被迫重新创建图表实例:

 let seriesMonth = {
                dataGrouping: {
                    forced: true,
                    units : [ [ 'month', [ 1 ] ] ]
                }
            }

 chart.series[ 0 ].update( seriesMonth );

By using either one or both, one should be able to get any desired result. 通过使用其中之一或两者,应该能够获得任何期望的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM