How do I display barChart ""bars from "zero "on the y axis - ios-charts

I have created a simple bar chart with some data all in the same range( between 20 and 30).
the generated chart displays the data starting at just bellow 20 to just above 30.
How can I render the graph to display the data from 0 on the y axis
included below is my trial code and an image of the resulting graph
import UIKit
import Charts
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
generateChartData()
setChart() }
let temperatures = [22.1, 21.5, 27.3, 27.5, 30.0, 22.2,27.6, 20.0, 23.5]
let colours = [ "red", "blue", "green", "orange", "grey", "yellow", "black", "white"]
var combinedData:[BarChartDataEntry] = []
#IBOutlet weak var barChart: BarChartView!
func setChart() {
let chartDataSet = BarChartDataSet(entries: combinedData, label: "interesting data")
let chartData = BarChartData(dataSet: chartDataSet)
barChart.data = chartData
}
func generateChartData() {
for i in 0..<colours.count {
let thisEntry = BarChartDataEntry(x: Double(i), y: temperatures[i], data: colours[i])
combinedData.append(thisEntry)
}
}
}bar graph displaying truncated bars

You can set the axises minimum value, like this:
barChart.leftAxis.axisMinimum = 0
barChart.rightAxis.axisMinimum = 0
Or even not zero, but some minus value. Added here left and right axis for simplicity if you disable one of them.

Related

How to set days on X-axis

I wanna create a chart with 7 days only on the x-axis
I wanna plot readings according to the day and hours they have been saved at but only show days' names.
time format: Thursday-7:23 pm which is something like "dd - HH: MM a"
import UIKit
import Charts
import TinyConstraints
class ViewController: UIViewController,ChartViewDelegate{
lazy var lineChartView: LineChartView = {
let chartView = LineChartView()
//X-axis
chartView.xAxis.centerAxisLabelsEnabled = false
chartView.xAxis.setLabelCount(7, force: true)//sets x axis to have 7 values
let xAxis = lineChartView.xAxis
chartView.xAxis.axisLineWidth = 1.5
chartView.xAxis.drawGridLinesEnabled = false//hides x-axis grids
//chartView.xAxis.valueFormatter??
chartView.animate(xAxisDuration: 2.5)
return chartView
}()
func setChartData() {
//create a set for line 1
let color1 = NSUIColor(red: CGFloat(140.0/255.0), green: CGFloat(170.0/255.0), blue: CGFloat(177.0/255.0), alpha: 1)
let set1 = LineChartDataSet(entries: yValues, label: "hello")
set1.mode = .cubicBezier //makes curves smoother
set1.setCircleColor(color1)
set1.circleRadius = 6
set1.circleHoleRadius = 3
set1.lineWidth = 3
set1.setColor(color1)
//create a line chart data
let data1 = LineChartData(dataSet: set1)
data1.setDrawValues(false)//hides values on markers
//add data to lineChartView
lineChartView.data = data1
}
let yValues: [ChartDataEntry] = [
ChartDataEntry(x: 0.0, y: 10.0),
ChartDataEntry(x: 1.0, y: 5.0),
ChartDataEntry(x: 2.0, y: 7.0),
]
}

iOS Charts Radar Chart size

