jquery highcharts y 轴上的文本

发布于 2025-01-07 00:36:05 字数 1165 浏览 0 评论 0原文

您好,我正在尝试创建一个带有高图表的简单柱形图。

该图表显示了学生四项作业的成绩。

四个分配中的每一个都在 x 轴上列出。

y 轴应连续显示每个可能的等级 A - E(即每个字母必须出现在轴上,即使系列中没有该等级的数据)。

我尝试使用数字来表示系列数据中的成绩,然后使用 Highcharts 中的建议y 轴的文本标签 手动填充 y 轴,但仅显示第一年级。

另外,当我在标签格式化程序函数中 alert(this.value) 时,我得到了一系列返回的零星数字(0,500,0,5,10),其中只有几个与我的系列数据中的数字。请帮忙!

这是我的代码;

$(document).ready(function(){

    var seriesData = [{
                    name: 'Grade',
                    data: [5,2,1,4]
                    }];

    var xAxisLabels = {categories: ['Assignment One', 'Assignment Two', 'Assignment Three', 'Assignment Four']};

    var yAxisLabels = ['E','D','C','B','A'];

    var chart = new Highcharts.Chart({

        chart: {
            renderTo: 'chart',
            defaultSeriesType: 'column'
        },
        series: seriesData,
        xAxis: xAxisLabels,
        yAxis: {        
            labels: {
                formatter: function() {
                    return yAxisLabels[this.value];
                }
            }
        }

    });


});

Hi I am trying to create a simple column chart with high charts.

The chart displays a students grades for four assignments.

Each of the four assignments is listed across the x-axis.

The y Axis should display each of the possible grades A - E consecutively (i.e. each letter MUST appear on the axis, even if there is no data in the series for that grade).

I have tried using numbers to signify the grades in my series data and then using the suggestion at Highcharts text labels for y-axis to manually populate the y-axis, but only the first grade shows.

Plus, when I alert(this.value) in the label formatter function, I get a sporadic series of numbers returned(0,500,0,5,10), of which only a couple bare any relation to the numbers in my series data. Help Please!

Here is my code;

$(document).ready(function(){

    var seriesData = [{
                    name: 'Grade',
                    data: [5,2,1,4]
                    }];

    var xAxisLabels = {categories: ['Assignment One', 'Assignment Two', 'Assignment Three', 'Assignment Four']};

    var yAxisLabels = ['E','D','C','B','A'];

    var chart = new Highcharts.Chart({

        chart: {
            renderTo: 'chart',
            defaultSeriesType: 'column'
        },
        series: seriesData,
        xAxis: xAxisLabels,
        yAxis: {        
            labels: {
                formatter: function() {
                    return yAxisLabels[this.value];
                }
            }
        }

    });


});

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

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

发布评论

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

评论(1

故人的歌 2025-01-14 00:36:05

由于 javascript 数组索引从 0 开始,因此您必须添加一个空标签,因为您的值范围从 (5:A 到 1:E)。

我添加了一个工具提示格式化程序来更改工具提示的标签以显示成绩而不是数值。

$(document).ready(function(){
    var seriesData = [{
                    name: 'Assignments',
                    data: [5,2,1,4]
                    }];

    var xAxisLabels = {categories: ['Assignment One', 'Assignment Two', 'Assignment Three', 'Assignment Four']};

    var yAxisLabels = ['','E','D','C','B','A'];

    var chart = new Highcharts.Chart({

        chart: {
            renderTo: 'chart',
            defaultSeriesType: 'column'
        },
        title: { text: 'Achievements of John Doe' },
        series: seriesData,
        xAxis: xAxisLabels,
        yAxis: {
            endOnTick: true,
            min: 0,
            max: 5,
            title: { text: 'Grade' },
            labels: {
                formatter: function() {
                    return yAxisLabels[this.value];
                }
            }
        },
        tooltip: {
            formatter: function() {
                return this.x+': '+yAxisLabels[this.y];
            }
        }
    });
});

高图输出

Since javascript array indices start at 0 you have to add an empty label since your values range from (5:A to 1:E).

I added a tooltip formatter to change the label of the tooltip to show the grades as well and not the numeric values.

$(document).ready(function(){
    var seriesData = [{
                    name: 'Assignments',
                    data: [5,2,1,4]
                    }];

    var xAxisLabels = {categories: ['Assignment One', 'Assignment Two', 'Assignment Three', 'Assignment Four']};

    var yAxisLabels = ['','E','D','C','B','A'];

    var chart = new Highcharts.Chart({

        chart: {
            renderTo: 'chart',
            defaultSeriesType: 'column'
        },
        title: { text: 'Achievements of John Doe' },
        series: seriesData,
        xAxis: xAxisLabels,
        yAxis: {
            endOnTick: true,
            min: 0,
            max: 5,
            title: { text: 'Grade' },
            labels: {
                formatter: function() {
                    return yAxisLabels[this.value];
                }
            }
        },
        tooltip: {
            formatter: function() {
                return this.x+': '+yAxisLabels[this.y];
            }
        }
    });
});

Highchart output

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文