【问题标题】:HIGHCHARTS auto-rotate x-axis labels for custom positioned labelsHIGHCHARTS 为自定义定位标签自动旋转 x 轴标签
【发布时间】:2021-11-11 19:38:41
【问题描述】:

我有一个在 highcharts 中的图表,带有自定义移动 x 轴标签(以适应刻度之间而不是刻度),最后一个标签被删除(有关原始图表的开发,请参阅此主题:HIGHCHARTS: Event on Export can't access x-axis labels)。 ​

当 x 轴标签的数量变大和/或图表宽度变得太窄时,默认的自动旋转似乎不起作用。相反,标签用省略号('...')截断。请参阅下面的图片和 JS Fiddle:

这里是 JS 小提琴:https://jsfiddle.net/SirMunchington/g5t4kr7L/2

这是 jsfiddle 中的代码,因为包括小提琴链接在内的主题中需要代码:

HTML:

<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/offline-exporting.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

<a href title="Download Chart" id="download-link">Download Chart</a>

<div id="container"></div>

JS:

var download_link = document.getElementById('download-link');
download_link.onclick = downloadChartImage;

function downloadChartImage() {
  Highcharts.charts[0].exportChartLocal({
    filename: 'test123',
    type: 'image/png',
  })
  return false;
}

chart_options = {
  "colors": ["#f28f43", "#ffea00", "#2f7ed8", "#9370db", "#1aadce", "#8bbc21", "#c42525"],
  "exporting": {
    "chartOptions": {
      "chart": {
        "backgroundColor": "#ffffff",
      }
    },
    "sourceWidth": 1250,
    "sourceHeight": 500,
    "scale": 2,
    "fallbackToExportServer": false,
    "buttons": {
      "contextButton": {
        "enabled": false
      }
    }
  },
  "title": {
    "text": "EDITZ TEST GOAL FOR INSTITUTION Updated Name",
    "style": {
      "color": "#274b6d",
      "fontSize": "16px"
    }
  },
  "chart": {
    "reflow": true,
    "backgroundColor": "transparent",
    "borderRadius": 5,
    "events": {
      render() {
        let chart = this,
          xAxis = chart.xAxis[0],
          ticks = xAxis.ticks,
          firstTick = ticks[xAxis.tickPositions[0]],
          secondTick = ticks[xAxis.tickPositions[1]],
          ticksDistance = secondTick.mark.getBBox().x - firstTick.mark.getBBox().x;

        for (let i in ticks) {
          let tick = ticks[i];
          if (!tick.isLast) {
            tick.label.translate(ticksDistance / 2, 0)
          }
        }
      }
    }
  },
  "plotOptions": {
    "series": {
      "label": {
        "connectorAllowed": true
      },
      "pointStart": 2011,
      "animation": false,
      "enableMouseTracking": true,
      "stickyTracking": false,
      "shadow": false
    },
    "line": {
      "animation": false,
      "enableMouseTracking": true,
      "shadow": false,
      "marker": {
        "enabled": true
      }
    }
  },
  "legend": {
    "borderColor": "#000000",
    "borderRadius": 5,
    "borderWidth": 1,
    "useHTML": true,
    "adjustChartSize": true,
    "itemHiddenStyle": {
      "color": "#8e8e8e"
    },
    "itemStyle": {
      "fontWeight": "normal",
      "color": "#274b6d"
    },
    "itemDistance": 10,
    "symbolRadius": 2
  },
  "credits": {
    "enabled": false
  },
  "xAxis": {
    "type": "datetime",
    "labels": {
      formatter: function() {
        return moment(this.value).year()
      }
    },
    "tickPositions": [1277967600000, 1309503600000, 1341126000000, 1372662000000, 1404198000000, 1435734000000, 1467356400000, 1498892400000, 1530428400000, 1561964400000, 1593586800000, 1625122800000, 1656658800000, 1688194800000, 1719817200000, 1751439600000, 1783062000000, 1814684400000, 1846306800000, 1877929200000, 1909551600000, 1941174000000, 1972796400000],
    "endOnTick": true,
    "startOnTick": true,
    showLastLabel: false,
    "maxPadding": 0,
    "minPadding": 0,
    "tickLength": 5,
    "title": {
      "text": "Fiscal Year",
      "style": {
        "color": "#4d759e",
        "fontWeight": "bold"
      }
    },
    "plotLines": [{
      "color": "rgba(0, 0, 0, 0.3)",
      "width": 1,
      "value": 1341125999999,
      "dashStyle": "longdash"
    }, {
      "color": "rgba(0, 0, 0, 0.8)",
      "width": 1,
      "value": 1605130646775,
      "dashStyle": "longdash"
    }, {
      "color": "rgba(139, 188, 33, 0.5)",
      "width": 1,
      "value": 1657609200000,
      "dashStyle": "longdash"
    }, {
      "color": "#f28f43",
      "width": 1,
      "value": 1612166400000,
      "dashStyle": "longdash"
    }]
  },
  "yAxis": {
    "tickLength": 5,
    "title": {
      "text": "Energy consumed (MMBtu)",
      "style": {
        "color": "#4d759e",
        "fontWeight": "bold"
      }
    }
  },
  "series": [{
    "type": "line",
    "name": "Past consumption (Projected)",
    "color": "#000000",
    "marker": {
      "symbol": "circle"
    },
    "dashStyle": "Dash",
    "legendIndex": 0,
    "tooltip": {
      "valueDecimals": 0,
      "outside": true,
      "useHTML": true,
      "headerFormat": "<span style=\"font-weight:bold; color:{series.color};\">{series.name}</span><br />"
    },
    "data": [
      [1277967599999, 420113.1142557926],
      [1341125999999, 420113.1142557926]
    ]
  }, {
    "type": "line",
    "name": "Consumption (Actual)",
    "color": "#000000",
    "lineWidth": 2,
    "marker": {
      "symbol": "circle"
    },
    "legendIndex": 1,
    "tooltip": {
      "valueDecimals": 0,
      "outside": true,
      "useHTML": true,
      "headerFormat": "<span style=\"font-weight:bold; color:{series.color};\">{series.name}</span><br />"
    },
    "data": [
      [1341125999999, 420113.1142557926],
      [1435733999999, -50],
      [1467356399999, 37.53355796084987],
      [1498892399999, 500],
      [1530428399999, -500],
      [1561964399999, 250],
      [1593586799999, -250]
    ]
  }, {
    "type": "scatter",
    "name": "Baseline",
    "color": "rgba(0, 0, 0, 0.9)",
    "marker": {
      "symbol": "square",
      "radius": 5
    },
    "legendIndex": 2,
    "tooltip": {
      "enabled": true,
      "useHTML": true,
      "outside": true,
      "headerFormat": "<span style=\"font-weight:bold; color:{series.color};\">{series.name}</span><br />"
    },
    "data": [
      [1341125999999, 420113.1142557926]
    ]
  }, {
    "type": "line",
    "name": "Baseline consumption",
    "color": "rgba(140, 21, 21, 0.75)",
    "marker": {
      "symbol": "triangle"
    },
    "dashStyle": "Dash",
    "legendIndex": 3,
    "tooltip": {
      "valueDecimals": 0,
      "outside": true,
      "useHTML": true,
      "headerFormat": "<span style=\"font-weight:bold; color:{series.color};\">{series.name}</span><br />"
    },
    "data": [
      [1341125999999, 420113.1142557926],
      [1372661999999, 420113.1142557926],
      [1404197999999, 420113.1142557926],
      [1435733999999, 420113.1142557926],
      [1467356399999, 420113.1142557926],
      [1498892399999, 420113.1142557926],
      [1530428399999, 420113.1142557926],
      [1561964399999, 420113.1142557926],
      [1593586799999, 420113.1142557926],
      [1625122799999, 420113.1142557926],
      [1656658799999, 420113.1142557926],
      [1688194799999, 420113.1142557926],
      [1719817199999, 420113.1142557926],
      [1751353199999, 420113.1142557926],
      [1782889199999, 420113.1142557926],
      [1814425199999, 420113.1142557926],
      [1845961199999, 420113.1142557926],
      [1877497199999, 420113.1142557926],
      [1909033199999, 420113.1142557926],
      [1940569199999, 420113.1142557926],
      [1972105199999, 420113.1142557926],
      [2003641199999, 420113.1142557926],
      [2035177199999, 420113.1142557926]
    ]
  }, {
    "type": "scatter",
    "name": "Current Date",
    "color": "#000000",
    "marker": {
      "symbol": "circle",
      "radius": 5
    },
    "legendIndex": 4,
    "tooltip": {
      "enabled": true,
      "useHTML": true,
      "outside": true,
      "headerFormat": "<span style=\"font-weight:bold; color:{series.color};\">{series.name}</span><br />"
    },
    "data": [
      [1605130646777, 420113.1142557926]
    ]
  }, {
    "type": "scatter",
    "name": "Intermediate Target #1",
    "color": "#f28f43",
    "marker": {
      "symbol": "diamond",
      "radius": 5
    },
    "legendIndex": 5,
    "tooltip": {
      "enabled": true,
      "useHTML": true,
      "outside": true,
      "headerFormat": "<span style=\"font-weight:bold; color:{series.color};\">{series.name}</span><br />"
    },
    "data": [
      [1612166400000, 1234]
    ]
  }, {
    "type": "scatter",
    "name": "Primary Target",
    "color": "#8bbc21",
    "marker": {
      "symbol": "diamond",
      "radius": 5
    },
    "legendIndex": 6,
    "tooltip": {
      "enabled": true,
      "valueDecimals": 0,
      "useHTML": true,
      "headerFormat": "<span style=\"font-weight:bold; color:{series.color};\">{series.name}</span><br />"
    },
    "data": [
      [1657609200000, 123]
    ]
  }]
}

Highcharts.chart('container', chart_options)

【问题讨论】:

  • 我不确定您的问题是否与this question 有关,但在 LinusR 的回答中,autoRotation 仅适用于类别图表。因此,您可以尝试添加诸如加载和重绘之类的事件来调用函数以根据 morganfree 的评论调整标签旋转。

标签: highcharts


【解决方案1】:

在事件加载中,您可以更新您的 xaxis 标签旋转并设置与类别长度、刻度的关系。

events: {
  load: function() {
    var chart = this,
      xAxis = chart.xAxis[0];
    console.log(xAxis.categories);

    xAxis.update({
      labels: {
        rotation: 45
      }
    })

  }
}

演示:https://jsfiddle.net/BlackLabel/Lchtve8a/

https://api.highcharts.com/class-reference/Highcharts.Axis#update https://api.highcharts.com/highcharts/chart.events.load


您可以使用 xAxis.labels.rotation 更改自动旋转标签并将旋转调整为响应式设计规则 responsive.rules.chartOptions。 但我不确定您是否指的是出口行为,如果是,请说明。

responsive: {
    rules: [{
        condition: {
            maxWidth: 500
        },
        chartOptions: {
            xAxis: {
                labels: {
                    formatter: function () {
                        return this.value.charAt(0);
                    },
                    rotation: 90
                }
            },
            yAxis: {
                labels: {
                    align: 'left',
                    x: 0,
                    y: -2
                },
                title: {
                    text: ''
                }
            }
        }
    }]
}

现场演示:

https://jsfiddle.net/BlackLabel/gqysmkat/

API 参考:

https://api.highcharts.com/highcharts/responsive.rules.chartOptions https://api.highcharts.com/highcharts/xAxis.labels.rotation

【讨论】:

  • 理想情况下,首先是网页版(除非导出是相同的解决方案)。挑战在于图表根据用户的不同有不同的刻度数,因此对于什么图表宽度应该触发旋转没有一个规则。我认为自动旋转不适用于这种图表(根据您的回答和用户对上述问题的评论)?是否没有设置highcharts自动检测标签何时溢出并在此类图表中旋转它们?
  • 在上面的小提琴中调整框架的大小似乎不会改变标签。你有折线图的例子吗?也许在我的小提琴的副本中?
  • 您可以访问类别,您现在可以编写一个函数,根据刻度数更改标签的位置。
  • 最好的方法是使用刻度数,并在此基础上计算。您可能可以编写一个函数来计算标签应更改的单个刻度的长度,但这需要更多的工作和自定义。
猜你喜欢
  • 1970-01-01
  • 2013-09-25
  • 1970-01-01
  • 2021-05-26
  • 2019-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
相关资源
最近更新 更多