Hi all i need help i want to format BarChartDataEntry labels as shown in image.
My Code
var dataEntries: [BarChartDataEntry] = []
var dataEntriesBottom: [BarChartDataEntry] = []
for i in 0..<goals.count {
let dataEntry = BarChartDataEntry(x: Double(i), y: Double(goals[i]))
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(entries: dataEntries, label: "")
chartDataSet.colors = [.systemGreen]
let chartData = BarChartData(dataSet: chartDataSet)
chartData.barWidth = 0.4
chartView.xAxis.drawAxisLineEnabled = false
chartView.leftAxis.drawAxisLineEnabled = false
chartView.xAxis.drawGridLinesEnabled = false
chartView.rightAxis.drawAxisLineEnabled = false
chartView.leftAxis.drawLabelsEnabled = false
chartView.rightAxis.drawLabelsEnabled = false
chartView.leftAxis.drawGridLinesEnabled = false
chartView.rightAxis.drawGridLinesEnabled = false
chartView.xAxis.enabled = false
chartView.xAxis.drawLabelsEnabled = false
chartView.xAxis.granularity = 0
self.chartView.animate(yAxisDuration: 1.0)
self.chartView.legend.enabled = false
chartView.data = chartData
try something like this:
let myFormatter = NumberFormatter()
myFormatter.numberStyle = .decimal
myFormatter.maximumFractionDigits = 0
chartData.setValueFormatter(DefaultValueFormatter(formatter: myFormatter))
Related
I have created lineChartView. Below xAxis values are displayed hours with 1 hour time interval. My graph starts at 0 and I am not able to show proper xAxis label value.
I want to put extra space in xAxis starting so that the xAxis label can proper visible.
Here is my code:
func setChart(yValues: [Double], xValues: [String]) {
chartView.noDataText = "You need to provide data for the chart."
var dataEntries: [ChartDataEntry] = []
for i in 0..<yValues.count {
let dataEntry = ChartDataEntry(x: Double(i), y: yValues[i])
dataEntries.append(dataEntry)
}
let chartDataSet = LineChartDataSet(entries: dataEntries, label: "")
chartDataSet.colors = [.black]
chartDataSet.drawFilledEnabled = true
let chartData = LineChartData(dataSet: chartDataSet)
let chartFormatter = LineChartFormatter(labels: xValues)
chartView.xAxis.valueFormatter = chartFormatter
chartView.xAxis.labelCount = dataEntries.count
chartView.data = chartData
chartView.xAxis.granularity = 1.0
chartView.xAxis.granularityEnabled = true
chartView.xAxis.centerAxisLabelsEnabled = false
chartView.xAxis.axisMinimum = 0
chartView.xAxis.axisMaximum = Double(dataEntries.count)
chartView.setVisibleXRange(minXRange: 1.0, maxXRange: 3.0)
}
I am trying to achieve the functionality to show the win and loss percent of every month parallel to the bar. Please provide any suggestion.
I am using danielgindi/Charts
I am trying to achive the this functinality.
here is the chart setup code:
chartView.legend.enabled = true
chartView.chartDescription?.enabled = false
chartView.maxVisibleCount = 12
chartView.drawBarShadowEnabled = false
chartView.drawValueAboveBarEnabled = false
chartView.delegate = self
chartView.dragEnabled = true
chartView.doubleTapToZoomEnabled = false
chartView.pinchZoomEnabled = true
chartView.noDataTextColor = .black
chartView.highlightFullBarEnabled = true
chartView.drawGridBackgroundEnabled = false
chartView.leftAxis.enabled = false
chartView.leftAxis.drawGridLinesEnabled = false
chartView.rightAxis.enabled = false
chartView.rightAxis.drawGridLinesEnabled = false
chartView.xAxis.enabled = true
chartView.xAxis.drawGridLinesEnabled = false
chartView.xAxis.drawAxisLineEnabled = false
chartView.xAxis.labelPosition = .bottom
chartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: months)
chartView.xAxis.setLabelCount(months.count, force: false)
chartView.xAxis.granularity = 1
let l = chartView.legend
l.horizontalAlignment = .center
l.verticalAlignment = .bottom
l.orientation = .horizontal
l.drawInside = false
l.xEntrySpace = 5
l.yEntrySpace = 2
l.yOffset = 5
l.xOffset = 20
here i am assigning the value to chart
let yVals = (0..<data.count).map { (i) -> BarChartDataEntry in
let val1 = Double(data[i].ownopportunityOwn.roundToOneDigitCroreValue)
let val2 = Double(data[i].ownopportunityAssistby.roundToOneDigitCroreValue)
return BarChartDataEntry(x: Double(i), yValues: [val1, val2])
}
let set = BarChartDataSet(values: yVals, label: "")
set.drawIconsEnabled = false
set.colors = [AppColors.Graph_LightGreenColor,AppColors.Graph_PurpleColor]
set.roundedCorners = [.topLeft,.topRight]
let data = BarChartData(dataSet: set)
data.setValueFont(.systemFont(ofSize: 7, weight: .bold))
data.setValueFormatter(DefaultValueFormatter(formatter: formatter))
data.setValueTextColor(.white)
chartView.fitBars = true
chartView.data = data
var array = [LegendEntry]()
var legendValueArray = [(title: DashboardConstant.K_OWN_WINS.value, Color: AppColors.Graph_LightGreenColor),(title: DashboardConstant.K_ASSISTED_BY_WIN.value, Color: AppColors.Graph_PurpleColor)]
for object in legendValueArray {
array.append( LegendEntry(label: object.title, form: .square, formSize: 10, formLineWidth: 1, formLineDashPhase: 1, formLineDashLengths: nil, formColor: object.Color))
}
self.chartView.legend.setCustom(entries: array)
self.chartView.fitScreen()
self.chartView.notifyDataSetChanged()
chartView.animate(xAxisDuration: 3, yAxisDuration: 3)
I'm using Charts to display a bar graph but, I cannot get the x-Axis to print out with the days of the week instead of: 0,1,2,3, etc.
Initializing chart:
var weekdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
func initializeChart() {
chartView.noDataText = "No Data"
chartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .easeInBounce)
chartView.xAxis.labelPosition = .bottom
chartView.chartDescription.text = "Revenue"
//chartView.xAxis.setLabelsToSkip(0)
chartView.legend.enabled = false
chartView.scaleYEnabled = false
chartView.scaleXEnabled = false
chartView.pinchZoomEnabled = false
chartView.doubleTapToZoomEnabled = false
chartView.leftAxis.axisMinimum = 0.0
chartView.leftAxis.axisMaximum = 100.00
chartView.highlighter = nil
chartView.rightAxis.enabled = false
chartView.xAxis.drawGridLinesEnabled = false
}
This is the data I'm populating:
if json != nil {
let week = json["week"]
let month = json["month"]
print("Check", week, month)
var dataEntries: [BarChartDataEntry] = []
for i in 0..<self.weekdays.count {
let day = self.weekdays[i]
let dataEntry = BarChartDataEntry(x: Double(i), yValues: [(week[day].double)!])
dataEntries.append(dataEntry)
print("data entry", dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Weekdays")
chartDataSet.colors = ChartColorTemplates.material()
let chartData = BarChartData(dataSet: chartDataSet)
self.chartView.data = chartData
}
I just want to print the days of the week on the x-Axis. Could you help?
You need to set valueFormatter for X-axis, try this code. hope it helps!
if json != nil {
let week = json["week"]
let month = json["month"]
print("Check", week, month)
var dataEntries: [BarChartDataEntry] = []
for i in 0..<self.weekdays.count {
let day = self.weekdays[i]
let dataEntry = BarChartDataEntry(x: Double(i), yValues:
[(week[day].double)!] , data: weekdays as AnyObject?))
dataEntries.append(dataEntry)
print("data entry", dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Weekdays")
chartDataSet.colors = ChartColorTemplates.material()
let chartData = BarChartData(dataSet: chartDataSet)
let xAxisValue = chartView.xAxis
xAxisValue.valueFormatter = axisFormatDelegate
self.chartView.data = chartData
}
extension DemoBaseViewController: IAxisValueFormatter {
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return weekdays[Int(value)]
}
}
In swift 3 I did this radarChart
But I didn't find how to enable zoom on it and I didn't find if this is even possible.
Here is the code that generated it :
func Radar() {
radarVw.xAxis.valueFormatter = self
var dataEntries: [RadarChartDataEntry] = []
for i in 0..<(useri?.skills!)!.count {
let dataEntry = RadarChartDataEntry(value: (useri?.skills!)![i]["level"].double!)
dataEntries.append(dataEntry)
skillsName.append((useri?.skills!)![i]["name"].string!)
}
let chartDataSet = RadarChartDataSet(values: dataEntries, label: "skills")
let chartData = RadarChartData(dataSet: chartDataSet)
radarVw.xAxis.labelHeight = 0.5
radarVw.xAxis.labelWidth = 0.5
radarVw.legend.enabled = false
radarVw.xAxis.wordWrapEnabled = true
radarVw.data = chartData
radarVw.chartDescription?.text = ""
}
I have an iOS Chart, as per the image below, and the labels are showing incorrectly (or not what I want).
As you can see they both say 'Games Played' and are the same colour, as is each bar in my graph.
How can I change the colour of each bar and show the correct respective labels, 'Won', 'Drawn', 'Lost'?
This is my code:
override func viewDidLoad() {
super.viewDidLoad()
let results = ["Won", "Drawn", "Lost"]
let games = [totalWins, totalDraws, totalLosses]
setChart(dataPoints: results, values: games)
}
func setChart(dataPoints: [String], values: [Double]){
let formato:BarChartFormatter = BarChartFormatter()
let xaxis:XAxis = XAxis()
barChartView.noDataText = "you need to provide some data for the chart."
var dataEntries: [BarChartDataEntry] = Array()
for i in 0..<dataPoints.count {
let dataEntry = BarChartDataEntry(x: Double(i), y: values[i])
dataEntries.append(dataEntry)
formato.stringForValue (Double(i), axis: xaxis)
}
xaxis.valueFormatter = formato
barChartView.xAxis.valueFormatter = xaxis.valueFormatter
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Games Played")
let chartData = BarChartData(dataSets: [chartDataSet])
chartData.addDataSet(chartDataSet)
barChartView.data = chartData
barChartView.leftAxis.forceLabelsEnabled = true
barChartView.rightAxis.forceLabelsEnabled = true
barChartView.xAxis.granularityEnabled = true
barChartView.xAxis.granularity = 1.0
barChartView.leftAxis.drawGridLinesEnabled = false
barChartView.rightAxis.drawGridLinesEnabled = false
barChartView.xAxis.drawGridLinesEnabled = false
barChartView.rightAxis.enabled = false
barChartView.drawGridBackgroundEnabled = false
self.barChartView.xAxis.labelPosition = XAxis.LabelPosition.bottom
}
Simply add chartDataSet.colors = ChartColorTemplates.material() or you can add your array of UIColor in order to customise according to your need.