I'm using the Charts library and am trying to replicate this design:
I'm sort of getting there, but the chart is rendering itself way too small:
I'm expecting the chart to fill the entire width of the screen, and use all the vertical space. To be clear: the RadarChartView is the width of the entire black area, and the entire vertical space right up to the legend (which is not part of the chart view itself).
Any ideas?
This is the table cell code that shows the chart:
import Charts
import UIKit
final class ReportSpiderChart: UITableViewCell {
private let labels = ["ARTISTS", "TRACKS", "ALBUMS"]
#IBOutlet private var chartView: RadarChartView!
override func awakeFromNib() {
super.awakeFromNib()
chartView.webLineWidth = 1
chartView.innerWebLineWidth = 1
chartView.webColor = .init(hex: "28282A")
chartView.innerWebColor = .init(hex: "28282A")
chartView.legend.enabled = false
let xAxis = chartView.xAxis
xAxis.labelFont = .systemFont(ofSize: 11, weight: .semibold)
xAxis.xOffset = 0
xAxis.yOffset = 0
xAxis.labelTextColor = .init(hex: "919198")
xAxis.valueFormatter = self
let yAxis = chartView.yAxis
yAxis.labelCount = 3
yAxis.labelFont = .systemFont(ofSize: 11, weight: .semibold)
yAxis.labelTextColor = .init(hex: "919198")
yAxis.axisMinimum = 0
yAxis.drawLabelsEnabled = false
}
func configure(data: ReportData) {
let entries: [RadarChartDataEntry] = [
.init(value: Double(data.artists)),
.init(value: Double(data.tracks)),
.init(value: Double(data.albums)),
]
chartView.yAxis.axisMaximum = Double(max(max(data.artists, data.tracks), data.albums))
let dataSet = RadarChartDataSet(entries: entries)
dataSet.fillColor = UIColor(hex: "FA4B4B").withAlphaComponent(0.75)
dataSet.fillAlpha = 0.75
dataSet.drawFilledEnabled = true
dataSet.lineWidth = 0
dataSet.drawHighlightCircleEnabled = false
dataSet.setDrawHighlightIndicators(false)
let data = RadarChartData(dataSets: [dataSet])
data.setDrawValues(false)
chartView.data = data
}
}
extension ReportSpiderChart: IAxisValueFormatter {
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return labels[Int(value) % labels.count]
}
}
It seems that their is a spaceTop and spaceBottom property on axis, did you try to set them to 0 on both axis ?
https://github.com/danielgindi/Charts/blob/1bbec78109c7842d130d53ff8811bb6dbe865ba4/Source/Charts/Components/YAxis.swift#L72

PieChart using Charts on swift. Need to remove the values of the elements

