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

Charts 3.0: How to set x-values for a line chart? #1627

Closed
arienmalec opened this issue Oct 9, 2016 · 6 comments
Closed

Charts 3.0: How to set x-values for a line chart? #1627

arienmalec opened this issue Oct 9, 2016 · 6 comments

Comments

@arienmalec
Copy link

I want a line chart that represents temporal data with x labels for each timeperiod (e.g., each day), and x-y values only for collected data.

In the previous version, I did this like so:

let d = LineChartData(xVals: presenter.xLabels, dataSet: dataSet)
//...
self.data = d

This constructor no longer exists, and despite searching around, I haven't seen a clear substitute.

What's the Charts 3.0 equivalent?

@danielgindi
Copy link
Collaborator

You haven't searched enough... You can see the demos, or this:
#1474

@arienmalec
Copy link
Author

This is not terribly responsive. By "searching around" I did intend to mean that I had read the migration note, and looking at the demos.

Despite this, the answer to my question is not apparent.

All of the demos have one y value per x value. The ChartDataEntry object takes only an x,y pair.

I want a graph that has more x value than y values.

This is similar to #1584 although I'm not sure I understand the workaround described (setting min/max x axis values).

@liuxuan30
Copy link
Member

@arienmalec not very clear what's your issue. If you want to set x values, just follow line chart or Time Line Chart in ChartsDemo. I'ts enough info
An entry only took only one x and y. It's point, having more x values than y values in entry does not make sense.

@arienmalec
Copy link
Author

@liuxuan30

As a concrete example, imagine an exercise log. On the x-axis, there are days. Y values are work performed. There are y values only for days when exercise was performed. The line graph displays the trend of exercise sessions.

This kind of time series chart is fairly common, and worked just fine in the previous version of Charts, by setting the xVals explicitly as noted above.

For comparison, I've attached an Excel graph that displays the pattern.
time-series-line-graph

@liuxuan30
Copy link
Member

liuxuan30 commented Oct 11, 2016

With ChartsDemo I can have something like below that looks similar to yours.
delete

You can also try Time Line Chart as well. they are two different x axis style.

@arienmalec
Copy link
Author

Here's the solution for posterity:

Where the code was previously:

let d = LineChartData(xVals: xLabels, dataSet: dataSet)
//...
self.data = d

The Charts 3.0 version is:

let d = LineChartData(dataSet: dataSet)
self.data = d
self.xAxis.valueFormatter = XValsFormatter(xVals: xLabels)
xAxis.axisMinimum = Double(0)
xAxis.axisMaximum = Double(presenter.xLabels.count - 1)
class XValsFormatter: NSObject, IAxisValueFormatter {

    let xVals: [String]
    init(xVals: [String]) {
        self.xVals = xVals
    }

    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        return xVals[Int(value)]
    }

}

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

3 participants