简体   繁体   English

重建Highchart不起作用

[英]Rebuild Highchart doesn't work

I'm trying to destroy and rebuild a Highcharts.stockChart like this: 我正在尝试破坏和重建这样的Highcharts.stockChart:

Chart options: 图表选项:

$(document).ready(function () {
var label = [];
var seriesData = [];

columnChartOptions =  {
    exporting: {
        enabled: false
    },
    navigator: {
        enabled: false
    },
    scrollbar: {
        enabled: false
    },
    chart: {
        type: 'column'
    },
    credits: {
        href: " ",
        text: " "
    },
    title: {
        text: ''
    },
    yAxis: {
        min: 0,
        title: {
            text: 'kWh'
        }
    },
    legend: {
        enabled: false
    },
    tooltip: {
        shared: true,
        valueDecimals: 2
    },
    plotOptions: {
        column: {
            grouping: false,
            shadow: false,
            borderWidth: 0,
            groupPadding: 0,
            color: '#688DC4'
        }

    },
    rangeSelector: {
        allButtonsEnabled: true,
        verticalAlign: 'top',
        x: 0,
        y: 0,
        buttonPosition: {
            align: 'left',
            x: 0,
            y: 0
        },
        buttonTheme: {
            width: 50
        },
        selected: 2,
        buttonSpacing: 5,
        buttons: [{
            type: 'week',
            count: 1,
            text: 'Wocheeee',
            events: {
                click: function () {

                }
            }

        }, {
            type: 'month',
            count: 1,
            text: 'Monat',
            events: {
                click: function () {
                    myChart.destroy();
                    myChart = new Highcharts.StockChart('consumptionGraph', columnChartOptions);
                    myChart.showLoading("Wird geladen...");
                    setWeeklyData();
                }
            }
        }, {
            type: 'year',
            count: 1,
            text: 'Jahr',
            events: {
                click: function () {
                    myChart.destroy();
                    myChart = new Highcharts.StockChart('consumptionGraph', columnChartOptions);
                    myChart.showLoading("Wird geladen...");
                    setMonthlyData();
                    myChart.redraw();
                }
            }
        }]
    },
    series: []
};    

initial create with monthly data: 使用月度数据进行初始创建:

myChart = new Highcharts.StockChart('consumptionGraph', columnChartOptions);
myChart.showLoading("Wird geladen...");
setMonthlyData();

function setMonthlyData() {
    $.ajax({
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        url: '/Home/GetMonthlyConsumption',
        type: 'POST',
        cache: false,
        success: function (data) {
            var MonthlyConsumption = JSON.parse(data);

            for (var i = 0; i < MonthlyConsumption.length; i++) {
                seriesData.push(MonthlyConsumption[i].ActualValue);
                label.push(MonthlyConsumption[i].NameOfDate);
            }

            myChart.xAxis[0].update({ categories: label }, true);

            while (myChart.series.length > 0) {
                myChart.series[0].remove(true);
            }
            console.log(myChart.series.length);

            myChart.addSeries({
                name: 'Verbrauch (kWh)',
                color: '#688DC4',
                data: seriesData,
                animation: {
                    duration: 1000,
                    easing: 'easeOutBounce',
                },
            }, true);
            myChart.hideLoading();
        },
        error: function (error) {

            console.log(error);
            //monthlyColumnChart.showLoading(error.statusText);
        }
    });
}

After clicking the rangebutton for Year, I'm expecting a fully new build Chart but instead the new Series and xAxis value which should be changed is added to the existing chart. 单击Year的range按钮后,我期望使用一个全新的构建图表,但应将应更改的新Series和xAxis值添加到现有图表中。

Strange result: 结果奇怪:

added instead of building new 添加而不是新建 添加而不是新建

I solved the problem above by using the dataGrouping option of buttons and series like you can see here: 我通过使用按钮和系列的dataGrouping选项解决了上述问题,如下所示:

var seriesData = [];
Highcharts.setOptions({
    lang: {
        rangeSelectorFrom: "Von",
        rangeSelectorTo: "Bis",
        months: ["Jannuar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"],
        weekdays: ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'],
        shortMonths: ["Jan", "Feb", "Mrz", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"],
        rangeSelectorZoom: '',
        decimalPoint: "."
    }
});

columnChartOptions =  {
    exporting: {
        enabled: false
    },
    navigator: {
        enabled: false
    },
    scrollbar: {
        enabled: false
    },
    chart: {
        type: 'column'
    },
    credits: {
        href: " ",
        text: " "
    },
    title: {
        text: ''
    },
    yAxis: {
        min: 0,
        title: {
            text: 'kWh'
        },
        lineWidth: 1,
        opposite: false
    },
    xAxis: {
        type: 'datetime',
        dateTimeLabelFormats: {
            month: '%B',
            week: '%e. %b',
            day: '%e. %b',
            hour: '%H'
        }
    },
    legend: {
        enabled: false
    },
    tooltip: {
        shared: true,
        valueDecimals: 2
    },
    plotOptions: {
        series: {
            marker: {
                enabled: true
            }
        },
        column: {
            grouping: false,
            shadow: false,
            borderWidth: 0,
            groupPadding: 0,
            color: '#688DC4'
        }

    },
    rangeSelector: {
        inputDateFormat: '%d.%m.%Y',
        inputEditDateFormat: '%d.%m.%Y',
        inputEnabled: true,
        inputDateParser: function (value) {
            value = value.split(/[\.]/);
            return Date.UTC(
                value[2],
                value[1]-1,
                value[0]
            );
        },
        verticalAlign: 'top',
        x: 0,
        y: 0,
        buttonPosition: {
            align: 'left',
            x: 0,
            y: 0
        },
        buttonTheme: {
            width: 50
        },
        allButtonsEnabled: true,
        selected: 3,
        buttonSpacing: 5,
        buttons: [{
            type: 'day',
            count: 1,
            text: 'Tag',
            dataGrouping: {
                approximation: "sum",
                enabled: true,
                forced: true,
                units: [['hour', [1]]]
            }
        }, {
            type: 'week',
            count: 1,
            text: 'Woche',
            dataGrouping: {
                approximation: "sum",
                enabled: true,
                forced: true,
                units: [['day', [1]]]
            }
        }, {
            type: 'month',
            count: 1,
            text: 'Monat',
            dataGrouping: {
                approximation: "sum",
                enabled: true,
                forced: true,
                units: [['week', [1]]]
            }
        }, {
            type: 'year',
            count: 1,
            text: 'Jahr',
            dataGrouping: {
                approximation: "sum",
                enabled: true,
                forced: true,
                units: [['month', [1]]]
            }
        }]
    },
    series: []
};

myChart = new Highcharts.StockChart('consumptionGraph', columnChartOptions);




myChart.showLoading("Wird geladen...");
setChartData();

function setChartData() {

            var consumerSeries = [
                [Date.UTC(2018, 1, 1), 150], [Date.UTC(2018, 1, 11), 180], [Date.UTC(2018, 1, 12), 199],
                [Date.UTC(2018, 7, 1), 150], [Date.UTC(2018, 7, 2), 130], [Date.UTC(2018, 7, 5), 280],
                [Date.UTC(2018, 9, 1), 150], [Date.UTC(2018, 9, 2), 130], [Date.UTC(2018, 9, 5), 190],
                [Date.UTC(2018, 12, 1), 150], [Date.UTC(2018, 12, 2), 130], [Date.UTC(2018, 12, 5), 250], 
            ];

            consumerSeriesObject = {
                name: "Consumption",
                data: consumerSeries,
                showInNavigator: true,
                color: '#688DC4',
                dataGrouping: {
                    approximation: "sum",
                    enabled: true,
                    forced: true,
                    units: [['month', [1]]]
                }
            };

            myChart.addSeries(consumerSeriesObject, true);
            myChart.hideLoading();

}` 

JS Fiddle JS小提琴

Now I want to change the color of every column which includes a xAxis value over and equal to 200. (column 2+4 of my Example). 现在,我想更改每列的颜色,其中包括xAxis值大于等于200(我的示例的第2 + 4列)。 Is there any way to do this? 有什么办法吗?

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

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