Skip to content

Commit

Permalink
yaxis.showAlways property to prevent hiding y-axis when user toggles …
Browse files Browse the repository at this point in the history
…series via legend click - fixes #339
  • Loading branch information
junedchhipa committed Feb 22, 2019
1 parent 5875dd5 commit 5aa8b00
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 100 deletions.
130 changes: 91 additions & 39 deletions src/modules/Legend.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class Legend {
let text = legendFormatter(legendNames[i], { seriesIndex: i, w })

let collapsedSeries = false
let ancillaryCollapsedSeries = false
if (w.globals.collapsedSeries.length > 0) {
for (let c = 0; c < w.globals.collapsedSeries.length; c++) {
if (w.globals.collapsedSeries[c].index === i) {
Expand All @@ -106,6 +107,18 @@ class Legend {
}
}

if (w.globals.ancillaryCollapsedSeriesIndices.length > 0) {
for (
let c = 0;
c < w.globals.ancillaryCollapsedSeriesIndices.length;
c++
) {
if (w.globals.ancillaryCollapsedSeriesIndices[c] === i) {
ancillaryCollapsedSeries = true
}
}
}

let elMarker = document.createElement('span')
elMarker.classList.add('apexcharts-legend-marker')

Expand Down Expand Up @@ -149,10 +162,10 @@ class Legend {

Graphics.setAttrs(elMarker, {
rel: i + 1,
'data:collapsed': collapsedSeries
'data:collapsed': collapsedSeries || ancillaryCollapsedSeries
})

if (collapsedSeries) {
if (collapsedSeries || ancillaryCollapsedSeries) {
elMarker.classList.add('inactive-legend')
}

Expand All @@ -177,7 +190,7 @@ class Legend {

Graphics.setAttrs(elLegendText, {
rel: i + 1,
'data:collapsed': collapsedSeries
'data:collapsed': collapsedSeries || ancillaryCollapsedSeries
})

elLegend.appendChild(elMarker)
Expand All @@ -191,7 +204,8 @@ class Legend {
total === 0 &&
coreUtils.seriesHaveSameValues(i) &&
!coreUtils.isSeriesNull(i) &&
w.globals.collapsedSeriesIndices.indexOf(i) === -1
w.globals.collapsedSeriesIndices.indexOf(i) === -1 &&
w.globals.ancillaryCollapsedSeriesIndices.indexOf(i) === -1
) {
elLegend.classList.add('apexcharts-hidden-zero-series')
}
Expand All @@ -200,7 +214,8 @@ class Legend {
if (!w.config.legend.showForNullSeries) {
if (
coreUtils.isSeriesNull(i) &&
w.globals.collapsedSeriesIndices.indexOf(i) === -1
w.globals.collapsedSeriesIndices.indexOf(i) === -1 &&
w.globals.ancillaryCollapsedSeriesIndices.indexOf(i) === -1
) {
elLegend.classList.add('apexcharts-hidden-null-series')
}
Expand All @@ -226,10 +241,10 @@ class Legend {

Graphics.setAttrs(elLegend, {
rel: i + 1,
'data:collapsed': collapsedSeries
'data:collapsed': collapsedSeries || ancillaryCollapsedSeries
})

if (collapsedSeries) {
if (collapsedSeries || ancillaryCollapsedSeries) {
elLegend.classList.add('inactive-legend')
}

Expand Down Expand Up @@ -550,7 +565,9 @@ class Legend {
}
}
w.globals.collapsedSeries = []
w.globals.ancillaryCollapsedSeries = []
w.globals.collapsedSeriesIndices = []
w.globals.ancillaryCollapsedSeriesIndices = []
w.globals.risingSeries = risingSeries
w.config.series = series
this.ctx._updateSeries(
Expand Down Expand Up @@ -585,43 +602,52 @@ class Legend {
}

if (isHidden) {
if (w.globals.collapsedSeries.length > 0) {
for (let c = 0; c < w.globals.collapsedSeries.length; c++) {
if (w.globals.collapsedSeries[c].index === realIndex) {
if (w.globals.axisCharts) {
w.config.series[realIndex].data = w.globals.collapsedSeries[
c
].data.slice()
w.globals.collapsedSeries.splice(c, 1)
w.globals.collapsedSeriesIndices.splice(c, 1)
w.globals.risingSeries.push(realIndex)
} else {
w.config.series[realIndex] = w.globals.collapsedSeries[c].data
w.globals.collapsedSeries.splice(c, 1)
w.globals.collapsedSeriesIndices.splice(c, 1)
w.globals.risingSeries.push(realIndex)
}
this.ctx._updateSeries(
w.config.series,
w.config.chart.animations.dynamicAnimation.enabled
)
}
}
}
this.riseCollapsedSeries(
w.globals.collapsedSeries,
w.globals.collapsedSeriesIndices,
realIndex
)
this.riseCollapsedSeries(
w.globals.ancillaryCollapsedSeries,
w.globals.ancillaryCollapsedSeriesIndices,
realIndex
)
} else {
if (w.globals.axisCharts) {
w.globals.collapsedSeries.push({
index: realIndex,
data: w.config.series[realIndex].data.slice(),
type: seriesEl.parentNode.className.baseVal.split('-')[1]
})
w.globals.collapsedSeriesIndices.push(realIndex)
let shouldNotHideYAxis = false

let removeIndexOfRising = w.globals.risingSeries.indexOf(realIndex)
if (
w.config.yaxis[realIndex] &&
w.config.yaxis[realIndex].show &&
w.config.yaxis[realIndex].showAlways
) {
shouldNotHideYAxis = true
if (
w.globals.ancillaryCollapsedSeriesIndices.indexOf(realIndex) < 0
) {
w.globals.ancillaryCollapsedSeries.push({
index: realIndex,
data: w.config.series[realIndex].data.slice(),
type: seriesEl.parentNode.className.baseVal.split('-')[1]
})
w.globals.ancillaryCollapsedSeriesIndices.push(realIndex)
}
}

if (!shouldNotHideYAxis) {
w.globals.collapsedSeries.push({
index: realIndex,
data: w.config.series[realIndex].data.slice(),
type: seriesEl.parentNode.className.baseVal.split('-')[1]
})
w.globals.collapsedSeriesIndices.push(realIndex)

let removeIndexOfRising = w.globals.risingSeries.indexOf(realIndex)

w.globals.risingSeries.splice(removeIndexOfRising, 1)
w.globals.risingSeries.splice(removeIndexOfRising, 1)
}

// mutating the user's config object here
// TODO: AVOID mutating the user's config object below
w.config.series[realIndex].data = []
} else {
w.globals.collapsedSeries.push({
Expand Down Expand Up @@ -664,6 +690,32 @@ class Legend {
seriesEl.fire('click')
}
}

riseCollapsedSeries(series, seriesIndices, realIndex) {
const w = this.w

if (series.length > 0) {
for (let c = 0; c < series.length; c++) {
if (series[c].index === realIndex) {
if (w.globals.axisCharts) {
w.config.series[realIndex].data = series[c].data.slice()
series.splice(c, 1)
seriesIndices.splice(c, 1)
w.globals.risingSeries.push(realIndex)
} else {
w.config.series[realIndex] = series[c].data
series.splice(c, 1)
seriesIndices.splice(c, 1)
w.globals.risingSeries.push(realIndex)
}
this.ctx._updateSeries(
w.config.series,
w.config.chart.animations.dynamicAnimation.enabled
)
}
}
}
}
}

export default Legend
8 changes: 4 additions & 4 deletions src/modules/Scales.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ export default class Range {
niceScale(yMin, yMax, index = 0, ticks = 10) {
if (
(yMin === Number.MIN_VALUE && yMax === 0) ||
(!Utils.isNumber(yMin) && !Utils.isNumber(yMax))
(!Utils.isNumber(yMin) && !Utils.isNumber(yMax)) ||
(yMin === Number.MIN_VALUE && yMax === Number.MIN_SAFE_INTEGER)
) {
// when all values are 0
yMin = 0
yMax = 1
ticks = 1
yMax = ticks
let linearScale = this.linearScale(yMin, yMax, ticks)
return linearScale
}
Expand Down Expand Up @@ -212,7 +212,7 @@ export default class Range {
minY,
maxY,
index,
y.tickAmount ? y.tickAmount : 6
y.tickAmount ? y.tickAmount : 5
)
}
}
Expand Down
55 changes: 0 additions & 55 deletions src/modules/axes/YAxis.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,64 +425,9 @@ export default class YAxis {
yaxe.floating ||
yaxisLabelCoords[index].width === 0

// let yAxisWidth = yaxisLabelCoords[index].width + yTitleCoords[index].width
// if (index > 0 && !w.config.yaxis[index - 1].opposite) {
// prevLeftYAxisWidth =
// yaxisLabelCoords[index - 1].width + yTitleCoords[index - 1].width
// }

// if (shouldNotDrawAxis) {
// yAxisWidth = 0
// prevLeftYAxisWidth = 0
// }

// let multipleYPadd =
// this.multipleYs && yTitleCoords[index].width > 0
// ? yTitleCoords[index].width * 1.02
// : 15

// let paddingForYAxisTitle = this.xPaddingForYAxisTitle(
// index,
// {
// width: yaxisLabelCoords[index].width
// },
// {
// width: yTitleCoords[index].width
// },
// yaxe.opposite
// )

// if (index > 0 && !w.config.yaxis[index - 1].opposite) {
// paddingForPrevYAxisTitle = this.xPaddingForYAxisTitle(
// index - 1,
// {
// width: yaxisLabelCoords[index - 1].width
// },
// {
// width: yTitleCoords[index - 1].width
// },
// w.config.yaxis[index - 1].opposite
// )
// }

// yAxisWidth = yAxisWidth + Math.abs(paddingForYAxisTitle.padd)
// prevLeftYAxisWidth =
// prevLeftYAxisWidth + Math.abs(paddingForPrevYAxisTitle.padd)

let axisWidth = yaxisLabelCoords[index].width + yTitleCoords[index].width

if (!yaxe.opposite) {
// left side y axis
// let offset = yAxisWidth + 5
// if (shouldNotDrawAxis) {
// offset = 0
// }

// if (index > 0 && !w.config.yaxis[index - 1].opposite) {
// leftOffsetX =
// yaxisLabelCoords[index - 1].width + yTitleCoords[index - 1].width
// }

xLeft = w.globals.translateX - leftOffsetX

if (!shouldNotDrawAxis) {
Expand Down
6 changes: 4 additions & 2 deletions src/modules/settings/Globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ export default class Globals {
noLabelsProvided: false, // if user didn't provide any categories/labels or x values, fallback to 1,2,3,4...
allSeriesCollapsed: false,
collapsedSeries: [], // when user collapses a series, it goes into this array
collapsedSeriesIndices: [], // this just stores the index of the collapsedSeries instead of whole object
collapsedSeriesIndices: [], // this stores the index of the collapsedSeries instead of whole object for quick access
ancillaryCollapsedSeries: [], // when user collapses an "alwaysVisible" series, it goes into this array
ancillaryCollapsedSeriesIndices: [], // this stores the index of the collapsedSeries whose y-axis is always visible
risingSeries: [], // when user re-opens a collapsed series, it goes here
selectedDataPoints: [],
ignoreYAxisIndexes: [], // when series are being collapsed in multiple y axes, ignore certain index
Expand All @@ -76,7 +78,7 @@ export default class Globals {
minY: Number.MIN_VALUE, // is 5e-324, i.e. the smallest positive number
// NOTE: If there are multiple y axis, the first yaxis array element will be considered for all y values calculations. Rest all will be calculated based on that
maxY: -Number.MAX_VALUE, // is -1.7976931348623157e+308
// NOTE: The above note for minY apllies here as well
// NOTE: The above note for minY applies here as well

minYArr: [],
maxYArr: [],
Expand Down
1 change: 1 addition & 0 deletions src/modules/settings/Options.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default class Options {
constructor() {
this.yAxis = {
show: true,
showAlways: false,
seriesName: undefined,
opposite: false,
logarithmic: false,
Expand Down

0 comments on commit 5aa8b00

Please sign in to comment.