Skip to content

Commit

Permalink
close ChartsOrg#3659: fix index out of bounds issue when using stacke…
Browse files Browse the repository at this point in the history
…d bar chart
  • Loading branch information
pmairoldi authored and Shineeth Hamza committed Oct 31, 2018
1 parent 6c57008 commit e11e64b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ open class BarChartDataSet: BarLineScatterCandleBubbleChartDataSet, IBarChartDat
}

/// array of labels used to describe the different values of the stacked bars
open var stackLabels: [String] = ["Stack"]
open var stackLabels: [String] = []

// MARK: - Styling functions and accessors

Expand Down
16 changes: 14 additions & 2 deletions Source/Charts/Renderers/BarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -806,15 +806,27 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer

if dataSet.isStacked, let vals = e.yValues
{
let stackLabel = dataSet.stackLabels[idx % stackSize]
let labelCount = min(dataSet.colors.count, stackSize)

let stackLabel: String?
if (dataSet.stackLabels.count > 0 && labelCount > 0) {
let labelIndex = idx % labelCount
stackLabel = dataSet.stackLabels.indices.contains(labelIndex) ? dataSet.stackLabels[labelIndex] : nil
} else {
stackLabel = nil
}

elementValueText = dataSet.valueFormatter?.stringForValue(
vals[idx % stackSize],
entry: e,
dataSetIndex: dataSetIndex,
viewPortHandler: viewPortHandler) ?? "\(e.y)"

elementValueText = stackLabel + " \(elementValueText)"
if let stackLabel = stackLabel {
elementValueText = stackLabel + " \(elementValueText)"
} else {
elementValueText = "\(elementValueText)"
}
}

let dataSetCount = dataProvider.barData?.dataSetCount ?? -1
Expand Down
15 changes: 12 additions & 3 deletions Source/Charts/Renderers/LegendRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,21 @@ open class LegendRenderer: Renderer
{
let bds = dataSet as! IBarChartDataSet
var sLabels = bds.stackLabels

for j in 0..<min(clrs.count, bds.stackSize)
let minEntries = min(clrs.count, bds.stackSize)

for j in 0..<minEntries
{
let label: String?
if (sLabels.count > 0 && minEntries > 0) {
let labelIndex = j % minEntries
label = sLabels.indices.contains(labelIndex) ? sLabels[labelIndex] : nil
} else {
label = nil
}

entries.append(
LegendEntry(
label: sLabels[j % sLabels.count],
label: label,
form: dataSet.form,
formSize: dataSet.formSize,
formLineWidth: dataSet.formLineWidth,
Expand Down

0 comments on commit e11e64b

Please sign in to comment.