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

Last Value doesn't appear in barCharts and lineCharts #1384

Closed
ajimenezjulio opened this issue Aug 31, 2016 · 11 comments
Closed

Last Value doesn't appear in barCharts and lineCharts #1384

ajimenezjulio opened this issue Aug 31, 2016 · 11 comments
Labels

Comments

@ajimenezjulio
Copy link

ajimenezjulio commented Aug 31, 2016

I have some troubles with the library, when I create a barChart I have to add an extra value to my values array in order to show all my original values, and in the lineChart the last value appears in the line graph but it doesn't have its value above the point like the others.

I let you my code and some pictures showing the problem:

BarChart: My xAxis have a custom format showing months.

override func viewDidLoad() {
    super.viewDidLoad()

    let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]
    max = unitsSold.maxElement()        

    setChart(unitsSold)        
}


func setChart(values: [Double]) {

    let formato:BarChartFormatter = BarChartFormatter()
    let xaxis:XAxis = XAxis()

    barChartView.noDataText = "You need to provide data for the chart."

    var dataEntries: [BarChartDataEntry] = []

    for i in 0..<values.count {
        let dataEntry = BarChartDataEntry(x: Double(i), y:values[i])
        dataEntries.append(dataEntry)

        formato.stringForValue(Double(i), axis: xaxis)
    }

    xaxis.valueFormatter = formato

    let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
    chartDataSet.colors = ChartColorTemplates.joyful()

    barChartView.fitBars = true //Para que las barras inicial y final se muestren completas
    barChartView.xAxis.valueFormatter = xaxis.valueFormatter
    barChartView.xAxis.labelPosition = .Bottom
    barChartView.leftAxis.axisMinimum = 0
    barChartView.leftAxis.axisMaximum = max + 2
    barChartView.rightAxis.enabled = false
    barChartView.drawValueAboveBarEnabled = false
    barChartView.descriptionText = ""
    barChartView.xAxis.drawGridLinesEnabled = false
    barChartView.animate(xAxisDuration: 1.0, yAxisDuration: 1.0)

    let chartData = BarChartData()
    chartData.addDataSet(chartDataSet)
    barChartView.data = chartData
}

LineChart: My xAxis have a custom format showing months.

override func viewDidLoad() {
    super.viewDidLoad()

    let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0]
    max = unitsSold.maxElement()

    setChart(unitsSold)   
}

func setChart(values: [Double]) {

    let formato:BarChartFormatter = BarChartFormatter()
    let xaxis:XAxis = XAxis()

    var dataEntries: [ChartDataEntry] = []

    for i in 0..<values.count {
        let dataEntry = ChartDataEntry(x: Double(i), y: values[i])
        dataEntries.append(dataEntry)

        formato.stringForValue(Double(i), axis: xaxis)
    }

    xaxis.valueFormatter = formato

    let lineChartDataSet = LineChartDataSet(values: dataEntries, label: "Units Sold")
    lineChartDataSet.colors = ChartColorTemplates.joyful()
    lineChartDataSet.circleColors = ChartColorTemplates.joyful()
    lineChartDataSet.drawCircleHoleEnabled = false


    let lineChartData = LineChartData( dataSet: lineChartDataSet)
    lineChartData.setValueFont(UIFont(name: "HelveticaNeue-Light", size: 7))

    lineChartView.fitScreen()
    lineChartView.xAxis.labelPosition = .Bottom
    lineChartView.xAxis.axisMaximum = Double(values.count) 
    lineChartView.leftAxis.axisMinimum = 0
    lineChartView.leftAxis.axisMaximum = max + 3
    lineChartView.rightAxis.enabled = false
    lineChartView.descriptionText = ""
    lineChartView.xAxis.drawGridLinesEnabled = false
    lineChartView.animate(xAxisDuration: 1.0, yAxisDuration: 1.0)
    lineChartView.xAxis.valueFormatter = xaxis.valueFormatter

    lineChartView.data = lineChartData
 }

The responses are: For barChart December doesn't appear and in lineChart the last value doesn't show its value above it, I hope someone can help.

BarChart

enter image description here

@ajimenezjulio ajimenezjulio changed the title Last Value doesn't show in barCharts and lineCharts Last Value doesn't appear in barCharts and lineCharts Aug 31, 2016
@chuynadamas
Copy link
Contributor

