Skip to content

Commit

Permalink
annotations fix for horizontal bar charts - fixes #51
Browse files Browse the repository at this point in the history
  • Loading branch information
junedchhipa committed Nov 9, 2018
1 parent 82af2d6 commit 2980c28
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
6 changes: 0 additions & 6 deletions samples/bar/basic-bar.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@
}],
xaxis: {
categories: ['South Korea', 'Canada', 'United Kingdom', 'Netherlands', 'Italy', 'France', 'Japan', 'United States', 'China', 'Germany'],
},
yaxis: {

},
tooltip: {

}
}

Expand Down
29 changes: 23 additions & 6 deletions src/modules/Annotations.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class Annotations {
this.w = ctx.w
this.graphics = new Graphics(this.ctx)

if (this.w.config.chart.type === 'bar' && this.w.config.plotOptions.bar.horizontal) {
this.invertAxis = true
}

this.xDivision = this.w.globals.gridWidth / this.w.globals.dataPoints
}

Expand Down Expand Up @@ -46,9 +50,12 @@ class Annotations {
addXaxisAnnotation (anno, parent, index) {
let w = this.w

const min = this.invertAxis ? w.globals.minY : w.globals.minX
const range = this.invertAxis ? w.globals.yRange[0] : w.globals.xRange

let strokeDashArray = anno.strokeDashArray

let x1 = (anno.x - w.globals.minX) / (w.globals.xRange / w.globals.gridWidth)
let x1 = (anno.x - min) / (range / w.globals.gridWidth)

let line = this.graphics.drawLine(
x1 + anno.offsetX,
Expand Down Expand Up @@ -101,7 +108,16 @@ class Annotations {

let strokeDashArray = anno.strokeDashArray

let y1 = w.globals.gridHeight - (anno.y - w.globals.minYArr[anno.yAxisIndex]) / (w.globals.yRange[anno.yAxisIndex] / w.globals.gridHeight)
let y1

if (this.invertAxis) {
let catIndex = w.globals.labels.indexOf(anno.y)
const xLabel = w.globals.dom.baseEl.querySelector('.apexcharts-yaxis-texts-g text:nth-child(' + (catIndex + 1) + ')')

y1 = parseInt(xLabel.getAttribute('y'))
} else {
y1 = w.globals.gridHeight - (anno.y - w.globals.minYArr[anno.yAxisIndex]) / (w.globals.yRange[anno.yAxisIndex] / w.globals.gridHeight)
}

const text = anno.label.text ? anno.label.text : ''

Expand Down Expand Up @@ -166,11 +182,12 @@ class Annotations {
let x = 0
let y = 0
let pointY = 0
if (typeof anno.x === 'string') {
if (w.config.chart.type === 'bar' && w.config.plotOptions.bar.horizontal) {
// todo
}

if (this.invertAxis) {
console.warn('Point annotation is not supported in horizontal bar charts.')
}

if (typeof anno.x === 'string') {
let catIndex = w.globals.labels.indexOf(anno.x)
const xLabel = w.globals.dom.baseEl.querySelector('.apexcharts-xaxis-texts-g text:nth-child(' + (catIndex + 1) + ')')

Expand Down

0 comments on commit 2980c28

Please sign in to comment.