javascript - Highcharts 中的反向轴从表中获取数据

标签 javascript html charts highcharts html-table

我正在尝试在表格中的 highcharts 图表上设置 x 轴,以便它是月份。 y 轴就是单位。我真的不知道如何改变这个。我尝试过更改此部分:

options.xAxis.categories = [];
        $('thead th', table).each( function(i) {
            options.xAxis.categories.push(this.innerHTML);
        });

但运气不好。所以我将其保留为默认值,希望有人能够提供帮助。

您可以在 this JSFiddle 查看我正在尝试执行的操作.

最佳答案

从您的代码看来,您的月份位于 thead 而不是 tbody 中。所以你应该真正使用 $('thead th') 选择器。我还建议使用 :parent selector过滤掉空的ths

$('thead th:parent', table).each( function(i) {
    var month=$(this).text();
    options.xAxis.categories.push(month);
});

此外,如果您希望 x 轴为月份,则需要更改您的系列数据,您现在应该只有 3 个系列,而不是之前的 6 个。每个系列现在有 6 个点(每个月一个) ) 而不是之前的 3 点/系列。因此,您的表解析需要更改为类似这样的内容。

options.series = [];
$('tbody tr', table).each(function (i) {
    var tr = this;
    var serie = {};
    serie.name = $('th', tr).text();
    serie.data = [];
    $('td', tr).each(function (j) {
        serie.data.push(parseFloat(this.innerHTML));
    });
    options.series.push(serie);
});

<强> Demo @ jsFiddle

关于javascript - Highcharts 中的反向轴从表中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14492954/

相关文章:

javascript - 有什么方法可以在调用构造函数之前重新设置 Function 对象的 "this"引用吗?

javascript - 带有 i18next 的 gettext 样式键和一般工作流

javascript - 谷歌图表 : How to get the percentage values of every slice in a pie chart?

Javascript ChartJS 永久更新图表和 Canvas

javascript - TypeScript/JavaScript 中的 array.indexOf

javascript - 如何理解 "{ fun: function.bind(this) }"?

javascript - 每次 iframe 加载页面时显示加载 gif

html - 移动浏览器上的 CSS

python - 在 gmail 中使用预填充的 html 正文撰写电子邮件

c# - 使用 Oxyplot 和 Xamarin 的堆积柱形图