Calculate years based on months in 17 months graph in TypeScript (Highcharts)

huangapple go评论85阅读模式
英文:

Calculate years based on months in 17 months graph in TypeScript (Highcharts)

问题

我有一个困扰了我很久的问题。

我有这样一个柱状图,横跨17个月 - 12个月前和6个月后。

目前是五月,往回推12个月也是五月。此外,往后推6个月是十一月。

在工具提示中,我需要显示每个月的特定年份。

我有两根并排的柱子,其中一根应显示该年份之前的年份。

例如,左边柱子 -> 2022,右边柱子 -> 2023,依此类推。

英文:

I have an issue I've been struggling with.

I have this column chart that spans 17 months - 12 months back and 6 months ahead.

Currently, it's May, and 12 months back is also May. Additionally, 6 months ahead is November.

In the tooltip, I need to show the specific year for each month.

I have two columns side by side, where one column should display the year prior to that particular year.

Ex. left bar->2022 right bar->2023 and so on.

Calculate years based on months in 17 months graph in TypeScript (Highcharts)

It has to be beside the header text here in the tooltip:
Tooltip hover image

My code:

private getSeriesDataFromGraphData(graphData: GetPreviousYearGraphData[]): Highcharts.PointOptionsObject[] {
	return graphData.map(({y, b2b, b2c, normal}) => ({y, b2b, b2c, normal}));
}

// Using the search-filters does not affect this graph. This graph is only affected when filtering between brands.
private renderOrderAmountCompareOnBrand(
	strCurrencyCode: string,
	arrCategories: string[],
	arrPrevSeriesDataStock: GetPreviousYearGraphData[],
	arrPrevSeriesDataPre: GetPreviousYearGraphData[],
	arrSeriesDataStock: GetPreviousYearGraphData[],
	arrSeriesDataPre: GetPreviousYearGraphData[]
) {
	// Get the series data for each set of graph data
	const prevSeriesDataStock = this.getSeriesDataFromGraphData(arrPrevSeriesDataStock);
	const prevSeriesDataPre = this.getSeriesDataFromGraphData(arrPrevSeriesDataPre);
	const seriesDataStock = this.getSeriesDataFromGraphData(arrSeriesDataStock);
	const seriesDataPre = this.getSeriesDataFromGraphData(arrSeriesDataPre);

	this.OrderAmountComparedOnBrandChart = Highcharts.chart('claimsOrderAmountPerSalesChannel', {
			chart: {
				type: 'column'
			},
			title: {
				text: 'Order amount compared on brand'
			},
			xAxis: {
				categories: arrCategories
			},
			yAxis: {
				allowDecimals: false,
				min: 0,
				title: {
					text: 'Brand order statistics'
				}
			},
			tooltip: {
				formatter: function () {
					let strStyling = "display: flex; justify-content: space-between; gap: 20px;";
					let startDate: Date = new Date();

					startDate.setMonth(startDate.getMonth() - 12);

					let year = startDate.getFullYear();

					if (!this.series.name.includes('Previous year')) {
						year += 1;
					}

					const GetPreviousYearGraphData = this.point as GetPreviousYearGraphData;

					return `
						<b>${this.point.category} | ${this.series.name} | ${year}</b><br><br>
						<div style="${strStyling}">
							<span><b>Sales staff:</b></span> ${Highcharts.numberFormat(GetPreviousYearGraphData.normal || 0, 0)} ${strCurrencyCode ?? ''}<br>
						</div>
						<div style="${strStyling}">
							<span><b>B2B:</b></span> ${Highcharts.numberFormat(GetPreviousYearGraphData.b2b || 0, 0)} ${strCurrencyCode ?? ''}<br>
						</div>
						<div style="${strStyling}">
							<span><b>B2C:</b></span> ${Highcharts.numberFormat(GetPreviousYearGraphData.b2c || 0, 0)} ${strCurrencyCode ?? ''}<br>
						</div>
						<div style="${strStyling}">
							<span><b>Total:</b></span> ${Highcharts.numberFormat(this.y || 0, 0)} ${strCurrencyCode ?? ''}
						</div>
					`;
				},
				useHTML: true
			},
			plotOptions: {
				column: {
					stacking: 'normal',
					dataLabels: {
						enabled: false
					}
				}
			},
			series: [
				{
					name: 'Stock Previous year',
					data: prevSeriesDataStock,
					stack: 'Stock1'
				},
				{
					name: 'Pre Previous year',
					data: prevSeriesDataPre,
					stack: 'Stock1'
				},
				{
					name: 'Stock',
					data: seriesDataStock,
					stack: 'Stock'
				},
				{
					name: 'Pre',
					data: seriesDataPre,
					stack: 'Stock'
				}
			] as SeriesOptionsType[]
		} as Options
	);
}

I tried a whole bunch of things, but i cant get it to calculate correctly.

答案1

得分: 0

我明白最大的问题是计算去年月份的适当年份。为此,您可以检查this.point.x - 12currentDate.getMonth() + 1的总和是否大于0,并根据计算设置月份。

例如:

tooltip: {
  formatter: function() {
    let strStyling = "display: flex; justify-content: space-between; gap: 20px;";
    const strCurrencyCode = '';

    const currentDate = new Date();
    const isCurrentYear = this.point.x - 12 + currentDate.getMonth() + 1 > 0;

    currentDate.setMonth(currentDate.getMonth() - (isCurrentYear ? 12 : 24));

    let year = currentDate.getFullYear();

    if (!this.series.name.includes('Previous year')) {
      year += 1;
    }

    return `...`;
  },
  useHTML: true
}

实时演示: 链接

API 参考文档: 链接

英文:

I understand that the biggest problem is to calculate the proper year for the last year's months. To do that, you can check if a sum of this.point.x - 12 and currentDate.getMonth() + 1 is greater than 0 and set month depending on the calculations.

For example:

  tooltip: {
    formatter: function() {
      let strStyling = "display: flex; justify-content: space-between; gap: 20px;";
      const strCurrencyCode = '';

      const currentDate = new Date();
      const isCurrentYear = this.point.x - 12 + currentDate.getMonth() + 1 > 0;

      currentDate.setMonth(currentDate.getMonth() - (isCurrentYear ? 12 : 24));

      let year = currentDate.getFullYear();

      if (!this.series.name.includes('Previous year')) {
        year += 1;
      }

      return `...`;
    },
    useHTML: true
  }

Live demo: http://jsfiddle.net/BlackLabel/xf3Lu7qp/

API Reference: https://api.highcharts.com/highcharts/tooltip.formatter

huangapple
  • 本文由 发表于 2023年5月17日 19:48:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271769.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定