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

Is it possible to show date values in the Stacked BarChart? #3408

Closed
blulion opened this issue Apr 18, 2018 · 2 comments
Closed

Is it possible to show date values in the Stacked BarChart? #3408

blulion opened this issue Apr 18, 2018 · 2 comments

Comments

@blulion
Copy link

blulion commented Apr 18, 2018

What did you do?

My requirement is to show horizontally grouped stacked bar chart plotted with the date values. For this, I customized horizontal bar chart example to show grouped horizontally stacked bar chart.

Fig 1)

screen shot 2018-04-18 at 10 46 26 pm

for i in 0..<models.count {
            let variant = self.models![i]
            let designValues = self.getDesignData(forModel: variant) //returns TimeInterval in an array
            let designEntry = BarChartDataEntry(x: Double(i), yValues: designValues)
            dataEntries.append(designEntry)
            
            let prodValues = self.getProductionData(forModel: variant)  //returns TimeInterval in an array
            let prodEntry = BarChartDataEntry(x: Double(i), yValues: prodValues)
            dataEntries1.append(rdEntry)
        }
        
        let dateValueFormatter = DateFormatter()
        dateValueFormatter.dateFormat = "MMM YYYY"
        
        let chartDataSet = BarChartDataSet(values: dataEntries, label: "Design")
        chartDataSet.axisDependency = .right
        chartDataSet.valueFormatter = DateValueFormatter(dateFormatter: dateValueFormatter)

        let chartDataSet1 = BarChartDataSet(values: dataEntries1, label: "Production")
        chartDataSet1.valueTextColor = UIColor.clear
        chartDataSet1.axisDependency = .right
        chartDataSet1.valueFormatter =  DateValueFormatter(dateFormatter: dateValueFormatter)
        
        let dataSets: [BarChartDataSet] = [chartDataSet,chartDataSet1]
        chartDataSet.colors = [ChartColorTemplates.material()[0], ChartColorTemplates.material()[1]]
        chartDataSet1.colors = [UIColor(red: 230/255, green: 126/255, blue: 34/255, alpha: 1), ChartColorTemplates.material()[2]]
        
        let chartData = BarChartData(dataSets: dataSets)
        chartView.data = chartData
       
        let groupSpace = 0.25
        let barSpace = 0.05
        // (0.3 + 0.05) * 2 + 0.3 = 1.00 -> interval per "group"
        chartData.barWidth = barWidth;
        
        chartData.groupBars(fromX: 0.0, groupSpace: groupSpace, barSpace: barSpace)
        chartView.notifyDataSetChanged()
        chartView.data = chartData
      
        //background color
        chartView.backgroundColor = UIColor(red: 189/255, green: 195/255, blue: 199/255, alpha: 1)
        
        //chart animation
        chartView.animate(xAxisDuration: 1.5, yAxisDuration: 1.5, easingOption: .linear)

What did you expect to happen?

When I add the date values (TimeInterval), the date should be stacked properly with respect to the axis dependency and I should able to see the correct values plotted on the stacked bar chart. The expected bar chart should be something like this.

Fig 2)

screen shot 2018-04-18 at 11 13 15 pm

Note: I added custom logic in stringForValue: method in dateformatter to show the date values with respect to the bar chart (above fig). But it is not working when I zoom.

What happened instead?

When I try to use the date values (date range are between 2016 and 2019) to plot the stacked bar chart, the values are getting added to the previous values while displaying the bar chart. Hence the date range is going till year 2286 (see below fig.). So I calculated the difference between date values and plotted the bar chart. Now I am able to show the bars between the date range. But the values indicated in the bar chart is displaying date values from 1970. (see fig 1)

screen shot 2018-04-18 at 11 00 38 pm

How can I show the correct date values with respect to the bar chart. Also When I am trying to tap on the second or third stack in the bar, the first stack of the bar is getting clicked.

Charts Environment

**Charts version/Branch/Commit Number: 3.0.5
**Xcode version: 9.2
**Swift version: 4.1
**Platform(s) running Charts: iOS
**macOS version running Xcode: 10.12.6

@blulion blulion changed the title Is it possible to show date values in the Stacked BarChart Is it possible to show date values in the Stacked BarChart? Apr 18, 2018
@liuxuan30
Copy link
Member

you just have to figure out a way using valueFormatter. Next time, ask on stack overflow.

@blulion
Copy link
Author

blulion commented Apr 27, 2018

Thank you. I fixed the issue. Sorry about that. Next time, I will ask it on StackOverflow.

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