How to remove static string "DataSet" from Legends of Pie Chart for danielgindi/Charts ios? - ios

I am using danielgindi/Charts for iOS/Swift. There is an extra Legend Entry with label "DataSet" displays in Pie Chart as seen in this image:
When I traced, I found there are two entries in the array of LegendEntry found in the PieChartView legend, i.e. PieChartView.legend.entries, where as I have only one object in my array.
Here is the code:
let dataSet = PieChartDataSet()
dataSet.drawIconsEnabled = false
dataSet.setColor(AppColors.selectedMenuItem)
dataSet.sliceSpace = 3
dataSet.iconsOffset = CGPoint(x: 0, y: 40)
dataSet.selectionShift = 5
var totalRevenuePer:Double = 0.0
_ = arrRevenue.map({ (objRevenue) -> Void in
if let percentage = Double(objRevenue.per ?? "0.0"), percentage != 0.0{
dataSet.append(PieChartDataEntry(value: percentage, label: "\((objRevenue.rev_center_name ?? "") + " " + objRevenue.revenue.currencyString())"))
totalRevenuePer += percentage
}
})
var colors = AppColors.TenderColors
if totalRevenuePer < 100{ colors.append(.clear) }
dataSet.colors = colors
let data = PieChartData(dataSet: dataSet)
data.setValueFormatter(PercentageFormatter())
data.setValueFont(NSUIFont.systemFont(ofSize: 11))
data.setValueTextColor(.white)
pieChart.data = data
pieChart.highlightValue(nil)
let legend = pieChart.legend
legend.textColor = .white
legend.entries.last?.label = ""
pieChart.animate(yAxisDuration: 1.4, easingOption: .easeInOutQuad)
// Refresh chart with new data
pieChart.notifyDataSetChanged()
Appreciate any help, thank you.

It's a property of PieChartDataSet
The default value, if you don't set your own, is "DataSet"
let dataSet = PieChartDataSet()
// provide your own
dataSet.label = "My Label"
// or, no label
dataSet.label = ""

Related

How can I fix error multiple colors of BarChart in Swift?

I have a bar chart to show item using Chart library. I got an error that the explain description show multiple colors for each item. Please help me how can i fix this, i want to show exactly the color like column (only one color), not multiple like this. Thanks for your helping.
My Screen
Here is my code, note that items is dynamic, i got from API:
func setDataCount(items: [Item]) {
let dataEntry = (0..<items.count).map { (i) -> ChartDataEntry in
let value = Double(items[i].soldQuantity)
return BarChartDataEntry(x: Double(i), y: value)
}
var arrayDataSet = [IChartDataSet]()
var arrayColor = [UIColor]()
var set = BarChartDataSet()
for i in 0...items.count - 1 {
set = BarChartDataSet(entries: dataEntry, label: items[i].name)
arrayColor.append(UIColor.random)
set.colors = arrayColor
// CONVERT DOUBLE -> INT
set.valueFormatter = DefaultValueFormatter(decimals: 0)
arrayDataSet.append(set)
}
let data = BarChartData(dataSets: arrayDataSet)
data.setValueFont(.systemFont(ofSize: 10, weight: .bold))
data.barWidth = 0.2
chartView.data = data
}

Is there a way to estimate where a ChartLimitLine and a LineChartDataSet intersect on a graph?

I am using the Charts pod library in my app. I have a limit line that intersects a LineChartDataSet and I would like to estimate at what x-value the intersection takes place. I could not find a specific function for this in the library (unless I am missing something), and so I am wondering what the best way to go about trying to do this is?
I am creating the limit line as follows:
let line2 = ChartLimitLine(limit: 0, label: "Break-Even")
line2.lineColor = .lightGray
line2.valueTextColor = .lightGray
line2.labelPosition = .topRight
line2.lineWidth = 1
line2.lineDashLengths = [3]
chartView.leftAxis.addLimitLine(line2)
And this is how I am adding my data to my chart (there are two series, two lines).
var todayDataValues : [ChartDataEntry] = [ ]
var expiryDataValues : [ChartDataEntry] = [ ]
for (index, item) in todayValues.enumerated() {
todayDataValues.append(ChartDataEntry(x: doubleSpots[index], y: item))
}
for (index, item2) in expiryValues.enumerated() {
expiryDataValues.append(ChartDataEntry(x: doubleSpots[index], y: item2))
}
let set1 = LineChartDataSet(entries: todayDataValues, label: "Today")
set1.mode = .linear set1.drawCirclesEnabled = false
set1.setColor(UIColor(named: "TodayLine")!) set1.lineWidth = 3
set1.highlightColor = .systemGray let set2 = LineChartDataSet(entries: expiryDataValues, label: self.selectedExpiry) set2.mode = .linear
set2.setColor(UIColor(named: "ExpiryLine")!) set2.lineWidth = 3
set2.drawCirclesEnabled = false set2.highlightColor = .systemGray let data = LineChartData(dataSets: [set1, set2]) data.setDrawValues(false)
self.lineChartView.data = data
Neither data set has an x value of 0 (where my limit line is), but I am asking how I can make a calculation based on the equation of the line drawn between the two points that does intersect 0 - what the x-value might be.

