重建Highchart不起作用

时间:2018-06-20 10:57:30

标签: javascript highcharts highstock

我正试图破坏并重建这样的Highcharts.stockChart:

图表选项:

$(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: []
};    

使用每月数据进行初始创建:

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);
        }
    });
}

单击Year的range按钮后,我期望使用一个全新的构建图表,但应将应更改的新Series和xAxis值添加到现有图表中。

奇怪的结果:

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

1 个答案:

答案 0 :(得分:0)

我通过使用按钮和系列的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

现在,我想更改每列的颜色,其中包括xAxis值大于或等于200。(示例中的第2 + 4列)。 有什么办法吗?