【问题标题】:Different xAxis label, max and colors for drilldown in HighchartsHighcharts 中用于向下钻取的不同 xAxis 标签、最大值和颜色
【发布时间】:2019-08-01 11:31:50
【问题描述】:

我有一个设置,我正在使用不同的数据集生成多个 Highcharts。我现在开始实施向下钻取,它需要为 xAxis 提供不同的标签,需要更改最大值,并且我想为每个条设置颜色。

  1. 关于 xAxis,我使用的对象获取向下钻取的数据对我来说似乎没问题,但不是使用名称,而是显示循环的 i

    (5) […] ​ 0:{…} ​​数据:Array(5) [ 3, 8, 3, ... ] ​​id:“dd1” ​​​名称:“糟糕!” ​​类型:“列”

  2. 对于最大值,基本图表应该是 5,但向下钻取时没有设置,我试过了

    向下钻取:{ y轴:{ 最大:100 } }

但它什么也没做。

  1. TBH 我还没有尝试过颜色,预期的结果将是根据一周的质量对条进行着色,例如糟糕的一周 = 红色到极好 = 绿色

我还尝试为一般钻取设置图表类型(现在我正在为每个数据对象设置'type':'column',这对我来说似乎是多余的,但我不知道我是否可以使用某些东西像 ...drilldown.chart: { 'type':'column'} 来定义向下钻取的一般设置,但这也不会显示任何结果。

我尝试使用绑定到 charts.events.drilldown 或 plotoptions.series.points.events.click 的函数来实现几个设置示例,但我对 Highcharts 和 JavaScript 的了解非常有限,无法成功。

代码:

// Fallback for browsers that cannot handle Object.values (i.e. that do not support ES6)
if (!Object.values) {
    Object.values = function(source) {
        var result = Object.keys(source).map(function(x) {
            return source[x];
        });
        return result;
    };
}

// Setting general HighCharts options
var options = {
    chart: {
        type: 'areaspline'
    },
    credits: {
        enabled: false
    },
    legend: {
        enabled: false
    },
    xAxis: {
        type: 'category'
    },
    yAxis: {
        title: {
            enabled: false
        },
        max: 5
    },
    series: [{
        fillColor: {
            linearGradient: {
                x1: 0,
                y1: 0,
                x2: 0,
                y2: 1
            },
            stops: [
                [0, '#F0A830'],
                [1, '#f4f4f4']
            ]
        },
        marker: {
            fillColor: 'white',
            lineWidth: 2,
            lineColor: '#F0A830',
            radius: 5
        },
        lineColor: '#F0A830',
        lineWidth: 5,
    }],
    plotOptions: {
        series: {
            marker: {
                enabled: false,
                symbol: 'circle'
            }
        }
    },
    drilldown: {
        yAxis: {
            max: 100
        },
        chart: {
            'type': 'column'
        },
        drillUpButton: {
            theme: {
                fill: 'white',
                'stroke-width': 1,
                stroke: 'silver',
                r: 0,
                states: {
                    hover: {
                        fill: '#a4edba'
                    },
                    select: {
                        stroke: '#039',
                        fill: '#a4edba'
                    }
                }
            }
        }
    }
};

// Temporary data delivery
var circlesData = {
    "0": {
        "title": "MyTeam",
        "values": [4, 3, 2, 3, 4],
        "weeks": [1, 2, 3, 4, 5],
        "weeksTotal": [6, 7, 8, 9, 10],
        "valuesDetail": {
            "valuesDetailLabel": ["Bad!", "Hmm", "Itwasokay", "Prettygood", "Fantastic"],
            "valuesDetailData": {
                0: [3, 4, 4, 1, 15],
                1: [2, 12, 5, 3, 1],
                2: [18, 2, 2, 2, 2],
                3: [3, 2, 4, 1, 5],
                4: [1, 2, 1, 1, 15]
            }
        }
    },
    "1": {
        "title": "YourTeam",
        "values": [1, 4, 5, 2, 3],
        "weeks": [1, 2, 3, 4, 5],
        "weeksTotal": [6, 7, 8, 9, 10],
        "valuesDetail": {
            "valuesDetailLabel": ["Bad!", "Hmm", "Itwasokay", "Prettygood", "Fantastic"],
            "valuesDetailData": {
                0: [3, 8, 3, 1, 4],
                1: [3, 12, 4, 3, 1],
                2: [4, 2, 2, 2, 2],
                3: [3, 2, 4, 5, 8],
                4: [1, 2, 1, 1, 15]
            }
        }
    }
}

console.log(circlesData);

var circlesDataString = JSON.stringify(circlesData); //this just turns the object array 'info' into a string

var obj = JSON.parse(circlesDataString);
console.log('Loading initiated...');

// Loop for creating individual HighCharts
Object.keys(obj).forEach(function(item) {
    // Create container div
    $('#outerContainer').append('<div class="innerContainer" id="circles' + item + '"></div>');

    // Get data of last iteration and determin quality of last week for color coding/wording
    var latestScore = circlesData[item].values.slice(-1)[0];
    console.log('latestScore: ' + latestScore)

    var chartColor = '';
    var weekText = '';
    var className = '';

    if (latestScore < 2.5) {
        chartColor = '#FD6E72';
        chartColorLight = '#fc8385';
        weekText = 'Bad week';
    } else if (latestScore >= 2.5 && latestScore < 3.5) {
        chartColor = '#FFCC73';
        chartColorLight = '#FBD486';
        weekText = 'Ok week';
    } else {
        chartColor = '#2CCC76';
        chartColorLight = '#82DB9D';
        weekText = 'Good week';
    }
    // create array for first chart view
    var chartData = [];
    var len = circlesData[item].values.length;
    for (i = 0; i < len; i++) {
        chartData.push({
            'name': 'w' + circlesData[item].weeks[i],
            'y': circlesData[item].values[i],
            'drilldown': 'dd' + circlesData[item].values[i]
        });
    };
    // set array for drilldown items
    var drillDown = [];
    for (i = 0; i < len; i++) {
        drillDown.push({
            'type': 'column',
            'id': 'dd' + circlesData[item].values[i],
            'data': circlesData[item].valuesDetail.valuesDetailData[i],
            'name': circlesData[item].valuesDetail.valuesDetailLabel[i]
        });
    };
    console.log('This is drillDown');
    console.log(drillDown);
    // Setting individual Highcharts options per Circle
    options.series[0] = {
        name: circlesData[item].title,
        data: chartData,
        color: chartColor,
        fillColor: {
            linearGradient: {
                x1: 0,
                y1: 0,
                x2: 0,
                y2: 1
            },
            stops: [
                [0, chartColor],
                [1, '#f4f4f4']
            ]
        },
    };
    // Set drilldown options
    options.drilldown.series = drillDown;
    options.title = {
        text: circlesData[item].title
    };
    options.subtitle = {
        text: weekText,
        style: {
            color: chartColor,
        }
    };
    console.log(options);

    // Call Highcharts
    $('#circles' + item).highcharts(options);
    console.log('Circle' + item + ' loaded...');
});

https://codepen.io/anon/pen/eqRPGV

【问题讨论】:

    标签: javascript highcharts


    【解决方案1】:
    1. xAxis 标签:对于具有不同名称的标签,每个数据点都应该有一个名称,例如:0: [["Bad!",3], ["Hmm",8], ["Itwasokay",3], ["Prettygood",1], ["Fantastic",4]]

    2. 最大值: Drilldown.yAxis 不是 API,因此它不起作用。相反,应该更新全局最大值。在这种情况下,我们可以有条件地将其设置为 null,如:if(options.drilldown) options.yAxis.max = null;

    3. 要使每一列具有不同的颜色,您需要用所需的颜色数组 options.colors 覆盖全局颜色数组,并为向下钻取下的每个系列对象定义属性:'colorByPoint': true对象。

    4. 对于图表类型,drilldown.chart: { 'type':'column'} 不起作用,因为没有用于向下钻取的图表 API。虽然看起来是多余的,但每个 Drilldown.series 对象都有自己的图表类型。在本例中为“列”。

    此外,每个 Drilldown.series 对象中的 id 应该是唯一的。在你的代码中,而不是这样做,'id': 'dd' + circlesData[item].values[i],你可以使用周数组来做到这一点,比如:'id': 'dd' + circlesData[item].weeks[i]。因为 circlesData["0"].values 有重复数据。

    以下是更新后的代码。可以参考jsfiddle。

        // Fallback for browsers that cannot handle Object.values (i.e. that do not support ES6)
    if (!Object.values) {
        Object.values = function(source) {
            var result = Object.keys(source).map(function(x) {
                return source[x];
            });
            return result;
        };
    }
    
    // Setting general HighCharts options
    var options = {
        chart: {
            type: 'areaspline'
        },
      colors: ['#ED561B', '#DDDF00', '#24CBE5', '#058DC7', '#50B432'],
        credits: {
            enabled: false
        },
        legend: {
            enabled: false
        },
        xAxis: {
            type: 'category'
        },
        yAxis: {
            title: {
                enabled: false
            },
            max: 5
        },
        series: [{
            fillColor: {
                linearGradient: {
                    x1: 0,
                    y1: 0,
                    x2: 0,
                    y2: 1
                },
                stops: [
                    [0, '#F0A830'],
                    [1, '#f4f4f4']
                ]
            },
            marker: {
                fillColor: 'white',
                lineWidth: 2,
                lineColor: '#F0A830',
                radius: 5
            },
            lineColor: '#F0A830',
            lineWidth: 5,
        }],
        plotOptions: {
            series: {
                marker: {
                    enabled: false,
                    symbol: 'circle'
                }
            }
        },
        drilldown: {
            drillUpButton: {
                theme: {
                    fill: 'white',
                    'stroke-width': 1,
                    stroke: 'silver',
                    r: 0,
                    states: {
                        hover: {
                            fill: '#a4edba'
                        },
                        select: {
                            stroke: '#039',
                            fill: '#a4edba'
                        }
                    }
                }
            }
        }
    };
    
    // Temporary data delivery
    var circlesData = {
        "0": {
            "title": "MyTeam",
            "values": [4, 3, 2, 3, 4],
            "weeks": [1, 2, 3, 4, 5],
            "weeksTotal": [6, 7, 8, 9, 10],
            "valuesDetail": {
                "valuesDetailLabel": ["Bad!", "Hmm", "Itwasokay", "Prettygood", "Fantastic"],
                "valuesDetailData": {
                    0: [["Bad!",3], ["Hmm",4], ["Itwasokay",4], ["Prettygood",1], ["Fantastic",15]],
                    1: [["Bad!",2], ["Hmm",12], ["Itwasokay",5], ["Prettygood",3], ["Fantastic",1]],
                    2: [["Bad!",18], ["Hmm",2], ["Itwasokay",2], ["Prettygood",2], ["Fantastic",2]],
                    3: [["Bad!",3], ["Hmm",2], ["Itwasokay",4], ["Prettygood",1], ["Fantastic",5]],
                    4: [["Bad!",1], ["Hmm",2], ["Itwasokay",1], ["Prettygood",1], ["Fantastic",15]]
                }
            }
        },
        "1": {
            "title": "YourTeam",
            "values": [1, 4, 5, 2, 3],
            "weeks": [1, 2, 3, 4, 5],
            "weeksTotal": [6, 7, 8, 9, 10],
            "valuesDetail": {
                "valuesDetailLabel": ["Bad!", "Hmm", "Itwasokay", "Prettygood", "Fantastic"],
                "valuesDetailData": {
                    0: [["Bad!",3], ["Hmm",8], ["Itwasokay",3], ["Prettygood",1], ["Fantastic",4]],
                    1: [["Bad!",3], ["Hmm",12], ["Itwasokay",4], ["Prettygood",3], ["Fantastic",1]],
                    2: [["Bad!",4], ["Hmm",2], ["Itwasokay",2], ["Prettygood",2], ["Fantastic",2]],
                    3: [["Bad!",3], ["Hmm",2], ["Itwasokay",4], ["Prettygood",5], ["Fantastic",8]],
                    4: [["Bad!",1], ["Hmm",2], ["Itwasokay",1], ["Prettygood",1], ["Fantastic",15]]
                }
            }
        }
    }
    
    console.log(circlesData);
    
    var circlesDataString = JSON.stringify(circlesData); //this just turns the object array 'info' into a string
    
    var obj = JSON.parse(circlesDataString);
    console.log('Loading initiated...');
    
    // Loop for creating individual HighCharts
    Object.keys(obj).forEach(function(item) {
        // Create container div
        $('#outerContainer').append('<div class="innerContainer" id="circles' + item + '"></div>');
    
        // Get data of last iteration and determin quality of last week for color coding/wording
        var latestScore = circlesData[item].values.slice(-1)[0];
        console.log('latestScore: ' + latestScore)
    
        var chartColor = '';
        var weekText = '';
        var className = '';
    
        if (latestScore < 2.5) {
            chartColor = '#FD6E72';
            chartColorLight = '#fc8385';
            weekText = 'Bad week';
        } else if (latestScore >= 2.5 && latestScore < 3.5) {
            chartColor = '#FFCC73';
            chartColorLight = '#FBD486';
            weekText = 'Ok week';
        } else {
            chartColor = '#2CCC76';
            chartColorLight = '#82DB9D';
            weekText = 'Good week';
        }
        // create array for first chart view
        var chartData = [];
        var len = circlesData[item].values.length;
        for (i = 0; i < len; i++) {
            chartData.push({
                'name': 'w' + circlesData[item].weeks[i],
                'y': circlesData[item].values[i],
                'drilldown': 'dd' + circlesData[item].weeks[i]
            });
        };
        // set array for drilldown items
        var drillDown = [];
        for (i = 0; i < len; i++) {
            drillDown.push({
                'type': 'column',
                'id': 'dd' + circlesData[item].weeks[i],
                'data': circlesData[item].valuesDetail.valuesDetailData[i],
              'name':'w' + circlesData[item].weeks[i],
              'colorByPoint': true,
            });
        };
        console.log('This is drillDown');
        console.log(drillDown);
        // Setting individual Highcharts options per Circle
        options.series[0] = {
            name: circlesData[item].title,
            data: chartData,
            color: chartColor,
            fillColor: {
                linearGradient: {
                    x1: 0,
                    y1: 0,
                    x2: 0,
                    y2: 1
                },
                stops: [
                    [0, chartColor],
                    [1, '#f4f4f4']
                ]
            },
        };
        // Set drilldown options
        options.drilldown.series = drillDown;
        options.title = {
            text: circlesData[item].title
        };
        options.subtitle = {
            text: weekText,
            style: {
                color: chartColor,
            }
        };
    
      //do this conditionally
      if(options.drilldown) options.yAxis.max = null;
    
        console.log('option', options);
    
        // Call Highcharts
        $('#circles' + item).highcharts(options);
        console.log('Circle' + item + ' loaded...');
    });
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2013-05-06
      • 2012-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多