Ios Swift PieChart

How can I display the legend values in ios pie chart? I was able to get only the non zero values. Can anyone help?
func updateChartData12() {
let chart = PieChartView(frame: self.view.frame)
// 2. generate chart data entries
let track = ["Income", "Expense", "Wallet", "Bank"]
let money = [650, 456.13, 0, 0]
var entries = [PieChartDataEntry]()
for (index, value) in money.enumerated() {
let entry = PieChartDataEntry()
if value != 0 {
entry.y = value
entry.label = track[index]
entries.append( entry)
}
}
// 3. chart setup
let set = PieChartDataSet( values: entries, label: "")
// this is custom extension method. Download the code for more details.
var colors: [UIColor] = []
for _ in 0..<money.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)
}
set.colors = colors
let data = PieChartData(dataSet: set)
chart.data = data
chart.noDataText = "No data available"
// user interaction
let d = Description()
chart.chartDescription = d
chart.centerText = "Pie Chart"
chart.holeRadiusPercent = 0.2
chart.transparentCircleColor = UIColor.clear
self.view.addSubview(chart)
}
I am using this code..But I need to display total y values i.e legend at the bottom
Your pie chart is greater than 100%. The data I have is intended to show a total that is over 100% and That the reason it's not work.
let track = ["Income", "Expense", "Wallet", "Bank"]
let money = [650, 456.13, 0, 0]
So use following money array if it's work fine then you would like to check is there build in method to ensure the sum of percentages in the pie chart is 100%
let money = [65, 35, 0, 0]

Many Gradient Fill in same lineChart Swift for iOS-Charts

let gradientColors = [UIColor.cyanColor().CGColor, UIColor.clearColor().CGColor] // Colors of the gradient
let colorLocations:[CGFloat] = [1.0, 0.0] // Positioning of the gradient
let gradient = CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), gradientColors, colorLocations) // Gradient Object
set.fill = ChartFill.fillWithLinearGradient(gradient!, angle: 90.0) // Set the Gradient
set.drawFilledEnabled = true // Draw the Gradient
Result
I want to have many fill depending of xAxis value like this :
It's possible to do this ??
Solution:
The solution is to use many dataset, from the second one, the first value for each data need to be same the last value of the previous dataset so at the end it will look like you have one dataset
ex:
let dollars1 = [1453.0,2352,5431,1442] // last value = first value of next dataset
let dollars2 = [1442.0,2234,8763,4453] // last value = first value of next dataset
let dollars3 = [4453.0,3456,7843,5678] // last value = first value of next dataset
func setChartData(months : [String]) {
var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
for var i = 0; i < months.count; i++ {
yVals1.append(ChartDataEntry(value: dollars1[i], xIndex: i))
}
let set1: LineChartDataSet = LineChartDataSet(yVals: yVals1, label: "First Set")
set1.axisDependency = .Left // Line will correlate with left axis values
set1.setColor(UIColor.redColor().colorWithAlphaComponent(0.5))
set1.setCircleColor(UIColor.redColor())
set1.lineWidth = 2.0
set1.circleRadius = 6.0
set1.fillAlpha = 1
set1.fillColor = UIColor.redColor()
set1.drawFilledEnabled = true
var yVals2 : [ChartDataEntry] = [ChartDataEntry]()
for var i = 0; i < months.count; i++ {
yVals2.append(ChartDataEntry(value: dollars2[i], xIndex: i))
}
let set2: LineChartDataSet = LineChartDataSet(yVals: yVals2, label: "Second Set")
set2.axisDependency = .Left // Line will correlate with left axis values
set2.setColor(UIColor.greenColor().colorWithAlphaComponent(0.5))
set2.setCircleColor(UIColor.greenColor())
set2.lineWidth = 2.0
set2.circleRadius = 6.0
set2.fillAlpha = 1
set2.fillColor = UIColor.greenColor()
set2.drawFilledEnabled = true
var yVals3 : [ChartDataEntry] = [ChartDataEntry]()
for var i = 0; i < months.count; i++ {
yVals3.append(ChartDataEntry(value: dollars3[i], xIndex: i))
}
let set3: LineChartDataSet = LineChartDataSet(yVals: yVals3, label: "Second Set")
set3.axisDependency = .Left // Line will correlate with left axis values
set3.setColor(UIColor.blueColor().colorWithAlphaComponent(0.5))
set3.setCircleColor(UIColor.blueColor())
set3.lineWidth = 2.0
set3.circleRadius = 6.0
set3.fillAlpha = 65 / 255.0
set3.fillColor = UIColor.blueColor()
set3.drawFilledEnabled = true
//3 - create an array to store our LineChartDataSets
var dataSets : [LineChartDataSet] = [LineChartDataSet]()
dataSets.append(set1)
dataSets.append(set2)
dataSets.append(set3)
//4 - pass our months in for our x-axis label value along with our dataSets
let data: LineChartData = LineChartData(xVals: months, dataSets: dataSets)
data.setValueTextColor(UIColor.whiteColor())
//5 - finally set our data
self.lineChartView.data = data
}
Yes its possible to do that gradient fill you want ,just replace below line and add some color so you can check.
set.fill = ChartFill.fillWithLinearGradient(gradient!, angle: 90.0)
Set the angle 180 so it will work
set.fill = ChartFill.fillWithLinearGradient(gradient!, angle: 180.0)

