Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StackedBarChart isn't stacking bars #2815

Closed
rossmooney opened this issue Sep 21, 2017 · 3 comments
Closed

StackedBarChart isn't stacking bars #2815

rossmooney opened this issue Sep 21, 2017 · 3 comments

Comments

@rossmooney
Copy link

rossmooney commented Sep 21, 2017

I'm trying to create a stacked bar chart, but the bars won't stack. I just get alternating colors for adjacent bars, and the values are summed from the array passed into it. I pass in an array of x values (indexes) and an array of y value arrays as items. A coworker ran into the same issue on the Android platform, but I don't see this issue mentioned anywhere else so maybe we aren't setting something correctly?

screen shot 2017-09-21 at 2 15 24 pm

        //Get x values and y value arrays
 		guard let chart = chart as? BarChartView,
            case .stackedY(let yValues) = data.yValues,
        case .index(let xValues, _) = data.xValues else { return }

        //Create entries
        let entries = yValues.enumerated().map { index, items in
            BarChartDataEntry(x: Double(xValues[index]), yValues: items)
        }
                
        //Build set
        let set = BarChartDataSet(values: [], label: config.title)
        set.setColors(self.chartColors, alpha: 1.0)
        set.drawValuesEnabled = false

        //Update with entries
        entries.forEach { entry in _ = set.addEntry(entry) }
        barChartData?.dataSets[0] = set
        
        //Labels and legends
        set.stackLabels = ["Source1", "Source2"]
        chart.legend.enabled = !set.stackLabels.isEmpty
        chart.legend.xEntrySpace = chartWidth / 4
        chart.legend.xOffset = 20
        chart.legend.form = .circle

        //Update data on chart
        barChartData.barWidth = 0.5
        chart.data = barChartData
@liuxuan30
Copy link
Member

liuxuan30 commented Sep 22, 2017

check out ChartDemo; you don't setup correctly. The stacked y values should be in an array.
e.g.

    for (int i = 0; i < count; i++)
    {
        double mult = (range + 1);
        double val1 = (double) (arc4random_uniform(mult) + mult / 3);
        double val2 = (double) (arc4random_uniform(mult) + mult / 3);
        double val3 = (double) (arc4random_uniform(mult) + mult / 3);
        
        [yVals addObject:[[BarChartDataEntry alloc] initWithX:i yValues:@[@(val1), @(val2), @(val3)] icon: [UIImage imageNamed:@"icon"]]];
    }

@rossmooney
Copy link
Author

Thanks for your response, but I am using an array of y-values for each entry.

Turns out my issue was that I was not using the initializer of BarChartDataSet to pass in the values. The stack size is only calculated there, which is a requirement for bar chart stacking to work.

@liuxuan30
Copy link
Member

all I see. I don't realize this. Sorry

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants