具有分类 Y 轴的柱形图

发布于 2025-01-11 15:26:12 字数 445 浏览 0 评论 0 原文

我正在尝试将类别分配给柱形图上的 Y 轴。我尝试过使用categories,但这会导致一个特殊的结果,列未到达X轴,并且值不正确

https://jsfiddle.net/fmgLa5h4/1/

我尝试过各种其他属性来纠正此问题,但无济于事。

我还尝试过使用 labels.formatter ,它得到了稍微更理想的结果,但标签与刻度线对齐 - 我再次尝试了其他方法来纠正这个问题,但没有成功

https://jsfiddle.net/fmgLa5h4/2/

I'm trying to assign categories to the Y Axis on a column chart. I've tried using categories but this causes a peculiar result with the columns not reaching the X Axis, and the values being incorrect

https://jsfiddle.net/fmgLa5h4/1/

I've tried various other properties to correct this but to no avail.

I've also tried using labels.formatter, which gets a slightly more desirable result but with the labels aligned to the ticks - again I've tried other ways to correct this without success

https://jsfiddle.net/fmgLa5h4/2/

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

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

发布评论

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

评论(1

囍孤女 2025-01-18 15:26:12

您可以尝试使用 data.seriesMapping 映射数据并设置自己的 dataLabels 或类别,看一下这个示例:

var categories = ['A', 'B', 'C', 'D', 'E'],
  yAxisLabels = ['10', '20', '30', '40', '50'];

Highcharts.chart('container', {

  data: {
    csv: document.getElementById('data').innerHTML,
    seriesMapping: [{
      // x: 0, // X values are pulled from column 0 by default
      // y: 1, // Y values are pulled from column 1 by default
      label: 2 // Labels are pulled from column 2 and picked up in the dataLabels.format below
    }]
  },
  chart: {
    type: 'column'
  },
  title: {
    text: 'Daily runs'
  },
  xAxis: {
    minTickInterval: 24 * 36e5
  },
  yAxis: {
    labels: {
      formatter: function() {
        return yAxisLabels[this.value] + ', ' + this.value || '';
      },
    },

    tickInterval: 1,
    max: categories.length - 1,
  },
  legend: {
    enabled: false
  },
  plotOptions: {
    series: {
      dataLabels: {
        enabled: true,
        format: '{point.label}'
      },
    }
  }

});

对于格式化数据表, yAxis.labels.formatter 是一个不错的选择。在映射之前,您的数据可能需要格式化。

示例概念:

data = data.replace(/\r\n/g, '\n') // Unix
    .replace(/\r/g, '\n') // Mac
    .split('\n');
data.forEach(function(row, i){
    if (i) {
        row = row.split(',');
        data[i] = row.concat([
            categories.indexOf(row[1]),
            categories.indexOf(row[3])
        ]).join(',');
    }
});
data = data.join('\n');

现场演示:
https://jsfiddle.net/BlackLabel/g5svkrac/

API 参考:

https://api.highcharts.com/highcharts/yAxis.labels.formatter

https://api.highcharts.com/highcharts/data.seriesMapping


你的意思是定位标签,你可以使用 yAxis.labels.y 相对于刻度线位置的所有标签的位置偏移轴。

https://jsfiddle.net/BlackLabel/g5svkrac/1/

You can try to mapping the data using data.seriesMapping and set your own dataLabels or categories, take a look at this example:

var categories = ['A', 'B', 'C', 'D', 'E'],
  yAxisLabels = ['10', '20', '30', '40', '50'];

Highcharts.chart('container', {

  data: {
    csv: document.getElementById('data').innerHTML,
    seriesMapping: [{
      // x: 0, // X values are pulled from column 0 by default
      // y: 1, // Y values are pulled from column 1 by default
      label: 2 // Labels are pulled from column 2 and picked up in the dataLabels.format below
    }]
  },
  chart: {
    type: 'column'
  },
  title: {
    text: 'Daily runs'
  },
  xAxis: {
    minTickInterval: 24 * 36e5
  },
  yAxis: {
    labels: {
      formatter: function() {
        return yAxisLabels[this.value] + ', ' + this.value || '';
      },
    },

    tickInterval: 1,
    max: categories.length - 1,
  },
  legend: {
    enabled: false
  },
  plotOptions: {
    series: {
      dataLabels: {
        enabled: true,
        format: '{point.label}'
      },
    }
  }

});

For formatting data tables, yAxis.labels.formatter is a good option. It is possible that your data will need to be formatted before you map it.

Sample concept:

data = data.replace(/\r\n/g, '\n') // Unix
    .replace(/\r/g, '\n') // Mac
    .split('\n');
data.forEach(function(row, i){
    if (i) {
        row = row.split(',');
        data[i] = row.concat([
            categories.indexOf(row[1]),
            categories.indexOf(row[3])
        ]).join(',');
    }
});
data = data.join('\n');

Live demo:
https://jsfiddle.net/BlackLabel/g5svkrac/

API References:

https://api.highcharts.com/highcharts/yAxis.labels.formatter

https://api.highcharts.com/highcharts/data.seriesMapping


You meant to positioning the label, you can use yAxis.labels.y to position offset of all labels relative to the tick positions on the axis.

https://jsfiddle.net/BlackLabel/g5svkrac/1/

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