I am facing with issue regarding correct naming on x-Axis
Here you can see the result of chart with values – splitting into groups working correctly, numeric values are correctly under the groups, but there are values of positions not of the years values.
Correct result should be with years
0.5 should be represented as 2019
2.5 should be represented as 2020
6 should be represented as 2021
9.5 should be represented as 2022
11 should be represented as 2024
Here is my code
List quantity = [1, 1, 6, 0, 5, 1, 7, 1, 3, 2, 1, 1]
List categories = ["May 2019", "Jul 2019", "Nov 2020", "Dec 2020", "Apr 2021", "May 2021", "Jul 2021", "Aug 2021", "Dec 2021", "Feb 2022", "May 2022", "Jun 2024"]
List data = [776.16, 776.16, 776.16, 0, 259.854, 519.25, -135.81, 805.17, 571.89, 915.81, 1124.37, 1135.54]
String currency = "USD"
// Calculate tick positions and labels for the years
def tickPositions = []
def yearLabels = []
def monthDataPerYear = [:]
def cumulativeMonths = 0
categories.each { category ->
def year = category.split(' ')[1]
if (!monthDataPerYear.containsKey(year)) {
monthDataPerYear[year] = []
}
monthDataPerYear[year].add(category)
}
monthDataPerYear.each { year, months ->
def numberOfMonths = months.size()
def middleOfYear = cumulativeMonths + (numberOfMonths / 2) - 0.5
tickPositions.add(cumulativeMonths) // Position separation line at the start of the new year's first month
yearLabels.add([position: middleOfYear, label: year])
cumulativeMonths += numberOfMonths
}
def chartWithNetInvoiceQty = [
chart: [
type: 'column'
],
title: [
text: "Price evolution of Corn"
],
xAxis: [
[
categories: categories,
crosshair: true,
labels: [
useHTML: true
]
],
[
linkedTo: 0,
categories: yearLabels.collect { it.label },
tickPositions: yearLabels.collect { it.position },
labels: [
align: 'center',
useHTML: true,
y: 20 // Adjust this value to move the labels down
],
tickmarkPlacement: 'on',
opposite: false,
lineWidth: 0, // Hide the axis line
plotLines: tickPositions.collect { position ->
return [
color: 'gray',
width: 1,
value: position - 0.5 // Adjust position for plot lines to be before the first month of the new year
]
}
]
],
yAxis: [
[
title: [
text: 'Quantity'
]
],
[
opposite: true,
title: [
text: 'Price'
],
labels: [
format: "{value} ${currency}"
]
]
],
tooltip: [
shared: true
],
series: [
[
name: 'Sum of Qty',
type: 'column',
data: quantity,
color: "#00FF00"
],
[
name: 'Average price per unit',
yAxis: 1,
type: 'line',
data: data,
color: "#000000",
tooltip: [
pointFormat: '{series.name}: <b>{point.y:.2f}</b><br/>'
]
]
]
]
return api.buildHighchart(chartWithNetInvoiceQty)
What am I doing wrong?