Multiple colours to Bar Charts Swift one above the other

I am using CHARTS library in swift.
https://github.com/danielgindi/Charts
I got suceess in creating Pie chart and bar chart.
Now I want to create bar chart with one "x" value and 2 "y" values.
I created the bar chart with two different values.
But I am not able to set the colours for them
I passed the colour array to delegate method setColors but it takes the last colour from the array.
Can any one please guide me for having different colours on bar chart.
Here is my code to call BarChart library
months = ["HR","Operations","iOS","Android","PHP","Support","Testing","Designer"]
let unitsSold = [10.0,20.0,30.0,20.0,20.0,10.0,15.0,20.0]
let unitsSold1 = [20.0,30.0,50.0,40.0,30.0,20.0,45.0,50.0]
let barChartClass = self.storyboard!.instantiateViewControllerWithIdentifier("BarChartViewControllerID") as! BarChartViewController
self.addChildViewController(barChartClass)
barChartClass.view.frame = CGRectMake(20, 20, 740, 900)
barChartClass.barChartView.doubleTapToZoomEnabled = false
barChartClass.barChartView.setScaleEnabled(false)
self.view.addSubview(barChartClass.view)
barChartClass.barChartView.legend.enabled = false
barChartClass.setChartBarGroupDataSet(months, values: unitsSold, values2: unitsSold1, sortIndex: 0)
And this is the method call for setChartBarGroupDataSet
func setChartBarGroupDataSet(dataPoints: [String], values: [Double], values2: [Double],sortIndex:Int)
{
var dataEntries: [BarChartDataEntry] = []
for i in 0..<dataPoints.count
{
let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
let dataEntry1 = BarChartDataEntry(value: values2[i], xIndex: i)
dataEntries.append(dataEntry)
dataEntries.append(dataEntry1)
}
let chartDataSet = BarChartDataSet(yVals: dataEntries, label: " ")
let COLOR_SET : [UIColor]!
COLOR_SET = [UIColor.redColor(),UIColor.greenColor()]
chartDataSet.setColors(COLOR_SET, alpha: 1.0)
let dataSets: [BarChartDataSet] = [chartDataSet]
let data = BarChartData(xVals: dataPoints, dataSets: dataSets)
barChartView.data = data
barChartView.descriptionText = ""
barChartView.rightAxis.drawGridLinesEnabled = false
barChartView.rightAxis.drawAxisLineEnabled = false
barChartView.rightAxis.drawLabelsEnabled = false
barChartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .EaseInBounce)
}
What I want is something like this
Thanks in advance.
This is a stacked bar chart. In Charts' demos, you can see a stacked bar chart.
Basically, each BarChartDataEntry received either one value, or an array of values. Pass in an array of values - and you are good to go!
let entry = BarChartDataEntry(values: [4.6, 3.8], xIndex: 0)
dataset.colors = [UIColor.cyanColor(), UIColor.greenColor()]
dataset.stackLabels = ["Inactive inventory", "Active inventory"]
Answered in the duplicate issue too: https://github.com/danielgindi/Charts/issues/1324

Resources