Hi @ajimenezjulio

Regarding the bars i have the same problem.

Are you using fitBars? If yes,I guess that the problem is that in the function swift internal override func calcMinMax() over the class BarChartView they have the following calculate

        if fitBars
        {
            _xAxis.calculate(
                min: data.xMin - data.barWidth / 2.0,
                max: data.xMax - data.barWidth / 2.0)
        }
        else
        {
            _xAxis.calculate(min: data.xMin, max: data.xMax)
        }

In my case, I'm using the fitbars property, but I guess that we need to add instead of subtract the size of the barWidth/2.0 to the data.xMax this to increase the value of the range to get the extra space on the chart. In other hand this jus fix the problem with the fitBars, but I guess that the problem on the following case is about the way the library calculate the space for the groups of charts.

@danielgindi @PhilJay Could you guys take a look of what I'm saying above.

_FYI every group of charts has the same number of bars, so the library isn't drawing the rest of the chart for the last group also i'm using fitBars property_

screen shot 2016-08-31 at 12 52 01 pm

@ajimenezjulio
Copy link
Author

O: @chuynadamas you are right changing the "-" symbol to "+" it solves the problem with the barChart graph:

if fitBars {
                _xAxis.calculate(
                min: data.xMin - data.barWidth / 2.0,
                max: data.xMax + data.barWidth / 2.0)
        }

@liuxuan30
Copy link
Member

liuxuan30 commented Sep 1, 2016

@ajimenezjulio is it Chart 3.0?

@chuynadamas you post this screenshot in #1359 (comment), are the same issue?

@chuynadamas
Copy link
Contributor

chuynadamas commented Sep 1, 2016

Hey @liuxuan30,

Yes, this is 3.0 and no, there is a different issue. #1359 is about the positions of the labels.

Thanks in advance.

@ajimenezjulio
Copy link
Author

ajimenezjulio commented Sep 1, 2016

Yep the problem #1359 is different.

I think that the problem is in the function that draws the value, similar to "drawValues" in barChart but in lineChart it doesn't exist, finding where the library draws the values for its respective point in the graph we can solve the issue. The question is where it is?.

@gw-tan
Copy link

gw-tan commented Sep 2, 2016

I faced this issue as well with the LineChart. I made a temporary fix (not sure if this is the correct location to fix this) that works well.

Renderers -> LineChartRenderer.swift -> line 536 ( drawValues )

for j in _xBounds.min.stride(to: Int(ceil(Double(_xBounds.max - _xBounds.min) * phaseX + Double(_xBounds.min))) + 1, by: 1)

add the +1 to the calculation, solves the problem. Anyone can verify if this should be the fix for the problem?

this is out of topic but, drawing a bezier curve line chart has the same problem as well. The last point is not connected to the rest of the curve.

@liuxuan30 liuxuan30 added the bug label Sep 2, 2016
@ajimenezjulio
Copy link
Author

Holy crap! you're incredible @FatsGW modifying that line solves the problem with the last value in lineChart, thank you!!!

@danielgindi
Copy link
Collaborator

@PhilJay ?

@PhilJay
Copy link
Collaborator

PhilJay commented Sep 6, 2016

Just checked on Android, could not reproduce the issue. Maybe this is iOS only?

@danielgindi
Copy link
Collaborator

This is already fixed by one of the PRs, please re-check.

@ghost
Copy link

ghost commented May 25, 2018

I faced this issue as well with the LineChart. I made a temporary fix (not sure if this is the correct location to fix this) that works well.
Renderers -> LineChartRenderer.swift -> line 536 ( drawValues )
for j in _xBounds.min.stride(to: Int(ceil(Double(_xBounds.max - _xBounds.min) * phaseX + Double(_xBounds.min))) + 1, by: 1)
add the +1 to the calculation, solves the problem. Anyone can verify if this should be the fix for the problem?

Same Problem I have. Above fix is not working for me
Here is my code at Renderers -> LineChartRenderer.swift -> line 536 ( drawValues )
for j in stride(from: _xBounds.min, through: min(_xBounds.min + _xBounds.range, _xBounds.max), by: 1)
Please let me know How I have to change the code to fix the issue?
@danielgindi @FatsGW

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

No branches or pull requests

6 participants