In the black circles data that i wont to hide. I wonna to get simple green and red chat w\o text in elements.
I checked all the methods of PieChartView and wont find method to hide this data. Sims it integrated to elements...
Inplementation code:
import Charts
class ViewController: UIViewController {
#IBOutlet weak var pieChart: PieChartView!
override func viewDidLoad() {
super.viewDidLoad()
// pieChart.chartAnimator
// pieChart.drawCenterTextEnabled = false
// pieChart.drawHoleEnabled = false
let months = ["", ""]
let unitsSold = [60.0, 40.0]
setChart(dataPoints: months, values: unitsSold)
}
func setChart(dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
// let da = ChartDataEntry(
let dataEntry1 = ChartDataEntry(x: Double(0.0), y: 60.0)
let dataEntry2 = ChartDataEntry(x: Double(0.0), y: 40.0)
dataEntries.append(dataEntry1)
dataEntries.append(dataEntry2)
print(dataEntries[0].data as Any)
let pieChartDataSet = PieChartDataSet(entries: dataEntries, label: nil)
let pieChartData = PieChartData(dataSet: pieChartDataSet)
pieChart.data = pieChartData
let colors: [UIColor] = [UIColor(cgColor: UIColor.red.cgColor), UIColor(cgColor: UIColor.green.cgColor)]
pieChartDataSet.colors = colors
}
You need to set the text color as clear, it will work fine. Check out following change in your current code
let pieChartData = PieChartData(dataSet: pieChartDataSet)
pieChartData.setValueTextColor(NSUIColor.clear)
pieChart.data = pieChartData

Set labels at RadarChartData

I am using Charts v3.2.2 framework by danielgindi for iOS and macOS to draw a RadarChartView. There is a github repository that provides an example xcode project including Playgrounds. One is for RadarChartView.
Following the example I can set RadarChartData with two different RadarChartDataSets
var chartView = RadarChartView(frame: rect)
let data = RadarChartData(dataSets: [set1, set2])
chartView.data = data
It shows a chart like this:
The green labels set to x-axis are numbers from 0.0 to 4.0, but they should be string labels.
I cannot figure out how to set these labels that should be drawn around the RadarChart at the end of each web line. I guess it should be something like this:
data.setLabels("London", "Paris", "Berlin", "New York", "Tokio")
But this isn't working although it is a feature of class RadarChartData to set the desired labels.
Can somebody help me with that issue?
EDIT: complete code example
import Cocoa
import Charts
import PlaygroundSupport
let r = CGRect(x: 0, y: 0, width: 400, height: 400)
var chartView = RadarChartView(frame: r)
// General settings
chartView.webColor = NSUIColor.lightGray
chartView.innerWebColor = NSUIColor.lightGray
chartView.webAlpha = 1.0
// xAxis settings
let xAxis = chartView.xAxis
xAxis.xOffset = 0.0
xAxis.yOffset = 0.0
xAxis.labelTextColor = NSUIColor.green
xAxis.drawLabelsEnabled = true
// yAxis settings
let yAxis = chartView.yAxis
yAxis.labelCount = 5
yAxis.axisMinimum = 0.0
yAxis.axisMaximum = 80.0
yAxis.drawLabelsEnabled = true
// Legend settings
let legend = chartView.legend
// ... (irrelevant)
// Description
chartView.chartDescription?.enabled = true
chartView.chartDescription?.text = "Radar demo"
chartView.chartDescription?.textColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
// RadarChartDataEntry
let mult = 80.0
let min = 20.0
let cnt = 5
var entries1 = [RadarChartDataEntry]()
var entries2 = [RadarChartDataEntry]()
for i in 1...cnt
{
let values1 = (Double(arc4random_uniform(UInt32(mult))) + min)
entries1.append(RadarChartDataEntry(value: values1, data: "a" as AnyObject))
let values2 = (Double(arc4random_uniform(UInt32(mult))) + min)
entries2.append(RadarChartDataEntry(value: values2, data: "b" as AnyObject))
}
// RadarChartDataSet
let set1 = RadarChartDataSet(entries: entries1, label: "Last Week")
set1.drawFilledEnabled = true
set1.fillAlpha = 0.7
set1.lineWidth = 2.0
set1.drawHighlightCircleEnabled = true
set1.setDrawHighlightIndicators(false)
let set2 = RadarChartDataSet(entries: entries2, label: "This Week")
set2.drawFilledEnabled = true
set2.fillAlpha = 0.7
set2.lineWidth = 2.0
set2.drawHighlightCircleEnabled = true
set2.setDrawHighlightIndicators(false)
// RadarChartData
let data = RadarChartData(dataSets: [set1, set2])
data.setLabels("London", "Paris", "Berlin", "New York", "Tokio")
data.setDrawValues ( true )
data.setValueTextColor( NSUIColor.white )
chartView.data = data
chartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .easeInBounce)
// show chartView
PlaygroundPage.current.liveView = chartView
You need to override IAxisValueFormatter func stringForValue(_ value: Double, axis: AxisBase?) -> String {} function.
Like below:
Step1: Customize your xAxis with custom formatter.
let xValues = ["X1", "X2", "X3", "X4", "X5", "X6", "X7", "X8", "X9", "X10"]
let chartFormatter = RadarChartFormatter(labels: xValues)
let xAxis = XAxis()
xAxis.valueFormatter = chartFormatter
self.xAxis.valueFormatter = xAxis.valueFormatter
Step2: Implement Custom formatter with below method.
private class RadarChartFormatter: NSObject, IAxisValueFormatter {
var labels: [String] = []
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
if Int(value) < labels.count {
return labels[Int(value)]
}else{
return String("")
}
}
init(labels: [String]) {
super.init()
self.labels = labels
}
}
You will get below output in your RadarCharView:
Hope this will help you to get your custom labels on Radar chart!

SwiftChart has no member chart

I am using the SwiftCharts library found here.
I have the following code:
class ChartTestViewController: UIViewController {
#IBOutlet weak var chartView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let xaxis = ["monday", "tuesday", "wednesday", "thursday", "friday"]
let yaxis = [1, 2, 3, 4, 5]
let letdoublearray = yaxis.map { Double($0) }
let tuples = zip(xaxis, letdoublearray).map { ($0, $1) }
let chartConfig = BarsChartConfig(
valsAxisConfig: ChartAxisConfig(from: 0, to: 8, by: 2)
)
let frame = CGRect(x: 0, y: 70, width: 300, height: 500)
let chart = BarsChart(
frame: frame,
chartConfig: chartConfig,
xTitle: "X axis",
yTitle: "Y axis",
bars: tuples,
color: UIColor.red,
barWidth: 20
)
self.chart = chart // error here
self.chartView = chart.view
I am getting an error at the self.chart = chart line saying chart is not a member of the class. I already have chart defined so I am not sure where else I need to create another chart variable.
Your view controller does not have a chart property. Simply add one.
class ChartTestViewController: UIViewController {
var chart: BarsChart!
#IBOutlet weak var chartView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// lots of code here
self.chart = chart
}