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

Bar Chart Not Showing Colours Correctly #3823

Closed
VSmallbon opened this issue Jan 21, 2019 · 1 comment
Closed

Bar Chart Not Showing Colours Correctly #3823

VSmallbon opened this issue Jan 21, 2019 · 1 comment

Comments

@VSmallbon
Copy link

I'm looking to change the colours of a Bar Chart but every time I try and set the colours in BarChartDataSet the correct colours don't appear. I've read through the documentation but I can't work out what is going wrong.

       let yValuesDataSet = BarChartDataSet(values: yValues, label: "(\(measurementMetadata[0].unit ?? ""))")
        styleAxis(yValuesDataSet, color: lineColor, labelColor: textColor, innerColor: innerColor, filled:true)
    
        if showDetail {
            yValuesDataSet.stackLabels = [String]()
            legendEntries = [LegendEntry]()
            for (i,singleMeasurementMetadata) in measurementMetadata.enumerated()
            {
                yValuesDataSet.stackLabels.append(singleMeasurementMetadata.displayName!)
                legendEntries.append(LegendEntry(label: singleMeasurementMetadata.displayName!, form: .square, formSize: 5, formLineWidth: 1, formLineDashPhase: 0, formLineDashLengths: nil, formColor: UserInterfaceManager.Colours.darkGreenyBlue))
            }
        } else {
            yValuesDataSet.label = ""//highLevelLabel
            legendEntries = [LegendEntry]()
            legendEntries.append(LegendEntry(label: highLevelLabel, form: .square, formSize: 5, formLineWidth: 1, formLineDashPhase: 0, formLineDashLengths: nil, formColor: lineColor))
            
            yValuesDataSet.stackLabels = [String]()
            yValuesDataSet.stackLabels.append(highLevelLabel)
        }
        
        yValuesDataSet.colors = ChartColorTemplates.pastel()
        
        let chartData = BarChartData(dataSet: yValuesDataSet)
        chartData.barWidth = 0.5
        
        chartView.data = chartData
        chartView.notifyDataSetChanged()

screen shot 2019-01-21 at 12 28 58 pm

If instead of using ChartColorTemplates.pastel() as a template I use a custom UIColor array, that also produces funny results. An array of 4 colours will produce correct results but an array of 5 colours (or more) will produce funny results (either with the colours appearing out of order or all bars showing the same colour). However oddly enough, the colours in the legend will be in the correct orde.r

    var barColours = [UIColor]()
    barColours.append(UserInterfaceManager.graphColours.sleepBlue)
    barColours.append(UserInterfaceManager.Colours.darkOrange)
    barColours.append(UserInterfaceManager.graphColours.sleepRed)
    barColours.append(UserInterfaceManager.graphColours.sleepGreen)
    barColours.append(UserInterfaceManager.Colours.darkestGreenyBlue)
    barColours.append(UserInterfaceManager.Colours.lightOrange)
   
    yValuesDataSet.colors = barColours
    let chartData = BarChartData(dataSet: yValuesDataSet)
    chartData.barWidth = 0.5

    chartView.data = chartData
    chartView.notifyDataSetChanged()

screen shot 2019-01-21 at 12 41 33 pm

Using Charts 3.2.1 with Swift 4 and XCode Version 10.1

@liuxuan30
Copy link
Member

please check ChartsDemo first. I don't see issues with multiple colors.

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