问题
I'm working with chart-ios library and i manage to show data in graph like below image.
My Json data is like below. i showing field1+field2+field3
in X and created_at
in y axis. this data is for 1 day.
[
{
"created_at": "2020-05-10T00:01:09Z",
"field1": 17.57,
"field2": 56.37,
"field3": 44.06,
"equipment": 3
},
{
"created_at": "2020-05-10T00:01:55Z",
"field1": 17.57,
"field2": 54.52,
"field3": 44.62,
"equipment": 3
},
{
"created_at": "2020-05-10T00:02:40Z",
"field1": 21.63,
"field2": 52.7,
"field3": 51.27,
"equipment": 3
},
{
"created_at": "2020-05-10T00:03:26Z",
"field1": 17.15,
"field2": 54.95,
"field3": 44.21,
"equipment": 3
},
{
"created_at": "2020-05-10T00:04:12Z",
"field1": 15.97,
"field2": 54.69,
"field3": 41.36,
"equipment": 3
},
{
"created_at": "2020-05-10T00:04:57Z",
"field1": 15.86,
"field2": 38.58,
"field3": 36.59,
"equipment": 3
},
{
"created_at": "2020-05-10T00:05:43Z",
"field1": 7.66,
"field2": 51.6,
"field3": 45.46,
"equipment": 3
}
..... 1000 more...
]
What i want is to show x axis label in 1-hour interval. like 11:00, 12:00 and 13:00 , so on. its like fixed 24 hours i want to show, not more than that. but x value count is more than 1000 records.
so how do i do this? can any one point me out in right direction?
Thanks
回答1:
- X value need to be Double -> usually I use timeIntervalSince1970
- you need a CustomValueFormatter
- set xAxis minimum to date with 00:00
- set xAxis maximum to date with 00:00 + 1 day
- xAxis.granularity = 3600 -> only show label for every hour
- chart.setVisibleXRange(minXRange: 5 * 3600, maxXRange: 5 * 3600) -> show only 5 hour range with scroll
check this project: https://github.com/aiwiguna/ExampleCharts/tree/feature/hourly-charts
来源:https://stackoverflow.com/questions/62680758/how-to-show-only-24hr-column-in-line-chart