iOS Charts not showing Y values
You should add data.setDrawValues(true)
when you create the BarChartData
:
...
let data = BarChartData(dataSet: barChartDataSet)
data.setDrawValues(true)
data.setValueFont(UIFont(name:"Lato-Regular", size:10)!)
data.setValueTextColor(UIColor(hex:UIConstants.Graphics.BrandTheme.ThemeColor))
...
For version 3.2.1
of Charts
you also need to remove this line because values are not rendered if drawIconsEnabled
is false
barChartDataSet.drawIconsEnabled = false
iOS Charts - single values not showing Swift
The only way I could get around this was to add an additional invisible line.
I created a clear line that started the day before and ended the day after my single values.
As long as there is a line on the chart that goes from one point to another, the other single values show.
var singleValue = false
for i in 0...(dataSets.count - 1) {
if dataSets[i].values.count > 1{
singleValue = true
}
}
var data = dataSets
if singleValue == false {
let minNS = Calendar.current.date(byAdding: .day, value: -1, to: minNSDate as! Date)
let maxNS = Calendar.current.date(byAdding: .day, value: 1, to: maxNSDate as! Date)
var dataEntries: [ChartDataEntry] = []
let dataEntry1 = ChartDataEntry(x:Double(String(format: "%.2f",Double((minNS?.timeIntervalSince1970)!)))!,y:00.00)
let dataEntry2 = ChartDataEntry(x:Double(String(format: "%.2f",Double((maxNS?.timeIntervalSince1970)!)))!,y:00.00)
dataEntries.append(dataEntry1)
dataEntries.append(dataEntry2)
let set = LineChartDataSet(values: dataEntries, label: "")
set.setCircleColor(UIColor.clear)
set.circleHoleColor = UIColor.clear
set.setColor(UIColor.white, alpha: 0.0)
set.drawValuesEnabled = false
data.append(set)
}
chart.chartDescription?.text = ""
let chartData = LineChartData(dataSets: data)
chart.data = chartData
iOS Charts does not show value labels when more than 3 data sets are plotted
I fixed this with one line in viewDidLoad where I first set up my chart.
_chartView.maxVisibleCount = 500;
I guess after adding the 4th data set I had more than the default value of maxVisibleCount (whatever that is) and when that happens no data labels are drawn regardless of other settings.
I figured this out when modifying my data set's drawValuesEnabled field and inside the auto complete text it said "this value is ignored when maxVisibleCount is reached".
iOS-Charts Library: x-axis labels without backing data not showing
Ok so thanks to @wingzero 's comment, I have been able to get this working. There are a few things required to do so. For simplicity's sake, I am going to explain how to get the "days of the week" labels working as I originally asked. If you follow these steps, however, you should be able to tweak them to format your chart however you like (for example, with months of the year).
1) Make sure that your chart's x-axis minimum and maximum values are set. In this case, you'd want to say: chartView.xAxis.axisMinimum = 0.0
and chartView.axisMaximum = 6.0
. This is important for step 2.
2) As Wingzero alluded to, create a subclass of XAxisRenderer
that allows us to grab the minimum and maximum values set in step one and determine what values should be passed to our IAxisValueFormatter
subclass in step three. In this case:
class XAxisWeekRenderer: XAxisRenderer {
override func computeAxis(min: Double, max: Double, inverted: Bool) {
axis?.entries = [0, 1, 2, 3, 4, 5, 6]
}
}
Make sure to pass this renderer to your chart like this: chartView.xAxisRenderer = XAxisWeekRenderer()
3) Create a subclass of IAxisValueFormatter
that takes the values we passed to the chart in step two ([0, 1, 2, 3, 4, 5, 6]) and gets corresponding label names. This is what I did in my original question here. To recap:
public class CustomChartFormatter: NSObject, IAxisValueFormatter {
var days: = ["S", "M", "T", "W", "T", "F", "S"]
public func stringForValue(value: Double, axis: AxisBase?) -> String {
return days[Int(value)]
}
}
4) Set the labelCount
on your graph to be equal to the number of labels you want. In this case, it would be 7. I show how to do this, along with the rest of the steps, below the last step here.
5) Force the labels to be enabled
6) Force granularity on the chart to be enabled and set granularity to 1. From what I understand, setting the granularity to 1 means that if the data your chart passes to stringForValue
is not in round numbers, the chart will essentially round said data or treat it like it is rounded. This is important since if you passed in 0.5, it's possible that your stringForValue
might not produce the right strings for your labels.
7) Set the value formatter on the xAxis to be the custom formatter you created in step 3.
Steps 4-7 (plus setting the formatter created in step 3) are shown below:
chartView.xAxis.labelCount = 7
chartView.xAxis.forceLabelsEnabled = true
chartView.xAxis.granularityEnabled = true
chartView.xAxis.granularity = 1
chartView.xAxis.valueFormatter = CustomChartFormatter()
Swift charts not showing value labels
Figured it out. Turns out, when you create a data set using an array of ChartDataEntry objects, the array must be immutable (let
rather than var
), or labels will not be shown.
Correct data not showing in ios Charts
You have changed x & y values and hence index is coming as your pie chart's value. Also, please use PieChartDataEntry.
(working code on my box)
override func viewDidLoad() {
super.viewDidLoad()
let days = ["Mo", "Tu", "Wed", "Thur", "Fri", "Sat", "Sun"]
let gross = [11.00, 90.95, 250.00, 40.90, 60.88, 99.99, 25.00]
setChart(days: days, gross: gross)
}
func setChart(days: [String], gross: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<days.count {
let dataEntry = PieChartDataEntry(value : gross[i], label : days[i])
dataEntries.append(dataEntry)
}
let pieChartDataSet = PieChartDataSet(values: dataEntries, label: "Gross Income")
pieChartDataSet.sliceSpace = 2.0
let pieChartData = PieChartData(dataSets: [pieChartDataSet])
var colors: [UIColor] = []
for _ in 0..<days.count {
let red = Double(arc4random_uniform(256))
let green = Double(arc4random_uniform(256))
let blue = Double(arc4random_uniform(256))
let color = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1)
colors.append(color)
}
pieChartDataSet.colors = colors
pieChartView.data = pieChartData
}
Try this and let me know.
Related Topics
Prefix(_ Maxlength:) Is Type-Erased When Used with a Struct That Conforms to Lazysequenceprotocol
Should I Invalidate Timer When My Application Is Suspended or in Background
iOS 14 Widget Detect System Theme Change
Redirect Process Stdout to Apple System Log Facility in Swift
Uicolor Extension in Swift Error
How to Write a Generic Function for Floating Point Values in Swift
How to Import Zbar Framework in Swift Project
Why Is The Swiftui List Not Lazy (Compared to Lazyvstack)
Swift's Decimal Precision Issue
Wrong Value Returned After Formatting Timestamp
How to Check for Self with Contains() in Swift
Uialertview or UIalertcontroller to Display Only Once in Swift
Disable Bringing App Window to Front. After Closing Another Window
Will a a Weak Wrapper Class Throw Off a Hashable Based Collection When The Wrapped Value Is Set Nil
How to Name File Stored to Files App via Uactivityviewcontroller